def ResNet(stack_fn, preact, use_bias, model_name='resnet', include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000, **kwargs): """Instantiates the ResNet, ResNetV2, and ResNeXt architecture. 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`. Arguments: stack_fn: a function that returns output tensor for the stacked residual blocks. preact: whether to use pre-activation or not (True for ResNetV2, False for ResNet and ResNeXt). use_bias: whether to use biases for convolutional layers or not (True for ResNet and ResNetV2, False for ResNeXt). model_name: string, model name. include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization), 'imagenet' (pre-training on ImageNet), or the path to the weights file to be loaded. input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. input_shape: optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(224, 224, 3)` (with `channels_last` data format) or `(3, 224, 224)` (with `channels_first` data format). It should have exactly 3 inputs channels. pooling: optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional layer. - `avg` means that global average pooling will be applied to the output of the last convolutional layer, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. 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. **kwargs: For backwards compatibility only. Returns: A Keras model instance. Raises: ValueError: in case of invalid argument for `weights`, or invalid input shape. """ if 'layers' in kwargs: global layers layers = kwargs.pop('layers') if kwargs: raise ValueError('Unknown argument(s): %s' % (kwargs,)) if not (weights in {'imagenet', None} or os.path.exists(weights)): raise ValueError('The `weights` argument should be either ' '`None` (random initialization), `imagenet` ' '(pre-training on ImageNet), ' 'or the path to the weights file to be loaded.') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as `"imagenet"` with `include_top`' ' as true, `classes` should be 1000') # Determine proper input shape input_shape =obtain_input_shape( input_shape, default_size=224, min_size=32, data_format=backend.image_data_format(), require_flatten=include_top, weights=weights) if input_tensor is None: img_input = layers.Input(shape=input_shape) else: if not backend.is_keras_tensor(input_tensor): img_input = layers.Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor bn_axis = 3 if backend.image_data_format() == 'channels_last' else 1 x = layers.ZeroPadding2D( padding=((3, 3), (3, 3)), name='conv1_pad')(img_input) x = layers.Conv2D(64, 7, strides=2, use_bias=use_bias, name='conv1_conv')(x) if not preact: x = layers.BatchNormalization( axis=bn_axis, epsilon=1.001e-5, name='conv1_bn')(x) x = layers.Activation('relu', name='conv1_relu')(x) x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)), name='pool1_pad')(x) x = layers.MaxPooling2D(3, strides=2, name='pool1_pool')(x) x = stack_fn(x) if preact: x = layers.BatchNormalization( axis=bn_axis, epsilon=1.001e-5, name='post_bn')(x) x = layers.Activation('relu', name='post_relu')(x) if include_top: x = layers.GlobalAveragePooling2D(name='avg_pool')(x) x = layers.Dense(classes, activation='softmax', name='probs')(x) else: if pooling == 'avg': x = layers.GlobalAveragePooling2D(name='avg_pool')(x) elif pooling == 'max': x = layers.GlobalMaxPooling2D(name='max_pool')(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = layer_utils.get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = training.Model(inputs, x, name=model_name) # Load weights. if (weights == 'imagenet') and (model_name in WEIGHTS_HASHES): if include_top: file_name = model_name + '_weights_tf_dim_ordering_tf_kernels.h5' file_hash = WEIGHTS_HASHES[model_name][0] else: file_name = model_name + '_weights_tf_dim_ordering_tf_kernels_notop.h5' file_hash = WEIGHTS_HASHES[model_name][1] weights_path = data_utils.get_file( file_name, BASE_WEIGHTS_PATH + file_name, cache_subdir='models', file_hash=file_hash) model.load_weights(weights_path) elif weights is not None: model.load_weights(weights) return model
def resnet50(num_classes, dtype='float32', batch_size=None): """Instantiates the ResNet50 architecture. Args: num_classes: `int` number of classes for image classification. dtype: dtype to use float32 or float16 are most common. batch_size: Size of the batches for each step. Returns: A Keras model instance. """ input_shape = (32, 64, 3) img_input = layers.Input(shape=input_shape, dtype=dtype, batch_size=batch_size) if backend.image_data_format() == 'channels_first': x = layers.Lambda( lambda x: backend.permute_dimensions(x, (0, 3, 1, 2)), name='transpose')(img_input) bn_axis = 1 else: # channels_last x = img_input bn_axis = 3 # x = layers.ZeroPadding2D(padding=(1, 1), name='conv1_pad')(x) x = layers.Conv2D(16, (7, 7), strides=(2, 2), padding='same', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY), name='conv1')(x) x = layers.BatchNormalization(axis=bn_axis, momentum=BATCH_NORM_DECAY, epsilon=BATCH_NORM_EPSILON, name='bn_conv1')(x) x = layers.Activation('relu')(x) x = layers.MaxPooling2D((2, 4), strides=(2, 4), padding='same')(x) x = conv_block(x, 3, [16, 16, 64], stage=2, block='a', strides=(1, 1)) x = identity_block(x, 3, [16, 16, 64], stage=2, block='b') x = identity_block(x, 3, [16, 16, 64], stage=2, block='c') x = conv_block(x, 3, [32, 32, 128], stage=3, block='a') x = identity_block(x, 3, [32, 32, 128], stage=3, block='b') x = identity_block(x, 3, [32, 32, 128], stage=3, block='c') x = identity_block(x, 3, [32, 32, 128], stage=3, block='d') x = conv_block(x, 3, [64, 64, 256], stage=4, block='a') x = identity_block(x, 3, [64, 64, 256], stage=4, block='b') x = identity_block(x, 3, [64, 64, 256], stage=4, block='c') x = identity_block(x, 3, [64, 64, 256], stage=4, block='d') x = identity_block(x, 3, [64, 64, 256], stage=4, block='e') x = identity_block(x, 3, [64, 64, 256], stage=4, block='f') x = conv_block(x, 3, [128, 128, 512], stage=5, block='a') x = identity_block(x, 3, [128, 128, 512], stage=5, block='b') x = identity_block(x, 3, [128, 128, 512], stage=5, block='c') rm_axes = [1, 2 ] if backend.image_data_format() == 'channels_last' else [2, 3] x = layers.Lambda(lambda x: backend.mean(x, rm_axes), name='reduce_mean')(x) x = layers.Dense(num_classes, kernel_initializer=initializers.RandomNormal(stddev=0.01), kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY), bias_regularizer=regularizers.l2(L2_WEIGHT_DECAY), name='fc1000')(x) # TODO(reedwm): Remove manual casts once mixed precision can be enabled with a # single line of code. x = backend.cast(x, 'float32') x = layers.Activation('softmax')(x) # Create model. return models.Model(img_input, x, name='resnet50')
def __init__(self, num_classes=10, dtype="float32", batch_size=None): super(CustomModel, self).__init__(name="resnet50") if backend.image_data_format() == "channels_first": self._lambda = layers.Lambda( lambda x: backend.permute_dimensions(x, (0, 3, 1, 2)), name="transpose", ) bn_axis = 1 data_format = "channels_first" else: bn_axis = 3 data_format = "channels_last" self._padding = layers.ZeroPadding2D(padding=(3, 3), data_format=data_format, name="zero_pad") self._conv2d_1 = layers.Conv2D( 64, (7, 7), strides=(2, 2), padding="valid", use_bias=False, kernel_initializer="he_normal", kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY), name="conv1", ) self._bn_1 = layers.BatchNormalization( axis=bn_axis, momentum=BATCH_NORM_DECAY, epsilon=BATCH_NORM_EPSILON, name="bn_conv1", ) self._activation_1 = layers.Activation("relu") self._maxpooling2d = layers.MaxPooling2D((3, 3), strides=(2, 2), padding="same") self._conv_block_1 = ConvBlock(3, [64, 64, 256], stage=2, block="a", strides=(1, 1)) self._identity_block_1 = IdentityBlock(3, [64, 64, 256], stage=2, block="b") self._identity_block_2 = IdentityBlock(3, [64, 64, 256], stage=2, block="c") self._conv_block_2 = ConvBlock(3, [128, 128, 512], stage=3, block="a") self._identity_block_3 = IdentityBlock(3, [128, 128, 512], stage=3, block="b") self._identity_block_4 = IdentityBlock(3, [128, 128, 512], stage=3, block="c") self._identity_block_5 = IdentityBlock(3, [128, 128, 512], stage=3, block="d") self._conv_block_3 = ConvBlock(3, [256, 256, 1024], stage=4, block="a") self._identity_block_6 = IdentityBlock(3, [256, 256, 1024], stage=4, block="b") self._identity_block_7 = IdentityBlock(3, [256, 256, 1024], stage=4, block="c") self._identity_block_8 = IdentityBlock(3, [256, 256, 1024], stage=4, block="d") self._identity_block_9 = IdentityBlock(3, [256, 256, 1024], stage=4, block="e") self._identity_block_10 = IdentityBlock(3, [256, 256, 1024], stage=4, block="f") self._conv_block_4 = ConvBlock(3, [512, 512, 2048], stage=5, block="a") self._identity_block_11 = IdentityBlock(3, [512, 512, 2048], stage=5, block="b") self._identity_block_12 = IdentityBlock(3, [512, 512, 2048], stage=5, block="c") rm_axes = ([1, 2] if backend.image_data_format() == "channels_last" else [2, 3]) self._lamba_2 = layers.Lambda(lambda x: backend.mean(x, rm_axes), name="reduce_mean") self._dense = layers.Dense( num_classes, kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY), bias_regularizer=regularizers.l2(L2_WEIGHT_DECAY), name="fc1000", ) self._activation_2 = layers.Activation("softmax")
def __init__(self, out_channels, atrous_rate, norm_layer, norm_kwargs, conv_trainable=True): super(ASPPConv, self).__init__() self.conv = klayers.Conv2D(out_channels, kernel_size=3, padding='same', kernel_initializer='he_uniform', dilation_rate=atrous_rate, use_bias=False, trainable=conv_trainable) self.bn = norm_layer(**({} if norm_kwargs is None else norm_kwargs)) self.relu = klayers.ReLU()
def conv_building_block(input_tensor, kernel_size, filters, stage, block, strides=(2, 2), training=None): """A block that has a conv layer at shortcut. Arguments: input_tensor: input tensor kernel_size: default 3, the kernel size of middle conv layer at main path filters: list of integers, the filters of 3 conv layer at main path stage: integer, current stage label, used for generating layer names block: current block label, used for generating layer names strides: Strides for the first conv layer in the block. training: Only used if training keras model with Estimator. In other scenarios it is handled automatically. Returns: Output tensor for the block. Note that from stage 3, the first conv layer at main path is with strides=(2, 2) And the shortcut should have strides=(2, 2) as well """ filters1, filters2 = filters if tf.keras.backend.image_data_format() == 'channels_last': bn_axis = 3 else: bn_axis = 1 conv_name_base = 'res' + str(stage) + block + '_branch' bn_name_base = 'bn' + str(stage) + block + '_branch' x = layers.Conv2D(filters1, kernel_size, strides=strides, padding='same', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY), name=conv_name_base + '2a')(input_tensor) x = layers.BatchNormalization(axis=bn_axis, momentum=BATCH_NORM_DECAY, epsilon=BATCH_NORM_EPSILON, name=bn_name_base + '2a')(x, training=training) x = layers.Activation('relu')(x) x = layers.Conv2D(filters2, kernel_size, padding='same', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY), name=conv_name_base + '2b')(x) x = layers.BatchNormalization(axis=bn_axis, momentum=BATCH_NORM_DECAY, epsilon=BATCH_NORM_EPSILON, name=bn_name_base + '2b')(x, training=training) shortcut = layers.Conv2D( filters2, (1, 1), strides=strides, use_bias=False, kernel_initializer='he_normal', kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY), name=conv_name_base + '1')(input_tensor) shortcut = layers.BatchNormalization(axis=bn_axis, momentum=BATCH_NORM_DECAY, epsilon=BATCH_NORM_EPSILON, name=bn_name_base + '1')( shortcut, training=training) x = layers.add([x, shortcut]) x = layers.Activation('relu')(x) return x
def _adjust_block(p, ip, filters, block_id=None): """Adjusts the input `previous path` to match the shape of the `input`. Used in situations where the output number of filters needs to be changed. Arguments: p: Input tensor which needs to be modified ip: Input tensor whose shape needs to be matched filters: Number of output filters to be matched block_id: String block_id Returns: Adjusted Keras tensor """ channel_dim = 1 if backend.image_data_format() == 'channels_first' else -1 img_dim = 2 if backend.image_data_format() == 'channels_first' else -2 ip_shape = backend.int_shape(ip) if p is not None: p_shape = backend.int_shape(p) with backend.name_scope('adjust_block'): if p is None: p = ip elif p_shape[img_dim] != ip_shape[img_dim]: with backend.name_scope('adjust_reduction_block_%s' % block_id): p = layers.Activation('relu', name='adjust_relu_1_%s' % block_id)(p) p1 = layers.AveragePooling2D((1, 1), strides=(2, 2), padding='valid', name='adjust_avg_pool_1_%s' % block_id)( p) p1 = layers.Conv2D( filters // 2, (1, 1), padding='same', use_bias=False, name='adjust_conv_1_%s' % block_id, kernel_initializer='he_normal')( p1) p2 = layers.ZeroPadding2D(padding=((0, 1), (0, 1)))(p) p2 = layers.Cropping2D(cropping=((1, 0), (1, 0)))(p2) p2 = layers.AveragePooling2D((1, 1), strides=(2, 2), padding='valid', name='adjust_avg_pool_2_%s' % block_id)( p2) p2 = layers.Conv2D( filters // 2, (1, 1), padding='same', use_bias=False, name='adjust_conv_2_%s' % block_id, kernel_initializer='he_normal')( p2) p = layers.concatenate([p1, p2], axis=channel_dim) p = layers.BatchNormalization( axis=channel_dim, momentum=0.9997, epsilon=1e-3, name='adjust_bn_%s' % block_id)( p) elif p_shape[channel_dim] != filters: with backend.name_scope('adjust_projection_block_%s' % block_id): p = layers.Activation('relu')(p) p = layers.Conv2D( filters, (1, 1), strides=(1, 1), padding='same', name='adjust_conv_projection_%s' % block_id, use_bias=False, kernel_initializer='he_normal')( p) p = layers.BatchNormalization( axis=channel_dim, momentum=0.9997, epsilon=1e-3, name='adjust_bn_%s' % block_id)( p) return p
def NASNet( input_shape=None, penultimate_filters=4032, num_blocks=6, stem_block_filters=96, skip_reduction=True, filter_multiplier=2, include_top=True, weights=None, input_tensor=None, pooling=None, classes=1000, default_size=None, classifier_activation='softmax', ): """Instantiates a NASNet model. 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`. Arguments: input_shape: Optional shape tuple, the input shape is by default `(331, 331, 3)` for NASNetLarge and `(224, 224, 3)` for NASNetMobile. It should have exactly 3 input channels, and width and height should be no smaller than 32. E.g. `(224, 224, 3)` would be one valid value. penultimate_filters: Number of filters in the penultimate layer. NASNet models use the notation `NASNet (N @ P)`, where: - N is the number of blocks - P is the number of penultimate filters num_blocks: Number of repeated blocks of the NASNet model. NASNet models use the notation `NASNet (N @ P)`, where: - N is the number of blocks - P is the number of penultimate filters stem_block_filters: Number of filters in the initial stem block skip_reduction: Whether to skip the reduction step at the tail end of the network. filter_multiplier: Controls the width of the network. - If `filter_multiplier` < 1.0, proportionally decreases the number of filters in each layer. - If `filter_multiplier` > 1.0, proportionally increases the number of filters in each layer. - If `filter_multiplier` = 1, default number of filters from the paper are used at each layer. include_top: Whether to include the fully-connected layer at the top of the network. weights: `None` (random initialization) or `imagenet` (ImageNet weights) input_tensor: Optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. pooling: Optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional block. - `avg` means that global average pooling will be applied to the output of the last convolutional block, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. 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. default_size: Specifies the default image size of the model classifier_activation: A `str` or callable. The activation function to use on the "top" layer. Ignored unless `include_top=True`. Set `classifier_activation=None` to return the logits of the "top" layer. Returns: A `keras.Model` instance. Raises: ValueError: In case of invalid argument for `weights`, invalid input shape or invalid `penultimate_filters` value. ValueError: if `classifier_activation` is not `softmax` or `None` when using a pretrained top layer. """ if not (weights in {'imagenet', None} or os.path.exists(weights)): raise ValueError('The `weights` argument should be either ' '`None` (random initialization), `imagenet` ' '(pre-training on ImageNet), ' 'or the path to the weights file to be loaded.') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as `"imagenet"` with `include_top` ' 'as true, `classes` should be 1000') if (isinstance(input_shape, tuple) and None in input_shape and weights == 'imagenet'): raise ValueError('When specifying the input shape of a NASNet' ' and loading `ImageNet` weights, ' 'the input_shape argument must be static ' '(no None entries). Got: `input_shape=' + str(input_shape) + '`.') if default_size is None: default_size = 331 # Determine proper input shape and default size. input_shape = imagenet_utils.obtain_input_shape( input_shape, default_size=default_size, min_size=32, data_format=backend.image_data_format(), require_flatten=True, weights=weights) if backend.image_data_format() != 'channels_last': logging.warning('The NASNet family of models is only available ' 'for the input data format "channels_last" ' '(width, height, channels). ' 'However your settings specify the default ' 'data format "channels_first" (channels, width, height).' ' You should set `image_data_format="channels_last"` ' 'in your Keras config located at ~/.keras/keras.json. ' 'The model being returned right now will expect inputs ' 'to follow the "channels_last" data format.') backend.set_image_data_format('channels_last') old_data_format = 'channels_first' else: old_data_format = None if input_tensor is None: img_input = layers.Input(shape=input_shape) else: if not backend.is_keras_tensor(input_tensor): img_input = layers.Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor if penultimate_filters % (24 * (filter_multiplier**2)) != 0: raise ValueError( 'For NASNet-A models, the `penultimate_filters` must be a multiple ' 'of 24 * (`filter_multiplier` ** 2). Current value: %d' % penultimate_filters) channel_dim = 1 if backend.image_data_format() == 'channels_first' else -1 filters = penultimate_filters // 24 x = layers.Conv2D( stem_block_filters, (3, 3), strides=(2, 2), padding='valid', use_bias=False, name='stem_conv1', kernel_initializer='he_normal')( img_input) x = layers.BatchNormalization( axis=channel_dim, momentum=0.9997, epsilon=1e-3, name='stem_bn1')( x) p = None x, p = _reduction_a_cell( x, p, filters // (filter_multiplier**2), block_id='stem_1') x, p = _reduction_a_cell( x, p, filters // filter_multiplier, block_id='stem_2') for i in range(num_blocks): x, p = _normal_a_cell(x, p, filters, block_id='%d' % (i)) x, p0 = _reduction_a_cell( x, p, filters * filter_multiplier, block_id='reduce_%d' % (num_blocks)) p = p0 if not skip_reduction else p for i in range(num_blocks): x, p = _normal_a_cell( x, p, filters * filter_multiplier, block_id='%d' % (num_blocks + i + 1)) x, p0 = _reduction_a_cell( x, p, filters * filter_multiplier**2, block_id='reduce_%d' % (2 * num_blocks)) p = p0 if not skip_reduction else p for i in range(num_blocks): x, p = _normal_a_cell( x, p, filters * filter_multiplier**2, block_id='%d' % (2 * num_blocks + i + 1)) x = layers.Activation('relu')(x) if include_top: x = layers.GlobalAveragePooling2D()(x) imagenet_utils.validate_activation(classifier_activation, weights) x = layers.Dense(classes, activation=classifier_activation, name='predictions')(x) else: if pooling == 'avg': x = layers.GlobalAveragePooling2D()(x) elif pooling == 'max': x = layers.GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = layer_utils.get_source_inputs(input_tensor) else: inputs = img_input model = training.Model(inputs, x, name='NASNet') # Load weights. if weights == 'imagenet': if default_size == 224: # mobile version if include_top: weights_path = data_utils.get_file( 'nasnet_mobile.h5', NASNET_MOBILE_WEIGHT_PATH, cache_subdir='models', file_hash='020fb642bf7360b370c678b08e0adf61') else: weights_path = data_utils.get_file( 'nasnet_mobile_no_top.h5', NASNET_MOBILE_WEIGHT_PATH_NO_TOP, cache_subdir='models', file_hash='1ed92395b5b598bdda52abe5c0dbfd63') model.load_weights(weights_path) elif default_size == 331: # large version if include_top: weights_path = data_utils.get_file( 'nasnet_large.h5', NASNET_LARGE_WEIGHT_PATH, cache_subdir='models', file_hash='11577c9a518f0070763c2b964a382f17') else: weights_path = data_utils.get_file( 'nasnet_large_no_top.h5', NASNET_LARGE_WEIGHT_PATH_NO_TOP, cache_subdir='models', file_hash='d81d89dc07e6e56530c4e77faddd61b5') model.load_weights(weights_path) else: raise ValueError('ImageNet weights can only be loaded with NASNetLarge' ' or NASNetMobile') elif weights is not None: model.load_weights(weights) if old_data_format: backend.set_image_data_format(old_data_format) return model
def MobileNet(input_shape=None, alpha=1.0, depth_multiplier=1, dropout=1e-3): if input_shape is None: default_size = 224 else: if backend.image_data_format() == 'channels_first': rows = input_shape[1] cols = input_shape[2] else: rows = input_shape[0] cols = input_shape[1] if rows == cols and rows in [128, 160, 192, 224]: default_size = rows else: default_size = 224 input_shape = imagenet_utils.obtain_input_shape( input_shape, default_size=default_size, min_size=32, data_format=backend.image_data_format(), require_flatten=True) row_axis = 0 if backend.image_data_format() == 'channels_last' else 0 rows = input_shape[row_axis] img_input = layers.Input(shape=input_shape) x = _conv_block(img_input, 32, alpha, strides=(2, 2)) x = _depthwise_conv_block(x, 64, alpha, depth_multiplier, block_id=1) x = _depthwise_conv_block(x, 128, alpha, depth_multiplier, strides=(2, 2), block_id=2) x = _depthwise_conv_block(x, 128, alpha, depth_multiplier, block_id=3) x = _depthwise_conv_block(x, 256, alpha, depth_multiplier, strides=(2, 2), block_id=4) x = _depthwise_conv_block(x, 256, alpha, depth_multiplier, block_id=5) x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, strides=(2, 2), block_id=6) x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=7) x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=8) x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=9) x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=10) x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=11) x = _depthwise_conv_block(x, 1024, alpha, depth_multiplier, strides=(2, 2), block_id=12) x = _depthwise_conv_block(x, 1024, alpha, depth_multiplier, block_id=13) if backend.image_data_format() == 'channels_first': shape = (int(1024 * alpha), 1, 1) else: shape = (1, 1, int(1024 * alpha)) x = layers.GlobalAveragePooling2D()(x) x = layers.Reshape(shape, name='reshape_1')(x) x = layers.Dropout(dropout, name='dropout')(x) x = layers.Conv2D(NUM_CLASSES, (1, 1), padding='same', name='conv_preds')(x) x = layers.Reshape((NUM_CLASSES, ), name='reshape_2')(x) imagenet_utils.validate_activation('softmax', None) x = layers.Activation(activation='softmax', name='predictions')(x) # Create model. model = training.Model(img_input, x, name='mobilenet_%0.2f_%s' % (alpha, rows)) return model
def bottleneck_block(input_tensor, filters, kernel_size=3, stride=1, conv_shortcut=False, name=None): """A residual block. Arguments: input_tensor: input tensor. filters: integer, filters of the bottleneck layer. kernel_size: default 3, kernel size of the bottleneck layer. stride: default 1, stride of the first layer. conv_shortcut: default False, use convolution shortcut if True, otherwise identity shortcut. name: string, block label. Returns: Output tensor for the residual block. """ if backend.image_data_format() == 'channels_last': bn_axis = -1 else: bn_axis = 1 preact = layers.BatchNormalization(axis=bn_axis, name=name + '_preact_bn')(input_tensor) preact = layers.Activation(activation='relu', name=name + '_preact_relu')(preact) if conv_shortcut: shortcut = layers.Conv2D(filters=4 * filters, kernel_size=1, strides=stride, name=name + '_0_conv')(preact) else: shortcut = input_tensor x = layers.Conv2D(filters=filters, kernel_size=1, strides=1, use_bias=False, name=name + '_1_conv')(preact) x = layers.BatchNormalization(axis=bn_axis, name=name + '_1_bn')(x) x = layers.Activation(activation='relu', name=name + '_1_relu')(x) x = layers.ZeroPadding2D(padding=1, name=name + '_2_pad')(x) x = layers.Conv2D(filters=filters, kernel_size=kernel_size, strides=stride, use_bias=False, name=name + '_2_conv')(x) x = layers.BatchNormalization(axis=bn_axis, name=name + '_2_bn')(x) x = layers.Activation(activation='relu', name=name + '_2_relu')(x) x = layers.Conv2D(filters=4 * filters, kernel_size=1, strides=1, name=name + '_3_conv')(x) x = layers.Add(name=name + '_out')([shortcut, x]) return x
def conv_block_test_0(input_tensor, num_filters): encoder = layers.Conv2D(num_filters, (3, 3), padding='same')(input_tensor) encoder = layers.Activation('relu')(encoder) encoder = layers.Conv2D(num_filters, (3, 3), padding='same')(encoder) encoder = layers.Activation('relu')(encoder) return encoder
def testSupports_KerasLayer(self): self.assertTrue(self.quantize_registry.supports(l.Dense(10))) self.assertTrue(self.quantize_registry.supports(l.Conv2D(10, (2, 2))))
def __init__(self, num_classes=10): super(GoogLeNet, self).__init__() self.conv1 = Sequential([ layers.Conv2D(filters=64, kernel_size=(7, 7), strides=2, padding='same'), layers.MaxPool2D(pool_size=(3, 3), strides=2, padding='same'), layers.Activation('relu') ]) self.conv2 = Sequential([ layers.Conv2D(filters=192, kernel_size=(3, 3), strides=1, padding='same'), layers.MaxPool2D(pool_size=(3, 3), strides=2, padding='same'), layers.Activation('relu') ]) self.inc3a = Inception_Block(out_channels_1_1=64, reduce_channels_3_3=96, out_channels_3_3=128, reduce_channels_5_5=16, out_channels_5_5=32, pool_proj=32, check=256) self.inc3b = Inception_Block(out_channels_1_1=128, reduce_channels_3_3=128, out_channels_3_3=192, reduce_channels_5_5=32, out_channels_5_5=96, pool_proj=64, check=480) self.inc4a = Inception_Block(out_channels_1_1=192, reduce_channels_3_3=96, out_channels_3_3=208, reduce_channels_5_5=16, out_channels_5_5=48, pool_proj=64, check=512) self.inc4b = Inception_Block(out_channels_1_1=160, reduce_channels_3_3=112, out_channels_3_3=224, reduce_channels_5_5=24, out_channels_5_5=64, pool_proj=64, check=512) self.inc4c = Inception_Block(out_channels_1_1=128, reduce_channels_3_3=128, out_channels_3_3=256, reduce_channels_5_5=24, out_channels_5_5=64, pool_proj=64, check=512) self.inc4d = Inception_Block(out_channels_1_1=112, reduce_channels_3_3=144, out_channels_3_3=288, reduce_channels_5_5=32, out_channels_5_5=64, pool_proj=64, check=528) self.inc4e = Inception_Block(out_channels_1_1=256, reduce_channels_3_3=160, out_channels_3_3=320, reduce_channels_5_5=32, out_channels_5_5=128, pool_proj=128, check=832) self.inc5a = Inception_Block(out_channels_1_1=256, reduce_channels_3_3=160, out_channels_3_3=320, reduce_channels_5_5=32, out_channels_5_5=128, pool_proj=128, check=832) self.inc5b = Inception_Block(out_channels_1_1=384, reduce_channels_3_3=192, out_channels_3_3=384, reduce_channels_5_5=48, out_channels_5_5=128, pool_proj=128, check=1024) self.last_seq = Sequential([ layers.GlobalAveragePooling2D(), layers.Dropout(0.4), layers.Dense(num_classes), # layers.Dense(num_classes, activation=tf.nn.softmax) ])
def create_network(Zt, Ct, filter_size=5, channels=1, strides=[2, 2, 2, 2, 2], upscaling_filters=[512, 256, 128, 64, 1], encoder_filters=[64, 128, 256], decoder_filters=[256, 128, 64], resblock_filters=[256, 256, 256, 256, 256], resblock_ks=[3, 3, 3, 3, 3, 3] ): with tf.name_scope("Gen"): # Generator Z = kl.Input((None, None, channels,), tensor=Zt, name="Z") C = kl.Input((None, None, channels,), tensor=Ct, name="C") layer = Z # Upscaling for l in range(len(upscaling_filters) - 1): layer = kl.Conv2DTranspose( filters=upscaling_filters[l], kernel_size=filter_size, padding="same", strides=strides[l], kernel_regularizer=kr.l2(), activation="relu")(layer) layer = kl.BatchNormalization()(layer) layer = kl.Conv2DTranspose( filters=upscaling_filters[-1], kernel_size=filter_size, padding="same", strides=strides[-1], activation="relu", kernel_regularizer=kr.l2())(layer) layer = kl.concatenate([layer, C]) # Encoder skips = [] for l in range(len(encoder_filters)): layer = kl.Conv2D( filters=encoder_filters[l], kernel_size=filter_size, padding="same", activation="relu", kernel_regularizer=kr.l2())(layer) layer = kl.AveragePooling2D()(layer) layer = kl.BatchNormalization()(layer) skips.append(layer) # Residual blocks for l in range(len(resblock_filters)): layer = ResidualBlock(resblock_filters[l], nb_layers=3, kernel_size=resblock_ks[l])(layer) # Decoder skips = skips[::-1] for l in range(len(decoder_filters)): layer = kl.concatenate([layer, skips[l]]) layer = kl.Conv2DTranspose( filters=decoder_filters[l], kernel_size=filter_size, padding="same", strides=strides[l], kernel_regularizer=kr.l2(), activation="relu")(layer) layer = kl.BatchNormalization()(layer) G_out = kl.Conv2D( filters=1, kernel_size=filter_size, activation="tanh", padding="same", kernel_regularizer=kr.l2())(layer) model = k.Model(inputs=[Z, C], outputs=G_out, name="G") return model
def init_model(learning_rate, dense_layers, dense_nodes, activation): """ Hyper-parameters: learning_rate: Learning-rate for the optimizer. dense_layers: Number of dense layers. dense_nodes: Number of nodes in each dense layer. activation: Activation function for all layers. """ # Start construction of a Keras Sequential model. model = models.Sequential() # Add an input layer which is similar to a feed_dict in TensorFlow. # Note that the input-shape must be a tuple containing the image-size. model.add(layers.InputLayer(input_shape=(IMG_SIZE_flat,))) # The input from MNIST is a flattened array with 784 elements, # but the convolutional layers expect images with shape (28, 28, 1) model.add(layers.Reshape(IMG_SHAPE_FULL)) # First convolutional layer. # There are many hyper-parameters in this layer, but we only # want to optimize the activation-function in this example. model.add(layers.Conv2D( kernel_size=5, strides=1, filters=16, padding='same', activation=activation, name='layer_conv1')) model.add(layers.MaxPooling2D(pool_size=2, strides=2)) # Second convolutional layer. # Again, we only want to optimize the activation-function here. model.add(layers.Conv2D( kernel_size=5, strides=1, filters=36, padding='same', activation=activation, name='layer_conv2')) model.add(layers.MaxPooling2D(pool_size=2, strides=2)) # Flatten the 4-rank output of the convolutional layers # to 2-rank that can be input to a fully-connected / dense layer. model.add(layers.Flatten()) # Add fully-connected / dense layers. # The number of layers is a hyper-parameter we want to optimize. for i in range(dense_layers): # Name of the layer. This is not really necessary # because Keras should give them unique names. name = 'layer_dense_{0}'.format(i+1) # Add the dense / fully-connected layer to the model. # This has two hyper-parameters we want to optimize: # The number of nodes and the activation function. model.add(layers.Dense( dense_nodes, activation=activation, name=name)) # Last fully-connected / dense layer with softmax-activation # for use in classification. model.add(layers.Dense(NUM_CLASSES, activation='softmax')) # Use the Adam method for training the network. # We want to find the best learning-rate for the Adam method. optimizer = optimizers.Adam(lr=learning_rate) # In Keras we need to compile the model so it can be trained. model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy']) return model
from tensorflow.python.keras import models, layers from tensorflow.python.keras.utils import to_categorical (train_images, train_labels), (test_images, test_labels) = mnist.load_data() train_images = train_images.reshape((60000, 28, 28, 1)) train_images = train_images.astype('float32') / 255 test_images = test_images.reshape((10000, 28, 28, 1)) test_images = test_images.astype('float32') / 255 train_labels = to_categorical(train_labels) test_labels = to_categorical(test_labels) model = models.Sequential() model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1))) model.add(layers.MaxPool2D(2, 2)) model.add(layers.Conv2D(64, (3, 3), activation='relu')) model.add(layers.MaxPool2D(2, 2)) model.add(layers.Conv2D(64, (3, 3), activation='relu')) model.add(layers.Flatten()) model.add(layers.Dense(64, activation='relu')) model.add(layers.Dense(10, activation='softmax')) # print(model.summary()) model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) model.fit(train_images, train_labels, epochs=5, batch_size=64)
def conv_block(input_tensor, num_filters): encoder = layers.Conv2D(num_filters, (3, 3), padding='same')(input_tensor) encoder = layers.BatchNormalization()(encoder) encoder = layers.LeakyReLU(alpha=0.3)(encoder) return encoder
def DenseNet(blocks, include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000, **kwargs): """Instantiates the DenseNet architecture. 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`. # Arguments blocks: numbers of building blocks for the four dense layers. include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization), 'imagenet' (pre-training on ImageNet), or the path to the weights file to be loaded. input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. input_shape: optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(224, 224, 3)` (with `'channels_last'` data format) or `(3, 224, 224)` (with `'channels_first'` data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 32. E.g. `(200, 200, 3)` would be one valid value. pooling: optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional block. - `avg` means that global average pooling will be applied to the output of the last convolutional block, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. 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 Keras model instance. # Raises ValueError: in case of invalid argument for `weights`, or invalid input shape. """ if not (weights in {'imagenet', None} or os.path.exists(weights)): raise ValueError('The `weights` argument should be either ' '`None` (random initialization), `imagenet` ' '(pre-training on ImageNet), ' 'or the path to the weights file to be loaded.') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError( 'If using `weights` as `"imagenet"` with `include_top`' ' as true, `classes` should be 1000') # Determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=32, data_format=backend.image_data_format(), require_flatten=include_top, weights=weights) if input_tensor is None: img_input = layers.Input(shape=input_shape) else: if not backend.is_keras_tensor(input_tensor): img_input = layers.Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor bn_axis = 3 if backend.image_data_format() == 'channels_last' else 1 x = layers.ZeroPadding2D(padding=((3, 3), (3, 3)))(img_input) x = layers.Conv2D(64, 7, strides=2, use_bias=False, name='conv1/conv')(x) x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='conv1/bn')(x) x = layers.Activation('relu', name='conv1/relu')(x) x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)))(x) x = layers.MaxPooling2D(3, strides=2, name='pool1')(x) x = dense_block(x, blocks[0], name='conv2') x = transition_block(x, 0.5, name='pool2') x = dense_block(x, blocks[1], name='conv3') x = transition_block(x, 0.5, name='pool3') x = dense_block(x, blocks[2], name='conv4') x = transition_block(x, 0.5, name='pool4') x = dense_block(x, blocks[3], name='conv5') x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='bn')(x) x = layers.Activation('relu', name='relu')(x) if include_top: x = layers.GlobalAveragePooling2D(name='avg_pool')(x) x = layers.Dense(classes, activation='softmax', name='fc1000')(x) else: if pooling == 'avg': x = layers.GlobalAveragePooling2D(name='avg_pool')(x) elif pooling == 'max': x = layers.GlobalMaxPooling2D(name='max_pool')(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = keras_utils.get_source_inputs(input_tensor) else: inputs = img_input # Create model. if blocks == [6, 12, 24, 16]: model = models.Model(inputs, x, name='densenet121') else: model = models.Model(inputs, x, name='densenet') # Load weights. if weights == 'imagenet': if include_top or (blocks != [6, 12, 24, 16]): raise ValueError('Only DenseNet121 without top is available' ' pretrained') # NOTE: If your Hub Application has multiple configurations (e.g., # DenseNet {121, 169, 201} x {include_top=True, include_top=False}) # each of these would have their own weights_path/SavedModel. weights_path = 'densenet121_weights_tf_dim_ordering_tf_kernels_notop' # Download the SavedModel and use only the checkpoint (variables). path = resolve(('https://github.com/jharmsen/hub-application/' 'releases/download/v1/{}.tar.gz').format(weights_path)) path = os.path.join(path, 'variables/variables') model.load_weights(path) elif weights is not None: model.load_weights(weights) return model
def RetinaNet(input_shape, num_classes, num_anchor=9): inputs = tf.keras.Input(shape=input_shape) # FPN resnet50 = tf.keras.applications.ResNet50(weights="imagenet", include_top=False, input_tensor=inputs, pooling=None) assert resnet50.layers[80].name == "conv3_block4_out" C3 = resnet50.layers[80].output assert resnet50.layers[142].name == "conv4_block6_out" C4 = resnet50.layers[142].output assert resnet50.layers[-1].name == "conv5_block3_out" C5 = resnet50.layers[-1].output P5 = layers.Conv2D(256, kernel_size=1, strides=1, padding='same', kernel_regularizer=regularizers.l2(0.0001))(C5) P5_upsampling = layers.UpSampling2D()(P5) P4 = layers.Conv2D(256, kernel_size=1, strides=1, padding='same', kernel_regularizer=regularizers.l2(0.0001))(C4) P4 = layers.Add()([P5_upsampling, P4]) P4_upsampling = layers.UpSampling2D()(P4) P3 = layers.Conv2D(256, kernel_size=1, strides=1, padding='same', kernel_regularizer=regularizers.l2(0.0001))(C3) P3 = layers.Add()([P4_upsampling, P3]) P6 = layers.Conv2D(256, kernel_size=3, strides=2, padding='same', name="P6", kernel_regularizer=regularizers.l2(0.0001))(C5) P7 = layers.Activation('relu')(P6) P7 = layers.Conv2D(256, kernel_size=3, strides=2, padding='same', name="P7", kernel_regularizer=regularizers.l2(0.0001))(P7) P5 = layers.Conv2D(256, kernel_size=3, strides=1, padding='same', name="P5", kernel_regularizer=regularizers.l2(0.0001))(P5) P4 = layers.Conv2D(256, kernel_size=3, strides=1, padding='same', name="P4", kernel_regularizer=regularizers.l2(0.0001))(P4) P3 = layers.Conv2D(256, kernel_size=3, strides=1, padding='same', name="P3", kernel_regularizer=regularizers.l2(0.0001))(P3) # classification subnet cls_subnet = _classification_sub_net(num_classes=num_classes, num_anchor=num_anchor) P3_cls = cls_subnet(P3) P4_cls = cls_subnet(P4) P5_cls = cls_subnet(P5) P6_cls = cls_subnet(P6) P7_cls = cls_subnet(P7) cls_output = layers.Concatenate(axis=-2)( [P3_cls, P4_cls, P5_cls, P6_cls, P7_cls]) # localization subnet loc_subnet = _regression_sub_net(num_anchor=num_anchor) P3_loc = loc_subnet(P3) P4_loc = loc_subnet(P4) P5_loc = loc_subnet(P5) P6_loc = loc_subnet(P6) P7_loc = loc_subnet(P7) loc_output = layers.Concatenate(axis=-2)( [P3_loc, P4_loc, P5_loc, P6_loc, P7_loc]) return tf.keras.Model(inputs=inputs, outputs=[cls_output, loc_output])
def _normal_a_cell(ip, p, filters, block_id=None): """Adds a Normal cell for NASNet-A (Fig. 4 in the paper). Arguments: ip: Input tensor `x` p: Input tensor `p` filters: Number of output filters block_id: String block_id Returns: A Keras tensor """ channel_dim = 1 if backend.image_data_format() == 'channels_first' else -1 with backend.name_scope('normal_A_block_%s' % block_id): p = _adjust_block(p, ip, filters, block_id) h = layers.Activation('relu')(ip) h = layers.Conv2D( filters, (1, 1), strides=(1, 1), padding='same', name='normal_conv_1_%s' % block_id, use_bias=False, kernel_initializer='he_normal')( h) h = layers.BatchNormalization( axis=channel_dim, momentum=0.9997, epsilon=1e-3, name='normal_bn_1_%s' % block_id)( h) with backend.name_scope('block_1'): x1_1 = _separable_conv_block( h, filters, kernel_size=(5, 5), block_id='normal_left1_%s' % block_id) x1_2 = _separable_conv_block( p, filters, block_id='normal_right1_%s' % block_id) x1 = layers.add([x1_1, x1_2], name='normal_add_1_%s' % block_id) with backend.name_scope('block_2'): x2_1 = _separable_conv_block( p, filters, (5, 5), block_id='normal_left2_%s' % block_id) x2_2 = _separable_conv_block( p, filters, (3, 3), block_id='normal_right2_%s' % block_id) x2 = layers.add([x2_1, x2_2], name='normal_add_2_%s' % block_id) with backend.name_scope('block_3'): x3 = layers.AveragePooling2D((3, 3), strides=(1, 1), padding='same', name='normal_left3_%s' % (block_id))( h) x3 = layers.add([x3, p], name='normal_add_3_%s' % block_id) with backend.name_scope('block_4'): x4_1 = layers.AveragePooling2D((3, 3), strides=(1, 1), padding='same', name='normal_left4_%s' % (block_id))( p) x4_2 = layers.AveragePooling2D((3, 3), strides=(1, 1), padding='same', name='normal_right4_%s' % (block_id))( p) x4 = layers.add([x4_1, x4_2], name='normal_add_4_%s' % block_id) with backend.name_scope('block_5'): x5 = _separable_conv_block( h, filters, block_id='normal_left5_%s' % block_id) x5 = layers.add([x5, h], name='normal_add_5_%s' % block_id) x = layers.concatenate([p, x1, x2, x3, x4, x5], axis=channel_dim, name='normal_concat_%s' % block_id) return x, ip
def structureModel(self): Inputs = layers.Input(shape=self._inputShape, batch_size=self._iBatchSize) Con1 = layers.Conv2D(64, (3, 3), name='Con1', activation='relu', padding='SAME', input_shape=self._inputShape, strides=1)(Inputs) Con2 = layers.Conv2D(64, (3, 3), name='Con2', activation='relu', padding='SAME', strides=1)(Con1) Side1 = sideBranch(Con2, 1) MaxPooling1 = layers.MaxPooling2D((2, 2), name='MaxPooling1', strides=2, padding='SAME')(Con2) # outputs1 Con3 = layers.Conv2D(128, (3, 3), name='Con3', activation='relu', padding='SAME', strides=1)(MaxPooling1) Con4 = layers.Conv2D(128, (3, 3), name='Con4', activation='relu', padding='SAME', strides=1)(Con3) Side2 = sideBranch(Con4, 2) MaxPooling2 = layers.MaxPooling2D((2, 2), name='MaxPooling2', strides=2, padding='SAME')(Con4) # outputs2 Con5 = layers.Conv2D(256, (3, 3), name='Con5', activation='relu', padding='SAME', strides=1)(MaxPooling2) Con6 = layers.Conv2D(256, (3, 3), name='Con6', activation='relu', padding='SAME', strides=1)(Con5) Con7 = layers.Conv2D(256, (3, 3), name='Con7', activation='relu', padding='SAME', strides=1)(Con6) Side3 = sideBranch(Con7, 4) MaxPooling3 = layers.MaxPooling2D((2, 2), name='MaxPooling3', strides=2, padding='SAME')(Con7) # outputs3 Con8 = layers.Conv2D(512, (3, 3), name='Con8', activation='relu', padding='SAME', strides=1)(MaxPooling3) Con9 = layers.Conv2D(512, (3, 3), name='Con9', activation='relu', padding='SAME', strides=1)(Con8) Con10 = layers.Conv2D(512, (3, 3), name='Con10', activation='relu', padding='SAME', strides=1)(Con9) Side4 = sideBranch(Con10, 8) MaxPooling4 = layers.MaxPooling2D((2, 2), name='MaxPooling4', strides=2, padding='SAME')(Con10) # outputs4 Con11 = layers.Conv2D(512, (3, 3), name='Con11', activation='relu', padding='SAME', strides=1)(MaxPooling4) Con12 = layers.Conv2D(512, (3, 3), name='Con12', activation='relu', padding='SAME', strides=1)(Con11) Con13 = layers.Conv2D(512, (3, 3), name='Con13', activation='relu', padding='SAME', strides=1)(Con12) Side5 = sideBranch(Con13, 16) Fuse = layers.Concatenate(axis=-1)([Side1, Side2, Side3, Side4, Side5]) # learn fusion weight Fuse = layers.Conv2D(1, (1, 1), name='Fuse', padding='SAME', use_bias=False, activation=None)(Fuse) output1 = layers.Activation('sigmoid', name='output1')(Side1) output2 = layers.Activation('sigmoid', name='output2')(Side2) output3 = layers.Activation('sigmoid', name='output3')(Side3) output4 = layers.Activation('sigmoid', name='output4')(Side4) output5 = layers.Activation('sigmoid', name='output5')(Side5) output6 = layers.Activation('sigmoid', name='output6')(Fuse) outputs = [output1, output2, output3, output4, output5, output6] self._pModel = Model(inputs=Inputs, outputs=outputs) pAdam = optimizers.adam(lr=0.0001) self._pModel.compile(loss={'output1': classBalancedSigmoidCrossEntropy, 'output2': classBalancedSigmoidCrossEntropy, 'output3': classBalancedSigmoidCrossEntropy, 'output4': classBalancedSigmoidCrossEntropy, 'output5': classBalancedSigmoidCrossEntropy, 'output6': classBalancedSigmoidCrossEntropy }, optimizer=pAdam)
def _reduction_a_cell(ip, p, filters, block_id=None): """Adds a Reduction cell for NASNet-A (Fig. 4 in the paper). Arguments: ip: Input tensor `x` p: Input tensor `p` filters: Number of output filters block_id: String block_id Returns: A Keras tensor """ channel_dim = 1 if backend.image_data_format() == 'channels_first' else -1 with backend.name_scope('reduction_A_block_%s' % block_id): p = _adjust_block(p, ip, filters, block_id) h = layers.Activation('relu')(ip) h = layers.Conv2D( filters, (1, 1), strides=(1, 1), padding='same', name='reduction_conv_1_%s' % block_id, use_bias=False, kernel_initializer='he_normal')( h) h = layers.BatchNormalization( axis=channel_dim, momentum=0.9997, epsilon=1e-3, name='reduction_bn_1_%s' % block_id)( h) h3 = layers.ZeroPadding2D( padding=imagenet_utils.correct_pad(h, 3), name='reduction_pad_1_%s' % block_id)( h) with backend.name_scope('block_1'): x1_1 = _separable_conv_block( h, filters, (5, 5), strides=(2, 2), block_id='reduction_left1_%s' % block_id) x1_2 = _separable_conv_block( p, filters, (7, 7), strides=(2, 2), block_id='reduction_right1_%s' % block_id) x1 = layers.add([x1_1, x1_2], name='reduction_add_1_%s' % block_id) with backend.name_scope('block_2'): x2_1 = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='valid', name='reduction_left2_%s' % block_id)( h3) x2_2 = _separable_conv_block( p, filters, (7, 7), strides=(2, 2), block_id='reduction_right2_%s' % block_id) x2 = layers.add([x2_1, x2_2], name='reduction_add_2_%s' % block_id) with backend.name_scope('block_3'): x3_1 = layers.AveragePooling2D((3, 3), strides=(2, 2), padding='valid', name='reduction_left3_%s' % block_id)( h3) x3_2 = _separable_conv_block( p, filters, (5, 5), strides=(2, 2), block_id='reduction_right3_%s' % block_id) x3 = layers.add([x3_1, x3_2], name='reduction_add3_%s' % block_id) with backend.name_scope('block_4'): x4 = layers.AveragePooling2D((3, 3), strides=(1, 1), padding='same', name='reduction_left4_%s' % block_id)( x1) x4 = layers.add([x2, x4]) with backend.name_scope('block_5'): x5_1 = _separable_conv_block( x1, filters, (3, 3), block_id='reduction_left4_%s' % block_id) x5_2 = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='valid', name='reduction_right5_%s' % block_id)( h3) x5 = layers.add([x5_1, x5_2], name='reduction_add4_%s' % block_id) x = layers.concatenate([x2, x3, x4, x5], axis=channel_dim, name='reduction_concat_%s' % block_id) return x, ip
def MobileNetV2(input_shape=None, alpha=1.0): if input_shape is None: default_size = 224 else: if backend.image_data_format() == 'channels_first': rows = input_shape[1] cols = input_shape[2] else: rows = input_shape[0] cols = input_shape[1] if rows == cols and rows in [96, 128, 160, 192, 224]: default_size = rows else: default_size = 224 input_shape = imagenet_utils.obtain_input_shape( input_shape, default_size=default_size, min_size=32, data_format=backend.image_data_format(), require_flatten=True) row_axis = 0 if backend.image_data_format() == 'channels_last' else 1 rows = input_shape[row_axis] img_input = layers.Input(shape=input_shape) channel_axis = 1 if backend.image_data_format() == 'channels_first' else -1 first_block_filters = _make_divisible(32 * alpha, 8) x = layers.Conv2D(first_block_filters, kernel_size=3, strides=(2, 2), padding='same', use_bias=False, name='Conv1')(img_input) x = layers.BatchNormalization(axis=channel_axis, epsilon=1e-3, momentum=0.999, name='bn_Conv1')(x) x = layers.ReLU(6., name='Conv1_relu')(x) x = _inverted_res_block(x, filters=16, alpha=alpha, stride=1, expansion=1, block_id=0) x = _inverted_res_block(x, filters=24, alpha=alpha, stride=2, expansion=6, block_id=1) x = _inverted_res_block(x, filters=24, alpha=alpha, stride=1, expansion=6, block_id=2) x = _inverted_res_block(x, filters=32, alpha=alpha, stride=2, expansion=6, block_id=3) x = _inverted_res_block(x, filters=32, alpha=alpha, stride=1, expansion=6, block_id=4) x = _inverted_res_block(x, filters=32, alpha=alpha, stride=1, expansion=6, block_id=5) x = _inverted_res_block(x, filters=64, alpha=alpha, stride=2, expansion=6, block_id=6) x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, expansion=6, block_id=7) x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, expansion=6, block_id=8) x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, expansion=6, block_id=9) x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, expansion=6, block_id=10) x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, expansion=6, block_id=11) x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, expansion=6, block_id=12) x = _inverted_res_block(x, filters=160, alpha=alpha, stride=2, expansion=6, block_id=13) x = _inverted_res_block(x, filters=160, alpha=alpha, stride=1, expansion=6, block_id=14) x = _inverted_res_block(x, filters=160, alpha=alpha, stride=1, expansion=6, block_id=15) x = _inverted_res_block(x, filters=320, alpha=alpha, stride=1, expansion=6, block_id=16) # no alpha applied to last conv as stated in the paper: # if the width multiplier is greater than 1 we # increase the number of output channels if alpha > 1.0: last_block_filters = _make_divisible(1280 * alpha, 8) else: last_block_filters = 1280 x = layers.Conv2D(last_block_filters, kernel_size=1, use_bias=False, name='Conv_1')(x) x = layers.BatchNormalization(axis=channel_axis, epsilon=1e-3, momentum=0.999, name='Conv_1_bn')(x) x = layers.ReLU(6., name='out_relu')(x) x = layers.GlobalAveragePooling2D()(x) imagenet_utils.validate_activation('softmax', None) x = layers.Dense(NUM_CLASSES, activation='softmax', name='predictions')(x) # Create model. model = training.Model(img_input, x, name='mobilenetv2_%0.2f_%s' % (alpha, rows)) return model
def resnet(num_blocks, classes=10, training=None): """Instantiates the ResNet architecture. Arguments: num_blocks: integer, the number of conv/identity blocks in each block. The ResNet contains 3 blocks with each block containing one conv block followed by (layers_per_block - 1) number of idenity blocks. Each conv/idenity block has 2 convolutional layers. With the input convolutional layer and the pooling layer towards the end, this brings the total size of the network to (6*num_blocks + 2) classes: optional number of classes to classify images into training: Only used if training keras model with Estimator. In other scenarios it is handled automatically. Returns: A Keras model instance. """ input_shape = (32, 32, 3) img_input = layers.Input(shape=input_shape) if backend.image_data_format() == 'channels_first': x = layers.Lambda( lambda x: backend.permute_dimensions(x, (0, 3, 1, 2)), name='transpose')(img_input) bn_axis = 1 else: # channel_last x = img_input bn_axis = 3 x = layers.ZeroPadding2D(padding=(1, 1), name='conv1_pad')(x) x = layers.Conv2D(16, (3, 3), strides=(1, 1), padding='valid', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY), name='conv1')(x) x = layers.BatchNormalization( axis=bn_axis, momentum=BATCH_NORM_DECAY, epsilon=BATCH_NORM_EPSILON, name='bn_conv1', )(x, training=training) x = layers.Activation('relu')(x) x = resnet_block(x, size=num_blocks, kernel_size=3, filters=[16, 16], stage=2, conv_strides=(1, 1), training=training) x = resnet_block(x, size=num_blocks, kernel_size=3, filters=[32, 32], stage=3, conv_strides=(2, 2), training=training) x = resnet_block(x, size=num_blocks, kernel_size=3, filters=[64, 64], stage=4, conv_strides=(2, 2), training=training) rm_axes = [1, 2 ] if backend.image_data_format() == 'channels_last' else [2, 3] x = layers.Lambda(lambda x: backend.mean(x, rm_axes), name='reduce_mean')(x) x = layers.Dense(classes, activation='softmax', kernel_initializer=initializers.RandomNormal(stddev=0.01), kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY), bias_regularizer=regularizers.l2(L2_WEIGHT_DECAY), name='fc10')(x) inputs = img_input # Create model. model = tf.keras.models.Model(inputs, x, name='resnet56') return model
def DenseNet( blocks, include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000, classifier_activation='softmax', ): """Instantiates the DenseNet architecture. 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`. Caution: Be sure to properly pre-process your inputs to the application. Please see `applications.densenet.preprocess_input` for an example. Arguments: blocks: numbers of building blocks for the four dense layers. include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization), 'imagenet' (pre-training on ImageNet), or the path to the weights file to be loaded. input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. input_shape: optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(224, 224, 3)` (with `'channels_last'` data format) or `(3, 224, 224)` (with `'channels_first'` data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 32. E.g. `(200, 200, 3)` would be one valid value. pooling: optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional block. - `avg` means that global average pooling will be applied to the output of the last convolutional block, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. 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. classifier_activation: A `str` or callable. The activation function to use on the "top" layer. Ignored unless `include_top=True`. Set `classifier_activation=None` to return the logits of the "top" layer. Returns: A `keras.Model` instance. Raises: ValueError: in case of invalid argument for `weights`, or invalid input shape. ValueError: if `classifier_activation` is not `softmax` or `None` when using a pretrained top layer. """ if not (weights in {'imagenet', None} or os.path.exists(weights)): raise ValueError('The `weights` argument should be either ' '`None` (random initialization), `imagenet` ' '(pre-training on ImageNet), ' 'or the path to the weights file to be loaded.') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError( 'If using `weights` as `"imagenet"` with `include_top`' ' as true, `classes` should be 1000') # Determine proper input shape input_shape = imagenet_utils.obtain_input_shape( input_shape, default_size=224, min_size=32, data_format=backend.image_data_format(), require_flatten=include_top, weights=weights) if input_tensor is None: img_input = layers.Input(shape=input_shape) else: if not backend.is_keras_tensor(input_tensor): img_input = layers.Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor bn_axis = 3 if backend.image_data_format() == 'channels_last' else 1 x = layers.ZeroPadding2D(padding=((3, 3), (3, 3)))(img_input) x = layers.Conv2D(64, 7, strides=2, use_bias=False, name='conv1/conv')(x) x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='conv1/bn')(x) x = layers.Activation('relu', name='conv1/relu')(x) x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)))(x) x = layers.MaxPooling2D(3, strides=2, name='pool1')(x) x = dense_block(x, blocks[0], name='conv2') x = transition_block(x, 0.5, name='pool2') x = dense_block(x, blocks[1], name='conv3') x = transition_block(x, 0.5, name='pool3') x = dense_block(x, blocks[2], name='conv4') x = transition_block(x, 0.5, name='pool4') x = dense_block(x, blocks[3], name='conv5') x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='bn')(x) x = layers.Activation('relu', name='relu')(x) if include_top: x = layers.GlobalAveragePooling2D(name='avg_pool')(x) imagenet_utils.validate_activation(classifier_activation, weights) x = layers.Dense(classes, activation=classifier_activation, name='predictions')(x) else: if pooling == 'avg': x = layers.GlobalAveragePooling2D(name='avg_pool')(x) elif pooling == 'max': x = layers.GlobalMaxPooling2D(name='max_pool')(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = layer_utils.get_source_inputs(input_tensor) else: inputs = img_input # Create model. if blocks == [6, 12, 24, 16]: model = training.Model(inputs, x, name='densenet121') elif blocks == [6, 12, 32, 32]: model = training.Model(inputs, x, name='densenet169') elif blocks == [6, 12, 48, 32]: model = training.Model(inputs, x, name='densenet201') else: model = training.Model(inputs, x, name='densenet') # Load weights. if weights == 'imagenet': if include_top: if blocks == [6, 12, 24, 16]: weights_path = data_utils.get_file( 'densenet121_weights_tf_dim_ordering_tf_kernels.h5', DENSENET121_WEIGHT_PATH, cache_subdir='models', file_hash='9d60b8095a5708f2dcce2bca79d332c7') elif blocks == [6, 12, 32, 32]: weights_path = data_utils.get_file( 'densenet169_weights_tf_dim_ordering_tf_kernels.h5', DENSENET169_WEIGHT_PATH, cache_subdir='models', file_hash='d699b8f76981ab1b30698df4c175e90b') elif blocks == [6, 12, 48, 32]: weights_path = data_utils.get_file( 'densenet201_weights_tf_dim_ordering_tf_kernels.h5', DENSENET201_WEIGHT_PATH, cache_subdir='models', file_hash='1ceb130c1ea1b78c3bf6114dbdfd8807') else: if blocks == [6, 12, 24, 16]: weights_path = data_utils.get_file( 'densenet121_weights_tf_dim_ordering_tf_kernels_notop.h5', DENSENET121_WEIGHT_PATH_NO_TOP, cache_subdir='models', file_hash='30ee3e1110167f948a6b9946edeeb738') elif blocks == [6, 12, 32, 32]: weights_path = data_utils.get_file( 'densenet169_weights_tf_dim_ordering_tf_kernels_notop.h5', DENSENET169_WEIGHT_PATH_NO_TOP, cache_subdir='models', file_hash='b8c4d4c20dd625c148057b9ff1c1176b') elif blocks == [6, 12, 48, 32]: weights_path = data_utils.get_file( 'densenet201_weights_tf_dim_ordering_tf_kernels_notop.h5', DENSENET201_WEIGHT_PATH_NO_TOP, cache_subdir='models', file_hash='c13680b51ded0fb44dff2d8f86ac8bb1') model.load_weights(weights_path) elif weights is not None: model.load_weights(weights) return model
def get_cnn_model(): # cnn_model.add(layers.Conv2D( # filters=64, # strides=[2, 2], # kernel_size=[2, 2], # padding="same", # activation=activations.relu, # input_shape=input_shape # )) # cnn_model.add(layers.MaxPooling2D(pool_size=[2, 2], strides=2, padding="same")) # # cnn_model.add(layers.Conv2D( # filters=32, # strides=[2, 2], # kernel_size=[2, 2], # padding="same", # activation=activations.relu # )) # cnn_model.add(layers.MaxPooling2D(pool_size=[2, 2], strides=2, padding="same")) # cnn_model.add(layers.Dropout(0.25)) # cnn_model.add(layers.Flatten()) # cnn_model.add(layers.Dropout(0.25)) # cnn_model.add(layers.Dense( # units=128, # activation=activations.relu, # )) # cnn_model.add(layers.Dropout(0.25)) # cnn_model.add(layers.Dense(num_classes, tf.nn.softmax)) # # cnn_model.compile( # optimizer=optimizers.Adagrad(), # loss=losses.categorical_crossentropy, # metrics=[metrics.top_k_categorical_accuracy] # ) input = layers.Input(shape=input_shape, dtype=tf.float32, name="x") c1 = layers.Conv2D(filters=32, kernel_size=[2, 2], strides=2, padding="same", activation=activations.relu)(input) mp1 = layers.MaxPooling2D(strides=2, padding="same", pool_size=[2, 2])(c1) c2 = layers.Conv2D(filters=64, kernel_size=[2, 2], strides=2, padding="same", activation=activations.relu)(mp1) mp2 = layers.MaxPooling2D(strides=2, padding="same", pool_size=[2, 2])(c2) flat = layers.Flatten()(mp2) dense1 = layers.Dense(units=1024, activation=activations.relu)(flat) output = layers.Dense(units=num_classes, activation=activations.softmax)(dense1) cnn_model = models.Model(inputs=input, outputs=output) cnn_model.compile(optimizer=optimizers.Adagrad(), loss=losses.categorical_crossentropy, metrics=[ metrics.categorical_accuracy, metrics.binary_accuracy, metrics.mean_squared_error ]) return cnn_model
x_train = x_train.astype('float32') x_test = x_test.astype('float32') x_train /= 255 x_test /= 255 y_train = keras.utils.to_categorical(y_train, num_classes) y_test = keras.utils.to_categorical(y_test, num_classes) ############################## # Create model ############################## model = models.Sequential() for cnn_depth in range(1, 3): model.add( layers.Conv2D(32 * cnn_depth, kernel_size=(3, 3), strides=(1, 1), activation='relu', input_shape=input_shape, kernel_regularizer=regularizers.l2(l2_weights))) model.add(layers.Activation("relu")) model.add(layers.BatchNormalization()) model.add( layers.Conv2D(32 * cnn_depth, kernel_size=(3, 3), strides=(1, 1), activation='relu', input_shape=input_shape, kernel_regularizer=regularizers.l2(l2_weights))) model.add(layers.Activation("relu")) model.add(layers.BatchNormalization()) model.add(layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(layers.Dropout(rate=0.1 + 0.025 * cnn_depth))
def conv_block(input_tensor, kernel_size, filters, stage, block, strides=(2, 2)): """A block that has a conv layer at shortcut. Note that from stage 3, the second conv layer at main path is with strides=(2, 2) And the shortcut should have strides=(2, 2) as well Args: input_tensor: input tensor kernel_size: default 3, the kernel size of middle conv layer at main path filters: list of integers, the filters of 3 conv layer at main path stage: integer, current stage label, used for generating layer names block: 'a','b'..., current block label, used for generating layer names strides: Strides for the second conv layer in the block. Returns: Output tensor for the block. """ filters1, filters2, filters3 = filters if backend.image_data_format() == 'channels_last': bn_axis = 3 else: bn_axis = 1 conv_name_base = 'res' + str(stage) + block + '_branch' bn_name_base = 'bn' + str(stage) + block + '_branch' x = layers.Conv2D(filters1, (1, 1), use_bias=False, kernel_initializer='he_normal', kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY), name=conv_name_base + '2a')(input_tensor) x = layers.BatchNormalization(axis=bn_axis, momentum=BATCH_NORM_DECAY, epsilon=BATCH_NORM_EPSILON, name=bn_name_base + '2a')(x) x = layers.Activation('relu')(x) x = layers.Conv2D(filters2, kernel_size, strides=strides, padding='same', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY), name=conv_name_base + '2b')(x) x = layers.BatchNormalization(axis=bn_axis, momentum=BATCH_NORM_DECAY, epsilon=BATCH_NORM_EPSILON, name=bn_name_base + '2b')(x) x = layers.Activation('relu')(x) x = layers.Conv2D(filters3, (1, 1), use_bias=False, kernel_initializer='he_normal', kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY), name=conv_name_base + '2c')(x) x = layers.BatchNormalization(axis=bn_axis, momentum=BATCH_NORM_DECAY, epsilon=BATCH_NORM_EPSILON, name=bn_name_base + '2c')(x) shortcut = layers.Conv2D( filters3, (1, 1), strides=strides, use_bias=False, kernel_initializer='he_normal', kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY), name=conv_name_base + '1')(input_tensor) shortcut = layers.BatchNormalization(axis=bn_axis, momentum=BATCH_NORM_DECAY, epsilon=BATCH_NORM_EPSILON, name=bn_name_base + '1')(shortcut) x = layers.add([x, shortcut]) x = layers.Activation('relu')(x) return x
def define_model(self) -> keras.Model: x = Input(shape=[ self.model_parameters.img_height, self.model_parameters.img_width, self.model_parameters.num_channels ]) z = Input(shape=[ self.model_parameters.img_height, self.model_parameters.img_width, self.model_parameters.num_channels ]) xz = z if self.model_parameters.has_input_images: xz += x xz = layers.Conv2D( filters=32, kernel_size=(3, 3), padding='same', use_bias=False, )(xz) xz = layers.BatchNormalization()(xz) xz = layers.LeakyReLU(alpha=0.2)(xz) xz = layers.Conv2D( filters=32, kernel_size=(3, 3), padding='same', use_bias=False, )(xz) xz = layers.BatchNormalization()(xz) xz = layers.LeakyReLU(alpha=0.2)(xz) xz = layers.Conv2D( filters=32, kernel_size=(3, 3), padding='same', use_bias=False, )(xz) xz = layers.BatchNormalization()(xz) xz = layers.LeakyReLU(alpha=0.2)(xz) xz = layers.Conv2D( filters=32, kernel_size=(3, 3), padding='same', use_bias=False, )(xz) xz = layers.BatchNormalization()(xz) xz = layers.LeakyReLU(alpha=0.2)(xz) xz = layers.Conv2D( filters=3, kernel_size=(3, 3), padding='same', activation='tanh', use_bias=False, )(xz) if self.model_parameters.has_input_images: xz += x model = Model(name=self.model_name, inputs=[x, z], outputs=xz) return model
decoder4 = decoder_block(center, encoder4, 512) # 16 decoder3 = decoder_block(decoder4, encoder3, 256) # 32 decoder2 = decoder_block(decoder3, encoder2, 128) # 64 decoder1 = decoder_block(decoder2, encoder1, 64) # 128 decoder0 = decoder_block(decoder1, encoder0, 32) # 256 outputs = layers.Conv2D(1, (1, 1), activation='sigmoid')(decoder0) model = models.Model(inputs=[inputs], outputs=[outputs]) def dice_coeff(y_true, y_pred): smooth = 1. # Flatten y_true_f = tf.reshape(y_true, [-1]) y_pred_f = tf.reshape(y_pred, [-1]) intersection = tf.reduce_sum(y_true_f * y_pred_f) score = (2. * intersection + smooth) / (tf.reduce_sum(y_true_f) + tf.reduce_sum(y_pred_f) + smooth) return score
def block3(x, filters, kernel_size=3, stride=1, groups=32, conv_shortcut=True, name=None): """A residual block. Arguments: x: input tensor. filters: integer, filters of the bottleneck layer. kernel_size: default 3, kernel size of the bottleneck layer. stride: default 1, stride of the first layer. groups: default 32, group size for grouped convolution. conv_shortcut: default True, use convolution shortcut if True, otherwise identity shortcut. name: string, block label. Returns: Output tensor for the residual block. """ bn_axis = 3 if backend.image_data_format() == 'channels_last' else 1 if conv_shortcut: shortcut = layers.Conv2D( (64 // groups) * filters, 1, strides=stride, use_bias=False, name=name + '_0_conv')(x) shortcut = layers.BatchNormalization( axis=bn_axis, epsilon=1.001e-5, name=name + '_0_bn')(shortcut) else: shortcut = x x = layers.Conv2D(filters, 1, use_bias=False, name=name + '_1_conv')(x) x = layers.BatchNormalization( axis=bn_axis, epsilon=1.001e-5, name=name + '_1_bn')(x) x = layers.Activation('relu', name=name + '_1_relu')(x) c = filters // groups x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)), name=name + '_2_pad')(x) x = layers.DepthwiseConv2D( kernel_size, strides=stride, depth_multiplier=c, use_bias=False, name=name + '_2_conv')(x) x_shape = backend.int_shape(x)[1:-1] x = layers.Reshape(x_shape + (groups, c, c))(x) x = layers.Lambda( lambda x: sum(x[:, :, :, :, i] for i in range(c)), name=name + '_2_reduce')(x) x = layers.Reshape(x_shape + (filters,))(x) x = layers.BatchNormalization( axis=bn_axis, epsilon=1.001e-5, name=name + '_2_bn')(x) x = layers.Activation('relu', name=name + '_2_relu')(x) x = layers.Conv2D( (64 // groups) * filters, 1, use_bias=False, name=name + '_3_conv')(x) x = layers.BatchNormalization( axis=bn_axis, epsilon=1.001e-5, name=name + '_3_bn')(x) x = layers.Add(name=name + '_add')([shortcut, x]) x = layers.Activation('relu', name=name + '_out')(x) return x