# attn_layer = Conv2D(8, kernel_size = (1,1), padding = 'same', activation = 'relu')(attn_layer) # attn_layer = Conv2D(1, kernel_size = (1,1), padding = 'valid',activation = 'sigmoid')(attn_layer) # # fan it out to all of the channels # up_c2_w = np.ones((1, 1, 1, pt_depth)) # up_c2 = Conv2D(pt_depth, kernel_size = (1,1), padding = 'same',activation = 'linear', use_bias = False, weights = [up_c2_w]) # up_c2.trainable = False # attn_layer = up_c2(attn_layer) # mask_features = multiply([attn_layer, bn_features]) # gap_features = GlobalAveragePooling2D()(mask_features) # gap_mask = GlobalAveragePooling2D()(attn_layer) # to account for missing values from the attention model # gap = Lambda(lambda x: x[0]/x[1], name = 'RescaleGAP')([gap_features, gap_mask]) glb_max = GlobalMaxPooling2D()(bn_features) gap_dr = Dropout(0.25)(glb_max) dr_steps = Dropout(0.25)(Dense(512, activation=LeakyReLU(alpha=0.5))(gap_dr)) dr_steps = Dropout(0.25)(Dense(512, activation=LeakyReLU(alpha=0.5))(dr_steps)) dr_steps = Dropout(0.25)(Dense(512, activation='relu')(dr_steps)) dr_steps = Dropout(0.25)(Dense(512, activation='relu')(dr_steps)) out_layer = Dense(5, activation='softmax')(dr_steps) model = Model(inputs=[in_lay], outputs=[out_layer]) # from keras.metrics import top_k_categorical_accuracy # def top_2_accuracy(in_gt, in_pred): # return top_k_categorical_accuracy(in_gt, in_pred, k=2) # retina_model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', # metrics = ['categorical_accuracy', top_2_accuracy])
def InceptionResNetV2(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000, dropout_keep_prob=0.8): """Instantiates the Inception-ResNet v2 architecture. Optionally loads weights pre-trained on ImageNet. Note that when using TensorFlow, for best performance you should set `"image_data_format": "channels_last"` in your Keras config at `~/.keras/keras.json`. The model and the weights are compatible with both TensorFlow and Theano. The data format convention used by the model is the one specified in your Keras config file. Note that the default input image size for this model is 299x299, instead of 224x224 as in the VGG16 and ResNet models. Also, the input preprocessing function is different (i.e., do not use `imagenet_utils.preprocess_input()` with this model. Use `preprocess_input()` defined in this module instead). # Arguments include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization) or `'imagenet'` (pre-training on ImageNet). 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 `(299, 299, 3)` (with `channels_last` data format) or `(3, 299, 299)` (with `channels_first` data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 139. E.g. `(150, 150, 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 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. dropout_keep_prob: dropout keep rate after pooling and before the classification layer, only to be specified if `include_top` is `True`. # Returns A Keras `Model` instance. # Raises ValueError: in case of invalid argument for `weights`, or invalid input shape. """ if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') 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=299, min_size=139, data_format=K.image_data_format(), require_flatten=False, weights=weights) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor # Stem block: 35 x 35 x 192 x = conv2d_bn(img_input, 32, 3, strides=2, padding='valid', name='Conv2d_1a_3x3') x = conv2d_bn(x, 32, 3, padding='valid', name='Conv2d_2a_3x3') x = conv2d_bn(x, 64, 3, name='Conv2d_2b_3x3') x = MaxPooling2D(3, strides=2, name='MaxPool_3a_3x3')(x) x = conv2d_bn(x, 80, 1, padding='valid', name='Conv2d_3b_1x1') x = conv2d_bn(x, 192, 3, padding='valid', name='Conv2d_4a_3x3') x = MaxPooling2D(3, strides=2, name='MaxPool_5a_3x3')(x) # Mixed 5b (Inception-A block): 35 x 35 x 320 channel_axis = 1 if K.image_data_format() == 'channels_first' else 3 name_fmt = partial(_generate_layer_name, prefix='Mixed_5b') branch_0 = conv2d_bn(x, 96, 1, name=name_fmt('Conv2d_1x1', 0)) branch_1 = conv2d_bn(x, 48, 1, name=name_fmt('Conv2d_0a_1x1', 1)) branch_1 = conv2d_bn(branch_1, 64, 5, name=name_fmt('Conv2d_0b_5x5', 1)) branch_2 = conv2d_bn(x, 64, 1, name=name_fmt('Conv2d_0a_1x1', 2)) branch_2 = conv2d_bn(branch_2, 96, 3, name=name_fmt('Conv2d_0b_3x3', 2)) branch_2 = conv2d_bn(branch_2, 96, 3, name=name_fmt('Conv2d_0c_3x3', 2)) branch_pool = AveragePooling2D(3, strides=1, padding='same', name=name_fmt('AvgPool_0a_3x3', 3))(x) branch_pool = conv2d_bn(branch_pool, 64, 1, name=name_fmt('Conv2d_0b_1x1', 3)) branches = [branch_0, branch_1, branch_2, branch_pool] x = Concatenate(axis=channel_axis, name='Mixed_5b')(branches) # 10x Block35 (Inception-ResNet-A block): 35 x 35 x 320 for block_idx in range(1, 11): x = _inception_resnet_block(x, scale=0.17, block_type='Block35', block_idx=block_idx) # Mixed 6a (Reduction-A block): 17 x 17 x 1088 name_fmt = partial(_generate_layer_name, prefix='Mixed_6a') branch_0 = conv2d_bn(x, 384, 3, strides=2, padding='valid', name=name_fmt('Conv2d_1a_3x3', 0)) branch_1 = conv2d_bn(x, 256, 1, name=name_fmt('Conv2d_0a_1x1', 1)) branch_1 = conv2d_bn(branch_1, 256, 3, name=name_fmt('Conv2d_0b_3x3', 1)) branch_1 = conv2d_bn(branch_1, 384, 3, strides=2, padding='valid', name=name_fmt('Conv2d_1a_3x3', 1)) branch_pool = MaxPooling2D(3, strides=2, padding='valid', name=name_fmt('MaxPool_1a_3x3', 2))(x) branches = [branch_0, branch_1, branch_pool] x = Concatenate(axis=channel_axis, name='Mixed_6a')(branches) # 20x Block17 (Inception-ResNet-B block): 17 x 17 x 1088 for block_idx in range(1, 21): x = _inception_resnet_block(x, scale=0.1, block_type='Block17', block_idx=block_idx) # Mixed 7a (Reduction-B block): 8 x 8 x 2080 name_fmt = partial(_generate_layer_name, prefix='Mixed_7a') branch_0 = conv2d_bn(x, 256, 1, name=name_fmt('Conv2d_0a_1x1', 0)) branch_0 = conv2d_bn(branch_0, 384, 3, strides=2, padding='valid', name=name_fmt('Conv2d_1a_3x3', 0)) branch_1 = conv2d_bn(x, 256, 1, name=name_fmt('Conv2d_0a_1x1', 1)) branch_1 = conv2d_bn(branch_1, 288, 3, strides=2, padding='valid', name=name_fmt('Conv2d_1a_3x3', 1)) branch_2 = conv2d_bn(x, 256, 1, name=name_fmt('Conv2d_0a_1x1', 2)) branch_2 = conv2d_bn(branch_2, 288, 3, name=name_fmt('Conv2d_0b_3x3', 2)) branch_2 = conv2d_bn(branch_2, 320, 3, strides=2, padding='valid', name=name_fmt('Conv2d_1a_3x3', 2)) branch_pool = MaxPooling2D(3, strides=2, padding='valid', name=name_fmt('MaxPool_1a_3x3', 3))(x) branches = [branch_0, branch_1, branch_2, branch_pool] x = Concatenate(axis=channel_axis, name='Mixed_7a')(branches) # 10x Block8 (Inception-ResNet-C block): 8 x 8 x 2080 for block_idx in range(1, 10): x = _inception_resnet_block(x, scale=0.2, block_type='Block8', block_idx=block_idx) x = _inception_resnet_block(x, scale=1., activation=None, block_type='Block8', block_idx=10) # Final convolution block x = conv2d_bn(x, 1536, 1, name='Conv2d_7b_1x1') if include_top: # Classification block x = GlobalAveragePooling2D(name='AvgPool')(x) x = Dropout(1.0 - dropout_keep_prob, name='Dropout')(x) x = Dense(classes, name='Logits')(x) x = Activation('softmax', name='Predictions')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D(name='AvgPool')(x) elif pooling == 'max': x = GlobalMaxPooling2D(name='MaxPool')(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor` if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model model = Model(inputs, x, name='inception_resnet_v2') # Load weights if weights == 'imagenet': if K.image_data_format() == 'channels_first': if K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') if include_top: weights_filename = 'inception_resnet_v2_weights_tf_dim_ordering_tf_kernels.h5' weights_path = get_file( weights_filename, BASE_WEIGHT_URL + weights_filename, cache_subdir='models', md5_hash='e693bd0210a403b3192acc6073ad2e96') else: weights_filename = 'inception_resnet_v2_weights_tf_dim_ordering_tf_kernels_notop.h5' weights_path = get_file( weights_filename, BASE_WEIGHT_URL + weights_filename, cache_subdir='models', md5_hash='d19885ff4a710c122648d3b5c3b684e4') model.load_weights(weights_path) return model
def EfficientNet(width_coefficient, depth_coefficient, default_size, dropout_rate=0.2, drop_connect_rate=0.2, depth_divisor=8, activation_fn=swish, blocks_args=DEFAULT_BLOCKS_ARGS, model_name='efficientnet', include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, num_classes=1000, **kwargs): # Instantiates the EfficientNet architecture using given scaling coefficients. """ # Arguments 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. blocks_args: list of dicts, parameters to construct block modules. model_name: string, model name. include_top: whether to include the FC layer at the top of the network. weights: `None` (random initialization), 'imagenet' or the path to any weights. input_tensor: optional Keras tensor (output of `layers.Input()`) input_shape: tuple, only to be specified if `include_top` is False. pooling: Optional mode for feature extraction when `include_top` is `False`. - `None`: the output of model is the 4D tensor of the last conv layer - `avg` means global average pooling and the output as a 2D tensor. - `max` means global max pooling will be applied. num_classes: specified if `include_top` is True # 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 num_classes != 1000: raise ValueError( 'If using `weights` as `"imagenet"` with `include_top`' ' as true, `classes` should be 1000') # Determine the proper input shape input_shape = _obtain_input_shape(input_shape, default_size=default_size, min_size=32, data_format=K.image_data_format(), require_flatten=include_top, weights=weights) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor bn_axis = -1 if K.image_data_format() == 'channels_last' else 1 def round_filters(filters, divisor=depth_divisor): # Round number of filters based on depth multiplier. filters *= width_coefficient new_filters = max(divisor, int(filters + divisor / 2) // divisor * divisor) # Ensure the 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)) # Build the stem x = img_input x = ZeroPadding2D(padding=correct_pad(K, x, 3), name='stem_conv_pad')(x) x = Conv2D(round_filters(32), kernel_size=(3, 3), strides=2, padding='valid', use_bias=False, kernel_initializer=CONV_KERNEL_INITIALIZER, name='stem_conv')(x) x = BatchNormalization(axis=bn_axis, name='stem_bn')(x) x = Activation(activation_fn, name='stem_activation')(x) # Build the blocks from copy import deepcopy blocks_args = deepcopy(blocks_args) # See the above blocks_args b = 0 blocks = float(sum(args['repeats'] for args in blocks_args)) for (i, args) in enumerate(blocks_args): assert args['repeats'] > 0 # Update the 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 growth. if j > 0: args['strides'] = 1 args['filters_in'] = args['filters_out'] x = block(x, activation_fn, drop_connect_rate * b / blocks, name='block{}{}_'.format(i + 1, chr(j + 97)), **args) b += 1 # Build the top x = Conv2D(round_filters(1280), kernel_size=(1, 1), padding='same', use_bias=False, kernel_initializer=CONV_KERNEL_INITIALIZER, name='top_conv')(x) x = BatchNormalization(axis=bn_axis, name='top_bn')(x) x = Activation(activation_fn, name='top_activation')(x) if include_top: x = GlobalAveragePooling2D(name='avg_pool')(x) if dropout_rate > 0: x = Dropout(dropout_rate, name='top_dropout')(x) x = Dense(num_classes, activation='softmax', kernel_initializer=DENSE_KERNEL_INITIALIZER, name='probs')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D(name='avg_pool')(x) elif pooling == 'max': x = GlobalMaxPooling2D(name='max_pool')(x) # Ensure the model considers any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Build the model. model = Model(inputs, x, name=model_name) # Load weights. if weights == 'imagenet': if include_top: file_suff = '_weights_tf_dim_ordering_tf_kernels_autoaugment.h5' file_hash = WEIGHTS_HASHES[model_name[-2:]][0] else: file_suff = '_weights_tf_dim_ordering_tf_kernels_autoaugment_notop.h5' file_hash = WEIGHTS_HASHES[model_name[-2:]][1] file_name = model_name + file_suff weights_path = 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 ResNet(input_shape=None, classes=10, block='bottleneck', residual_unit='v2', repetitions=None, initial_filters=64, activation='softmax', include_top=True, input_tensor=None, dropout=None, transition_dilation_rate=(1, 1), initial_strides=(2, 2), initial_kernel_size=(7, 7), initial_pooling='max', final_pooling=None, top='classification'): """Builds a custom ResNet like architecture. Defaults to ResNet50 v2. Args: 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` dim ordering) or `(3, 224, 224)` (with `channels_first` dim ordering). It should have exactly 3 dimensions, and width and height should be no smaller than 8. E.g. `(224, 224, 3)` would be one valid value. classes: The number of outputs at final softmax layer block: The block function to use. This is either `'basic'` or `'bottleneck'`. The original paper used `basic` for layers < 50. repetitions: Number of repetitions of various block units. At each block unit, the number of filters are doubled and the input size is halved. Default of None implies the ResNet50v2 values of [3, 4, 6, 3]. residual_unit: the basic residual unit, 'v1' for conv bn relu, 'v2' for bn relu conv. See [Identity Mappings in Deep Residual Networks](https://arxiv.org/abs/1603.05027) for details. dropout: None for no dropout, otherwise rate of dropout from 0 to 1. Based on [Wide Residual Networks.(https://arxiv.org/pdf/1605.07146) paper. transition_dilation_rate: Dilation rate for transition layers. For semantic segmentation of images use a dilation rate of (2, 2). initial_strides: Stride of the very first residual unit and MaxPooling2D call, with default (2, 2), set to (1, 1) for small images like cifar. initial_kernel_size: kernel size of the very first convolution, (7, 7) for imagenet and (3, 3) for small image datasets like tiny imagenet and cifar. See [ResNeXt](https://arxiv.org/abs/1611.05431) paper for details. initial_pooling: Determine if there will be an initial pooling layer, 'max' for imagenet and None for small image datasets. See [ResNeXt](https://arxiv.org/abs/1611.05431) paper for details. final_pooling: Optional pooling mode for feature extraction at the final model layer 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. top: Defines final layers to evaluate based on a specific problem type. Options are 'classification' for ImageNet style problems, 'segmentation' for problems like the Pascal VOC dataset, and None to exclude these layers entirely. Returns: The keras `Model`. """ if activation not in ['softmax', 'sigmoid', None]: raise ValueError('activation must be one of "softmax", "sigmoid", or None') if activation == 'sigmoid' and classes != 1: raise ValueError('sigmoid activation can only be used when classes = 1') if repetitions is None: repetitions = [3, 4, 6, 3] # Determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=32, min_size=8, data_format=K.image_data_format(), require_flatten=include_top) _handle_dim_ordering() if len(input_shape) != 3: raise Exception("Input shape should be a tuple (nb_channels, nb_rows, nb_cols)") if block == 'basic': block_fn = basic_block elif block == 'bottleneck': block_fn = bottleneck elif isinstance(block, six.string_types): block_fn = _string_to_function(block) else: block_fn = block if residual_unit == 'v2': residual_unit = _bn_relu_conv elif residual_unit == 'v1': residual_unit = _conv_bn_relu elif isinstance(residual_unit, six.string_types): residual_unit = _string_to_function(residual_unit) else: residual_unit = residual_unit # Permute dimension order if necessary if K.image_data_format() == 'channels_first': input_shape = (input_shape[1], input_shape[2], input_shape[0]) # Determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=32, min_size=8, data_format=K.image_data_format(), require_flatten=include_top) img_input = Input(shape=input_shape, tensor=input_tensor) x = _conv_bn_relu(filters=initial_filters, kernel_size=initial_kernel_size, strides=initial_strides)(img_input) if initial_pooling == 'max': x = MaxPooling2D(pool_size=(3, 3), strides=initial_strides, padding="same")(x) block = x filters = initial_filters for i, r in enumerate(repetitions): transition_dilation_rates = [transition_dilation_rate] * r transition_strides = [(1, 1)] * r if transition_dilation_rate == (1, 1): transition_strides[0] = (2, 2) block = _residual_block(block_fn, filters=filters, stage=i, blocks=r, is_first_layer=(i == 0), dropout=dropout, transition_dilation_rates=transition_dilation_rates, transition_strides=transition_strides, residual_unit=residual_unit)(block) filters *= 2 # Last activation x = _bn_relu(block) # Classifier block if include_top and top is 'classification': x = GlobalAveragePooling2D()(x) x = Dense(units=classes, activation=activation, kernel_initializer="he_normal")(x) elif include_top and top is 'segmentation': x = Conv2D(classes, (1, 1), activation='linear', padding='same')(x) if K.image_data_format() == 'channels_first': channel, row, col = input_shape else: row, col, channel = input_shape x = Reshape((row * col, classes))(x) x = Activation(activation)(x) x = Reshape((row, col, classes))(x) elif final_pooling == 'avg': x = GlobalAveragePooling2D()(x) elif final_pooling == 'max': x = GlobalMaxPooling2D()(x) model = Model(inputs=img_input, outputs=x) return model
def VGG16(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000): if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') 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=48, data_format=K.image_data_format(), include_top=include_top) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor #Block_1 x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input) x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) #Block_2 x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x) x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) #Block_3 x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) #Block_4 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x) #Block_5 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x) if include_top: x = Flatten(name='flatten')(x) x = Dense(4096, activation='relu', name='fc1')(x) x = Dense(4096, activation='relu', name='fc2')(x) x = Dense(classes, activation='softmax', name='predictions')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) #Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input #create model model = Model(inputs, x, name='vgg16') #load weights if weights == 'imagenet': if include_top: weights_path = get_file( 'vgg16_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH, cache_subdir='models') else: weights_path = get_file( 'vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models') model.load_weights(weights_path) if K.backend() == 'theano': layer_utils.convert_all_kernels_in_model(model) if K.image_data_format() == 'channels_first': if include_top: maxpool = model.get_layer(name='block5_pool') shape = maxpool.output_shape[1:] dense = model.get_layer(name='fc1') layer_utils.convert_dense_weights_data_format( dense, shape, 'channels_first') if K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') return model
def VGG16(include_top=True, weights='vggface', input_tensor=None, input_shape=None, pooling=None, classes=2622): input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=48, data_format=K.image_data_format(), require_flatten=include_top) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor # Block 1 x = Conv2D(64, (3, 3), activation='relu', padding='same', name='conv1_1')( img_input) x = Conv2D(64, (3, 3), activation='relu', padding='same', name='conv1_2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='pool1')(x) # Block 2 x = Conv2D(128, (3, 3), activation='relu', padding='same', name='conv2_1')( x) x = Conv2D(128, (3, 3), activation='relu', padding='same', name='conv2_2')( x) x = MaxPooling2D((2, 2), strides=(2, 2), name='pool2')(x) # Block 3 x = Conv2D(256, (3, 3), activation='relu', padding='same', name='conv3_1')( x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='conv3_2')( x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='conv3_3')( x) x = MaxPooling2D((2, 2), strides=(2, 2), name='pool3')(x) # Block 4 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv4_1')( x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv4_2')( x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv4_3')( x) x = MaxPooling2D((2, 2), strides=(2, 2), name='pool4')(x) # Block 5 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv5_1')( x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv5_2')( x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv5_3')( x) x = MaxPooling2D((2, 2), strides=(2, 2), name='pool5')(x) if include_top: # Classification block x = Flatten(name='flatten')(x) x = Dense(4096, name='fc6')(x) x = Activation('relu', name='fc6/relu')(x) x = Dense(4096, name='fc7')(x) x = Activation('relu', name='fc7/relu')(x) x = Dense(classes, name='fc8')(x) x = Activation('softmax', name='fc8/softmax')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x, name='vggface_vgg16') # load weights if weights == 'vggface': if include_top: weights_path = get_file('rcmalli_vggface_tf_vgg16.h5', utils. VGG16_WEIGHTS_PATH, cache_subdir=utils.VGGFACE_DIR) else: weights_path = get_file('rcmalli_vggface_tf_notop_vgg16.h5', utils.VGG16_WEIGHTS_PATH_NO_TOP, cache_subdir=utils.VGGFACE_DIR) model.load_weights(weights_path, by_name=True) if K.backend() == 'theano': layer_utils.convert_all_kernels_in_model(model) if K.image_data_format() == 'channels_first': if include_top: maxpool = model.get_layer(name='pool5') shape = maxpool.output_shape[1:] dense = model.get_layer(name='fc6') layer_utils.convert_dense_weights_data_format(dense, shape, 'channels_first') if K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') return model
def InceptionV3(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000): """Instantiates the Inception v3 architecture. Optionally loads weights pre-trained on ImageNet. Note that when using TensorFlow, for best performance you should set `image_data_format="channels_last"` in your Keras config at ~/.keras/keras.json. The model and the weights are compatible with both TensorFlow and Theano. The data format convention used by the model is the one specified in your Keras config file. Note that the default input image size for this model is 299x299. Arguments: include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization) or "imagenet" (pre-training on ImageNet). 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 `(299, 299, 3)` (with `channels_last` data format) or `(3, 299, 299)` (with `channels_first` data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 139. E.g. `(150, 150, 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 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. Returns: A Keras model instance. Raises: ValueError: in case of invalid argument for `weights`, or invalid input shape. """ if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') 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=299, min_size=139, data_format=K.image_data_format(), include_top=include_top) if input_tensor is None: img_input = Input(shape=input_shape) else: img_input = Input(tensor=input_tensor, shape=input_shape) if K.image_data_format() == 'channels_first': channel_axis = 1 else: channel_axis = 3 x = conv2d_bn(img_input, 32, 3, 3, strides=(2, 2), padding='valid') x = conv2d_bn(x, 32, 3, 3, padding='valid') x = conv2d_bn(x, 64, 3, 3) x = MaxPooling2D((3, 3), strides=(2, 2))(x) x = conv2d_bn(x, 80, 1, 1, padding='valid') x = conv2d_bn(x, 192, 3, 3, padding='valid') x = MaxPooling2D((3, 3), strides=(2, 2))(x) # mixed 0, 1, 2: 35 x 35 x 256 branch1x1 = conv2d_bn(x, 64, 1, 1) branch5x5 = conv2d_bn(x, 48, 1, 1) branch5x5 = conv2d_bn(branch5x5, 64, 5, 5) branch3x3dbl = conv2d_bn(x, 64, 1, 1) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 32, 1, 1) x = layers.concatenate([branch1x1, branch5x5, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed0') # mixed 1: 35 x 35 x 256 branch1x1 = conv2d_bn(x, 64, 1, 1) branch5x5 = conv2d_bn(x, 48, 1, 1) branch5x5 = conv2d_bn(branch5x5, 64, 5, 5) branch3x3dbl = conv2d_bn(x, 64, 1, 1) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 64, 1, 1) x = layers.concatenate([branch1x1, branch5x5, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed1') # mixed 2: 35 x 35 x 256 branch1x1 = conv2d_bn(x, 64, 1, 1) branch5x5 = conv2d_bn(x, 48, 1, 1) branch5x5 = conv2d_bn(branch5x5, 64, 5, 5) branch3x3dbl = conv2d_bn(x, 64, 1, 1) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 64, 1, 1) x = layers.concatenate([branch1x1, branch5x5, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed2') # mixed 3: 17 x 17 x 768 branch3x3 = conv2d_bn(x, 384, 3, 3, strides=(2, 2), padding='valid') branch3x3dbl = conv2d_bn(x, 64, 1, 1) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3, strides=(2, 2), padding='valid') branch_pool = MaxPooling2D((3, 3), strides=(2, 2))(x) x = layers.concatenate([branch3x3, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed3') # mixed 4: 17 x 17 x 768 branch1x1 = conv2d_bn(x, 192, 1, 1) branch7x7 = conv2d_bn(x, 128, 1, 1) branch7x7 = conv2d_bn(branch7x7, 128, 1, 7) branch7x7 = conv2d_bn(branch7x7, 192, 7, 1) branch7x7dbl = conv2d_bn(x, 128, 1, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 1, 7) branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 192, 1, 1) x = layers.concatenate([branch1x1, branch7x7, branch7x7dbl, branch_pool], axis=channel_axis, name='mixed4') # mixed 5, 6: 17 x 17 x 768 for i in range(2): branch1x1 = conv2d_bn(x, 192, 1, 1) branch7x7 = conv2d_bn(x, 160, 1, 1) branch7x7 = conv2d_bn(branch7x7, 160, 1, 7) branch7x7 = conv2d_bn(branch7x7, 192, 7, 1) branch7x7dbl = conv2d_bn(x, 160, 1, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 1, 7) branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 192, 1, 1) x = layers.concatenate( [branch1x1, branch7x7, branch7x7dbl, branch_pool], axis=channel_axis, name='mixed' + str(5 + i)) # mixed 7: 17 x 17 x 768 branch1x1 = conv2d_bn(x, 192, 1, 1) branch7x7 = conv2d_bn(x, 192, 1, 1) branch7x7 = conv2d_bn(branch7x7, 192, 1, 7) branch7x7 = conv2d_bn(branch7x7, 192, 7, 1) branch7x7dbl = conv2d_bn(x, 192, 1, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 192, 1, 1) x = layers.concatenate([branch1x1, branch7x7, branch7x7dbl, branch_pool], axis=channel_axis, name='mixed7') # mixed 8: 8 x 8 x 1280 branch3x3 = conv2d_bn(x, 192, 1, 1) branch3x3 = conv2d_bn(branch3x3, 320, 3, 3, strides=(2, 2), padding='valid') branch7x7x3 = conv2d_bn(x, 192, 1, 1) branch7x7x3 = conv2d_bn(branch7x7x3, 192, 1, 7) branch7x7x3 = conv2d_bn(branch7x7x3, 192, 7, 1) branch7x7x3 = conv2d_bn(branch7x7x3, 192, 3, 3, strides=(2, 2), padding='valid') branch_pool = MaxPooling2D((3, 3), strides=(2, 2))(x) x = layers.concatenate([branch3x3, branch7x7x3, branch_pool], axis=channel_axis, name='mixed8') # mixed 9: 8 x 8 x 2048 for i in range(2): branch1x1 = conv2d_bn(x, 320, 1, 1) branch3x3 = conv2d_bn(x, 384, 1, 1) branch3x3_1 = conv2d_bn(branch3x3, 384, 1, 3) branch3x3_2 = conv2d_bn(branch3x3, 384, 3, 1) branch3x3 = layers.concatenate([branch3x3_1, branch3x3_2], axis=channel_axis, name='mixed9_' + str(i)) branch3x3dbl = conv2d_bn(x, 448, 1, 1) branch3x3dbl = conv2d_bn(branch3x3dbl, 384, 3, 3) branch3x3dbl_1 = conv2d_bn(branch3x3dbl, 384, 1, 3) branch3x3dbl_2 = conv2d_bn(branch3x3dbl, 384, 3, 1) branch3x3dbl = layers.concatenate([branch3x3dbl_1, branch3x3dbl_2], axis=channel_axis) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 192, 1, 1) x = layers.concatenate( [branch1x1, branch3x3, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed' + str(9 + i)) if include_top: # Classification block x = GlobalAveragePooling2D(name='avg_pool')(x) x = Dense(classes, activation='softmax', name='predictions')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x, name='inception_v3') # load weights if weights == 'imagenet': if K.image_data_format() == 'channels_first': if K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') if include_top: weights_path = get_file( 'inception_v3_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH, cache_subdir='models', md5_hash='9a0d58056eeedaa3f26cb7ebd46da564') else: weights_path = get_file( 'inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models', md5_hash='bcbd6486424b2319ff4ef7d526e38f63') model.load_weights(weights_path) if K.backend() == 'theano': convert_all_kernels_in_model(model) return model
def test_resnet(): # for reproducibility rand_seed = 1337 np.random.seed(rand_seed) out_size = (49, 2) roi_size = 128 input_shape = (roi_size, roi_size, 1) img_input = Input(shape=input_shape) x = Conv2D(32, (3, 3), padding='same', use_bias=False, name='conv0')(img_input) x = BatchNormalization(name='conv0a_bn')(x) x = Activation('relu', name='conv0_act')(x) x = MaxPooling2D((2, 2), name='conv0_pool')(x) x = Conv2D(64, (3, 3), padding='same', use_bias=False, name='conv1a')(x) x = BatchNormalization(name='conv1a_bn')(x) x = Activation('relu', name='conv1a_act')(x) residual = Conv2D(128, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x) residual = BatchNormalization()(residual) x = Conv2D(128, (3, 3), padding='same', use_bias=False, name='block2_sepconv1')(x) x = BatchNormalization(name='block2_sepconv1_bn')(x) x = Activation('relu', name='block2_sepconv2_act')(x) x = Conv2D(128, (3, 3), padding='same', use_bias=False, name='block2_sepconv2')(x) x = BatchNormalization(name='block2_sepconv2_bn')(x) x = MaxPooling2D((2, 2), padding='same', name='block2_pool')(x) x = layers.add([x, residual]) residual = Conv2D(512, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x) residual = BatchNormalization()(residual) x = Activation('relu', name='block3_sepconv1_act')(x) x = Conv2D(256, (3, 3), padding='same', use_bias=False, name='block3_sepconv1')(x) x = BatchNormalization(name='block3_sepconv1_bn')(x) x = Activation('relu', name='block3_sepconv2_act')(x) x = Conv2D(512, (3, 3), padding='same', use_bias=False, name='block3_sepconv2')(x) x = BatchNormalization(name='block3_sepconv2_bn')(x) x = MaxPooling2D((2, 2), padding='same', name='block3_pool')(x) x = layers.add([x, residual]) x = Conv2D(1024, (3, 3), padding='same', use_bias=False, name='block14_sepconv2')(x) x = BatchNormalization(name='block14_sepconv2_bn')(x) x = Activation('relu', name='block14_sepconv2_act')(x) x = GlobalMaxPooling2D(name='avg_pool')(x) #layers seems to be key to capture the worm shape #i need to use ELU instead of RELU otherwise the skeletons converges to 0 x = Dense(1024, name='dense0', activation='elu')(x) x = Dropout(0.4)(x) x = Dense(512, name='dense1', activation='elu')(x) x = Dropout(0.4)(x) x = Dense(256, name='dense2', activation='elu')(x) x = Dropout(0.2)(x) x = Dense(128, name='dense3', activation='elu')(x) x = Dropout(0.1)(x) x = Dense(out_size[0]*out_size[1], activation='elu', name='skeleton')(x) x = Reshape((out_size))(x) model = Model(img_input, x) optimizer = Adam(lr=1e-3)#, decay=0.05) model.compile(loss='mean_absolute_error', optimizer=optimizer, metrics=['mean_absolute_error', 'mean_squared_error', 'mean_absolute_percentage_error']) return model
def test_simple_v2(out_size = (49, 2), roi_size = 128): # for reproducibility rand_seed = 1337 np.random.seed(rand_seed) input_shape = (roi_size, roi_size, 1) img_input = Input(shape=input_shape) x = Conv2D(32, (3, 3), padding='same', name='conv0')(img_input) x = Activation('relu', name='conv0_act')(x) x = MaxPooling2D((2, 2), name='conv0_pool')(x) x = Conv2D(64, (3, 3), padding='same', name='conv1a')(x) x = BatchNormalization(name='conv1a_bn')(x) x = Activation('relu', name='conv1a_act')(x) x = Conv2D(64, (3, 3), padding='same', name='conv1b')(x) x = BatchNormalization(name='conv1b_bn')(x) x = Activation('relu', name='conv1b_act')(x) x = MaxPooling2D((2, 2), name='conv1_pool')(x) x = Conv2D(128, (3, 3), padding='same', name='conv2a')(x) x = BatchNormalization(name='conv2a_bn')(x) x = Activation('relu', name='conv2a_act')(x) x = Conv2D(128, (3, 3), padding='same', name='conv2b')(x) x = BatchNormalization(name='conv2b_bn')(x) x = Activation('relu', name='conv2b_act')(x) x = MaxPooling2D((2, 2), name='conv2_pool')(x) x = Conv2D(256, (3, 3), padding='same', name='conv3a')(x) x = BatchNormalization(name='conv3a_bn')(x) x = Activation('relu', name='conv3a_act')(x) x = Conv2D(256, (3, 3), padding='same', name='conv3b')(x) x = BatchNormalization(name='conv3b_bn')(x) x = Activation('relu', name='conv3b_act')(x) x = MaxPooling2D((2, 2), name='conv3_pool')(x) x = Conv2D(512, (3, 3), padding='same', name='conv4a')(x) x = BatchNormalization(name='conv4a_bn')(x) x = Activation('relu', name='conv4a_act')(x) x = Conv2D(512, (3, 3), padding='same', name='conv4b')(x) x = BatchNormalization(name='conv4b_bn')(x) x = Activation('relu', name='conv4b_act')(x) x = GlobalMaxPooling2D(name='avg_pool')(x) x = Dense(1024, name='dense0', activation='elu')(x) x = Dropout(0.4)(x) x = Dense(1024, name='dense1', activation='elu')(x) x = Dropout(0.4)(x) x = Dense(np.prod(out_size), activation='elu', name='skeleton')(x) x = Reshape((out_size))(x) model = Model(img_input, x) optimizer = Adam(lr=1e-3, decay=0.05) model.compile(loss='mean_absolute_error', optimizer=optimizer, metrics=['mean_absolute_error', 'mean_squared_error', 'mean_absolute_percentage_error']) model_name = 'simple_v2' return model, model_name
def extract_folds_features(argv): # Parse command line to read with which network should the script extract image information parser = argparse.ArgumentParser( description='Receives the desired network model for extraction') parser.add_argument( '--network', '-n', default="vgg16", help= 'The desired network. We only support vgg16, inceptionV3 and resnet50') parser.add_argument( '--pooling', '-p', default="GAP", help= 'Used pooling layer. We support: Global Max Poolling [GAP], Global Average Pooling[GMP], None [None]' ) parser.add_argument('--fold', '-f', default="all", help='Select which fold to extract features') args = parser.parse_args() # Check if network is suported # We now only have vgg16 if not ((args.network == "vgg16") or (args.network == "inceptionV3") or (args.network == "resnet50")): print_error( "We currently only suport the followuing models: vgg16, inceptionv3 and resnet50" ) exit(1) if not ((args.pooling == "GAP") or (args.pooling == "GMP") or (args.pooling == "None")): print_error("We currently only suport GAP, GMP and None layers") exit(1) # ------------------ Create extracting model inputs = Input(CONST_VEC_DATASET_OUTPUT_IMAGE_SHAPE) # ------------------ Convolution ------------------ if args.network == "vgg16": convolutional_layer = VGG16( weights='imagenet', include_top=False, input_shape=CONST_VEC_DATASET_OUTPUT_IMAGE_SHAPE) for layer in convolutional_layer.layers[:]: layer.trainable = False #Freezes all layers in the vgg16 output_shape = CONST_VEC_NETWORK_VGG16_OUTPUTSHAPE if args.network == "inceptionV3": convolutional_layer = InceptionV3( weights='imagenet', include_top=False, input_shape=CONST_VEC_DATASET_OUTPUT_IMAGE_SHAPE) for layer in convolutional_layer.layers[:]: layer.trainable = False #Freezes all layers in the vgg16 output_shape = CONST_VEC_NETWORK_INCEPTIONV3_OUTPUTSHAPE if args.network == "resnet50": convolutional_layer = ResNet50( weights='imagenet', include_top=False, input_shape=CONST_VEC_DATASET_OUTPUT_IMAGE_SHAPE) for layer in convolutional_layer.layers[:]: layer.trainable = False #Freezes all layers in the vgg16 output_shape = CONST_VEC_NETWORK_RESNET50_OUTPUTSHAPE convolution_output = convolutional_layer(inputs) # ------------------ Pooling ------------------ if args.pooling == "None": outputs = convolution_output elif args.pooling == "GAP": outputs = GlobalAveragePooling2D(data_format=None)(convolution_output) elif args.pooling == "GMP": outputs = GlobalMaxPooling2D(data_format=None)(convolution_output) model = Model(inputs=inputs, outputs=outputs) print_info("Extracting network summary:") model.summary() # ------------------ Extracting process telegramSendMessage("Starting extracting process with network " + args.network + " and pooling layer " + args.pooling) print_info("Starting extracting process with network " + args.network + " and pooling layer " + args.pooling) # Create folder to host all extracted models if (args.network == "vgg16"): extraction_datapath = os.path.join(CONST_STR_DATASET_BASE_PATH, CONST_STR_DATASET_DATAPATH, CONST_STR_DATASET_FOLDS_DATAPATH, CONST_STR_DATASET_FEATURES_VGG16) if (args.network == "inceptionV3"): extraction_datapath = os.path.join( CONST_STR_DATASET_BASE_PATH, CONST_STR_DATASET_DATAPATH, CONST_STR_DATASET_FOLDS_DATAPATH, CONST_STR_DATASET_FEATURES_INCEPTIONV3) if (args.network == "resnet50"): extraction_datapath = os.path.join( CONST_STR_DATASET_BASE_PATH, CONST_STR_DATASET_DATAPATH, CONST_STR_DATASET_FOLDS_DATAPATH, CONST_STR_DATASET_FEATURES_RESNET50) try: if not os.path.exists(extraction_datapath): os.makedirs(extraction_datapath) except OSError: print_error("Could not make directory for features extraction") telegramSendMessage("Error: Creating directory") exit(1) if args.fold == "all": for fold in folds: # Load fold dataset print_info("Loading dataset from " + fold["name"]) telegramSendMessage("Loading dataset from " + fold["name"]) try: input_train_data = np.load( os.path.join( CONST_STR_DATASET_BASE_PATH, CONST_STR_DATASET_DATAPATH, CONST_STR_DATASET_FOLDS_DATAPATH, "input_training_data_" + fold['name'] + ".npy")) input_test_data = np.load( os.path.join(CONST_STR_DATASET_BASE_PATH, CONST_STR_DATASET_DATAPATH, CONST_STR_DATASET_FOLDS_DATAPATH, "input_testing_data_" + fold['name'] + ".npy")) except: print_error("Could not find dataset. Did you build it?") telegramSendMessage( "Could not find dataset. Did you build it?") exit(1) # Extracting features of fold print_info("Extracting training features") telegramSendMessage("Extracting training features") train_features = [] number_of_images = input_train_data.shape[0] for index in range(number_of_images): # Expand dimention of image from (224,224,3) to (1,224,224,3) image = np.expand_dims(input_train_data[index], 0) # Pass image throught the model image_feature = model.predict(image) # Append to the train_features array train_features.append(image_feature) #Transform array into ndarray train_features = np.array(train_features).astype("float32") train_features = np.reshape(train_features, (number_of_images, output_shape[2])) #Save the extracted features print_info("Saving training features") telegramSendMessage("Saving training features") np.save( os.path.join( extraction_datapath, "input_training_data_" + args.pooling + "_" + fold["name"]), train_features) ### Repeat to test dataset print_info("Extracting testing features") telegramSendMessage("Extracting testing features") test_features = [] number_of_images = input_test_data.shape[0] for index in range(number_of_images): image = np.expand_dims(input_test_data[index], 0) image_feature = model.predict(image) test_features.append(image_feature) test_features = np.array(test_features).astype("float32") test_features = np.reshape(test_features, (number_of_images, output_shape[2])) #Save the extracted features print_info("Saving testing features") telegramSendMessage("Saving testing features") np.save( os.path.join( extraction_datapath, "input_testing_data_" + args.pooling + "_" + fold["name"]), test_features) # Forcefully delete input datas from memory del input_train_data del input_test_data del train_features del test_features gc.collect() else: fold_name = args.fold # Load fold dataset print_info("Loading dataset from " + fold_name) telegramSendMessage("Loading dataset from " + fold_name) try: input_train_data = np.load( os.path.join(CONST_STR_DATASET_BASE_PATH, CONST_STR_DATASET_DATAPATH, CONST_STR_DATASET_FOLDS_DATAPATH, "input_training_data_" + fold_name + ".npy")) input_test_data = np.load( os.path.join(CONST_STR_DATASET_BASE_PATH, CONST_STR_DATASET_DATAPATH, CONST_STR_DATASET_FOLDS_DATAPATH, "input_testing_data_" + fold_name + ".npy")) except: print_error("Could not find dataset. Did you build it?") telegramSendMessage("Could not find dataset. Did you build it?") exit(1) # Extracting features of fold print_info("Extracting training features") telegramSendMessage("Extracting training features") train_features = [] number_of_images = input_train_data.shape[0] for index in range(number_of_images): # Expand dimention of image from (224,224,3) to (1,224,224,3) image = np.expand_dims(input_train_data[index], 0) # Pass image throught the model image_feature = model.predict(image) # Append to the train_features array train_features.append(image_feature) #Transform array into ndarray train_features = np.array(train_features).astype("float32") train_features = np.reshape(train_features, (number_of_images, output_shape[2])) #Save the extracted features print_info("Saving training features") telegramSendMessage("Saving training features") np.save( os.path.join( extraction_datapath, "input_training_data_" + args.pooling + "_" + fold_name), train_features) ### Repeat to test dataset print_info("Extracting testing features") telegramSendMessage("Extracting testing features") test_features = [] number_of_images = input_test_data.shape[0] for index in range(number_of_images): image = np.expand_dims(input_test_data[index], 0) image_feature = model.predict(image) test_features.append(image_feature) test_features = np.array(test_features).astype("float32") test_features = np.reshape(test_features, (number_of_images, output_shape[2])) #Save the extracted features print_info("Saving testing features") telegramSendMessage("Saving testing features") np.save( os.path.join( extraction_datapath, "input_testing_data_" + args.pooling + "_" + fold_name), test_features) # Forcefully delete input datas from memory del input_train_data del input_test_data del train_features del test_features gc.collect() print_info("Extraction script end") telegramSendMessage("Extraction script end")
def get_test_model_full(): """Returns a maximally complex test model, using all supported layer types with different parameter combination. """ input_shapes = [ (26, 28, 3), (4, 4, 3), (4, 4, 3), (4,), (2, 3), (27, 29, 1), (17, 1), (17, 4), (2, 3), ] inputs = [Input(shape=s) for s in input_shapes] outputs = [] for inp in inputs[6:8]: for padding in ['valid', 'same']: for s in range(1, 6): for out_channels in [1, 2]: for d in range(1, 4): outputs.append( Conv1D(out_channels, s, padding=padding, dilation_rate=d)(inp)) for padding_size in range(0, 5): outputs.append(ZeroPadding1D(padding_size)(inp)) for crop_left in range(0, 2): for crop_right in range(0, 2): outputs.append(Cropping1D((crop_left, crop_right))(inp)) for upsampling_factor in range(1, 5): outputs.append(UpSampling1D(upsampling_factor)(inp)) for padding in ['valid', 'same']: for pool_factor in range(1, 6): for s in range(1, 4): outputs.append( MaxPooling1D(pool_factor, strides=s, padding=padding)(inp)) outputs.append( AveragePooling1D(pool_factor, strides=s, padding=padding)(inp)) outputs.append(GlobalMaxPooling1D()(inp)) outputs.append(GlobalAveragePooling1D()(inp)) for inp in [inputs[0], inputs[5]]: for padding in ['valid', 'same']: for h in range(1, 6): for out_channels in [1, 2]: for d in range(1, 4): outputs.append( Conv2D(out_channels, (h, 1), padding=padding, dilation_rate=(d, 1))(inp)) outputs.append( SeparableConv2D(out_channels, (h, 1), padding=padding, dilation_rate=(d, 1))(inp)) for sy in range(1, 4): outputs.append( Conv2D(out_channels, (h, 1), strides=(1, sy), padding=padding)(inp)) outputs.append( SeparableConv2D(out_channels, (h, 1), strides=(sy, sy), padding=padding)(inp)) for sy in range(1, 4): outputs.append( DepthwiseConv2D((h, 1), strides=(sy, sy), padding=padding)(inp)) outputs.append( MaxPooling2D((h, 1), strides=(1, sy), padding=padding)(inp)) for w in range(1, 6): for out_channels in [1, 2]: for d in range(1, 4) if sy == 1 else [1]: outputs.append( Conv2D(out_channels, (1, w), padding=padding, dilation_rate=(1, d))(inp)) outputs.append( SeparableConv2D(out_channels, (1, w), padding=padding, dilation_rate=(1, d))(inp)) for sx in range(1, 4): outputs.append( Conv2D(out_channels, (1, w), strides=(sx, 1), padding=padding)(inp)) outputs.append( SeparableConv2D(out_channels, (1, w), strides=(sx, sx), padding=padding)(inp)) for sx in range(1, 4): outputs.append( DepthwiseConv2D((1, w), strides=(sy, sy), padding=padding)(inp)) outputs.append( MaxPooling2D((1, w), strides=(1, sx), padding=padding)(inp)) outputs.append(ZeroPadding2D(2)(inputs[0])) outputs.append(ZeroPadding2D((2, 3))(inputs[0])) outputs.append(ZeroPadding2D(((1, 2), (3, 4)))(inputs[0])) outputs.append(Cropping2D(2)(inputs[0])) outputs.append(Cropping2D((2, 3))(inputs[0])) outputs.append(Cropping2D(((1, 2), (3, 4)))(inputs[0])) for y in range(1, 3): for x in range(1, 3): outputs.append(UpSampling2D(size=(y, x))(inputs[0])) outputs.append(GlobalAveragePooling2D()(inputs[0])) outputs.append(GlobalMaxPooling2D()(inputs[0])) outputs.append(AveragePooling2D((2, 2))(inputs[0])) outputs.append(MaxPooling2D((2, 2))(inputs[0])) outputs.append(UpSampling2D((2, 2))(inputs[0])) outputs.append(keras.layers.concatenate([inputs[0], inputs[0]])) outputs.append(Dropout(0.5)(inputs[0])) outputs.append(BatchNormalization()(inputs[0])) outputs.append(BatchNormalization(center=False)(inputs[0])) outputs.append(BatchNormalization(scale=False)(inputs[0])) outputs.append(Conv2D(2, (3, 3), use_bias=True)(inputs[0])) outputs.append(Conv2D(2, (3, 3), use_bias=False)(inputs[0])) outputs.append(SeparableConv2D(2, (3, 3), use_bias=True)(inputs[0])) outputs.append(SeparableConv2D(2, (3, 3), use_bias=False)(inputs[0])) outputs.append(DepthwiseConv2D(2, (3, 3), use_bias=True)(inputs[0])) outputs.append(DepthwiseConv2D(2, (3, 3), use_bias=False)(inputs[0])) outputs.append(Dense(2, use_bias=True)(inputs[3])) outputs.append(Dense(2, use_bias=False)(inputs[3])) shared_conv = Conv2D(1, (1, 1), padding='valid', name='shared_conv', activation='relu') up_scale_2 = UpSampling2D((2, 2)) x1 = shared_conv(up_scale_2(inputs[1])) # (1, 8, 8) x2 = shared_conv(up_scale_2(inputs[2])) # (1, 8, 8) x3 = Conv2D(1, (1, 1), padding='valid')(up_scale_2(inputs[2])) # (1, 8, 8) x = keras.layers.concatenate([x1, x2, x3]) # (3, 8, 8) outputs.append(x) x = Conv2D(3, (1, 1), padding='same', use_bias=False)(x) # (3, 8, 8) outputs.append(x) x = Dropout(0.5)(x) outputs.append(x) x = keras.layers.concatenate([ MaxPooling2D((2, 2))(x), AveragePooling2D((2, 2))(x)]) # (6, 4, 4) outputs.append(x) x = Flatten()(x) # (1, 1, 96) x = Dense(4, use_bias=False)(x) outputs.append(x) x = Dense(3)(x) # (1, 1, 3) outputs.append(x) outputs.append(Activation(relu6)(inputs[7])) outputs.append(keras.layers.Add()([inputs[4], inputs[8], inputs[8]])) outputs.append(keras.layers.Subtract()([inputs[4], inputs[8]])) outputs.append(keras.layers.Multiply()([inputs[4], inputs[8], inputs[8]])) outputs.append(keras.layers.Average()([inputs[4], inputs[8], inputs[8]])) outputs.append(keras.layers.Maximum()([inputs[4], inputs[8], inputs[8]])) outputs.append(keras.layers.Concatenate()([inputs[4], inputs[8], inputs[8]])) intermediate_input_shape = (3,) intermediate_in = Input(intermediate_input_shape) intermediate_x = intermediate_in intermediate_x = Dense(8)(intermediate_x) intermediate_x = Dense(5)(intermediate_x) intermediate_model = Model( inputs=[intermediate_in], outputs=[intermediate_x], name='intermediate_model') intermediate_model.compile(loss='mse', optimizer='nadam') x = intermediate_model(x) # (1, 1, 5) intermediate_model_2 = Sequential() intermediate_model_2.add(Dense(7, input_shape=(5,))) intermediate_model_2.add(Dense(5)) intermediate_model_2.compile(optimizer='rmsprop', loss='categorical_crossentropy') x = intermediate_model_2(x) # (1, 1, 5) x = Dense(3)(x) # (1, 1, 3) shared_activation = Activation('tanh') outputs = outputs + [ Activation('tanh')(inputs[3]), Activation('hard_sigmoid')(inputs[3]), Activation('selu')(inputs[3]), Activation('sigmoid')(inputs[3]), Activation('softplus')(inputs[3]), Activation('softmax')(inputs[3]), Activation('relu')(inputs[3]), LeakyReLU()(inputs[3]), ELU()(inputs[3]), PReLU()(inputs[2]), PReLU()(inputs[3]), PReLU()(inputs[4]), shared_activation(inputs[3]), inputs[4], inputs[1], x, shared_activation(x), ] print('Model has {} outputs.'.format(len(outputs))) model = Model(inputs=inputs, outputs=outputs, name='test_model_full') model.compile(loss='mse', optimizer='nadam') # fit to dummy data training_data_size = 1 batch_size = 1 epochs = 10 data_in = generate_input_data(training_data_size, input_shapes) data_out = generate_output_data(training_data_size, outputs) model.fit(data_in, data_out, epochs=epochs, batch_size=batch_size) return model
def ResNet152(include_top=True, weights=None, input_tensor=None, input_shape=None, large_input=False, pooling=None, classes=1000): """Instantiate the ResNet152 architecture. Keyword arguments: include_top -- whether to include the fully-connected layer at the top of the network. (default True) weights -- one of `None` (random initialization) or "imagenet" (pre-training on ImageNet). (default None) input_tensor -- optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model.(default None) 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 197. E.g. `(200, 200, 3)` would be one valid value. (default None) large_input -- if True, then the input shape expected will be `(448, 448, 3)` (with `channels_last` data format) or `(3, 448, 448)` (with `channels_first` data format). (default False) 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. (default None) classes -- optional number of classes to classify image into, only to be specified if `include_top` is True, and if no `weights` argument is specified. (default 1000) Returns: A Keras model instance. Raises: ValueError: in case of invalid argument for `weights`, or invalid input shape. """ if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') 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') eps = 1.1e-5 if large_input: img_size = 448 else: img_size = 224 # Determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=img_size, min_size=197, data_format=K.image_data_format(), require_flatten=include_top) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor # handle dimension ordering for different backends if K.image_dim_ordering() == 'tf': bn_axis = 3 else: bn_axis = 1 x = ZeroPadding2D((3, 3), name='conv1_zeropadding')(img_input) x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1', use_bias=False)(x) x = BatchNormalization(epsilon=eps, axis=bn_axis, name='bn_conv1')(x) x = Scale(axis=bn_axis, name='scale_conv1')(x) x = Activation('relu', name='conv1_relu')(x) x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1')(x) x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1)) x = identity_block(x, 3, [64, 64, 256], stage=2, block='b') x = identity_block(x, 3, [64, 64, 256], stage=2, block='c') x = conv_block(x, 3, [128, 128, 512], stage=3, block='a') for i in range(1, 8): x = identity_block(x, 3, [128, 128, 512], stage=3, block='b' + str(i)) x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a') for i in range(1, 36): x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b' + str(i)) x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c') if large_input: x = AveragePooling2D((14, 14), name='avg_pool')(x) else: x = AveragePooling2D((7, 7), name='avg_pool')(x) # include classification layer by default, not included for feature extraction if include_top: x = Flatten()(x) x = Dense(classes, activation='softmax', name='fc1000')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x, name='resnet152') return model
def share_stream(x_shape, outputNum=256): input = Input(x_shape) x_a = input conv1 = Conv2D(filters=32, kernel_size=(5, 5), strides=(1, 1), padding='same', use_bias=use_bias, trainable=Trainable)(x_a) conv1 = Activation('relu')(conv1) conv1 = Conv2D(filters=32, kernel_size=(1, 1), strides=(1, 1), padding='same', use_bias=use_bias, trainable=Trainable)(conv1) x_a = Conv2D(filters=32, kernel_size=(1, 1), strides=(1, 1), padding='same', use_bias=use_bias, trainable=Trainable)(x_a) x_a = add([x_a, conv1]) attention = Conv2D(filters=32, kernel_size=(5, 5), activation='sigmoid', strides=(1, 1), padding='same', use_bias=use_bias, trainable=Trainable)(x_a) x_a = multiply([x_a, attention]) x_a = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='valid')(x_a) conv1 = Conv2D(filters=64, kernel_size=(3, 3), strides=(1, 1), padding='same', use_bias=use_bias, trainable=Trainable)(x_a) conv1 = Activation('relu')(conv1) conv1 = Conv2D(filters=64, kernel_size=(1, 1), strides=(1, 1), padding='same', use_bias=use_bias, trainable=Trainable)(conv1) x_a = Conv2D(filters=64, kernel_size=(1, 1), strides=(1, 1), padding='same', use_bias=use_bias, trainable=Trainable)(x_a) x_a = add([x_a, conv1]) x_a = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='valid')(x_a) x = Conv2D(filters=128, kernel_size=(3, 3), strides=(1, 1), padding='same', use_bias=use_bias, trainable=Trainable)(x_a) temp = Multiply()([ Conv2D(filters=128, kernel_size=(3, 3), padding='same', trainable=Trainable)(Lambda(lambda x: x * 10000000.)( Activation("tanh")(Lambda(lambda x: x / 10000000.)(x)))), x ]) for jj in range(2, taylor): temp = Add()([ Multiply()([ Conv2D(filters=128, kernel_size=(3, 3), padding='same', trainable=Trainable)(Lambda(lambda x: x * 10000000.)( Activation("tanh")( Lambda(lambda x: x / 10000000.)(x)))), Lambda(lambda x: (x**jj) / math.factorial(jj))(x) ]), temp ]) temp = Add()([ Conv2D(filters=128, kernel_size=(3, 3), padding='same', trainable=Trainable)(Lambda(lambda x: x * 10000000.)( Activation("tanh")(Lambda(lambda x: x / 10000000.)(x)))), temp ]) conv2 = Lambda(lambda x: x * 10000000.)(Activation("tanh")( Lambda(lambda x: x / 10000000.)(temp))) conv2 = Conv2D(filters=128, kernel_size=(1, 1), strides=(1, 1), padding='same', use_bias=use_bias, trainable=Trainable)(conv2) x_a = Conv2D(filters=128, kernel_size=(1, 1), strides=(1, 1), padding='same', use_bias=use_bias, trainable=Trainable)(x_a) x_a = add([x_a, conv2]) x_a = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='valid')(x_a) conv3 = Conv2D(filters=outputNum, kernel_size=(3, 3), strides=(1, 1), padding='same', use_bias=use_bias, trainable=Trainable)(x_a) conv3 = Activation('relu')(conv3) conv3 = Conv2D(filters=outputNum, kernel_size=(1, 1), strides=(1, 1), padding='same', use_bias=use_bias, trainable=Trainable)(conv3) x_a = Conv2D(filters=outputNum, kernel_size=(1, 1), strides=(1, 1), padding='same', use_bias=use_bias, trainable=Trainable)(x_a) x_a = add([x_a, conv3]) x_a = GlobalMaxPooling2D()(x_a) shared_layer = Model(input, x_a) return shared_layer
def SEInceptionV3(include_top=True, weights=None, input_tensor=None, input_shape=None, pooling=None, classes=1000): """Instantiates the Squeeze and Excite Inception v3 architecture. # Arguments include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization) or "imagenet" (pre-training on ImageNet). 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 `(299, 299, 3)` (with `channels_last` data format) or `(3, 299, 299)` (with `channels_first` data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 139. E.g. `(150, 150, 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 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. # Returns A Keras model instance. # Raises ValueError: in case of invalid argument for `weights`, or invalid input shape. """ if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') 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=299, min_size=139, data_format=K.image_data_format(), require_flatten=include_top) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor if K.image_data_format() == 'channels_first': channel_axis = 1 else: channel_axis = 3 x = _conv2d_bn(img_input, 32, 3, 3, strides=(2, 2), padding='valid') x = _conv2d_bn(x, 32, 3, 3, padding='valid') x = _conv2d_bn(x, 64, 3, 3) x = MaxPooling2D((3, 3), strides=(2, 2))(x) x = _conv2d_bn(x, 80, 1, 1, padding='valid') x = _conv2d_bn(x, 192, 3, 3, padding='valid') x = MaxPooling2D((3, 3), strides=(2, 2))(x) # mixed 0, 1, 2: 35 x 35 x 256 branch1x1 = _conv2d_bn(x, 64, 1, 1) branch5x5 = _conv2d_bn(x, 48, 1, 1) branch5x5 = _conv2d_bn(branch5x5, 64, 5, 5) branch3x3dbl = _conv2d_bn(x, 64, 1, 1) branch3x3dbl = _conv2d_bn(branch3x3dbl, 96, 3, 3) branch3x3dbl = _conv2d_bn(branch3x3dbl, 96, 3, 3) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = _conv2d_bn(branch_pool, 32, 1, 1) x = layers.concatenate([branch1x1, branch5x5, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed0') # squeeze and excite block x = squeeze_excite_block(x) # mixed 1: 35 x 35 x 256 branch1x1 = _conv2d_bn(x, 64, 1, 1) branch5x5 = _conv2d_bn(x, 48, 1, 1) branch5x5 = _conv2d_bn(branch5x5, 64, 5, 5) branch3x3dbl = _conv2d_bn(x, 64, 1, 1) branch3x3dbl = _conv2d_bn(branch3x3dbl, 96, 3, 3) branch3x3dbl = _conv2d_bn(branch3x3dbl, 96, 3, 3) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = _conv2d_bn(branch_pool, 64, 1, 1) x = layers.concatenate([branch1x1, branch5x5, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed1') # squeeze and excite block x = squeeze_excite_block(x) # mixed 2: 35 x 35 x 256 branch1x1 = _conv2d_bn(x, 64, 1, 1) branch5x5 = _conv2d_bn(x, 48, 1, 1) branch5x5 = _conv2d_bn(branch5x5, 64, 5, 5) branch3x3dbl = _conv2d_bn(x, 64, 1, 1) branch3x3dbl = _conv2d_bn(branch3x3dbl, 96, 3, 3) branch3x3dbl = _conv2d_bn(branch3x3dbl, 96, 3, 3) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = _conv2d_bn(branch_pool, 64, 1, 1) x = layers.concatenate([branch1x1, branch5x5, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed2') # squeeze and excite block x = squeeze_excite_block(x) # mixed 3: 17 x 17 x 768 branch3x3 = _conv2d_bn(x, 384, 3, 3, strides=(2, 2), padding='valid') branch3x3dbl = _conv2d_bn(x, 64, 1, 1) branch3x3dbl = _conv2d_bn(branch3x3dbl, 96, 3, 3) branch3x3dbl = _conv2d_bn(branch3x3dbl, 96, 3, 3, strides=(2, 2), padding='valid') branch_pool = MaxPooling2D((3, 3), strides=(2, 2))(x) x = layers.concatenate([branch3x3, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed3') # squeeze and excite block x = squeeze_excite_block(x) # mixed 4: 17 x 17 x 768 branch1x1 = _conv2d_bn(x, 192, 1, 1) branch7x7 = _conv2d_bn(x, 128, 1, 1) branch7x7 = _conv2d_bn(branch7x7, 128, 1, 7) branch7x7 = _conv2d_bn(branch7x7, 192, 7, 1) branch7x7dbl = _conv2d_bn(x, 128, 1, 1) branch7x7dbl = _conv2d_bn(branch7x7dbl, 128, 7, 1) branch7x7dbl = _conv2d_bn(branch7x7dbl, 128, 1, 7) branch7x7dbl = _conv2d_bn(branch7x7dbl, 128, 7, 1) branch7x7dbl = _conv2d_bn(branch7x7dbl, 192, 1, 7) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = _conv2d_bn(branch_pool, 192, 1, 1) x = layers.concatenate([branch1x1, branch7x7, branch7x7dbl, branch_pool], axis=channel_axis, name='mixed4') # squeeze and excite block x = squeeze_excite_block(x) # mixed 5, 6: 17 x 17 x 768 for i in range(2): branch1x1 = _conv2d_bn(x, 192, 1, 1) branch7x7 = _conv2d_bn(x, 160, 1, 1) branch7x7 = _conv2d_bn(branch7x7, 160, 1, 7) branch7x7 = _conv2d_bn(branch7x7, 192, 7, 1) branch7x7dbl = _conv2d_bn(x, 160, 1, 1) branch7x7dbl = _conv2d_bn(branch7x7dbl, 160, 7, 1) branch7x7dbl = _conv2d_bn(branch7x7dbl, 160, 1, 7) branch7x7dbl = _conv2d_bn(branch7x7dbl, 160, 7, 1) branch7x7dbl = _conv2d_bn(branch7x7dbl, 192, 1, 7) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = _conv2d_bn(branch_pool, 192, 1, 1) x = layers.concatenate( [branch1x1, branch7x7, branch7x7dbl, branch_pool], axis=channel_axis, name='mixed' + str(5 + i)) # squeeze and excite block x = squeeze_excite_block(x) # mixed 7: 17 x 17 x 768 branch1x1 = _conv2d_bn(x, 192, 1, 1) branch7x7 = _conv2d_bn(x, 192, 1, 1) branch7x7 = _conv2d_bn(branch7x7, 192, 1, 7) branch7x7 = _conv2d_bn(branch7x7, 192, 7, 1) branch7x7dbl = _conv2d_bn(x, 192, 1, 1) branch7x7dbl = _conv2d_bn(branch7x7dbl, 192, 7, 1) branch7x7dbl = _conv2d_bn(branch7x7dbl, 192, 1, 7) branch7x7dbl = _conv2d_bn(branch7x7dbl, 192, 7, 1) branch7x7dbl = _conv2d_bn(branch7x7dbl, 192, 1, 7) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = _conv2d_bn(branch_pool, 192, 1, 1) x = layers.concatenate([branch1x1, branch7x7, branch7x7dbl, branch_pool], axis=channel_axis, name='mixed7') # squeeze and excite block x = squeeze_excite_block(x) # mixed 8: 8 x 8 x 1280 branch3x3 = _conv2d_bn(x, 192, 1, 1) branch3x3 = _conv2d_bn(branch3x3, 320, 3, 3, strides=(2, 2), padding='valid') branch7x7x3 = _conv2d_bn(x, 192, 1, 1) branch7x7x3 = _conv2d_bn(branch7x7x3, 192, 1, 7) branch7x7x3 = _conv2d_bn(branch7x7x3, 192, 7, 1) branch7x7x3 = _conv2d_bn(branch7x7x3, 192, 3, 3, strides=(2, 2), padding='valid') branch_pool = MaxPooling2D((3, 3), strides=(2, 2))(x) x = layers.concatenate([branch3x3, branch7x7x3, branch_pool], axis=channel_axis, name='mixed8') # squeeze and excite block x = squeeze_excite_block(x) # mixed 9: 8 x 8 x 2048 for i in range(2): branch1x1 = _conv2d_bn(x, 320, 1, 1) branch3x3 = _conv2d_bn(x, 384, 1, 1) branch3x3_1 = _conv2d_bn(branch3x3, 384, 1, 3) branch3x3_2 = _conv2d_bn(branch3x3, 384, 3, 1) branch3x3 = layers.concatenate([branch3x3_1, branch3x3_2], axis=channel_axis, name='mixed9_' + str(i)) branch3x3dbl = _conv2d_bn(x, 448, 1, 1) branch3x3dbl = _conv2d_bn(branch3x3dbl, 384, 3, 3) branch3x3dbl_1 = _conv2d_bn(branch3x3dbl, 384, 1, 3) branch3x3dbl_2 = _conv2d_bn(branch3x3dbl, 384, 3, 1) branch3x3dbl = layers.concatenate([branch3x3dbl_1, branch3x3dbl_2], axis=channel_axis) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = _conv2d_bn(branch_pool, 192, 1, 1) x = layers.concatenate( [branch1x1, branch3x3, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed' + str(9 + i)) # squeeze and excite block x = squeeze_excite_block(x) if include_top: # Classification block x = GlobalAveragePooling2D(name='avg_pool')(x) x = Dense(classes, activation='softmax', name='predictions')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x, name='inception_v3') return model
def _create_se_resnet(classes, img_input, include_top, initial_conv_filters, filters, depth, width, bottleneck, weight_decay, pooling): '''Creates a SE ResNet model with specified parameters Args: initial_conv_filters: number of features for the initial convolution include_top: Flag to include the last dense layer filters: number of filters per block, defined as a list. filters = [64, 128, 256, 512 depth: number or layers in the each block, defined as a list. ResNet-50 = [3, 4, 6, 3] ResNet-101 = [3, 6, 23, 3] ResNet-152 = [3, 8, 36, 3] width: width multiplier for network (for Wide ResNet) bottleneck: adds a bottleneck conv to reduce computation weight_decay: weight_decay (l2 norm) 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. Returns: a Keras Model ''' channel_axis = 1 if K.image_data_format() == 'channels_first' else -1 N = list(depth) # branches branches = [] for i in range(len(img_input)): # block 1 (initial conv block) branch = Conv2D(initial_conv_filters, (7, 7), padding='same', use_bias=False, strides=(2, 2), kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(img_input[i]) branch = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(branch) # block 2 (projection block) for i in range(N[0]): if bottleneck: branch = _resnet_bottleneck_block(branch, filters[0], width) else: branch = _resnet_block(branch, filters[0], width) branches.append(branch) x = concatenate(branches) ''' # block 1 (initial conv block) x = Conv2D(initial_conv_filters, (7, 7), padding='same', use_bias=False, strides=(2, 2), kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(img_input) x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x) # block 2 (projection block) for i in range(N[0]): if bottleneck: x = _resnet_bottleneck_block(x, filters[0], width) else: x = _resnet_block(x, filters[0], width) ''' # block 3 - N for k in range(1, len(N)): if bottleneck: x = _resnet_bottleneck_block(x, filters[k], width, strides=(2, 2)) else: x = _resnet_block(x, filters[k], width, strides=(2, 2)) for i in range(N[k] - 1): if bottleneck: x = _resnet_bottleneck_block(x, filters[k], width) else: x = _resnet_block(x, filters[k], width) x = BatchNormalization(axis=channel_axis)(x) x = Activation('relu')(x) if include_top: x = GlobalAveragePooling2D()(x) x = Dense(classes, use_bias=False, kernel_regularizer=l2(weight_decay), activation='sigmoid', name='output')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) return x
def pyramid_feat2_large(): #DIDN'T CONVERNGED out_size = (49, 2) roi_size = 128 rand_seed = 1337 np.random.seed(rand_seed) input_shape = (roi_size, roi_size, 1) img_input = Input(shape=input_shape) block1 = Conv2D(32, (3, 3), padding='same', name='conv0a')(img_input) block1 = Activation('relu', name='conv0a_act')(block1) block1 = Conv2D(32, (3, 3), padding='same', name='conv0b')(block1) block1 = Activation('relu', name='conv0b_act')(block1) block2 = MaxPooling2D((2, 2), name='conv0_pool')(block1) block2 = Conv2D(64, (3, 3), padding='same', name='conv1a')(block2) block2 = BatchNormalization(name='conv1a_bn')(block2) block2 = Activation('relu', name='conv1a_act')(block2) block2 = Conv2D(64, (3, 3), padding='same', name='conv1b')(block2) block2 = BatchNormalization(name='conv1b_bn')(block2) block2 = Activation('relu', name='conv1b_act')(block2) block3 = MaxPooling2D((2, 2), name='conv1_pool')(block2) block3 = Conv2D(128, (3, 3), padding='same', name='conv2a')(block3) block3 = BatchNormalization(name='conv2a_bn')(block3) block3 = Activation('relu', name='conv2a_act')(block3) block3 = Conv2D(128, (3, 3), padding='same', name='conv2b')(block3) block3 = BatchNormalization(name='conv2b_bn')(block3) block3 = Activation('relu', name='conv2b_act')(block3) block4 = MaxPooling2D((2, 2), name='conv2_pool')(block3) block4 = Conv2D(256, (3, 3), padding='same', name='conv3a')(block4) block4 = BatchNormalization(name='conv3a_bn')(block4) block4 = Activation('relu', name='conv3a_act')(block4) block4 = Conv2D(256, (3, 3), padding='same', name='conv3b')(block4) block4 = BatchNormalization(name='conv3b_bn')(block4) block4 = Activation('relu', name='conv3b_act')(block4) block5 = MaxPooling2D((2, 2), name='conv3_pool')(block4) block5 = Conv2D(512, (3, 3), padding='same', name='conv4a')(block5) block5 = BatchNormalization(name='conv4a_bn')(block5) block5 = Activation('relu', name='conv4a_act')(block5) block5 = Conv2D(512, (3, 3), padding='same', name='conv4b')(block5) block5 = BatchNormalization(name='conv4b_bn')(block5) block5 = Activation('relu', name='conv4b_act')(block5) feat_top = GlobalMaxPooling2D(name='avg_pool')(block5) feat_top = Dense(1024, name='dense0', activation='elu')(feat_top) feat_top = Dropout(0.4)(feat_top) down_block5 = Conv2D(1, (1, 1), padding='same')(block5) down_block5 = UpSampling2D((2,2))(down_block5) lat_block4 = Conv2D(1, (1, 1), padding='same')(block4) p_block4 = layers.add([lat_block4, down_block5]) down_block4 = UpSampling2D((2,2))(p_block4) lat_block3 = Conv2D(1, (1, 1), padding='same')(block3) p_block3 = layers.add([lat_block3, down_block4]) down_block3 = UpSampling2D((2,2))(p_block3) lat_block2 = Conv2D(1, (1, 1), padding='same')(block2) p_block2 = layers.add([lat_block2, down_block3]) feat_bot = Flatten()(p_block2) feat_bot = Dense(1024, activation='elu')(feat_bot) feat_bot = Dropout(0.4)(feat_bot) all_feats = layers.add([feat_top, feat_bot]) all_feats = Dense(1024, activation='elu')(all_feats) feat_bot = Dropout(0.4)(feat_bot) skel_out = Dense(out_size[0]*out_size[1], activation='elu', name='skeleton')(all_feats) skel_out = Reshape((out_size))(skel_out) model = Model(img_input, skel_out) #optimizer = Adam(lr=1e-2, decay=0.1) optimizer = Adam(lr=1e-3, decay=0.1) model.compile(loss='mean_absolute_error', optimizer=optimizer, metrics=['mean_absolute_error', 'mean_squared_error', 'mean_absolute_percentage_error']) return model, 'pyramid_feat2_large'
def __create_dense_net( nb_classes, img_input, include_top, depth=40, nb_dense_block=3, growth_rate=12, nb_filter=-1, nb_layers_per_block=-1, bottleneck=False, reduction=0.0, dropout_rate=None, weight_decay=1e-4, subsample_initial_block=False, pooling=None, activation="softmax", transition_pooling="avg", ): """ Build the DenseNet model # Arguments nb_classes: number of classes img_input: tuple of shape (channels, rows, columns) or (rows, columns, channels) include_top: flag to include the final Dense layer depth: number or layers nb_dense_block: number of dense blocks to add to end (generally = 3) growth_rate: number of filters to add per dense block nb_filter: initial number of filters. Default -1 indicates initial number of filters is 2 * growth_rate nb_layers_per_block: number of layers in each dense block. Can be a -1, positive integer or a list. If -1, calculates nb_layer_per_block from the depth of the network. If positive integer, a set number of layers per dense block. If list, nb_layer is used as provided. Note that list size must be (nb_dense_block + 1) bottleneck: add bottleneck blocks reduction: reduction factor of transition blocks. Note : reduction value is inverted to compute compression dropout_rate: dropout rate weight_decay: weight decay rate subsample_initial_block: Changes model type to suit different datasets. Should be set to True for ImageNet, and False for CIFAR datasets. When set to True, the initial convolution will be strided and adds a MaxPooling2D before the initial dense block. 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. activation: Type of activation at the top layer. Can be one of 'softmax' or 'sigmoid'. Note that if sigmoid is used, classes must be 1. transition_pooling: `avg` for avg pooling (default), `max` for max pooling, None for no pooling during scale transition blocks. Please note that this default differs from the DenseNetFCN paper in accordance with the DenseNet paper. # Returns a keras tensor # Raises ValueError: in case of invalid argument for `reduction` or `nb_dense_block` """ with K.name_scope("DenseNet"): concat_axis = 1 if K.image_data_format() == "channels_first" else -1 if reduction != 0.0: if not (reduction <= 1.0 and reduction > 0.0): raise ValueError( "`reduction` value must lie between 0.0 and 1.0") # layers in each dense block if type(nb_layers_per_block) is list or type( nb_layers_per_block) is tuple: nb_layers = list(nb_layers_per_block) # Convert tuple to list if len(nb_layers) != nb_dense_block: raise ValueError( "If `nb_dense_block` is a list, its length must match " "the number of layers provided by `nb_layers`.") final_nb_layer = nb_layers[-1] nb_layers = nb_layers[:-1] else: if nb_layers_per_block == -1: assert (depth - 4) % 3 == 0, ("Depth must be 3 N + 4 " "if nb_layers_per_block == -1") count = int((depth - 4) / 3) if bottleneck: count = count // 2 nb_layers = [count for _ in range(nb_dense_block)] final_nb_layer = count else: final_nb_layer = nb_layers_per_block nb_layers = [nb_layers_per_block] * nb_dense_block # compute initial nb_filter if -1, else accept users initial nb_filter if nb_filter <= 0: nb_filter = 2 * growth_rate # compute compression factor compression = 1.0 - reduction # Initial convolution if subsample_initial_block: initial_kernel = (7, 7) initial_strides = (2, 2) else: initial_kernel = (3, 3) initial_strides = (1, 1) x = Conv2D( nb_filter, initial_kernel, kernel_initializer="he_normal", padding="same", name="initial_conv2D", strides=initial_strides, use_bias=False, kernel_regularizer=l2(weight_decay), )(img_input) if subsample_initial_block: x = BatchNormalization(axis=concat_axis, epsilon=1.1e-5, name="initial_bn")(x) x = Activation("relu")(x) x = MaxPooling2D((3, 3), strides=(2, 2), padding="same")(x) # Add dense blocks for block_idx in range(nb_dense_block - 1): x, nb_filter = __dense_block( x, nb_layers[block_idx], nb_filter, growth_rate, bottleneck=bottleneck, dropout_rate=dropout_rate, weight_decay=weight_decay, block_prefix="dense_%i" % block_idx, ) # add transition_block x = __transition_block( x, nb_filter, compression=compression, weight_decay=weight_decay, block_prefix="tr_%i" % block_idx, transition_pooling=transition_pooling, ) nb_filter = int(nb_filter * compression) # The last dense_block does not have a transition_block x, nb_filter = __dense_block( x, final_nb_layer, nb_filter, growth_rate, bottleneck=bottleneck, dropout_rate=dropout_rate, weight_decay=weight_decay, block_prefix="dense_%i" % (nb_dense_block - 1), ) x = BatchNormalization(axis=concat_axis, epsilon=1.1e-5, name="final_bn")(x) x = Activation("relu")(x) if include_top: if pooling == "avg": x = GlobalAveragePooling2D()(x) elif pooling == "max": x = GlobalMaxPooling2D()(x) x = Dense(nb_classes, activation=activation)(x) else: if pooling == "avg": x = GlobalAveragePooling2D()(x) elif pooling == "max": x = GlobalMaxPooling2D()(x) return x
def VGG_CNN_F(arch_mode='full', weights='imagenet', input_shape=(224, 224, 3), pooling=None, classes=1000): """ This function constructs VGG_CNN_F and is based on https://github.com/keras-team/keras/blob/master/keras/applications/vgg16.py Arguments: arch_mode: {'full', 'rmlast', 'notop'} """ 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 arch_mode == 'full' and classes != 1000: raise ValueError('If using `weights` as imagenet with `arch_mode`' ' as `full`, `classes` should be 1000') img_input = Input(shape=input_shape) x = Conv2D(64, (11, 11), strides=(4, 4), activation='relu', name='conv1')(img_input) # conv1 x = LRN(n=5, alpha=0.0005, beta=0.75, k=2, name='norm1')(x) # norm1 x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same', name='pool1')(x) # pool1 x = ZeroPadding2D(padding=(2, 2))(x) x = Conv2D(256, (5, 5), strides=(1, 1), activation='relu', name='conv2')(x) # conv2 x = LRN(n=5, alpha=0.0005, beta=0.75, k=2, name='norm2')(x) # norm2 x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool2')(x) # pool2 x = ZeroPadding2D(padding=(1, 1))(x) x = Conv2D(256, (3, 3), strides=(1, 1), activation='relu', name='conv3')(x) # conv3 x = ZeroPadding2D(padding=(1, 1))(x) x = Conv2D(256, (3, 3), strides=(1, 1), activation='relu', name='conv4')(x) # conv4 x = ZeroPadding2D(padding=(1, 1))(x) x = Conv2D(256, (3, 3), strides=(1, 1), activation='relu', name='conv5')(x) # conv5 x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool5')(x) # pool5 if arch_mode == 'notop': if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) else: x = Flatten()(x) x = Dense(4096, activation='relu', name='fc6')(x) # fc6 x = Dropout(0.5)(x) # drop6 x = Dense(4096, activation='relu', name='fc7')(x) # fc7 x = Dropout(0.5)(x) # drop7 if arch_mode == 'full': x = Dense(classes, name='fc8')(x) # fc8 x = Activation('softmax', name='prob')(x) # prob elif arch_mode != 'rmlast': raise ValueError( 'arch_mode: {} is not supported.'.format(arch_mode)) inputs = img_input # Create model. model = Model(inputs, x, name='vgg_cnn_f') # load weights if weights == 'imagenet': if arch_mode == 'full': weights_path = 'init_weights/vgg_cnn_f.h5' elif arch_mode == 'rmlast': weights_path = 'init_weights/vgg_cnn_f_rmlast.h5' elif arch_mode == 'notop': weights_path = 'init_weights/vgg_cnn_f_notop.h5' model.load_weights(weights_path) if K.backend() == 'theano': raise NotImplemented('Support for loading imagenet weights with ' 'theano backend is not implemented yet!') if K.image_data_format() == 'channels_first': raise NotImplemented( 'Support for loading imagenet weights with ' 'channels_first image data format is not implemented yet!') elif weights is not None: model.load_weights(weights) return model
def SENET50(include_top=True, weights='vggface', input_tensor=None, input_shape=None, pooling=None, classes=8631): input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=197, data_format=K.image_data_format(), require_flatten=include_top, weights=weights) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor if K.image_data_format() == 'channels_last': bn_axis = 3 else: bn_axis = 1 x = Conv2D( 64, (7, 7), use_bias=False, strides=(2, 2), padding='same', name='conv1/7x7_s2')(img_input) x = BatchNormalization(axis=bn_axis, name='conv1/7x7_s2/bn')(x) x = Activation('relu')(x) x = MaxPooling2D((3, 3), strides=(2, 2))(x) x = senet_conv_block(x, 3, [64, 64, 256], stage=2, block=1, strides=(1, 1)) x = senet_identity_block(x, 3, [64, 64, 256], stage=2, block=2) x = senet_identity_block(x, 3, [64, 64, 256], stage=2, block=3) x = senet_conv_block(x, 3, [128, 128, 512], stage=3, block=1) x = senet_identity_block(x, 3, [128, 128, 512], stage=3, block=2) x = senet_identity_block(x, 3, [128, 128, 512], stage=3, block=3) x = senet_identity_block(x, 3, [128, 128, 512], stage=3, block=4) x = senet_conv_block(x, 3, [256, 256, 1024], stage=4, block=1) x = senet_identity_block(x, 3, [256, 256, 1024], stage=4, block=2) x = senet_identity_block(x, 3, [256, 256, 1024], stage=4, block=3) x = senet_identity_block(x, 3, [256, 256, 1024], stage=4, block=4) x = senet_identity_block(x, 3, [256, 256, 1024], stage=4, block=5) x = senet_identity_block(x, 3, [256, 256, 1024], stage=4, block=6) x = senet_conv_block(x, 3, [512, 512, 2048], stage=5, block=1) x = senet_identity_block(x, 3, [512, 512, 2048], stage=5, block=2) x = senet_identity_block(x, 3, [512, 512, 2048], stage=5, block=3) x = AveragePooling2D((7, 7), name='avg_pool')(x) if include_top: x = Flatten()(x) x = Dense(classes, activation='softmax', name='classifier')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x, name='vggface_senet50') # load weights if weights == 'vggface': if include_top: weights_path = get_file('rcmalli_vggface_tf_senet50.h5', utils.SENET50_WEIGHTS_PATH, cache_subdir=utils.VGGFACE_DIR) else: weights_path = get_file('rcmalli_vggface_tf_notop_senet50.h5', utils.SENET50_WEIGHTS_PATH_NO_TOP, cache_subdir=utils.VGGFACE_DIR) model.load_weights(weights_path) if K.backend() == 'theano': layer_utils.convert_all_kernels_in_model(model) if include_top: maxpool = model.get_layer(name='avg_pool') shape = maxpool.output_shape[1:] dense = model.get_layer(name='classifier') layer_utils.convert_dense_weights_data_format(dense, shape, 'channels_first') if K.image_data_format() == 'channels_first' and K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') elif weights is not None: model.load_weights(weights) return model
def test_keras_2_image_bias(self): #define Keras model and get prediction input_shape1 = (100, 60, 3) input_shape2 = (23, 45, 3) data1 = Input(shape=input_shape1) data2 = Input(shape=input_shape2) a_pool = GlobalMaxPooling2D()(data1) b_pool = GlobalMaxPooling2D()(data2) output = keras.layers.add([a_pool, b_pool]) model = Model(inputs=[data1, data2], outputs=output) data1 = np.ones(input_shape1) data2 = np.ones(input_shape2) keras_input1 = np.ones(input_shape1) keras_input2 = np.ones(input_shape2) data1[:, :, 0] = 100.0 data1[:, :, 1] = 79.0 data1[:, :, 2] = 194.0 data2[:, :, 0] = 130.0 data2[:, :, 1] = 91.0 data2[:, :, 2] = 11.0 red_bias1 = -88.0 green_bias1 = -2 blue_bias1 = -40 red_bias2 = -100.0 green_bias2 = -29 blue_bias2 = -15 keras_input1[:, :, 0] = data1[:, :, 2] + blue_bias1 keras_input1[:, :, 1] = data1[:, :, 1] + green_bias1 keras_input1[:, :, 2] = data1[:, :, 0] + red_bias1 keras_input2[:, :, 0] = data2[:, :, 0] + red_bias2 keras_input2[:, :, 1] = data2[:, :, 1] + green_bias2 keras_input2[:, :, 2] = data2[:, :, 2] + blue_bias2 keras_preds = model.predict([ np.expand_dims(keras_input1, axis=0), np.expand_dims(keras_input2, axis=0) ]) keras_preds = keras_preds.flatten() #convert to coreml and get predictions model_dir = tempfile.mkdtemp() model_path = os.path.join(model_dir, 'keras.mlmodel') from coremltools.converters import keras as keras_converter coreml_model = keras_converter.convert( model, input_names=['data1', 'data2'], output_names=['output'], image_input_names=['data1', 'data2'], red_bias={ 'data1': red_bias1, 'data2': red_bias2 }, green_bias={ 'data1': green_bias1, 'data2': green_bias2 }, blue_bias={ 'data1': blue_bias1, 'data2': blue_bias2 }, is_bgr={ 'data1': True, 'data2': False }) #coreml_model.save(model_path) #coreml_model = coremltools.models.MLModel(model_path) coreml_input_dict = dict() coreml_input_dict["data1"] = PIL.Image.fromarray(data1.astype( np.uint8)) coreml_input_dict["data2"] = PIL.Image.fromarray(data2.astype( np.uint8)) coreml_preds = coreml_model.predict( coreml_input_dict)['output'].flatten() #compare self.assertEquals(len(keras_preds), len(coreml_preds)) max_relative_error = compare_models(keras_preds, coreml_preds) self.assertAlmostEquals(max(max_relative_error, .001), .001, delta=1e-6) if os.path.exists(model_dir): shutil.rmtree(model_dir)
def VGG16(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000): """Instantiates the VGG16 architecture. Optionally loads weights pre-trained on ImageNet. Note that when using TensorFlow, for best performance you should set `image_data_format='channels_last'` in your Keras config at ~/.keras/keras.json. The model and the weights are compatible with both TensorFlow and Theano. The data format convention used by the model is the one specified in your Keras config file. # Arguments include_top: whether to include the 3 fully-connected layers 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 input channels, and width and height should be no smaller than 48. 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 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. # 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=48, data_format=K.image_data_format(), require_flatten=include_top, weights=weights) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor # Block 1 x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input) x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) # Block 2 x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x) x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) # Block 3 x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) # Block 4 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x) # Block 5 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x) if include_top: # Classification block x = Flatten(name='flatten')(x) x = Dense(4096, activation='relu', name='fc1')(x) x = Dense(4096, activation='relu', name='fc2')(x) x = Dense(classes, activation='softmax', name='predictions')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x, name='vgg16') # load weights if weights == 'imagenet': if include_top: weights_path = WEIGHTS_PATH else: weights_path = WEIGHTS_PATH_NO_TOP model.load_weights(weights_path) if K.backend() == 'theano': layer_utils.convert_all_kernels_in_model(model) if K.image_data_format() == 'channels_first': if include_top: maxpool = model.get_layer(name='block5_pool') shape = maxpool.output_shape[1:] dense = model.get_layer(name='fc1') layer_utils.convert_dense_weights_data_format(dense, shape, 'channels_first') if K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') elif weights is not None: model.load_weights(weights) return model
def build_model_up(input_shape, n_label): input_img = Input(shape=input_shape) # segmentation network seg_conv1 = conv(input_img, 32, 5, 'seg_conv1_01') seg_conv1 = conv(seg_conv1, 32, 5, 'seg_conv1_02') # 500 seg_maxp1 = MaxPooling2D(pool_size=(2, 2))(seg_conv1) # 250 seg_conv2 = conv(seg_maxp1, 64, 5, 'seg_conv2_01') # 250 seg_conv2 = conv(seg_conv2, 64, 5, 'seg_conv2_02') seg_conv2 = conv(seg_conv2, 64, 5, 'seg_conv2_03') # 250 seg_maxp2 = MaxPooling2D(pool_size=(2, 2))(seg_conv2) # 125 seg_conv3 = conv(seg_maxp2, 64, 5, 'seg_conv3_01') # 125 seg_conv3 = conv(seg_conv3, 64, 5, 'seg_conv3_02') seg_conv3 = conv(seg_conv3, 64, 5, 'seg_conv3_03') seg_conv3 = conv(seg_conv3, 64, 5, 'seg_conv3_04') # 125 seg_maxp3 = MaxPooling2D(pool_size=(2, 2))(seg_conv3) # 62 seg_conv4 = conv(seg_maxp3, 1024, 5, 'seg_conv4_01') # 62 seg_mask = Conv2D(n_label, 1, strides=(1, 1), padding='same', kernel_initializer='he_normal', activation='softmax', name='seg_mask')(seg_conv4) seg_upsp5 = UpSampling2D()(seg_conv4) # 124 seg_upsp5 = ZeroPadding2D(padding=((0, 1), (0, 1)))(seg_upsp5) # 125 seg_upsp5 = concatenate([seg_upsp5, seg_conv3], axis=3) seg_conv5 = conv(seg_upsp5, 64, 5, 'seg_conv5_01') #seg_conv5 = conv(seg_upsp5, 64, 5, 'seg_conv5_02') #seg_conv5 = conv(seg_upsp5, 64, 5, 'seg_conv5_03') #seg_conv5 = conv(seg_upsp5, 64, 5, 'seg_conv5_04') # 125 seg_upsp6 = UpSampling2D()(seg_conv5) # 256 seg_upsp6 = concatenate([seg_upsp6, seg_conv2], axis=3) seg_conv6 = conv(seg_upsp6, 64, 5, 'seg_conv6_01') #seg_conv6 = conv(seg_upsp6, 64, 5, 'seg_conv6_02') #seg_conv6 = conv(seg_upsp6, 64, 5, 'seg_conv6_03') # 250 seg_upsp7 = UpSampling2D()(seg_conv6) # 512 seg_upsp7 = concatenate([seg_upsp7, seg_conv1], axis=3) seg_conv7 = conv(seg_upsp7, 32, 5, 'seg_conv7_01') #seg_conv7 = conv(seg_upsp7, 32, 5, 'seg_conv7_02') # 500 seg_output = Conv2D(n_label, 1, strides=(1, 1), padding='same', kernel_initializer='he_normal', activation='softmax', name='seg_output')(seg_conv7) # decision network dec_conv1 = concatenate([seg_conv4, seg_mask], axis=3) dec_conv1 = MaxPooling2D(pool_size=(2, 2))(dec_conv1) dec_conv2 = conv(dec_conv1, 16, 5, 'dec_conv2_01') dec_conv2 = MaxPooling2D(pool_size=(2, 2))(dec_conv2) dec_conv3 = conv(dec_conv2, 32, 5, 'dec_conv3_01') dec_conv3 = MaxPooling2D(pool_size=(2, 2))(dec_conv3) dec_conv4 = conv(dec_conv3, 64, 5, 'dec_conv4_01') # classification class1 = GlobalMaxPooling2D()(dec_conv4) class2 = GlobalAveragePooling2D()(dec_conv4) class3 = GlobalAveragePooling2D()(seg_mask) class4 = GlobalMaxPooling2D()(seg_mask) classes = concatenate([class1, class2, class3, class4], axis=1) class_output = Dense(n_label, activation='softmax', name='class_output')(classes) model = Model(inputs=input_img, outputs=[seg_output, class_output]) return model
def ResNet50(include_top=True, weights='imagenet', input_tensor=None, input_shape=(224, 224, 3), pooling="max", classes=2): if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') 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=197, data_format=K.image_data_format(), require_flatten=include_top) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor if K.image_data_format() == 'channels_last': bn_axis = 3 else: bn_axis = 1 x = ZeroPadding2D((3, 3))(img_input) x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(x) x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x) x = Activation('relu')(x) x = MaxPooling2D((3, 3), strides=(2, 2))(x) x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1)) x = identity_block(x, 3, [64, 64, 256], stage=2, block='b') x = identity_block(x, 3, [64, 64, 256], stage=2, block='c') x = conv_block(x, 3, [128, 128, 512], stage=3, block='a') x = identity_block(x, 3, [128, 128, 512], stage=3, block='b') x = identity_block(x, 3, [128, 128, 512], stage=3, block='c') x = identity_block(x, 3, [128, 128, 512], stage=3, block='d') x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f') x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c') x = AveragePooling2D((7, 7), name='avg_pool')(x) if include_top: x = Flatten()(x) x = Dense(classes, activation='softmax', name='fc1000')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x, name='resnet50') # load weights if weights == 'imagenet': if include_top: weights_path = get_file( 'resnet50_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH, cache_subdir='models', md5_hash='a7b3fe01876f51b976af0dea6bc144eb') else: weights_path = get_file( 'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models', md5_hash='a268eb855778b3df3c7506639542a6af') model.load_weights(weights_path) if K.image_data_format() == 'channels_first': if include_top: maxpool = model.get_layer(name='avg_pool') shape = maxpool.output_shape[1:] dense = model.get_layer(name='fc1000') layer_utils.convert_dense_weights_data_format( dense, shape, 'channels_first') if K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') return model
def simple_resnet_1113( input_shape=(75, 75, 3), KernelSize=(5, 5), Momentum=0.99): X_input = Input(input_shape) #input_CNN = ZeroPadding2D((0, 0))(X_input) input_CNN = BatchNormalization(momentum=Momentum)(X_input) ## Input Layer input_CNN = Conv2D(32, kernel_size=KernelSize, padding='same', name='c11')(input_CNN) input_CNN = BatchNormalization(momentum=Momentum, name='b11')(input_CNN) input_CNN = Activation('elu')(input_CNN) input_CNN = MaxPooling2D((2, 2), strides=(2, 2), name='m11')(input_CNN) #input_CNN = Dropout(0.25)(input_CNN) input_CNN = Conv2D(64, kernel_size=KernelSize, padding='same', name='c12')(input_CNN) input_CNN = BatchNormalization(momentum=Momentum, name='b12')(input_CNN) input_CNN = Activation('elu')(input_CNN) input_CNN = MaxPooling2D((2, 2), strides=(2, 2), name='m12')(input_CNN) #input_CNN = Dropout(0.25)(input_CNN) ## First Residual input_CNN_residual = BatchNormalization(momentum=Momentum)(input_CNN) input_CNN_residual = Conv2D(128, kernel_size=KernelSize, padding='same')(input_CNN_residual) input_CNN_residual = BatchNormalization( momentum=Momentum)(input_CNN_residual) input_CNN_residual = Activation('elu')(input_CNN_residual) input_CNN_residual = Dropout(0.25)(input_CNN_residual) input_CNN_residual = Conv2D(64, kernel_size=KernelSize, padding='same')(input_CNN_residual) input_CNN_residual = BatchNormalization( momentum=Momentum)(input_CNN_residual) input_CNN_residual = Activation('elu')(input_CNN_residual) input_CNN_residual = Dropout(0.25)(input_CNN_residual) input_CNN_residual = Add()([input_CNN_residual, input_CNN]) ## Top CNN top_CNN = Conv2D(128, kernel_size=KernelSize, padding='same')(input_CNN_residual) top_CNN = BatchNormalization(momentum=Momentum)(top_CNN) top_CNN = Activation('elu')(top_CNN) top_CNN = MaxPooling2D((2, 2), strides=(2, 2))(top_CNN) top_CNN = Conv2D(256, kernel_size=KernelSize, padding='same')(top_CNN) top_CNN = BatchNormalization(momentum=Momentum)(top_CNN) top_CNN = Activation('elu')(top_CNN) top_CNN = Dropout(0.25)(top_CNN) top_CNN = MaxPooling2D((2, 2), strides=(2, 2))(top_CNN) top_CNN = Conv2D(512, kernel_size=KernelSize, padding='same')(top_CNN) top_CNN = BatchNormalization(momentum=Momentum)(top_CNN) top_CNN = Activation('elu')(top_CNN) top_CNN = Dropout(0.25)(top_CNN) top_CNN = MaxPooling2D((2, 2), strides=(2, 2))(top_CNN) top_CNN = GlobalMaxPooling2D()(top_CNN) #Dense Layers #X = Flatten()(top_CNN) X = Dense(512)(top_CNN) X = BatchNormalization(momentum=Momentum)(X) X = Activation('elu')(X) X = Dropout(0.25)(X) X = Dense(256)(X) X = BatchNormalization(momentum=Momentum)(X) X = Activation('elu')(X) X = Dropout(0.25)(X) X = Dense(1, activation='sigmoid')(X) model = Model(inputs=X_input, outputs=X, name='simple_resnet') return model
def NASNet(input_shape=None, penultimate_filters=4032, nb_blocks=6, stem_filters=96, skip_reduction=True, use_auxiliary_branch=False, filters_multiplier=2, dropout=0.5, weight_decay=5e-5, include_top=True, weights=None, input_tensor=None, pooling=None, classes=1000, default_size=None): """Instantiates a NASNet architecture. Note that only TensorFlow is supported for now, therefore it only works with the data format `image_data_format='channels_last'` in your Keras config at `~/.keras/keras.json`. # Arguments input_shape: optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(331, 331, 3)` for NASNetLarge or `(224, 224, 3)` for NASNetMobile It should have exactly 3 inputs 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 nb_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_filters: number of filters in the initial stem block skip_reduction: Whether to skip the reduction step at the tail end of the network. Set to `False` for CIFAR models. use_auxiliary_branch: Whether to use the auxiliary branch during training or evaluation. filters_multiplier: controls the width of the network. - If `filters_multiplier` < 1.0, proportionally decreases the number of filters in each layer. - If `filters_multiplier` > 1.0, proportionally increases the number of filters in each layer. - If `filters_multiplier` = 1, default number of filters from the paper are used at each layer. dropout: dropout rate weight_decay: l2 regularization weight 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 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. default_size: specifies the default image size of the model # Returns A Keras model instance. # Raises ValueError: in case of invalid argument for `weights`, or invalid input shape. RuntimeError: If attempting to run this model with a backend that does not support separable convolutions. """ if K.backend() != 'tensorflow': raise RuntimeError('Only Tensorflow backend is currently supported, ' 'as other backends do not support ' 'separable convolution.') if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') 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 default_size is None: default_size = 331 # Determine proper input shape and default size. input_shape = _obtain_input_shape(input_shape, default_size=default_size, min_size=32, data_format=K.image_data_format(), require_flatten=include_top, weights=weights) if K.image_data_format() != 'channels_last': warnings.warn('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.') K.set_image_data_format('channels_last') old_data_format = 'channels_first' else: old_data_format = None if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor assert penultimate_filters % 24 == 0, "`penultimate_filters` needs to be divisible " \ "by 24." channel_dim = 1 if K.image_data_format() == 'channels_first' else -1 filters = penultimate_filters // 24 if not skip_reduction: x = Conv2D(stem_filters, (3, 3), strides=(2, 2), padding='valid', use_bias=False, name='stem_conv1', kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(img_input) else: x = Conv2D(stem_filters, (3, 3), strides=(1, 1), padding='same', use_bias=False, name='stem_conv1', kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(img_input) x = BatchNormalization(axis=channel_dim, momentum=_BN_DECAY, epsilon=_BN_EPSILON, name='stem_bn1')(x) p = None if not skip_reduction: # imagenet / mobile mode x, p = _reduction_A(x, p, filters // (filters_multiplier ** 2), weight_decay, id='stem_1') x, p = _reduction_A(x, p, filters // filters_multiplier, weight_decay, id='stem_2') for i in range(nb_blocks): x, p = _normal_A(x, p, filters, weight_decay, id='%d' % (i)) x, p0 = _reduction_A(x, p, filters * filters_multiplier, weight_decay, id='reduce_%d' % (nb_blocks)) p = p0 if not skip_reduction else p for i in range(nb_blocks): x, p = _normal_A(x, p, filters * filters_multiplier, weight_decay, id='%d' % (nb_blocks + i + 1)) auxiliary_x = None if not skip_reduction: # imagenet / mobile mode if use_auxiliary_branch: auxiliary_x = _add_auxiliary_head(x, classes, weight_decay) x, p0 = _reduction_A(x, p, filters * filters_multiplier ** 2, weight_decay, id='reduce_%d' % (2 * nb_blocks)) if skip_reduction: # CIFAR mode if use_auxiliary_branch: auxiliary_x = _add_auxiliary_head(x, classes, weight_decay) p = p0 if not skip_reduction else p for i in range(nb_blocks): x, p = _normal_A(x, p, filters * filters_multiplier ** 2, weight_decay, id='%d' % (2 * nb_blocks + i + 1)) x = Activation('relu')(x) if include_top: x = GlobalAveragePooling2D()(x) x = Dropout(dropout)(x) x = Dense(classes, activation='softmax', kernel_regularizer=l2(weight_decay), name='predictions')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. if use_auxiliary_branch: model = Model(inputs, [x, auxiliary_x], name='NASNet_with_auxiliary') else: model = Model(inputs, x, name='NASNet') # load weights if weights == 'imagenet': if default_size == 224: # mobile version if include_top: if use_auxiliary_branch: weight_path = NASNET_MOBILE_WEIGHT_PATH_WITH_AUXULARY model_name = 'nasnet_mobile_with_aux.h5' else: weight_path = NASNET_MOBILE_WEIGHT_PATH model_name = 'nasnet_mobile.h5' else: if use_auxiliary_branch: weight_path = NASNET_MOBILE_WEIGHT_PATH_WITH_AUXULARY_NO_TOP model_name = 'nasnet_mobile_with_aux_no_top.h5' else: weight_path = NASNET_MOBILE_WEIGHT_PATH_NO_TOP model_name = 'nasnet_mobile_no_top.h5' weights_file = get_file(model_name, weight_path, cache_subdir='models') model.load_weights(weights_file, by_name=True) elif default_size == 331: # large version if include_top: if use_auxiliary_branch: weight_path = NASNET_LARGE_WEIGHT_PATH_WITH_auxiliary model_name = 'nasnet_large_with_aux.h5' else: weight_path = NASNET_LARGE_WEIGHT_PATH model_name = 'nasnet_large.h5' else: if use_auxiliary_branch: weight_path = NASNET_LARGE_WEIGHT_PATH_WITH_auxiliary_NO_TOP model_name = 'nasnet_large_with_aux_no_top.h5' else: weight_path = NASNET_LARGE_WEIGHT_PATH_NO_TOP model_name = 'nasnet_large_no_top.h5' weights_file = get_file(model_name, weight_path, cache_subdir='models') model.load_weights(weights_file, by_name=True) else: raise ValueError('ImageNet weights can only be loaded on NASNetLarge or NASNetMobile') if old_data_format: K.set_image_data_format(old_data_format) return model
def ResNet50(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000): """Instantiates the ResNet50 architecture. Optionally loads weights pre-trained on ImageNet. Note that when using TensorFlow, for best performance you should set `image_data_format="channels_last"` in your Keras config at ~/.keras/keras.json. The model and the weights are compatible with both TensorFlow and Theano. The data format convention used by the model is the one specified in your Keras config file. # Arguments include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization) or "imagenet" (pre-training on ImageNet). 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, 244)` (with `channels_first` data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 197. 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 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. # Returns A Keras model instance. # Raises ValueError: in case of invalid argument for `weights`, or invalid input shape. """ if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') 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=197, data_format=K.image_data_format(), require_flatten=include_top) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor if K.image_data_format() == 'channels_last': bn_axis = 3 else: bn_axis = 1 x = ZeroPadding2D((3, 3))(img_input) x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(x) x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x) x = Activation('relu')(x) x = MaxPooling2D((3, 3), strides=(2, 2))(x) x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1)) x = identity_block(x, 3, [64, 64, 256], stage=2, block='b') x = identity_block(x, 3, [64, 64, 256], stage=2, block='c') x = conv_block(x, 3, [128, 128, 512], stage=3, block='a') x = identity_block(x, 3, [128, 128, 512], stage=3, block='b') x = identity_block(x, 3, [128, 128, 512], stage=3, block='c') x = identity_block(x, 3, [128, 128, 512], stage=3, block='d') x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f') x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c') x = AveragePooling2D((7, 7), name='avg_pool')(x) if include_top: x = Flatten()(x) x = Dense(classes, activation='softmax', name='fc1000')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x, name='resnet50') # load weights if weights == 'imagenet': if include_top: weights_path = get_file( 'resnet50_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH, cache_subdir='models', md5_hash='a7b3fe01876f51b976af0dea6bc144eb') else: weights_path = get_file( 'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models', md5_hash='a268eb855778b3df3c7506639542a6af') model.load_weights(weights_path) if K.backend() == 'theano': layer_utils.convert_all_kernels_in_model(model) if K.image_data_format() == 'channels_first': if include_top: maxpool = model.get_layer(name='avg_pool') shape = maxpool.output_shape[1:] dense = model.get_layer(name='fc1000') layer_utils.convert_dense_weights_data_format( dense, shape, 'channels_first') if K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') return model
def SqueezeNet(include_top=True, weights="imagenet", input_tensor=None, input_shape=None, pooling=None, classes=1000): if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') 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=48, data_format=K.image_data_format(), require_flatten=include_top) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor x = Convolution2D(64, kernel_size=(3, 3), strides=(2, 2), padding="same", activation="relu", name='conv1')(img_input) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='maxpool1', padding="valid")(x) x = _fire(x, (16, 64, 64), name="fire2") x = _fire(x, (16, 64, 64), name="fire3") x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='maxpool3', padding="valid")(x) x = _fire(x, (32, 128, 128), name="fire4") x = _fire(x, (32, 128, 128), name="fire5") x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='maxpool5', padding="valid")(x) x = _fire(x, (48, 192, 192), name="fire6") x = _fire(x, (48, 192, 192), name="fire7") x = _fire(x, (64, 256, 256), name="fire8") x = _fire(x, (64, 256, 256), name="fire9") if include_top: x = Dropout(0.5, name='dropout9')(x) x = Convolution2D(classes, (1, 1), padding='valid', name='conv10')(x) x = AveragePooling2D(pool_size=(13, 13), name='avgpool10')(x) x = Flatten(name='flatten10')(x) x = Activation("softmax", name='softmax')(x) else: if pooling == "avg": x = GlobalAveragePooling2D(name="avgpool10")(x) else: x = GlobalMaxPooling2D(name="maxpool10")(x) model = Model(img_input, x, name="squeezenet") if weights == 'imagenet': weights_path = get_file('squeezenet_weights.h5', WEIGHTS_PATH, cache_subdir='models') model.load_weights(weights_path) return model
def build_model(lr, l2, activation='sigmoid'): ############## # BRANCH MODEL ############## regul = regularizers.l2(l2) optim = Adam(lr=lr) kwargs = {'padding': 'same', 'kernel_regularizer': regul} inp = Input(shape=img_shape) # 384x384x1 x = Conv2D(64, (9, 9), strides=2, activation='relu', **kwargs)(inp) x = MaxPooling2D((2, 2), strides=(2, 2), name='maxpool1')(x) # 96x96x64 for i in range(2): x = BatchNormalization(name=f'bn{i+1}')(x) x = Conv2D(64, (3, 3), activation='relu', **kwargs)(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='layer1.maxpool')(x) # 48x48x64 x = BatchNormalization()(x) x = Conv2D(128, (1, 1), activation='relu', **kwargs)(x) # 48x48x128 for _ in range(4): x = subblock(x, 64, **kwargs) x = MaxPooling2D((2, 2), strides=(2, 2), name='layer2.maxpool')(x) # 24x24x128 x = BatchNormalization()(x) x = Conv2D(256, (1, 1), activation='relu', **kwargs)(x) # 24x24x256 for _ in range(4): x = subblock(x, 64, **kwargs) x = MaxPooling2D((2, 2), strides=(2, 2), name='layer3.maxpool')(x) # 12x12x256 x = BatchNormalization()(x) x = Conv2D(384, (1, 1), activation='relu', **kwargs)(x) # 12x12x384 for _ in range(4): x = subblock(x, 96, **kwargs) x = MaxPooling2D((2, 2), strides=(2, 2), name='layer4.maxpool')(x) # 6x6x384 x = BatchNormalization()(x) x = Conv2D(512, (1, 1), activation='relu', **kwargs)(x) # 6x6x512 for _ in range(4): x = subblock(x, 128, **kwargs) x = GlobalMaxPooling2D()(x) # 512 branch_model = Model(inp, x) ############ # HEAD MODEL ############ mid = 32 xa_inp = Input(shape=branch_model.output_shape[1:]) xb_inp = Input(shape=branch_model.output_shape[1:]) x1 = Lambda(lambda x: x[0] * x[1])([xa_inp, xb_inp]) x2 = Lambda(lambda x: x[0] + x[1])([xa_inp, xb_inp]) x3 = Lambda(lambda x: K.abs(x[0] - x[1]))([xa_inp, xb_inp]) x4 = Lambda(lambda x: K.square(x))(x3) x = Concatenate()([x1, x2, x3, x4]) x = Reshape((4, branch_model.output_shape[1], 1), name='reshape1')(x) # Per feature NN with shared weight is implemented using CONV2D with appropriate stride. x = Conv2D(mid, (4, 1), activation='relu', padding='valid')(x) x = Reshape((branch_model.output_shape[1], mid, 1))(x) x = Conv2D(1, (1, mid), activation='linear', padding='valid')(x) x = Flatten(name='flatten')(x) # Weighted sum implemented as a Dense layer. x = Dense(1, use_bias=True, activation=activation, name='weighted-average')(x) head_model = Model([xa_inp, xb_inp], x, name='head') ######################## # SIAMESE NEURAL NETWORK ######################## # Complete model is constructed by calling the branch model on each input image, # and then the head model on the resulting 512-vectors. img_a = Input(shape=img_shape) img_b = Input(shape=img_shape) xa = branch_model(img_a) xb = branch_model(img_b) x = head_model([xa, xb]) model = Model([img_a, img_b], x) model.compile(optim, loss='binary_crossentropy', metrics=['binary_crossentropy', 'acc']) return model, branch_model, head_model
def ResNet152(include_top=True, weights=None, input_tensor=None, input_shape=None, large_input=False, pooling=None, classes=1000): """Instantiate the ResNet152 architecture. Keyword arguments: include_top -- whether to include the fully-connected layer at the top of the network. (default True) weights -- one of `None` (random initialization) or "imagenet" (pre-training on ImageNet). (default None) input_tensor -- optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model.(default None) 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 197. E.g. `(200, 200, 3)` would be one valid value. (default None) large_input -- if True, then the input shape expected will be `(448, 448, 3)` (with `channels_last` data format) or `(3, 448, 448)` (with `channels_first` data format). (default False) 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. (default None) classes -- optional number of classes to classify image into, only to be specified if `include_top` is True, and if no `weights` argument is specified. (default 1000) 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') eps = 1.1e-5 if large_input: img_size = 448 else: img_size = 224 # Determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=img_size, min_size=197, data_format=K.image_data_format(), require_flatten=include_top, weights=weights) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor # handle dimension ordering for different backends if K.image_dim_ordering() == 'tf': bn_axis = 3 else: bn_axis = 1 x = ZeroPadding2D((3, 3), name='conv1_zeropadding')(img_input) x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1', use_bias=False)(x) x = BatchNormalization(epsilon=eps, axis=bn_axis, name='bn_conv1')(x) x = Scale(axis=bn_axis, name='scale_conv1')(x) x = Activation('relu', name='conv1_relu')(x) x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1')(x) x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1)) x = identity_block(x, 3, [64, 64, 256], stage=2, block='b') x = identity_block(x, 3, [64, 64, 256], stage=2, block='c') x = conv_block(x, 3, [128, 128, 512], stage=3, block='a') for i in range(1, 8): x = identity_block(x, 3, [128, 128, 512], stage=3, block='b' + str(i)) x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a') for i in range(1, 36): x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b' + str(i)) x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c') if large_input: x = AveragePooling2D((14, 14), name='avg_pool')(x) else: x = AveragePooling2D((7, 7), name='avg_pool')(x) # include classification layer by default, not included for feature extraction if include_top: x = Flatten()(x) x = Dense(classes, activation='softmax', name='fc1000')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x, name='resnet152') # load weights if weights == 'imagenet': if include_top: weights_path = get_file('resnet152_weights_tf.h5', WEIGHTS_PATH, cache_subdir='models', md5_hash='cdb18a2158b88e392c0905d47dcef965') else: weights_path = get_file('resnet152_weights_tf_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models', md5_hash='4a90dcdafacbd17d772af1fb44fc2660') model.load_weights(weights_path, by_name=True) if K.backend() == 'theano': layer_utils.convert_all_kernels_in_model(model) if include_top: maxpool = model.get_layer(name='avg_pool') shape = maxpool.output_shape[1:] dense = model.get_layer(name='fc1000') layer_utils.convert_dense_weights_data_format(dense, shape, 'channels_first') if K.image_data_format() == 'channels_first' and K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') elif weights is not None: model.load_weights(weights) return model
def SqueezeNet(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000): """Instantiates the SqueezeNet architecture. """ if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') 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') input_shape = _obtain_input_shape(input_shape, default_size=227, min_size=48, data_format=K.image_data_format(), require_flatten=include_top) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor x = Convolution2D(64, (3, 3), strides=(2, 2), padding='valid', name='conv1')(img_input) x = Activation('relu', name='relu_conv1')(x) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool1')(x) x = fire_module(x, fire_id=2, squeeze=16, expand=64) x = fire_module(x, fire_id=3, squeeze=16, expand=64) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool3')(x) x = fire_module(x, fire_id=4, squeeze=32, expand=128) x = fire_module(x, fire_id=5, squeeze=32, expand=128) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool5')(x) x = fire_module(x, fire_id=6, squeeze=48, expand=192) x = fire_module(x, fire_id=7, squeeze=48, expand=192) x = fire_module(x, fire_id=8, squeeze=64, expand=256) x = fire_module(x, fire_id=9, squeeze=64, expand=256) if include_top: # It's not obvious where to cut the network... # Could do the 8th or 9th layer... some work recommends cutting earlier layers. x = Dropout(0.5, name='drop9')(x) x = Convolution2D(classes, (1, 1), padding='valid', name='conv10')(x) x = Activation('relu', name='relu_conv10')(x) x = GlobalAveragePooling2D()(x) x = Activation('softmax', name='loss')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling=='max': x = GlobalMaxPooling2D()(x) elif pooling==None: pass else: raise ValueError("Unknown argument for 'pooling'=" + pooling) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input model = Model(inputs, x, name='squeezenet') # load weights if weights == 'imagenet': if include_top: weights_path = get_file('squeezenet_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH, cache_subdir='models') else: weights_path = get_file('squeezenet_weights_tf_dim_ordering_tf_kernels_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models') model.load_weights(weights_path, by_name=True) if K.backend() == 'theano': layer_utils.convert_all_kernels_in_model(model) if K.image_data_format() == 'channels_first': if K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') return model