From de4ebabfc8340943102c821b60e80e2ec626508e Mon Sep 17 00:00:00 2001 From: littletomatodonkey Date: Sun, 13 Sep 2020 10:32:47 +0000 Subject: [PATCH] remove unused files --- ppcls/modeling/architectures/csp_resnet.py | 258 ---------- ppcls/modeling/architectures/darts_gs.py | 543 --------------------- ppcls/modeling/architectures/layers.py | 250 ---------- 3 files changed, 1051 deletions(-) delete mode 100644 ppcls/modeling/architectures/csp_resnet.py delete mode 100644 ppcls/modeling/architectures/darts_gs.py delete mode 100644 ppcls/modeling/architectures/layers.py diff --git a/ppcls/modeling/architectures/csp_resnet.py b/ppcls/modeling/architectures/csp_resnet.py deleted file mode 100644 index b1be8d25..00000000 --- a/ppcls/modeling/architectures/csp_resnet.py +++ /dev/null @@ -1,258 +0,0 @@ -# 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. - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import math - -import paddle.fluid as fluid -from paddle.fluid.param_attr import ParamAttr - -__all__ = [ - "CSPResNet50_leaky", "CSPResNet50_mish", "CSPResNet101_leaky", - "CSPResNet101_mish" -] - - -class CSPResNet(): - def __init__(self, layers=50, act="leaky_relu"): - self.layers = layers - self.act = act - - def net(self, input, class_dim=1000, data_format="NCHW"): - layers = self.layers - supported_layers = [50, 101] - assert layers in supported_layers, \ - "supported layers are {} but input layer is {}".format( - supported_layers, layers) - - if layers == 50: - depth = [3, 3, 5, 2] - elif layers == 101: - depth = [3, 3, 22, 2] - - num_filters = [64, 128, 256, 512] - - conv = self.conv_bn_layer( - input=input, - num_filters=64, - filter_size=7, - stride=2, - act=self.act, - name="conv1", - data_format=data_format) - conv = fluid.layers.pool2d( - input=conv, - pool_size=2, - pool_stride=2, - pool_padding=0, - pool_type='max', - data_format=data_format) - - for block in range(len(depth)): - conv_name = "res" + str(block + 2) + chr(97) - if block != 0: - conv = self.conv_bn_layer( - input=conv, - num_filters=num_filters[block], - filter_size=3, - stride=2, - act=self.act, - name=conv_name + "_downsample", - data_format=data_format) - - # split - left = conv - right = conv - if block == 0: - ch = num_filters[block] - else: - ch = num_filters[block] * 2 - right = self.conv_bn_layer( - input=right, - num_filters=ch, - filter_size=1, - act=self.act, - name=conv_name + "_right_first_route", - data_format=data_format) - - for i in range(depth[block]): - conv_name = "res" + str(block + 2) + chr(97 + i) - - right = self.bottleneck_block( - input=right, - num_filters=num_filters[block], - stride=1, - name=conv_name, - data_format=data_format) - - # route - left = self.conv_bn_layer( - input=left, - num_filters=num_filters[block] * 2, - filter_size=1, - act=self.act, - name=conv_name + "_left_route", - data_format=data_format) - right = self.conv_bn_layer( - input=right, - num_filters=num_filters[block] * 2, - filter_size=1, - act=self.act, - name=conv_name + "_right_route", - data_format=data_format) - conv = fluid.layers.concat([left, right], axis=1) - - conv = self.conv_bn_layer( - input=conv, - num_filters=num_filters[block] * 2, - filter_size=1, - stride=1, - act=self.act, - name=conv_name + "_merged_transition", - data_format=data_format) - - pool = fluid.layers.pool2d( - input=conv, - pool_type='avg', - global_pooling=True, - data_format=data_format) - stdv = 1.0 / math.sqrt(pool.shape[1] * 1.0) - out = fluid.layers.fc( - input=pool, - size=class_dim, - param_attr=fluid.param_attr.ParamAttr( - name="fc_0.w_0", - initializer=fluid.initializer.Uniform(-stdv, stdv)), - bias_attr=ParamAttr(name="fc_0.b_0")) - return out - - def conv_bn_layer(self, - input, - num_filters, - filter_size, - stride=1, - groups=1, - act=None, - name=None, - data_format='NCHW'): - conv = fluid.layers.conv2d( - input=input, - num_filters=num_filters, - filter_size=filter_size, - stride=stride, - padding=(filter_size - 1) // 2, - groups=groups, - act=None, - param_attr=ParamAttr(name=name + "_weights"), - bias_attr=False, - name=name + '.conv2d.output.1', - data_format=data_format) - - if name == "conv1": - bn_name = "bn_" + name - else: - bn_name = "bn" + name[3:] - bn = fluid.layers.batch_norm( - input=conv, - act=None, - name=bn_name + '.output.1', - param_attr=ParamAttr(name=bn_name + '_scale'), - bias_attr=ParamAttr(bn_name + '_offset'), - moving_mean_name=bn_name + '_mean', - moving_variance_name=bn_name + '_variance', - data_layout=data_format) - if act == "relu": - bn = fluid.layers.relu(bn) - elif act == "leaky_relu": - bn = fluid.layers.leaky_relu(bn) - elif act == "mish": - bn = self._mish(bn) - return bn - - def _mish(self, input): - return input * fluid.layers.tanh(self._softplus(input)) - - def _softplus(self, input): - expf = fluid.layers.exp(fluid.layers.clip(input, -200, 50)) - return fluid.layers.log(1 + expf) - - def shortcut(self, input, ch_out, stride, is_first, name, data_format): - if data_format == 'NCHW': - ch_in = input.shape[1] - else: - ch_in = input.shape[-1] - if ch_in != ch_out or stride != 1 or is_first is True: - return self.conv_bn_layer( - input, ch_out, 1, stride, name=name, data_format=data_format) - else: - return input - - def bottleneck_block(self, input, num_filters, stride, name, data_format): - conv0 = self.conv_bn_layer( - input=input, - num_filters=num_filters, - filter_size=1, - act="leaky_relu", - name=name + "_branch2a", - data_format=data_format) - conv1 = self.conv_bn_layer( - input=conv0, - num_filters=num_filters, - filter_size=3, - stride=stride, - act="leaky_relu", - name=name + "_branch2b", - data_format=data_format) - conv2 = self.conv_bn_layer( - input=conv1, - num_filters=num_filters * 2, - filter_size=1, - act=None, - name=name + "_branch2c", - data_format=data_format) - - short = self.shortcut( - input, - num_filters * 2, - stride, - is_first=False, - name=name + "_branch1", - data_format=data_format) - - ret = short + conv2 - ret = fluid.layers.leaky_relu(ret, alpha=0.1) - return ret - - -def CSPResNet50_leaky(): - model = CSPResNet(layers=50, act="leaky_relu") - return model - - -def CSPResNet50_mish(): - model = CSPResNet(layers=50, act="mish") - return model - - -def CSPResNet101_leaky(): - model = CSPResNet(layers=101, act="leaky_relu") - return model - - -def CSPResNet101_mish(): - model = CSPResNet(layers=101, act="mish") - return model diff --git a/ppcls/modeling/architectures/darts_gs.py b/ppcls/modeling/architectures/darts_gs.py deleted file mode 100644 index ff5c9655..00000000 --- a/ppcls/modeling/architectures/darts_gs.py +++ /dev/null @@ -1,543 +0,0 @@ -# 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. -# -# Based on: -# -------------------------------------------------------- -# DARTS -# Copyright (c) 2018, Hanxiao Liu. -# Licensed under the Apache License, Version 2.0; -# -------------------------------------------------------- - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -import os -import sys -import numpy as np -import time -import functools -import paddle -import paddle.fluid as fluid -from paddle.fluid.param_attr import ParamAttr -from paddle.fluid.initializer import Xavier -from paddle.fluid.initializer import Normal -from paddle.fluid.initializer import Constant - -from collections import namedtuple -Genotype = namedtuple('Genotype', 'normal normal_concat reduce reduce_concat') - -arch_dict = { - 'DARTS_GS_6M': Genotype( - normal=[('sep_conv_3x3', 1), ('sep_conv_3x3', 0), ('sep_conv_5x5', 1), - ('sep_conv_5x5', 0), ('sep_conv_3x3', 2), ('sep_conv_3x3', 1), - ('skip_connect', 4), ('sep_conv_3x3', 3)], - normal_concat=range(2, 6), - reduce=[('sep_conv_5x5', 0), ('max_pool_3x3', 1), ('dil_conv_5x5', 2), - ('sep_conv_5x5', 0), ('sep_conv_3x3', 1), ('dil_conv_5x5', 3), - ('dil_conv_3x3', 1), ('sep_conv_3x3', 2)], - reduce_concat=range(2, 6)), - 'DARTS_GS_4M': Genotype( - normal=[('sep_conv_3x3', 0), ('sep_conv_3x3', 1), ('sep_conv_3x3', 0), - ('sep_conv_3x3', 1), ('sep_conv_3x3', 1), ('skip_connect', 0), - ('skip_connect', 0), ('dil_conv_3x3', 1)], - normal_concat=range(2, 6), - reduce=[('max_pool_3x3', 0), ('max_pool_3x3', 1), ('max_pool_3x3', 0), - ('avg_pool_3x3', 1), ('skip_connect', 3), ('skip_connect', 2), - ('sep_conv_3x3', 0), ('sep_conv_5x5', 2)], - reduce_concat=range(2, 6)), -} - -__all__ = list(arch_dict.keys()) - -OPS = { - 'none' : lambda input, C, stride, name, affine: Zero(input, stride, name), - 'avg_pool_3x3' : lambda input, C, stride, name, affine: fluid.layers.pool2d(input, 3, 'avg', pool_stride=stride, pool_padding=1, name=name), - 'max_pool_3x3' : lambda input, C, stride, name, affine: fluid.layers.pool2d(input, 3, 'max', pool_stride=stride, pool_padding=1, name=name), - 'skip_connect' : lambda input,C, stride, name, affine: Identity(input, name) if stride == 1 else FactorizedReduce(input, C, name=name, affine=affine), - 'sep_conv_3x3' : lambda input,C, stride, name, affine: SepConv(input, C, C, 3, stride, 1, name=name, affine=affine), - 'sep_conv_5x5' : lambda input,C, stride, name, affine: SepConv(input, C, C, 5, stride, 2, name=name, affine=affine), - 'sep_conv_7x7' : lambda input,C, stride, name, affine: SepConv(input, C, C, 7, stride, 3, name=name, affine=affine), - 'dil_conv_3x3' : lambda input,C, stride, name, affine: DilConv(input, C, C, 3, stride, 2, 2, name=name, affine=affine), - 'dil_conv_5x5' : lambda input,C, stride, name, affine: DilConv(input, C, C, 5, stride, 4, 2, name=name, affine=affine), - 'conv_7x1_1x7' : lambda input,C, stride, name, affine: SevenConv(input, C, name=name, affine=affine) -} - - -def ReLUConvBN(input, - C_out, - kernel_size, - stride, - padding, - name='', - affine=True): - relu_a = fluid.layers.relu(input) - conv2d_a = fluid.layers.conv2d( - relu_a, C_out, kernel_size, stride, padding, bias_attr=False) - if affine: - reluconvbn_out = fluid.layers.batch_norm( - conv2d_a, - param_attr=ParamAttr( - initializer=Constant(1.), name=name + 'op.2.weight'), - bias_attr=ParamAttr( - initializer=Constant(0.), name=name + 'op.2.bias'), - moving_mean_name=name + 'op.2.running_mean', - moving_variance_name=name + 'op.2.running_var') - else: - reluconvbn_out = fluid.layers.batch_norm( - conv2d_a, - param_attr=ParamAttr( - initializer=Constant(1.), - learning_rate=0., - name=name + 'op.2.weight'), - bias_attr=ParamAttr( - initializer=Constant(0.), - learning_rate=0., - name=name + 'op.2.bias'), - moving_mean_name=name + 'op.2.running_mean', - moving_variance_name=name + 'op.2.running_var') - return reluconvbn_out - - -def DilConv(input, - C_in, - C_out, - kernel_size, - stride, - padding, - dilation, - name='', - affine=True): - relu_a = fluid.layers.relu(input) - conv2d_a = fluid.layers.conv2d( - relu_a, - C_in, - kernel_size, - stride, - padding, - dilation, - groups=C_in, - bias_attr=False, - use_cudnn=False) - conv2d_b = fluid.layers.conv2d(conv2d_a, C_out, 1, bias_attr=False) - if affine: - dilconv_out = fluid.layers.batch_norm( - conv2d_b, - param_attr=ParamAttr( - initializer=Constant(1.), name=name + 'op.3.weight'), - bias_attr=ParamAttr( - initializer=Constant(0.), name=name + 'op.3.bias'), - moving_mean_name=name + 'op.3.running_mean', - moving_variance_name=name + 'op.3.running_var') - else: - dilconv_out = fluid.layers.batch_norm( - conv2d_b, - param_attr=ParamAttr( - initializer=Constant(1.), - learning_rate=0., - name=name + 'op.3.weight'), - bias_attr=ParamAttr( - initializer=Constant(0.), - learning_rate=0., - name=name + 'op.3.bias'), - moving_mean_name=name + 'op.3.running_mean', - moving_variance_name=name + 'op.3.running_var') - return dilconv_out - - -def SepConv(input, - C_in, - C_out, - kernel_size, - stride, - padding, - name='', - affine=True): - relu_a = fluid.layers.relu(input) - conv2d_a = fluid.layers.conv2d( - relu_a, - C_in, - kernel_size, - stride, - padding, - groups=C_in, - bias_attr=False, - use_cudnn=False) - conv2d_b = fluid.layers.conv2d(conv2d_a, C_in, 1, bias_attr=False) - if affine: - bn_a = fluid.layers.batch_norm( - conv2d_b, - param_attr=ParamAttr( - initializer=Constant(1.), name=name + 'op.3.weight'), - bias_attr=ParamAttr( - initializer=Constant(0.), name=name + 'op.3.bias'), - moving_mean_name=name + 'op.3.running_mean', - moving_variance_name=name + 'op.3.running_var') - else: - bn_a = fluid.layers.batch_norm( - conv2d_b, - param_attr=ParamAttr( - initializer=Constant(1.), - learning_rate=0., - name=name + 'op.3.weight'), - bias_attr=ParamAttr( - initializer=Constant(0.), - learning_rate=0., - name=name + 'op.3.bias'), - moving_mean_name=name + 'op.3.running_mean', - moving_variance_name=name + 'op.3.running_var') - - relu_b = fluid.layers.relu(bn_a) - conv2d_d = fluid.layers.conv2d( - relu_b, - C_in, - kernel_size, - 1, - padding, - groups=C_in, - bias_attr=False, - use_cudnn=False) - conv2d_e = fluid.layers.conv2d(conv2d_d, C_out, 1, bias_attr=False) - if affine: - sepconv_out = fluid.layers.batch_norm( - conv2d_e, - param_attr=ParamAttr( - initializer=Constant(1.), name=name + 'op.7.weight'), - bias_attr=ParamAttr( - initializer=Constant(0.), name=name + 'op.7.bias'), - moving_mean_name=name + 'op.7.running_mean', - moving_variance_name=name + 'op.7.running_var') - else: - sepconv_out = fluid.layers.batch_norm( - conv2d_e, - param_attr=ParamAttr( - initializer=Constant(1.), - learning_rate=0., - name=name + 'op.7.weight'), - bias_attr=ParamAttr( - initializer=Constant(0.), - learning_rate=0., - name=name + 'op.7.bias'), - moving_mean_name=name + 'op.7.running_mean', - moving_variance_name=name + 'op.7.running_var') - return sepconv_out - - -def SevenConv(input, C_out, stride, name='', affine=True): - relu_a = fluid.layers.relu(input) - conv2d_a = fluid.layers.conv2d( - relu_a, - C_out, (1, 7), (1, stride), (0, 3), - param_attr=ParamAttr( - initializer=Xavier( - uniform=False, fan_in=0), - name=name + 'op.1.weight'), - bias_attr=False) - conv2d_b = fluid.layers.conv2d( - conv2d_a, - C_out, (7, 1), (stride, 1), (3, 0), - param_attr=ParamAttr( - initializer=Xavier( - uniform=False, fan_in=0), - name=name + 'op.2.weight'), - bias_attr=False) - if affine: - out = fluid.layers.batch_norm( - conv2d_b, - param_attr=ParamAttr( - initializer=Constant(1.), name=name + 'op.3.weight'), - bias_attr=ParamAttr( - initializer=Constant(0.), name=name + 'op.3.bias'), - moving_mean_name=name + 'op.3.running_mean', - moving_variance_name=name + 'op.3.running_var') - else: - out = fluid.layers.batch_norm( - conv2d_b, - param_attr=ParamAttr( - initializer=Constant(1.), - learning_rate=0., - name=name + 'op.3.weight'), - bias_attr=ParamAttr( - initializer=Constant(0.), - learning_rate=0., - name=name + 'op.3.bias'), - moving_mean_name=name + 'op.3.running_mean', - moving_variance_name=name + 'op.3.running_var') - - -def Identity(input, name=''): - return input - - -def Zero(input, stride, name=''): - ones = np.ones(input.shape[-2:]) - ones[::stride, ::stride] = 0 - ones = fluid.layers.assign(ones) - return input * ones - - -def FactorizedReduce(input, C_out, name='', affine=True): - relu_a = fluid.layers.relu(input) - conv2d_a = fluid.layers.conv2d( - relu_a, - C_out // 2, - 1, - 2, - param_attr=ParamAttr( - initializer=Xavier( - uniform=False, fan_in=0), - name=name + 'conv_1.weight'), - bias_attr=False) - h_end = relu_a.shape[2] - w_end = relu_a.shape[3] - slice_a = fluid.layers.slice(relu_a, [2, 3], [1, 1], [h_end, w_end]) - conv2d_b = fluid.layers.conv2d( - slice_a, - C_out // 2, - 1, - 2, - param_attr=ParamAttr( - initializer=Xavier( - uniform=False, fan_in=0), - name=name + 'conv_2.weight'), - bias_attr=False) - out = fluid.layers.concat([conv2d_a, conv2d_b], axis=1) - if affine: - out = fluid.layers.batch_norm( - out, - param_attr=ParamAttr( - initializer=Constant(1.), name=name + 'bn.weight'), - bias_attr=ParamAttr( - initializer=Constant(0.), name=name + 'bn.bias'), - moving_mean_name=name + 'bn.running_mean', - moving_variance_name=name + 'bn.running_var') - else: - out = fluid.layers.batch_norm( - out, - param_attr=ParamAttr( - initializer=Constant(1.), - learning_rate=0., - name=name + 'bn.weight'), - bias_attr=ParamAttr( - initializer=Constant(0.), - learning_rate=0., - name=name + 'bn.bias'), - moving_mean_name=name + 'bn.running_mean', - moving_variance_name=name + 'bn.running_var') - return out - - -class Cell(): - def __init__(self, genotype, C_prev_prev, C_prev, C, reduction, - reduction_prev): - - if reduction_prev: - self.preprocess0 = functools.partial(FactorizedReduce, C_out=C) - else: - self.preprocess0 = functools.partial( - ReLUConvBN, C_out=C, kernel_size=1, stride=1, padding=0) - self.preprocess1 = functools.partial( - ReLUConvBN, C_out=C, kernel_size=1, stride=1, padding=0) - if reduction: - op_names, indices = zip(*genotype.reduce) - concat = genotype.reduce_concat - else: - op_names, indices = zip(*genotype.normal) - concat = genotype.normal_concat - print(op_names, indices, concat, reduction) - self._compile(C, op_names, indices, concat, reduction) - - def _compile(self, C, op_names, indices, concat, reduction): - assert len(op_names) == len(indices) - self._steps = len(op_names) // 2 - self._concat = concat - self.multiplier = len(concat) - - self._ops = [] - for name, index in zip(op_names, indices): - stride = 2 if reduction and index < 2 else 1 - op = functools.partial(OPS[name], C=C, stride=stride, affine=True) - self._ops += [op] - self._indices = indices - - def forward(self, s0, s1, drop_prob, is_train, name): - self.training = is_train - preprocess0_name = name + 'preprocess0.' - preprocess1_name = name + 'preprocess1.' - s0 = self.preprocess0(s0, name=preprocess0_name) - s1 = self.preprocess1(s1, name=preprocess1_name) - out = [s0, s1] - for i in range(self._steps): - h1 = out[self._indices[2 * i]] - h2 = out[self._indices[2 * i + 1]] - op1 = self._ops[2 * i] - op2 = self._ops[2 * i + 1] - h3 = op1(h1, name=name + '_ops.' + str(2 * i) + '.') - h4 = op2(h2, name=name + '_ops.' + str(2 * i + 1) + '.') - if self.training and drop_prob > 0.: - if h3 != h1: - h3 = fluid.layers.dropout( - h3, - drop_prob, - dropout_implementation='upscale_in_train') - if h4 != h2: - h4 = fluid.layers.dropout( - h4, - drop_prob, - dropout_implementation='upscale_in_train') - s = h3 + h4 - out += [s] - return fluid.layers.concat([out[i] for i in self._concat], axis=1) - - -def AuxiliaryHeadImageNet(input, num_classes, aux_name='auxiliary_head'): - relu_a = fluid.layers.relu(input) - pool_a = fluid.layers.pool2d(relu_a, 5, 'avg', 2) - conv2d_a = fluid.layers.conv2d( - pool_a, 128, 1, name=aux_name + '.features.2', bias_attr=False) - bn_a_name = aux_name + '.features.3' - bn_a = fluid.layers.batch_norm( - conv2d_a, - act='relu', - name=bn_a_name, - param_attr=ParamAttr( - initializer=Constant(1.), name=bn_a_name + '.weight'), - bias_attr=ParamAttr( - initializer=Constant(0.), name=bn_a_name + '.bias'), - moving_mean_name=bn_a_name + '.running_mean', - moving_variance_name=bn_a_name + '.running_var') - conv2d_b = fluid.layers.conv2d( - bn_a, 768, 2, name=aux_name + '.features.5', bias_attr=False) - bn_b_name = aux_name + '.features.6' - bn_b = fluid.layers.batch_norm( - conv2d_b, - act='relu', - name=bn_b_name, - param_attr=ParamAttr( - initializer=Constant(1.), name=bn_b_name + '.weight'), - bias_attr=ParamAttr( - initializer=Constant(0.), name=bn_b_name + '.bias'), - moving_mean_name=bn_b_name + '.running_mean', - moving_variance_name=bn_b_name + '.running_var') - pool_b = fluid.layers.adaptive_pool2d(bn_b, (1, 1), "avg") - fc_name = aux_name + '.classifier' - fc = fluid.layers.fc(pool_b, - num_classes, - name=fc_name, - param_attr=ParamAttr( - initializer=Normal(scale=1e-3), - name=fc_name + '.weight'), - bias_attr=ParamAttr( - initializer=Constant(0.), name=fc_name + '.bias')) - return fc - - -def StemConv0(input, C_out): - conv_a = fluid.layers.conv2d( - input, C_out // 2, 3, stride=2, padding=1, bias_attr=False) - bn_a = fluid.layers.batch_norm( - conv_a, - act='relu', - param_attr=ParamAttr( - initializer=Constant(1.), name='stem0.1.weight'), - bias_attr=ParamAttr( - initializer=Constant(0.), name='stem0.1.bias'), - moving_mean_name='stem0.1.running_mean', - moving_variance_name='stem0.1.running_var') - - conv_b = fluid.layers.conv2d( - bn_a, C_out, 3, stride=2, padding=1, bias_attr=False) - bn_b = fluid.layers.batch_norm( - conv_b, - param_attr=ParamAttr( - initializer=Constant(1.), name='stem0.3.weight'), - bias_attr=ParamAttr( - initializer=Constant(0.), name='stem0.3.bias'), - moving_mean_name='stem0.3.running_mean', - moving_variance_name='stem0.3.running_var') - return bn_b - - -def StemConv1(input, C_out): - relu_a = fluid.layers.relu(input) - conv_a = fluid.layers.conv2d( - relu_a, C_out, 3, stride=2, padding=1, bias_attr=False) - bn_a = fluid.layers.batch_norm( - conv_a, - param_attr=ParamAttr( - initializer=Constant(1.), name='stem1.1.weight'), - bias_attr=ParamAttr( - initializer=Constant(0.), name='stem1.1.bias'), - moving_mean_name='stem1.1.running_mean', - moving_variance_name='stem1.1.running_var') - return bn_a - - -class NetworkImageNet(object): - def __init__(self, arch='DARTS_6M'): - self.class_num = 1000 - self.init_channel = 48 - self._layers = 14 - self._auxiliary = False - self.drop_path_prob = 0 - genotype = arch_dict[arch] - - C = self.init_channel - layers = self._layers - C_prev_prev, C_prev, C_curr = C, C, C - self.cells = [] - reduction_prev = True - for i in range(layers): - if i in [layers // 3, 2 * layers // 3]: - C_curr *= 2 - reduction = True - else: - reduction = False - cell = Cell(genotype, C_prev_prev, C_prev, C_curr, reduction, - reduction_prev) - reduction_prev = reduction - self.cells += [cell] - C_prev_prev, C_prev = C_prev, cell.multiplier * C_curr - if i == 2 * layers // 3: - C_to_auxiliary = C_prev - - def net(self, input, class_dim=1000, is_train=True): - self.logits_aux = None - num_channel = self.init_channel - s0 = StemConv0(input, num_channel) - s1 = StemConv1(s0, num_channel) - for i, cell in enumerate(self.cells): - name = 'cells.' + str(i) + '.' - s0, s1 = s1, cell.forward(s0, s1, self.drop_path_prob, is_train, - name) - if i == int(2 * self._layers // 3): - if self._auxiliary and is_train: - self.logits_aux = AuxiliaryHeadImageNet(s1, self.class_num) - out = fluid.layers.adaptive_pool2d(s1, (1, 1), "avg") - self.logits = fluid.layers.fc(out, - size=self.class_num, - param_attr=ParamAttr( - initializer=Normal(scale=1e-4), - name='classifier.weight'), - bias_attr=ParamAttr( - initializer=Constant(0.), - name='classifier.bias')) - return self.logits - - -def DARTS_GS_6M(): - return NetworkImageNet(arch='DARTS_GS_6M') - - -def DARTS_GS_4M(): - return NetworkImageNet(arch='DARTS_GS_4M') diff --git a/ppcls/modeling/architectures/layers.py b/ppcls/modeling/architectures/layers.py deleted file mode 100644 index f99103b0..00000000 --- a/ppcls/modeling/architectures/layers.py +++ /dev/null @@ -1,250 +0,0 @@ -#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. - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import math -import warnings - -import paddle.fluid as fluid - - -def initial_type(name, - input, - op_type, - fan_out, - init="google", - use_bias=False, - filter_size=0, - stddev=0.02): - if init == "kaiming": - if op_type == 'conv': - fan_in = input.shape[1] * filter_size * filter_size - elif op_type == 'deconv': - fan_in = fan_out * filter_size * filter_size - else: - if len(input.shape) > 2: - fan_in = input.shape[1] * input.shape[2] * input.shape[3] - else: - fan_in = input.shape[1] - bound = 1 / math.sqrt(fan_in) - param_attr = fluid.ParamAttr( - name=name + "_weights", - initializer=fluid.initializer.Uniform( - low=-bound, high=bound)) - if use_bias == True: - bias_attr = fluid.ParamAttr( - name=name + '_offset', - initializer=fluid.initializer.Uniform( - low=-bound, high=bound)) - else: - bias_attr = False - elif init == 'google': - n = filter_size * filter_size * fan_out - param_attr = fluid.ParamAttr( - name=name + "_weights", - initializer=fluid.initializer.NormalInitializer( - loc=0.0, scale=math.sqrt(2.0 / n))) - if use_bias == True: - bias_attr = fluid.ParamAttr( - name=name + "_offset", - initializer=fluid.initializer.Constant(0.0)) - else: - bias_attr = False - - else: - param_attr = fluid.ParamAttr( - name=name + "_weights", - initializer=fluid.initializer.NormalInitializer( - loc=0.0, scale=stddev)) - if use_bias == True: - bias_attr = fluid.ParamAttr( - name=name + "_offset", - initializer=fluid.initializer.Constant(0.0)) - else: - bias_attr = False - return param_attr, bias_attr - - -def cal_padding(img_size, stride, filter_size, dilation=1): - """Calculate padding size.""" - if img_size % stride == 0: - out_size = max(filter_size - stride, 0) - else: - out_size = max(filter_size - (img_size % stride), 0) - return out_size // 2, out_size - out_size // 2 - - -def init_batch_norm_layer(name="batch_norm"): - param_attr = fluid.ParamAttr( - name=name + '_scale', initializer=fluid.initializer.Constant(1.0)) - bias_attr = fluid.ParamAttr( - name=name + '_offset', - initializer=fluid.initializer.Constant(value=0.0)) - return param_attr, bias_attr - - -def init_fc_layer(fout, name='fc'): - n = fout # fan-out - init_range = 1.0 / math.sqrt(n) - - param_attr = fluid.ParamAttr( - name=name + '_weights', - initializer=fluid.initializer.UniformInitializer( - low=-init_range, high=init_range)) - bias_attr = fluid.ParamAttr( - name=name + '_offset', - initializer=fluid.initializer.Constant(value=0.0)) - return param_attr, bias_attr - - -def norm_layer(input, norm_type='batch_norm', name=None): - if norm_type == 'batch_norm': - param_attr = fluid.ParamAttr( - name=name + '_weights', - initializer=fluid.initializer.Constant(1.0)) - bias_attr = fluid.ParamAttr( - name=name + '_offset', - initializer=fluid.initializer.Constant(value=0.0)) - return fluid.layers.batch_norm( - input, - param_attr=param_attr, - bias_attr=bias_attr, - moving_mean_name=name + '_mean', - moving_variance_name=name + '_variance') - - elif norm_type == 'instance_norm': - helper = fluid.layer_helper.LayerHelper("instance_norm", **locals()) - dtype = helper.input_dtype() - epsilon = 1e-5 - mean = fluid.layers.reduce_mean(input, dim=[2, 3], keep_dim=True) - var = fluid.layers.reduce_mean( - fluid.layers.square(input - mean), dim=[2, 3], keep_dim=True) - if name is not None: - scale_name = name + "_scale" - offset_name = name + "_offset" - scale_param = fluid.ParamAttr( - name=scale_name, - initializer=fluid.initializer.Constant(1.0), - trainable=True) - offset_param = fluid.ParamAttr( - name=offset_name, - initializer=fluid.initializer.Constant(0.0), - trainable=True) - scale = helper.create_parameter( - attr=scale_param, shape=input.shape[1:2], dtype=dtype) - offset = helper.create_parameter( - attr=offset_param, shape=input.shape[1:2], dtype=dtype) - - tmp = fluid.layers.elementwise_mul(x=(input - mean), y=scale, axis=1) - tmp = tmp / fluid.layers.sqrt(var + epsilon) - tmp = fluid.layers.elementwise_add(tmp, offset, axis=1) - return tmp - else: - raise NotImplementedError("norm tyoe: [%s] is not support" % norm_type) - - -def conv2d(input, - num_filters=64, - filter_size=7, - stride=1, - stddev=0.02, - padding=0, - groups=None, - name="conv2d", - norm=None, - act=None, - relufactor=0.0, - use_bias=False, - padding_type=None, - initial="normal", - use_cudnn=True): - - if padding != 0 and padding_type != None: - warnings.warn( - 'padding value and padding type are set in the same time, and the final padding width and padding height are computed by padding_type' - ) - - param_attr, bias_attr = initial_type( - name=name, - input=input, - op_type='conv', - fan_out=num_filters, - init=initial, - use_bias=use_bias, - filter_size=filter_size, - stddev=stddev) - - def get_padding(filter_size, stride=1, dilation=1): - padding = ((stride - 1) + dilation * (filter_size - 1)) // 2 - return padding - - need_crop = False - if padding_type == "SAME": - top_padding, bottom_padding = cal_padding(input.shape[2], stride, - filter_size) - left_padding, right_padding = cal_padding(input.shape[2], stride, - filter_size) - height_padding = bottom_padding - width_padding = right_padding - if top_padding != bottom_padding or left_padding != right_padding: - height_padding = top_padding + stride - width_padding = left_padding + stride - need_crop = True - padding = [height_padding, width_padding] - elif padding_type == "VALID": - height_padding = 0 - width_padding = 0 - padding = [height_padding, width_padding] - elif padding_type == "DYNAMIC": - padding = get_padding(filter_size, stride) - else: - padding = padding - - conv = fluid.layers.conv2d( - input, - num_filters, - filter_size, - groups=groups, - name=name, - stride=stride, - padding=padding, - use_cudnn=use_cudnn, - param_attr=param_attr, - bias_attr=bias_attr) - - if need_crop: - conv = conv[:, :, 1:, 1:] - - if norm is not None: - conv = norm_layer(input=conv, norm_type=norm, name=name + "_norm") - if act == 'relu': - conv = fluid.layers.relu(conv, name=name + '_relu') - elif act == 'leaky_relu': - conv = fluid.layers.leaky_relu( - conv, alpha=relufactor, name=name + '_leaky_relu') - elif act == 'tanh': - conv = fluid.layers.tanh(conv, name=name + '_tanh') - elif act == 'sigmoid': - conv = fluid.layers.sigmoid(conv, name=name + '_sigmoid') - elif act == 'swish': - conv = fluid.layers.swish(conv, name=name + '_swish') - elif act == None: - conv = conv - else: - raise NotImplementedError("activation: [%s] is not support" % act) - - return conv -- GitLab