def build_model_resnet50(lock_base_model: bool): base_model = ResNet50(input_shape=INPUT_SHAPE, include_top=False, pooling=None) if lock_base_model: for layer in base_model.layers: layer.trainable = False x = AveragePooling2D((5, 5), name='avg_pool5', strides=1)(base_model.layers[-2].output) x = GlobalMaxPooling2D()(x) res = Dense(NB_CLASSES, activation='sigmoid', name='classes', kernel_initializer='zero', kernel_regularizer=l1(1e-5))(x) model = Model(inputs=base_model.inputs, outputs=res) return model
def __init__(self, include_top=True, classes=1000, pooling=None): self.include_top = include_top self.pooling = pooling self.block1_list = self._make_block(block_index=1) self.block2_list = self._make_block(block_index=2) self.block3_list = self._make_block(block_index=3) self.block4_list = self._make_block(block_index=4) self.block5_list = self._make_block(block_index=5) self.flatten = Flatten(name='flatten') self.layer14 = Dense(4096, activation='relu', name='fc1') self.layer15 = Dense(4096, activation='relu', name='fc2') self.layer16 = Dense(classes, activation='softmax', name='predictions') self.GAP = GlobalAveragePooling2D() self.GMP = GlobalMaxPooling2D()
def __init__(self, include_top=True, classes=1000, pooling=None): if K.image_data_format() == 'channels_last': bn_axis = 3 else: bn_axis = 1 self.include_top = include_top self.pooling = pooling self.conv1 = Conv2D(64, (7, 7), strides=(2, 2), padding='same', name='conv1') self.bn1 = BatchNormalization(axis=bn_axis, name='bn_conv1') self.relu = Activation('relu') self.maxpool = MaxPooling2D((3, 3), strides=(2, 2)) self.layer1_list = [ conv_block(3, [64, 64, 256], stage=2, block='a', strides=(1, 1)), identity_block(3, [64, 64, 256], stage=2, block='b'), identity_block(3, [64, 64, 256], stage=2, block='c'), ] self.layer2_list = [ conv_block(3, [128, 128, 512], stage=3, block='a'), identity_block(3, [128, 128, 512], stage=3, block='b'), identity_block(3, [128, 128, 512], stage=3, block='c'), identity_block(3, [128, 128, 512], stage=3, block='d'), ] self.layer3_list = [ conv_block(3, [256, 256, 1024], stage=4, block='a'), identity_block(3, [256, 256, 1024], stage=4, block='b'), identity_block(3, [256, 256, 1024], stage=4, block='c'), identity_block(3, [256, 256, 1024], stage=4, block='d'), identity_block(3, [256, 256, 1024], stage=4, block='e'), identity_block(3, [256, 256, 1024], stage=4, block='f'), ] self.layer4_list = [ conv_block(3, [512, 512, 2048], stage=5, block='a'), identity_block(3, [512, 512, 2048], stage=5, block='b'), identity_block(3, [512, 512, 2048], stage=5, block='c'), ] self.avgpool = AveragePooling2D((7, 7), name='avg_pool') self.flatten = Flatten() self.fc = Dense(classes, activation='softmax', name='fc1000') self.GAP = GlobalAveragePooling2D() self.GMP = GlobalMaxPooling2D()
def init_model(self, input_shape, num_classes, **kwargs): # FIXME: keras sequential model is better than keras functional api, # why??? inputs = Input(shape=input_shape) # dropout0 = SpatialDropout2D(rate=0.1, data_format='channels_last')(inputs) min_size = min(input_shape[:2]) pool_l = None for i in range(5): if i == 0: conv_l = Conv2D(64, 3, input_shape=input_shape, padding='same', data_format='channels_last')(inputs) else: conv_l = Conv2D(64, 3, padding='same')(pool_l) activation_l = Activation('relu')(conv_l) bn_l = BatchNormalization()(activation_l) pool_l = MaxPooling2D(pool_size=(2, 2))(bn_l) min_size //= 2 if min_size < 2: break avgpool_l = GlobalAveragePooling2D(data_format='channels_last')(pool_l) maxpool_l = GlobalMaxPooling2D(data_format='channels_last')(pool_l) concat = Concatenate()([avgpool_l, maxpool_l]) flatten = Flatten()(concat) bn1 = BatchNormalization()(flatten) dense1 = Dense(256, activation='relu')(bn1) bn2 = BatchNormalization()(dense1) dropout1 = Dropout(rate=0.5)(bn2) outputs = Dense(num_classes, activation='softmax')(dropout1) model = TFModel(inputs=inputs, outputs=outputs) # optimizer = tf.keras.optimizers.Adadelta() optimizer = tf.keras.optimizers.Adam() # optimizer = optimizers.SGD(lr=1e-3, decay=2e-4, momentum=0.9, clipvalue=5) model.compile(loss='sparse_categorical_crossentropy', optimizer=optimizer, metrics=['accuracy']) model.summary() self.is_init = True self._model = model
def get_model(): efficient_net_layers = tf.keras.applications.EfficientNetB0(weights=None, include_top=False, input_shape=(128, 128, 3)) for layer in efficient_net_layers.layers: layer.trainable = True model = tf.keras.Sequential() model.add(efficient_net_layers) model.add(GlobalMaxPooling2D()) model.add(Dense(256, use_bias=False)) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Dropout(DROPOUT_DENSE_LAYER)) model.add(Dense(classes_to_predict, activation="softmax")) model.summary() return model
def discriminator(num_filters=64): x_in = Input(shape=(HR_SIZE, HR_SIZE, 3)) x = Lambda(normalize_m11)(x_in) x = discriminator_block(x, num_filters, batchnorm=False) x = discriminator_block(x, num_filters, strides=2) x = discriminator_block(x, num_filters * 2) x = discriminator_block(x, num_filters * 2, strides=2) x = discriminator_block(x, num_filters * 4) x = discriminator_block(x, num_filters * 4, strides=2) x = discriminator_block(x, num_filters * 8) x = discriminator_block(x, num_filters * 8, strides=2) x = GlobalMaxPooling2D()(x) x = Dense(1024)(x) x = LeakyReLU(alpha=0.2)(x) x = Dense(1, activation='sigmoid')(x) return Model(x_in, x)
def __init__(self, num_classes, dense_layers=None, input_shape=(224, 224, 3), top_method='flatten', mobilenet_params=None): if dense_layers is None: dense_layers = [] if mobilenet_params is None: mobilenet_params = {} base_model = self._get_base_model(input_shape, **mobilenet_params) bottleneck = base_model.output x = { 'flatten': Flatten(), 'avg': GlobalAveragePooling2D(), 'max': GlobalMaxPooling2D() }[top_method](bottleneck) x = Dropout(settings.MODEL_CONFIG['drop_rate'])(x) for units in dense_layers: x = Dense( units, activation='relu', kernel_initializer=tf.keras.initializers.VarianceScaling())(x) x = Dropout(settings.MODEL_CONFIG['drop_rate'])(x) predictions = Dense( num_classes, activation='softmax', kernel_initializer=tf.keras.initializers.VarianceScaling(), use_bias=False, kernel_regularizer=tf.keras.regularizers.l2( settings.MODEL_CONFIG['l2_reg']))(x) self.base_model = base_model self.model = Model(base_model.input, predictions)
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 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 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 train(train_data_dir, validation_data_dir, model_path): # Pre-Trained CNN Model using imagenet dataset for pre-trained weights # base_model = Xception(input_shape=(img_width, img_height, 3), weights='imagenet', include_top=False) if K.image_data_format() == 'channels_first': input_shape = (3, img_width, img_height) else: input_shape = (img_width, img_height, 3) if os.path.isfile(MODEL): model = load_model(MODEL) else: base_model = InceptionV3(input_shape=input_shape, weights='imagenet', include_top=False) x = base_model.output x = GlobalMaxPooling2D()(x) x = Dense(128, activation='relu', name='fc1')(x) x = Dropout(0.2)(x) predictions = Dense(nb_classes, activation='softmax', name='predictions')(x) # add your top layer block to your base model model = Model(base_model.input, predictions) #print(model.summary()) # first: train only the top layers (which were randomly initialized) # i.e. freeze all layers of the based model that is already pre-trained. for layer in base_model.layers: layer.trainable = False model.compile(optimizer='nadam', loss='categorical_crossentropy', # categorical_crossentropy if multi-class classifier metrics=['accuracy']) # Read Data and Augment it: Make sure to select augmentations that are appropriate to your images. # To save augmentations un-comment save lines and add to your flow parameters. train_datagen = ImageDataGenerator(rescale=1. / 255, rotation_range=transformation_ratio, shear_range=transformation_ratio, zoom_range=transformation_ratio, cval=transformation_ratio, horizontal_flip=True, vertical_flip=True) validation_datagen = ImageDataGenerator(rescale=1. / 255) # os.makedirs(os.path.join(os.path.abspath(train_data_dir), 'preview'), exist_ok=True) train_generator = train_datagen.flow_from_directory(train_data_dir, target_size=[img_width, img_height], batch_size=batch_size, class_mode='categorical') # save_to_dir=os.path.join(os.path.abspath(train_data_dir), '../preview') # save_prefix='aug', # save_format='jpeg') # use the above 3 commented lines if you want to save and look at how the data augmentations look like validation_generator = validation_datagen.flow_from_directory(validation_data_dir, target_size=[img_width, img_height], batch_size=batch_size, class_mode='categorical') # save weights of best training epoch: monitor either val_loss or val_acc #top_weights_path = os.path.join(os.path.abspath(model_path), 'top_model_weights.h5') callbacks_list = [ ModelCheckpoint(MODEL, monitor='val_acc', verbose=1, save_best_only=True), EarlyStopping(monitor='val_acc', patience=5, verbose=0), TensorBoard(log_dir='tensorboard/inception-v3-train-top-layer', histogram_freq=0, write_graph=False, write_images=False) ] # Train Simple CNN model.fit_generator(train_generator, steps_per_epoch=nb_train_samples // batch_size, epochs=nb_epoch / 5, validation_data=validation_generator, validation_steps=nb_validation_samples // batch_size, callbacks=callbacks_list)
def xception(include_top=True, weights="imagenet", input_tensor=None, input_shape=None, pooling=None, classes=1000): input_shape = _obtain_input_shape(input_shape=input_shape, default_size=299, min_size=71, 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(): img_input = Input(shape=input_shape, tensor=input_tensor) else: img_input = input_tensor if K.image_data_format() == 'channels_last': bn_axis = -1 else: bn_axis = 1 # Block 1 x = Conv2D(32, (3, 3), strides=(2, 2), use_bias=False, kernel_initializer='he_normal', kernel_regularizer=l2(l2_norm), name="conv1_1")(img_input) x = BatchNormalization(axis=bn_axis, name="conv1_1_bn")(x) x = Activation('relu')(x) x = Conv2D(64, (3, 3), use_bias=False, kernel_initializer='he_normal', kernel_regularizer=l2(l2_norm), name="conv1_2")(x) x = BatchNormalization(axis=bn_axis, name="conv1_2_bn")(x) x = Activation('relu')(x) # Block 2 x = residual_separable(x, [128, 128, 128], block=2, bn_axis=bn_axis) # Block 3 x = residual_separable(x, [256, 256, 256], block=3, bn_axis=bn_axis) # Block 4 x = residual_separable(x, [728, 728, 728], block=4, bn_axis=bn_axis) # Block 5->12 for i in range(5, 13, 1): x = middle_flow(x, 728, block=i, bn_axis=bn_axis) # Block 13 x = residual_separable(x, [1024, 728, 1024], block=13, bn_axis=bn_axis) # Block14 x = SeparableConv2D(1536, (3, 3), padding='same', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=l2(l2_norm), name="conv14_1")(x) x = BatchNormalization(axis=bn_axis, name="conv14_1_bn")(x) x = Activation('relu')(x) x = SeparableConv2D(2048, (3, 3), padding='same', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=l2(l2_norm), name="conv14_2")(x) x = BatchNormalization(axis=bn_axis, name="conv14_2_bn")(x) x = Activation('relu', name="pool5")(x) if include_top: x = GlobalAveragePooling2D()(x) x = Dense(classes, activation='softmax', name='classifier')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) if input_tensor is None: inputs = get_source_inputs(input_tensor) else: inputs = img_input model = Model(inputs, x, name='xception') model.summary() return model
def VGG16_Places365(include_top=True, weights='places', input_tensor=None, input_shape=None, pooling=None, classes=365): """Instantiates the VGG16-places365 architecture. Optionally loads weights pre-trained on Places. 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), 'places' (pre-training on Places), 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, 244)` (with `channels_first` data format). It should have exactly 3 inputs 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 {'places', None} or os.path.exists(weights)): raise ValueError('The `weights` argument should be either ' '`None` (random initialization), `places` ' '(pre-training on Places), ' 'or the path to the weights file to be loaded.') if weights == 'places' and include_top and classes != 365: raise ValueError('If using `weights` as places with `include_top`' ' as true, `classes` should be 365') # 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 # Block 1 x = Conv2D(filters=64, kernel_size=3, strides=(1, 1), padding='same', kernel_regularizer=l2(0.0002), activation='relu', name='block1_conv1')(img_input) x = Conv2D(filters=64, kernel_size=3, strides=(1, 1), padding='same', kernel_regularizer=l2(0.0002), activation='relu', name='block1_conv2')(x) x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name="block1_pool", padding='valid')(x) # Block 2 x = Conv2D(filters=128, kernel_size=3, strides=(1, 1), padding='same', kernel_regularizer=l2(0.0002), activation='relu', name='block2_conv1')(x) x = Conv2D(filters=128, kernel_size=3, strides=(1, 1), padding='same', kernel_regularizer=l2(0.0002), activation='relu', name='block2_conv2')(x) x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name="block2_pool", padding='valid')(x) # Block 3 x = Conv2D(filters=256, kernel_size=3, strides=(1, 1), padding='same', kernel_regularizer=l2(0.0002), activation='relu', name='block3_conv1')(x) x = Conv2D(filters=256, kernel_size=3, strides=(1, 1), padding='same', kernel_regularizer=l2(0.0002), activation='relu', name='block3_conv2')(x) x = Conv2D(filters=256, kernel_size=3, strides=(1, 1), padding='same', kernel_regularizer=l2(0.0002), activation='relu', name='block3_conv3')(x) x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name="block3_pool", padding='valid')(x) # Block 4 x = Conv2D(filters=512, kernel_size=3, strides=(1, 1), padding='same', kernel_regularizer=l2(0.0002), activation='relu', name='block4_conv1')(x) x = Conv2D(filters=512, kernel_size=3, strides=(1, 1), padding='same', kernel_regularizer=l2(0.0002), activation='relu', name='block4_conv2')(x) x = Conv2D(filters=512, kernel_size=3, strides=(1, 1), padding='same', kernel_regularizer=l2(0.0002), activation='relu', name='block4_conv3')(x) x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name="block4_pool", padding='valid')(x) # Block 5 x = Conv2D(filters=512, kernel_size=3, strides=(1, 1), padding='same', kernel_regularizer=l2(0.0002), activation='relu', name='block5_conv1')(x) x = Conv2D(filters=512, kernel_size=3, strides=(1, 1), padding='same', kernel_regularizer=l2(0.0002), activation='relu', name='block5_conv2')(x) x = Conv2D(filters=512, kernel_size=3, strides=(1, 1), padding='same', kernel_regularizer=l2(0.0002), activation='relu', name='block5_conv3')(x) x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name="block5_pool", padding='valid')(x) if include_top: # Classification block x = Flatten(name='flatten')(x) x = Dense(4096, activation='relu', name='fc1')(x) x = Dropout(0.5, name='drop_fc1')(x) x = Dense(4096, activation='relu', name='fc2')(x) x = Dropout(0.5, name='drop_fc2')(x) x = Dense(365, 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-places365') # load weights if weights == 'places': if include_top: weights_path = get_file( 'vgg16-places365_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH, cache_subdir='models') else: weights_path = get_file( 'vgg16-places365_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.') elif weights is not None: model.load_weights(weights) return model
def build_model(input_shape=None): input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=24, data_format=K.image_data_format(), require_flatten=False, weights='None') img_input = Input(shape=input_shape) channel_axis = 3 reg = regularizers.l2(0.001) # layer 0 x = conv2d_bn(img_input, 32, 3, 3, strides=(2, 2), padding='valid', reg=reg) x = conv2d_bn(x, 32, 3, 3, padding='valid', reg=reg) x = conv2d_bn(x, 64, 3, 3, reg=reg) x = MaxPooling2D((3, 3), strides=(2, 2))(x) x = conv2d_bn(x, 80, 1, 1, padding='valid', reg=reg) x = conv2d_bn(x, 192, 3, 3, padding='valid', reg=reg) 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, reg=reg) branch5x5 = conv2d_bn(branch5x5, 64, 5, 5, reg=reg) branch3x3dbl = conv2d_bn(x, 64, 1, 1, reg=reg) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3, reg=reg) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3, reg=reg) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 32, 1, 1, reg=reg) x = layers.concatenate([branch1x1, branch5x5, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed0') # mixed 2: 35 x 35 x 256 branch1x1 = conv2d_bn(x, 64, 1, 1, reg=reg) branch5x5 = conv2d_bn(x, 48, 1, 1, reg=reg) branch5x5 = conv2d_bn(branch5x5, 64, 5, 5, reg=reg) branch3x3dbl = conv2d_bn(x, 64, 1, 1, reg=reg) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3, reg=reg) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3, reg=reg) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 64, 1, 1, reg=reg) x = layers.concatenate([branch1x1, branch5x5, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed2') # mixed 5, 6: 17 x 17 x 768 for i in range(1): branch1x1 = conv2d_bn(x, 192, 1, 1, reg=reg) branch7x7 = conv2d_bn(x, 160, 1, 1, reg=reg) branch7x7 = conv2d_bn(branch7x7, 160, 1, 7, reg=reg) branch7x7 = conv2d_bn(branch7x7, 192, 7, 1, reg=reg) branch7x7dbl = conv2d_bn(x, 160, 1, 1, reg=reg) branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1, reg=reg) branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 1, 7, reg=reg) branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1, reg=reg) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7, reg=reg) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 192, 1, 1, reg=reg) x = layers.concatenate( [branch1x1, branch7x7, branch7x7dbl, branch_pool], axis=channel_axis, name='mixed' + str(5 + i)) x = GlobalMaxPooling2D()(x) x = Flatten()(x) x = Dense(384, activation='relu')(x) x = Dense(64, activation='relu')(x) x = Dense(num_classes, activation='softmax')(x) model = Model(img_input, x, name='micro_xception_v1') opt = optimizers.Adam(lr=0.001, decay=0.001) model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy']) return model