def MT_Hybrid_CAN(n_frame, nb_filters1, nb_filters2, input_shape_1, input_shape_2, kernel_size_1=(3, 3, 3), kernel_size_2=(3, 3), dropout_rate1=0.25, dropout_rate2=0.5, pool_size_1=(2, 2, 2), pool_size_2=(2, 2), nb_dense=128): diff_input = Input(shape=input_shape_1) rawf_input = Input(shape=input_shape_2) # Motion branch d1 = Conv3D(nb_filters1, kernel_size_1, padding='same', activation='tanh')(diff_input) d2 = Conv3D(nb_filters1, kernel_size_1, activation='tanh')(d1) # App branch r1 = Conv2D(nb_filters1, kernel_size_2, padding='same', activation='tanh')(rawf_input) r2 = Conv2D(nb_filters1, kernel_size_2, activation='tanh')(r1) # Mask from App (g1) * Motion Branch (d2) g1 = Conv2D(1, (1, 1), padding='same', activation='sigmoid')(r2) g1 = Attention_mask()(g1) g1 = K.expand_dims(g1, axis=-1) gated1 = multiply([d2, g1]) # Motion Branch d3 = AveragePooling3D(pool_size_1)(gated1) d4 = Dropout(dropout_rate1)(d3) d5 = Conv3D(nb_filters2, kernel_size_1, padding='same', activation='tanh')(d4) d6 = Conv3D(nb_filters2, kernel_size_1, activation='tanh')(d5) # App branch r3 = AveragePooling2D(pool_size_2)(r2) r4 = Dropout(dropout_rate1)(r3) r5 = Conv2D(nb_filters2, kernel_size_2, padding='same', activation='tanh')(r4) r6 = Conv2D(nb_filters2, kernel_size_2, activation='tanh')(r5) # Mask from App (g2) * Motion Branch (d6) g2 = Conv2D(1, (1, 1), padding='same', activation='sigmoid')(r6) g2 = Attention_mask()(g2) g2 = K.repeat_elements(g2, d6.shape[3], axis=-1) g2 = K.expand_dims(g2, axis=-1) gated2 = multiply([d6, g2]) # Motion Branch d7 = AveragePooling3D(pool_size_1)(gated2) d8 = Dropout(dropout_rate1)(d7) # Motion Branch d9 = Flatten()(d8) d10_y = Dense(nb_dense, activation='tanh')(d9) d11_y = Dropout(dropout_rate2)(d10_y) out_y = Dense(n_frame, name='output_1')(d11_y) d10_r = Dense(nb_dense, activation='tanh')(d9) d11_r = Dropout(dropout_rate2)(d10_r) out_r = Dense(n_frame, name='output_2')(d11_r) model = Model(inputs=[diff_input, rawf_input], outputs=[out_y, out_r]) return model
def _add_discriminator_block(old_model, config): # new shape is double the size of previous one old_input_shape = list(old_model.input.shape) new_input_shape = (old_input_shape[-2] * 2, old_input_shape[-2] * 2, old_input_shape[-1]) model_input = Input(shape=new_input_shape, name="doubled_dis_input") # weights init w_init = RandomNormal(stddev=0.02) w_const = max_norm(1.0) # conv layers x = model_input for strides in [1, 3, 3]: x = Conv2D(config['filters'], strides, padding='same', kernel_initializer=w_init, kernel_constraint=w_const)(x) x = LeakyReLU()(x) x = AveragePooling2D()(x) new_block = x # skip the input, 1x1 and activation for the old model for i in range(config['num_input_layers'], len(old_model.layers)): x = old_model.layers[i](x) # define straight-through model model1 = Model(model_input, x) # compile model model1.compile(loss=wasserstein_loss, optimizer=Adam(lr=config['learning_rate'], beta_1=config['beta_1'], beta_2=config['beta_2'], epsilon=config['epsilon'])) # downsample the new larger image downsample = AveragePooling2D()(model_input) # connect old input processing to downsampled new input old_block = old_model.layers[1](downsample) old_block = old_model.layers[2](old_block) # fade in output of old model input layer with new input x = WeightedSum()([old_block, new_block]) # skip the input, 1x1 and activation for the old model for i in range(config['num_input_layers'], len(old_model.layers)): x = old_model.layers[i](x) # define fade-in model model2 = Model(model_input, x) # compile model model2.compile(loss=wasserstein_loss, optimizer=Adam(lr=config['learning_rate'], beta_1=config['beta_1'], beta_2=config['beta_2'], epsilon=config['epsilon'])) return [model1, model2]
def MTTS_CAN(n_frame, nb_filters1, nb_filters2, input_shape, kernel_size=(3, 3), dropout_rate1=0.25, dropout_rate2=0.5, pool_size=(2, 2), nb_dense=128): diff_input = Input(shape=input_shape) rawf_input = Input(shape=input_shape) d1 = TSM_Cov2D(diff_input, n_frame, nb_filters1, kernel_size, padding='same', activation='tanh') d2 = TSM_Cov2D(d1, n_frame, nb_filters1, kernel_size, padding='valid', activation='tanh') r1 = Conv2D(nb_filters1, kernel_size, padding='same', activation='tanh')(rawf_input) r2 = Conv2D(nb_filters1, kernel_size, activation='tanh')(r1) g1 = Conv2D(1, (1, 1), padding='same', activation='sigmoid')(r2) g1 = Attention_mask()(g1) gated1 = multiply([d2, g1]) d3 = AveragePooling2D(pool_size)(gated1) d4 = Dropout(dropout_rate1)(d3) r3 = AveragePooling2D(pool_size)(r2) r4 = Dropout(dropout_rate1)(r3) d5 = TSM_Cov2D(d4, n_frame, nb_filters2, kernel_size, padding='same', activation='tanh') d6 = TSM_Cov2D(d5, n_frame, nb_filters2, kernel_size, padding='valid', activation='tanh') r5 = Conv2D(nb_filters2, kernel_size, padding='same', activation='tanh')(r4) r6 = Conv2D(nb_filters2, kernel_size, activation='tanh')(r5) g2 = Conv2D(1, (1, 1), padding='same', activation='sigmoid')(r6) g2 = Attention_mask()(g2) gated2 = multiply([d6, g2]) d7 = AveragePooling2D(pool_size)(gated2) d8 = Dropout(dropout_rate1)(d7) d9 = Flatten()(d8) d10_y = Dense(nb_dense, activation='tanh')(d9) d11_y = Dropout(dropout_rate2)(d10_y) out_y = Dense(1, name='output_1')(d11_y) d10_r = Dense(nb_dense, activation='tanh')(d9) d11_r = Dropout(dropout_rate2)(d10_r) out_r = Dense(1, name='output_2')(d11_r) model = Model(inputs=[diff_input, rawf_input], outputs=[out_y, out_r]) return model
def evaluate_on_cifar10(): tf.random.set_seed(42) total_depth = 100 n_blocks = 3 depth = (total_depth - 4) // n_blocks growth_rate = 12 filters = growth_rate * 2 # region Model input_layer = Input(shape=[32, 32, 3]) layer = input_layer layer = Conv2D(filters=filters, kernel_size=3, strides=1, padding="same")(layer) for k in range(n_blocks): layer = DenseBlock2D(kernel_size=3, growth_rate=growth_rate, depth=depth, use_batch_normalization=True)(layer) if k < (n_blocks - 1): filters += growth_rate * depth // 4 layer = transition_block(layer, filters) else: layer = AveragePooling2D(pool_size=8)(layer) layer = Flatten()(layer) layer = Dense(units=10, activation="softmax")(layer) model = Model(inputs=input_layer, outputs=layer) model.summary() model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["acc"]) # endregion # region Data (x_train, y_train), (x_test, y_test) = cifar10.load_data() x_train = x_train.astype(np.float32) / 255.0 x_test = x_test.astype(np.float32) / 255.0 y_train = to_categorical(y_train, num_classes=10) y_test = to_categorical(y_test, num_classes=10) generator = ImageDataGenerator(rotation_range=15, width_shift_range=5. / 32, height_shift_range=5. / 32, horizontal_flip=True) generator.fit(x_train) # endregion log_dir = "../logs/tests/dense_block_cifar10/{}".format(int(time())) log_dir = os.path.normpath(log_dir) tensorboard = TensorBoard(log_dir=log_dir, profile_batch=0) model.fit_generator(generator.flow(x_train, y_train, batch_size=64), steps_per_epoch=100, epochs=300, validation_data=(x_test, y_test), validation_steps=100, verbose=1, callbacks=[tensorboard])
def vgg16_avg(input_shape): img_input = Input(shape=input_shape) # Block 1 x = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='block1_conv1')(img_input) x = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='block1_conv2')(x) x = AveragePooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) # Block 2 x = Convolution2D(128, 3, 3, activation='relu', border_mode='same', name='block2_conv1')(x) x = Convolution2D(128, 3, 3, activation='relu', border_mode='same', name='block2_conv2')(x) x = AveragePooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) # Block 3 x = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='block3_conv1')(x) x = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='block3_conv2')(x) x = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='block3_conv3')(x) x = AveragePooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) # Block 4 x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block4_conv1')(x) x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block4_conv2')(x) x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block4_conv3')(x) x = AveragePooling2D((2, 2), strides=(2, 2), name='block4_pool')(x) # Block 5 x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block5_conv1')(x) x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block5_conv2')(x) x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block5_conv3')(x) x = AveragePooling2D((2, 2), strides=(2, 2), name='block5_pool')(x)
def InceptionV3(include_top=True, weights='imagenet', input_tensor=None, model_input=None, pooling=None, classes=1000, model_path=""): """Instantiates the Inception v3 architecture. Optionally loads weights pre-trained on ImageNet. Note that when using TensorFlow, for best performance you should set `image_data_format='channels_last'` in your Keras config at ~/.keras/keras.json. The model and the weights are compatible with both TensorFlow and Theano. The data format convention used by the model is the one specified in your Keras config file. Note that the default input image size for this model is 299x299. # Arguments include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization) or 'imagenet' (pre-training on ImageNet). input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. input_shape: optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(299, 299, 3)` (with `channels_last` data format) or `(3, 299, 299)` (with `channels_first` data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 139. E.g. `(150, 150, 3)` would be one valid value. pooling: Optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional layer. - `avg` means that global average pooling will be applied to the output of the last convolutional layer, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. classes: optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. # Returns A Keras model instance. # Raises ValueError: in case of invalid argument for `weights`, or invalid input shape. """ if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as imagenet with `include_top`' ' as true, `classes` should be 1000') img_input = model_input channel_axis = 3 x = conv2d_bn(img_input, 32, 3, 3, strides=(2, 2), padding='valid') x = conv2d_bn(x, 32, 3, 3, padding='valid') x = conv2d_bn(x, 64, 3, 3) x = MaxPooling2D((3, 3), strides=(2, 2))(x) x = conv2d_bn(x, 80, 1, 1, padding='valid') x = conv2d_bn(x, 192, 3, 3, padding='valid') x = MaxPooling2D((3, 3), strides=(2, 2))(x) # mixed 0, 1, 2: 35 x 35 x 256 branch1x1 = conv2d_bn(x, 64, 1, 1) branch5x5 = conv2d_bn(x, 48, 1, 1) branch5x5 = conv2d_bn(branch5x5, 64, 5, 5) branch3x3dbl = conv2d_bn(x, 64, 1, 1) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 32, 1, 1) x = layers.concatenate( [branch1x1, branch5x5, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed0') # mixed 1: 35 x 35 x 256 branch1x1 = conv2d_bn(x, 64, 1, 1) branch5x5 = conv2d_bn(x, 48, 1, 1) branch5x5 = conv2d_bn(branch5x5, 64, 5, 5) branch3x3dbl = conv2d_bn(x, 64, 1, 1) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 64, 1, 1) x = layers.concatenate( [branch1x1, branch5x5, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed1') # mixed 2: 35 x 35 x 256 branch1x1 = conv2d_bn(x, 64, 1, 1) branch5x5 = conv2d_bn(x, 48, 1, 1) branch5x5 = conv2d_bn(branch5x5, 64, 5, 5) branch3x3dbl = conv2d_bn(x, 64, 1, 1) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 64, 1, 1) x = layers.concatenate( [branch1x1, branch5x5, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed2') # mixed 3: 17 x 17 x 768 branch3x3 = conv2d_bn(x, 384, 3, 3, strides=(2, 2), padding='valid') branch3x3dbl = conv2d_bn(x, 64, 1, 1) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch3x3dbl = conv2d_bn( branch3x3dbl, 96, 3, 3, strides=(2, 2), padding='valid') branch_pool = MaxPooling2D((3, 3), strides=(2, 2))(x) x = layers.concatenate( [branch3x3, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed3') # mixed 4: 17 x 17 x 768 branch1x1 = conv2d_bn(x, 192, 1, 1) branch7x7 = conv2d_bn(x, 128, 1, 1) branch7x7 = conv2d_bn(branch7x7, 128, 1, 7) branch7x7 = conv2d_bn(branch7x7, 192, 7, 1) branch7x7dbl = conv2d_bn(x, 128, 1, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 1, 7) branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 192, 1, 1) x = layers.concatenate( [branch1x1, branch7x7, branch7x7dbl, branch_pool], axis=channel_axis, name='mixed4') # mixed 5, 6: 17 x 17 x 768 for i in range(2): branch1x1 = conv2d_bn(x, 192, 1, 1) branch7x7 = conv2d_bn(x, 160, 1, 1) branch7x7 = conv2d_bn(branch7x7, 160, 1, 7) branch7x7 = conv2d_bn(branch7x7, 192, 7, 1) branch7x7dbl = conv2d_bn(x, 160, 1, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 1, 7) branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7) branch_pool = AveragePooling2D( (3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 192, 1, 1) x = layers.concatenate( [branch1x1, branch7x7, branch7x7dbl, branch_pool], axis=channel_axis, name='mixed' + str(5 + i)) # mixed 7: 17 x 17 x 768 branch1x1 = conv2d_bn(x, 192, 1, 1) branch7x7 = conv2d_bn(x, 192, 1, 1) branch7x7 = conv2d_bn(branch7x7, 192, 1, 7) branch7x7 = conv2d_bn(branch7x7, 192, 7, 1) branch7x7dbl = conv2d_bn(x, 192, 1, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 192, 1, 1) x = layers.concatenate( [branch1x1, branch7x7, branch7x7dbl, branch_pool], axis=channel_axis, name='mixed7') # mixed 8: 8 x 8 x 1280 branch3x3 = conv2d_bn(x, 192, 1, 1) branch3x3 = conv2d_bn(branch3x3, 320, 3, 3, strides=(2, 2), padding='valid') branch7x7x3 = conv2d_bn(x, 192, 1, 1) branch7x7x3 = conv2d_bn(branch7x7x3, 192, 1, 7) branch7x7x3 = conv2d_bn(branch7x7x3, 192, 7, 1) branch7x7x3 = conv2d_bn( branch7x7x3, 192, 3, 3, strides=(2, 2), padding='valid') branch_pool = MaxPooling2D((3, 3), strides=(2, 2))(x) x = layers.concatenate( [branch3x3, branch7x7x3, branch_pool], axis=channel_axis, name='mixed8') # mixed 9: 8 x 8 x 2048 for i in range(2): branch1x1 = conv2d_bn(x, 320, 1, 1) branch3x3 = conv2d_bn(x, 384, 1, 1) branch3x3_1 = conv2d_bn(branch3x3, 384, 1, 3) branch3x3_2 = conv2d_bn(branch3x3, 384, 3, 1) branch3x3 = layers.concatenate( [branch3x3_1, branch3x3_2], axis=channel_axis, name='mixed9_' + str(i)) branch3x3dbl = conv2d_bn(x, 448, 1, 1) branch3x3dbl = conv2d_bn(branch3x3dbl, 384, 3, 3) branch3x3dbl_1 = conv2d_bn(branch3x3dbl, 384, 1, 3) branch3x3dbl_2 = conv2d_bn(branch3x3dbl, 384, 3, 1) branch3x3dbl = layers.concatenate( [branch3x3dbl_1, branch3x3dbl_2], axis=channel_axis) branch_pool = AveragePooling2D( (3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 192, 1, 1) x = layers.concatenate( [branch1x1, branch3x3, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed' + str(9 + i)) if include_top: # Classification block x = GlobalAveragePooling2D(name='avg_pool')(x) x = Dense(classes, activation='softmax', name='predictions')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. inputs = img_input # Create model. model = Model(inputs, x, name='inception_v3') # load weights if weights == 'imagenet': if K.image_data_format() == 'channels_first': if K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') if include_top: weights_path = model_path model.load_weights(weights_path) else: weights_path = "" elif (weights == "trained"): weights_path = model_path model.load_weights(weights_path) return model
def build(input_shape, num_outputs, block_fn, hp_lambda, repetitions): """Builds a custom ResNet like architecture. Args: input_shape: The input shape in the form (nb_channels, nb_rows, nb_cols) num_outputs: The number of outputs at final softmax layer block_fn: The block function to use. This is either `basic_block` or `bottleneck`. The original paper used basic_block 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 Returns: The keras `Model`. """ _handle_dim_ordering() if len(input_shape) != 3: raise Exception( "Input shape should be a tuple (nb_channels, nb_rows, nb_cols)" ) # Permute dimension order if necessary if K.image_data_format() == 'channels_last': input_shape = (input_shape[1], input_shape[2], input_shape[0]) # Load function from str if needed. block_fn = _get_block(block_fn) input = Input(shape=input_shape) conv1 = _conv_bn_relu(filters=16, kernel_size=(7, 7), strides=(2, 2))(input) pool1 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding="same")(conv1) block = pool1 filters = 16 for i, r in enumerate(repetitions): #block = SpatialDropout2D(rate=0.5)(block) block = _residual_block(block_fn, filters=filters, repetitions=r, is_first_layer=(i == 0))(block) filters *= 2 # Last activation block_output_split = _bn_relu(block) #block = SpatialDropout2D(rate=0.5)(block) # Classifier block class label block_shape = K.int_shape(block_output_split) pool2 = AveragePooling2D(pool_size=(block_shape[ROW_AXIS], block_shape[COL_AXIS]), strides=(1, 1))(block_output_split) flatten1 = Flatten()(pool2) dense_class = Dense(units=num_outputs, kernel_initializer="he_normal", activation="sigmoid")(flatten1) # Classifier block domain label hp_lambda = 0.01 Flip = flipGradient.GradientReversal(hp_lambda) block = Flip(block_output_split) block_shape = K.int_shape(block) pool2 = AveragePooling2D(pool_size=(block_shape[ROW_AXIS], block_shape[COL_AXIS]), strides=(1, 1))(block) flatten1 = Flatten()(pool2) # flatten1 = Dense(units=256, kernel_initializer="he_normal", activation="relu")(flatten1) # flatten1 = Dense(units=256, kernel_initializer="he_normal", activation="relu")(flatten1) dense_domain = Dense(units=num_outputs, kernel_initializer="he_normal", activation="sigmoid")(flatten1) model_combined = Model(inputs=input, outputs=[dense_class, dense_domain]) model_class = Model(inputs=input, outputs=dense_class) model_domain = Model(inputs=input, outputs=dense_domain) return (model_combined, model_class)
def _main(args): config_path = os.path.expanduser(args.config_path) weights_path = os.path.expanduser(args.weights_path) assert config_path.endswith('.cfg'), '{} is not a .cfg file'.format( config_path) assert weights_path.endswith( '.weights'), '{} is not a .weights file'.format(weights_path) output_path = os.path.expanduser(args.output_path) assert output_path.endswith( '.h5'), 'output path {} is not a .h5 file'.format(output_path) output_root = os.path.splitext(output_path)[0] # Load weights and config. print('Loading weights.') weights_file = open(weights_path, 'rb') major, minor, revision = np.ndarray(shape=(3, ), dtype='int32', buffer=weights_file.read(12)) if (major * 10 + minor) >= 2 and major < 1000 and minor < 1000: seen = np.ndarray(shape=(1, ), dtype='int64', buffer=weights_file.read(8)) else: seen = np.ndarray(shape=(1, ), dtype='int32', buffer=weights_file.read(4)) print('Weights Header: ', major, minor, revision, seen) print('Parsing Darknet config.') unique_config_file = unique_config_sections(config_path) cfg_parser = configparser.ConfigParser() cfg_parser.read_file(unique_config_file) print('Creating Keras model.') image_height = int(cfg_parser['net_0']['height']) image_width = int(cfg_parser['net_0']['width']) input_layer = Input(shape=(image_height, image_width, 3)) prev_layer = input_layer print(input_layer.shape) all_layers = [prev_layer] weight_decay = float(cfg_parser['net_0']['decay'] ) if 'net_0' in cfg_parser.sections() else 5e-4 count = 0 out_index = [] for section in cfg_parser.sections(): print('Parsing section {}'.format(section)) if section.startswith('convolutional'): filters = int(cfg_parser[section]['filters']) size = int(cfg_parser[section]['size']) stride = int(cfg_parser[section]['stride']) pad = int(cfg_parser[section]['pad']) activation = cfg_parser[section]['activation'] batch_normalize = 'batch_normalize' in cfg_parser[section] padding = 'same' if pad == 1 and stride == 1 else 'valid' # Setting weights. # Darknet serializes convolutional weights as: # [bias/beta, [gamma, mean, variance], conv_weights] prev_layer_shape = K.int_shape(prev_layer) weights_shape = (size, size, prev_layer_shape[-1], filters) darknet_w_shape = (filters, weights_shape[2], size, size) weights_size = np.product(weights_shape) print('conv2d', 'bn' if batch_normalize else ' ', activation, weights_shape) conv_bias = np.ndarray(shape=(filters, ), dtype='float32', buffer=weights_file.read(filters * 4)) count += filters if batch_normalize: bn_weights = np.ndarray(shape=(3, filters), dtype='float32', buffer=weights_file.read(filters * 12)) count += 3 * filters bn_weight_list = [ bn_weights[0], # scale gamma conv_bias, # shift beta bn_weights[1], # running mean bn_weights[2] # running var ] conv_weights = np.ndarray(shape=darknet_w_shape, dtype='float32', buffer=weights_file.read(weights_size * 4)) count += weights_size # DarkNet conv_weights are serialized Caffe-style: # (out_dim, in_dim, height, width) # We would like to set these to Tensorflow order: # (height, width, in_dim, out_dim) conv_weights = np.transpose(conv_weights, [2, 3, 1, 0]) conv_weights = [conv_weights] if batch_normalize else [ conv_weights, conv_bias ] # Handle activation. act_fn = None if activation == 'leaky': pass # Add advanced activation later. elif activation != 'linear': raise ValueError( 'Unknown activation function `{}` in section {}'.format( activation, section)) # Create Conv2D layer if stride > 1: # Darknet uses left and top padding instead of 'same' mode prev_layer = ZeroPadding2D(((1, 0), (1, 0)))(prev_layer) conv_layer = (Conv2D(filters, (size, size), strides=(stride, stride), kernel_regularizer=l2(weight_decay), use_bias=not batch_normalize, weights=conv_weights, activation=act_fn, padding=padding))(prev_layer) if batch_normalize: conv_layer = (BatchNormalization( weights=bn_weight_list))(conv_layer) prev_layer = conv_layer print(prev_layer.shape) if activation == 'linear': all_layers.append(prev_layer) elif activation == 'leaky': act_layer = LeakyReLU(alpha=0.1)(prev_layer) prev_layer = act_layer all_layers.append(act_layer) elif section.startswith('route'): ids = [int(i) for i in cfg_parser[section]['layers'].split(',')] layers = [all_layers[i] for i in ids] if len(layers) > 1: print('Concatenating route layers:', layers) concatenate_layer = Concatenate()(layers) all_layers.append(concatenate_layer) prev_layer = concatenate_layer else: skip_layer = layers[0] # only one layer to route all_layers.append(skip_layer) prev_layer = skip_layer elif section.startswith('maxpool'): size = int(cfg_parser[section]['size']) stride = int(cfg_parser[section]['stride']) all_layers.append( MaxPooling2D(pool_size=(size, size), strides=(stride, stride), padding='same')(prev_layer)) prev_layer = all_layers[-1] elif section.startswith('avgpool'): if cfg_parser.items(section): raise ValueError('{} with params unsupported.'.format(section)) all_layers.append( AveragePooling2D(pool_size=(prev_layer.shape[1], prev_layer.shape[2]))(prev_layer)) prev_layer = all_layers[-1] elif section.startswith('shortcut'): index = int(cfg_parser[section]['from']) activation = cfg_parser[section]['activation'] assert activation == 'linear', 'Only linear activation supported.' all_layers.append(Add()([all_layers[index], prev_layer])) prev_layer = all_layers[-1] elif section.startswith('upsample'): stride = int(cfg_parser[section]['stride']) assert stride == 2, 'Only stride=2 supported.' all_layers.append(UpSampling2D(stride)(prev_layer)) prev_layer = all_layers[-1] elif section.startswith('yolo'): out_index.append(len(all_layers) - 1) all_layers.append(None) prev_layer = all_layers[-1] # elif section.startswith('net'): # pass elif (section.startswith('net') or section.startswith('cost') or section.startswith('softmax')): pass else: raise ValueError( 'Unsupported section header type: {}'.format(section)) # Create and save model. if len(out_index) == 0: out_index.append(len(all_layers) - 1) model = Model(inputs=input_layer, outputs=[all_layers[i] for i in out_index]) print(model.summary()) if args.weights_only: model.save_weights('{}'.format(output_path)) print('Saved Keras weights to {}'.format(output_path)) else: model.save('{}'.format(output_path)) print('Saved Keras model to {}'.format(output_path)) # Check to see if all weights have been read. remaining_weights = len(weights_file.read()) / 4 weights_file.close() print('Read {} of {} from Darknet weights.'.format( count, count + remaining_weights)) if remaining_weights > 0: print('Warning: {} unused weights'.format(remaining_weights)) if args.plot_model: plot(model, to_file='{}.png'.format(output_root), show_shapes=True) print('Saved model plot to {}.png'.format(output_root))
def EEGNet(nb_classes, Chans = 64, Samples = 128, dropoutRate = 0.5, kernLength = 64, F1 = 8, D = 2, F2 = 16, norm_rate = 0.25, dropoutType = 'Dropout'): """ Keras Implementation of EEGNet http://iopscience.iop.org/article/10.1088/1741-2552/aace8c/meta Note that this implements the newest version of EEGNet and NOT the earlier version (version v1 and v2 on arxiv). We strongly recommend using this architecture as it performs much better and has nicer properties than our earlier version. For example: 1. Depthwise Convolutions to learn spatial filters within a temporal convolution. The use of the depth_multiplier option maps exactly to the number of spatial filters learned within a temporal filter. This matches the setup of algorithms like FBCSP which learn spatial filters within each filter in a filter-bank. This also limits the number of free parameters to fit when compared to a fully-connected convolution. 2. Separable Convolutions to learn how to optimally combine spatial filters across temporal bands. Separable Convolutions are Depthwise Convolutions followed by (1x1) Pointwise Convolutions. While the original paper used Dropout, we found that SpatialDropout2D sometimes produced slightly better results for classification of ERP signals. However, SpatialDropout2D significantly reduced performance on the Oscillatory dataset (SMR, BCI-IV Dataset 2A). We recommend using the default Dropout in most cases. Assumes the input signal is sampled at 128Hz. If you want to use this model for any other sampling rate you will need to modify the lengths of temporal kernels and average pooling size in blocks 1 and 2 as needed (double the kernel lengths for double the sampling rate, etc). Note that we haven't tested the model performance with this rule so this may not work well. The model with default parameters gives the EEGNet-8,2 model as discussed in the paper. This model should do pretty well in general, although it is advised to do some model searching to get optimal performance on your particular dataset. We set F2 = F1 * D (number of input filters = number of output filters) for the SeparableConv2D layer. We haven't extensively tested other values of this parameter (say, F2 < F1 * D for compressed learning, and F2 > F1 * D for overcomplete). We believe the main parameters to focus on are F1 and D. Inputs: nb_classes : int, number of classes to classify Chans, Samples : number of channels and time points in the EEG data dropoutRate : dropout fraction kernLength : length of temporal convolution in first layer. We found that setting this to be half the sampling rate worked well in practice. For the SMR dataset in particular since the data was high-passed at 4Hz we used a kernel length of 32. F1, F2 : number of temporal filters (F1) and number of pointwise filters (F2) to learn. Default: F1 = 8, F2 = F1 * D. D : number of spatial filters to learn within each temporal convolution. Default: D = 2 dropoutType : Either SpatialDropout2D or Dropout, passed as a string. """ if dropoutType == 'SpatialDropout2D': dropoutType = SpatialDropout2D elif dropoutType == 'Dropout': dropoutType = Dropout else: raise ValueError('dropoutType must be one of SpatialDropout2D ' 'or Dropout, passed as a string.') input1 = Input(shape = (1, Chans, Samples)) ################################################################## block1 = Conv2D(F1, (1, kernLength), padding = 'same', input_shape = (1, Chans, Samples), use_bias = False)(input1) block1 = BatchNormalization(axis = 1)(block1) block1 = DepthwiseConv2D((Chans, 1), use_bias = False, depth_multiplier = D, depthwise_constraint = max_norm(1.))(block1) block1 = BatchNormalization(axis = 1)(block1) block1 = Activation('elu')(block1) block1 = AveragePooling2D((1, 4))(block1) block1 = dropoutType(dropoutRate)(block1) block2 = SeparableConv2D(F2, (1, 16), use_bias = False, padding = 'same')(block1) block2 = BatchNormalization(axis = 1)(block2) block2 = Activation('elu')(block2) block2 = AveragePooling2D((1, 8))(block2) block2 = dropoutType(dropoutRate)(block2) flatten = Flatten(name = 'flatten')(block2) dense = Dense(nb_classes, name = 'dense', kernel_constraint = max_norm(norm_rate))(flatten) softmax = Activation('softmax', name = 'softmax')(dense) return Model(inputs=input1, outputs=softmax)
def resnet_v2(input_shape, depth, num_classes=10, fused_batch_norm=False): """ResNet Version 2 Model builder [b] Stacks of (1 x 1)-(3 x 3)-(1 x 1) BN-ReLU-Conv2D or also known as bottleneck layer First shortcut connection per layer is 1 x 1 Conv2D. Second and onwards shortcut connection is identity. At the beginning of each stage, the feature map size is halved (downsampled) by a convolutional layer with strides=2, while the number of filter maps is doubled. Within each stage, the layers have the same number filters and the same filter map sizes. Features maps sizes: conv1 : 32x32, 16 stage 0: 32x32, 64 stage 1: 16x16, 128 stage 2: 8x8, 256 # Arguments input_shape (tensor): shape of input image tensor depth (int): number of core convolutional layers num_classes (int): number of classes (CIFAR10 has 10) # Returns model (Model): Keras model instance """ if (depth - 2) % 9 != 0: raise ValueError('depth should be 9n+2 (eg 56 or 110 in [b])') # Start model definition. num_filters_in = 16 num_res_blocks = int((depth - 2) / 9) inputs = Input(shape=input_shape) # v2 performs Conv2D with BN-ReLU on input before splitting into 2 paths x = resnet_layer(inputs=inputs, num_filters=num_filters_in, conv_first=True, fused_batch_norm=fused_batch_norm) # Instantiate the stack of residual units for stage in range(3): for res_block in range(num_res_blocks): activation = 'relu' batch_normalization = True strides = 1 if stage == 0: num_filters_out = num_filters_in * 4 if res_block == 0: # first layer and first stage activation = None batch_normalization = False else: num_filters_out = num_filters_in * 2 if res_block == 0: # first layer but not first stage strides = 2 # downsample # bottleneck residual unit y = resnet_layer(inputs=x, num_filters=num_filters_in, kernel_size=1, strides=strides, activation=activation, batch_normalization=batch_normalization, conv_first=False) y = resnet_layer(inputs=y, num_filters=num_filters_in, conv_first=False) y = resnet_layer(inputs=y, num_filters=num_filters_out, kernel_size=1, conv_first=False) if res_block == 0: # linear projection residual shortcut connection to match # changed dims x = resnet_layer(inputs=x, num_filters=num_filters_out, kernel_size=1, strides=strides, activation=None, batch_normalization=False) x = keras.layers.add([x, y]) num_filters_in = num_filters_out # Add classifier on top. # v2 has BN-ReLU before Pooling x = BatchNormalization(fused=fused_batch_norm)(x) x = Activation('relu')(x) x = AveragePooling2D(pool_size=8)(x) y = Flatten()(x) outputs = Dense(num_classes, activation='softmax', kernel_initializer='he_normal')(y) # Instantiate model. model = Model(inputs=inputs, outputs=outputs) return model
def ResNet50(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000): """Instantiates the ResNet50 architecture. Optionally loads weights pre-trained on ImageNet. Note that when using TensorFlow, for best performance you should set `image_data_format='channels_last'` in your Keras config at ~/.keras/keras.json. The model and the weights are compatible with both TensorFlow and Theano. The data format convention used by the model is the one specified in your Keras config file. # Arguments include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization), '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 197. E.g. `(200, 200, 3)` would be one valid value. pooling: Optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional layer. - `avg` means that global average pooling will be applied to the output of the last convolutional layer, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. classes: optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. # Returns A Keras model instance. # Raises ValueError: in case of invalid argument for `weights`, or invalid input shape. """ if 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=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 = ZeroPadding2D(padding=(3, 3), name='conv1_pad')(img_input) x = Conv2D(64, (7, 7), strides=(2, 2), padding='valid', name='conv1')(x) x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x) x = Activation('relu')(x) x = MaxPooling2D((3, 3), strides=(2, 2))(x) x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1)) x = identity_block(x, 3, [64, 64, 256], stage=2, block='b') x = identity_block(x, 3, [64, 64, 256], stage=2, block='c') x = conv_block(x, 3, [128, 128, 512], stage=3, block='a') x = identity_block(x, 3, [128, 128, 512], stage=3, block='b') x = identity_block(x, 3, [128, 128, 512], stage=3, block='c') x = identity_block(x, 3, [128, 128, 512], stage=3, block='d') x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f') x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c') x = AveragePooling2D((7, 7), name='avg_pool')(x) if include_top: x = Flatten()(x) x = Dense(classes, activation='softmax', name='fc1000')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x, name='resnet50') # load weights if weights == 'imagenet': if include_top: weights_path = get_file( 'resnet50_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH, cache_subdir='models', md5_hash='a7b3fe01876f51b976af0dea6bc144eb') else: weights_path = get_file( 'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models', md5_hash='a268eb855778b3df3c7506639542a6af') model.load_weights(weights_path) if K.backend() == 'theano': layer_utils.convert_all_kernels_in_model(model) elif weights is not None: model.load_weights(weights) return model
Dropout) from tensorflow.python.keras.callbacks import (EarlyStopping, ModelCheckpoint, LearningRateScheduler) import tensorflow as tf tf.flags.DEFINE_integer('batch_size', 8, 'batch size, default: 4') tf.flags.DEFINE_integer('img_size', 224, 'square images acquired') tf.flags.DEFINE_integer('epochs', 50, 'epochs, default: 10') FLAGS = tf.flags.FLAGS model = Xception(input_shape=(FLAGS.img_size, FLAGS.img_size, 3), include_top=False, weights='imagenet') x = model.output x = AveragePooling2D(pool_size=(2, 2))(x) x = Dense(32, activation='relu')(x) x = Dropout(0.1)(x) x = Flatten()(x) x = Dense(2, activation='softmax', kernel_regularizer=l2(.0005))(x) model = Model(inputs=model.inputs, outputs=x) opt = SGD(lr=0.0001, momentum=.9) model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy']) train_datagen = ImageDataGenerator(rescale=1. / 255, rotation_range=15, width_shift_range=0.1, height_shift_range=0.1,
activation='relu' ) ) model.add(MaxPooling2D(pool_size=(2,2))) model.add(Dropout(0.25)) # layer2 - conv model.add( Conv2D( filters=40, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu' ) ) model.add(AveragePooling2D(pool_size=(2,2))) model.add(Dropout(0.25)) # Fully connection layer # ---------------------------------------- model.add(Flatten()) model.add(Dense(units=512, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(units=10, activation='softmax')) startTime = time.time() model.compile( optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'] )
def EEGNet_SSVEP(nb_classes, Chans=64, Samples=128, regRate=0.0001, dropoutRate=0.25, kernLength=64, numFilters=8): """ Keras Implementation of the variant of EEGNet that was used to classify signals from an SSVEP task (https://arxiv.org/abs/1803.04566) Inputs: nb_classes : int, number of classes to classify Chans, Samples : number of channels and time points in the EEG data regRate : regularization parameter for L1 and L2 penalties dropoutRate : dropout fraction kernLength : length of temporal convolution in first layer numFilters : number of temporal-spatial filter pairs to learn """ input1 = Input(shape=(1, Chans, Samples)) ################################################################## layer1 = Conv2D(numFilters, (1, kernLength), padding='same', kernel_regularizer=l1_l2(l1=0.0, l2=0.0), input_shape=(1, Chans, Samples), use_bias=False)(input1) layer1 = BatchNormalization(axis=1)(layer1) layer1 = DepthwiseConv2D((Chans, 1), depthwise_regularizer=l1_l2(l1=regRate, l2=regRate), use_bias=False)(layer1) layer1 = BatchNormalization(axis=1)(layer1) layer1 = Activation('elu')(layer1) layer1 = SpatialDropout2D(dropoutRate)(layer1) layer2 = SeparableConv2D(numFilters, (1, 8), depthwise_regularizer=l1_l2(l1=0.0, l2=regRate), use_bias=False, padding='same')(layer1) layer2 = BatchNormalization(axis=1)(layer2) layer2 = Activation('elu')(layer2) layer2 = AveragePooling2D((1, 4))(layer2) layer2 = SpatialDropout2D(dropoutRate)(layer2) layer3 = SeparableConv2D(numFilters * 2, (1, 8), depth_multiplier=2, depthwise_regularizer=l1_l2(l1=0.0, l2=regRate), use_bias=False, padding='same')(layer2) layer3 = BatchNormalization(axis=1)(layer3) layer3 = Activation('elu')(layer3) layer3 = AveragePooling2D((1, 4))(layer3) layer3 = SpatialDropout2D(dropoutRate)(layer3) flatten = Flatten(name='flatten')(layer3) dense = Dense(nb_classes, name='dense')(flatten) softmax = Activation('softmax', name='softmax')(dense) return Model(inputs=input1, outputs=softmax)
def network_model(input_shape, input_name, num_classes): X_input = Input(shape=input_shape, name=input_name) # Stage 1 X = Conv2D(64, (7, 7), strides=(1, 1), padding='same', name='conv1', kernel_initializer=glorot_uniform(seed=0))(X_input) X = BatchNormalization(axis=3, name='bn_conv1')(X) X = Activation('relu')(X) X = MaxPooling2D((3, 3), strides=(1, 1), padding='same')(X) # Stage 2 X = convolutional_block(X, f=3, filters=[64, 64, 256], stage=2, block='a', s=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') # Stage 3 X = convolutional_block(X, f=3, filters=[128, 128, 512], stage=3, block='a', s=2) 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') # Stage 4 X = convolutional_block(X, f=3, filters=[256, 256, 1024], stage=4, block='a', s=2) 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') # Stage 5 X = convolutional_block(X, f=3, filters=[512, 512, 2048], stage=5, block='a', s=2) X = identity_block(X, 3, [512, 512, 2048], stage=5, block='b') X = identity_block(X, 3, [512, 512, 2048], stage=5, block='c') # Average pooling X = AveragePooling2D(pool_size=(7, 7), strides=(1, 1), name='avg_pool')(X) # output layer X = Flatten()(X) X = Dense(num_classes, activation='softmax', name='fc' + str(num_classes), kernel_initializer=glorot_uniform(seed=0))(X) model = Model(inputs=X_input, outputs=X, name='Resnet50') return model
def loadModel(): myInput = Input(shape=(96, 96, 3)) x = ZeroPadding2D(padding=(3, 3), input_shape=(96, 96, 3))(myInput) x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(x) x = BatchNormalization(axis=3, epsilon=0.00001, name='bn1')(x) x = Activation('relu')(x) x = ZeroPadding2D(padding=(1, 1))(x) x = MaxPooling2D(pool_size=3, strides=2)(x) x = Lambda(lambda x: tf.nn.lrn(x, alpha=1e-4, beta=0.75), name='lrn_1')(x) x = Conv2D(64, (1, 1), name='conv2')(x) x = BatchNormalization(axis=3, epsilon=0.00001, name='bn2')(x) x = Activation('relu')(x) x = ZeroPadding2D(padding=(1, 1))(x) x = Conv2D(192, (3, 3), name='conv3')(x) x = BatchNormalization(axis=3, epsilon=0.00001, name='bn3')(x) x = Activation('relu')(x) x = Lambda(lambda x: tf.nn.lrn(x, alpha=1e-4, beta=0.75), name='lrn_2')(x) x = ZeroPadding2D(padding=(1, 1))(x) x = MaxPooling2D(pool_size=3, strides=2)(x) # Inception3a inception_3a_3x3 = Conv2D(96, (1, 1), name='inception_3a_3x3_conv1')(x) inception_3a_3x3 = BatchNormalization( axis=3, epsilon=0.00001, name='inception_3a_3x3_bn1')(inception_3a_3x3) inception_3a_3x3 = Activation('relu')(inception_3a_3x3) inception_3a_3x3 = ZeroPadding2D(padding=(1, 1))(inception_3a_3x3) inception_3a_3x3 = Conv2D(128, (3, 3), name='inception_3a_3x3_conv2')(inception_3a_3x3) inception_3a_3x3 = BatchNormalization( axis=3, epsilon=0.00001, name='inception_3a_3x3_bn2')(inception_3a_3x3) inception_3a_3x3 = Activation('relu')(inception_3a_3x3) inception_3a_5x5 = Conv2D(16, (1, 1), name='inception_3a_5x5_conv1')(x) inception_3a_5x5 = BatchNormalization( axis=3, epsilon=0.00001, name='inception_3a_5x5_bn1')(inception_3a_5x5) inception_3a_5x5 = Activation('relu')(inception_3a_5x5) inception_3a_5x5 = ZeroPadding2D(padding=(2, 2))(inception_3a_5x5) inception_3a_5x5 = Conv2D(32, (5, 5), name='inception_3a_5x5_conv2')(inception_3a_5x5) inception_3a_5x5 = BatchNormalization( axis=3, epsilon=0.00001, name='inception_3a_5x5_bn2')(inception_3a_5x5) inception_3a_5x5 = Activation('relu')(inception_3a_5x5) inception_3a_pool = MaxPooling2D(pool_size=3, strides=2)(x) inception_3a_pool = Conv2D( 32, (1, 1), name='inception_3a_pool_conv')(inception_3a_pool) inception_3a_pool = BatchNormalization( axis=3, epsilon=0.00001, name='inception_3a_pool_bn')(inception_3a_pool) inception_3a_pool = Activation('relu')(inception_3a_pool) inception_3a_pool = ZeroPadding2D(padding=((3, 4), (3, 4)))(inception_3a_pool) inception_3a_1x1 = Conv2D(64, (1, 1), name='inception_3a_1x1_conv')(x) inception_3a_1x1 = BatchNormalization( axis=3, epsilon=0.00001, name='inception_3a_1x1_bn')(inception_3a_1x1) inception_3a_1x1 = Activation('relu')(inception_3a_1x1) inception_3a = concatenate([ inception_3a_3x3, inception_3a_5x5, inception_3a_pool, inception_3a_1x1 ], axis=3) # Inception3b inception_3b_3x3 = Conv2D(96, (1, 1), name='inception_3b_3x3_conv1')(inception_3a) inception_3b_3x3 = BatchNormalization( axis=3, epsilon=0.00001, name='inception_3b_3x3_bn1')(inception_3b_3x3) inception_3b_3x3 = Activation('relu')(inception_3b_3x3) inception_3b_3x3 = ZeroPadding2D(padding=(1, 1))(inception_3b_3x3) inception_3b_3x3 = Conv2D(128, (3, 3), name='inception_3b_3x3_conv2')(inception_3b_3x3) inception_3b_3x3 = BatchNormalization( axis=3, epsilon=0.00001, name='inception_3b_3x3_bn2')(inception_3b_3x3) inception_3b_3x3 = Activation('relu')(inception_3b_3x3) inception_3b_5x5 = Conv2D(32, (1, 1), name='inception_3b_5x5_conv1')(inception_3a) inception_3b_5x5 = BatchNormalization( axis=3, epsilon=0.00001, name='inception_3b_5x5_bn1')(inception_3b_5x5) inception_3b_5x5 = Activation('relu')(inception_3b_5x5) inception_3b_5x5 = ZeroPadding2D(padding=(2, 2))(inception_3b_5x5) inception_3b_5x5 = Conv2D(64, (5, 5), name='inception_3b_5x5_conv2')(inception_3b_5x5) inception_3b_5x5 = BatchNormalization( axis=3, epsilon=0.00001, name='inception_3b_5x5_bn2')(inception_3b_5x5) inception_3b_5x5 = Activation('relu')(inception_3b_5x5) inception_3b_pool = Lambda(lambda x: x**2, name='power2_3b')(inception_3a) inception_3b_pool = AveragePooling2D(pool_size=(3, 3), strides=(3, 3))(inception_3b_pool) inception_3b_pool = Lambda(lambda x: x * 9, name='mult9_3b')(inception_3b_pool) inception_3b_pool = Lambda(lambda x: K.sqrt(x), name='sqrt_3b')(inception_3b_pool) inception_3b_pool = Conv2D( 64, (1, 1), name='inception_3b_pool_conv')(inception_3b_pool) inception_3b_pool = BatchNormalization( axis=3, epsilon=0.00001, name='inception_3b_pool_bn')(inception_3b_pool) inception_3b_pool = Activation('relu')(inception_3b_pool) inception_3b_pool = ZeroPadding2D(padding=(4, 4))(inception_3b_pool) inception_3b_1x1 = Conv2D(64, (1, 1), name='inception_3b_1x1_conv')(inception_3a) inception_3b_1x1 = BatchNormalization( axis=3, epsilon=0.00001, name='inception_3b_1x1_bn')(inception_3b_1x1) inception_3b_1x1 = Activation('relu')(inception_3b_1x1) inception_3b = concatenate([ inception_3b_3x3, inception_3b_5x5, inception_3b_pool, inception_3b_1x1 ], axis=3) # Inception3c inception_3c_3x3 = Conv2D(128, (1, 1), strides=(1, 1), name='inception_3c_3x3_conv1')(inception_3b) inception_3c_3x3 = BatchNormalization( axis=3, epsilon=0.00001, name='inception_3c_3x3_bn1')(inception_3c_3x3) inception_3c_3x3 = Activation('relu')(inception_3c_3x3) inception_3c_3x3 = ZeroPadding2D(padding=(1, 1))(inception_3c_3x3) inception_3c_3x3 = Conv2D(256, (3, 3), strides=(2, 2), name='inception_3c_3x3_conv' + '2')(inception_3c_3x3) inception_3c_3x3 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3c_3x3_bn' + '2')(inception_3c_3x3) inception_3c_3x3 = Activation('relu')(inception_3c_3x3) inception_3c_5x5 = Conv2D(32, (1, 1), strides=(1, 1), name='inception_3c_5x5_conv1')(inception_3b) inception_3c_5x5 = BatchNormalization( axis=3, epsilon=0.00001, name='inception_3c_5x5_bn1')(inception_3c_5x5) inception_3c_5x5 = Activation('relu')(inception_3c_5x5) inception_3c_5x5 = ZeroPadding2D(padding=(2, 2))(inception_3c_5x5) inception_3c_5x5 = Conv2D(64, (5, 5), strides=(2, 2), name='inception_3c_5x5_conv' + '2')(inception_3c_5x5) inception_3c_5x5 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3c_5x5_bn' + '2')(inception_3c_5x5) inception_3c_5x5 = Activation('relu')(inception_3c_5x5) inception_3c_pool = MaxPooling2D(pool_size=3, strides=2)(inception_3b) inception_3c_pool = ZeroPadding2D(padding=((0, 1), (0, 1)))(inception_3c_pool) inception_3c = concatenate( [inception_3c_3x3, inception_3c_5x5, inception_3c_pool], axis=3) # inception 4a inception_4a_3x3 = Conv2D(96, (1, 1), strides=(1, 1), name='inception_4a_3x3_conv' + '1')(inception_3c) inception_4a_3x3 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_4a_3x3_bn' + '1')(inception_4a_3x3) inception_4a_3x3 = Activation('relu')(inception_4a_3x3) inception_4a_3x3 = ZeroPadding2D(padding=(1, 1))(inception_4a_3x3) inception_4a_3x3 = Conv2D(192, (3, 3), strides=(1, 1), name='inception_4a_3x3_conv' + '2')(inception_4a_3x3) inception_4a_3x3 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_4a_3x3_bn' + '2')(inception_4a_3x3) inception_4a_3x3 = Activation('relu')(inception_4a_3x3) inception_4a_5x5 = Conv2D(32, (1, 1), strides=(1, 1), name='inception_4a_5x5_conv1')(inception_3c) inception_4a_5x5 = BatchNormalization( axis=3, epsilon=0.00001, name='inception_4a_5x5_bn1')(inception_4a_5x5) inception_4a_5x5 = Activation('relu')(inception_4a_5x5) inception_4a_5x5 = ZeroPadding2D(padding=(2, 2))(inception_4a_5x5) inception_4a_5x5 = Conv2D(64, (5, 5), strides=(1, 1), name='inception_4a_5x5_conv' + '2')(inception_4a_5x5) inception_4a_5x5 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_4a_5x5_bn' + '2')(inception_4a_5x5) inception_4a_5x5 = Activation('relu')(inception_4a_5x5) inception_4a_pool = Lambda(lambda x: x**2, name='power2_4a')(inception_3c) inception_4a_pool = AveragePooling2D(pool_size=(3, 3), strides=(3, 3))(inception_4a_pool) inception_4a_pool = Lambda(lambda x: x * 9, name='mult9_4a')(inception_4a_pool) inception_4a_pool = Lambda(lambda x: K.sqrt(x), name='sqrt_4a')(inception_4a_pool) inception_4a_pool = Conv2D(128, (1, 1), strides=(1, 1), name='inception_4a_pool_conv' + '')(inception_4a_pool) inception_4a_pool = BatchNormalization(axis=3, epsilon=0.00001, name='inception_4a_pool_bn' + '')(inception_4a_pool) inception_4a_pool = Activation('relu')(inception_4a_pool) inception_4a_pool = ZeroPadding2D(padding=(2, 2))(inception_4a_pool) inception_4a_1x1 = Conv2D(256, (1, 1), strides=(1, 1), name='inception_4a_1x1_conv' + '')(inception_3c) inception_4a_1x1 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_4a_1x1_bn' + '')(inception_4a_1x1) inception_4a_1x1 = Activation('relu')(inception_4a_1x1) inception_4a = concatenate([ inception_4a_3x3, inception_4a_5x5, inception_4a_pool, inception_4a_1x1 ], axis=3) # inception4e inception_4e_3x3 = Conv2D(160, (1, 1), strides=(1, 1), name='inception_4e_3x3_conv' + '1')(inception_4a) inception_4e_3x3 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_4e_3x3_bn' + '1')(inception_4e_3x3) inception_4e_3x3 = Activation('relu')(inception_4e_3x3) inception_4e_3x3 = ZeroPadding2D(padding=(1, 1))(inception_4e_3x3) inception_4e_3x3 = Conv2D(256, (3, 3), strides=(2, 2), name='inception_4e_3x3_conv' + '2')(inception_4e_3x3) inception_4e_3x3 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_4e_3x3_bn' + '2')(inception_4e_3x3) inception_4e_3x3 = Activation('relu')(inception_4e_3x3) inception_4e_5x5 = Conv2D(64, (1, 1), strides=(1, 1), name='inception_4e_5x5_conv' + '1')(inception_4a) inception_4e_5x5 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_4e_5x5_bn' + '1')(inception_4e_5x5) inception_4e_5x5 = Activation('relu')(inception_4e_5x5) inception_4e_5x5 = ZeroPadding2D(padding=(2, 2))(inception_4e_5x5) inception_4e_5x5 = Conv2D(128, (5, 5), strides=(2, 2), name='inception_4e_5x5_conv' + '2')(inception_4e_5x5) inception_4e_5x5 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_4e_5x5_bn' + '2')(inception_4e_5x5) inception_4e_5x5 = Activation('relu')(inception_4e_5x5) inception_4e_pool = MaxPooling2D(pool_size=3, strides=2)(inception_4a) inception_4e_pool = ZeroPadding2D(padding=((0, 1), (0, 1)))(inception_4e_pool) inception_4e = concatenate( [inception_4e_3x3, inception_4e_5x5, inception_4e_pool], axis=3) # inception5a inception_5a_3x3 = Conv2D(96, (1, 1), strides=(1, 1), name='inception_5a_3x3_conv' + '1')(inception_4e) inception_5a_3x3 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_5a_3x3_bn' + '1')(inception_5a_3x3) inception_5a_3x3 = Activation('relu')(inception_5a_3x3) inception_5a_3x3 = ZeroPadding2D(padding=(1, 1))(inception_5a_3x3) inception_5a_3x3 = Conv2D(384, (3, 3), strides=(1, 1), name='inception_5a_3x3_conv' + '2')(inception_5a_3x3) inception_5a_3x3 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_5a_3x3_bn' + '2')(inception_5a_3x3) inception_5a_3x3 = Activation('relu')(inception_5a_3x3) inception_5a_pool = Lambda(lambda x: x**2, name='power2_5a')(inception_4e) inception_5a_pool = AveragePooling2D(pool_size=(3, 3), strides=(3, 3))(inception_5a_pool) inception_5a_pool = Lambda(lambda x: x * 9, name='mult9_5a')(inception_5a_pool) inception_5a_pool = Lambda(lambda x: K.sqrt(x), name='sqrt_5a')(inception_5a_pool) inception_5a_pool = Conv2D(96, (1, 1), strides=(1, 1), name='inception_5a_pool_conv' + '')(inception_5a_pool) inception_5a_pool = BatchNormalization(axis=3, epsilon=0.00001, name='inception_5a_pool_bn' + '')(inception_5a_pool) inception_5a_pool = Activation('relu')(inception_5a_pool) inception_5a_pool = ZeroPadding2D(padding=(1, 1))(inception_5a_pool) inception_5a_1x1 = Conv2D(256, (1, 1), strides=(1, 1), name='inception_5a_1x1_conv' + '')(inception_4e) inception_5a_1x1 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_5a_1x1_bn' + '')(inception_5a_1x1) inception_5a_1x1 = Activation('relu')(inception_5a_1x1) inception_5a = concatenate( [inception_5a_3x3, inception_5a_pool, inception_5a_1x1], axis=3) # inception_5b inception_5b_3x3 = Conv2D(96, (1, 1), strides=(1, 1), name='inception_5b_3x3_conv' + '1')(inception_5a) inception_5b_3x3 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_5b_3x3_bn' + '1')(inception_5b_3x3) inception_5b_3x3 = Activation('relu')(inception_5b_3x3) inception_5b_3x3 = ZeroPadding2D(padding=(1, 1))(inception_5b_3x3) inception_5b_3x3 = Conv2D(384, (3, 3), strides=(1, 1), name='inception_5b_3x3_conv' + '2')(inception_5b_3x3) inception_5b_3x3 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_5b_3x3_bn' + '2')(inception_5b_3x3) inception_5b_3x3 = Activation('relu')(inception_5b_3x3) inception_5b_pool = MaxPooling2D(pool_size=3, strides=2)(inception_5a) inception_5b_pool = Conv2D(96, (1, 1), strides=(1, 1), name='inception_5b_pool_conv' + '')(inception_5b_pool) inception_5b_pool = BatchNormalization(axis=3, epsilon=0.00001, name='inception_5b_pool_bn' + '')(inception_5b_pool) inception_5b_pool = Activation('relu')(inception_5b_pool) inception_5b_pool = ZeroPadding2D(padding=(1, 1))(inception_5b_pool) inception_5b_1x1 = Conv2D(256, (1, 1), strides=(1, 1), name='inception_5b_1x1_conv' + '')(inception_5a) inception_5b_1x1 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_5b_1x1_bn' + '')(inception_5b_1x1) inception_5b_1x1 = Activation('relu')(inception_5b_1x1) inception_5b = concatenate( [inception_5b_3x3, inception_5b_pool, inception_5b_1x1], axis=3) av_pool = AveragePooling2D(pool_size=(3, 3), strides=(1, 1))(inception_5b) reshape_layer = Flatten()(av_pool) dense_layer = Dense(128, name='dense_layer')(reshape_layer) norm_layer = Lambda(lambda x: tf.math.l2_normalize(x, axis=1), name='norm_layer')(dense_layer) # Final Model model = Model(inputs=[myInput], outputs=norm_layer) home = str(Path.home()) if not os.path.isfile(home + '/.deepface/weights/openface_weights.h5'): print("openface_weights.h5 will be downloaded...") url = 'https://drive.google.com/uc?id=1LSe1YCV1x-BfNnfb7DFZTNpv_Q9jITxn' output = home + '/.deepface/weights/openface_weights.h5' gdown.download(url, output, quiet=False) model.load_weights(home + '/.deepface/weights/openface_weights.h5') return model
def InceptionResNetV2(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000): """Instantiates the Inception-ResNet v2 architecture. Optionally loads weights pre-trained on ImageNet. Note that when using TensorFlow, for best performance you should set `"image_data_format": "channels_last"` in your Keras config at `~/.keras/keras.json`. The model and the weights are compatible with TensorFlow, Theano and CNTK backends. The data format convention used by the model is the one specified in your Keras config file. Note that the default input image size for this model is 299x299, instead of 224x224 as in the VGG16 and ResNet models. Also, the input preprocessing function is different (i.e., do not use `imagenet_utils.preprocess_input()` with this model. Use `preprocess_input()` defined in this module instead). Arguments: include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization), '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)` (with `'channels_last'` data format) or `(3, 299, 299)` (with `'channels_first'` data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 139. E.g. `(150, 150, 3)` would be one valid value. pooling: Optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional layer. - `'avg'` means that global average pooling will be applied to the output of the last convolutional layer, and thus the output of the model will be a 2D tensor. - `'max'` means that global max pooling will be applied. classes: optional number of classes to classify images into, only to be specified if `include_top` is `True`, and if no `weights` argument is specified. Returns: A Keras `Model` instance. Raises: ValueError: in case of invalid argument for `weights`, or invalid input shape. """ if 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=299, min_size=139, data_format=K.image_data_format(), require_flatten=False, weights=weights) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor # Stem block: 35 x 35 x 192 x = conv2d_bn(img_input, 32, 3, strides=2, padding='valid') x = conv2d_bn(x, 32, 3, padding='valid') x = conv2d_bn(x, 64, 3) x = MaxPooling2D(3, strides=2)(x) x = conv2d_bn(x, 80, 1, padding='valid') x = conv2d_bn(x, 192, 3, padding='valid') x = MaxPooling2D(3, strides=2)(x) # Mixed 5b (Inception-A block): 35 x 35 x 320 branch_0 = conv2d_bn(x, 96, 1) branch_1 = conv2d_bn(x, 48, 1) branch_1 = conv2d_bn(branch_1, 64, 5) branch_2 = conv2d_bn(x, 64, 1) branch_2 = conv2d_bn(branch_2, 96, 3) branch_2 = conv2d_bn(branch_2, 96, 3) branch_pool = AveragePooling2D(3, strides=1, padding='same')(x) branch_pool = conv2d_bn(branch_pool, 64, 1) branches = [branch_0, branch_1, branch_2, branch_pool] channel_axis = 1 if K.image_data_format() == 'channels_first' else 3 x = Concatenate(axis=channel_axis, name='mixed_5b')(branches) # 10x block35 (Inception-ResNet-A block): 35 x 35 x 320 for block_idx in range(1, 11): x = inception_resnet_block( x, scale=0.17, block_type='block35', block_idx=block_idx) # Mixed 6a (Reduction-A block): 17 x 17 x 1088 branch_0 = conv2d_bn(x, 384, 3, strides=2, padding='valid') branch_1 = conv2d_bn(x, 256, 1) branch_1 = conv2d_bn(branch_1, 256, 3) branch_1 = conv2d_bn(branch_1, 384, 3, strides=2, padding='valid') branch_pool = MaxPooling2D(3, strides=2, padding='valid')(x) branches = [branch_0, branch_1, branch_pool] x = Concatenate(axis=channel_axis, name='mixed_6a')(branches) # 20x block17 (Inception-ResNet-B block): 17 x 17 x 1088 for block_idx in range(1, 21): x = inception_resnet_block( x, scale=0.1, block_type='block17', block_idx=block_idx) # Mixed 7a (Reduction-B block): 8 x 8 x 2080 branch_0 = conv2d_bn(x, 256, 1) branch_0 = conv2d_bn(branch_0, 384, 3, strides=2, padding='valid') branch_1 = conv2d_bn(x, 256, 1) branch_1 = conv2d_bn(branch_1, 288, 3, strides=2, padding='valid') branch_2 = conv2d_bn(x, 256, 1) branch_2 = conv2d_bn(branch_2, 288, 3) branch_2 = conv2d_bn(branch_2, 320, 3, strides=2, padding='valid') branch_pool = MaxPooling2D(3, strides=2, padding='valid')(x) branches = [branch_0, branch_1, branch_2, branch_pool] x = Concatenate(axis=channel_axis, name='mixed_7a')(branches) # 10x block8 (Inception-ResNet-C block): 8 x 8 x 2080 for block_idx in range(1, 10): x = inception_resnet_block( x, scale=0.2, block_type='block8', block_idx=block_idx) x = inception_resnet_block( x, scale=1., activation=None, block_type='block8', block_idx=10) # Final convolution block: 8 x 8 x 1536 x = conv2d_bn(x, 1536, 1, name='conv_7b') if include_top: # Classification block x = GlobalAveragePooling2D(name='avg_pool')(x) x = Dense(classes, activation='softmax', name='predictions')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor` if input_tensor is not None: inputs = layer_utils.get_source_inputs(input_tensor) else: inputs = img_input # Create model model = Model(inputs, x, name='inception_resnet_v2') # Load weights if weights == 'imagenet': if include_top: fname = 'inception_resnet_v2_weights_tf_dim_ordering_tf_kernels.h5' weights_path = get_file( fname, BASE_WEIGHT_URL + fname, cache_subdir='models', file_hash='e693bd0210a403b3192acc6073ad2e96') else: fname = 'inception_resnet_v2_weights_tf_dim_ordering_tf_kernels_notop.h5' weights_path = get_file( fname, BASE_WEIGHT_URL + fname, cache_subdir='models', file_hash='d19885ff4a710c122648d3b5c3b684e4') model.load_weights(weights_path) elif weights is not None: model.load_weights(weights) return model
def EEGNet_SSVEP(nb_classes=12, Chans=8, Samples=256, dropoutRate=0.5, kernLength=256, F1=96, D=1, F2=96, dropoutType='Dropout'): """ SSVEP Variant of EEGNet, as used in [1]. Inputs: nb_classes : int, number of classes to classify Chans, Samples : number of channels and time points in the EEG data dropoutRate : dropout fraction kernLength : length of temporal convolution in first layer F1, F2 : number of temporal filters (F1) and number of pointwise filters (F2) to learn. D : number of spatial filters to learn within each temporal convolution. dropoutType : Either SpatialDropout2D or Dropout, passed as a string. [1]. Waytowich, N. et. al. (2018). Compact Convolutional Neural Networks for Classification of Asynchronous Steady-State Visual Evoked Potentials. Journal of Neural Engineering vol. 15(6). http://iopscience.iop.org/article/10.1088/1741-2552/aae5d8 """ if dropoutType == 'SpatialDropout2D': dropoutType = SpatialDropout2D elif dropoutType == 'Dropout': dropoutType = Dropout else: raise ValueError('dropoutType must be one of SpatialDropout2D ' 'or Dropout, passed as a string.') input1 = Input(shape=(1, Chans, Samples)) ################################################################## block1 = Conv2D(F1, (1, kernLength), padding='same', input_shape=(1, Chans, Samples), use_bias=False)(input1) block1 = BatchNormalization(axis=1)(block1) block1 = DepthwiseConv2D((Chans, 1), use_bias=False, depth_multiplier=D, depthwise_constraint=max_norm(1.))(block1) block1 = BatchNormalization(axis=1)(block1) block1 = Activation('elu')(block1) block1 = AveragePooling2D((1, 4))(block1) block1 = dropoutType(dropoutRate)(block1) block2 = SeparableConv2D(F2, (1, 16), use_bias=False, padding='same')(block1) block2 = BatchNormalization(axis=1)(block2) block2 = Activation('elu')(block2) block2 = AveragePooling2D((1, 8))(block2) block2 = dropoutType(dropoutRate)(block2) flatten = Flatten(name='flatten')(block2) dense = Dense(nb_classes, name='dense')(flatten) softmax = Activation('softmax', name='softmax')(dense) return Model(inputs=input1, outputs=softmax)