diff --git a/configs/my_example.yaml b/configs/my_example.yaml new file mode 100644 index 000000000..577623469 --- /dev/null +++ b/configs/my_example.yaml @@ -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 diff --git a/paddlevideo/loader/dataset/__init__.py b/paddlevideo/loader/dataset/__init__.py index 451fdd839..3a70303aa 100644 --- a/paddlevideo/loader/dataset/__init__.py +++ b/paddlevideo/loader/dataset/__init__.py @@ -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' ] diff --git a/paddlevideo/loader/dataset/example_dataset.py b/paddlevideo/loader/dataset/example_dataset.py new file mode 100644 index 000000000..213437467 --- /dev/null +++ b/paddlevideo/loader/dataset/example_dataset.py @@ -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] diff --git a/paddlevideo/loader/pipelines/__init__.py b/paddlevideo/loader/pipelines/__init__.py index 99254e6b6..02bd4f3d8 100644 --- a/paddlevideo/loader/pipelines/__init__.py +++ b/paddlevideo/loader/pipelines/__init__.py @@ -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' ] diff --git a/paddlevideo/loader/pipelines/example_pipeline.py b/paddlevideo/loader/pipelines/example_pipeline.py new file mode 100644 index 000000000..552816657 --- /dev/null +++ b/paddlevideo/loader/pipelines/example_pipeline.py @@ -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 diff --git a/paddlevideo/modeling/backbones/__init__.py b/paddlevideo/modeling/backbones/__init__.py index 90984a619..5dc1df6ad 100644 --- a/paddlevideo/modeling/backbones/__init__.py +++ b/paddlevideo/modeling/backbones/__init__.py @@ -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' ] diff --git a/paddlevideo/modeling/backbones/example_backbone.py b/paddlevideo/modeling/backbones/example_backbone.py new file mode 100644 index 000000000..047ee0900 --- /dev/null +++ b/paddlevideo/modeling/backbones/example_backbone.py @@ -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 diff --git a/paddlevideo/modeling/framework/recognizers/__init__.py b/paddlevideo/modeling/framework/recognizers/__init__.py index 724176f3c..87b7d7e72 100644 --- a/paddlevideo/modeling/framework/recognizers/__init__.py +++ b/paddlevideo/modeling/framework/recognizers/__init__.py @@ -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' ] diff --git a/paddlevideo/modeling/framework/recognizers/example_recognizer.py b/paddlevideo/modeling/framework/recognizers/example_recognizer.py new file mode 100644 index 000000000..f27ca7cc3 --- /dev/null +++ b/paddlevideo/modeling/framework/recognizers/example_recognizer.py @@ -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 diff --git a/paddlevideo/modeling/heads/__init__.py b/paddlevideo/modeling/heads/__init__.py index 8e1b24d16..514cff8e5 100644 --- a/paddlevideo/modeling/heads/__init__.py +++ b/paddlevideo/modeling/heads/__init__.py @@ -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' ] diff --git a/paddlevideo/modeling/heads/example_head.py b/paddlevideo/modeling/heads/example_head.py new file mode 100644 index 000000000..71ea8ed40 --- /dev/null +++ b/paddlevideo/modeling/heads/example_head.py @@ -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 diff --git a/run.sh b/run.sh index 3d05714da..6455e5c2d 100644 --- a/run.sh +++ b/run.sh @@ -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 @@ -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