Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions configs/my_example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# PaddleVieo Example Configuration
MODEL:
framework: "ExampleRecognizer"
backbone:
name: "ExampleBackbone"
head:
name: "ExampleHead"

DATASET:
batch_size: 10
num_workers: 0
train:
format: "ExampleDataset"

PIPELINE:
train:
norm:
name: "ExamplePipeline" # NOTE: indent

OPTIMIZER:
name: 'Adam'
learning_rate:
name: 'StepDecay'
learning_rate: 0.001
step_size: 10


model_name: "ExampleModel"
log_interval: 1
epochs: 5
3 changes: 2 additions & 1 deletion paddlevideo/loader/dataset/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
from .bmn_dataset import BMNDataset
from .feature import FeatureDataset
from .skeleton import SkeletonDataset
from .example_dataset import ExampleDataset

__all__ = [
'VideoDataset', 'FrameDataset', 'SFVideoDataset', 'BMNDataset',
'FeatureDataset', 'SkeletonDataset'
'FeatureDataset', 'SkeletonDataset', 'ExampleDataset'
]
41 changes: 41 additions & 0 deletions paddlevideo/loader/dataset/example_dataset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import copy
import numpy as np

from ..registry import DATASETS
from .base import BaseDataset


@DATASETS.register()
class ExampleDataset(BaseDataset):
""" ExampleDataset """
def __init__(self, pipeline, file_path=''):
super().__init__(file_path, pipeline)

def load_file(self):
"""load file, abstractmethod"""
self.x = np.random.rand(100, 20, 20)
self.y = np.random.randint(10, size=(100, 1))

def prepare_train(self, idx):
"""Prepare the frames for training/valid given index. """
results = {'data': self.x[idx], 'label': self.y[idx]}
results = self.pipeline(results)
return results['data'], results['label']

def __len__(self):
"""get the size of the dataset."""
return self.x.shape[0]
3 changes: 2 additions & 1 deletion paddlevideo/loader/pipelines/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
from .mix import Cutmix, Mixup
from .sample import Sampler
from .skeleton_pipeline import AutoPadding, Iden, SkeletonNorm
from .example_pipeline import ExamplePipeline

__all__ = [
'Scale', 'RandomCrop', 'CenterCrop', 'RandomFlip', 'Image2Array',
'Normalization', 'Compose', 'VideoDecoder', 'FrameDecoder', 'Sampler',
'Mixup', 'Cutmix', 'JitterScale', 'MultiCrop', 'PackOutput', 'TenCrop',
'UniformCrop', 'DecodeSampler', 'LoadFeat', 'GetMatchMap', 'GetVideoLabel',
'AutoPadding', 'SkeletonNorm', 'Iden'
'AutoPadding', 'SkeletonNorm', 'Iden', 'ExamplePipeline'
]
31 changes: 31 additions & 0 deletions paddlevideo/loader/pipelines/example_pipeline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from ..registry import PIPELINES


@PIPELINES.register()
class ExamplePipeline(object):
"""Example Pipeline """
def __init__(self, mean=0, std=1.0):
self.mean = mean
self.std = std

def __call__(self, results):
data = results['data']
label = results['label']
norm_data = (data - self.mean) / self.std
results['data'] = norm_data.astype('float32')
results['label'] = label.astype('int64')
return results
3 changes: 2 additions & 1 deletion paddlevideo/modeling/backbones/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
from .vit import VisionTransformer
from .stgcn import STGCN
from .agcn import AGCN
from .example_backbone import ExampleBackbone

__all__ = [
'ResNet', 'ResNetTSM', 'ResNetTweaksTSM', 'ResNetSlowFast', 'BMN',
'ResNetTweaksTSN', 'VisionTransformer', 'STGCN', 'AGCN'
'ResNetTweaksTSN', 'VisionTransformer', 'STGCN', 'AGCN', 'ExampleBackbone'
]
39 changes: 39 additions & 0 deletions paddlevideo/modeling/backbones/example_backbone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import paddle
import paddle.nn as nn
import paddle.nn.functional as F

from ..registry import BACKBONES


@BACKBONES.register()
class ExampleBackbone(nn.Layer):
"""Example Backbone"""
def __init__(self):
super(ExampleBackbone, self).__init__()
## 2.1 网络Backbobe
self.layer1 = paddle.nn.Flatten(1, -1)
self.layer2 = paddle.nn.Linear(400, 512)
self.layer3 = paddle.nn.ReLU()
self.layer4 = paddle.nn.Dropout(0.2)

def forward(self, x):
""" model forward"""
y = self.layer1(x)
y = self.layer2(y)
y = self.layer3(y)
y = self.layer4(y)
return y
3 changes: 2 additions & 1 deletion paddlevideo/modeling/framework/recognizers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
from .recognizer3d import Recognizer3D
from .recognizer_transformer import RecognizerTransformer
from .recognizer_gcn import RecognizerGCN
from .example_recognizer import ExampleRecognizer

__all__ = [
'BaseRecognizer', 'Recognizer1D', 'Recognizer2D', 'Recognizer3D',
'RecognizerTransformer', 'RecognizerGCN'
'RecognizerTransformer', 'RecognizerGCN', 'ExampleRecognizer'
]
34 changes: 34 additions & 0 deletions paddlevideo/modeling/framework/recognizers/example_recognizer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

from ...registry import RECOGNIZERS
from .base import BaseRecognizer
import paddle


@RECOGNIZERS.register()
class ExampleRecognizer(BaseRecognizer):
"""Example Recognizer model framework."""
def forward_net(self, imgs):
"""model forward method"""
feature = self.backbone(imgs)
cls_score = self.head(feature)
return cls_score

def train_step(self, data_batch):
"""Define how the model is going to train, from input to output.
"""
imgs = data_batch[0]
labels = data_batch[1:]
cls_score = self.forward_net(imgs)
loss_metrics = self.head.loss(cls_score, labels)
return loss_metrics
3 changes: 2 additions & 1 deletion paddlevideo/modeling/heads/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
from .attention_lstm_head import AttentionLstmHead
from .timesformer_head import TimeSformerHead
from .stgcn_head import STGCNHead
from .example_head import ExampleHead

__all__ = [
'BaseHead', 'TSNHead', 'TSMHead', 'ppTSMHead', 'ppTSNHead', 'SlowFastHead',
'AttentionLstmHead', 'TimeSformerHead', 'STGCNHead'
'AttentionLstmHead', 'TimeSformerHead', 'STGCNHead', 'ExampleHead'
]
31 changes: 31 additions & 0 deletions paddlevideo/modeling/heads/example_head.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import paddle
import paddle.nn as nn
from ..registry import HEADS
from .base import BaseHead


@HEADS.register()
class ExampleHead(BaseHead):
""" Example Head """
def __init__(self, num_classes=10, in_channels=512):
super().__init__(num_classes, in_channels)
self.head = paddle.nn.Linear(in_channels, num_classes)

def forward(self, x):
"""model forward """
y = self.head(x)
return y
5 changes: 4 additions & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7

start_time=$(date +%s)

# run example model
python3.7 main.py -c configs/my_example.yaml

# run tsm training
#python3.7 -B -m paddle.distributed.launch --gpus="0,1,2,3,4,5,6,7" --log_dir=log_tsm main.py --validate -c configs/recognition/tsm/tsm_k400_frames.yaml

Expand All @@ -32,7 +35,7 @@ start_time=$(date +%s)
#python3.7 -B -m paddle.distributed.launch --gpus="0,1,2,3,4,5,6,7" --log_dir=log_attetion_lstm main.py --validate -c configs/recognition/attention_lstm/attention_lstm_youtube-8m.yaml

# run pp-tsm training
python3.7 -B -m paddle.distributed.launch --gpus="0,1,2,3,4,5,6,7" --log_dir=log_pptsm main.py --validate -c configs/recognition/pptsm/pptsm_k400_frames_uniform.yaml
#python3.7 -B -m paddle.distributed.launch --gpus="0,1,2,3,4,5,6,7" --log_dir=log_pptsm main.py --validate -c configs/recognition/pptsm/pptsm_k400_frames_uniform.yaml

# run pp-tsn training
# python3.7 -B -m paddle.distributed.launch --gpus="0,1,2,3,4,5,6,7" --log_dir=log_pptsn main.py --validate -c configs/recognition/pptsn/pptsn_k400_frames.yaml
Expand Down