def conv_block(input_tensor, kernel_size, filters, stage, block, strides=(2, 2), trainable=True): nb_filter1, nb_filter2, nb_filter3 = filters if K.image_data_format() == 'channels_last': bn_axis = 3 else: bn_axis = 1 conv_name_base = 'res' + str(stage) + block + '_branch' bn_name_base = 'bn' + str(stage) + block + '_branch' x = Convolution2D(nb_filter1, (1, 1), strides=strides, name=conv_name_base + '2a', trainable=trainable)(input_tensor) x = FixedBatchNormalization(axis=bn_axis, name=bn_name_base + '2a')(x) x = Activation('relu')(x) x = Convolution2D(nb_filter2, (kernel_size, kernel_size), padding='same', name=conv_name_base + '2b', trainable=trainable)(x) x = FixedBatchNormalization(axis=bn_axis, name=bn_name_base + '2b')(x) x = Activation('relu')(x) x = Convolution2D(nb_filter3, (1, 1), name=conv_name_base + '2c', trainable=trainable)(x) x = FixedBatchNormalization(axis=bn_axis, name=bn_name_base + '2c')(x) shortcut = Convolution2D(nb_filter3, (1, 1), strides=strides, name=conv_name_base + '1', trainable=trainable)(input_tensor) shortcut = FixedBatchNormalization(axis=bn_axis, name=bn_name_base + '1')(shortcut) x = Add()([x, shortcut]) x = Activation('relu')(x) return x
def identity_block(input_tensor, kernel_size, filters, stage, block, trainable=True): ''' The identity_block is the block that has no conv layer at shortcut # Arguments input_tensor: input tensor kernel_size: defualt 3, the kernel size of middle conv layer at main path filters: list of integers, the nb_filters of 3 conv layer at main path stage: integer, current stage label, used for generating layer names block: 'a','b'..., current block label, used for generating layer names ''' nb_filter1, nb_filter2, nb_filter3 = filters # because dim_ordering == 'tf' bn_axis = 3 conv_name_base = 'res' + str(stage) + block + '_branch' bn_name_base = 'bn' + str(stage) + block + '_branch' x = Convolution2D(nb_filter1, (1, 1), name=conv_name_base + '2a', trainable=trainable)(input_tensor) x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2a')(x) x = Activation('relu', name=conv_name_base + '2a_relu')(x) x = Convolution2D(nb_filter2, (kernel_size, kernel_size), padding='same', name=conv_name_base + '2b', trainable=trainable)(x) x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2b')(x) x = Activation('relu', name=conv_name_base + '2b_relu')(x) x = Convolution2D(nb_filter3, (1, 1), name=conv_name_base + '2c', trainable=trainable)(x) x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2c')(x) x = Add()([x, input_tensor]) x = Activation('relu', name='res' + str(stage) + block + '_relu')(x) return x
def create_standalone_nvidia_cnn(activation='linear', input_shape=(60, 180, 3), output_shape=1): """ Activation: linear, softmax. Architecture is from nvidia paper mentioned in https://github.com/tanelp/self-driving-convnet/blob/master/train.py """ from tensorflow.keras.layers import Convolution2D from tensorflow.keras.regularizers import l2 from tensorflow.keras.layers import Dense from tensorflow.keras.layers import Flatten from tensorflow.keras.layers import Input from tensorflow.keras.models import Model from tensorflow.keras.optimizers import Adam from tensorflow.keras.losses import mean_squared_error, mean_absolute_error inputs = Input(shape=input_shape) conv_1 = Convolution2D(24, kernel_size=(5, 5), kernel_regularizer=l2(0.0005), strides=(2, 2), padding="same", activation="elu")(inputs) conv_2 = Convolution2D(36, kernel_size=(5, 5), kernel_regularizer=l2(0.0005), strides=(2, 2), padding="same", activation="elu")(conv_1) conv_3 = Convolution2D(48, kernel_size=(5, 5), kernel_regularizer=l2(0.0005), strides=(2, 2), padding="same", activation="elu")(conv_2) conv_4 = Convolution2D(64, kernel_size=(3, 3), kernel_regularizer=l2(0.0005), padding="same", activation="elu")(conv_3) conv_5 = Convolution2D(64, kernel_size=(3, 3), kernel_regularizer=l2(0.0005), padding="same", activation="elu")(conv_4) flatten = Flatten()(conv_5) dense_1 = Dense(1164, kernel_regularizer=l2(0.0005), activation="elu")(flatten) dense_2 = Dense(100, kernel_regularizer=l2(0.0005), activation="elu")(dense_1) dense_3 = Dense(50, kernel_regularizer=l2(0.0005), activation="elu")(dense_2) dense_4 = Dense(10, kernel_regularizer=l2(0.0005), activation="elu")(dense_3) out_dense = Dense(output_shape, activation=activation)(dense_4) model = Model(inputs=inputs, outputs=out_dense) optimizer = Adam(lr=3e-4) model.compile(loss=mean_absolute_error, optimizer=optimizer) return model
def buildModel(): model = Sequential() model.add(Convolution2D(filters=nbFiltre, kernel_size=kernelSize, strides=stridesConv, input_shape=(imageSize, imageSize, 3), activation=activationConvolution)) model.add(MaxPooling2D(pool_size=sizeMatricePool, strides=stridesPool)) nbrLayer = 1 multiplicateur = 1 for i in range(nbrPoolConv - 1): model.add(Convolution2D(filters=nbFiltre * multiplicateur, kernel_size=kernelSize, strides=stridesConv, activation=activationConvolution)) model.add(MaxPooling2D(pool_size=sizeMatricePool, strides=stridesPool)) nbrLayer += 1 multiplicateur = math.floor((nbrLayer / 2) + 1) model.add(Flatten()) for j in range(nbrHiddenLayer): model.add(Dense(units=neuroneCoucheCacher, activation=fctActivation)) ##Couche de sortie model.add(Dropout(rateDropOut)) model.add(Dense(units=outUnit, activation="sigmoid")) model.compile(optimizer=optimizer, loss=fctLoss, metrics=allMetrics) return model
def create_model(): strategy = tf.distribute.MirroredStrategy() with strategy.scope(): shape = (48, 96, 1) filters = 32 depth = 8 stacks = 1 input_img = Input(shape) model = MaskedConvolution2D(filters=filters, kernel_size=(7, 7), padding='same', mask='A')(input_img) # model = InitialBlock(filters)(input_img) model = ResidualBlockList(filters, depth, stacks)(model) for _ in range(2): model = Convolution2D(filters, (1, 1), padding='valid')(model) model = ReLU()(model) outs = Convolution2D(1, (1, 1), padding='valid')(model) outs = Activation('sigmoid')(outs) model = Model(input_img, outs) model.compile(optimizer=Nadam(), loss='binary_crossentropy', metrics=['accuracy']) model.summary() return model
def rnn_lstm(seq_length=3, num_outputs=2, input_shape=(120, 160, 3)): # add sequence length dimensions as keras time-distributed expects shape # of (num_samples, seq_length, input_shape) img_seq_shape = (seq_length,) + input_shape img_in = Input(batch_shape=img_seq_shape, name='img_in') drop_out = 0.3 x = Sequential() x.add(TD(Convolution2D(24, (5,5), strides=(2,2), activation='relu'), input_shape=img_seq_shape)) x.add(TD(Dropout(drop_out))) x.add(TD(Convolution2D(32, (5, 5), strides=(2, 2), activation='relu'))) x.add(TD(Dropout(drop_out))) x.add(TD(Convolution2D(32, (3, 3), strides=(2, 2), activation='relu'))) x.add(TD(Dropout(drop_out))) x.add(TD(Convolution2D(32, (3, 3), strides=(1, 1), activation='relu'))) x.add(TD(Dropout(drop_out))) x.add(TD(MaxPooling2D(pool_size=(2, 2)))) x.add(TD(Flatten(name='flattened'))) x.add(TD(Dense(100, activation='relu'))) x.add(TD(Dropout(drop_out))) x.add(LSTM(128, return_sequences=True, name="LSTM_seq")) x.add(Dropout(.1)) x.add(LSTM(128, return_sequences=False, name="LSTM_fin")) x.add(Dropout(.1)) x.add(Dense(128, activation='relu')) x.add(Dropout(.1)) x.add(Dense(64, activation='relu')) x.add(Dense(10, activation='relu')) x.add(Dense(num_outputs, activation='linear', name='model_outputs')) return x
def conv_block(input_tensor,kernel_size,filters,stage,block,strides=(2,2)): filters1, filters2 = filters if backend.image_data_format() == 'channels_last': bn_axis = 3 else: bn_axis = 1 conv_name_base = 'res' + str(stage) + block + '_branch' bn_name_base = 'bn' + str(stage) + block + '_branch' x = Convolution2D(filters1,(1,1),strides=strides, kernel_initializer='he_normal', name = conv_name_base + '2a')(input_tensor) x = BatchNormalization(bn_axis,name=bn_name_base + '2a')(x) x = Activation('relu')(x) x = Convolution2D(filters2, kernel_size, padding='same', kernel_initializer='he_normal', name = conv_name_base + '2b')(x) x = BatchNormalization(axis=bn_axis,name=bn_name_base+'2b')(x) x = Activation('relu')(x) shortcut = Convolution2D(filters2,(1,1),strides=strides, kernel_initializer='he_normal', name=conv_name_base+'1')(input_tensor) shortcut = BatchNormalization( axis=bn_axis,name=bn_name_base+'1')(shortcut) x = tf.keras.layers.add([x,shortcut]) x = Activation('relu')(x) return x
def conv_block(x, stage, branch, nb_filter, dropout_rate=None, weight_decay=1e-4): '''Apply BatchNorm, Relu, bottleneck 1x1 Conv2D, 3x3 Conv2D, and option dropout # Arguments x: input tensor stage: index for dense block branch: layer index within each dense block nb_filter: number of filters dropout_rate: dropout rate weight_decay: weight decay factor ''' eps = 1.1e-5 conv_name_base = 'conv' + str(stage) + '_' + str(branch) relu_name_base = 'relu' + str(stage) + '_' + str(branch) # 1x1 Convolution (Bottleneck layer) inter_channel = nb_filter * 4 x = BatchNormalization(epsilon=eps, axis=concat_axis, name=conv_name_base+'_x1_bn')(x) x = Scale(axis=concat_axis, name=conv_name_base+'_x1_scale')(x) x = Activation('relu', name=relu_name_base+'_x1')(x) x = Convolution2D(inter_channel, (1, 1), name=conv_name_base+'_x1', use_bias=False)(x) if dropout_rate: x = Dropout(dropout_rate)(x) # 3x3 Convolution x = BatchNormalization(epsilon=eps, axis=concat_axis, name=conv_name_base+'_x2_bn')(x) x = Scale(axis=concat_axis, name=conv_name_base+'_x2_scale')(x) x = Activation('relu', name=relu_name_base+'_x2')(x) x = ZeroPadding2D((1, 1), name=conv_name_base+'_x2_zeropadding')(x) x = Convolution2D(nb_filter, (3, 3), name=conv_name_base+'_x2', use_bias=False)(x) if dropout_rate: x = Dropout(dropout_rate)(x) return x
def learnConcatRealImagBlock(I, filter_size, featmaps, stage, block, convArgs, bnArgs, d): """Learn initial imaginary component for input.""" conv_name_base = 'res' + str(stage) + block + '_branch' bn_name_base = 'bn' + str(stage) + block + '_branch' O = BatchNormalization(name=bn_name_base + '2a', **bnArgs)(I) O = Spline()(tf.concat([O, tf.zeros_like(O)], axis=-1)) #Activation(d.act)(O) O = Convolution2D(featmaps[0], filter_size, name=conv_name_base + '2a', padding='same', kernel_initializer='he_normal', use_bias=False, kernel_regularizer=l2(0.0001))(O) O = BatchNormalization(name=bn_name_base + '2b', **bnArgs)(O) O = Spline()(O) #Activation(d.act)(O) O = Convolution2D(featmaps[1], filter_size, name=conv_name_base + '2b', padding='same', kernel_initializer='he_normal', use_bias=False, kernel_regularizer=l2(0.0001))(O) return O
def dueling_dqn(self, input_shape, action_size, learning_rate): """ Define and return the TF.Keras model, compiled. The model consists of two branches: The state_value NN and the action_advantage NN leading into one merged state_action_value model for which the loss is calculated and backprobragated. """ state_input = Input(shape=(input_shape)) #CNN for image recognition, flatten at the end to form the input for both branches. x = Convolution2D(32, 8, 4, activation='relu')(state_input) #orig: 32,8,8,strides=(4,4) x = Convolution2D(64, 4, 2, activation='relu')(x) #orig: 64,4,4,strides=(2, 2) x = Convolution2D(64, 3, 3, activation='relu')(x) x = Flatten()(x) # state value tower - V state_value = Dense(256, activation='relu')(x) state_value = Dense(1)(state_value) state_value = Lambda(lambda s: K.expand_dims(s[:, 0], axis=-1), output_shape=(action_size,))(state_value) # action advantage tower - A action_advantage = Dense(256, activation='relu')(x) action_advantage = Dense(action_size)(action_advantage) action_advantage = Lambda(lambda a: a[:, :] - K.mean(a[:, :], keepdims=True), output_shape=(action_size,))(action_advantage) # merge to state-action value function Q state_action_value = tf.keras.layers.add([state_value, action_advantage]) #create a Model instance with the imput(state_input) and output(Q-function) model = Model(inputs=state_input, outputs=state_action_value) model.compile(loss='mse',optimizer=Adam(lr=learning_rate)) print(model.summary()) return model
def wide_basic(incoming, nb_in_filters, nb_out_filters, dropout=None, subsample=(2, 2)): nb_bottleneck_filter = nb_out_filters if nb_in_filters == nb_out_filters: # conv3x3 y = BatchNormalization(axis=1)(incoming) y = Activation('relu')(y) y = ZeroPadding2D((1, 1))(y) y = Convolution2D(nb_bottleneck_filter, kernel_size=3, strides=subsample, padding='valid')(y) # conv3x3 y = BatchNormalization(axis=1)(y) y = Activation('relu')(y) if dropout is not None: y = Dropout(dropout)(y) y = ZeroPadding2D((1, 1))(y) y = Convolution2D(nb_bottleneck_filter, kernel_size=3, strides=(1, 1), padding='valid')(y) return Add()([incoming, y]) else: # Residual Units for increasing dimensions # common BN, ReLU shortcut = BatchNormalization(axis=1)(incoming) shortcut = Activation('relu')(shortcut) # conv3x3 y = ZeroPadding2D((1, 1))(shortcut) y = Convolution2D(nb_bottleneck_filter, kernel_size=3, strides=subsample, padding='valid')(y) # conv3x3 y = BatchNormalization(axis=1)(y) y = Activation('relu')(y) if dropout is not None: y = Dropout(dropout)(y) y = ZeroPadding2D((1, 1))(y) y = Convolution2D(nb_out_filters, kernel_size=3, strides=(1, 1), padding='valid')(y) # shortcut shortcut = Convolution2D(nb_out_filters, kernel_size=1, strides=subsample, padding='same')(shortcut) return Add()([shortcut, y])
def expand_conv(init, base, k, strides=(1, 1), reg=None): x = tfa.layers.SpectralNormalization( Convolution2D(base * k, (3, 3), padding='same', strides=strides, kernel_initializer='he_normal', kernel_regularizer=reg, use_bias=False))(init) x = Activation('swish')(x) x = tfa.layers.SpectralNormalization( Convolution2D(base * k, (3, 3), padding='same', kernel_initializer='he_normal', kernel_regularizer=reg, use_bias=False))(x) skip = tfa.layers.SpectralNormalization( Convolution2D(base * k, (1, 1), padding='same', strides=strides, kernel_initializer='he_normal', kernel_regularizer=reg, use_bias=False))(init) m = Add()([x, skip]) return m
def Getmodel_tensorflow_normal(nb_classes): # nb_classes = len(charset) img_rows, img_cols = 23, 23 nb_filters = 16 nb_pool = 2 nb_conv = 3 model = Sequential() model.add(Convolution2D(nb_filters, nb_conv, nb_conv, border_mode='valid', input_shape=(img_rows, img_cols,1))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool))) model.add(Convolution2D(nb_filters, nb_conv, nb_conv)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool))) model.add(Flatten()) model.add(Dense(256)) model.add(Dropout(0.5)) model.add(Activation('relu')) model.add(Dense(nb_classes)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy']) return model
def Getmodel_tensorflow_light(nb_classes): # nb_classes = len(charset) img_rows, img_cols = 23, 23 # number of convolutional filters to use nb_filters = 8 # size of pooling area for max pooling nb_pool = 2 # convolution kernel size nb_conv = 3 # x = np.load('x.npy') # y = np_utils.to_categorical(range(3062)*45*5*2, nb_classes) # weight = ((type_class - np.arange(type_class)) / type_class + 1) ** 3 # weight = dict(zip(range(3063), weight / weight.mean())) # 调整权重,高频字优先 model = Sequential() model.add(Convolution2D(nb_filters, nb_conv, nb_conv, border_mode='valid', input_shape=(img_rows, img_cols,1))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool))) model.add(Convolution2D(nb_filters, nb_conv*2, nb_conv*2)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool))) model.add(Flatten()) model.add(Dense(32)) # model.add(Dropout(0.25)) model.add(Activation('relu')) model.add(Dense(nb_classes)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) return model
def get_model_cnn( input_shape=(None, None, 1), n_classes=10, ): inputs = Input(input_shape) x = Convolution2D(64, kernel_size=3, activation="relu")(inputs) x = MaxPooling2D(pool_size=2)(x) x = Convolution2D(64, kernel_size=3, activation="relu")(x) x = MaxPooling2D(pool_size=2)(x) x = Convolution2D(64, kernel_size=3, activation="relu")(x) x = GlobalMaxPooling2D()(x) out1 = Dense(2, activation="linear")(x) out = Dense(10, activation="relu")(out1) out = Dense(n_classes, activation="softmax")(out) model = Model(inputs, out) model.compile(optimizer=Adam(0.0001), loss=categorical_crossentropy, metrics=["acc"]) model_aux = Model(inputs, out1) model_aux.compile(optimizer=Adam(0.0001), loss=categorical_crossentropy, metrics=["acc"]) model.summary() return model, model_aux
def make_discriminator(): """Creates a discriminator model that takes an image as input and outputs a single value, representing whether the input is real or generated. Unlike normal GANs, the output is not sigmoid and does not represent a probability! Instead, the output should be as large and negative as possible for generated inputs and as large and positive as possible for real inputs. Note that the improved WGAN paper suggests that BatchNormalization should not be used in the discriminator.""" model = Sequential() if K.image_data_format() == 'channels_first': model.add( Convolution2D(64, (5, 5), padding='same', input_shape=(1, 28, 28))) else: model.add( Convolution2D(64, (5, 5), padding='same', input_shape=(28, 28, 1))) model.add(LeakyReLU()) model.add( Convolution2D(128, (5, 5), kernel_initializer='he_normal', strides=[2, 2])) model.add(LeakyReLU()) model.add( Convolution2D(128, (5, 5), kernel_initializer='he_normal', padding='same', strides=[2, 2])) model.add(LeakyReLU()) model.add(Flatten()) model.add(Dense(1024, kernel_initializer='he_normal')) model.add(LeakyReLU()) model.add(Dense(1, kernel_initializer='he_normal')) return model
def create_model(input_shape, config, is_training=True): weight_decay = 0.001 model = Sequential() model.add( Convolution2D(32, 7, 7, W_regularizer=l2(weight_decay), activation="relu", input_shape=input_shape)) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add( Convolution2D(64, 5, 5, W_regularizer=l2(weight_decay), activation="relu")) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add( Convolution2D(128, 3, 3, W_regularizer=l2(weight_decay), activation="relu")) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add( Convolution2D(256, 3, 3, W_regularizer=l2(weight_decay), activation="relu")) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add( Convolution2D(512, 3, 3, W_regularizer=l2(weight_decay), activation="relu")) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Dropout(0.5)) model.add(Flatten()) model.add(Dense(1024, W_regularizer=l2(weight_decay), activation="relu")) model.add(Dense(config["num_classes"], activation="softmax")) return model
def make_generator(): """Creates a generator model that takes a 100-dimensional noise vector as a "seed", and outputs images of size 28x28x1.""" model = Sequential() model.add(Dense(1024, input_dim=100)) model.add(LeakyReLU()) model.add(Dense(128 * 7 * 7)) model.add(BatchNormalization()) model.add(LeakyReLU()) if K.image_data_format() == 'channels_first': model.add(Reshape((128, 7, 7), input_shape=(128 * 7 * 7, ))) bn_axis = 1 else: model.add(Reshape((7, 7, 128), input_shape=(128 * 7 * 7, ))) bn_axis = -1 model.add(Conv2DTranspose(128, (5, 5), strides=2, padding='same')) model.add(BatchNormalization(axis=bn_axis)) model.add(LeakyReLU()) model.add(Convolution2D(64, (5, 5), padding='same')) model.add(BatchNormalization(axis=bn_axis)) model.add(LeakyReLU()) model.add(Conv2DTranspose(64, (5, 5), strides=2, padding='same')) model.add(BatchNormalization(axis=bn_axis)) model.add(LeakyReLU()) # Because we normalized training inputs to lie in the range [-1, 1], # the tanh function should be used for the output of the generator to ensure # its output also lies in this range. model.add(Convolution2D(1, (5, 5), padding='same', activation='tanh')) return model
def conv_block(x, stage, branch, nb_filter, dropout_rate=None, weight_decay=1e-4): conv_name_base = 'conv' + str(stage) + '_' + str(branch) relu_name_base = 'relu' + str(stage) + '_' + str(branch) # 1x1 Convolution (Bottleneck layer) inter_channel = nb_filter * 4 x = BatchNormalization(name=conv_name_base + '_x1_bn')(x) x = Activation('relu', name=relu_name_base + '_x1')(x) x = Convolution2D(inter_channel, 1, 1, name=conv_name_base + '_x1', use_bias=False)(x) if dropout_rate: x = Dropout(dropout_rate)(x) # 3x3 Convolution x = BatchNormalization(name=conv_name_base + '_x2_bn')(x) x = Activation('relu', name=relu_name_base + '_x2')(x) x = ZeroPadding2D((1, 1), name=conv_name_base + '_x2_zeropadding')(x) x = Convolution2D(nb_filter, 3, 1, name=conv_name_base + '_x2', use_bias=False)(x) if dropout_rate: x = Dropout(dropout_rate)(x) return x
def create_nvidia_model1(input_shape: List): model = Sequential() model.add( Convolution2D(24, (5, 5), strides=(2, 2), padding="same", input_shape=input_shape)) model.add(Activation("relu")) model.add(Convolution2D(36, (5, 5), strides=(2, 2), padding="same")) model.add(Activation("relu")) model.add(Convolution2D(48, (5, 5), strides=(2, 2), padding="same")) model.add(Activation("relu")) model.add(Convolution2D(64, (3, 3), strides=(2, 2), padding="same")) model.add(Activation("relu")) model.add(Convolution2D(64, (3, 3), strides=(2, 2), padding="same")) model.add(Flatten()) model.add(Activation("relu")) model.add(Dense(100)) model.add(Activation("relu")) model.add(Dense(50)) model.add(Activation("relu")) model.add(Dense(10)) model.add(Activation("relu")) model.add(Dense(1, bias_initializer=Constant(MEAN_PULL_TIME))) return model
def conv_block(m, num_kernels, kernel_size, strides, padding, activation, dropout, data_format, bn): """ Bulding block with convolutional layers for one level. :param m: model :param num_kernels: number of convolution filters on the particular level, positive integer :param kernel_size: size of the convolution kernel, tuple of two positive integers :param strides: strides values, tuple of two positive integers :param padding: used padding by convolution, takes values: 'same' or 'valid' :param activation: activation_function after every convolution :param dropout: percentage of weights to be dropped, float between 0 and 1 :param data_format: ordering of the dimensions in the inputs, takes values: 'channel_first' or 'channel_last' :param bn: weather to use Batch Normalization layers after each convolution layer, True for use Batch Normalization, False do not use Batch Normalization :return: model """ n = Convolution2D(num_kernels, kernel_size, strides=strides, activation=activation, padding=padding, data_format=data_format)(m) n = BatchNormalization()(n) if bn else n n = Dropout(dropout)(n) n = Convolution2D(num_kernels, kernel_size, strides=strides, activation=activation, padding=padding, data_format=data_format)(n) n = BatchNormalization()(n) if bn else n return n
def create_comma_model_large_dropout(input_shape: List): model = Sequential() model.add( Convolution2D(16, (8, 8), strides=(4, 4), padding="same", input_shape=input_shape)) # model.add(ELU()) model.add(Activation("relu")) model.add(Convolution2D(32, (5, 5), strides=(2, 2), padding="same")) # model.add(ELU()) model.add(Activation("relu")) model.add(Convolution2D(64, (5, 5), strides=(2, 2), padding="same")) model.add(Flatten()) # model.add(Dropout(.5)) # model.add(ELU()) model.add(Activation("relu")) model.add(Dense(1024)) model.add(Dropout(0.5)) # model.add(ELU()) model.add(Activation("relu")) model.add(Dense(1, bias_initializer=Constant(MEAN_PULL_TIME))) return model
def buildModel(): model = Sequential() # phase traitement image model.add(Convolution2D(filters=nbFiltre, kernel_size=kernelSize, strides=stridesConv, input_shape=(imageSize, imageSize, 3), activation=activationConvolution)) # Phase de convolution , on transforme l'image avec la fonction de convolution grace a des filtre pour avoir un set de featurs map model.add(MaxPooling2D(pool_size=sizeMatricePool, strides=stridesPool)) # Phase de Pooling ( avec le max pooling ) , permet de réduire la taille en gardant l'information la plus importante ( pour les différente prise de vue) nbrLayer = 1 multiplicateur = 1 for i in range(nbrPoolConv - 1): model.add(Convolution2D(filters=nbFiltre * multiplicateur, kernel_size=kernelSize, strides=stridesConv, activation=activationConvolution)) model.add(MaxPooling2D(pool_size=sizeMatricePool, strides=stridesPool)) nbrLayer += 1 multiplicateur = math.floor((nbrLayer / 2) + 1) # phase feed forward model.add(Flatten()) # Couche d'entrée # Etape de flattening ( linéarise la matrice de pooling feature map pour le réseau de ANN) for j in range(nbrHiddenLayer): model.add(Dense(units=neuroneCoucheCacher, activation=fctActivation)) ##Couche de sortie model.add(Dropout(rateDropOut)) model.add(Dense(units=outUnit, activation="sigmoid")) model.compile(optimizer=optimizer, loss=fctLoss, metrics=allMetrics) return model
def encode_block(size, inputs, kernel, stride, activation, kinit, padding, batch_normalization=False, max_pool=True): conv1 = Convolution2D(size, kernel_size=kernel, strides=stride, activation=activation, kernel_initializer=kinit, padding=padding)(inputs) conv1 = BatchNormalization()(conv1) if batch_normalization else conv1 conv1 = Convolution2D(size, kernel_size=kernel, strides=stride, activation=activation, kernel_initializer=kinit, padding=padding)(conv1) conv1 = BatchNormalization()(conv1) if batch_normalization else conv1 pool1 = MaxPooling2D(pool_size=(2, 2))(conv1) if max_pool else None return conv1, pool1
def conv_block_td(input_tensor, kernel_size, filters, stage, block, input_shape, strides=(2, 2), trainable=True): # conv block time distributed nb_filter1, nb_filter2, nb_filter3 = filters bn_axis = 3 conv_name_base = 'res' + str(stage) + block + '_branch' bn_name_base = 'bn' + str(stage) + block + '_branch' x = TimeDistributed(Convolution2D(nb_filter1, (1, 1), strides=strides, trainable=trainable, kernel_initializer='normal'), input_shape=input_shape, name=conv_name_base + '2a')(input_tensor) x = TimeDistributed(BatchNormalization(axis=bn_axis), name=bn_name_base + '2a')(x) x = Activation('relu', name=conv_name_base + '2a_relu')(x) x = TimeDistributed(Convolution2D(nb_filter2, (kernel_size, kernel_size), padding='same', trainable=trainable, kernel_initializer='normal'), name=conv_name_base + '2b')(x) x = TimeDistributed(BatchNormalization(axis=bn_axis), name=bn_name_base + '2b')(x) x = Activation('relu', name=conv_name_base + '2b_relu')(x) x = TimeDistributed(Convolution2D(nb_filter3, (1, 1), kernel_initializer='normal'), name=conv_name_base + '2c', trainable=trainable)(x) x = TimeDistributed(BatchNormalization(axis=bn_axis), name=bn_name_base + '2c')(x) shortcut = TimeDistributed(Convolution2D(nb_filter3, (1, 1), strides=strides, trainable=trainable, kernel_initializer='normal'), name=conv_name_base + '1')(input_tensor) shortcut = TimeDistributed(BatchNormalization(axis=bn_axis), name=bn_name_base + '1')(shortcut) x = Add()([x, shortcut]) x = Activation('relu', name='res' + str(stage) + block + '_relu')(x) return x
def conv_t_block(size, input_1, input_2, kernel, stride, activation, kinit, padding, axis, batch_normalization=False): conv1 = Convolution2DTranspose(256, (2, 2), strides=(2, 2), padding=padding)(input_1) conv1 = BatchNormalization()(conv1) if input_2 is not None: conv1 = concatenate([conv1, input_2], axis=axis) conv2 = Convolution2D(256, kernel_size=kernel, strides=stride, activation=activation, kernel_initializer=kinit, padding=padding)(conv1) conv2 = BatchNormalization()(conv2) if batch_normalization else conv2 conv3 = Convolution2D(256, kernel_size=kernel, strides=stride, activation=activation, kernel_initializer=kinit, padding=padding)(conv2) return conv3
def identity_block_td(input_tensor, kernel_size, filters, stage, block, trainable=True): # identity block time distributed # it receives the same parameters of the above function nb_filter1, nb_filter2, nb_filter3 = filters bn_axis = 3 conv_name_base = 'res' + str(stage) + block + '_branch' bn_name_base = 'bn' + str(stage) + block + '_branch' x = TimeDistributed(Convolution2D(nb_filter1, (1, 1), trainable=trainable, kernel_initializer='normal'), name=conv_name_base + '2a')(input_tensor) x = TimeDistributed(BatchNormalization(axis=bn_axis), name=bn_name_base + '2a')(x) x = Activation('relu')(x) x = TimeDistributed(Convolution2D(nb_filter2, (kernel_size, kernel_size), trainable=trainable, kernel_initializer='normal', padding='same'), name=conv_name_base + '2b')(x) x = TimeDistributed(BatchNormalization(axis=bn_axis), name=bn_name_base + '2b')(x) x = Activation('relu', name=conv_name_base + '2b_relu')(x) x = TimeDistributed(Convolution2D(nb_filter3, (1, 1), trainable=trainable, kernel_initializer='normal'), name=conv_name_base + '2c')(x) x = TimeDistributed(BatchNormalization(axis=bn_axis), name=bn_name_base + '2c')(x) x = Add()([x, input_tensor]) x = Activation('relu', name='res' + str(stage) + block + '_relu')(x) return x
def lvo_cnn(input_shape=(192, 640, 3)): '''Define CNN model''' model = Sequential() model.add( Convolution2D(filters=64, kernel_size=3, strides=(2, 2), padding='valid', input_shape=input_shape, activation='relu')) model.add( Convolution2D(filters=128, kernel_size=3, strides=(2, 2), padding='same', activation='relu')) model.add( Convolution2D(filters=256, kernel_size=3, strides=(2, 2), padding='same', activation='relu')) model.add( Convolution2D(filters=512, kernel_size=3, strides=(2, 2), padding='same', activation='relu')) model.add(Flatten()) return model
def mlp(input_shape: Tuple[int, ...], output_shape: Tuple[int, ...], layer_size: int = 256, dropout_amount: float = 0.2, num_layers: int = 3) -> Model: """ Simple multi-layer perceptron: just fully-connected layers with dropout between them, with softmax predictions. Creates num_layers layers. """ num_classes = output_shape[0] model = Sequential() # Don't forget to pass input_shape to the first layer of the model ##### Your code below (Lab 1) model.add( Reshape((input_shape[0], input_shape[1], 1), input_shape=input_shape)) model.add(Convolution2D(256, 3, 3, activation='relu', padding='same')) model.add(Convolution2D(256, 3, 3, activation='relu', padding='same')) model.add(Convolution2D(256, 3, 3, activation='relu', padding='same')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(BatchNormalization()) model.add(Flatten()) for i in range(num_layers): model.add(Dense(layer_size, activation='selu')) model.add(Dropout(dropout_amount)) model.add(BatchNormalization()) model.add(Dense(num_classes, activation='softmax')) ##### Your code above (Lab 1) return model
def identity_block_td(input_tensor, kernel_size, filters, stage, block, trainable=True): # identity block time distributed nb_filter1, nb_filter2, nb_filter3 = filters if K.image_data_format() == 'channels_last': bn_axis = 3 else: bn_axis = 1 conv_name_base = 'res' + str(stage) + block + '_branch' bn_name_base = 'bn' + str(stage) + block + '_branch' x = TimeDistributed(Convolution2D(nb_filter1, (1, 1), trainable=trainable, kernel_initializer='normal'), name=conv_name_base + '2a')(input_tensor) x = TimeDistributed(FixedBatchNormalization(axis=bn_axis), name=bn_name_base + '2a')(x) x = Activation('relu')(x) x = TimeDistributed(Convolution2D(nb_filter2, (kernel_size, kernel_size), trainable=trainable, kernel_initializer='normal',padding='same'), name=conv_name_base + '2b')(x) x = TimeDistributed(FixedBatchNormalization(axis=bn_axis), name=bn_name_base + '2b')(x) x = Activation('relu')(x) x = TimeDistributed(Convolution2D(nb_filter3, (1, 1), trainable=trainable, kernel_initializer='normal'), name=conv_name_base + '2c')(x) x = TimeDistributed(FixedBatchNormalization(axis=bn_axis), name=bn_name_base + '2c')(x) x = Add()([x, input_tensor]) x = Activation('relu')(x) return x