def vgg16(weights_path=None): model = Sequential() model.add(ZeroPadding2D((1, 1), input_shape=(3, 224, 224))) model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_1')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_2')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_1')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_2')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_1')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_2')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_3')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_1')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_2')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_3')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_1')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_2')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_3')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(Flatten(name="flatten")) model.add(Dense(4096, activation='relu', name='dense_1')) model.add(Dropout(0.5)) model.add(Dense(4096, activation='relu', name='dense_2')) model.add(Dropout(0.5)) model.add(Dense(1000, name='dense_3')) model.add(Activation("softmax", name="softmax")) if weights_path: model.load_weights(weights_path) return model
def comp_double(self): ''' double model. Simialar to two-pathway, except takes in a 4x33x33 patch and it's center 4x5x5 patch. merges paths at flatten layer. ''' print('Compiling double model...') single = Sequential() single.add( Convolution2D(64, 7, 7, border_mode='valid', W_regularizer=l1l2(l1=0.01, l2=0.01), input_shape=(4, 33, 33))) single.add(Activation('relu')) single.add(BatchNormalization(mode=0, axis=1)) single.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1))) single.add(Dropout(0.5)) single.add( Convolution2D(nb_filter=128, nb_row=5, nb_col=5, activation='relu', border_mode='valid', W_regularizer=l1l2(l1=0.01, l2=0.01))) single.add(BatchNormalization(mode=0, axis=1)) single.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1))) single.add(Dropout(0.5)) single.add( Convolution2D(nb_filter=256, nb_row=5, nb_col=5, activation='relu', border_mode='valid', W_regularizer=l1l2(l1=0.01, l2=0.01))) single.add(BatchNormalization(mode=0, axis=1)) single.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1))) single.add(Dropout(0.5)) single.add( Convolution2D(nb_filter=128, nb_row=3, nb_col=3, activation='relu', border_mode='valid', W_regularizer=l1l2(l1=0.01, l2=0.01))) single.add(Dropout(0.25)) single.add(Flatten()) # add small patch to train on five = Sequential() five.add(Reshape((100, 1), input_shape=(4, 5, 5))) five.add(Flatten()) five.add(MaxoutDense(128, nb_feature=5)) five.add(Dropout(0.5)) model = Sequential() # merge both paths model.add(Merge([five, single], mode='concat', concat_axis=1)) model.add(Dense(5)) model.add(Activation('softmax')) sgd = SGD(lr=0.001, decay=0.01, momentum=0.9) model.compile(loss='categorical_crossentropy', optimizer='sgd') print('Done.') return model
def create_model(self, height=32, width=32, channels=3, load_weights=False, batch_size=128): # Perform check that model input shape is divisible by 4 init = super(DeepDenoiseSR, self).create_model(height, width, channels, load_weights, batch_size) c1 = Convolution2D(self.n1, 3, 3, activation='relu', border_mode='same')(init) c1 = Convolution2D(self.n1, 3, 3, activation='relu', border_mode='same')(c1) x = MaxPooling2D((2, 2))(c1) c2 = Convolution2D(self.n2, 3, 3, activation='relu', border_mode='same')(x) c2 = Convolution2D(self.n2, 3, 3, activation='relu', border_mode='same')(c2) x = MaxPooling2D((2, 2))(c2) c3 = Convolution2D(self.n3, 3, 3, activation='relu', border_mode='same')(x) x = UpSampling2D()(c3) c2_2 = Convolution2D(self.n2, 3, 3, activation='relu', border_mode='same')(x) c2_2 = Convolution2D(self.n2, 3, 3, activation='relu', border_mode='same')(c2_2) m1 = merge([c2, c2_2], mode='sum') m1 = UpSampling2D()(m1) c1_2 = Convolution2D(self.n1, 3, 3, activation='relu', border_mode='same')(m1) c1_2 = Convolution2D(self.n1, 3, 3, activation='relu', border_mode='same')(c1_2) m2 = merge([c1, c1_2], mode='sum') decoded = Convolution2D(channels, 5, 5, activation='linear', border_mode='same')(m2) model = Model(init, decoded) adam = optimizers.Adam(lr=1e-3) model.compile(optimizer=adam, loss='mse', metrics=[PSNRLoss]) if load_weights: model.load_weights(self.weight_path) self.model = model return model
def GenreRecCNN(weights='msd', input_tensor=None, n_classes=10): '''Creates CNN model for Music Genre Recognition inputs: weights (str): if None (random initialization) or "msd" (pre-training on ImageNet). input_tensor (tuple of ints): Keras tensor to use as image input for the model. outputs: A Keras model instance. ''' if weights not in {'msd', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `msd` ' '(pre-training on Million Song Dataset).') # Determine proper input shape if K.image_dim_ordering() == 'th': input_shape = (1, 96, 1366) else: input_shape = (96, 1366, 1) if input_tensor is None: melgram_input = Input(shape=input_shape) else: melgram_input = Input(shape=input_tensor) # Determine input axis if K.image_dim_ordering() == 'th': channel_axis = 1 freq_axis = 2 time_axis = 3 else: channel_axis = 3 freq_axis = 1 time_axis = 2 # Input block x = BatchNormalization(axis=time_axis, name='bn_0_freq')(melgram_input) # Conv block 1 x = Convolution2D(32, 3, 3, border_mode='same', name='conv1')(x) x = BatchNormalization(axis=channel_axis, mode=0, name='bn1')(x) x = ELU()(x) x = MaxPooling2D(pool_size=(2, 4), name='pool1')(x) # Conv block 2 x = Convolution2D(128, 3, 3, border_mode='same', name='conv2')(x) x = BatchNormalization(axis=channel_axis, mode=0, name='bn2')(x) x = ELU()(x) x = MaxPooling2D(pool_size=(2, 4), name='pool2')(x) # Conv block 3 x = Convolution2D(128, 3, 3, border_mode='same', name='conv3')(x) x = BatchNormalization(axis=channel_axis, mode=0, name='bn3')(x) x = ELU()(x) x = MaxPooling2D(pool_size=(2, 4), name='pool3')(x) # Conv block 4 x = Convolution2D(192, 3, 3, border_mode='same', name='conv4')(x) x = BatchNormalization(axis=channel_axis, mode=0, name='bn4')(x) x = ELU()(x) x = MaxPooling2D(pool_size=(3, 5), name='pool4')(x) # Conv block 5 x = Convolution2D(256, 3, 3, border_mode='same', name='conv5')(x) x = BatchNormalization(axis=channel_axis, mode=0, name='bn5')(x) x = ELU()(x) x = MaxPooling2D(pool_size=(4, 4), name='pool5')(x) # Output x = Flatten(name='Flatten_1')(x) if weights is None: # Create model x = Dense(n_classes, activation='softmax', name='output')(x) model = Model(melgram_input, x) return model else: # Load input x = Dense(50, activation='sigmoid', name='output')(x) # Theano dim ordering is reqeuired to use the pre-trained model if K.image_dim_ordering() == 'tf': raise RuntimeError("Please set image_dim_ordering == 'th'." "You can set it at ~/.keras/keras.json") # Create model initial_model = Model(melgram_input, x) initial_model.load_weights('/home/stasdon/git/temp/musicgenrerecognition/scripts/crnn/weights/music_tagger_cnn_weights_%s.h5' % K._BACKEND, by_name=True) # Eliminate last layer pop_layer(initial_model) # Add new Dense layer last = initial_model.get_layer('Flatten_1') preds = (Dense(n_classes, activation='softmax', name='preds'))(last.output) model = Model(initial_model.input, preds) for layer in model.layers[:-6]: layer.trainable = False return model
def MusicTaggerCRNN(weights='msd', input_tensor=None, include_top=True): '''Instantiate the MusicTaggerCRNN architecture, optionally loading weights pre-trained on Million Song Dataset. Note that when using TensorFlow, for best performance you should set `image_dim_ordering="tf"` in your Keras config at ~/.keras/keras.json. The model and the weights are compatible with both TensorFlow and Theano. The dimension ordering convention used by the model is the one specified in your Keras config file. For preparing mel-spectrogram input, see `audio_conv_utils.py` in [applications](https://github.com/fchollet/keras/tree/master/keras/applications). You will need to install [Librosa](http://librosa.github.io/librosa/) to use it. # Arguments weights: one of `None` (random initialization) or "msd" (pre-training on ImageNet). input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. include_top: whether to include the 1 fully-connected layer (output layer) at the top of the network. If False, the network outputs 32-dim features. # Returns A Keras model instance. ''' if weights not in {'msd', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `msd` ' '(pre-training on Million Song Dataset).') # Determine proper input shape if K.image_dim_ordering() == 'th': input_shape = (1, 96, 1366) else: input_shape = (96, 1366, 1) if input_tensor is None: melgram_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): melgram_input = Input(tensor=input_tensor, shape=input_shape) else: melgram_input = input_tensor # Determine input axis if K.image_dim_ordering() == 'th': channel_axis = 1 freq_axis = 2 time_axis = 3 else: channel_axis = 3 freq_axis = 1 time_axis = 2 # Input block x = ZeroPadding2D(padding=(0, 37))(melgram_input) x = BatchNormalization(axis=time_axis, name='bn_0_freq')(x) # Conv block 1 x = Convolution2D(64, 3, 3, border_mode='same', name='conv1')(x) x = BatchNormalization(axis=channel_axis, mode=0, name='bn1')(x) x = ELU()(x) x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name='pool1')(x) # Conv block 2 x = Convolution2D(128, 3, 3, border_mode='same', name='conv2')(x) x = BatchNormalization(axis=channel_axis, mode=0, name='bn2')(x) x = ELU()(x) x = MaxPooling2D(pool_size=(3, 3), strides=(3, 3), name='pool2')(x) # Conv block 3 x = Convolution2D(128, 3, 3, border_mode='same', name='conv3')(x) x = BatchNormalization(axis=channel_axis, mode=0, name='bn3')(x) x = ELU()(x) x = MaxPooling2D(pool_size=(4, 4), strides=(4, 4), name='pool3')(x) # Conv block 4 x = Convolution2D(128, 3, 3, border_mode='same', name='conv4')(x) x = BatchNormalization(axis=channel_axis, mode=0, name='bn4')(x) x = ELU()(x) x = MaxPooling2D(pool_size=(4, 4), strides=(4, 4), name='pool4')(x) # reshaping if K.image_dim_ordering() == 'th': x = Permute((3, 1, 2))(x) x = Reshape((15, 128))(x) # GRU block 1, 2, output x = GRU(32, return_sequences=True, name='gru1')(x) x = GRU(32, return_sequences=False, name='gru2')(x) if include_top: x = Dense(50, activation='sigmoid', name='output')(x) # Create model model = Model(melgram_input, x) if weights is None: return model else: # Load weights if K.image_dim_ordering() == 'tf': weights_path = get_file('music_tagger_crnn_weights_tf_kernels_tf_dim_ordering.h5', TF_WEIGHTS_PATH, cache_subdir='models') else: weights_path = get_file('music_tagger_crnn_weights_tf_kernels_th_dim_ordering.h5', TH_WEIGHTS_PATH, cache_subdir='models') model.load_weights(weights_path, by_name=True) if K.backend() == 'theano': convert_all_kernels_in_model(model) return model
def generator_model(): global BATCH_SIZE # imgs: input: 256x256xch # U-Net structure, must change to relu inputs = Input((IN_CH, img_cols, img_rows)) e1 = BatchNormalization(mode=2)(inputs) e1 = Convolution2D(64, 4, 4, subsample=(2,2), activation='relu',init='uniform', border_mode='same')(e1) e1 = BatchNormalization(mode=2)(e1) e2 = Convolution2D(128, 4, 4, subsample=(2,2), activation='relu',init='uniform', border_mode='same')(e1) e2 = BatchNormalization(mode=2)(e2) e3 = Convolution2D(256, 4, 4, subsample=(2,2), activation='relu',init='uniform', border_mode='same')(e2) e3 = BatchNormalization(mode=2)(e3) e4 = Convolution2D(512, 4, 4, subsample=(2,2), activation='relu',init='uniform', border_mode='same')(e3) e4 = BatchNormalization(mode=2)(e4) e5 = Convolution2D(512, 4, 4, subsample=(2,2), activation='relu',init='uniform', border_mode='same')(e4) e5 = BatchNormalization(mode=2)(e5) e6 = Convolution2D(512, 4, 4, subsample=(2,2), activation='relu',init='uniform', border_mode='same')(e5) e6 = BatchNormalization(mode=2)(e6) e7 = Convolution2D(512, 4, 4, subsample=(2,2), activation='relu',init='uniform', border_mode='same')(e6) e7 = BatchNormalization(mode=2)(e7) e8 = Convolution2D(512, 4, 4, subsample=(2,2), activation='relu',init='uniform', border_mode='same')(e7) e8 = BatchNormalization(mode=2)(e8) d1 = Deconvolution2D(512, 5, 5, subsample=(2,2), activation='relu',init='uniform', output_shape=(None, 512, 2, 2), border_mode='same')(e8) d1 = merge([d1, e7], mode='concat', concat_axis=1) d1 = BatchNormalization(mode=2)(d1) d2 = Deconvolution2D(512, 5, 5, subsample=(2,2), activation='relu',init='uniform', output_shape=(None, 512, 4, 4), border_mode='same')(d1) d2 = merge([d2, e6], mode='concat', concat_axis=1) d2 = BatchNormalization(mode=2)(d2) d3 = Dropout(0.2)(d2) d3 = Deconvolution2D(512, 5, 5, subsample=(2,2), activation='relu',init='uniform', output_shape=(None, 512, 8, 8), border_mode='same')(d3) d3 = merge([d3, e5], mode='concat', concat_axis=1) d3 = BatchNormalization(mode=2)(d3) d4 = Dropout(0.2)(d3) d4 = Deconvolution2D(512, 5, 5, subsample=(2,2), activation='relu',init='uniform', output_shape=(None, 512, 16, 16), border_mode='same')(d4) d4 = merge([d4, e4], mode='concat', concat_axis=1) d4 = BatchNormalization(mode=2)(d4) d5 = Dropout(0.2)(d4) d5 = Deconvolution2D(256, 5, 5, subsample=(2,2), activation='relu',init='uniform', output_shape=(None, 256, 32, 32), border_mode='same')(d5) d5 = merge([d5, e3], mode='concat', concat_axis=1) d5 = BatchNormalization(mode=2)(d5) d6 = Dropout(0.2)(d5) d6 = Deconvolution2D(128, 5, 5, subsample=(2,2), activation='relu',init='uniform', output_shape=(None, 128, 64, 64), border_mode='same')(d6) d6 = merge([d6, e2], mode='concat', concat_axis=1) d6 = BatchNormalization(mode=2)(d6) d7 = Dropout(0.2)(d6) d7 = Deconvolution2D(64, 5, 5, subsample=(2,2), activation='relu',init='uniform', output_shape=(None, 64,128, 128), border_mode='same')(d7) d7 = merge([d7,e1], mode='concat', concat_axis=1) d7 = BatchNormalization(mode=2)(d7) d8 = Deconvolution2D(3, 5, 5, subsample=(2,2), activation='relu',init='uniform', output_shape=(None, 3, 256, 256), border_mode='same')(d7) d8 = BatchNormalization(mode=2)(d8) d9 = Activation('tanh')(d8) model = Model(input=inputs, output=d9) return model
def DenseNet(nb_dense_block=4, growth_rate=48, nb_filter=96, reduction=0.0, dropout_rate=0.0, weight_decay=1e-4, classes=1000, weights_path=None): '''Instantiate the DenseNet 161 architecture, # Arguments nb_dense_block: number of dense blocks to add to end growth_rate: number of filters to add per dense block nb_filter: initial number of filters reduction: reduction factor of transition blocks. dropout_rate: dropout rate weight_decay: weight decay factor classes: optional number of classes to classify images weights_path: path to pre-trained weights # Returns A Keras model instance. ''' eps = 1.1e-5 # compute compression factor compression = 1.0 - reduction # Handle Dimension Ordering for different backends global concat_axis if K.image_dim_ordering() == 'tf': concat_axis = 3 img_input = Input(shape=(224, 224, 3), name='data') else: concat_axis = 1 img_input = Input(shape=(3, 224, 224), name='data') # From architecture for ImageNet (Table 1 in the paper) nb_filter = 96 nb_layers = [6, 12, 36, 24] # For DenseNet-161 # Initial convolution x = ZeroPadding2D((3, 3), name='conv1_zeropadding')(img_input) x = Convolution2D(nb_filter, 7, 7, subsample=(2, 2), name='conv1', bias=False)(x) x = BatchNormalization(epsilon=eps, axis=concat_axis, name='conv1_bn')(x) x = Scale(axis=concat_axis, name='conv1_scale')(x) x = Activation('relu', name='relu1')(x) x = ZeroPadding2D((1, 1), name='pool1_zeropadding')(x) x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1')(x) # Add dense blocks for block_idx in range(nb_dense_block - 1): stage = block_idx + 2 x, nb_filter = dense_block(x, stage, nb_layers[block_idx], nb_filter, growth_rate, dropout_rate=dropout_rate, weight_decay=weight_decay) # Add transition_block x = transition_block(x, stage, nb_filter, compression=compression, dropout_rate=dropout_rate, weight_decay=weight_decay) nb_filter = int(nb_filter * compression) final_stage = stage + 1 x, nb_filter = dense_block(x, final_stage, nb_layers[-1], nb_filter, growth_rate, dropout_rate=dropout_rate, weight_decay=weight_decay) x = BatchNormalization(epsilon=eps, axis=concat_axis, name='conv' + str(final_stage) + '_blk_bn')(x) x = Scale(axis=concat_axis, name='conv' + str(final_stage) + '_blk_scale')(x) x = Activation('relu', name='relu' + str(final_stage) + '_blk')(x) x = GlobalAveragePooling2D(name='pool' + str(final_stage))(x) x = Dense(classes, name='fc6')(x) x = Activation('softmax', name='prob')(x) model = Model(img_input, x, name='densenet') if weights_path is not None: model.load_weights(weights_path) return model
print('Train:', image_paths_train.shape, angles_train.shape) print('Test:', image_paths_test.shape, angles_test.shape) if RunModel: #NVIDIA model - coding done by https://github.com/jeremy-shannon model = Sequential() # Normalize model.add(Lambda(lambda x: x / 127.5 - 1.0, input_shape=(66, 200, 3))) # Add three 5x5 convolution layers (output depth 24, 36, and 48), each with 2x2 stride model.add( Convolution2D(24, 5, 5, subsample=(2, 2), border_mode='valid', W_regularizer=l2(0.001))) model.add(ELU()) model.add( Convolution2D(36, 5, 5, subsample=(2, 2), border_mode='valid', W_regularizer=l2(0.001))) model.add(ELU()) model.add( Convolution2D(48, 5, 5,
#loss values in each experiment Pool_Valid_Loss = np.zeros(shape=(nb_epoch, 1)) Pool_Train_Loss = np.zeros(shape=(nb_epoch, 1)) Pool_Valid_Acc = np.zeros(shape=(nb_epoch, 1)) Pool_Train_Acc = np.zeros(shape=(nb_epoch, 1)) x_pool_All = np.zeros(shape=(1)) Y_train = np_utils.to_categorical(y_train, nb_classes) print('Training Model Without Acquisitions in Experiment', e) model = Sequential() model.add( Convolution2D(nb_filters, nb_conv, nb_conv, border_mode='valid', input_shape=(1, img_rows, img_cols))) model.add(Activation('relu')) model.add(Convolution2D(nb_filters, nb_conv, nb_conv)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool))) model.add(Dropout(0.25)) model.add( Convolution2D(nb_filters * 2, nb_conv, nb_conv, border_mode='valid', input_shape=(1, img_rows, img_cols))) model.add(Activation('relu'))
def get_model_classification(input_shape, filename=None, l1_value=10e-12, l2_value=10e-14): model = Sequential() model.add( Convolution2D(nb_filter=32, nb_row=3, nb_col=3, border_mode='valid', input_shape=(input_shape[0], input_shape[1], 1), dim_ordering='tf', W_regularizer=l1l2(l1=l1_value, l2=l2_value), activity_regularizer=activity_l1l2(l1=l1_value, l2=l2_value))) model.add(BatchNormalization()) model.add(Activation('relu')) model.add( AtrousConv2D(nb_filter=64, nb_row=3, nb_col=3, atrous_rate=(2, 2), border_mode='valid', dim_ordering='tf', W_regularizer=l1l2(l1=l1_value, l2=l2_value), activity_regularizer=activity_l1l2(l1=l1_value, l2=l2_value))) model.add(BatchNormalization()) model.add(Activation('relu')) model.add( AtrousConv2D(nb_filter=128, nb_row=3, nb_col=3, atrous_rate=(4, 4), border_mode='valid', dim_ordering='tf', W_regularizer=l1l2(l1=l1_value, l2=l2_value), activity_regularizer=activity_l1l2(l1=l1_value, l2=l2_value))) model.add(BatchNormalization()) model.add(Activation('relu')) model.add( AtrousConv2D(nb_filter=512, nb_row=3, nb_col=3, atrous_rate=(8, 8), border_mode='valid', dim_ordering='tf', W_regularizer=l1l2(l1=l1_value, l2=l2_value), activity_regularizer=activity_l1l2(l1=l1_value, l2=l2_value))) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add( Convolution2D(nb_filter=512, nb_row=1, nb_col=1, border_mode='valid', dim_ordering='tf', W_regularizer=l1l2(l1=l1_value, l2=l2_value), activity_regularizer=activity_l1l2(l1=l1_value, l2=l2_value))) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add( Convolution2D(nb_filter=1, nb_row=1, nb_col=1, border_mode='valid', dim_ordering='tf', W_regularizer=l1l2(l1=l1_value, l2=l2_value), activity_regularizer=activity_l1l2(l1=l1_value, l2=l2_value))) model.add(Activation('sigmoid')) print("compiling model") # load previously trained model if filename is not None: print("Loading weights from", filename) model.load_weights(filename) model.compile( optimizer=Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08), metrics=[dice_error_metric, binary_crossentropy_metric, 'accuracy'], loss=binary_crossentropy) print("Model padding:", (input_shape[0] - model.output_shape[1]) / 2, (input_shape[1] - model.output_shape[2]) / 2) return model
def get_model_classification_refinement(input_shape, filename=None, l1_value=10e-12, l2_value=10e-14, mode='valid'): def binary_crossentropy(y_true, y_pred): p1 = 0.412244897959 p0 = 1 - p1 w = p1 * K.cast(K.equal(y_true, 0), K.floatx()) + p0 * K.cast( K.equal(y_true, 1), K.floatx()) return K.sum(w * K.binary_crossentropy(y_pred, y_true), axis=[1, 2, 3]) / K.sum(w, axis=[1, 2, 3]) def binary_crossentropy_metric(y_true, y_pred): return K.mean(binary_crossentropy(y_true, y_pred)) model = Sequential() model.add( Convolution2D(nb_filter=32, nb_row=3, nb_col=3, border_mode=mode, input_shape=(input_shape[0], input_shape[1], 1), dim_ordering='tf', W_regularizer=l1l2(l1=l1_value, l2=l2_value), activity_regularizer=activity_l1l2(l1=l1_value, l2=l2_value))) model.add(Activation('relu')) model.add( Convolution2D(nb_filter=32, nb_row=3, nb_col=3, border_mode=mode, dim_ordering='tf', W_regularizer=l1l2(l1=l1_value, l2=l2_value), activity_regularizer=activity_l1l2(l1=l1_value, l2=l2_value))) model.add(Activation('relu')) model.add(MaxPooling2D(dim_ordering='tf')) model.add( Convolution2D(nb_filter=64, nb_row=3, nb_col=3, border_mode=mode, dim_ordering='tf', W_regularizer=l1l2(l1=l1_value, l2=l2_value), activity_regularizer=activity_l1l2(l1=l1_value, l2=l2_value))) model.add(Activation('relu')) model.add( Convolution2D(nb_filter=64, nb_row=3, nb_col=3, border_mode=mode, dim_ordering='tf', W_regularizer=l1l2(l1=l1_value, l2=l2_value), activity_regularizer=activity_l1l2(l1=l1_value, l2=l2_value))) model.add(Activation('relu')) model.add(MaxPooling2D(dim_ordering='tf')) model.add( Convolution2D(nb_filter=128, nb_row=3, nb_col=3, border_mode=mode, dim_ordering='tf', W_regularizer=l1l2(l1=l1_value, l2=l2_value), activity_regularizer=activity_l1l2(l1=l1_value, l2=l2_value))) model.add(Activation('relu')) model.add( Convolution2D(nb_filter=128, nb_row=3, nb_col=3, border_mode=mode, dim_ordering='tf', W_regularizer=l1l2(l1=l1_value, l2=l2_value), activity_regularizer=activity_l1l2(l1=l1_value, l2=l2_value))) model.add(Activation('relu')) model.add(MaxPooling2D(dim_ordering='tf')) model.add( Convolution2D(nb_filter=512, nb_row=16, nb_col=16, border_mode=mode, dim_ordering='tf', W_regularizer=l1l2(l1=l1_value, l2=l2_value), activity_regularizer=activity_l1l2(l1=l1_value, l2=l2_value))) model.add(Activation('relu')) model.add( Convolution2D(nb_filter=512, nb_row=1, nb_col=1, border_mode=mode, dim_ordering='tf', W_regularizer=l1l2(l1=l1_value, l2=l2_value), activity_regularizer=activity_l1l2(l1=l1_value, l2=l2_value))) model.add(Activation('relu')) model.add( Convolution2D(nb_filter=1, nb_row=1, nb_col=1, border_mode=mode, dim_ordering='tf', W_regularizer=l1l2(l1=l1_value, l2=l2_value), activity_regularizer=activity_l1l2(l1=l1_value, l2=l2_value))) model.add(Activation('sigmoid')) print("compiling model") # load previously trained model if filename is not None: print("Loading weights from", filename) model.load_weights(filename) model.compile( optimizer=Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08), metrics=[dice_error_metric, binary_crossentropy_metric, 'accuracy'], loss=binary_crossentropy) print("Model padding:", (input_shape[0] - model.output_shape[1]) / 2, (input_shape[1] - model.output_shape[2]) / 2) return model
# frame size nrows = 64 ncols = 64 wr = 0.00000 dp = 0.0 # speed, accel, distance, angle real_in = Input(shape=(2, ), name='real_input') # video frame in, grayscale frame_in = Input(shape=(3, nrows, ncols), name='img_input') # convolution for image input conv1 = Convolution2D(24, 5, 5, border_mode='same', W_regularizer=l1(wr), init='lecun_uniform') conv_l1 = conv1(frame_in) Econv_l1 = ELU()(conv_l1) pool_l1 = MaxPooling2D(pool_size=(2, 2))(Econv_l1) conv2 = Convolution2D(32, 5, 5, border_mode='same', W_regularizer=l1(wr), init='lecun_uniform') conv_l2 = conv2(pool_l1) Econv_l2 = ELU()(conv_l2) pool_l2 = MaxPooling2D(pool_size=(2, 2))(Econv_l2)
def VGG_16(): input_img = Input(shape=(3, 224, 224)) x = ZeroPadding2D((1, 1), input_shape=(3, 224, 224))(input_img) x = Convolution2D(64, 3, 3, activation='relu')(x) x = ZeroPadding2D((1, 1))(x) x = Convolution2D(64, 3, 3, activation='relu')(x) x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Convolution2D(128, 3, 3, activation='relu')(x) x = ZeroPadding2D((1, 1))(x) x = Convolution2D(128, 3, 3, activation='relu')(x) x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Convolution2D(256, 3, 3, activation='relu')(x) x = ZeroPadding2D((1, 1))(x) x = Convolution2D(256, 3, 3, activation='relu')(x) x = ZeroPadding2D((1, 1))(x) x = Convolution2D(256, 3, 3, activation='relu')(x) x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Convolution2D(512, 3, 3, activation='relu')(x) x = ZeroPadding2D((1, 1))(x) x = Convolution2D(512, 3, 3, activation='relu')(x) x = ZeroPadding2D((1, 1))(x) x = Convolution2D(512, 3, 3, activation='relu')(x) x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Convolution2D(512, 3, 3, activation='relu')(x) x = ZeroPadding2D((1, 1))(x) x = Convolution2D(512, 3, 3, activation='relu')(x) x = ZeroPadding2D((1, 1))(x) x = Convolution2D(512, 3, 3, activation='relu')(x) x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = Flatten()(x) x = Dense(4096, activation='relu')(x) x = Dropout(0.5)(x) x = Dense(4096, activation='relu')(x) x = Dropout(0.5)(x) aux_output = x prob_out = Dense(1000, activation='softmax')(x) model = Model(input=input_img, output=[prob_out, aux_output]) model.load_weights('vgg16_weights.h5') return model
print te_X.shape, te_y.shape tr_X, te_X = reshapeX(tr_X), reshapeX(te_X) print tr_X.shape, tr_y.shape print te_X.shape, te_y.shape ###build model by keras alpha = 1 kernelsize = 5 input_audio = Input(shape=(1, 1, 512)) x = Convolution2D(alpha * 64, 1, kernelsize, activation='relu', border_mode='same')(input_audio) x = AveragePooling2D((1, 2))(x) x = Convolution2D(alpha * 32, 1, kernelsize, activation='relu', border_mode='same')(x) x = AveragePooling2D((1, 2))(x) x = Convolution2D(alpha * 4, 1, kernelsize, activation='relu', border_mode='same')(x) encoded = AveragePooling2D((1, 2))(x)
trainL = len(trainFs) testL = len(testFs) print "number of examples: ", numEx print "training examples : ", trainL print "test examples : ", testL features, labels = helperFuncs.getTargets( "ocr") #get the target vector for each CID outsize = len(features[ features.keys()[0]]) #this it the size of the target (# of OCRfeatures) """DEFINE THE MODEL HERE""" model = Sequential() model.add(Convolution2D(16, 8, 8, input_shape=(1, size, size))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Convolution2D(32, 5, 5)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) #model.add(Dropout(0.25)) model.add(Convolution2D(32, 5, 5)) model.add(Activation('relu')) model.add(Convolution2D(64, 5, 5)) model.add(Activation('relu'))
from keras.layers.convolutional import Convolution2D from keras.models import Sequential from keras.optimizers import Adam import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt import helper #from keras.utils.visualize_util import plot tf.python.control_flow_ops = tf model = Sequential() model.add(Lambda(lambda x: x / 127.5 - 1.0, input_shape=(64, 64, 3))) model.add(Convolution2D(24, 5, 5, border_mode='same', subsample=(2, 2))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1))) model.add(Convolution2D(36, 5, 5, border_mode='same', subsample=(2, 2))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1))) model.add(Convolution2D(48, 5, 5, border_mode='same', subsample=(2, 2))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1))) model.add(Convolution2D(64, 3, 3, border_mode='same', subsample=(1, 1))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1))) model.add(Convolution2D(64, 3, 3, border_mode='same', subsample=(1, 1))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1))) model.add(Flatten())
print("length", len(y_train)) X_train_extra, Y_train_extra = balance_dataset(X_train_extra, Y_train_extra) print("length", len(X_train_extra)) Y_train_hot = one_hot_encoded(Y_train_extra, n_classes) Y_test_hot = one_hot_encoded(y_test, n_classes) print("Y_train shape", Y_train_hot.shape) X_train_set, X_validation, Y_train_set, Y_validation = train_test_split( X_train_extra, Y_train_hot, test_size=0.2, random_state=42) print(X_validation.shape) inputs = Input(shape=(height, width, num_channels)) lam_layer = Lambda(lambda x: x / 127.5 - 1.0, name="noramlise")(inputs) conv_1 = Convolution2D(24, 3, 3, border_mode='same', activation='elu')(lam_layer) conv_2 = Convolution2D(36, 3, 3, border_mode='same', activation='elu')(conv_1) conv_3 = Convolution2D(24, 3, 3, border_mode='same', activation='elu')(conv_2) conv_4 = Convolution2D(48, 3, 3, border_mode='same', activation='elu')(conv_3) conv_5 = Convolution2D(64, 3, 3, border_mode='same', activation='elu')(conv_4) drop = Dropout(0.5)(conv_5) flat_1 = Flatten()(drop) dense = Dense(43)(flat_1) final = Activation('softmax')(dense) model = Model(input=inputs, output=final) batch_size = 64 # I trained for 10 Epoch. I noticed that after 10 epoch the training accuray doesnt decrease further nb_epoch = 10
def MusicTaggerCRNN(weights='msd', input_tensor=None): '''Instantiate the MusicTaggerCRNN architecture, optionally loading weights pre-trained on Million Song Dataset. Note that when using TensorFlow, for best performance you should set `image_dim_ordering="tf"` in your Keras config at ~/.keras/keras.json. The model and the weights are compatible with both TensorFlow and Theano. The dimension ordering convention used by the model is the one specified in your Keras config file. For preparing mel-spectrogram input, see `audio_conv_utils.py` in [applications](https://github.com/fchollet/keras/tree/master/keras/applications). You will need to install [Librosa](http://librosa.github.io/librosa/) to use it. # Arguments weights: one of `None` (random initialization) or "msd" (pre-training on ImageNet). input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. # Returns A Keras model instance. ''' if weights not in {'msd', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `msd` ' '(pre-training on Million Song Dataset).') # Determine proper input shape if K.image_dim_ordering() == 'th': input_shape = (1, 96, 1366) else: input_shape = (96, 1366, 1) if input_tensor is None: melgram_input = Input(shape=input_shape) else: melgram_input = Input(shape=input_tensor) # Determine input axis if K.image_dim_ordering() == 'th': channel_axis = 1 freq_axis = 2 time_axis = 3 else: channel_axis = 3 freq_axis = 1 time_axis = 2 # Input block x = ZeroPadding2D(padding=(0, 37))(melgram_input) x = BatchNormalization(axis=time_axis, name='bn_0_freq')(x) # Conv block 1 x = Convolution2D(64, 3, 3, border_mode='same', name='conv1', trainable=False)(x) x = BatchNormalization(axis=channel_axis, mode=0, name='bn1', trainable=False)(x) x = ELU()(x) x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name='pool1', trainable=False)(x) x = Dropout(0.1, name='dropout1', trainable=False)(x) # Conv block 2 x = Convolution2D(128, 3, 3, border_mode='same', name='conv2', trainable=False)(x) x = BatchNormalization(axis=channel_axis, mode=0, name='bn2', trainable=False)(x) x = ELU()(x) x = MaxPooling2D(pool_size=(3, 3), strides=(3, 3), name='pool2', trainable=False)(x) x = Dropout(0.1, name='dropout2', trainable=False)(x) # Conv block 3 x = Convolution2D(128, 3, 3, border_mode='same', name='conv3', trainable=False)(x) x = BatchNormalization(axis=channel_axis, mode=0, name='bn3', trainable=False)(x) x = ELU()(x) x = MaxPooling2D(pool_size=(4, 4), strides=(4, 4), name='pool3', trainable=False)(x) x = Dropout(0.1, name='dropout3', trainable=False)(x) # Conv block 4 x = Convolution2D(128, 3, 3, border_mode='same', name='conv4', trainable=False)(x) x = BatchNormalization(axis=channel_axis, mode=0, name='bn4', trainable=False)(x) x = ELU()(x) x = MaxPooling2D(pool_size=(4, 4), strides=(4, 4), name='pool4', trainable=False)(x) x = Dropout(0.1, name='dropout4', trainable=False)(x) # reshaping if K.image_dim_ordering() == 'th': x = Permute((3, 1, 2))(x) x = Reshape((15, 128))(x) # GRU block 1, 2, output x = GRU(32, return_sequences=True, name='gru1')(x) x = GRU(32, return_sequences=False, name='gru2')(x) x = Dropout(0.3, name='final_drop')(x) if weights is None: # Create model x = Dense(10, activation='sigmoid', name='output')(x) model = Model(melgram_input, x) return model else: # Load input x = Dense(50, activation='sigmoid', name='output')(x) if K.image_dim_ordering() == 'tf': raise RuntimeError("Please set image_dim_ordering == 'th'." "You can set it at ~/.keras/keras.json") # Create model initial_model = Model(melgram_input, x) initial_model.load_weights('weights/music_tagger_crnn_weights_%s.h5' % K._BACKEND, by_name=True) # Eliminate last layer pop_layer(initial_model) # Add new Dense layer last = initial_model.get_layer('final_drop') preds = (Dense(10, activation='sigmoid', name='preds'))(last.output) model = Model(initial_model.input, preds) return model
def CNN(X_test, Y_test, X_train, Y_train): batch_size = 32 nb_epoch = 10 nb_classes = 11 # input image dimensions img_rows, img_cols = 32, 32 # the CIFAR10 images are RGB img_channels = 3 seed = 7 np.random.seed(seed) model = Sequential() ### PLEASE PUT YOUR CODE HERE! ### Add Convolution, Activation and Pooling layers, compile your model, and fit it. #Add convolutional layer with with 32 filters. output tensor format is 32,3,3 model.add( Convolution2D(32, 3, 3, border_mode='valid', activation='relu', input_shape=(32, 32, 3))) #reducing dimensionality of the features model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Convolution2D(64, 3, 3, border_mode='valid', activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(128, 3, 3, border_mode='valid', activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Dropout(0.2)) #Drop random 20% of weights model.add(Flatten()) #convert the tensor to 1D vector model.add(Dense(128)) #another layer which is fully connnected, changes the dimensions of vector model.add(Dropout(0.2)) model.add(Dense(nb_classes)) model.add(Activation('softmax')) ## Compoling the model sgd = SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True) opt = keras.optimizers.rmsprop(lr=0.001, decay=1e-6) ''' X_train = X_train.astype('float32') Y_train = Y_train.astype('float32') X_train /= 255 Y_train /= 255 ''' model.compile(loss='categorical_crossentropy', optimizer=opt, shuffle=True, metrics=['accuracy']) c, r = Y_train.shape Y_train.reshape(c, r) # kfold = StratifiedKFold(n_splits = 10, shuffle=True) # results = cross_val_score(model, X_train, Y_train, cv = kfold) # print(results.mean) model_hist = model.fit(X_train, Y_train, batch_size=batch_size, verbose=1, nb_epoch=nb_epoch, validation_data=(X_test, Y_test)) plot_model_history(model_hist) score = model.evaluate( X_test, Y_test, verbose=0, ) print('Test loss:', score[0]) print('Test accuracy:', score[1] * 100, '%')
batch_size = 32 train_samples, validation_samples = train_test_split(samples, test_size=0.2) # compile and train the model using the generator function train_generator = generator(train_samples, batch_size=batch_size) validation_generator = generator(validation_samples, batch_size=batch_size) from keras.models import Sequential from keras.layers import Flatten, Dense, Lambda, Activation, Dropout, Cropping2D from keras.layers.convolutional import Convolution2D from keras.layers.pooling import MaxPooling2D model = Sequential() model.add(Lambda(lambda x: x / 255.0 - 0.5, input_shape=(160, 320, 3))) model.add(Cropping2D(cropping=((60, 25), (0, 0)))) model.add(Convolution2D(24, 5, 5, activation="relu")) model.add(MaxPooling2D()) model.add(Convolution2D(36, 5, 5, activation="relu")) model.add(MaxPooling2D()) model.add(Convolution2D(48, 5, 5, activation="relu")) model.add(MaxPooling2D()) model.add(Convolution2D(64, 3, 3, activation="relu")) model.add(Convolution2D(64, 3, 3, activation="relu")) model.add(Flatten()) model.add(Dropout(0.5)) model.add(Dense(100)) model.add(Dropout(0.5)) model.add(Dense(50)) model.add(Dropout(0.5)) model.add(Dense(10)) model.add(Dense(1))
image_flip = np.fliplr(image) images.append(image_flip) measurements.append(-measurement) X_train = np.array(images) y_train = np.array(measurements) from keras.models import Sequential, Model from keras.layers import Flatten, Dense, Lambda, Cropping2D #Lambda Wraps arbitrary expression as a Layer object. from keras.layers.convolutional import Convolution2D from keras.layers import MaxPooling2D model = Sequential() model.add(Lambda(lambda x: x / 255.0 - 0.5, input_shape=(160, 320, 3))) #((normalise & mean center)) model.add(Cropping2D(cropping=((58, 16), (0, 0)))) #crop distracting details model.add(Convolution2D(6, 5, 5, activation="relu")) model.add(MaxPooling2D()) model.add(Convolution2D(6, 5, 5, activation="relu")) model.add(MaxPooling2D()) model.add(Flatten()) model.add(Dense(120)) model.add(Dense(84)) model.add(Dense(1)) model.compile(loss='mse', optimizer='adam') model.fit(X_train, y_train, validation_split=0.2, shuffle=True, nb_epoch=9) model.save('model.h5') print("fin")
def build(self, input_shape): self.input_spec = [InputSpec(shape=input_shape)] self.states = [None, None] self.trainable_weights = [] self.W_a = Convolution2D(self.nb_filters_att, self.nb_rows, self.nb_cols, border_mode='same', bias=True, init=self.init) self.U_a = Convolution2D(self.nb_filters_att, self.nb_rows, self.nb_cols, border_mode='same', bias=True, init=self.init) self.V_a = Convolution2D(1, self.nb_rows, self.nb_cols, border_mode='same', bias=False, init=self.attentive_init) self.W_a.build((input_shape[0], self.nb_filters_att, input_shape[3], input_shape[4])) self.U_a.build((input_shape[0], self.nb_filters_in, input_shape[3], input_shape[4])) self.V_a.build((input_shape[0], self.nb_filters_att, input_shape[3], input_shape[4])) self.W_a.built = True self.U_a.built = True self.V_a.built = True self.W_i = Convolution2D(self.nb_filters_out, self.nb_rows, self.nb_cols, border_mode='same', bias=True, init=self.init) self.U_i = Convolution2D(self.nb_filters_out, self.nb_rows, self.nb_cols, border_mode='same', bias=True, init=self.inner_init) self.W_i.build((input_shape[0], self.nb_filters_in, input_shape[3], input_shape[4])) self.U_i.build((input_shape[0], self.nb_filters_out, input_shape[3], input_shape[4])) self.W_i.built = True self.U_i.built = True self.W_f = Convolution2D(self.nb_filters_out, self.nb_rows, self.nb_cols, border_mode='same', bias=True, init=self.init) self.U_f = Convolution2D(self.nb_filters_out, self.nb_rows, self.nb_cols, border_mode='same', bias=True, init=self.inner_init) self.W_f.build((input_shape[0], self.nb_filters_in, input_shape[3], input_shape[4])) self.U_f.build((input_shape[0], self.nb_filters_out, input_shape[3], input_shape[4])) self.W_f.built = True self.U_f.built = True self.W_c = Convolution2D(self.nb_filters_out, self.nb_rows, self.nb_cols, border_mode='same', bias=True, init=self.init) self.U_c = Convolution2D(self.nb_filters_out, self.nb_rows, self.nb_cols, border_mode='same', bias=True, init=self.inner_init) self.W_c.build((input_shape[0], self.nb_filters_in, input_shape[3], input_shape[4])) self.U_c.build((input_shape[0], self.nb_filters_out, input_shape[3], input_shape[4])) self.W_c.built = True self.U_c.built = True self.W_o = Convolution2D(self.nb_filters_out, self.nb_rows, self.nb_cols, border_mode='same', bias=True, init=self.init) self.U_o = Convolution2D(self.nb_filters_out, self.nb_rows, self.nb_cols, border_mode='same', bias=True, init=self.inner_init) self.W_o.build((input_shape[0], self.nb_filters_in, input_shape[3], input_shape[4])) self.U_o.build((input_shape[0], self.nb_filters_out, input_shape[3], input_shape[4])) self.W_o.built = True self.U_o.built = True self.trainable_weights = [] self.trainable_weights.extend(self.W_a.trainable_weights) self.trainable_weights.extend(self.U_a.trainable_weights) self.trainable_weights.extend(self.V_a.trainable_weights) self.trainable_weights.extend(self.W_i.trainable_weights) self.trainable_weights.extend(self.U_i.trainable_weights) self.trainable_weights.extend(self.W_f.trainable_weights) self.trainable_weights.extend(self.U_f.trainable_weights) self.trainable_weights.extend(self.W_c.trainable_weights) self.trainable_weights.extend(self.U_c.trainable_weights) self.trainable_weights.extend(self.W_o.trainable_weights) self.trainable_weights.extend(self.U_o.trainable_weights)
batch_size = 32 samples_per_epoch = 1000 validation_steps = 300 nb_filters1 = 32 nb_filters2 = 64 conv1_size = 3 conv2_size = 2 pool_size = 2 classes_num = 3 lr = 0.0004 model = Sequential() model.add( Convolution2D(nb_filters1, conv1_size, conv1_size, border_mode="same", input_shape=(img_width, img_height, 3))) model.add(Activation("relu")) model.add(MaxPooling2D(pool_size=(pool_size, pool_size))) model.add( Convolution2D(nb_filters2, conv2_size, conv2_size, border_mode="same")) model.add(Activation("relu")) model.add(MaxPooling2D(pool_size=(pool_size, pool_size), dim_ordering='th')) model.add(Flatten()) model.add(Dense(256)) model.add(Activation("relu")) model.add(Dropout(0.5)) model.add(Dense(classes_num, activation='softmax'))
print(X_train.shape, y_train.shape) print(y_train.max(), y_train.min()) from keras.models import Sequential from keras.layers.core import Dense, Activation, Flatten, Lambda, Dropout from keras.layers.convolutional import Convolution2D from keras.layers.pooling import MaxPooling2D from keras.layers import Cropping2D model = Sequential() model.add(Lambda(lambda x: x / 255.0 - 0.5, input_shape=(160, 320, 3))) model.add(Cropping2D(cropping=((70, 25), (0, 0)))) # Layer 1 model.add(Convolution2D(24, 5, 5)) model.add(Activation('relu')) model.add(Dropout(0.8)) model.add(MaxPooling2D(pool_size=(2, 2))) # Layer 2 model.add(Convolution2D(36, 5, 5)) model.add(Activation('relu')) model.add(Dropout(0.7)) model.add(MaxPooling2D(pool_size=(2, 2))) # Layer 3 model.add(Convolution2D(48, 5, 5)) model.add(Activation('relu')) model.add(Dropout(0.7)) model.add(MaxPooling2D(pool_size=(2, 2)))
def comp_two_path(self): ''' compiles two-path model, takes in a 4x33x33 patch and assesses global and local paths, then merges the results. ''' print('Compiling two-path model...') #model = Graph() model.add_input(name='input', input_shape=(self.n_chan, 33, 33)) # local pathway, first convolution/pooling model.add_node(Convolution2D(64, 7, 7, border_mode='valid', activation='relu', W_regularizer=l1l2(l1=0.01, l2=0.01)), name='local_c1', input='input') model.add_node(MaxPooling2D(pool_size=(4, 4), strides=(1, 1), border_mode='valid'), name='local_p1', input='local_c1') # local pathway, second convolution/pooling model.add_node(Dropout(0.5), name='drop_lp1', input='local_p1') model.add_node(Convolution2D(64, 3, 3, border_mode='valid', activation='relu', W_regularizer=l1l2(l1=0.01, l2=0.01)), name='local_c2', input='drop_lp1') model.add_node(MaxPooling2D(pool_size=(2, 2), strides=(1, 1), border_mode='valid'), name='local_p2', input='local_c2') # global pathway model.add_node(Convolution2D(160, 13, 13, border_mode='valid', activation='relu', W_regularizer=l1l2(l1=0.01, l2=0.01)), name='global', input='input') # merge local and global pathways model.add_node(Dropout(0.5), name='drop_lp2', input='local_p2') model.add_node(Dropout(0.5), name='drop_g', input='global') model.add_node(Convolution2D(5, 21, 21, border_mode='valid', activation='relu', W_regularizer=l1l2(l1=0.01, l2=0.01)), name='merge', inputs=['drop_lp2', 'drop_g'], merge_mode='concat', concat_axis=1) # Flatten output of 5x1x1 to 1x5, perform softmax model.add_node(Flatten(), name='flatten', input='merge') model.add_node(Dense(5, activation='softmax'), name='dense_output', input='flatten') model.add_output(name='output', input='dense_output') sgd = SGD(lr=0.005, decay=0.1, momentum=0.9) model.compile('sgd', loss={'output': 'categorical_crossentropy'}) print('Done.') return model
train_samples, validation_samples = train_test_split(lines, test_size=0.2) # compile and train the model using the generator function train_generator = generator(train_samples, batch_size=32) validation_generator = generator(validation_samples, batch_size=32) model = Sequential() # model.add(Lambda(lambda x: (x / 127.5) - 1.0, input_shape=(160, 320, 3), output_shape=(160, 320, 3))) model.add( Lambda(lambda x: (x / 255.0) - 0.5, input_shape=(160, 320, 3), output_shape=(160, 320, 3))) model.add(Cropping2D(cropping=((70, 25), (0, 0)))) model.add(Convolution2D(24, (5, 5), strides=(2, 2), activation="relu")) model.add(Dropout(0.4)) model.add(Convolution2D(36, (5, 5), strides=(2, 2), activation="relu")) model.add(Dropout(0.4)) model.add(Convolution2D(48, (5, 5), strides=(2, 2), activation="relu")) model.add(Dropout(0.4)) model.add(Convolution2D(64, (3, 3), strides=(1, 1), activation="relu")) model.add(Dropout(0.4)) model.add(Convolution2D(64, (3, 3), strides=(1, 1), activation="relu")) model.add(Dropout(0.4)) model.add(Flatten()) model.add(Dense(100)) model.add(Dense(50)) model.add(Dense(10)) model.add(Dense(1))
def create_model(self, height=32, width=32, channels=3, load_weights=False, batch_size=128): """ Creates a model to remove / reduce noise from upscaled images. """ from keras.layers.convolutional import Deconvolution2D # Perform check that model input shape is divisible by 4 init = super(DenoisingAutoEncoderSR, self).create_model(height, width, channels, load_weights, batch_size) if K.image_dim_ordering() == "th": output_shape = (None, channels, width, height) else: output_shape = (None, width, height, channels) level1_1 = Convolution2D(self.n1, 3, 3, activation='relu', border_mode='same')(init) level2_1 = Convolution2D(self.n1, 3, 3, activation='relu', border_mode='same')(level1_1) level2_2 = Deconvolution2D(self.n1, 3, 3, activation='relu', output_shape=output_shape, border_mode='same')(level2_1) level2 = merge([level2_1, level2_2], mode='sum') level1_2 = Deconvolution2D(self.n1, 3, 3, activation='relu', output_shape=output_shape, border_mode='same')(level2) level1 = merge([level1_1, level1_2], mode='sum') decoded = Convolution2D(channels, 5, 5, activation='linear', border_mode='same')(level1) model = Model(init, decoded) adam = optimizers.Adam(lr=1e-3) model.compile(optimizer=adam, loss='mse', metrics=[PSNRLoss]) if load_weights: model.load_weights(self.weight_path) self.model = model return model
y_train = np.array(augmented_measurements) #X_train = np.array(images) #y_train = np.array(measurements) from keras.models import Sequential from keras.layers import Flatten, Dense, Lambda from keras.layers.pooling import MaxPooling2D from keras.layers.convolutional import Convolution2D from keras.models import load_model from keras.optimizers import Adam model = Sequential() model.add(Lambda(lambda x: x / 255.0 - 0.5, input_shape=(160, 320, 3))) model.add(Cropping2D(cropping=((75, 20), (0, 0)))) model.add(Convolution2D(24, 5, 5, subsample=(2, 2), activation="relu")) #model.add(MaxPooling2D()) model.add(Convolution2D(36, 5, 5, subsample=(2, 2), activation="relu")) #model.add(MaxPooling2D()) model.add(Convolution2D(48, 5, 5, subsample=(2, 2), activation="relu")) #model.add(MaxPooling2D()) model.add(Convolution2D(64, 3, 3, activation="relu")) model.add(Convolution2D(128, 3, 3, activation="relu")) model.add(Flatten()) model.add(Dense(100)) model.add(Dense(50)) model.add(Dense(10)) model.add(Dense(1)) optimizer = Adam(lr=0.0001)
nb_style_images = nb_tensors - 2 # Content and Output image not considered # combine the various images into a single Keras tensor input_tensor = K.concatenate(image_tensors, axis=0) if K.image_dim_ordering() == "th": shape = (nb_tensors, 3, img_width, img_height) else: shape = (nb_tensors, img_width, img_height, 3) ip = Input(tensor=input_tensor, shape=shape) # build the VGG16 network with our 3 images as input x = Convolution2D(64, 3, 3, activation='relu', name='conv1_1', border_mode='same')(ip) x = Convolution2D(64, 3, 3, activation='relu', name='conv1_2', border_mode='same')(x) x = pooling_func(x) x = Convolution2D(128, 3, 3, activation='relu', name='conv2_1',
from keras.models import Sequential from keras.layers.core import Dense, Activation, Flatten from keras.layers.convolutional import Convolution2D from keras.layers.pooling import MaxPooling2D from keras.optimizers import SGD from keras.utils import np_utils from data_generator import generator as GE import numpy as np model = Sequential() model.add( Convolution2D(4, 2, 2, activation='tanh', border_mode='valid', input_shape=(5, 8, 8))) # model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Convolution2D(8, 2, 2, activation='tanh', border_mode='valid')) # model.add(MaxPooling2D(pool_size=(2, 2), border_mode='valid')) model.add(Convolution2D(16, 2, 2, activation='tanh', border_mode='valid')) # model.add(MaxPooling2D(pool_size=(2, 2), border_mode='valid')) model.add(Flatten()) model.add(Dense(128, init='uniform')) model.add(Activation('tanh')) model.add(Dense(64, init='uniform')) model.add(Activation('softmax'))