from efficientnet import EfficientNetB0 as Net from efficientnet import center_crop_and_resize, preprocess_input # In[ ]: import efficientnet.keras as efn base_model = efn.EfficientNetB0(weights='imagenet') # In[13]: from tensorflow.keras import models from tensorflow.keras import layers dropout_rate = 0.2 model = models.Sequential() model.add(base_model) model.add(layers.GlobalMaxPooling2D(name="gap")) # model.add(layers.Flatten(name="flatten")) if dropout_rate > 0: model.add(layers.Dropout(dropout_rate, name="dropout_out")) # model.add(layers.Dense(256, activation='relu', name="fc1")) model.add(layers.Dense(2, activation="softmax", name="fc_out")) # In[14]: base_model # In[ ]:
def Xception_model(img_input, pooling=None): channel_axis = 1 if backend.image_data_format() == 'channels_first' else -1 #??? x = layers.Conv2D(32, (3, 3), strides=(2, 2), use_bias=False, name='block1_conv1')(img_input) x = layers.BatchNormalization(axis=channel_axis, name='block1_conv1_bn')(x) x = layers.Activation('relu', name='block1_conv1_act')(x) x = layers.Conv2D(64, (3, 3), use_bias=False, name='block1_conv2')(x) x = layers.BatchNormalization(axis=channel_axis, name='block1_conv2_bn')(x) x = layers.Activation('relu', name='block1_conv2_act')(x) residual = layers.Conv2D(128, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x) residual = layers.BatchNormalization(axis=channel_axis)(residual) x = layers.SeparableConv2D(128, (3, 3), padding='same', use_bias=False, name='block2_sepconv1')(x) x = layers.BatchNormalization(axis=channel_axis, name='block2_sepconv1_bn')(x) x = layers.Activation('relu', name='block2_sepconv2_act')(x) x = layers.SeparableConv2D(128, (3, 3), padding='same', use_bias=False, name='block2_sepconv2')(x) x = layers.BatchNormalization(axis=channel_axis, name='block2_sepconv2_bn')(x) x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block2_pool')(x) x = layers.add([x, residual]) residual = layers.Conv2D(256, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x) residual = layers.BatchNormalization(axis=channel_axis)(residual) x = layers.Activation('relu', name='block3_sepconv1_act')(x) x = layers.SeparableConv2D(256, (3, 3), padding='same', use_bias=False, name='block3_sepconv1')(x) x = layers.BatchNormalization(axis=channel_axis, name='block3_sepconv1_bn')(x) x = layers.Activation('relu', name='block3_sepconv2_act')(x) x = layers.SeparableConv2D(256, (3, 3), padding='same', use_bias=False, name='block3_sepconv2')(x) x = layers.BatchNormalization(axis=channel_axis, name='block3_sepconv2_bn')(x) x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block3_pool')(x) x = layers.add([x, residual]) residual = layers.Conv2D(728, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x) residual = layers.BatchNormalization(axis=channel_axis)(residual) x = layers.Activation('relu', name='block4_sepconv1_act')(x) x = layers.SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name='block4_sepconv1')(x) x = layers.BatchNormalization(axis=channel_axis, name='block4_sepconv1_bn')(x) x = layers.Activation('relu', name='block4_sepconv2_act')(x) x = layers.SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name='block4_sepconv2')(x) x = layers.BatchNormalization(axis=channel_axis, name='block4_sepconv2_bn')(x) x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block4_pool')(x) x = layers.add([x, residual]) for i in range(8): residual = x prefix = 'block' + str(i + 5) x = layers.Activation('relu', name=prefix + '_sepconv1_act')(x) x = layers.SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name=prefix + '_sepconv1')(x) x = layers.BatchNormalization(axis=channel_axis, name=prefix + '_sepconv1_bn')(x) x = layers.Activation('relu', name=prefix + '_sepconv2_act')(x) x = layers.SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name=prefix + '_sepconv2')(x) x = layers.BatchNormalization(axis=channel_axis, name=prefix + '_sepconv2_bn')(x) x = layers.Activation('relu', name=prefix + '_sepconv3_act')(x) x = layers.SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name=prefix + '_sepconv3')(x) x = layers.BatchNormalization(axis=channel_axis, name=prefix + '_sepconv3_bn')(x) x = layers.add([x, residual]) residual = layers.Conv2D(1024, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x) residual = layers.BatchNormalization(axis=channel_axis)(residual) x = layers.Activation('relu', name='block13_sepconv1_act')(x) x = layers.SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name='block13_sepconv1')(x) x = layers.BatchNormalization(axis=channel_axis, name='block13_sepconv1_bn')(x) x = layers.Activation('relu', name='block13_sepconv2_act')(x) x = layers.SeparableConv2D(1024, (3, 3), padding='same', use_bias=False, name='block13_sepconv2')(x) x = layers.BatchNormalization(axis=channel_axis, name='block13_sepconv2_bn')(x) x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block13_pool')(x) x = layers.add([x, residual]) x = layers.SeparableConv2D(1536, (3, 3), padding='same', use_bias=False, name='block14_sepconv1')(x) x = layers.BatchNormalization(axis=channel_axis, name='block14_sepconv1_bn')(x) x = layers.Activation('relu', name='block14_sepconv1_act')(x) x = layers.SeparableConv2D(2048, (3, 3), padding='same', use_bias=False, name='block14_sepconv2')(x) x = layers.BatchNormalization(axis=channel_axis, name='block14_sepconv2_bn')(x) x = layers.Activation('relu', name='block14_sepconv2_act')(x) if pooling == 'avg': x = layers.GlobalAveragePooling2D()(x) elif pooling == 'max': x = layers.GlobalMaxPooling2D()(x) return x
def model_DR(input_shape=None, alpha=1.0, depth_multiplier=1, dropout=1e-3, include_top=True, weights='imagenet', input_tensor=None, pooling=None, classes=1000): if input_shape is None: default_size = 224 else: if backend.image_data_format() == 'channels_first': rows = input_shape[1] cols = input_shape[2] else: rows = input_shape[0] cols = input_shape[1] if rows == cols and rows in [128, 160, 192, 224]: default_size = rows else: default_size = 224 input_shape = get_input_shape(input_shape, default_size=default_size, require_flatten=include_top, weights=weights, data_format=backend.image_data_format()) if backend.image_data_format() == 'channels_last': row_axis, col_axis = (0, 1) else: row_axis, col_axis = (1, 2) rows = input_shape[row_axis] cols = input_shape[col_axis] if weights == 'imagenet': if rows != cols or rows not in [128, 160, 192, 224]: if rows is None: rows = 224 if backend.image_data_format() != 'channels_last': backend.set_image_data_format('channels_last') old_data_format = 'channels_first' else: old_data_format = None if input_tensor is None: img_input = layers.Input(shape=input_shape) else: if not backend.is_keras_tensor(input_tensor): img_input = layers.Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor x = conv(img_input, 32, alpha, strides=(2, 2)) x = conv_depthwise(x, 64, alpha, depth_multiplier, block_id=1) x = conv_depthwise(x, 128, alpha, depth_multiplier, strides=(2, 2), block_id=2) x = conv_depthwise(x, 128, alpha, depth_multiplier, block_id=3) x = conv_depthwise(x, 256, alpha, depth_multiplier, strides=(2, 2), block_id=4) x = conv_depthwise(x, 256, alpha, depth_multiplier, block_id=5) x = conv_depthwise(x, 512, alpha, depth_multiplier, strides=(2, 2), block_id=6) x = conv_depthwise(x, 512, alpha, depth_multiplier, block_id=7) x = conv_depthwise(x, 512, alpha, depth_multiplier, block_id=8) x = conv_depthwise(x, 512, alpha, depth_multiplier, block_id=9) x = conv_depthwise(x, 512, alpha, depth_multiplier, block_id=10) x = conv_depthwise(x, 512, alpha, depth_multiplier, block_id=11) x = conv_depthwise(x, 1024, alpha, depth_multiplier, strides=(2, 2), block_id=12) x = conv_depthwise(x, 1024, alpha, depth_multiplier, block_id=13) if include_top: if backend.image_data_format() == 'channels_first': shape = (int(1024 * alpha), 1, 1) else: shape = (1, 1, int(1024 * alpha)) x = GlobalAveragePooling2D()(x) x = layers.Reshape(shape, name='reshape_1')(x) x = layers.Dropout(dropout, name='dropout')(x) x = layers.Conv2D(classes, (1, 1), padding='same', name='conv_preds')(x) x = layers.Activation('softmax', name='act_softmax')(x) x = layers.Reshape((classes, ), name='reshape_2')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = layers.GlobalMaxPooling2D()(x) if input_tensor is not None: if hasattr(keras_utils, 'get_source_inputs'): get_source_inputs = keras_utils.get_source_inputs else: get_source_inputs = engine.get_source_inputs inputs = get_source_inputs(input_tensor) else: inputs = img_input model = models.Model(inputs, x, name='mobilenet_%0.2f_%s' % (alpha, rows)) if weights == 'imagenet': if backend.image_data_format() == 'channels_first': raise ValueError('Weights for "channels_first" format ' 'are not available.') if alpha == 1.0: alpha_text = '1_0' elif alpha == 0.75: alpha_text = '7_5' elif alpha == 0.50: alpha_text = '5_0' else: alpha_text = '2_5' if include_top: model_name = 'mobilenet_%s_%d_tf.h5' % (alpha_text, rows) weight_path = BASE_WEIGHT_PATH + model_name weights_path = keras_utils.get_file(model_name, weight_path, cache_subdir='models') else: model_name = 'mobilenet_%s_%d_tf_no_top.h5' % (alpha_text, rows) weight_path = BASE_WEIGHT_PATH + model_name weights_path = keras_utils.get_file(model_name, weight_path, cache_subdir='models') model.load_weights(weights_path) elif weights is not None: model.load_weights(weights) if old_data_format: backend.set_image_data_format(old_data_format) return model
def ResNet50_mod(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000, **kwargs): """Instantiates the ResNet50 architecture. Optionally loads weights pre-trained on ImageNet. Note that the data format convention used by the model is the one specified in your Keras config at `~/.keras/keras.json`. # Arguments include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization), 'imagenet' (pre-training on ImageNet), or the path to the weights file to be loaded. input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. input_shape: optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(224, 224, 3)` (with `channels_last` data format) or `(3, 224, 224)` (with `channels_first` data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 32. E.g. `(200, 200, 3)` would be one valid value. pooling: Optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional block. - `avg` means that global average pooling will be applied to the output of the last convolutional block, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. classes: optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. # Returns A Keras model instance. # Raises ValueError: in case of invalid argument for `weights`, or invalid input shape. """ # global backend, layers, models, keras_utils # backend, layers, models, keras_utils = get_submodules_from_kwargs(kwargs) # if not (weights in {'imagenet', None} or os.path.exists(weights)): # raise ValueError('The `weights` argument should be either ' # '`None` (random initialization), `imagenet` ' # '(pre-training on ImageNet), ' # 'or the path to the weights file to be loaded.') # if weights == 'imagenet' and include_top and classes != 1000: # raise ValueError('If using `weights` as `"imagenet"` with `include_top`' # ' as true, `classes` should be 1000') # # Determine proper input shape # input_shape = _obtain_input_shape(input_shape, # default_size=224, # min_size=32, # data_format=backend.image_data_format(), # require_flatten=include_top, # weights=weights) # if input_tensor is None: # img_input = layers.Input(shape=input_shape) # else: # if not backend.is_keras_tensor(input_tensor): # img_input = layers.Input(tensor=input_tensor, shape=input_shape) # else: # img_input = input_tensor # if backend.image_data_format() == 'channels_last': # bn_axis = 3 # else: # bn_axis = 1 bn_axis = -1 img_input = Input(shape=input_shape) x = layers.ZeroPadding2D(padding=(3, 3), name='conv1_pad')(img_input) x = layers.Conv2D(64, (7, 7), strides=(2, 2), padding='valid', kernel_initializer='he_normal', name='conv1')(x) x = layers.BatchNormalization(axis=bn_axis, name='bn_conv1')(x) x = layers.Activation('relu')(x) x = layers.ZeroPadding2D(padding=(1, 1), name='pool1_pad')(x) x = layers.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') if include_top: x = layers.GlobalAveragePooling2D(name='avg_pool')(x) x = layers.Dense(classes, activation='softmax', name='fc1000')(x) else: if pooling == 'avg': x = layers.GlobalAveragePooling2D()(x) elif pooling == 'max': x = layers.GlobalMaxPooling2D()(x) else: warnings.warn('The output shape of `ResNet50(include_top=False)` ' 'has been changed since Keras 2.2.0.') # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = keras_utils.get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = models.Model(inputs, x, name='resnet50') # Load weights. if weights == 'imagenet': if include_top: weights_path = data_utils.get_file( 'resnet50_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH, cache_subdir='models', md5_hash='a7b3fe01876f51b976af0dea6bc144eb') else: weights_path = data_utils.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) elif weights is not None: model.load_weights(weights) return model
def VGGish(pump=None, input_shape=None, include_top=False, pooling='avg', weights='audioset', name='vggish', compress=False): '''A Keras implementation of the VGGish architecture. Arguments: pump (pumpp.Pump): The model pump object. input_shape (tuple): the model input shape. If ``include_top``, ``input_shape`` will be set to ``(params.NUM_FRAMES, params.NUM_BANDS, 1)``, otherwise it will be ``(None, None, 1)`` to accomodate variable sized inputs. include_top (bool): whether to include the fully connected layers. Default is False. pooling (str): what type of global pooling should be applied if no top? Default is 'avg' weights (str, None): the weights to use (see WEIGHTS_PATHS). Currently, there is only 'audioset'. Can also be a path to a keras weights file. name (str): the name for the model. Returns: A Keras model instance. ''' with tf.name_scope(name): if input_shape: pass elif include_top: input_shape = params.NUM_FRAMES, params.NUM_BANDS, 1 elif pump: # print(type(pump)) inputs = pump.layers('tf.keras')[params.PUMP_INPUT] else: input_shape = None, None, 1 # use input_shape to make input if input_shape: inputs = tfkl.Input(shape=input_shape, name='input_1') # setup layer params conv = partial( tfkl.Conv2D, kernel_size=(3, 3), strides=(1, 1), activation='relu', padding='same') maxpool = partial( tfkl.MaxPooling2D, pool_size=(2, 2), strides=(2, 2), padding='same') # Block 1 x = conv(64, name='conv1')(inputs) x = maxpool(name='pool1')(x) # Block 2 x = conv(128, name='conv2')(x) x = maxpool(name='pool2')(x) # Block 3 x = conv(256, name='conv3/conv3_1')(x) x = conv(256, name='conv3/conv3_2')(x) x = maxpool(name='pool3')(x) # Block 4 x = conv(512, name='conv4/conv4_1')(x) x = conv(512, name='conv4/conv4_2')(x) x = maxpool(name='pool4')(x) if include_top: dense = partial(tfkl.Dense, activation='relu') # FC block x = tfkl.Flatten(name='flatten_')(x) x = dense(4096, name='fc1/fc1_1')(x) x = dense(4096, name='fc1/fc1_2')(x) x = dense(params.EMBEDDING_SIZE, name='fc2')(x) if compress: x = Postprocess()(x) else: globalpool = ( tfkl.GlobalAveragePooling2D() if pooling == 'avg' else tfkl.GlobalMaxPooling2D() if pooling == 'max' else None) if globalpool: x = globalpool(x) # Create model model = Model(inputs, x, name='model') # print(model.summary()) model.load_weights('/home/mkolpe2s/rand/CNN/VGGish/vggish_keras/vggish_audioset_weights_without_fc2.h5') # load_vggish_weights(model, weights, strict=bool(weights)) # print("Okay") return model
""" batch_size=64; epochs=30 base_model=tf.keras.Sequential([ tkl.Input((img_size,img_size,3),name='input'), tkl.Conv2D(32,(5,5),padding='same'), tkl.Activation('relu'), tkl.MaxPooling2D(pool_size=(2,2)), tkl.Dropout(.25), tkl.Conv2D(196,(5,5)), tkl.Activation('relu'), tkl.MaxPooling2D(pool_size=(2,2)), tkl.Dropout(.25), tkl.GlobalMaxPooling2D(), tkl.Dense(512), tkl.Activation('relu'), tkl.Dropout(.25), tkl.Dense(128), tkl.Activation('relu'), tkl.Dropout(.25), tkl.Dense(num_classes,activation='softmax')]) adv_config=nsl.configs\ .make_adv_reg_config(multiplier=.2,adv_step_size=.05) adv_model=nsl.keras\ .AdversarialRegularization(base_model,adv_config=adv_config) model_weights='/tmp/checkpoint' checkpointer=tkc.ModelCheckpoint( filepath=model_weights,verbose=2,save_weights_only=True, monitor='val_sparse_categorical_accuracy',
def Xception(include_top=True, input_shape=(224, 224, 3), pooling=None, classes=1000): img_input = layers.Input(shape=input_shape) channel_axis = -1 x = layers.Conv2D(32, (3, 3), strides=(2, 2), use_bias=False, name='block1_conv1')(img_input) x = layers.BatchNormalization(axis=channel_axis, name='block1_conv1_bn')(x) x = layers.Activation('relu', name='block1_conv1_act')(x) x = layers.Conv2D(64, (3, 3), use_bias=False, name='block1_conv2')(x) x = layers.BatchNormalization(axis=channel_axis, name='block1_conv2_bn')(x) x = layers.Activation('relu', name='block1_conv2_act')(x) residual = layers.Conv2D(128, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x) residual = layers.BatchNormalization(axis=channel_axis)(residual) x = layers.SeparableConv2D(128, (3, 3), padding='same', use_bias=False, name='block2_sepconv1')(x) x = layers.BatchNormalization(axis=channel_axis, name='block2_sepconv1_bn')(x) x = layers.Activation('relu', name='block2_sepconv2_act')(x) x = layers.SeparableConv2D(128, (3, 3), padding='same', use_bias=False, name='block2_sepconv2')(x) x = layers.BatchNormalization(axis=channel_axis, name='block2_sepconv2_bn')(x) x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block2_pool')(x) x = layers.add([x, residual]) residual = layers.Conv2D(256, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x) residual = layers.BatchNormalization(axis=channel_axis)(residual) x = layers.Activation('relu', name='block3_sepconv1_act')(x) x = layers.SeparableConv2D(256, (3, 3), padding='same', use_bias=False, name='block3_sepconv1')(x) x = layers.BatchNormalization(axis=channel_axis, name='block3_sepconv1_bn')(x) x = layers.Activation('relu', name='block3_sepconv2_act')(x) x = layers.SeparableConv2D(256, (3, 3), padding='same', use_bias=False, name='block3_sepconv2')(x) x = layers.BatchNormalization(axis=channel_axis, name='block3_sepconv2_bn')(x) x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block3_pool')(x) x = layers.add([x, residual]) residual = layers.Conv2D(728, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x) residual = layers.BatchNormalization(axis=channel_axis)(residual) x = layers.Activation('relu', name='block4_sepconv1_act')(x) x = layers.SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name='block4_sepconv1')(x) x = layers.BatchNormalization(axis=channel_axis, name='block4_sepconv1_bn')(x) x = layers.Activation('relu', name='block4_sepconv2_act')(x) x = layers.SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name='block4_sepconv2')(x) x = layers.BatchNormalization(axis=channel_axis, name='block4_sepconv2_bn')(x) x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block4_pool')(x) x = layers.add([x, residual]) for i in range(8): residual = x prefix = 'block' + str(i + 5) x = layers.Activation('relu', name=prefix + '_sepconv1_act')(x) x = layers.SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name=prefix + '_sepconv1')(x) x = layers.BatchNormalization(axis=channel_axis, name=prefix + '_sepconv1_bn')(x) x = layers.Activation('relu', name=prefix + '_sepconv2_act')(x) x = layers.SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name=prefix + '_sepconv2')(x) x = layers.BatchNormalization(axis=channel_axis, name=prefix + '_sepconv2_bn')(x) x = layers.Activation('relu', name=prefix + '_sepconv3_act')(x) x = layers.SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name=prefix + '_sepconv3')(x) x = layers.BatchNormalization(axis=channel_axis, name=prefix + '_sepconv3_bn')(x) x = layers.add([x, residual]) residual = layers.Conv2D(1024, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x) residual = layers.BatchNormalization(axis=channel_axis)(residual) x = layers.Activation('relu', name='block13_sepconv1_act')(x) x = layers.SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name='block13_sepconv1')(x) x = layers.BatchNormalization(axis=channel_axis, name='block13_sepconv1_bn')(x) x = layers.Activation('relu', name='block13_sepconv2_act')(x) x = layers.SeparableConv2D(1024, (3, 3), padding='same', use_bias=False, name='block13_sepconv2')(x) x = layers.BatchNormalization(axis=channel_axis, name='block13_sepconv2_bn')(x) x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block13_pool')(x) x = layers.add([x, residual]) x = layers.SeparableConv2D(1536, (3, 3), padding='same', use_bias=False, name='block14_sepconv1')(x) x = layers.BatchNormalization(axis=channel_axis, name='block14_sepconv1_bn')(x) x = layers.Activation('relu', name='block14_sepconv1_act')(x) x = layers.SeparableConv2D(2048, (3, 3), padding='same', use_bias=False, name='block14_sepconv2')(x) x = layers.BatchNormalization(axis=channel_axis, name='block14_sepconv2_bn')(x) x = layers.Activation('relu', name='block14_sepconv2_act')(x) if include_top: x = layers.GlobalAveragePooling2D(name='avg_pool')(x) x = layers.Dense(classes, activation='softmax', name='predictions')(x) else: if pooling == 'avg': x = layers.GlobalAveragePooling2D()(x) elif pooling == 'max': x = layers.GlobalMaxPooling2D()(x) # Create model. model = models.Model(img_input, x, name='xception') return model
def ResNet(stack_fn, preact, use_bias, model_name='resnet', include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000, **kwargs): """Instantiates the ResNet, ResNetV2, and ResNeXt architecture. Optionally loads weights pre-trained on ImageNet. Note that the data format convention used by the model is the one specified in your Keras config at `~/.keras/keras.json`. # Arguments stack_fn: a function that returns output tensor for the stacked residual blocks. preact: whether to use pre-activation or not (True for ResNetV2, False for ResNet and ResNeXt). use_bias: whether to use biases for convolutional layers or not (True for ResNet and ResNetV2, False for ResNeXt). model_name: string, model name. include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization), 'imagenet' (pre-training on ImageNet), or the path to the weights file to be loaded. input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. input_shape: optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(224, 224, 3)` (with `channels_last` data format) or `(3, 224, 224)` (with `channels_first` data format). It should have exactly 3 inputs channels. pooling: optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional layer. - `avg` means that global average pooling will be applied to the output of the last convolutional layer, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. classes: optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. # Returns A Keras model instance. # Raises ValueError: in case of invalid argument for `weights`, or invalid input shape. """ if not (weights in {'imagenet', None} or os.path.exists(weights)): raise ValueError('The `weights` argument should be either ' '`None` (random initialization), `imagenet` ' '(pre-training on ImageNet), ' 'or the path to the weights file to be loaded.') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError( 'If using `weights` as `"imagenet"` with `include_top`' ' as true, `classes` should be 1000') # Determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=32, data_format=backend.image_data_format(), require_flatten=include_top, weights=weights) if input_tensor is None: img_input = layers.Input(shape=input_shape) else: if not backend.is_keras_tensor(input_tensor): img_input = layers.Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor bn_axis = 3 if backend.image_data_format() == 'channels_last' else 1 x = layers.ZeroPadding2D(padding=((3, 3), (3, 3)), name='conv1_pad')(img_input) x = layers.Conv2D(64, 7, strides=2, use_bias=use_bias, name='conv1_conv')(x) if preact is False: x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='conv1_bn')(x) x = layers.Activation('relu', name='conv1_relu')(x) x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)), name='pool1_pad')(x) x = layers.MaxPooling2D(3, strides=2, name='pool1_pool')(x) x = stack_fn(x) if preact is True: x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='post_bn')(x) x = layers.Activation('relu', name='post_relu')(x) if include_top: x = layers.GlobalAveragePooling2D(name='avg_pool')(x) x = layers.Dense(classes, activation='softmax', name='probs')(x) else: if pooling == 'avg': x = layers.GlobalAveragePooling2D(name='avg_pool')(x) elif pooling == 'max': x = layers.GlobalMaxPooling2D(name='max_pool')(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = keras_utils.get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = models.Model(inputs, x, name=model_name) # Load weights. if (weights == 'imagenet') and (model_name in WEIGHTS_HASHES): if include_top: file_name = model_name + '_weights_tf_dim_ordering_tf_kernels.h5' file_hash = WEIGHTS_HASHES[model_name][0] else: file_name = model_name + '_weights_tf_dim_ordering_tf_kernels_notop.h5' file_hash = WEIGHTS_HASHES[model_name][1] weights_path = keras_utils.get_file(file_name, BASE_WEIGHTS_PATH + file_name, cache_subdir='models', file_hash=file_hash) by_name = True if 'resnext' in model_name else False model.load_weights(weights_path, by_name=by_name) elif weights is not None: model.load_weights(weights) return model
def CreateModel(config): config_reg = config['model']['regularize'] config_model = config['model'] isEval = config_model['eval'] useBias = isEval # initial convolution # TODO: figure out why I need to do this (maybe because of the number of 2x2 reductions) # 8 would suggest 2^3 or 3 reductions first_conv_filters = mnet.makeDivisible(32 * config_model['alpha'], 8) image_shape = config_model['image-shape'] if config_model['nchw']: image_shape = (image_shape[2], image_shape[0], image_shape[1]) inputs = layers.Input(shape=image_shape, name='input_1') if config_model['nchw']: x = layers.Permute((2, 3, 1))(inputs) else: x = inputs x = layers.ZeroPadding2D(padding=mnet.correctPad( config_model['image-shape'][0], 3, 2), name='Conv1_pad')(x) in_conv_reg = None if config_reg['in-conv']['reg']: in_conv_reg = l2(config_reg['in-conv']['reg']) x = layers.Conv2D(32, (3, 3), strides=(2, 2), padding='valid', use_bias=useBias, name='Conv1', activity_regularizer=in_conv_reg)(x) if not isEval: x = mnet.addBN(x, 'Conv1_BN') x = mnet.addReLU6(x, 'Conv1_relu') if (not isEval) and (not config_reg['in-conv']['dropout'] is None): x = mnet.addDropout(x, 'Conv1_dropout', rate=config_reg['in-conv']['dropout']) # residual block layers # mnet.checkShape(x.shape[1:4], (112,112,32)) x = mnet.InvertedResidual(x, filters=16, block_id=0, expand=1, stride=1, config=config) # mnet.checkShape(x.shape[1:4], (112,112,16)) x = mnet.InvertedResidual(x, filters=24, block_id=1, expand=6, stride=2, config=config) x = mnet.InvertedResidual(x, filters=24, block_id=2, expand=6, stride=1, config=config) # mnet.checkShape(x.shape[1:4], (56,56,24)) x = mnet.InvertedResidual(x, filters=32, block_id=3, expand=6, stride=2, config=config) x = mnet.InvertedResidual(x, filters=32, block_id=4, expand=6, stride=1, config=config) x = mnet.InvertedResidual(x, filters=32, block_id=5, expand=6, stride=1, config=config) # mnet.checkShape(x.shape[1:4], (28,28,32)) x = mnet.InvertedResidual(x, filters=64, block_id=6, expand=6, stride=2, config=config) x = mnet.InvertedResidual(x, filters=64, block_id=7, expand=6, stride=1, config=config) x = mnet.InvertedResidual(x, filters=64, block_id=8, expand=6, stride=1, config=config) x = mnet.InvertedResidual(x, filters=64, block_id=9, expand=6, stride=1, config=config) # mnet.checkShape(x.shape[1:4], (14,14,64)) x = mnet.InvertedResidual(x, filters=96, block_id=10, expand=6, stride=1, config=config) x = mnet.InvertedResidual(x, filters=96, block_id=11, expand=6, stride=1, config=config) x = mnet.InvertedResidual(x, filters=96, block_id=12, expand=6, stride=1, config=config) # mnet.checkShape(x.shape[1:4], (14,14,96)) x = mnet.InvertedResidual(x, filters=160, block_id=13, expand=6, stride=2, config=config) x = mnet.InvertedResidual(x, filters=160, block_id=14, expand=6, stride=1, config=config) x = mnet.InvertedResidual(x, filters=160, block_id=15, expand=6, stride=1, config=config) # mnet.checkShape(x.shape[1:4], (7,7,160)) x = mnet.InvertedResidual(x, filters=320, block_id=16, expand=6, stride=1, config=config) # mnet.checkShape(x.shape[1:4], (7,7,320)) # x = mnet.InvertedResidual(x, filters=1280, block_id=17, expand=6, stride=1) # # mnet.checkShape(x.shape[1:4], (7,7,1280)) # last block conv out_conv_reg = None if config_reg['out-conv']['reg']: out_conv_reg = l2(config_reg['out-conv']['reg']) print("useBias: ", useBias) x = layers.Conv2D(1280, (1, 1), use_bias=useBias, name='ConvLast', activity_regularizer=out_conv_reg)(x) if not isEval: x = mnet.addBN(x, 'ConvLast_BN') x = mnet.addReLU6(x, 'ConvLast_relu') if (not isEval) and (not config_reg['out-conv']['dropout'] is None): x = mnet.addDropout(x, 'ConvLast_dropout', rate=config_reg['out-conv']['dropout']) # pool # replace this with maxpool x = layers.GlobalMaxPooling2D(name='global_max_pool')(x) # create model model_base = keras.Model(inputs=inputs, outputs=x, name="mobilenetv2") model_base_out = x # load and apply weights from imagenet training if config['training']['transfer-learn']: weights = loadWeights(alpha=config_model['alpha'], rows=config_model['image-shape'][0]) applyWeights(model_base, weights) # output layer dense_reg = None if config_reg['dense']['reg']: dense_reg = l2(config_reg['dense']['reg']) x = layers.Dense(config_model['classes'], use_bias=True, name='logits', activity_regularizer=dense_reg)(model_base_out) x = layers.Softmax(name='softmax')(x) model = keras.Model(inputs=inputs, outputs=x) print("\n") model.summary() return model
def build(self, hp): activation = hp.Choice('activation', ['relu', 'selu']) # Model definition. if self.input_tensor is not None: inputs = tf.keras.utils.get_source_inputs(self.input_tensor) x = self.input_tensor else: inputs = layers.Input(shape=self.input_shape) x = inputs # Initial conv2d. conv2d_num_filters = hp.Choice('conv2d_num_filters', [32, 64, 128], default=64) kernel_size = hp.Choice('kernel_size', [3, 5]) initial_strides = hp.Choice('initial_strides', [2]) x = conv(x, conv2d_num_filters, kernel_size=kernel_size, activation=activation, strides=initial_strides) # Separable convs. sep_num_filters = hp.Range('sep_num_filters', 128, 768, step=128, default=256) num_residual_blocks = hp.Range('num_residual_blocks', 2, 8, default=4) for _ in range(num_residual_blocks): x = residual(x, sep_num_filters, activation=activation, max_pooling=False) # Exit flow. x = residual(x, 2 * sep_num_filters, activation=activation, max_pooling=True) pooling = hp.Choice('pooling', ['avg', 'flatten', 'max']) if pooling == 'flatten': x = layers.Flatten()(x) elif pooling == 'avg': x = layers.GlobalAveragePooling2D()(x) else: x = layers.GlobalMaxPooling2D()(x) if self.include_top: # Dense num_dense_layers = hp.Range('num_dense_layers', 1, 3) dropout_rate = hp.Linear('dropout_rate', 0.0, 0.6, resolution=0.1, default=0.5) dense_use_bn = hp.Choice('dense_use_bn', [True, False]) for _ in range(num_dense_layers): x = dense(x, self.classes, activation=activation, batchnorm=dense_use_bn, dropout_rate=dropout_rate) output = layers.Dense(self.classes, activation='softmax')(x) model = keras.Model(inputs, output, name='Xception') model.compile(optimizer=keras.optimizers.Adam( hp.Choice('learning_rate', [1e-3, 1e-4, 1e-5])), loss='categorical_crossentropy', metrics=['accuracy']) return model else: return keras.Model(inputs, x, name='Xception')
model.summary() # The answer was: (40, 40, 32), so we can keep downsampling... model.add(layers.Conv2D(32, 3, activation="relu")) model.add(layers.Conv2D(32, 3, activation="relu")) model.add(layers.MaxPooling2D(3)) model.add(layers.Conv2D(32, 3, activation="relu")) model.add(layers.Conv2D(32, 3, activation="relu")) model.add(layers.MaxPooling2D(2)) # And now? model.summary() # Now that we have 4x4 feature maps, time to apply global max pooling. model.add(layers.GlobalMaxPooling2D()) # Finally, we add a classification layer. model.add(layers.Dense(10)) """ Very practical, right? """ """ ## What to do once you have a model Once your model architecture is ready, you will want to: - Train your model, evaluate it, and run inference. See our [guide to training & evaluation with the built-in loops](
def EfficientNet(width_coefficient, depth_coefficient, default_resolution, dropout_rate=0.2, drop_connect_rate=0.2, depth_divisor=8, blocks_args=DEFAULT_BLOCKS_ARGS, model_name='efficientnet', include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000, **kwargs): """Instantiates the EfficientNet architecture using given scaling coefficients. Optionally loads weights pre-trained on ImageNet. Note that the data format convention used by the model is the one specified in your Keras config at `~/.keras/keras.json`. # Arguments width_coefficient: float, scaling coefficient for network width. depth_coefficient: float, scaling coefficient for network depth. default_resolution: int, default input image size. dropout_rate: float, dropout rate before final classifier layer. drop_connect_rate: float, dropout rate at skip connections. depth_divisor: int. blocks_args: A list of BlockArgs to construct block modules. model_name: string, model name. include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization), 'imagenet' (pre-training on ImageNet), or the path to the weights file to be loaded. input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. input_shape: optional shape tuple, only to be specified if `include_top` is False. It should have exactly 3 inputs channels. pooling: optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional layer. - `avg` means that global average pooling will be applied to the output of the last convolutional layer, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. classes: optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. # 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=default_resolution, min_size=32, data_format=backend.image_data_format(), require_flatten=include_top, weights=weights) if input_tensor is None: img_input = layers.Input(shape=input_shape) else: if backend.backend() == 'tensorflow': from tensorflow.python.keras.backend import is_keras_tensor else: is_keras_tensor = backend.is_keras_tensor if not is_keras_tensor(input_tensor): img_input = layers.Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor bn_axis = 3 if backend.image_data_format() == 'channels_last' else 1 activation = get_swish(**kwargs) # Build stem x = img_input x = layers.Conv2D(round_filters(32, width_coefficient, depth_divisor), 3, strides=(2, 2), padding='same', use_bias=False, kernel_initializer=CONV_KERNEL_INITIALIZER, name='stem_conv')(x) x = layers.BatchNormalization(axis=bn_axis, name='stem_bn')(x) x = layers.Activation(activation, name='stem_activation')(x) # Build blocks num_blocks_total = sum(block_args.num_repeat for block_args in blocks_args) block_num = 0 for idx, block_args in enumerate(blocks_args): assert block_args.num_repeat > 0 # Update block input and output filters based on depth multiplier. block_args = block_args._replace( input_filters=round_filters(block_args.input_filters, width_coefficient, depth_divisor), output_filters=round_filters(block_args.output_filters, width_coefficient, depth_divisor), num_repeat=round_repeats(block_args.num_repeat, depth_coefficient)) # The first block needs to take care of stride and filter size increase. drop_rate = drop_connect_rate * float(block_num) / num_blocks_total x = mb_conv_block(x, block_args, activation=activation, drop_rate=drop_rate, prefix='block{}a_'.format(idx + 1)) block_num += 1 if block_args.num_repeat > 1: # pylint: disable=protected-access block_args = block_args._replace( input_filters=block_args.output_filters, strides=[1, 1]) # pylint: enable=protected-access for bidx in xrange(block_args.num_repeat - 1): drop_rate = drop_connect_rate * float( block_num) / num_blocks_total block_prefix = 'block{}{}_'.format( idx + 1, string.ascii_lowercase[bidx + 1]) x = mb_conv_block(x, block_args, activation=activation, drop_rate=drop_rate, prefix=block_prefix) block_num += 1 # Build top x = layers.Conv2D(round_filters(1280, width_coefficient, depth_divisor), 1, padding='same', use_bias=False, kernel_initializer=CONV_KERNEL_INITIALIZER, name='top_conv')(x) x = layers.BatchNormalization(axis=bn_axis, name='top_bn')(x) x = layers.Activation(activation, name='top_activation')(x) if include_top: x = layers.GlobalAveragePooling2D(name='avg_pool')(x) if dropout_rate and dropout_rate > 0: x = layers.Dropout(dropout_rate, name='top_dropout')(x) x = layers.Dense(classes, activation='softmax', kernel_initializer=DENSE_KERNEL_INITIALIZER, name='probs')(x) else: if pooling == 'avg': x = layers.GlobalAveragePooling2D(name='avg_pool')(x) elif pooling == 'max': x = layers.GlobalMaxPooling2D(name='max_pool')(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = keras_utils.get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = models.Model(inputs, x, name=model_name) # Load weights. if weights == 'imagenet': if include_top: file_name = model_name + '_weights_tf_dim_ordering_tf_kernels_autoaugment.h5' file_hash = WEIGHTS_HASHES[model_name][0] else: file_name = model_name + '_weights_tf_dim_ordering_tf_kernels_autoaugment_notop.h5' file_hash = WEIGHTS_HASHES[model_name][1] weights_path = keras_utils.get_file(file_name, BASE_WEIGHTS_PATH + file_name, cache_subdir='models', file_hash=file_hash) model.load_weights(weights_path) elif weights is not None: model.load_weights(weights) return model
def build(self, hp): version = hp.Choice("version", ["v1", "v2", "next"], default="v2") conv3_depth = hp.Choice("conv3_depth", [4, 8]) conv4_depth = hp.Choice("conv4_depth", [6, 23, 36]) # Version-conditional fixed parameters preact = True if version == "v2" else False use_bias = False if version == "next" else True # Model definition. bn_axis = 3 if backend.image_data_format() == "channels_last" else 1 if self.input_tensor is not None: inputs = tf.keras.utils.get_source_inputs(self.input_tensor) x = self.input_tensor else: inputs = layers.Input(shape=self.input_shape) x = inputs # Initial conv2d block. x = layers.ZeroPadding2D(padding=((3, 3), (3, 3)), name="conv1_pad")(x) x = layers.Conv2D(64, 7, strides=2, use_bias=use_bias, name="conv1_conv")(x) if preact is False: x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name="conv1_bn")(x) x = layers.Activation("relu", name="conv1_relu")(x) x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)), name="pool1_pad")(x) x = layers.MaxPooling2D(3, strides=2, name="pool1_pool")(x) # Middle hypertunable stack. if version == "v1": x = stack1(x, 64, 3, stride1=1, name="conv2") x = stack1(x, 128, conv3_depth, name="conv3") x = stack1(x, 256, conv4_depth, name="conv4") x = stack1(x, 512, 3, name="conv5") elif version == "v2": x = stack2(x, 64, 3, name="conv2") x = stack2(x, 128, conv3_depth, name="conv3") x = stack2(x, 256, conv4_depth, name="conv4") x = stack2(x, 512, 3, stride1=1, name="conv5") elif version == "next": x = stack3(x, 64, 3, name="conv2") x = stack3(x, 256, conv3_depth, name="conv3") x = stack3(x, 512, conv4_depth, name="conv4") x = stack3(x, 1024, 3, stride1=1, name="conv5") # Top of the model. if preact is True: x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name="post_bn")(x) x = layers.Activation("relu", name="post_relu")(x) pooling = hp.Choice("pooling", ["avg", "max"], default="avg") if pooling == "avg": x = layers.GlobalAveragePooling2D(name="avg_pool")(x) elif pooling == "max": x = layers.GlobalMaxPooling2D(name="max_pool")(x) if self.include_top: x = layers.Dense(self.classes, activation="softmax", name="probs")(x) model = keras.Model(inputs, x, name="ResNet") optimizer_name = hp.Choice("optimizer", ["adam", "rmsprop", "sgd"], default="adam") optimizer = keras.optimizers.get(optimizer_name) optimizer.learning_rate = hp.Choice("learning_rate", [0.1, 0.01, 0.001], default=0.01) model.compile( optimizer=optimizer, loss="categorical_crossentropy", metrics=["accuracy"], ) return model else: return keras.Model(inputs, x, name="ResNet")
def __init__(self): super(MaxAvgPool, self).__init__() self.max_pool = layers.GlobalMaxPooling2D() self.avg_pool = layers.GlobalAveragePooling2D()
def QuantizedMobileNetV1(input_shape=None, alpha=1.0, depth_multiplier=1, dropout=1e-3, include_top=True, weights='imagenet', input_tensor=None, pooling=None, classes=1000, batch_size=None, nbits=16, fbits=8, BN_nbits=None, BN_fbits=None, rounding_method='nearest', quant_mode='hybrid', overflow_mode=False, stop_gradient=False, verbose=True, **kwargs): """Instantiates the MobileNet architecture. To load a MobileNet model via `load_model`, import the custom objects `relu6` and pass them to the `custom_objects` parameter. E.g. model = load_model('mobilenet.h5', custom_objects={ 'relu6': mobilenet.relu6}) # Arguments input_shape: optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(224, 224, 3)` (with `channels_last` data format) or (3, 224, 224) (with `channels_first` data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 32. E.g. `(200, 200, 3)` would be one valid value. alpha: controls the width of the network. - If `alpha` < 1.0, proportionally decreases the number of filters in each layer. - If `alpha` > 1.0, proportionally increases the number of filters in each layer. - If `alpha` = 1, default number of filters from the paper are used at each layer. depth_multiplier: depth multiplier for depthwise convolution (also called the resolution multiplier) dropout: dropout rate include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization), 'imagenet' (pre-training on ImageNet), or the path to the weights file to be loaded. input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. 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. RuntimeError: If attempting to run this model with a backend that does not support separable convolutions. """ if verbose: print('\nBuilding model : Quantized MobileNet V1') pbar=tqdm(total=16) if BN_nbits is None: BN_nbits=nbits if BN_fbits is None: BN_fbits=fbits layer_quantizer=build_layer_quantizer(nbits,fbits,rounding_method,overflow_mode,stop_gradient) layer_BN_quantizer=build_layer_quantizer(BN_nbits,BN_fbits,rounding_method,overflow_mode,stop_gradient) if verbose: pbar.set_postfix_str('Preparation') pbar.update() 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 and default size. if input_shape is None: default_size = 224 else: if backend.image_data_format() == 'channels_first': rows = input_shape[1] cols = input_shape[2] else: rows = input_shape[0] cols = input_shape[1] if rows == cols and rows in [128, 160, 192, 224]: default_size = rows else: default_size = 224 input_shape = _obtain_input_shape(input_shape, default_size=default_size, min_size=32, data_format=backend.image_data_format(), require_flatten=include_top, weights=weights) if backend.image_data_format() == 'channels_last': row_axis, col_axis = (0, 1) else: row_axis, col_axis = (1, 2) rows = input_shape[row_axis] cols = input_shape[col_axis] if weights == 'imagenet': if depth_multiplier != 1: raise ValueError('If imagenet weights are being loaded, ' 'depth multiplier must be 1') if alpha not in [0.25, 0.50, 0.75, 1.0]: raise ValueError('If imagenet weights are being loaded, ' 'alpha can be one of' '`0.25`, `0.50`, `0.75` or `1.0` only.') if rows != cols or rows not in [128, 160, 192, 224]: if rows is None: rows = 224 warnings.warn('MobileNet shape is undefined.' ' Weights for input shape ' '(224, 224) will be loaded.') else: raise ValueError('If imagenet weights are being loaded, ' 'input must have a static square shape ' '(one of (128, 128), (160, 160), ' '(192, 192), or (224, 224)). ' 'Input shape provided = %s' % (input_shape,)) if backend.image_data_format() != 'channels_last': warnings.warn('The MobileNet family of models is only available ' 'for the input data format "channels_last" ' '(width, height, channels). ' 'However your settings specify the default ' 'data format "channels_first" (channels, width, height).' ' You should set `image_data_format="channels_last"` ' 'in your Keras config located at ~/.keras/keras.json. ' 'The model being returned right now will expect inputs ' 'to follow the "channels_last" data format.') backend.set_image_data_format('channels_last') old_data_format = 'channels_first' else: old_data_format = None if input_tensor is None: img_input = layers.Input(batch_shape=(batch_size,)+input_shape) else: if not backend.is_keras_tensor(input_tensor): img_input = layers.Input(tensor=input_tensor, batch_shape=(batch_size,)+input_shape) else: img_input = input_tensor if verbose: pbar.set_postfix_str('building standard conv block') x = _conv_block(img_input, 32, alpha, strides=(2, 2), layer_quantizer=layer_quantizer, layer_BN_quantizer=layer_BN_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building depthwise conv block 1') x = _depthwise_conv_block(x, 64, alpha, depth_multiplier, block_id=1, layer_quantizer=layer_quantizer, layer_BN_quantizer=layer_BN_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building depthwise conv block 2') x = _depthwise_conv_block(x, 128, alpha, depth_multiplier, strides=(2, 2), block_id=2, layer_quantizer=layer_quantizer, layer_BN_quantizer=layer_BN_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building depthwise conv block 3') x = _depthwise_conv_block(x, 128, alpha, depth_multiplier, block_id=3, layer_quantizer=layer_quantizer, layer_BN_quantizer=layer_BN_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building depthwise conv block 4') x = _depthwise_conv_block(x, 256, alpha, depth_multiplier, strides=(2, 2), block_id=4, layer_quantizer=layer_quantizer, layer_BN_quantizer=layer_BN_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building depthwise conv block 5') x = _depthwise_conv_block(x, 256, alpha, depth_multiplier, block_id=5, layer_quantizer=layer_quantizer, layer_BN_quantizer=layer_BN_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building depthwise conv block 6') x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, strides=(2, 2), block_id=6, layer_quantizer=layer_quantizer, layer_BN_quantizer=layer_BN_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building depthwise conv block 7') x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=7, layer_quantizer=layer_quantizer, layer_BN_quantizer=layer_BN_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building depthwise conv block 8') x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=8, layer_quantizer=layer_quantizer, layer_BN_quantizer=layer_BN_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building depthwise conv block 9') x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=9, layer_quantizer=layer_quantizer, layer_BN_quantizer=layer_BN_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building depthwise conv block 10') x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=10, layer_quantizer=layer_quantizer, layer_BN_quantizer=layer_BN_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building depthwise conv block 11') x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=11, layer_quantizer=layer_quantizer, layer_BN_quantizer=layer_BN_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building depthwise conv block 12') x = _depthwise_conv_block(x, 1024, alpha, depth_multiplier, strides=(2, 2), block_id=12, layer_quantizer=layer_quantizer, layer_BN_quantizer=layer_BN_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building depthwise conv block 13') x = _depthwise_conv_block(x, 1024, alpha, depth_multiplier, block_id=13, layer_quantizer=layer_quantizer, layer_BN_quantizer=layer_BN_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building output block') if include_top: if backend.image_data_format() == 'channels_first': shape = (int(1024 * alpha), 1, 1) else: shape = (1, 1, int(1024 * alpha)) x = layers.GlobalAveragePooling2D()(x) x = layers.Reshape(shape, name='reshape_1')(x) x = layers.Dropout(dropout, name='dropout')(x) x = QuantizedConv2D(classes, kernel_size=(1, 1), quantizers=layer_quantizer, padding='same', name='conv_preds', quant_mode=quant_mode, last_layer=True)(x) x = layers.Activation('softmax', name='act_softmax')(x) x = layers.Reshape((classes,), name='reshape_2')(x) else: if pooling == 'avg': x = layers.GlobalAveragePooling2D()(x) elif pooling == 'max': x = layers.GlobalMaxPooling2D()(x) if verbose: pbar.update() # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = keras_utils.get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = models.Model(inputs, x, name='quantized_mobilenet_%0.2f_%s' % (alpha, rows)) if verbose: pbar.set_postfix_str('Model Built') pbar.close() # load weights if weights == 'imagenet': if backend.image_data_format() == 'channels_first': raise ValueError('Weights for "channels_first" format ' 'are not available.') if alpha == 1.0: alpha_text = '1_0' elif alpha == 0.75: alpha_text = '7_5' elif alpha == 0.50: alpha_text = '5_0' else: alpha_text = '2_5' if include_top: model_name = 'mobilenet_%s_%d_tf.h5' % (alpha_text, rows) weight_path = BASE_WEIGHT_PATH + model_name weights_path = keras_utils.get_file(model_name, weight_path, cache_subdir='models') else: model_name = 'mobilenet_%s_%d_tf_no_top.h5' % (alpha_text, rows) weight_path = BASE_WEIGHT_PATH + model_name weights_path = keras_utils.get_file(model_name, weight_path, cache_subdir='models') model.load_weights(weights_path) elif weights is not None: model.load_weights(weights) if old_data_format: backend.set_image_data_format(old_data_format) return model
def Xception(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000, **kwargs): """Instantiates the Xception architecture. Optionally loads weights pre-trained on ImageNet. Note that the data format convention used by the model is the one specified in your Keras config at `~/.keras/keras.json`. 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), '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 `(299, 299, 3)`. It should have exactly 3 inputs channels, and width and height should be no smaller than 71. 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 block. - `avg` means that global average pooling will be applied to the output of the last convolutional block, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. classes: optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. # Returns A Keras model instance. # Raises ValueError: in case of invalid argument for `weights`, or invalid input shape. RuntimeError: If attempting to run this model with a backend that does not support separable convolutions. """ if not (weights in {'imagenet', None} or os.path.exists(weights)): raise ValueError('The `weights` argument should be either ' '`None` (random initialization), `imagenet` ' '(pre-training on ImageNet), ' 'or the path to the weights file to be loaded.') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError( 'If using `weights` as `"imagenet"` with `include_top`' ' as true, `classes` should be 1000') if input_tensor is None: img_input = layers.Input(shape=input_shape) else: if not backend.is_keras_tensor(input_tensor): img_input = layers.Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor channel_axis = 1 if backend.image_data_format() == 'channels_first' else -1 x = layers.ZeroPadding2D((1, 1))(img_input) x = layers.Conv2D(32, (3, 3), strides=(2, 2), use_bias=False, name='block1_conv1')(x) x = layers.BatchNormalization(axis=channel_axis, name='block1_conv1_bn')(x) x = layers.Activation('relu', name='block1_conv1_act')(x) x = layers.ZeroPadding2D((1, 1))(x) x = layers.Conv2D(64, (3, 3), use_bias=False, name='block1_conv2')(x) x = layers.BatchNormalization(axis=channel_axis, name='block1_conv2_bn')(x) x = layers.Activation('relu', name='block1_conv2_act')(x) residual = layers.Conv2D(128, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x) residual = layers.BatchNormalization(axis=channel_axis)(residual) x = layers.SeparableConv2D(128, (3, 3), padding='same', use_bias=False, name='block2_sepconv1')(x) x = layers.BatchNormalization(axis=channel_axis, name='block2_sepconv1_bn')(x) x = layers.Activation('relu', name='block2_sepconv2_act')(x) x = layers.SeparableConv2D(128, (3, 3), padding='same', use_bias=False, name='block2_sepconv2')(x) x = layers.BatchNormalization(axis=channel_axis, name='block2_sepconv2_bn')(x) x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block2_pool')(x) x = layers.add([x, residual]) residual = layers.Conv2D(256, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x) residual = layers.BatchNormalization(axis=channel_axis)(residual) x = layers.Activation('relu', name='block3_sepconv1_act')(x) x = layers.SeparableConv2D(256, (3, 3), padding='same', use_bias=False, name='block3_sepconv1')(x) x = layers.BatchNormalization(axis=channel_axis, name='block3_sepconv1_bn')(x) x = layers.Activation('relu', name='block3_sepconv2_act')(x) x = layers.SeparableConv2D(256, (3, 3), padding='same', use_bias=False, name='block3_sepconv2')(x) x = layers.BatchNormalization(axis=channel_axis, name='block3_sepconv2_bn')(x) x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block3_pool')(x) x = layers.add([x, residual]) residual = layers.Conv2D(728, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x) residual = layers.BatchNormalization(axis=channel_axis)(residual) x = layers.Activation('relu', name='block4_sepconv1_act')(x) x = layers.SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name='block4_sepconv1')(x) x = layers.BatchNormalization(axis=channel_axis, name='block4_sepconv1_bn')(x) x = layers.Activation('relu', name='block4_sepconv2_act')(x) x = layers.SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name='block4_sepconv2')(x) x = layers.BatchNormalization(axis=channel_axis, name='block4_sepconv2_bn')(x) x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block4_pool')(x) x = layers.add([x, residual]) for i in range(8): residual = x prefix = 'block' + str(i + 5) x = layers.Activation('relu', name=prefix + '_sepconv1_act')(x) x = layers.SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name=prefix + '_sepconv1')(x) x = layers.BatchNormalization(axis=channel_axis, name=prefix + '_sepconv1_bn')(x) x = layers.Activation('relu', name=prefix + '_sepconv2_act')(x) x = layers.SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name=prefix + '_sepconv2')(x) x = layers.BatchNormalization(axis=channel_axis, name=prefix + '_sepconv2_bn')(x) x = layers.Activation('relu', name=prefix + '_sepconv3_act')(x) x = layers.SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name=prefix + '_sepconv3')(x) x = layers.BatchNormalization(axis=channel_axis, name=prefix + '_sepconv3_bn')(x) x = layers.add([x, residual]) residual = layers.Conv2D(1024, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x) residual = layers.BatchNormalization(axis=channel_axis)(residual) x = layers.Activation('relu', name='block13_sepconv1_act')(x) x = layers.SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name='block13_sepconv1')(x) x = layers.BatchNormalization(axis=channel_axis, name='block13_sepconv1_bn')(x) x = layers.Activation('relu', name='block13_sepconv2_act')(x) x = layers.SeparableConv2D(1024, (3, 3), padding='same', use_bias=False, name='block13_sepconv2')(x) x = layers.BatchNormalization(axis=channel_axis, name='block13_sepconv2_bn')(x) x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block13_pool')(x) x = layers.add([x, residual]) x = layers.SeparableConv2D(1536, (3, 3), padding='same', use_bias=False, name='block14_sepconv1')(x) x = layers.BatchNormalization(axis=channel_axis, name='block14_sepconv1_bn')(x) x = layers.Activation('relu', name='block14_sepconv1_act')(x) x = layers.SeparableConv2D(2048, (3, 3), padding='same', use_bias=False, name='block14_sepconv2')(x) x = layers.BatchNormalization(axis=channel_axis, name='block14_sepconv2_bn')(x) x = layers.Activation('relu', name='block14_sepconv2_act')(x) if include_top: x = layers.GlobalAveragePooling2D(name='avg_pool')(x) x = layers.Dense(classes, activation='softmax', name='predictions')(x) else: if pooling == 'avg': x = layers.GlobalAveragePooling2D()(x) elif pooling == 'max': x = layers.GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = keras_utils.get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = models.Model(inputs, x, name='xception') # Load weights. if weights == 'imagenet': if include_top: weights_path = keras_utils.get_file( 'xception_weights_tf_dim_ordering_tf_kernels.h5', TF_WEIGHTS_PATH, cache_subdir='models', file_hash='0a58e3b7378bc2990ea3b43d5981f1f6') else: weights_path = keras_utils.get_file( 'xception_weights_tf_dim_ordering_tf_kernels_notop.h5', TF_WEIGHTS_PATH_NO_TOP, cache_subdir='models', file_hash='b0042744bf5b25fce3cb969f33bebb97') model.load_weights(weights_path) if backend.backend() == 'theano': keras_utils.convert_all_kernels_in_model(model) elif weights is not None: model.load_weights(weights) return model
- A loss function to train the discriminator. """ from tensorflow.keras import layers # Create the discriminator discriminator = keras.Sequential( [ keras.Input(shape=(28, 28, 1)), layers.Conv2D(64, (3, 3), strides=(2, 2), padding="same"), layers.LeakyReLU(alpha=0.2), layers.Conv2D(128, (3, 3), strides=(2, 2), padding="same"), layers.LeakyReLU(alpha=0.2), layers.GlobalMaxPooling2D(), layers.Dense(1), ], name="discriminator", ) # Create the generator latent_dim = 128 generator = keras.Sequential( [ keras.Input(shape=(latent_dim,)), # We want to generate 128 coefficients to reshape into a 7x7x128 map layers.Dense(7 * 7 * 128), layers.LeakyReLU(alpha=0.2), layers.Reshape((7, 7, 128)), layers.Conv2DTranspose(128, (4, 4), strides=(2, 2), padding="same"),
def EfficientNet( input_shape, block_args_list, global_params, include_top=True, pooling=None ): batch_norm_momentum = global_params.batch_norm_momentum batch_norm_epsilon = global_params.batch_norm_epsilon if global_params.data_format == 'channels_first': channel_axis = 1 else: channel_axis = -1 # Stem part inputs = KL.Input(shape=input_shape) x = inputs x = KL.Conv2D( filters=round_filters(32, global_params), kernel_size=[3, 3], strides=[2, 2], kernel_initializer=ConvKernalInitializer(), padding='same', use_bias=False, )(x) x = KL.BatchNormalization( axis=channel_axis, momentum=batch_norm_momentum, epsilon=batch_norm_epsilon )(x) x = Swish()(x) # Blocks part block_idx = 1 n_blocks = sum([block_args.num_repeat for block_args in block_args_list]) drop_rate = global_params.drop_connect_rate or 0 drop_rate_dx = drop_rate / n_blocks for block_args in block_args_list: assert block_args.num_repeat > 0 # Update block input and output filters based on depth multiplier. block_args = block_args._replace( input_filters=round_filters(block_args.input_filters, global_params), output_filters=round_filters(block_args.output_filters, global_params), num_repeat=round_repeats(block_args.num_repeat, global_params), ) # The first block needs to take care of stride and filter size increase. x = MBConvBlock( block_args, global_params, drop_connect_rate=drop_rate_dx * block_idx )(x) block_idx += 1 if block_args.num_repeat > 1: block_args = block_args._replace( input_filters=block_args.output_filters, strides=[1, 1] ) for _ in xrange(block_args.num_repeat - 1): x = MBConvBlock( block_args, global_params, drop_connect_rate=drop_rate_dx * block_idx )(x) block_idx += 1 # Head part x = KL.Conv2D( filters=round_filters(1280, global_params), kernel_size=[1, 1], strides=[1, 1], kernel_initializer=ConvKernalInitializer(), padding='same', use_bias=False, )(x) x = KL.BatchNormalization( axis=channel_axis, momentum=batch_norm_momentum, epsilon=batch_norm_epsilon )(x) x = Swish()(x) if include_top: x = KL.GlobalAveragePooling2D(data_format=global_params.data_format)(x) if global_params.dropout_rate > 0: x = KL.Dropout(global_params.dropout_rate)(x) x = KL.Dense( global_params.num_classes, kernel_initializer=DenseKernalInitializer() )(x) x = KL.Activation('softmax')(x) else: if pooling == 'avg': x = KL.GlobalAveragePooling2D(data_format=global_params.data_format)(x) elif pooling == 'max': x = KL.GlobalMaxPooling2D(data_format=global_params.data_format)(x) outputs = x model = KM.Model(inputs, outputs) return model
import tensorflow as tf from tensorflow.keras import layers from tensorflow.keras import optimizers from tensorflow.keras import losses from tensorflow.keras import metrics import numpy as np from numpy.random import random_sample # 网络搭建 image_input = tf.keras.Input(shape=(32,32,3), name="img_input") timeseries_input = tf.keras.Input(shape=(20,10), name="ts_input") x1 = layers.Conv2D(3,3)(image_input) x1 = layers.GlobalMaxPooling2D()(x1) x2 = layers.Conv1D(3,3)(timeseries_input) x2 = layers.GlobalMaxPooling1D()(x2) x = layers.concatenate([x1, x2]) score_output = layers.Dense(1, name="score_output")(x) class_output = layers.Dense(5, name="class_output")(x) # 模型构建 model = tf.keras.Model(inputs=[image_input, timeseries_input], outputs=[score_output, class_output]) # 绘制模型数据流图 tf.keras.utils.plot_model(model, "multi_io_model.png", show_shapes=True, dpi=500)
# https://www.tensorflow.org/guide/keras/functional # use the same stack of layers to instantiate two models: # an encoder model that turns image inputs into 16-dimensional vectors, # and an end-to-end autoencoder model for training print('encoder') encoder_input = keras.Input(shape=(28, 28, 1), name='img') x = layers.Conv2D(16, 3, activation='relu')(encoder_input) x = layers.Conv2D(32, 3, activation='relu')(x) x = layers.MaxPooling2D(3)(x) x = layers.Conv2D(32, 3, activation='relu')(x) x = layers.Conv2D(16, 3, activation='relu')(x) # global pull from 16 feature maps # turn image into 16-dimensional vectors encoder_output = layers.GlobalMaxPooling2D()(x) encoder = keras.Model(encoder_input, encoder_output, name='encoder') encoder.summary() print('decoder') x = layers.Reshape((4, 4, 1))(encoder_output) x = layers.Conv2DTranspose(16, 3, activation='relu')(x) x = layers.Conv2DTranspose(32, 3, activation='relu')(x) x = layers.UpSampling2D(3)(x) x = layers.Conv2DTranspose(16, 3, activation='relu')(x) decoder_output = layers.Conv2DTranspose(1, 3, activation='relu')(x) print('autoencoder') autoencoder = keras.Model(encoder_input, decoder_output, name='autoencoder') autoencoder.summary()
def DenseNet_greyscale(blocks,input_shape,pooling,trainable): if blocks == 121: blocks = [6, 12, 24, 16] elif blocks == 169: blocks == [6, 12, 32, 32] elif blocks == 201: blocks == [6, 12, 48, 32] img_input = layers.Input(shape=input_shape) bn_axis = 3 x = layers.ZeroPadding2D(padding=((3, 3), (3, 3)))(img_input) x = layers.Conv2D(64, 7, strides=2, use_bias=False, name='conv1/conv')(x) x = layers.BatchNormalization( axis=bn_axis, epsilon=1.001e-5, name='conv1/bn')(x) x = layers.Activation('relu', name='conv1/relu')(x) x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)))(x) x = layers.MaxPooling2D(3, strides=2, name='pool1')(x) x = dense_block(x, blocks[0], name='conv2') x = transition_block(x, 0.5, name='pool2') x = dense_block(x, blocks[1], name='conv3') x = transition_block(x, 0.5, name='pool3') x = dense_block(x, blocks[2], name='conv4') x = transition_block(x, 0.5, name='pool4') x = dense_block(x, blocks[3], name='conv5') x = layers.BatchNormalization( axis=bn_axis, epsilon=1.001e-5, name='bn')(x) x = layers.Activation('relu', name='relu')(x) if pooling == 'avg': x = layers.GlobalAveragePooling2D(name='avg_pool')(x) elif pooling == 'max': x = layers.GlobalMaxPooling2D(name='max_pool')(x) # Create model. if blocks == [6, 12, 24, 16]: model = models.Model(img_input, x, name='densenet121') elif blocks == [6, 12, 32, 32]: model = models.Model(img_input, x, name='densenet169') elif blocks == [6, 12, 48, 32]: model = models.Model(img_input, x, name='densenet201') # Load weights if blocks == [6, 12, 24, 16]: pretrained_model = DenseNet121(include_top=False,pooling=pooling) elif blocks == [6, 12, 32, 32]: pretrained_model = DenseNet169(include_top=False,pooling=pooling) elif blocks == [6, 12, 48, 32]: pretrained_model = DenseNet201(include_top=False,pooling=pooling) w = pretrained_model.layers[2].get_weights()[0].sum(2,keepdims=True) model.layers[2].set_weights([w]) model.layers[2].trainable = trainable model.trainable = trainable for l1,l2 in zip(model.layers[3:],pretrained_model.layers[3:]): l1.set_weights(l2.get_weights()) l1.trainable = trainable return model #test = DenseNet_greyscale(121,(224,224,1),'max',False)