diff --git a/cyclegan/README.md b/cyclegan/README.md new file mode 100644 index 0000000000000..ef35c3ab1b3ec --- /dev/null +++ b/cyclegan/README.md @@ -0,0 +1,139 @@ +# Cycle GAN +--- +## 内容 + +- [安装](#安装) +- [简介](#简介) +- [代码结构](#代码结构) +- [数据准备](#数据准备) +- [模型训练与预测](#模型训练与预测) + +## 安装 + +运行本目录下的程序示例需要使用PaddlePaddle develop最新版本。如果您的PaddlePaddle安装版本低于此要求,请按照[安装文档](http://www.paddlepaddle.org/docs/develop/documentation/zh/build_and_install/pip_install_cn.html)中的说明更新PaddlePaddle安装版本。 + +## 简介 +Cycle GAN 是一种image to image 的图像生成网络,实现了非对称图像数据集的生成和风格迁移。模型结构如下图所示,我们的模型包含两个生成网络 G: X → Y 和 F: Y → X,以及相关的判别器 DY 和 DX 。通过训练DY,使G将X图尽量转换为Y图,反之亦然。同时引入两个“周期一致性损失”,它们保证:如果我们从一个领域转换到另一个领域,它还可以被转换回去:(b)正向循环一致性损失:x→G(x)→F(G(x))≈x, (c)反向循环一致性损失:y→F(y)→G(F(y))≈y + +

+
+图1.网络结构 +

+ + +## 代码结构 +``` +├── data.py # 读取、处理数据。 +├── layers.py # 封装定义基础的layers。 +├── cyclegan.py # 定义基础生成网络和判别网络。 +├── train.py # 训练脚本。 +└── infer.py # 预测脚本。 +``` + + +## 数据准备 + +CycleGAN 支持的数据集可以参考download.py中的`cycle_pix_dataset`,可以通过指定`python download.py --dataset xxx` 下载得到。 + +由于版权问题,cityscapes 数据集无法通过脚本直接获得,需要从[官方](https://www.cityscapes-dataset.com/)下载数据, +下载完之后执行`python prepare_cityscapes_dataset.py --gtFine_dir ./gtFine/ --leftImg8bit_dir ./leftImg8bit --output_dir ./data/cityscapes/`处理, +将数据存放在`data/cityscapes`。 + +数据下载处理完毕后,需要您将数据组织为以下路径结构: +``` +data +|-- cityscapes +| |-- testA +| |-- testB +| |-- trainA +| |-- trainB + +``` + +然后运行txt生成脚本:`python generate_txt.py`,最终数据组织如下所示: +``` +data +|-- cityscapes +| |-- testA +| |-- testA.txt +| |-- testB +| |-- testB.txt +| |-- trainA +| |-- trainA.txt +| |-- trainB +| `-- trainB.txt + +``` + +以上数据文件中,`data`文件夹需要放在训练脚本`train.py`同级目录下。`testA`为存放真实街景图片的文件夹,`testB`为存放语义分割图片的文件夹,`testA.txt`和`testB.txt`分别为测试图片路径列表文件,格式如下: + +``` +data/cityscapes/testA/234_A.jpg +data/cityscapes/testA/292_A.jpg +data/cityscapes/testA/412_A.jpg +``` + +训练数据组织方式与测试数据相同。 + + +## 模型训练与预测 + +### 训练 + +在GPU单卡上训练: + +``` +env CUDA_VISIBLE_DEVICES=0 python train.py +``` + +执行`python train.py --help`可查看更多使用方式和参数详细说明。 + +图1为训练152轮的训练损失示意图,其中横坐标轴为训练轮数,纵轴为在训练集上的损失。其中,'g_loss','da_loss'和'db_loss'分别为生成器、判别器A和判别器B的训练损失。 + + +### 测试 + +执行以下命令可以选择已保存的训练权重,对测试集进行测试,通过 `--epoch` 制定权重轮次: + +``` +env CUDA_VISIBLE_DEVICES=0 python test.py --init_model=checkpoint/199 +``` +生成结果在 `output/eval`中 + + +### 预测 + +执行以下命令读取单张或多张图片进行预测: + +真实街景生成分割图像: + +``` +env CUDA_VISIBLE_DEVICES=0 python infer.py \ + --init_model="./checkpoints/199" --input="./image/testA/123_A.jpg" \ + --input_style=A +``` + +分割图像生成真实街景: + +``` +env CUDA_VISIBLE_DEVICES=0 python infer.py \ + --init_model="checkpoints/199" --input="./image/testB/78_B.jpg" \ + --input_style=B +``` +生成结果在 `output/single`中 + +训练180轮的模型预测效果如fakeA和fakeB所示: + + +

+
+A2B +

+ + +

+
+B2A +

+ +>在本文示例中,均可通过修改`CUDA_VISIBLE_DEVICES`改变使用的显卡号。 diff --git a/cyclegan/__init__.py b/cyclegan/__init__.py new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/cyclegan/check.py b/cyclegan/check.py new file mode 100644 index 0000000000000..79ab4862d3c20 --- /dev/null +++ b/cyclegan/check.py @@ -0,0 +1,58 @@ +# 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. + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import sys + +import paddle.fluid as fluid + +__all__ = ['check_gpu', 'check_version'] + + +def check_gpu(use_gpu): + """ + Log error and exit when set use_gpu=true in paddlepaddle + cpu version. + """ + err = "Config use_gpu cannot be set as true while you are " \ + "using paddlepaddle cpu version ! \nPlease try: \n" \ + "\t1. Install paddlepaddle-gpu to run model on GPU \n" \ + "\t2. Set use_gpu as false in config file to run " \ + "model on CPU" + + try: + if use_gpu and not fluid.is_compiled_with_cuda(): + print(err) + sys.exit(1) + except Exception as e: + pass + + +def check_version(): + """ + Log error and exit when the installed version of paddlepaddle is + not satisfied. + """ + err = "PaddlePaddle version 1.6 or higher is required, " \ + "or a suitable develop version is satisfied as well. \n" \ + "Please make sure the version is good with your code." \ + + try: + fluid.require_version('1.7.0') + except Exception as e: + print(err) + sys.exit(1) diff --git a/cyclegan/cyclegan.py b/cyclegan/cyclegan.py new file mode 100644 index 0000000000000..6fdd21c1bdf41 --- /dev/null +++ b/cyclegan/cyclegan.py @@ -0,0 +1,232 @@ +# 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. + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import numpy as np + +from layers import ConvBN, DeConvBN +import paddle.fluid as fluid +from model import Model, Loss + + +class ResnetBlock(fluid.dygraph.Layer): + def __init__(self, dim, dropout=False): + super(ResnetBlock, self).__init__() + self.dropout = dropout + self.conv0 = ConvBN(dim, dim, 3, 1) + self.conv1 = ConvBN(dim, dim, 3, 1, act=None) + + def forward(self, inputs): + out_res = fluid.layers.pad2d(inputs, [1, 1, 1, 1], mode="reflect") + out_res = self.conv0(out_res) + if self.dropout: + out_res = fluid.layers.dropout(out_res, dropout_prob=0.5) + out_res = fluid.layers.pad2d(out_res, [1, 1, 1, 1], mode="reflect") + out_res = self.conv1(out_res) + return out_res + inputs + + +class ResnetGenerator(fluid.dygraph.Layer): + def __init__(self, input_channel, n_blocks=9, dropout=False): + super(ResnetGenerator, self).__init__() + + self.conv0 = ConvBN(input_channel, 32, 7, 1) + self.conv1 = ConvBN(32, 64, 3, 2, padding=1) + self.conv2 = ConvBN(64, 128, 3, 2, padding=1) + + dim = 128 + self.resnet_blocks = [] + for i in range(n_blocks): + block = self.add_sublayer("generator_%d" % (i + 1), + ResnetBlock(dim, dropout)) + self.resnet_blocks.append(block) + + self.deconv0 = DeConvBN( + dim, 32 * 2, 3, 2, padding=[1, 1], outpadding=[0, 1, 0, 1]) + self.deconv1 = DeConvBN( + 32 * 2, 32, 3, 2, padding=[1, 1], outpadding=[0, 1, 0, 1]) + + self.conv3 = ConvBN( + 32, input_channel, 7, 1, norm=False, act=False, use_bias=True) + + def forward(self, inputs): + pad_input = fluid.layers.pad2d(inputs, [3, 3, 3, 3], mode="reflect") + y = self.conv0(pad_input) + y = self.conv1(y) + y = self.conv2(y) + for resnet_block in self.resnet_blocks: + y = resnet_block(y) + y = self.deconv0(y) + y = self.deconv1(y) + y = fluid.layers.pad2d(y, [3, 3, 3, 3], mode="reflect") + y = self.conv3(y) + y = fluid.layers.tanh(y) + return y + + +class NLayerDiscriminator(fluid.dygraph.Layer): + def __init__(self, input_channel, d_dims=64, d_nlayers=3): + super(NLayerDiscriminator, self).__init__() + self.conv0 = ConvBN( + input_channel, + d_dims, + 4, + 2, + 1, + norm=False, + use_bias=True, + relufactor=0.2) + + nf_mult, nf_mult_prev = 1, 1 + self.conv_layers = [] + for n in range(1, d_nlayers): + nf_mult_prev = nf_mult + nf_mult = min(2**n, 8) + conv = self.add_sublayer( + 'discriminator_%d' % (n), + ConvBN( + d_dims * nf_mult_prev, + d_dims * nf_mult, + 4, + 2, + 1, + relufactor=0.2)) + self.conv_layers.append(conv) + + nf_mult_prev = nf_mult + nf_mult = min(2**d_nlayers, 8) + self.conv4 = ConvBN( + d_dims * nf_mult_prev, d_dims * nf_mult, 4, 1, 1, relufactor=0.2) + self.conv5 = ConvBN( + d_dims * nf_mult, + 1, + 4, + 1, + 1, + norm=False, + act=None, + use_bias=True, + relufactor=0.2) + + def forward(self, inputs): + y = self.conv0(inputs) + for conv in self.conv_layers: + y = conv(y) + y = self.conv4(y) + y = self.conv5(y) + return y + + +class Generator(Model): + def __init__(self, input_channel=3): + super(Generator, self).__init__() + self.g = ResnetGenerator(input_channel) + + def forward(self, input): + fake = self.g(input) + return fake + + +class GeneratorCombine(Model): + def __init__(self, g_AB=None, g_BA=None, d_A=None, d_B=None, + is_train=True): + super(GeneratorCombine, self).__init__() + self.g_AB = g_AB + self.g_BA = g_BA + self.is_train = is_train + if self.is_train: + self.d_A = d_A + self.d_B = d_B + + def forward(self, input_A, input_B): + # Translate images to the other domain + fake_B = self.g_AB(input_A) + fake_A = self.g_BA(input_B) + + # Translate images back to original domain + cyc_A = self.g_BA(fake_B) + cyc_B = self.g_AB(fake_A) + if not self.is_train: + return fake_A, fake_B, cyc_A, cyc_B + + # Identity mapping of images + idt_A = self.g_AB(input_B) + idt_B = self.g_BA(input_A) + + # Discriminators determines validity of translated images + # d_A(g_AB(A)) + valid_A = self.d_A.d(fake_B) + # d_B(g_BA(A)) + valid_B = self.d_B.d(fake_A) + return input_A, input_B, fake_A, fake_B, cyc_A, cyc_B, idt_A, idt_B, valid_A, valid_B + + +class GLoss(Loss): + def __init__(self, lambda_A=10., lambda_B=10., lambda_identity=0.5): + super(GLoss, self).__init__() + self.lambda_A = lambda_A + self.lambda_B = lambda_B + self.lambda_identity = lambda_identity + + def forward(self, outputs, labels=None): + input_A, input_B, fake_A, fake_B, cyc_A, cyc_B, idt_A, idt_B, valid_A, valid_B = outputs + + def mse(a, b): + return fluid.layers.reduce_mean(fluid.layers.square(a - b)) + + def mae(a, b): # L1Loss + return fluid.layers.reduce_mean(fluid.layers.abs(a - b)) + + g_A_loss = mse(valid_A, 1.) + g_B_loss = mse(valid_B, 1.) + g_loss = g_A_loss + g_B_loss + + cyc_A_loss = mae(input_A, cyc_A) * self.lambda_A + cyc_B_loss = mae(input_B, cyc_B) * self.lambda_B + cyc_loss = cyc_A_loss + cyc_B_loss + + idt_loss_A = mae(input_B, idt_A) * (self.lambda_B * + self.lambda_identity) + idt_loss_B = mae(input_A, idt_B) * (self.lambda_A * + self.lambda_identity) + idt_loss = idt_loss_A + idt_loss_B + + loss = cyc_loss + g_loss + idt_loss + return loss + + +class Discriminator(Model): + def __init__(self, input_channel=3): + super(Discriminator, self).__init__() + self.d = NLayerDiscriminator(input_channel) + + def forward(self, real, fake): + pred_real = self.d(real) + pred_fake = self.d(fake) + return pred_real, pred_fake + + +class DLoss(Loss): + def __init__(self): + super(DLoss, self).__init__() + + def forward(self, inputs, labels=None): + pred_real, pred_fake = inputs + loss = fluid.layers.square(pred_fake) + fluid.layers.square(pred_real - + 1.) + loss = fluid.layers.reduce_mean(loss / 2.0) + return loss diff --git a/cyclegan/data.py b/cyclegan/data.py new file mode 100644 index 0000000000000..effa4eeee12a7 --- /dev/null +++ b/cyclegan/data.py @@ -0,0 +1,121 @@ +# 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. + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +import os +import random +import numpy as np +from PIL import Image, ImageOps + +DATASET = "cityscapes" +A_LIST_FILE = "./data/" + DATASET + "/trainA.txt" +B_LIST_FILE = "./data/" + DATASET + "/trainB.txt" +A_TEST_LIST_FILE = "./data/" + DATASET + "/testA.txt" +B_TEST_LIST_FILE = "./data/" + DATASET + "/testB.txt" +IMAGES_ROOT = "./data/" + DATASET + "/" + +import paddle.fluid as fluid + + +class Cityscapes(fluid.io.Dataset): + def __init__(self, root_path, file_path, mode='train', return_name=False): + self.root_path = root_path + self.file_path = file_path + self.mode = mode + self.return_name = return_name + self.images = [root_path + l for l in open(file_path, 'r').readlines()] + + def _train(self, image): + ## Resize + image = image.resize((286, 286), Image.BICUBIC) + ## RandomCrop + i = np.random.randint(0, 30) + j = np.random.randint(0, 30) + image = image.crop((i, j, i + 256, j + 256)) + # RandomHorizontalFlip + if np.random.rand() > 0.5: + image = ImageOps.mirror(image) + return image + + def __getitem__(self, idx): + f = self.images[idx].strip("\n\r\t ") + image = Image.open(f) + if self.mode == 'train': + image = self._train(image) + else: + image = image.resize((256, 256), Image.BICUBIC) + # ToTensor + image = np.array(image).transpose([2, 0, 1]).astype('float32') + image = image / 255.0 + # Normalize, mean=[0.5,0.5,0.5], std=[0.5,0.5,0.5] + image = (image - 0.5) / 0.5 + if self.return_name: + return [image], os.path.basename(f) + else: + return [image] + + def __len__(self): + return len(self.images) + + +def DataA(root=IMAGES_ROOT, fpath=A_LIST_FILE): + """ + Reader of images with A style for training. + """ + return Cityscapes(root, fpath) + + +def DataB(root=IMAGES_ROOT, fpath=B_LIST_FILE): + """ + Reader of images with B style for training. + """ + return Cityscapes(root, fpath) + + +def TestDataA(root=IMAGES_ROOT, fpath=A_TEST_LIST_FILE): + """ + Reader of images with A style for training. + """ + return Cityscapes(root, fpath, mode='test', return_name=True) + + +def TestDataB(root=IMAGES_ROOT, fpath=B_TEST_LIST_FILE): + """ + Reader of images with B style for training. + """ + return Cityscapes(root, fpath, mode='test', return_name=True) + + +class ImagePool(object): + def __init__(self, pool_size=50): + self.pool = [] + self.count = 0 + self.pool_size = pool_size + + def get(self, image): + if self.count < self.pool_size: + self.pool.append(image) + self.count += 1 + return image + else: + p = random.random() + if p > 0.5: + random_id = random.randint(0, self.pool_size - 1) + temp = self.pool[random_id] + self.pool[random_id] = image + return temp + else: + return image diff --git a/cyclegan/image/A2B.png b/cyclegan/image/A2B.png new file mode 100644 index 0000000000000..b67466da9bdf0 Binary files /dev/null and b/cyclegan/image/A2B.png differ diff --git a/cyclegan/image/B2A.png b/cyclegan/image/B2A.png new file mode 100644 index 0000000000000..851dd7422144a Binary files /dev/null and b/cyclegan/image/B2A.png differ diff --git a/cyclegan/image/net.png b/cyclegan/image/net.png new file mode 100644 index 0000000000000..46681f8eea989 Binary files /dev/null and b/cyclegan/image/net.png differ diff --git a/cyclegan/image/testA/123_A.jpg b/cyclegan/image/testA/123_A.jpg new file mode 100644 index 0000000000000..c78de45861aa3 Binary files /dev/null and b/cyclegan/image/testA/123_A.jpg differ diff --git a/cyclegan/image/testB/78_B.jpg b/cyclegan/image/testB/78_B.jpg new file mode 100644 index 0000000000000..849c3be3ce6bd Binary files /dev/null and b/cyclegan/image/testB/78_B.jpg differ diff --git a/cyclegan/infer.py b/cyclegan/infer.py new file mode 100644 index 0000000000000..0b61a958d59e1 --- /dev/null +++ b/cyclegan/infer.py @@ -0,0 +1,108 @@ +# 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. + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import os +import glob +import numpy as np +import argparse + +from PIL import Image +from scipy.misc import imsave + +import paddle.fluid as fluid +from check import check_gpu, check_version + +from model import Model, Input, set_device +from cyclegan import Generator, GeneratorCombine + + +def main(): + place = set_device(FLAGS.device) + fluid.enable_dygraph(place) if FLAGS.dynamic else None + + # Generators + g_AB = Generator() + g_BA = Generator() + g = GeneratorCombine(g_AB, g_BA, is_train=False) + + im_shape = [-1, 3, 256, 256] + input_A = Input(im_shape, 'float32', 'input_A') + input_B = Input(im_shape, 'float32', 'input_B') + g.prepare(inputs=[input_A, input_B]) + g.load(FLAGS.init_model, skip_mismatch=True, reset_optimizer=True) + + out_path = FLAGS.output + "/single" + if not os.path.exists(out_path): + os.makedirs(out_path) + for f in glob.glob(FLAGS.input): + image_name = os.path.basename(f) + image = Image.open(f).convert('RGB') + image = image.resize((256, 256), Image.BICUBIC) + image = np.array(image) / 127.5 - 1 + + image = image[:, :, 0:3].astype("float32") + data = image.transpose([2, 0, 1])[np.newaxis, :] + + if FLAGS.input_style == "A": + _, fake, _, _ = g.test([data, data]) + + if FLAGS.input_style == "B": + fake, _, _, _ = g.test([data, data]) + + fake = np.squeeze(fake[0]).transpose([1, 2, 0]) + + opath = "{}/fake{}{}".format(out_path, FLAGS.input_style, image_name) + imsave(opath, ((fake + 1) * 127.5).astype(np.uint8)) + print("transfer {} to {}".format(f, opath)) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser("CycleGAN inference") + parser.add_argument( + "-d", "--dynamic", action='store_false', help="Enable dygraph mode") + parser.add_argument( + "-p", + "--device", + type=str, + default='gpu', + help="device to use, gpu or cpu") + parser.add_argument( + "-i", + "--input", + type=str, + default='./image/testA/123_A.jpg', + help="input image") + parser.add_argument( + "-o", + '--output', + type=str, + default='output', + help="The test result to be saved to.") + parser.add_argument( + "-m", + "--init_model", + type=str, + default='checkpoint/199', + help="The init model file of directory.") + parser.add_argument( + "-s", "--input_style", type=str, default='A', help="A or B") + FLAGS = parser.parse_args() + print(FLAGS) + check_gpu(str.lower(FLAGS.device) == 'gpu') + check_version() + main() diff --git a/cyclegan/layers.py b/cyclegan/layers.py new file mode 100644 index 0000000000000..8c79ef5ff5416 --- /dev/null +++ b/cyclegan/layers.py @@ -0,0 +1,140 @@ +# Copyright (c) 2019 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. + +from __future__ import division +import paddle.fluid as fluid +from paddle.fluid.dygraph.nn import Conv2D, Conv2DTranspose, BatchNorm + +# cudnn is not better when batch size is 1. +use_cudnn = False +import numpy as np + + +class ConvBN(fluid.dygraph.Layer): + """docstring for Conv2D""" + + def __init__(self, + num_channels, + num_filters, + filter_size, + stride=1, + padding=0, + stddev=0.02, + norm=True, + is_test=False, + act='leaky_relu', + relufactor=0.0, + use_bias=False): + super(ConvBN, self).__init__() + + pattr = fluid.ParamAttr( + initializer=fluid.initializer.NormalInitializer( + loc=0.0, scale=stddev)) + self.conv = Conv2D( + num_channels=num_channels, + num_filters=num_filters, + filter_size=filter_size, + stride=stride, + padding=padding, + use_cudnn=use_cudnn, + param_attr=pattr, + bias_attr=use_bias) + if norm: + self.bn = BatchNorm( + num_filters, + param_attr=fluid.ParamAttr( + initializer=fluid.initializer.NormalInitializer(1.0, + 0.02)), + bias_attr=fluid.ParamAttr( + initializer=fluid.initializer.Constant(0.0)), + is_test=False, + trainable_statistics=True) + self.relufactor = relufactor + self.norm = norm + self.act = act + + def forward(self, inputs): + conv = self.conv(inputs) + if self.norm: + conv = self.bn(conv) + + if self.act == 'leaky_relu': + conv = fluid.layers.leaky_relu(conv, alpha=self.relufactor) + elif self.act == 'relu': + conv = fluid.layers.relu(conv) + else: + conv = conv + + return conv + + +class DeConvBN(fluid.dygraph.Layer): + def __init__(self, + num_channels, + num_filters, + filter_size, + stride=1, + padding=[0, 0], + outpadding=[0, 0, 0, 0], + stddev=0.02, + act='leaky_relu', + norm=True, + is_test=False, + relufactor=0.0, + use_bias=False): + super(DeConvBN, self).__init__() + + pattr = fluid.ParamAttr( + initializer=fluid.initializer.NormalInitializer( + loc=0.0, scale=stddev)) + self._deconv = Conv2DTranspose( + num_channels, + num_filters, + filter_size=filter_size, + stride=stride, + padding=padding, + param_attr=pattr, + bias_attr=use_bias) + if norm: + self.bn = BatchNorm( + num_filters, + param_attr=fluid.ParamAttr( + initializer=fluid.initializer.NormalInitializer(1.0, + 0.02)), + bias_attr=fluid.ParamAttr( + initializer=fluid.initializer.Constant(0.0)), + is_test=False, + trainable_statistics=True) + self.outpadding = outpadding + self.relufactor = relufactor + self.use_bias = use_bias + self.norm = norm + self.act = act + + def forward(self, inputs): + conv = self._deconv(inputs) + conv = fluid.layers.pad2d( + conv, paddings=self.outpadding, mode='constant', pad_value=0.0) + + if self.norm: + conv = self.bn(conv) + + if self.act == 'leaky_relu': + conv = fluid.layers.leaky_relu(conv, alpha=self.relufactor) + elif self.act == 'relu': + conv = fluid.layers.relu(conv) + else: + conv = conv + + return conv diff --git a/cyclegan/test.py b/cyclegan/test.py new file mode 100644 index 0000000000000..995663090f07e --- /dev/null +++ b/cyclegan/test.py @@ -0,0 +1,103 @@ +# 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. + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import os +import argparse +import numpy as np +from scipy.misc import imsave + +import paddle.fluid as fluid +from check import check_gpu, check_version + +from model import Model, Input, set_device +from cyclegan import Generator, GeneratorCombine +import data as data + + +def main(): + place = set_device(FLAGS.device) + fluid.enable_dygraph(place) if FLAGS.dynamic else None + + # Generators + g_AB = Generator() + g_BA = Generator() + g = GeneratorCombine(g_AB, g_BA, is_train=False) + + im_shape = [-1, 3, 256, 256] + input_A = Input(im_shape, 'float32', 'input_A') + input_B = Input(im_shape, 'float32', 'input_B') + g.prepare(inputs=[input_A, input_B]) + g.load(FLAGS.init_model, skip_mismatch=True, reset_optimizer=True) + + if not os.path.exists(FLAGS.output): + os.makedirs(FLAGS.output) + + test_data_A = data.TestDataA() + test_data_B = data.TestDataB() + + for i in range(len(test_data_A)): + data_A, A_name = test_data_A[i] + data_B, B_name = test_data_B[i] + data_A = np.array(data_A).astype("float32") + data_B = np.array(data_B).astype("float32") + + fake_A, fake_B, cyc_A, cyc_B = g.test([data_A, data_B]) + + datas = [fake_A, fake_B, cyc_A, cyc_B, data_A, data_B] + odatas = [] + for o in datas: + d = np.squeeze(o[0]).transpose([1, 2, 0]) + im = ((d + 1) * 127.5).astype(np.uint8) + odatas.append(im) + imsave(FLAGS.output + "/fakeA_" + B_name, odatas[0]) + imsave(FLAGS.output + "/fakeB_" + A_name, odatas[1]) + imsave(FLAGS.output + "/cycA_" + A_name, odatas[2]) + imsave(FLAGS.output + "/cycB_" + B_name, odatas[3]) + imsave(FLAGS.output + "/inputA_" + A_name, odatas[4]) + imsave(FLAGS.output + "/inputB_" + B_name, odatas[5]) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser("CycleGAN test") + parser.add_argument( + "-d", "--dynamic", action='store_false', help="Enable dygraph mode") + parser.add_argument( + "-p", + "--device", + type=str, + default='gpu', + help="device to use, gpu or cpu") + parser.add_argument( + "-b", "--batch_size", default=1, type=int, help="batch size") + parser.add_argument( + "-o", + '--output', + type=str, + default='output/eval', + help="The test result to be saved to.") + parser.add_argument( + "-m", + "--init_model", + type=str, + default='checkpoint/199', + help="The init model file of directory.") + FLAGS = parser.parse_args() + print(FLAGS) + check_gpu(str.lower(FLAGS.device) == 'gpu') + check_version() + main() diff --git a/cyclegan/train.py b/cyclegan/train.py new file mode 100644 index 0000000000000..c2203fc19c8e0 --- /dev/null +++ b/cyclegan/train.py @@ -0,0 +1,158 @@ +# 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. + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import numpy as np +import random +import argparse +import contextlib +import time + +import paddle +import paddle.fluid as fluid +from check import check_gpu, check_version + +from model import Model, Input, set_device + +import data as data +from cyclegan import Generator, Discriminator, GeneratorCombine, GLoss, DLoss + +step_per_epoch = 2974 + + +def opt(parameters): + lr_base = 0.0002 + bounds = [100, 120, 140, 160, 180] + lr = [1., 0.8, 0.6, 0.4, 0.2, 0.1] + bounds = [i * step_per_epoch for i in bounds] + lr = [i * lr_base for i in lr] + optimizer = fluid.optimizer.Adam( + learning_rate=fluid.layers.piecewise_decay( + boundaries=bounds, values=lr), + parameter_list=parameters, + beta1=0.5) + return optimizer + + +def main(): + place = set_device(FLAGS.device) + fluid.enable_dygraph(place) if FLAGS.dynamic else None + + # Generators + g_AB = Generator() + g_BA = Generator() + + # Discriminators + d_A = Discriminator() + d_B = Discriminator() + + g = GeneratorCombine(g_AB, g_BA, d_A, d_B) + + da_params = d_A.parameters() + db_params = d_B.parameters() + g_params = g_AB.parameters() + g_BA.parameters() + + da_optimizer = opt(da_params) + db_optimizer = opt(db_params) + g_optimizer = opt(g_params) + + im_shape = [None, 3, 256, 256] + input_A = Input(im_shape, 'float32', 'input_A') + input_B = Input(im_shape, 'float32', 'input_B') + fake_A = Input(im_shape, 'float32', 'fake_A') + fake_B = Input(im_shape, 'float32', 'fake_B') + + g_AB.prepare(inputs=[input_A]) + g_BA.prepare(inputs=[input_B]) + + g.prepare(g_optimizer, GLoss(), inputs=[input_A, input_B]) + d_A.prepare(da_optimizer, DLoss(), inputs=[input_B, fake_B]) + d_B.prepare(db_optimizer, DLoss(), inputs=[input_A, fake_A]) + + if FLAGS.resume: + g.load(FLAGS.resume) + + loader_A = fluid.io.DataLoader( + data.DataA(), + places=place, + shuffle=True, + return_list=True, + batch_size=FLAGS.batch_size) + loader_B = fluid.io.DataLoader( + data.DataB(), + places=place, + shuffle=True, + return_list=True, + batch_size=FLAGS.batch_size) + + A_pool = data.ImagePool() + B_pool = data.ImagePool() + + for epoch in range(FLAGS.epoch): + for i, (data_A, data_B) in enumerate(zip(loader_A, loader_B)): + data_A = data_A[0][0] if not FLAGS.dynamic else data_A[0] + data_B = data_B[0][0] if not FLAGS.dynamic else data_B[0] + start = time.time() + + fake_B = g_AB.test(data_A)[0] + fake_A = g_BA.test(data_B)[0] + g_loss = g.train([data_A, data_B])[0] + fake_pb = B_pool.get(fake_B) + da_loss = d_A.train([data_B, fake_pb])[0] + + fake_pa = A_pool.get(fake_A) + db_loss = d_B.train([data_A, fake_pa])[0] + + t = time.time() - start + if i % 20 == 0: + print("epoch: {} | step: {:3d} | g_loss: {:.4f} | " \ + "da_loss: {:.4f} | db_loss: {:.4f} | s/step {:.4f}". + format(epoch, i, g_loss[0], da_loss[0], db_loss[0], t)) + g.save('{}/{}'.format(FLAGS.checkpoint_path, epoch)) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser("CycleGAN Training on Cityscapes") + parser.add_argument( + "-d", "--dynamic", action='store_false', help="Enable dygraph mode") + parser.add_argument( + "-p", + "--device", + type=str, + default='gpu', + help="device to use, gpu or cpu") + parser.add_argument( + "-e", "--epoch", default=200, type=int, help="Epoch number") + parser.add_argument( + "-b", "--batch_size", default=1, type=int, help="batch size") + parser.add_argument( + "-o", + "--checkpoint_path", + type=str, + default='checkpoint', + help="path to save checkpoint") + parser.add_argument( + "-r", + "--resume", + default=None, + type=str, + help="checkpoint path to resume") + FLAGS = parser.parse_args() + print(FLAGS) + check_gpu(str.lower(FLAGS.device) == 'gpu') + check_version() + main() diff --git a/datasets/folder.py b/datasets/folder.py index 2b724b4cb8355..e853e7e106cf7 100644 --- a/datasets/folder.py +++ b/datasets/folder.py @@ -1,3 +1,17 @@ +# 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 os import sys import cv2 @@ -6,11 +20,11 @@ def has_valid_extension(filename, extensions): - """Checks if a file is an allowed extension. + """Checks if a file is a vilid extension. Args: - filename (string): path to a file - extensions (tuple of strings): extensions to consider (lowercase) + filename (str): path to a file + extensions (tuple of str): extensions to consider (lowercase) Returns: bool: True if the filename ends with one of given extensions diff --git a/image_classification/README.MD b/image_classification/README.MD index 2f450d5a58f17..9be3362090e97 100644 --- a/image_classification/README.MD +++ b/image_classification/README.MD @@ -30,6 +30,7 @@ ```bash python -u main.py --arch resnet50 /path/to/imagenet -d ``` +-d 是使用动态模式训练,默认为静态图模式。 ### 多卡训练 执行如下命令进行训练 @@ -64,11 +65,28 @@ CUDA_VISIBLE_DEVICES=0,1,2,3 python -m paddle.distributed.launch main.py --arch * **output-dir**: 模型文件保存的文件夹,默认值:'output' * **num-workers**: dataloader的进程数,默认值:4 * **resume**: 恢复训练的模型路径,默认值:None -* **eval-only**: 仅仅进行预测,默认值:False +* **eval-only**: 是否仅仅进行预测 +* **lr-scheduler**: 学习率衰减策略,默认值:piecewise +* **milestones**: piecewise学习率衰减策略的边界,默认值:[30, 60, 80] +* **weight-decay**: 模型权重正则化系数,默认值:1e-4 +* **momentum**: SGD优化器的动量,默认值:0.9 ## 模型 | 模型 | top1 acc | top5 acc | | --- | --- | --- | -| ResNet50 | 76.28 | 93.04 | +| [ResNet50](https://paddle-hapi.bj.bcebos.com/models/resnet50.pdparams) | 76.28 | 93.04 | +| [vgg16](https://paddle-hapi.bj.bcebos.com/models/vgg16.pdparams) | 71.84 | 90.71 | +| [mobilenet_v1](https://paddle-hapi.bj.bcebos.com/models/mobilenet_v1_x1.0.pdparams) | 71.25 | 89.92 | +| [mobilenet_v2](https://paddle-hapi.bj.bcebos.com/models/mobilenet_v2_x1.0.pdparams) | 72.27 | 90.66 | + +上述模型的复现参数请参考scripts下的脚本。 + + +## 参考文献 +- ResNet: [Deep Residual Learning for Image Recognitio](https://arxiv.org/abs/1512.03385), Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun +- MobileNetV1: [MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications](https://arxiv.org/abs/1704.04861), Andrew G. Howard, Menglong Zhu, Bo Chen, Dmitry Kalenichenko, Weijun Wang, Tobias Weyand, Marco Andreetto, Hartwig Adam +- MobileNetV2: [MobileNetV2: Inverted Residuals and Linear Bottlenecks](https://arxiv.org/pdf/1801.04381v4.pdf), Mark Sandler, Andrew Howard, Menglong Zhu, Andrey Zhmoginov, Liang-Chieh Chen +- VGG: [Very Deep Convolutional Networks for Large-scale Image Recognition](https://arxiv.org/pdf/1409.1556), Karen Simonyan, Andrew Zisserman + diff --git a/image_classification/imagenet_dataset.py b/image_classification/imagenet_dataset.py index 6fcd8840fb500..948ac5b8bb4c3 100644 --- a/image_classification/imagenet_dataset.py +++ b/image_classification/imagenet_dataset.py @@ -1,3 +1,17 @@ +# 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 os import cv2 import math diff --git a/image_classification/main.py b/image_classification/main.py index 8f8a44e67cdfb..781824fa60f9d 100644 --- a/image_classification/main.py +++ b/image_classification/main.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. +# 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. @@ -37,23 +37,36 @@ def make_optimizer(step_per_epoch, parameter_list=None): base_lr = FLAGS.lr - momentum = 0.9 - weight_decay = 1e-4 + lr_scheduler = FLAGS.lr_scheduler + momentum = FLAGS.momentum + weight_decay = FLAGS.weight_decay + + if lr_scheduler == 'piecewise': + milestones = FLAGS.milestones + boundaries = [step_per_epoch * e for e in milestones] + values = [base_lr * (0.1**i) for i in range(len(boundaries) + 1)] + learning_rate = fluid.layers.piecewise_decay( + boundaries=boundaries, values=values) + elif lr_scheduler == 'cosine': + learning_rate = fluid.layers.cosine_decay(base_lr, step_per_epoch, + FLAGS.epoch) + else: + raise ValueError( + "Expected lr_scheduler in ['piecewise', 'cosine'], but got {}". + format(lr_scheduler)) - boundaries = [step_per_epoch * e for e in [30, 60, 80]] - values = [base_lr * (0.1**i) for i in range(len(boundaries) + 1)] - learning_rate = fluid.layers.piecewise_decay( - boundaries=boundaries, values=values) learning_rate = fluid.layers.linear_lr_warmup( learning_rate=learning_rate, warmup_steps=5 * step_per_epoch, start_lr=0., end_lr=base_lr) + optimizer = fluid.optimizer.Momentum( learning_rate=learning_rate, momentum=momentum, regularization=fluid.regularizer.L2Decay(weight_decay), parameter_list=parameter_list) + return optimizer @@ -138,6 +151,20 @@ def main(): help="checkpoint path to resume") parser.add_argument( "--eval-only", action='store_true', help="enable dygraph mode") + parser.add_argument( + "--lr-scheduler", + default='piecewise', + type=str, + help="learning rate scheduler") + parser.add_argument( + "--milestones", + nargs='+', + type=int, + default=[30, 60, 80], + help="piecewise decay milestones") + parser.add_argument( + "--weight-decay", default=1e-4, type=float, help="weight decay") + parser.add_argument("--momentum", default=0.9, type=float, help="momentum") FLAGS = parser.parse_args() assert FLAGS.data, "error: must provide data path" main() diff --git a/model.py b/model.py index da5cebd669b83..6fecbf1d29fa3 100644 --- a/model.py +++ b/model.py @@ -42,6 +42,14 @@ def set_device(device): + """ + Args: + device (str): specify device type, 'cpu' or 'gpu'. + + Returns: + fluid.CUDAPlace or fluid.CPUPlace: Created GPU or CPU place. + """ + assert isinstance(device, six.string_types) and device.lower() in ['cpu', 'gpu'], \ "Expected device in ['cpu', 'gpu'], but got {}".format(device) @@ -114,9 +122,9 @@ def __init__(self, average=True): def forward(self, outputs, labels): raise NotImplementedError() - def __call__(self, outputs, labels): + def __call__(self, outputs, labels=None): labels = to_list(labels) - if in_dygraph_mode(): + if in_dygraph_mode() and labels: labels = [to_variable(l) for l in labels] losses = to_list(self.forward(to_list(outputs), labels)) if self.average: @@ -853,8 +861,6 @@ def prepare(self, if not isinstance(inputs, (list, dict, Input)): raise TypeError( "'inputs' must be list or dict in static graph mode") - if loss_function and not isinstance(labels, (list, Input)): - raise TypeError("'labels' must be list in static graph mode") metrics = metrics or [] for metric in to_list(metrics): @@ -1084,7 +1090,11 @@ def evaluate( return eval_result - def predict(self, test_data, batch_size=1, num_workers=0, stack_outputs=True): + def predict(self, + test_data, + batch_size=1, + num_workers=0, + stack_outputs=True): """ FIXME: add more comments and usage Args: diff --git a/models/__init__.py b/models/__init__.py index 85cbd8cac3816..02071502d382d 100644 --- a/models/__init__.py +++ b/models/__init__.py @@ -13,13 +13,22 @@ #limitations under the License. from . import resnet +from . import vgg +from . import mobilenetv1 +from . import mobilenetv2 from . import darknet from . import yolov3 from .resnet import * +from .mobilenetv1 import * +from .mobilenetv2 import * +from .vgg import * from .darknet import * from .yolov3 import * __all__ = resnet.__all__ \ + + vgg.__all__ \ + + mobilenetv1.__all__ \ + + mobilenetv2.__all__ \ + darknet.__all__ \ + yolov3.__all__ diff --git a/models/mobilenetv1.py b/models/mobilenetv1.py new file mode 100644 index 0000000000000..c2e7959b1b9bf --- /dev/null +++ b/models/mobilenetv1.py @@ -0,0 +1,266 @@ +# 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 numpy as np +import paddle +import paddle.fluid as fluid +from paddle.fluid.initializer import MSRA +from paddle.fluid.param_attr import ParamAttr +from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear + +from model import Model +from .download import get_weights_path + +__all__ = ['MobileNetV1', 'mobilenet_v1'] + +model_urls = { + 'mobilenetv1_1.0': + ('https://paddle-hapi.bj.bcebos.com/models/mobilenet_v1_x1.0.pdparams', + 'bf0d25cb0bed1114d9dac9384ce2b4a6') +} + + +class ConvBNLayer(fluid.dygraph.Layer): + def __init__(self, + num_channels, + filter_size, + num_filters, + stride, + padding, + channels=None, + num_groups=1, + act='relu', + use_cudnn=True, + name=None): + super(ConvBNLayer, self).__init__() + + self._conv = Conv2D( + num_channels=num_channels, + num_filters=num_filters, + filter_size=filter_size, + stride=stride, + padding=padding, + groups=num_groups, + act=None, + use_cudnn=use_cudnn, + param_attr=ParamAttr( + initializer=MSRA(), name=self.full_name() + "_weights"), + bias_attr=False) + + self._batch_norm = BatchNorm( + num_filters, + act=act, + param_attr=ParamAttr(name=self.full_name() + "_bn" + "_scale"), + bias_attr=ParamAttr(name=self.full_name() + "_bn" + "_offset"), + moving_mean_name=self.full_name() + "_bn" + '_mean', + moving_variance_name=self.full_name() + "_bn" + '_variance') + + def forward(self, inputs): + y = self._conv(inputs) + y = self._batch_norm(y) + return y + + +class DepthwiseSeparable(fluid.dygraph.Layer): + def __init__(self, + num_channels, + num_filters1, + num_filters2, + num_groups, + stride, + scale, + name=None): + super(DepthwiseSeparable, self).__init__() + + self._depthwise_conv = ConvBNLayer( + num_channels=num_channels, + num_filters=int(num_filters1 * scale), + filter_size=3, + stride=stride, + padding=1, + num_groups=int(num_groups * scale), + use_cudnn=False) + + self._pointwise_conv = ConvBNLayer( + num_channels=int(num_filters1 * scale), + filter_size=1, + num_filters=int(num_filters2 * scale), + stride=1, + padding=0) + + def forward(self, inputs): + y = self._depthwise_conv(inputs) + y = self._pointwise_conv(y) + return y + + +class MobileNetV1(Model): + """MobileNetV1 model from + `"MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications" `_. + + Args: + scale (float): scale of channels in each layer. Default: 1.0. + class_dim (int): output dim of last fc layer. Default: 1000. + """ + + def __init__(self, scale=1.0, class_dim=1000): + super(MobileNetV1, self).__init__() + self.scale = scale + self.dwsl = [] + + self.conv1 = ConvBNLayer( + num_channels=3, + filter_size=3, + channels=3, + num_filters=int(32 * scale), + stride=2, + padding=1) + + dws21 = self.add_sublayer( + sublayer=DepthwiseSeparable( + num_channels=int(32 * scale), + num_filters1=32, + num_filters2=64, + num_groups=32, + stride=1, + scale=scale), + name="conv2_1") + self.dwsl.append(dws21) + + dws22 = self.add_sublayer( + sublayer=DepthwiseSeparable( + num_channels=int(64 * scale), + num_filters1=64, + num_filters2=128, + num_groups=64, + stride=2, + scale=scale), + name="conv2_2") + self.dwsl.append(dws22) + + dws31 = self.add_sublayer( + sublayer=DepthwiseSeparable( + num_channels=int(128 * scale), + num_filters1=128, + num_filters2=128, + num_groups=128, + stride=1, + scale=scale), + name="conv3_1") + self.dwsl.append(dws31) + + dws32 = self.add_sublayer( + sublayer=DepthwiseSeparable( + num_channels=int(128 * scale), + num_filters1=128, + num_filters2=256, + num_groups=128, + stride=2, + scale=scale), + name="conv3_2") + self.dwsl.append(dws32) + + dws41 = self.add_sublayer( + sublayer=DepthwiseSeparable( + num_channels=int(256 * scale), + num_filters1=256, + num_filters2=256, + num_groups=256, + stride=1, + scale=scale), + name="conv4_1") + self.dwsl.append(dws41) + + dws42 = self.add_sublayer( + sublayer=DepthwiseSeparable( + num_channels=int(256 * scale), + num_filters1=256, + num_filters2=512, + num_groups=256, + stride=2, + scale=scale), + name="conv4_2") + self.dwsl.append(dws42) + + for i in range(5): + tmp = self.add_sublayer( + sublayer=DepthwiseSeparable( + num_channels=int(512 * scale), + num_filters1=512, + num_filters2=512, + num_groups=512, + stride=1, + scale=scale), + name="conv5_" + str(i + 1)) + self.dwsl.append(tmp) + + dws56 = self.add_sublayer( + sublayer=DepthwiseSeparable( + num_channels=int(512 * scale), + num_filters1=512, + num_filters2=1024, + num_groups=512, + stride=2, + scale=scale), + name="conv5_6") + self.dwsl.append(dws56) + + dws6 = self.add_sublayer( + sublayer=DepthwiseSeparable( + num_channels=int(1024 * scale), + num_filters1=1024, + num_filters2=1024, + num_groups=1024, + stride=1, + scale=scale), + name="conv6") + self.dwsl.append(dws6) + + self.pool2d_avg = Pool2D(pool_type='avg', global_pooling=True) + + self.out = Linear( + int(1024 * scale), + class_dim, + act='softmax', + param_attr=ParamAttr( + initializer=MSRA(), name=self.full_name() + "fc7_weights"), + bias_attr=ParamAttr(name="fc7_offset")) + + def forward(self, inputs): + y = self.conv1(inputs) + for dws in self.dwsl: + y = dws(y) + y = self.pool2d_avg(y) + y = fluid.layers.reshape(y, shape=[-1, 1024]) + y = self.out(y) + return y + + +def _mobilenet(arch, pretrained=False, **kwargs): + model = MobileNetV1(**kwargs) + if pretrained: + assert arch in model_urls, "{} model do not have a pretrained model now, you should set pretrained=False".format( + arch) + weight_path = get_weights_path(model_urls[arch][0], + model_urls[arch][1]) + assert weight_path.endswith( + '.pdparams'), "suffix of weight must be .pdparams" + model.load(weight_path[:-9]) + + return model + + +def mobilenet_v1(pretrained=False, scale=1.0): + model = _mobilenet('mobilenetv1_' + str(scale), pretrained, scale=scale) + return model diff --git a/models/mobilenetv2.py b/models/mobilenetv2.py new file mode 100644 index 0000000000000..0079ee79d932a --- /dev/null +++ b/models/mobilenetv2.py @@ -0,0 +1,252 @@ +# 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 numpy as np +import paddle +import paddle.fluid as fluid +from paddle.fluid.param_attr import ParamAttr +from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear + +from model import Model +from .download import get_weights_path + +__all__ = ['MobileNetV2', 'mobilenet_v2'] + +model_urls = { + 'mobilenetv2_1.0': + ('https://paddle-hapi.bj.bcebos.com/models/mobilenet_v2_x1.0.pdparams', + '8ff74f291f72533f2a7956a4efff9d88') +} + + +class ConvBNLayer(fluid.dygraph.Layer): + def __init__(self, + num_channels, + filter_size, + num_filters, + stride, + padding, + channels=None, + num_groups=1, + use_cudnn=True): + super(ConvBNLayer, self).__init__() + + tmp_param = ParamAttr(name=self.full_name() + "_weights") + self._conv = Conv2D( + num_channels=num_channels, + num_filters=num_filters, + filter_size=filter_size, + stride=stride, + padding=padding, + groups=num_groups, + act=None, + use_cudnn=use_cudnn, + param_attr=tmp_param, + bias_attr=False) + + self._batch_norm = BatchNorm( + num_filters, + param_attr=ParamAttr(name=self.full_name() + "_bn" + "_scale"), + bias_attr=ParamAttr(name=self.full_name() + "_bn" + "_offset"), + moving_mean_name=self.full_name() + "_bn" + '_mean', + moving_variance_name=self.full_name() + "_bn" + '_variance') + + def forward(self, inputs, if_act=True): + y = self._conv(inputs) + y = self._batch_norm(y) + if if_act: + y = fluid.layers.relu6(y) + return y + + +class InvertedResidualUnit(fluid.dygraph.Layer): + def __init__( + self, + num_channels, + num_in_filter, + num_filters, + stride, + filter_size, + padding, + expansion_factor, ): + super(InvertedResidualUnit, self).__init__() + num_expfilter = int(round(num_in_filter * expansion_factor)) + self._expand_conv = ConvBNLayer( + num_channels=num_channels, + num_filters=num_expfilter, + filter_size=1, + stride=1, + padding=0, + num_groups=1) + + self._bottleneck_conv = ConvBNLayer( + num_channels=num_expfilter, + num_filters=num_expfilter, + filter_size=filter_size, + stride=stride, + padding=padding, + num_groups=num_expfilter, + use_cudnn=False) + + self._linear_conv = ConvBNLayer( + num_channels=num_expfilter, + num_filters=num_filters, + filter_size=1, + stride=1, + padding=0, + num_groups=1) + + def forward(self, inputs, ifshortcut): + y = self._expand_conv(inputs, if_act=True) + y = self._bottleneck_conv(y, if_act=True) + y = self._linear_conv(y, if_act=False) + if ifshortcut: + y = fluid.layers.elementwise_add(inputs, y) + return y + + +class InvresiBlocks(fluid.dygraph.Layer): + def __init__(self, in_c, t, c, n, s): + super(InvresiBlocks, self).__init__() + + self._first_block = InvertedResidualUnit( + num_channels=in_c, + num_in_filter=in_c, + num_filters=c, + stride=s, + filter_size=3, + padding=1, + expansion_factor=t) + + self._inv_blocks = [] + for i in range(1, n): + tmp = self.add_sublayer( + sublayer=InvertedResidualUnit( + num_channels=c, + num_in_filter=c, + num_filters=c, + stride=1, + filter_size=3, + padding=1, + expansion_factor=t), + name=self.full_name() + "_" + str(i + 1)) + self._inv_blocks.append(tmp) + + def forward(self, inputs): + y = self._first_block(inputs, ifshortcut=False) + for inv_block in self._inv_blocks: + y = inv_block(y, ifshortcut=True) + return y + + +class MobileNetV2(Model): + """MobileNetV2 model from + `"MobileNetV2: Inverted Residuals and Linear Bottlenecks" `_. + + Args: + scale (float): scale of channels in each layer. Default: 1.0. + class_dim (int): output dim of last fc layer. Default: 1000. + """ + + def __init__(self, scale=1.0, class_dim=1000): + super(MobileNetV2, self).__init__() + self.scale = scale + self.class_dim = class_dim + + bottleneck_params_list = [ + (1, 16, 1, 1), + (6, 24, 2, 2), + (6, 32, 3, 2), + (6, 64, 4, 2), + (6, 96, 3, 1), + (6, 160, 3, 2), + (6, 320, 1, 1), + ] + + #1. conv1 + self._conv1 = ConvBNLayer( + num_channels=3, + num_filters=int(32 * scale), + filter_size=3, + stride=2, + padding=1) + + #2. bottleneck sequences + self._invl = [] + i = 1 + in_c = int(32 * scale) + for layer_setting in bottleneck_params_list: + t, c, n, s = layer_setting + i += 1 + tmp = self.add_sublayer( + sublayer=InvresiBlocks( + in_c=in_c, t=t, c=int(c * scale), n=n, s=s), + name='conv' + str(i)) + self._invl.append(tmp) + in_c = int(c * scale) + + #3. last_conv + self._out_c = int(1280 * scale) if scale > 1.0 else 1280 + self._conv9 = ConvBNLayer( + num_channels=in_c, + num_filters=self._out_c, + filter_size=1, + stride=1, + padding=0) + + #4. pool + self._pool2d_avg = Pool2D(pool_type='avg', global_pooling=True) + + #5. fc + tmp_param = ParamAttr(name=self.full_name() + "fc10_weights") + self._fc = Linear( + self._out_c, + class_dim, + act='softmax', + param_attr=tmp_param, + bias_attr=ParamAttr(name="fc10_offset")) + + def forward(self, inputs): + y = self._conv1(inputs, if_act=True) + for inv in self._invl: + y = inv(y) + y = self._conv9(y, if_act=True) + y = self._pool2d_avg(y) + y = fluid.layers.reshape(y, shape=[-1, self._out_c]) + y = self._fc(y) + return y + + +def _mobilenet(arch, pretrained=False, **kwargs): + model = MobileNetV2(**kwargs) + if pretrained: + assert arch in model_urls, "{} model do not have a pretrained model now, you should set pretrained=False".format( + arch) + weight_path = get_weights_path(model_urls[arch][0], + model_urls[arch][1]) + assert weight_path.endswith( + '.pdparams'), "suffix of weight must be .pdparams" + model.load(weight_path[:-9]) + + return model + + +def mobilenet_v2(pretrained=False, scale=1.0): + """MobileNetV2 + + Args: + pretrained (bool): If True, returns a model pre-trained on ImageNet + """ + model = _mobilenet('mobilenetv2_' + str(scale), pretrained, scale=scale) + return model diff --git a/models/resnet.py b/models/resnet.py index 1865a472f7e5f..f2cf4b603e689 100644 --- a/models/resnet.py +++ b/models/resnet.py @@ -1,3 +1,17 @@ +# 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. + from __future__ import division from __future__ import print_function @@ -11,7 +25,9 @@ from model import Model from .download import get_weights_path -__all__ = ['ResNet', 'resnet50', 'resnet101', 'resnet152'] +__all__ = [ + 'ResNet', 'resnet18', 'resnet34', 'resnet50', 'resnet101', 'resnet152' +] model_urls = { 'resnet50': ('https://paddle-hapi.bj.bcebos.com/models/resnet50.pdparams', @@ -48,7 +64,52 @@ def forward(self, inputs): return x +class BasicBlock(fluid.dygraph.Layer): + + expansion = 1 + + def __init__(self, num_channels, num_filters, stride, shortcut=True): + super(BasicBlock, self).__init__() + + self.conv0 = ConvBNLayer( + num_channels=num_channels, + num_filters=num_filters, + filter_size=3, + act='relu') + self.conv1 = ConvBNLayer( + num_channels=num_filters, + num_filters=num_filters, + filter_size=3, + stride=stride, + act='relu') + + if not shortcut: + self.short = ConvBNLayer( + num_channels=num_channels, + num_filters=num_filters, + filter_size=1, + stride=stride) + + self.shortcut = shortcut + + def forward(self, inputs): + y = self.conv0(inputs) + conv1 = self.conv1(y) + + if self.shortcut: + short = inputs + else: + short = self.short(inputs) + + y = short + conv1 + + return fluid.layers.relu(y) + + class BottleneckBlock(fluid.dygraph.Layer): + + expansion = 4 + def __init__(self, num_channels, num_filters, stride, shortcut=True): super(BottleneckBlock, self).__init__() @@ -65,20 +126,20 @@ def __init__(self, num_channels, num_filters, stride, shortcut=True): act='relu') self.conv2 = ConvBNLayer( num_channels=num_filters, - num_filters=num_filters * 4, + num_filters=num_filters * self.expansion, filter_size=1, act=None) if not shortcut: self.short = ConvBNLayer( num_channels=num_channels, - num_filters=num_filters * 4, + num_filters=num_filters * self.expansion, filter_size=1, stride=stride) self.shortcut = shortcut - self._num_channels_out = num_filters * 4 + self._num_channels_out = num_filters * self.expansion def forward(self, inputs): x = self.conv0(inputs) @@ -92,16 +153,25 @@ def forward(self, inputs): x = fluid.layers.elementwise_add(x=short, y=conv2) - layer_helper = LayerHelper(self.full_name(), act='relu') - return layer_helper.append_activation(x) - # return fluid.layers.relu(x) + return fluid.layers.relu(x) class ResNet(Model): + """ResNet model from + `"Deep Residual Learning for Image Recognition" `_ + + Args: + Block (BasicBlock|BottleneckBlock): block module of model. + depth (int): layers of resnet, default: 50. + num_classes (int): output dim of last fc layer, default: 1000. + """ + def __init__(self, Block, depth=50, num_classes=1000): super(ResNet, self).__init__() layer_config = { + 18: [2, 2, 2, 2], + 34: [3, 4, 6, 3], 50: [3, 4, 6, 3], 101: [3, 4, 23, 3], 152: [3, 8, 36, 3], @@ -111,8 +181,9 @@ def __init__(self, Block, depth=50, num_classes=1000): layer_config.keys(), depth) layers = layer_config[depth] - num_in = [64, 256, 512, 1024] - num_out = [64, 128, 256, 512] + + in_channels = 64 + out_channels = [64, 128, 256, 512] self.conv = ConvBNLayer( num_channels=3, @@ -128,9 +199,11 @@ def __init__(self, Block, depth=50, num_classes=1000): blocks = [] shortcut = False for b in range(num_blocks): + if b == 1: + in_channels = out_channels[idx] * Block.expansion block = Block( - num_channels=num_in[idx] if b == 0 else num_out[idx] * 4, - num_filters=num_out[idx], + num_channels=in_channels, + num_filters=out_channels[idx], stride=2 if b == 0 and idx != 0 else 1, shortcut=shortcut) blocks.append(block) @@ -142,8 +215,8 @@ def __init__(self, Block, depth=50, num_classes=1000): self.global_pool = Pool2D( pool_size=7, pool_type='avg', global_pooling=True) - stdv = 1.0 / math.sqrt(2048 * 1.0) - self.fc_input_dim = num_out[-1] * 4 * 1 * 1 + stdv = 1.0 / math.sqrt(out_channels[-1] * Block.expansion * 1.0) + self.fc_input_dim = out_channels[-1] * Block.expansion * 1 * 1 self.fc = Linear( self.fc_input_dim, num_classes, @@ -175,13 +248,46 @@ def _resnet(arch, Block, depth, pretrained): return model +def resnet18(pretrained=False): + """ResNet 18-layer model + + Args: + pretrained (bool): If True, returns a model pre-trained on ImageNet + """ + return _resnet('resnet18', BasicBlock, 18, pretrained) + + +def resnet34(pretrained=False): + """ResNet 34-layer model + + Args: + pretrained (bool): If True, returns a model pre-trained on ImageNet + """ + return _resnet('resnet34', BasicBlock, 34, pretrained) + + def resnet50(pretrained=False): + """ResNet 50-layer model + + Args: + pretrained (bool): If True, returns a model pre-trained on ImageNet + """ return _resnet('resnet50', BottleneckBlock, 50, pretrained) def resnet101(pretrained=False): + """ResNet 101-layer model + + Args: + pretrained (bool): If True, returns a model pre-trained on ImageNet + """ return _resnet('resnet101', BottleneckBlock, 101, pretrained) def resnet152(pretrained=False): + """ResNet 152-layer model + + Args: + pretrained (bool): If True, returns a model pre-trained on ImageNet + """ return _resnet('resnet152', BottleneckBlock, 152, pretrained) diff --git a/models/vgg.py b/models/vgg.py new file mode 100644 index 0000000000000..b8ca21f0c370c --- /dev/null +++ b/models/vgg.py @@ -0,0 +1,200 @@ +# 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 paddle +import paddle.fluid as fluid +from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear +from paddle.fluid.dygraph.container import Sequential + +from model import Model +from .download import get_weights_path + +__all__ = [ + 'VGG', + 'vgg11', + 'vgg11_bn', + 'vgg13', + 'vgg13_bn', + 'vgg16', + 'vgg16_bn', + 'vgg19_bn', + 'vgg19', +] + +model_urls = { + 'vgg16': ('https://paddle-hapi.bj.bcebos.com/models/vgg16.pdparams', + 'c788f453a3b999063e8da043456281ee') +} + + +class Classifier(fluid.dygraph.Layer): + def __init__(self, num_classes): + super(Classifier, self).__init__() + self.linear1 = Linear(512 * 7 * 7, 4096) + self.linear2 = Linear(4096, 4096) + self.linear3 = Linear(4096, num_classes, act='softmax') + + def forward(self, x): + x = self.linear1(x) + x = fluid.layers.relu(x) + x = fluid.layers.dropout(x, 0.5) + x = self.linear2(x) + x = fluid.layers.relu(x) + x = fluid.layers.dropout(x, 0.5) + out = self.linear3(x) + return out + + +class VGG(Model): + """VGG model from + `"Very Deep Convolutional Networks For Large-Scale Image Recognition" `_ + + Args: + features (fluid.dygraph.Layer): vgg features create by function make_layers. + num_classes (int): output dim of last fc layer. Default: 1000. + """ + + def __init__(self, features, num_classes=1000): + super(VGG, self).__init__() + self.features = features + classifier = Classifier(num_classes) + self.classifier = self.add_sublayer("classifier", + Sequential(classifier)) + + def forward(self, x): + x = self.features(x) + x = fluid.layers.flatten(x, 1) + x = self.classifier(x) + return x + + +def make_layers(cfg, batch_norm=False): + layers = [] + in_channels = 3 + + for v in cfg: + if v == 'M': + layers += [Pool2D(pool_size=2, pool_stride=2)] + else: + if batch_norm: + conv2d = Conv2D(in_channels, v, filter_size=3, padding=1) + layers += [conv2d, BatchNorm(v, act='relu')] + else: + conv2d = Conv2D( + in_channels, v, filter_size=3, padding=1, act='relu') + layers += [conv2d] + in_channels = v + return Sequential(*layers) + + +cfgs = { + 'A': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'], + 'B': + [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'], + 'D': [ + 64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', + 512, 512, 512, 'M' + ], + 'E': [ + 64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, + 512, 'M', 512, 512, 512, 512, 'M' + ], +} + + +def _vgg(arch, cfg, batch_norm, pretrained, **kwargs): + model = VGG(make_layers(cfgs[cfg], batch_norm=batch_norm), **kwargs) + + if pretrained: + assert arch in model_urls, "{} model do not have a pretrained model now, you should set pretrained=False".format( + arch) + weight_path = get_weights_path(model_urls[arch][0], + model_urls[arch][1]) + assert weight_path.endswith( + '.pdparams'), "suffix of weight must be .pdparams" + model.load(weight_path[:-9]) + + return model + + +def vgg11(pretrained=False, **kwargs): + """VGG 11-layer model + + Args: + pretrained (bool): If True, returns a model pre-trained on ImageNet + """ + return _vgg('vgg11', 'A', False, pretrained, **kwargs) + + +def vgg11_bn(pretrained=False, **kwargs): + """VGG 11-layer model with batch normalization + + Args: + pretrained (bool): If True, returns a model pre-trained on ImageNet + """ + return _vgg('vgg11_bn', 'A', True, pretrained, **kwargs) + + +def vgg13(pretrained=False, **kwargs): + """VGG 13-layer model + + Args: + pretrained (bool): If True, returns a model pre-trained on ImageNet + """ + return _vgg('vgg13', 'B', False, pretrained, **kwargs) + + +def vgg13_bn(pretrained=False, **kwargs): + """VGG 13-layer model with batch normalization + + Args: + pretrained (bool): If True, returns a model pre-trained on ImageNet + """ + return _vgg('vgg13_bn', 'B', True, pretrained, **kwargs) + + +def vgg16(pretrained=False, **kwargs): + """VGG 16-layer model + + Args: + pretrained (bool): If True, returns a model pre-trained on ImageNet + """ + return _vgg('vgg16', 'D', False, pretrained, **kwargs) + + +def vgg16_bn(pretrained=False, **kwargs): + """VGG 16-layer with batch normalization + + Args: + pretrained (bool): If True, returns a model pre-trained on ImageNet + """ + return _vgg('vgg16_bn', 'D', True, pretrained, **kwargs) + + +def vgg19(pretrained=False, **kwargs): + """VGG 19-layer model + + Args: + pretrained (bool): If True, returns a model pre-trained on ImageNet + """ + return _vgg('vgg19', 'E', False, pretrained, **kwargs) + + +def vgg19_bn(pretrained=False, **kwargs): + """VGG 19-layer model with batch normalization + + Args: + pretrained (bool): If True, returns a model pre-trained on ImageNet + """ + return _vgg('vgg19_bn', 'E', True, pretrained, **kwargs)