def __init__(self, se_filters, num_filters, is_gather_excite=False, use_bias=False, name=''): super(SqueezeExcite, self).__init__(name=name) self.se_filters = se_filters self.num_filters = num_filters self.squeeze = None self.excite = None self.is_gather_excite = is_gather_excite self.activation = get_activation('swish') self.pool = GlobalAvgPool2d(keepdim=True) self.use_bias = use_bias
def ResNet(block, layers, input_shape=(224, 224,3), num_classes=1000, use_bias=False, include_top=True, model_name='', **kwargs): """Instantiates the ResNet, ResNetV2, and ResNeXt architecture. Args block: a function that returns output tensor for the stacked residual blocks. layers: list of integer, the number of repeat units in each blocks. input_shape: optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(224, 224, 3)` It should have exactly 3 inputs channels. num_classes: optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. use_bias: whether to use biases for convolutional layers or not (True for ResNet and ResNetV2, False for ResNeXt). include_top: whether to include the fully-connected layer at the top of the network. model_name: string, model name. Returns A Keras model instance. Raises ValueError: in case of invalid argument for `weights`, or invalid input shape. """ def _make_layer(block, num_filters, blocklayers, strides=1, dilate=False,use_bias=use_bias,layer_name=''): layers = OrderedDict() layers['0']=block(num_filters=num_filters, strides=strides, expansion = 4, conv_shortcut=True,use_bias=use_bias, name=layer_name+'1') for k in range(1, blocklayers): layers['{0}'.format(k)]=block(num_filters=num_filters, strides=1, expansion = 4, conv_shortcut=False, use_bias=use_bias,name=layer_name+'{0}'.format(k+1)) laters_block=Sequential(layers) laters_block._name=layer_name return laters_block flow_list=[] resnet = Sequential() resnet.add_module('conv1',Conv2d_Block((7,7),64,strides=2,use_bias=use_bias,auto_pad=True,padding_mode='zero',normalization='batch',activation='relu',name='first_block')) resnet.add_module('maxpool',(MaxPool2d((3,3),strides=2,auto_pad=True,padding_mode='zero'))) resnet.add_module('layer1',(_make_layer(block, 64, layers[0],strides=1, dilate=None,use_bias=use_bias,layer_name='layer1' ))) resnet.add_module('layer2',(_make_layer(block, 128, layers[1], strides=2, dilate=None,use_bias=use_bias,layer_name='layer2' ))) resnet.add_module('layer3',(_make_layer(block, 256, layers[2], strides=2, dilate=None,use_bias=use_bias,layer_name='layer3' ))) resnet.add_module('layer4' ,(_make_layer(block, 512, layers[3], strides=2, dilate=None,use_bias=use_bias,layer_name='layer4' ))) resnet.add_module('avg_pool',GlobalAvgPool2d(name='avg_pool')) if include_top: resnet.add_module('fc',Dense(num_classes,activation=None,name='fc')) resnet.add_module('softmax', SoftMax(name='softmax')) resnet._name=model_name model=ImageClassificationModel(input_shape=input_shape,output=resnet) with open(os.path.join(os.path.dirname(os.path.abspath(__file__)) ,'imagenet_labels1.txt'), 'r', encoding='utf-8-sig') as f: labels = [l.rstrip() for l in f] model.class_names=labels input_np_shape=to_numpy(input_shape) model.preprocess_flow=[Resize((input_np_shape[0],input_np_shape[1]),keep_aspect=True), to_bgr(), Normalize([103.939, 116.779, 123.68], [1, 1, 1])] #model.summary() return model
def EfficientNet(width_coefficient, depth_coefficient, default_size, dropout_rate=0.2, drop_connect_rate=0.2, depth_divisor=8, model_name='efficientnet', include_top=True, num_classes=1000, **kwargs): """Instantiates the EfficientNet architecture using given scaling coefficients. Optionally loads weights pre-trained on ImageNet. Note that the data format convention used by the model is the one specified in your Keras config at `~/.keras/keras.json`. Args width_coefficient: float, scaling coefficient for network width. depth_coefficient: float, scaling coefficient for network depth. default_size: integer, default input image size. dropout_rate: float, dropout rate before final classifier layer. drop_connect_rate: float, dropout rate at skip connections. depth_divisor: integer, a unit of network width. activation_fn: activation function. model_name: string, model name. include_top: whether to include the fully-connected layer at the top of the network. input_shape: optional shape tuple, only to be specified if `include_top` is False. It should have exactly 3 inputs channels. num_classes: optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. Returns A Efficientnet model instance. """ default_block_args = deepcopy(DEFAULT_BLOCKS_ARGS) def round_filters(filters, divisor=depth_divisor): """Round number of filters based on depth multiplier.""" filters *= width_coefficient new_filters = builtins.max( divisor, int(filters + divisor / 2) // divisor * divisor) # Make sure that round down does not go down by more than 10%. if new_filters < 0.9 * filters: new_filters += divisor return int(new_filters) def round_repeats(repeats): """Round number of repeats based on depth multiplier.""" return int(math.ceil(depth_coefficient * repeats)) flow_list = [] efficientnet = Sequential(name=model_name) efficientnet.add_module( 'stem', Conv2d_Block((3, 3), round_filters(32), strides=2, use_bias=False, auto_pad=True, padding_mode='zero', normalization='batch', activation='swish', name='stem')) b = 0 blocks = float(builtins.sum(args['repeats'] for args in default_block_args)) for (i, args) in enumerate(default_block_args): assert args['repeats'] > 0 # Update block input and output filters based on depth multiplier. # args['filters_in'] = round_filters(args['filters_in']) # args['filters_out'] = round_filters(args['filters_out']) for j in range(round_repeats(args.pop('repeats'))): # The first block needs to take care of stride and filter size increase. if j > 0: args['strides'] = 1 args['filters_in'] = args['filters_out'] efficientnet.add_module( 'block{}{}'.format(i + 1, chr(j + 97)), efficient_block(expand_ratio=args['expand_ratio'], filters_in=round_filters(args['filters_in']), filters_out=round_filters(args['filters_out']), kernel_size=args['kernel_size'], strides=args['strides'], zero_pad=0, se_ratio=args['se_ratio'], drop_connect_rate=drop_connect_rate * b / blocks, name='block{}{}_'.format(i + 1, chr(j + 97)))), b += 1 efficientnet.add_module( 'top_conv', Conv2d_Block((1, 1), round_filters(1280), strides=1, use_bias=False, auto_pad=True, padding_mode='zero', normalization='batch', activation='swish', name='top_conv')) if include_top: efficientnet.add_module('avg_pool', GlobalAvgPool2d(name='avg_pool')) if dropout_rate > 0: efficientnet.add_module('top_dropout', Dropout(dropout_rate, name='top_dropout')) efficientnet.add_module('fc', Dense(num_classes, activation=None, name='fc')) efficientnet.add_module('softmax', SoftMax(axis=-1, name='softmax')) if isinstance(default_size, int): default_size = (default_size, default_size, 3) elif len(default_size) == 1: default_size = (default_size[0], default_size[0], 3) model = ImageClassificationModel(input_shape=default_size, output=efficientnet) with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'imagenet_labels1.txt'), 'r', encoding='utf-8-sig') as f: labels = [l.rstrip() for l in f] model.class_names = labels model.preprocess_flow = [ Resize((default_size[0], default_size[1]), keep_aspect=True), Normalize(0, 255), Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ] return model