-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fff82ed
commit 4783235
Showing
6 changed files
with
214 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# Copyright (c) 2020 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 random | ||
import numpy as np | ||
from ..registry import PIPELINES | ||
|
||
|
||
@PIPELINES.register() | ||
class Mixup(object): | ||
""" | ||
Mixup operator. | ||
Args: | ||
alpha(float): alpha value. | ||
""" | ||
def __init__(self, alpha=0.2): | ||
assert alpha > 0., \ | ||
'parameter alpha[%f] should > 0.0' % (alpha) | ||
self.alpha = alpha | ||
|
||
def __call__(self, batch): | ||
imgs, labels = batch | ||
bs = len(batch) | ||
idx = np.random.permutation(bs) | ||
lam = np.random.beta(self.alpha, self.alpha) | ||
lams = np.array([lam] * bs, dtype=np.float32) | ||
imgs = lam * imgs + (1 - lam) * imgs[idx] | ||
return [imgs, labels, labels[idx], lams] | ||
|
||
|
||
@PIPELINES.register() | ||
class Cutmix(object): | ||
""" Cutmix operator | ||
Args: | ||
alpha(float): alpha value. | ||
""" | ||
def __init__(self, alpha=0.2): | ||
assert alpha > 0., \ | ||
'parameter alpha[%f] should > 0.0' % (alpha) | ||
self.alpha = alpha | ||
|
||
def rand_bbox(self, size, lam): | ||
""" rand_bbox """ | ||
w = size[2] | ||
h = size[3] | ||
cut_rat = np.sqrt(1. - lam) | ||
cut_w = np.int(w * cut_rat) | ||
cut_h = np.int(h * cut_rat) | ||
|
||
# uniform | ||
cx = np.random.randint(w) | ||
cy = np.random.randint(h) | ||
|
||
bbx1 = np.clip(cx - cut_w // 2, 0, w) | ||
bby1 = np.clip(cy - cut_h // 2, 0, h) | ||
bbx2 = np.clip(cx + cut_w // 2, 0, w) | ||
bby2 = np.clip(cy + cut_h // 2, 0, h) | ||
|
||
return bbx1, bby1, bbx2, bby2 | ||
|
||
def __call__(self, batch): | ||
imgs, labels, bs = self._unpack(batch) | ||
idx = np.random.permutation(bs) | ||
lam = np.random.beta(self.alpha, self.alpha) | ||
|
||
bbx1, bby1, bbx2, bby2 = self.rand_bbox(imgs.shape, lam) | ||
imgs[:, :, bbx1:bbx2, bby1:bby2] = imgs[idx, :, bbx1:bbx2, bby1:bby2] | ||
lam = 1 - (float(bbx2 - bbx1) * (bby2 - bby1) / | ||
(imgs.shape[-2] * imgs.shape[-1])) | ||
lams = np.array([lam] * bs, dtype=np.float32) | ||
|
||
return [imgs, labels, labels[idx], lams] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Copyright (c) 2020 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 argparse | ||
import os | ||
import sys | ||
import os.path as osp | ||
|
||
import paddle | ||
import paddle.nn.functional as F | ||
from paddle.jit import to_static | ||
|
||
__dir__ = os.path.dirname(os.path.abspath(__file__)) | ||
sys.path.append(os.path.abspath(os.path.join(__dir__, '../'))) | ||
|
||
from paddlevideo.modeling.builder import build_model | ||
from paddlevideo.utils import get_config | ||
|
||
|
||
def parse_args(): | ||
|
||
parser = argparse.ArgumentParser("PaddleVideo Summary") | ||
parser.add_argument('-c', | ||
'--config', | ||
type=str, | ||
default='configs/example.yaml', | ||
help='config file path') | ||
|
||
parser.add_argument("--img_size", type=int, default=224) | ||
|
||
return parser.parse_args() | ||
|
||
|
||
def _trim(cfg): | ||
""" | ||
Reuse the trainging config will bring useless attribute, such as: backbone.pretrained model. Trim it here. | ||
""" | ||
model_name = cfg.model_name | ||
cfg = cfg.MODEL | ||
cfg.backbone.pretrained = "" | ||
return cfg, model_name | ||
|
||
|
||
def main(): | ||
args = parse_args() | ||
cfg, model_name = _trim(get_config(args.config, show=False)) | ||
print(f"Building model({model_name})...") | ||
model = build_model(cfg) | ||
|
||
params_info = paddle.summary(model, (1, 8, 3, 224, 224)) | ||
|
||
print(params_info) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |