def MNet64( input_shape=None, depth_multiplier=1, # DepthwiseConv2D param dropout=1e-3, input_tensor=None, classes=1000): if input_tensor is None: img_input = layers.Input(shape=input_shape) else: if not backend.is_keras_tensor(input_tensor): img_input = layers.Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor # Type / Stride Filter Shape Input Size # Conv / s2 3 × 3 × 3 × 32 224 × 224 × 3 3 × 3 × 1 × 32 64 x 64 x 1 S1! # Conv dw / s1 3 × 3 × 32 dw 112 × 112 × 32 64 x 64 x 32 # Conv / s1 1 × 1 × 32 × 64 112 × 112 × 32 64 x 64 x 32 # Conv dw / s2 3 × 3 × 64 dw 112 × 112 × 64 64 x 64 x 64 S1! # Conv / s1 1 × 1 × 64 × 128 56 × 56 × 64 64 x 64 x 64 # Conv dw / s1 3 × 3 × 128 dw 56 × 56 × 128 64 x 64 x 128 # Conv / s1 1 × 1 × 128 × 128 56 × 56 × 128 64 x 64 x 128 # Conv dw / s2 3 × 3 × 128 dw 56 × 56 × 128 64 x 64 x 128 # Conv / s1 1 × 1 × 128 × 256 28 × 28 × 128 32 x 32 x 128 # Conv dw / s1 3 × 3 × 256 dw 28 × 28 × 256 32 x 32 x 256 # Conv / s1 1 × 1 × 256 × 256 28 × 28 × 256 32 x 32 x 256 # Conv dw / s2 3 × 3 × 256 dw 28 × 28 × 256 32 x 32 x 256 # Conv / s1 1 × 1 × 256 × 512 14 × 14 × 256 16 x 16 x 256 # 5× # Conv dw / s1 3 × 3 × 512 dw 14 × 14 × 512 16 x 16 x 512 # Conv / s1 1 × 1 × 512 × 512 14 × 14 × 512 16 x 16 x 512 # Conv dw / s2 3 × 3 × 512 dw 14 × 14 × 512 16 x 16 x 512 # Conv / s1 1 × 1 × 512 × 1024 7 × 7 × 512 8 x 8 x 512 # Conv dw / s2 3 × 3 × 1024 dw 7 × 7 × 1024 8 x 8 x 1024 # Conv / s1 1 × 1 × 1024 × 1024 7 × 7 × 1024 8 x 8 x 1024 # Avg Pool / s1 Pool 7 × 7 7 × 7 × 1024 8 × 8 8 x 8 x 1024 # FC / s1 1024 × 1000 1 × 1 × 1024 1 × 1 × 1024 # Softmax / s1 1 × 1 × 1000 1 x 1 x NKanji init = initializers.TruncatedNormal(stddev=0.09) x = _conv_block(img_input, 32, strides=(1, 1), kernel_initializer=init) # strides=(2, 2) x = _depthwise_conv_block(x, 64, depth_multiplier, block_id=1, kernel_initializer=init) x = _depthwise_conv_block(x, 128, depth_multiplier, strides=(1, 1), block_id=2, kernel_initializer=init) # strides=(2, 2) x = _depthwise_conv_block(x, 128, depth_multiplier, block_id=3, kernel_initializer=init) x = _depthwise_conv_block(x, 256, depth_multiplier, strides=(2, 2), block_id=4, kernel_initializer=init) x = _depthwise_conv_block(x, 256, depth_multiplier, block_id=5, kernel_initializer=init) x = _depthwise_conv_block(x, 512, depth_multiplier, strides=(2, 2), block_id=6, kernel_initializer=init) x = _depthwise_conv_block(x, 512, depth_multiplier, block_id=7, kernel_initializer=init) x = _depthwise_conv_block(x, 512, depth_multiplier, block_id=8, kernel_initializer=init) x = _depthwise_conv_block(x, 512, depth_multiplier, block_id=9, kernel_initializer=init) x = _depthwise_conv_block(x, 512, depth_multiplier, block_id=10, kernel_initializer=init) x = _depthwise_conv_block(x, 512, depth_multiplier, block_id=11, kernel_initializer=init) x = _depthwise_conv_block(x, 1024, depth_multiplier, strides=(2, 2), block_id=12, kernel_initializer=init) x = _depthwise_conv_block(x, 1024, depth_multiplier, block_id=13, kernel_initializer=init) if backend.image_data_format() == 'channels_first': shape = (1024, 1, 1) else: shape = (1, 1, 1024) x = layers.GlobalAveragePooling2D()(x) x = layers.Reshape(shape, name='reshape_1')(x) x = layers.Dropout(dropout, name='dropout')(x) x = layers.Conv2D(classes, (1, 1), padding='same', kernel_initializer=init, name='conv_preds')(x) x = layers.Activation('softmax', name='act_softmax')(x) x = layers.Reshape((classes, ), name='reshape_2')(x) model = models.Model(img_input, x, name='mobilenet') return model
def __init__(self, shape): self.re_rate = 0.9 self.inputs = layers.Input(shape=shape) ac = layers.LeakyReLU(alpha=0.3) self.f_block = layers.Conv3D(4, (5, 5, 5), activation=ac, kernel_regularizer=regularizers.l2( self.re_rate), padding='same')(self.inputs) self.bn = layers.BatchNormalization()(self.f_block) self.mp1 = layers.MaxPooling3D((2, 2, 2))(self.bn) self.f_block1 = layers.Conv3D(8, (5, 5, 5), activation=ac, kernel_regularizer=regularizers.l2( self.re_rate), padding='same')(self.mp1) self.bn = layers.BatchNormalization()(self.f_block1) self.mp2 = layers.MaxPooling3D((2, 2, 2))(self.bn) self.f_block2 = layers.Conv3D(16, (4, 4, 4), activation=ac, kernel_regularizer=regularizers.l2( self.re_rate), padding='same')(self.mp2) self.f_block2 = layers.BatchNormalization()(self.f_block2) self.mp3 = layers.MaxPooling3D((2, 2, 2))(self.f_block2) self.f_block3 = layers.Conv3D(16, (3, 3, 3), activation=ac, kernel_regularizer=regularizers.l2( self.re_rate), padding='same')(self.mp3) self.f_block3 = layers.BatchNormalization()(self.f_block3) self.b_back3 = layers.Conv3D(16, (3, 3, 3), activation=ac, kernel_regularizer=regularizers.l2( self.re_rate), padding='same')(self.f_block3) self.b_back3 = layers.BatchNormalization()(self.b_back3) self.cat1 = layers.concatenate([self.f_block3, self.b_back3]) self.bn4 = layers.BatchNormalization()(self.cat1) self.b_back2 = layers.Conv3D( 16, (3, 3, 3), activation=ac, kernel_regularizer=regularizers.l2(self.re_rate), padding='same')(layers.UpSampling3D((2, 2, 2))(self.bn4)) self.b_back2 = layers.BatchNormalization()(self.b_back2) self.cat2 = layers.concatenate([self.mp2, self.b_back2]) self.b_back1 = layers.Conv3D( 16, (3, 3, 3), activation=ac, kernel_regularizer=regularizers.l2(self.re_rate), padding='same')(layers.UpSampling3D((2, 2, 2))(self.cat2)) self.b_back1 = layers.BatchNormalization()(self.b_back1) self.cat3 = layers.concatenate([self.mp1, self.b_back1]) self.gb = layers.GlobalAveragePooling3D()(self.cat3) # self.gb = layers.GlobalMaxPooling3D()(self.cat3) self.dense1 = layers.Dropout(rate=0.3)(self.gb) self.dense3 = layers.Dense(1, activation='sigmoid')(self.dense1) self.model = keras.Model(input=self.inputs, output=self.dense3)
#Building model with best parameters model = models.Sequential() model.add( layers.Conv2D(32, (5, 5), activation='relu', input_shape=(299, 299, 3))) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (5, 5), activation='relu')) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(128, (3, 3), activation='relu')) model.add(layers.AveragePooling2D((2, 2))) model.add(layers.Flatten()) model.add(layers.Dense(9, activation=a)) model.add(layers.Dropout(rate=c)) model.add(layers.Dense(9, activation='softmax')) #Compiling model.compile(loss='categorical_crossentropy', optimizer=e, metrics=['accuracy']) #Fitting hist = model.fit(images_train, labels_train, batch_size=b, epochs=d, validation_split=0.3) #Loss & Accuracy
def trainModel(): base_model = VGG16(weights=WEIGHTS, include_top=INCLUDE_TOP, input_shape=(HEIGHT, WIDTH, 3)) # Freeze the layers except the last 4 layers for layer in base_model.layers[:-4]: layer.trainable = False # Check the trainable status of the individual layers for layer in base_model.layers: print(layer, layer.trainable) # Create the model model = models.Sequential() # Add the vgg convolutional base model model.add(base_model) # Add new layers model.add(layers.Flatten()) model.add(layers.Dense(1024, activation='relu')) model.add(layers.Dropout(0.5)) model.add(layers.Dense(3, activation='softmax')) # Show a summary of the model. Check the number of trainable parameters model.summary() train_datagen = ImageDataGenerator( rescale=1./255, rotation_range=20, width_shift_range=0.2, height_shift_range=0.2, horizontal_flip=True, fill_mode='nearest' ) validation_datagen = ImageDataGenerator(rescale=1./255) train_generator = train_datagen.flow_from_directory( TRAIN_DIR, target_size=(HEIGHT, WIDTH), batch_size=BATCH_SIZE, class_mode='categorical') validation_generator = validation_datagen.flow_from_directory( VAL_DIR, target_size=(HEIGHT, WIDTH), batch_size=BATCH_SIZE, class_mode='categorical', shuffle=False) #adam = Adam(lr=0.00001) # Compile the model model.compile(loss='categorical_crossentropy', optimizer=RMSprop(lr=1e-4), metrics=['acc']) #model.compile(loss='categorical_crossentropy', # optimizer=adam, # metrics=['acc']) filepath="./checkpoints/" + "VGG16" + "_model_weights.h5" checkpoint = ModelCheckpoint(filepath, monitor=["acc"], verbose=1, mode='max') callbacks_list = [checkpoint] # Train the model history = model.fit_generator( train_generator, steps_per_epoch=num_train_images // BATCH_SIZE , epochs=args.num_epochs, validation_data=validation_generator, validation_steps=num_val_images // BATCH_SIZE, verbose=1, callbacks=callbacks_list) acc = history.history['acc'] val_acc = history.history['val_acc'] loss = history.history['loss'] val_loss = history.history['val_loss'] epochs = range(len(acc)) plt.plot(epochs, acc, 'b', label='Training acc') plt.plot(epochs, val_acc, 'r', label='Validation acc') plt.title('Training and validation accuracy') plt.legend() plt.figure() plt.plot(epochs, loss, 'b', label='Training loss') plt.plot(epochs, val_loss, 'r', label='Validation loss') plt.title('Training and validation loss') plt.legend() plt.show()
imageCopy(origin_dir, train_dogs_dir, name='dog', range_num=(0, 1000)) imageCopy(origin_dir, validation_dogs_dir, name='dog', range_num=(1000, 1500)) imageCopy(origin_dir, test_dogs_dir, name='dog', range_num=(1500, 2000)) ## keras model building ## model = models.Sequential() model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3))) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3, 3), activation='relu')) model.add(layers.MaxPool2D((2, 2))) model.add(layers.Conv2D(128, (3, 3), activation='relu')) model.add(layers.MaxPool2D((2, 2))) model.add(layers.Conv2D(128, (3, 3), activation='relu')) model.add(layers.MaxPool2D((2, 2))) model.add(layers.Flatten()) model.add(layers.Dropout((0.5))) model.add(layers.Dense(512, activation='relu')) model.add(layers.Dense(1, activation='sigmoid')) model.summary() model.compile(optimizers.adam(lr=0.0001), loss='binary_crossentropy', metrics=['acc']) ## preprocessing ## train_datagen = ImageDataGenerator(rescale=1./255) test_datagen = ImageDataGenerator(rescale=1./255) # pixel (0, 255) --> (0, 1) # rescale / class num # train_generator = train_datagen.flow_from_directory(train_dir, target_size=(150, 150), batch_size=20, class_mode='binary')
def design_dnn(nb_features, input_shape, nb_levels, conv_size, nb_labels, feat_mult=1, pool_size=2, padding='same', activation='elu', final_layer='dense-sigmoid', conv_dropout=0, conv_maxnorm=0, nb_input_features=1, batch_norm=False, name=None, prefix=None, use_strided_convolution_maxpool=True, nb_conv_per_level=2): """ "deep" cnn with dense or global max pooling layer @ end... Could use sequential... """ model_name = name if model_name is None: model_name = 'model_1' if prefix is None: prefix = model_name ndims = len(input_shape) input_shape = tuple(input_shape) convL = getattr(KL, 'Conv%dD' % ndims) maxpool = KL.MaxPooling3D if len(input_shape) == 3 else KL.MaxPooling2D if isinstance(pool_size, int): pool_size = (pool_size, ) * ndims # kwargs for the convolution layer conv_kwargs = {'padding': padding, 'activation': activation} if conv_maxnorm > 0: conv_kwargs['kernel_constraint'] = maxnorm(conv_maxnorm) # initialize a dictionary enc_tensors = {} # first layer: input name = '%s_input' % prefix enc_tensors[name] = KL.Input(shape=input_shape + (nb_input_features, ), name=name) last_tensor = enc_tensors[name] # down arm: # add nb_levels of conv + ReLu + conv + ReLu. Pool after each of first nb_levels - 1 layers for level in range(nb_levels): for conv in range(nb_conv_per_level): if conv_dropout > 0: name = '%s_dropout_%d_%d' % (prefix, level, conv) enc_tensors[name] = KL.Dropout(conv_dropout)(last_tensor) last_tensor = enc_tensors[name] name = '%s_conv_%d_%d' % (prefix, level, conv) nb_lvl_feats = np.round(nb_features * feat_mult**level).astype(int) enc_tensors[name] = convL(nb_lvl_feats, conv_size, **conv_kwargs, name=name)(last_tensor) last_tensor = enc_tensors[name] # max pool if use_strided_convolution_maxpool: name = '%s_strided_conv_%d' % (prefix, level) enc_tensors[name] = convL(nb_lvl_feats, pool_size, **conv_kwargs, name=name)(last_tensor) last_tensor = enc_tensors[name] else: name = '%s_maxpool_%d' % (prefix, level) enc_tensors[name] = maxpool(pool_size=pool_size, name=name, padding=padding)(last_tensor) last_tensor = enc_tensors[name] # dense layer if final_layer == 'dense-sigmoid': name = "%s_flatten" % prefix enc_tensors[name] = KL.Flatten(name=name)(last_tensor) last_tensor = enc_tensors[name] name = '%s_dense' % prefix enc_tensors[name] = KL.Dense(1, name=name, activation="sigmoid")(last_tensor) elif final_layer == 'dense-tanh': name = "%s_flatten" % prefix enc_tensors[name] = KL.Flatten(name=name)(last_tensor) last_tensor = enc_tensors[name] name = '%s_dense' % prefix enc_tensors[name] = KL.Dense(1, name=name)(last_tensor) last_tensor = enc_tensors[name] # Omittting BatchNorm for now, it seems to have a cpu vs gpu problem # https://github.com/tensorflow/tensorflow/pull/8906 # https://github.com/fchollet/keras/issues/5802 # name = '%s_%s_bn' % prefix # enc_tensors[name] = KL.BatchNormalization(axis=batch_norm, name=name)(last_tensor) # last_tensor = enc_tensors[name] name = '%s_%s_tanh' % prefix enc_tensors[name] = KL.Activation(activation="tanh", name=name)(last_tensor) elif final_layer == 'dense-softmax': name = "%s_flatten" % prefix enc_tensors[name] = KL.Flatten(name=name)(last_tensor) last_tensor = enc_tensors[name] name = '%s_dense' % prefix enc_tensors[name] = KL.Dense(nb_labels, name=name, activation="softmax")(last_tensor) # global max pooling layer elif final_layer == 'myglobalmaxpooling': name = '%s_batch_norm' % prefix enc_tensors[name] = KL.BatchNormalization(axis=batch_norm, name=name)(last_tensor) last_tensor = enc_tensors[name] name = '%s_global_max_pool' % prefix enc_tensors[name] = KL.Lambda(_global_max_nd, name=name)(last_tensor) last_tensor = enc_tensors[name] name = '%s_global_max_pool_reshape' % prefix enc_tensors[name] = KL.Reshape((1, 1), name=name)(last_tensor) last_tensor = enc_tensors[name] # cannot do activation in lambda layer. Could code inside, but will do extra lyaer name = '%s_global_max_pool_sigmoid' % prefix enc_tensors[name] = KL.Conv1D(1, 1, name=name, activation="sigmoid", use_bias=True)(last_tensor) elif final_layer == 'globalmaxpooling': name = '%s_conv_to_featmaps' % prefix enc_tensors[name] = KL.Conv3D(2, 1, name=name, activation="relu")(last_tensor) last_tensor = enc_tensors[name] name = '%s_global_max_pool' % prefix enc_tensors[name] = KL.GlobalMaxPooling3D(name=name)(last_tensor) last_tensor = enc_tensors[name] # cannot do activation in lambda layer. Could code inside, but will do extra lyaer name = '%s_global_max_pool_softmax' % prefix enc_tensors[name] = KL.Activation('softmax', name=name)(last_tensor) last_tensor = enc_tensors[name] # create the model model = Model(inputs=[enc_tensors['%s_input' % prefix]], outputs=[last_tensor], name=model_name) return model
fully_connected_size = int(input("Fully connected layer size: ")) epochs_phase_1 = int(input("Phase 1 epochs: ")) epochs_phase_2 = int(input("Phase 2 epochs: ")) batch_size = int(input("Batch size: ")) dropout_rate = float(input("Dropout rate: ")) # Clear previous layer session to match continiously constructed layer names in weights file K.clear_session() # Construct the classifier NN(input -> encoder -> Flatten -> FC -> output with 10 classes(0 - 9)) encoded = encoder(input_img, convolutional_layers, convolutional_filter_size, convolutional_filters_per_layer, dropout_rate) flatten = layers.Flatten()(encoded) fc = layers.Dense(fully_connected_size, activation='relu')(flatten) dropout = layers.Dropout(rate=dropout_rate)(fc) output_layer = layers.Dense(training_labels.num_classes(), activation='softmax')(dropout) classifier = Model(input_img, output_layer) classifier.compile(loss='categorical_crossentropy', optimizer=optimizers.Adam()) # Print it's summary classifier.summary() # Load encoder weights classifier.load_weights(autoencoder_weights_file, by_name=True) # Train phase 1: Only fully connected layer weights
def VGGNet(): image_input = Input(shape=(256, 256, 3), name='image_input') # x = CoordinateChannel2D()(inp) x = kl.Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding='same', name='block1_conv1')(image_input) x = kl.Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding='same', name='block1_conv2')(x) x = kl.Conv2D(filters=64, kernel_size=2, strides=2, activation='relu', padding='same', name='block1_reduction_conv')(x) x = kl.BatchNormalization()(x) x = kl.Dropout(0.5)(x) x = kl.Conv2D(filters=128, kernel_size=(3, 3), activation='relu', padding='same', name='block2_conv1')(x) x = kl.Conv2D(filters=128, kernel_size=(3, 3), activation='relu', padding='same', name='block2_conv2')(x) x = kl.Conv2D(filters=128, kernel_size=2, strides=2, activation='relu', padding='same', name='block2_reduction_conv')(x) x = kl.BatchNormalization()(x) x = kl.Dropout(0.5)(x) x = kl.Conv2D(filters=256, kernel_size=(3, 3), activation='relu', padding='same', name='block3_conv1')(x) x = kl.Conv2D(filters=256, kernel_size=(3, 3), activation='relu', padding='same', name='block3_conv2')(x) x = kl.Conv2D(filters=256, kernel_size=(3, 3), activation='relu', padding='same', name='block3_conv3')(x) x = kl.Conv2D(filters=256, kernel_size=2, strides=2, activation='relu', padding='same', name='block3_reduction_conv')(x) x = kl.BatchNormalization()(x) x = kl.Dropout(0.5)(x) x = kl.Conv2D(filters=512, kernel_size=(3, 3), activation='relu', padding='same', name='block4_conv1')(x) x = kl.Conv2D(filters=512, kernel_size=(3, 3), activation='relu', padding='same', name='block4_conv2')(x) x = kl.Conv2D(filters=512, kernel_size=(3, 3), activation='relu', padding='same', name='block4_conv3')(x) # x = CoordinateChannel2D(use_radius=True)(x) # x, samap, g = SelfAttention(ch=512, name='self_attention')(x) x = kl.Conv2D(filters=512, kernel_size=2, strides=2, activation='relu', padding='same', name='block4_reduction_conv')(x) x = kl.BatchNormalization()(x) x = kl.Dropout(0.5)(x) x = kl.Conv2D(filters=512, kernel_size=(3, 3), activation='relu', padding='same', name='block5_conv1')(x) x = kl.Conv2D(filters=512, kernel_size=(3, 3), activation='relu', padding='same', name='block5_conv2')(x) x = kl.Conv2D(filters=512, kernel_size=(3, 3), activation='relu', padding='same', name='block5_conv3')(x) # x, amaps = SoftAttention(ch=512, m=32, name='soft_attention')(x) x = kl.Conv2D(filters=512, kernel_size=2, strides=2, activation='relu', padding='same', name='block5_reduction_conv')(x) return Model(image_input, x, name='imgModel')
def DenseNet(): qw = Input(shape=(256, 256, 3), name='image_input') qw_1 = kl.Conv2D(strides=1, padding='valid', activation='relu', filters=64, name='conv', kernel_size=3)(qw) qw_1 = densenet.densenet.conv_block( x=qw_1, growth_rate=64, name='conv_1', ) qw_2 = densenet.densenet.dense_block(qw_1, blocks=1, name='block_1') qw_2 = kl.BatchNormalization()(qw_2) qw_2 = kl.Activation('relu')(qw_2) qw_2 = kl.Conv2D(filters=64, kernel_size=2, strides=2, activation='relu', padding='same', name='block1_reduction_conv')(qw_2) qw_2 = kl.Dropout(0.5)(qw_2) qw_2 = densenet.densenet.dense_block(qw_2, blocks=1, name='block_2') qw_2 = kl.BatchNormalization()(qw_2) qw_2 = kl.Activation('relu')(qw_2) # qw_2 = kl.MaxPool2D(pool_size=2)(qw_2) qw_2 = kl.Conv2D(filters=128, kernel_size=2, strides=2, activation='relu', padding='same', name='block2_reduction_conv')(qw_2) qw_2 = kl.Dropout(0.5)(qw_2) qw_2 = densenet.densenet.dense_block(qw_2, blocks=1, name='block_3') qw_2 = kl.BatchNormalization()(qw_2) qw_2 = kl.Activation('relu')(qw_2) # qw_2 = kl.MaxPool2D(pool_size=2)(qw_2) qw_2 = kl.Conv2D(filters=256, kernel_size=2, strides=2, activation='relu', padding='same', name='block3_reduction_conv')(qw_2) qw_2 = kl.Dropout(0.5)(qw_2) qw_2 = densenet.densenet.dense_block(qw_2, blocks=1, name='block_4') qw_2 = kl.BatchNormalization()(qw_2) qw_2 = kl.Activation('relu')(qw_2) # qw_2 = kl.MaxPool2D(pool_size=2)(qw_2) qw_2 = kl.Conv2D(filters=512, kernel_size=2, strides=2, activation='relu', padding='same', name='block4_reduction_conv')(qw_2) qw_2 = kl.Dropout(0.5)(qw_2) qw_2 = densenet.densenet.dense_block(qw_2, blocks=1, name='block_5') qw_2 = kl.BatchNormalization()(qw_2) qw_2 = kl.Activation('relu')(qw_2) # qw_2 = kl.MaxPool2D(pool_size=2)(qw_2) qw_2 = kl.Conv2D(filters=1024, kernel_size=2, strides=2, activation='relu', padding='same', name='block5_reduction_conv')(qw_2) # qw_2 = kl.Dropout(0.5)(qw_2) qw_2 = kl.BatchNormalization()(qw_2) qw_2 = kl.Activation('relu')(qw_2) return Model(qw, qw_2, name='imgModel')
def get_model(outputs, gpus=1): model = None if os.path.isfile(WEIGHTS_FILE) and False: model = models.load_model(WEIGHTS_FILE) print('model loaded') else: inp = layers.Input(shape=( 8, 8, 12, )) flat_inp = layers.Flatten()(inp) y = inp y = residual_block(y, 128, _project_shortcut=True) for i in range(25): y = residual_block(y, 128, _project_shortcut=False) y = layers.Flatten()(y) inp2 = layers.Input(shape=(4, )) y = layers.concatenate([y, flat_inp, inp2]) y = layers.Dense(1024, activation='relu', kernel_regularizer=regularizers.l2(WEIGHT_DECAY))(y) y = layers.Dropout(0.1)(y) y_policy = layers.Dropout(0.2)(y) y_policy = layers.Dense( 1024, activation='relu', kernel_regularizer=regularizers.l2(WEIGHT_DECAY))(y_policy) y_policy = layers.Dropout(0.1)(y_policy) y_policy = layers.Dense( 2048, activation='relu', kernel_regularizer=regularizers.l2(WEIGHT_DECAY))(y_policy) y_policy = layers.Dropout(0.1)(y_policy) y_policy = layers.Dense( outputs, activation='relu', kernel_regularizer=regularizers.l2(WEIGHT_DECAY))(y_policy) y_policy = layers.Activation("softmax")(y_policy) y_value = layers.Dropout(0.2)(y) y_value = layers.Dense( 1024, activation='relu', kernel_regularizer=regularizers.l2(WEIGHT_DECAY))(y_value) y_value = layers.Dropout(0.1)(y_value) y_value = layers.Dense( 2048, activation='relu', kernel_regularizer=regularizers.l2(WEIGHT_DECAY))(y_value) y_value = layers.Dropout(0.1)(y_value) y_value = layers.Dense( 2, activation='relu', kernel_regularizer=regularizers.l2(WEIGHT_DECAY))(y_value) y_value = layers.Activation("softmax")(y_value) model = models.Model(inputs=[inp, inp2], outputs=[y_policy, y_value]) # model.summary() model.compile(optimizer=optimizers.SGD(lr=0.001, momentum=0.6), loss='categorical_crossentropy', loss_weights=[1., 1.], metrics=['accuracy']) if os.path.isfile(WEIGHTS_FILE): model.load_weights(WEIGHTS_FILE) return model, model
textNet = textNet() textNet.summary() # In[301]: SVG( model_to_dot(textNet, show_layer_names=True, show_shapes=True).create(prog='dot', format='svg')) # # Define Complete Joint Network # In[302]: image_input = Input(shape=(256, 256, 3), name='image_input') image_features = imgNet(image_input) image_features = kl.Dropout(0.1)(image_features) image_features = kl.Flatten()(image_features) image_features = kl.Dense(512, activation='relu')(image_features) words_input = kl.Input(shape=(max_wlen, ), name='words_input') text_features = textNet(words_input) text_features = kl.Dropout(0.1)(text_features) text_features = kl.Dense(512, activation='relu')(text_features) g = kl.Concatenate()([image_features, text_features]) g = kl.Dense(512, activation='relu')(g) target = Dense(vocab_size + 1, activation='softmax', name='target_word')(g) model = Model([image_input, words_input], target) # In[303]:
def VGG16(): # Determine proper input shape input_shape = (128, 128, 200) img_input = layers.Input(shape=input_shape) # Block 1 x = layers.Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input) x = layers.Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x) x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) # Block 2 x = layers.Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x) x = layers.Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x) x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) # Block 3 x = layers.Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x) x = layers.Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x) x = layers.Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x) x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) # Block 4 x = layers.Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x) x = layers.Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x) x = layers.Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x) x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x) # Block 5 x = layers.Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x) x = layers.Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x) x = layers.Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x) x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x) # Classification block x = layers.Flatten(name='flatten')(x) x = layers.Dropout(0.3)(x) x = layers.Dense(4096, activation='relu', name='fc1')(x) x = layers.Dropout(0.3)(x) x = layers.Dense(4096, activation='relu', name='fc2')(x) #x = layers.Dense(2, name='predictions')(x) inputs = img_input # Create model. #model = models.Model(inputs, x, name='vgg16') y1 = layers.Dense(11, activation='softmax')(x) y2 = layers.Dense(11, activation='softmax')(x) model = Model(inputs=inputs, outputs=[y1, y2]) model.load_weights("weight/model_2_v1_tf.hdf5") adam = Adam(lr=0.0001) model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy']) return model
trains_l = trainig_data_labels[0:trains_nums] print(trains_d.shape) print(trains_l.shape) val_d = trainig_data[trains_nums:val_nums + trains_nums] val_l = trainig_data_labels[trains_nums:val_nums + trains_nums] # graph input_tensor = Input(shape=(13, )) x = layers.Dense( 64, activation='relu', kernel_regularizer=keras.regularizers.l2(0.001))(input_tensor) x = layers.Dense(128, activation='relu', kernel_regularizer=keras.regularizers.l2(0.001))(x) x = layers.Dropout(0.5)(x) x = layers.Dense(64, activation='relu', kernel_regularizer=keras.regularizers.l2(0.001))(x) output_tensor = layers.Dense(14, activation='softmax')(x) model = Model(input_tensor, output_tensor) model.summary() model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) callback = [ keras.callbacks.TensorBoard( log_dir="log_dir",
from keras import layers, models from loader import prepare_uic_data X, Y = prepare_uic_data() X = X.reshape(*X.shape, 1) model = models.Sequential() model.add(layers.Conv2D(32, (5, 5), activation='relu', input_shape=X.shape[1:])) model.add(layers.MaxPooling2D()) model.add(layers.Conv2D(32, (5, 5), activation='relu')) model.add(layers.MaxPooling2D()) model.add(layers.Flatten()) model.add(layers.Dense(512, activation='relu')) model.add(layers.Dropout(.2)) model.add(layers.Dense(2, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy']) model.fit(X, Y, validation_split=.2, epochs=10) model.save('uic_cnn.h5')
def conv_enc(nb_features, input_shape, nb_levels, conv_size, name=None, prefix=None, feat_mult=1, pool_size=2, dilation_rate_mult=1, padding='same', activation='elu', layer_nb_feats=None, use_residuals=False, nb_conv_per_level=2, conv_dropout=0, batch_norm=None): """ Fully Convolutional Encoder """ # naming model_name = name if prefix is None: prefix = model_name # volume size data ndims = len(input_shape) - 1 input_shape = tuple(input_shape) if isinstance(pool_size, int): pool_size = (pool_size, ) * ndims # prepare layers convL = getattr(KL, 'Conv%dD' % ndims) conv_kwargs = {'padding': padding, 'activation': activation} maxpool = getattr(KL, 'MaxPooling%dD' % ndims) # first layer: input name = '%s_input' % prefix last_tensor = KL.Input(shape=input_shape, name=name) input_tensor = last_tensor # down arm: # add nb_levels of conv + ReLu + conv + ReLu. Pool after each of first nb_levels - 1 layers lfidx = 0 for level in range(nb_levels): lvl_first_tensor = last_tensor nb_lvl_feats = np.round(nb_features * feat_mult**level).astype(int) conv_kwargs['dilation_rate'] = dilation_rate_mult**level for conv in range(nb_conv_per_level): if layer_nb_feats is not None: nb_lvl_feats = layer_nb_feats[lfidx] lfidx += 1 name = '%s_conv_downarm_%d_%d' % (prefix, level, conv) if conv < (nb_conv_per_level - 1) or (not use_residuals): last_tensor = convL(nb_lvl_feats, conv_size, **conv_kwargs, name=name)(last_tensor) else: # no activation last_tensor = convL(nb_lvl_feats, conv_size, padding=padding, name=name)(last_tensor) if conv_dropout > 0: # conv dropout along feature space only name = '%s_dropout_downarm_%d_%d' % (prefix, level, conv) noise_shape = [None, *[1] * ndims, nb_lvl_feats] last_tensor = KL.Dropout(conv_dropout, noise_shape=noise_shape)(last_tensor) if use_residuals: convarm_layer = last_tensor # the "add" layer is the original input # However, it may not have the right number of features to be added nb_feats_in = lvl_first_tensor.get_shape()[-1] nb_feats_out = convarm_layer.get_shape()[-1] add_layer = lvl_first_tensor if nb_feats_in > 1 and nb_feats_out > 1 and (nb_feats_in != nb_feats_out): name = '%s_expand_down_merge_%d' % (prefix, level) last_tensor = convL(nb_lvl_feats, conv_size, **conv_kwargs, name=name)(lvl_first_tensor) add_layer = last_tensor if conv_dropout > 0: name = '%s_dropout_down_merge_%d_%d' % (prefix, level, conv) noise_shape = [None, *[1] * ndims, nb_lvl_feats] last_tensor = KL.Dropout( conv_dropout, noise_shape=noise_shape)(last_tensor) name = '%s_res_down_merge_%d' % (prefix, level) last_tensor = KL.add([add_layer, convarm_layer], name=name) name = '%s_res_down_merge_act_%d' % (prefix, level) last_tensor = KL.Activation(activation, name=name)(last_tensor) if batch_norm is not None: name = '%s_bn_down_%d' % (prefix, level) last_tensor = KL.BatchNormalization(axis=batch_norm, name=name)(last_tensor) # max pool if we're not at the last level if level < (nb_levels - 1): name = '%s_maxpool_%d' % (prefix, level) last_tensor = maxpool(pool_size=pool_size, name=name, padding=padding)(last_tensor) # create the model and return model = Model(inputs=input_tensor, outputs=[last_tensor], name=model_name) return model
# add START and END to lines lines = [['START'] + l + ['END'] for l in lines] # force all lines to be same length maxlen = 0 for l in lines: if len(l) > maxlen: maxlen = len(l) for i in range(len(lines)): if len(lines[i]) < maxlen: lines[i] += ['BLANK'] * (maxlen - len(lines[i])) # condense list of paragraphs into an np tensor # dimensions: examples/sentences, character vectors, characters 1/0s data = np.zeros((len(lines), maxlen, num_chars)) for i, line in enumerate(lines): for j, c in enumerate(line): data[i][j][char_to_ind[c]] = 1 # split data into inputs and outputs seq_len = 100 X = np.zeros((0, seq_len, num_chars)) # create LSTM model lstm_input = kl.Input(shape=[maxlen, num_chars]) H = kl.LSTM(256)(lstm_input) H = kl.Dropout(0.2)(H) lstm_output = kl.Dense(num_chars, activation='softmax')(H) lstm = km.Model(lstm_input, lstm_output) lstm.compile(loss="categorical_crossentropy", optimizer="adam")
def conv_dec(nb_features, input_shape, nb_levels, conv_size, nb_labels, name=None, prefix=None, feat_mult=1, pool_size=2, use_skip_connections=False, padding='same', dilation_rate_mult=1, activation='elu', use_residuals=False, final_pred_activation='softmax', nb_conv_per_level=2, layer_nb_feats=None, batch_norm=None, conv_dropout=0, input_model=None): """ Fully Convolutional Decoder Parameters: ... use_skip_connections (bool): if true, turns an Enc-Dec to a U-Net. If true, input_tensor and tensors are required. It assumes a particular naming of layers. conv_enc... """ # naming model_name = name if prefix is None: prefix = model_name # if using skip connections, make sure need to use them. if use_skip_connections: assert input_model is not None, "is using skip connections, tensors dictionary is required" # first layer: input input_name = '%s_input' % prefix if input_model is None: input_tensor = KL.Input(shape=input_shape, name=input_name) last_tensor = input_tensor else: input_tensor = input_model.input last_tensor = input_model.output input_shape = last_tensor.shape.as_list()[1:] # vol size info ndims = len(input_shape) - 1 input_shape = tuple(input_shape) if isinstance(pool_size, int): if ndims > 1: pool_size = (pool_size, ) * ndims # prepare layers convL = getattr(KL, 'Conv%dD' % ndims) conv_kwargs = {'padding': padding, 'activation': activation} upsample = getattr(KL, 'UpSampling%dD' % ndims) # up arm: # nb_levels - 1 layers of Deconvolution3D # (approx via up + conv + ReLu) + merge + conv + ReLu + conv + ReLu lfidx = 0 for level in range(nb_levels - 1): nb_lvl_feats = np.round(nb_features * feat_mult**(nb_levels - 2 - level)).astype(int) conv_kwargs['dilation_rate'] = dilation_rate_mult**(nb_levels - 2 - level) # upsample matching the max pooling layers size name = '%s_up_%d' % (prefix, nb_levels + level) last_tensor = upsample(size=pool_size, name=name)(last_tensor) up_tensor = last_tensor # merge layers combining previous layer # TODO: add Cropping3D or Cropping2D if 'valid' padding if use_skip_connections: conv_name = '%s_conv_downarm_%d_%d' % ( prefix, nb_levels - 2 - level, nb_conv_per_level - 1) cat_tensor = input_model.get_layer(conv_name).output name = '%s_merge_%d' % (prefix, nb_levels + level) last_tensor = KL.concatenate([cat_tensor, last_tensor], axis=ndims + 1, name=name) # convolution layers for conv in range(nb_conv_per_level): if layer_nb_feats is not None: nb_lvl_feats = layer_nb_feats[lfidx] lfidx += 1 name = '%s_conv_uparm_%d_%d' % (prefix, nb_levels + level, conv) if conv < (nb_conv_per_level - 1) or (not use_residuals): last_tensor = convL(nb_lvl_feats, conv_size, **conv_kwargs, name=name)(last_tensor) else: last_tensor = convL(nb_lvl_feats, conv_size, padding=padding, name=name)(last_tensor) if conv_dropout > 0: name = '%s_dropout_uparm_%d_%d' % (prefix, level, conv) noise_shape = [None, *[1] * ndims, nb_lvl_feats] last_tensor = KL.Dropout(conv_dropout, noise_shape=noise_shape)(last_tensor) # residual block if use_residuals: # the "add" layer is the original input # However, it may not have the right number of features to be added add_layer = up_tensor nb_feats_in = add_layer.get_shape()[-1] nb_feats_out = last_tensor.get_shape()[-1] if nb_feats_in > 1 and nb_feats_out > 1 and (nb_feats_in != nb_feats_out): name = '%s_expand_up_merge_%d' % (prefix, level) add_layer = convL(nb_lvl_feats, conv_size, **conv_kwargs, name=name)(add_layer) if conv_dropout > 0: name = '%s_dropout_up_merge_%d_%d' % (prefix, level, conv) noise_shape = [None, *[1] * ndims, nb_lvl_feats] last_tensor = KL.Dropout( conv_dropout, noise_shape=noise_shape)(last_tensor) name = '%s_res_up_merge_%d' % (prefix, level) last_tensor = KL.add([last_tensor, add_layer], name=name) name = '%s_res_up_merge_act_%d' % (prefix, level) last_tensor = KL.Activation(activation, name=name)(last_tensor) if batch_norm is not None: name = '%s_bn_up_%d' % (prefix, level) last_tensor = KL.BatchNormalization(axis=batch_norm, name=name)(last_tensor) # Compute likelyhood prediction (no activation yet) name = '%s_likelihood' % prefix last_tensor = convL(nb_labels, 1, activation=None, name=name)(last_tensor) like_tensor = last_tensor # output prediction layer # we use a softmax to compute P(L_x|I) where x is each location if final_pred_activation == 'softmax': print("using final_pred_activation %s for %s" % (final_pred_activation, model_name)) name = '%s_prediction' % prefix softmax_lambda_fcn = lambda x: keras.activations.softmax( x, axis=ndims + 1) pred_tensor = KL.Lambda(softmax_lambda_fcn, name=name)(last_tensor) # otherwise create a layer that does nothing. else: name = '%s_prediction' % prefix pred_tensor = KL.Activation('linear', name=name)(like_tensor) # create the model and retun model = Model(inputs=input_tensor, outputs=pred_tensor, name=model_name) return model
def add_common_layers(y): y = layers.advanced_activations.LeakyReLU()(y) y = layers.Dropout(0.25)(y) return y
def model(reader): assert reader.channeltypes == [ChannelType.CHAR, ChannelType.WORD] text_1_char = L.Input(shape=(None, ), dtype='int32') text_1_word = L.Input(shape=(None, ), dtype='int32') text_2_char = L.Input(shape=(None, ), dtype='int32') text_2_word = L.Input(shape=(None, ), dtype='int32') word_mapping = reader.channels[1].vocabulary_map word_weights = gew.generate_embedding_weights(word_mapping) word_number = word_weights.shape[0] word_embedding_size = word_weights.shape[1] char_embedding = L.Embedding( len(reader.channels[0].vocabulary_above_cutoff) + 2, 5) word_embedding = L.Embedding(output_dim=word_embedding_size, input_dim=word_number, trainable=False, weights=[word_weights]) # Embed all inputs. text_1_char_emb = char_embedding(text_1_char) text_1_word_emb = word_embedding(text_1_word) text_2_char_emb = char_embedding(text_2_char) text_2_word_emb = word_embedding(text_2_word) # Define convolutions. char_conv8 = L.Convolution1D(filters=200, kernel_size=8, strides=1, activation='relu') char_conv4 = L.Convolution1D(filters=200, kernel_size=4, strides=1, activation='relu') word_conv8 = L.Convolution1D(filters=100, kernel_size=8, strides=1, activation='relu') # Convolve all inputs and pool them to a representation of the texts. text_1_char_repr_8 = L.GlobalMaxPooling1D()(char_conv8(text_1_char_emb)) text_1_char_repr_4 = L.GlobalMaxPooling1D()(char_conv4(text_1_char_emb)) text_1_word_repr_8 = L.GlobalMaxPooling1D()(word_conv8(text_1_word_emb)) text_2_char_repr_8 = L.GlobalMaxPooling1D()(char_conv8(text_2_char_emb)) text_2_char_repr_4 = L.GlobalMaxPooling1D()(char_conv4(text_2_char_emb)) text_2_word_repr_8 = L.GlobalMaxPooling1D()(word_conv8(text_2_word_emb)) # Representation of texts are concatenation of all representations. text_1_repr = L.Concatenate()( [text_1_char_repr_8, text_1_char_repr_4, text_1_word_repr_8]) text_2_repr = L.Concatenate()( [text_2_char_repr_8, text_2_char_repr_4, text_2_word_repr_8]) # Find the difference between the texts and learn on it. abs_diff = L.merge(inputs=[text_1_repr, text_2_repr], mode=lambda x: abs(x[0] - x[1]), output_shape=lambda x: x[0], name='absolute_difference') dense1 = L.Dense(500, activation='relu')(abs_diff) dense2 = L.Dense(500, activation='relu')(dense1) pruned = L.Dropout(0.3)(dense2) output = L.Dense(2, activation='softmax', name='output')(pruned) model = Model(inputs=[text_1_char, text_1_word, text_2_char, text_2_word], outputs=output) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) return model
def Conv2DMultiTaskIn1(x_train, y_train, ddg_train, x_test, y_test, ddg_test, x_val, y_val, ddg_val, class_weights_dict, modeldir): K.clear_session() summary = False verbose = 0 # setHyperParams------------------------------------------------------------------------------------------------ batch_size = 64 epochs = 200 lr = 0.0049 optimizer = 'sgd' activator = 'elu' basic_conv2D_layers = 1 basic_conv2D_filter_num = 16 loop_dilation2D_layers = 2 loop_dilation2D_filter_num = 64 loop_dilation2D_dropout_rate = 0.2008 dilation_lower = 2 dilation_upper = 16 reduce_layers = 3 # conv 3 times: 120 => 60 => 30 => 15 # print(reduce_layers) reduce_conv2D_filter_num = 16 reduce_conv2D_dropout_rate = 0.1783 residual_stride = 2 dense1_num = 128 dense2_num = 32 drop_num = 0.2605 kernel_size = (3, 3) pool_size = (2, 2) initializer = 'random_uniform' padding_style = 'same' loss_type = 'mse' metrics = ('mae', pearson_r) my_callbacks = [ callbacks.ReduceLROnPlateau( monitor='val_loss', factor=0.5, patience=5, ) ] if lr > 0: if optimizer == 'adam': chosed_optimizer = optimizers.Adam(lr=lr) elif optimizer == 'sgd': chosed_optimizer = optimizers.SGD(lr=lr) elif optimizer == 'rmsprop': chosed_optimizer = optimizers.RMSprop(lr=lr) # build -------------------------------------------------------------------------------------------------------- ## basic Conv2D input_layer = Input(shape=x_train.shape[1:]) y = layers.Conv2D(basic_conv2D_filter_num, kernel_size, padding=padding_style, kernel_initializer=initializer, activation=activator)(input_layer) y = layers.BatchNormalization(axis=-1)(y) if basic_conv2D_layers == 2: y = layers.Conv2D(basic_conv2D_filter_num, kernel_size, padding=padding_style, kernel_initializer=initializer, activation=activator)(y) y = layers.BatchNormalization(axis=-1)(y) ## loop with Conv2D with dilation (padding='same') for _ in range(loop_dilation2D_layers): y = layers.Conv2D(loop_dilation2D_filter_num, kernel_size, padding=padding_style, dilation_rate=dilation_lower, kernel_initializer=initializer, activation=activator)(y) y = layers.BatchNormalization(axis=-1)(y) y = layers.Dropout(loop_dilation2D_dropout_rate)(y) dilation_lower *= 2 if dilation_lower > dilation_upper: dilation_lower = 2 ## Conv2D with dilation (padding='valaid') and residual block to reduce dimention. for _ in range(reduce_layers): y = layers.Conv2D(reduce_conv2D_filter_num, kernel_size, padding=padding_style, kernel_initializer=initializer, activation=activator)(y) y = layers.BatchNormalization(axis=-1)(y) y = layers.Dropout(reduce_conv2D_dropout_rate)(y) y = layers.MaxPooling2D(pool_size, padding=padding_style)(y) residual = layers.Conv2D(reduce_conv2D_filter_num, 1, strides=residual_stride, padding='same')(input_layer) y = layers.add([y, residual]) residual_stride *= 2 ## flat & dense y = layers.Flatten()(y) y = layers.Dense(dense1_num, activation=activator)(y) y = layers.BatchNormalization(axis=-1)(y) y = layers.Dropout(drop_num)(y) y = layers.Dense(dense2_num, activation=activator)(y) y = layers.BatchNormalization(axis=-1)(y) y = layers.Dropout(drop_num)(y) output_layer = layers.Dense(1)(y) model = models.Model(inputs=input_layer, outputs=output_layer) if summary: model.summary() model.compile( optimizer=chosed_optimizer, loss=loss_type, metrics=list(metrics) # accuracy ) K.set_session(tf.Session(graph=model.output.graph)) init = K.tf.global_variables_initializer() K.get_session().run(init) result = model.fit( x=x_train, y=ddg_train, batch_size=batch_size, epochs=epochs, verbose=verbose, callbacks=my_callbacks, validation_data=(x_test, ddg_test), shuffle=True, ) # print('\n----------History:\n%s'%result.history) model_json = model.to_json() with open(modeldir + '/model.json', 'w') as json_file: json_file.write(model_json) return model, result.history
# 将28*28的方阵变成向量 train_images = train_images.reshape((60000, 28 * 28)).astype('float') test_images = test_images.reshape((10000, 28 * 28)).astype('float') # 1 -> [0 1 0 0 0...] train_labels = to_categorical(train_labels) test_labels = to_categorical(test_labels) network = models.Sequential() network.add( layers.Dense( units=128, activation='relu', input_shape=(28 * 28, ), kernel_regularizer=regularizers.l1(0.0001))) # 正则化 lambda=0.0001 network.add(layers.Dropout(0.01)) # 防止过拟合 network.add( layers.Dense(units=32, activation='relu', kernel_regularizer=regularizers.l1(0.0001))) network.add(layers.Dropout(0.01)) network.add(layers.Dense(units=10, activation='softmax')) # 优化器:RMSprop 损失函数:交叉熵损失函数 network.compile(optimizer=RMSprop(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy']) # 训练网络, epochs训练回合, batch_size每次训练给的数据 network.fit(train_images, train_labels, epochs=20, batch_size=128, verbose=2)
input_shape=(h, w, 1 if bw == 0 else 3), padding='valid')) model.add(layers.BatchNormalization()) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (5, 5), activation='relu')) model.add(layers.BatchNormalization()) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(128, (3, 3), activation='relu')) model.add(layers.BatchNormalization()) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(256, (3, 3), activation='relu')) model.add(layers.BatchNormalization()) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Flatten()) model.add(layers.Dense(2048, activation='relu')) model.add(layers.Dropout(0.2)) model.add(layers.Dense(512, activation='relu')) model.add(layers.Dropout(0.2)) model.add(layers.Dense(64, activation='relu')) model.add(layers.Dropout(0.2)) model.add(layers.Dense(1, activation='sigmoid')) model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc']) print(model.summary()) history = model.fit(train_set, train_label, validation_data=(val_set, val_label), epochs=int(epocs),
X_train, X_test, y_train, y_test = train_test_split(headlines, targets, test_size=0.2, random_state=0) return X_train, X_test, y_train, y_test if __name__ == '__main__': X_train, X_test, y_train, y_test = preprocess() clf = models.Sequential() clf.add(layers.Dense(256, activation='relu', input_shape=(X_train.shape[1],))) clf.add(layers.Dense(256, activation='relu')) clf.add(layers.Dense(256, activation='relu')) clf.add(layers.Dropout(rate=0.2)) clf.add(layers.Dense(256, activation='relu')) clf.add(layers.Dropout(rate=0.2)) clf.add(layers.Dense(41)) # 41是文本类型数 clf.add(layers.Activation('softmax')) clf.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) clf.fit(X_train, y_train, epochs=35, batch_size=100, verbose=2) # test model aa = clf.predict(X_test) # 具体看测试集预测情况 for i in range(300): print(aa[i].argmax(), y_test[i].argmax(), '\n') print("RESULT ON TEST: ", clf.evaluate(X_test, y_test, batch_size=1000))
def multi_net(img_rows, img_cols, color_type, num_classes=None): small = L.Input(shape=(img_rows, img_cols, color_type), name='small') x = conv_block(small, 5) x = L.Conv2D(36, (5, 5), activation='relu')(small) x = L.Conv2D(48, (3, 3), activation='relu')(x) x = L.Flatten()(x) x1 = L.Dense(256)(x) x = L.Dense(512, activation='relu')(x) x = L.Dropout(0.4)(x) x = L.Dense(512, activation='relu')(x) x = L.Dropout(0.4)(x) logits1 = L.Dense(num_classes, activation='softmax')(x) medium = L.Input(shape=(img_rows * 2, img_cols * 2, color_type), name='medium') x = conv_block(medium, 5) x = L.Conv2D(16, (3, 3), activation='relu')(x) x = L.Conv2D(16, (3, 3), activation='relu')(x) x = conv_block(x, 3) x = L.Conv2D(16, (3, 3), activation='relu')(x) x = L.Flatten()(x) x2 = L.Dense(256)(x) x = L.Dense(512, activation='relu')(x) x = L.Dropout(0.4)(x) x = L.Dense(512, activation='relu')(x) x = L.Dropout(0.4)(x) logits2 = L.Dense(num_classes, activation='softmax')(x) large = L.Input(shape=(img_rows * 3, img_cols * 3, color_type), name='large') x = conv_block(large, 5) x = L.Conv2D(16, (3, 3), activation='relu')(x) x = L.Conv2D(16, (3, 3), activation='relu')(x) x = conv_block(x, 3) x = L.Conv2D(16, (3, 3), activation='relu')(x) x = L.Conv2D(16, (3, 3), activation='relu')(x) x = L.Conv2D(16, (3, 3), activation='relu')(x) x = L.Conv2D(16, (3, 3), activation='relu')(x) x = L.Conv2D(16, (3, 3), activation='relu')(x) x = L.Flatten()(x) x3 = L.Dense(256)(x) x = L.Dense(512, activation='relu')(x) x = L.Dropout(0.4)(x) x = L.Dense(512, activation='relu')(x) x = L.Dropout(0.4)(x) logits3 = L.Dense(num_classes, activation='softmax')(x) # # combine all patch # merge0=L.concatenate([x1, x2, x3],axis=-1) # merge1=L.Dense(1024)(merge0) # merge2=L.Activation('relu')(merge1) # merge2 = L.Dropout(0.4)(merge2) # merge3=L.Dense(512)(merge2) # merge3=L.Activation('relu')(merge3) # merge3 = L.Dropout(0.4)(merge3) # logits=L.Dense(num_classes,activation='softmax')(merge0) logits = L.average([logits1, logits2, logits3]) new_model = K.models.Model([small, medium, large], logits) sgd = K.optimizers.SGD(lr=0.001, momentum=0.99, decay=1e-4) new_model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['acc']) return new_model
def __init__(self, shape): self.re_rate = 0.9 self.inputs = layers.Input(shape=shape) self.f_block = layers.Conv3D(4, (3, 3, 3), activation='relu', kernel_regularizer=regularizers.l2(self.re_rate), padding='same')(self.inputs) self.bn = layers.BatchNormalization()(self.f_block) self.mp1 = layers.MaxPooling3D((2, 2, 2))(self.bn) self.f_block1 = layers.Conv3D(16, (3, 3, 3), activation='relu', kernel_regularizer=regularizers.l2(self.re_rate), padding='same')(self.mp1) self.bn = layers.BatchNormalization()(self.f_block1) self.f_block1 = layers.Conv3D(16, (1, 1, 1), activation='relu', kernel_regularizer=regularizers.l2(self.re_rate), padding='same')(self.bn) self.f_block1 = layers.BatchNormalization()(self.f_block1) self.mp2 = layers.MaxPooling3D((2, 2, 2))(self.f_block1) self.f_block2 = layers.Conv3D(32, (3, 3, 3), activation='relu', kernel_regularizer=regularizers.l2(self.re_rate), padding='same')(self.mp2) self.f_block2 = layers.BatchNormalization()(self.f_block2) self.f_block2 = layers.Conv3D(32, (1, 1, 1), activation='relu', kernel_regularizer=regularizers.l2(self.re_rate), padding='same')(self.f_block2) self.f_block2 = layers.BatchNormalization()(self.f_block2) self.mp3 = layers.MaxPooling3D((2, 2, 2))(self.f_block2) self.f_block3 = layers.Conv3D(64, (3, 3, 3), activation='relu', kernel_regularizer=regularizers.l2(self.re_rate), padding='same')(self.mp3) self.f_block3 = layers.BatchNormalization()(self.f_block3) self.f_block3 = layers.Conv3D(64, (1, 1, 1), activation='relu', kernel_regularizer=regularizers.l2(self.re_rate), padding='same')(self.f_block3) self.f_block3 = layers.BatchNormalization()(self.f_block3) # self.mp4 = layers.MaxPooling3D((2, 2, 2))(self.f_block3) self.b_back3 = layers.Conv3D(64, (3, 3, 3), activation='relu', kernel_regularizer=regularizers.l2(self.re_rate), padding='same')(self.f_block3) self.b_back3 = layers.BatchNormalization()(self.b_back3) self.b_back3 = layers.Conv3D(64, (1, 1, 1), activation='relu', kernel_regularizer=regularizers.l2(self.re_rate), padding='same')(self.b_back3) self.b_back3 = layers.BatchNormalization()(self.b_back3) self.cat1 = layers.concatenate([self.f_block3, self.b_back3]) self.bn4 = layers.BatchNormalization()(self.cat1) self.b_back2 = layers.Conv3D(64, (3, 3, 3), activation='relu', kernel_regularizer=regularizers.l2(self.re_rate), padding='same')(layers.UpSampling3D((2, 2, 2))(self.bn4)) self.b_back2 = layers.BatchNormalization()(self.b_back2) self.b_back2 = layers.Conv3D(64, (1, 1, 1), activation='relu', kernel_regularizer=regularizers.l2(self.re_rate), padding='same')(self.b_back2) self.b_back2 = layers.BatchNormalization()(self.b_back2) self.cat2 = layers.concatenate([self.mp2, self.b_back2]) self.bn = layers.BatchNormalization()(self.cat2) self.b_back1 = layers.Conv3D(32, (3, 3, 3), activation='relu', kernel_regularizer=regularizers.l2(self.re_rate), padding='same')(layers.UpSampling3D((2, 2, 2))(self.bn)) self.b_back1 = layers.BatchNormalization()(self.b_back1) self.b_back1 = layers.Conv3D(32, (1, 1, 1), activation='relu', kernel_regularizer=regularizers.l2(self.re_rate), padding='same')(self.b_back1) self.b_back1 = layers.BatchNormalization()(self.b_back1) self.cat3 = layers.concatenate([self.mp1, self.b_back1]) self.gb = layers.GlobalAveragePooling3D()(self.cat3) self.drop = layers.Dropout(rate=0.7)(self.gb) self.dense1 = layers.Dense(128, activation='relu')(self.drop) self.dense1 = layers.Dropout(rate=0.6)(self.dense1) self.dense2 = layers.Dense(64, activation='relu')(self.dense1) self.dense3 = layers.Dense(4, activation='softmax')(self.dense2) self.model = keras.Model(input=self.inputs, output=self.dense3)
def __init__(self, name=None): super().__init__(name=name) self.d1 = layers.Dense(1000) self.d2 = layers.Dense(1000) self.dropout = layers.Dropout(0.1)
# In[2]: discriminator_input = layers.Input(shape=(height, width, channels)) x = layers.Conv2D(128, 3)(discriminator_input) x = layers.LeakyReLU()(x) x = layers.Conv2D(128, 4, strides=2)(x) x = layers.LeakyReLU()(x) x = layers.Conv2D(128, 4, strides=2)(x) x = layers.LeakyReLU()(x) x = layers.Conv2D(128, 4, strides=2)(x) x = layers.LeakyReLU()(x) x = layers.Flatten()(x) # One dropout layer - important trick! x = layers.Dropout(0.4)(x) # Classification layer x = layers.Dense(1, activation='sigmoid')(x) discriminator = keras.models.Model(discriminator_input, x) discriminator.summary() # To stabilize training, we use learning rate decay # and gradient clipping (by value) in the optimizer. discriminator_optimizer = keras.optimizers.RMSprop(lr=0.0008, clipvalue=1.0, decay=1e-8) discriminator.compile(optimizer=discriminator_optimizer, loss='binary_crossentropy')
def retain(ARGS): '''Create the model''' #Define the constant for model saving reshape_size = ARGS.emb_size + ARGS.numeric_size if ARGS.allow_negative: embeddings_constraint = FreezePadding() beta_activation = 'tanh' output_constraint = None else: embeddings_constraint = FreezePadding_Non_Negative() beta_activation = 'sigmoid' output_constraint = non_neg() #Get available gpus , returns empty list if none glist = get_available_gpus() def reshape(data): '''Reshape the context vectors to 3D vector''' return K.reshape(x=data, shape=(K.shape(data)[0], 1, reshape_size)) #Code Input codes = L.Input((None, None), name='codes_input') inputs_list = [codes] #Calculate embedding for each code and sum them to a visit level codes_embs_total = L.Embedding( ARGS.num_codes + 1, ARGS.emb_size, name='embedding', embeddings_constraint=embeddings_constraint)(codes) codes_embs = L.Lambda(lambda x: K.sum(x, axis=2))(codes_embs_total) #Numeric input if needed if ARGS.numeric_size: numerics = L.Input((None, ARGS.numeric_size), name='numeric_input') inputs_list.append(numerics) full_embs = L.concatenate([codes_embs, numerics], name='catInp') else: full_embs = codes_embs #Apply dropout on inputs full_embs = L.Dropout(ARGS.dropout_input)(full_embs) #Time input if needed if ARGS.use_time: time = L.Input((None, 1), name='time_input') inputs_list.append(time) time_embs = L.concatenate([full_embs, time], name='catInp2') else: time_embs = full_embs #Setup Layers #This implementation uses Bidirectional LSTM instead of reverse order # (see https://github.com/mp2893/retain/issues/3 for more details) #If training on GPU and Tensorflow use CuDNNLSTM for much faster training if glist: alpha = L.Bidirectional(L.CuDNNLSTM(ARGS.recurrent_size, return_sequences=True), name='alpha') beta = L.Bidirectional(L.CuDNNLSTM(ARGS.recurrent_size, return_sequences=True), name='beta') else: alpha = L.Bidirectional(L.LSTM(ARGS.recurrent_size, return_sequences=True, implementation=2), name='alpha') beta = L.Bidirectional(L.LSTM(ARGS.recurrent_size, return_sequences=True, implementation=2), name='beta') alpha_dense = L.Dense(1, kernel_regularizer=l2(ARGS.l2)) beta_dense = L.Dense(ARGS.emb_size + ARGS.numeric_size, activation=beta_activation, kernel_regularizer=l2(ARGS.l2)) #Compute alpha, visit attention alpha_out = alpha(time_embs) alpha_out = L.TimeDistributed(alpha_dense, name='alpha_dense_0')(alpha_out) alpha_out = L.Softmax(axis=1)(alpha_out) #Compute beta, codes attention beta_out = beta(time_embs) beta_out = L.TimeDistributed(beta_dense, name='beta_dense_0')(beta_out) #Compute context vector based on attentions and embeddings c_t = L.Multiply()([alpha_out, beta_out, full_embs]) c_t = L.Lambda(lambda x: K.sum(x, axis=1))(c_t) #Reshape to 3d vector for consistency between Many to Many and Many to One implementations contexts = L.Lambda(reshape)(c_t) #Make a prediction contexts = L.Dropout(ARGS.dropout_context)(contexts) output_layer = L.Dense(1, activation='sigmoid', name='dOut', kernel_regularizer=l2(ARGS.l2), kernel_constraint=output_constraint) #TimeDistributed is used for consistency # between Many to Many and Many to One implementations output = L.TimeDistributed(output_layer, name='time_distributed_out')(contexts) #Define the model with appropriate inputs model = Model(inputs=inputs_list, outputs=[output]) return model
def Conv2DClassifierIn1(x_train,y_train,x_test,y_test): summary = True verbose = 1 # setHyperParams------------------------------------------------------------------------------------------------ batch_size = {{choice([512,256,128,64,32])}} epoch = {{choice([300,275,250,225,200,175,150,125,100,75,50,25])}} conv_block={{choice(['four', 'three', 'two'])}} conv1_num={{choice([64,32,16,8])}} conv2_num={{choice([128,64,32,16])}} conv3_num={{choice([128,64,32])}} conv4_num={{choice([256,128,64,32])}} dense1_num={{choice([512, 256, 128])}} dense2_num={{choice([256, 128, 64])}} l1_regular_rate = {{uniform(0.00001, 1)}} l2_regular_rate = {{uniform(0.000001, 1)}} drop1_num={{uniform(0.1, 1)}} drop2_num={{uniform(0.0001, 1)}} activator={{choice(['tanh','relu','elu'])}} optimizer={{choice(['SGD','rmsprop','adam'])}} #--------------------------------------------------------------------------------------------------------------- kernel_size = (3, 3) pool_size = (2, 2) initializer = 'random_uniform' padding_style = 'same' loss_type='binary_crossentropy' metrics=['accuracy'] my_callback = None # early_stopping = EarlyStopping(monitor='val_loss', patience=4) # checkpointer = ModelCheckpoint(filepath='keras_weights.hdf5', # verbose=1, # save_best_only=True) # my_callback = callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.2, # patience=5, min_lr=0.0001) # build -------------------------------------------------------------------------------------------------------- input_layer = Input(shape=x_train.shape[1:]) conv = layers.Conv2D(conv1_num,kernel_size,padding=padding_style,kernel_initializer=initializer,activation=activator)(input_layer) conv = layers.Conv2D(conv1_num,kernel_size,padding=padding_style,kernel_initializer=initializer,activation=activator)(conv) pool = layers.MaxPooling2D(pool_size,padding=padding_style)(conv) if conv_block == 'two': conv = layers.Conv2D(conv2_num,kernel_size,padding=padding_style,kernel_initializer=initializer,activation=activator)(pool) conv = layers.Conv2D(conv2_num,kernel_size,padding=padding_style,kernel_initializer=initializer,activation=activator)(conv) BatchNorm = layers.BatchNormalization(axis=-1)(conv) pool = layers.MaxPooling2D(pool_size,padding=padding_style)(BatchNorm) elif conv_block == 'three': conv = layers.Conv2D(conv2_num,kernel_size,padding=padding_style,kernel_initializer=initializer,activation=activator)(pool) conv = layers.Conv2D(conv2_num,kernel_size,padding=padding_style,kernel_initializer=initializer,activation=activator)(conv) BatchNorm = layers.BatchNormalization(axis=-1)(conv) pool = layers.MaxPooling2D(pool_size,padding=padding_style)(BatchNorm) conv = layers.Conv2D(conv3_num,kernel_size,padding=padding_style,kernel_initializer=initializer,activation=activator)(pool) conv = layers.Conv2D(conv3_num,kernel_size,padding=padding_style,kernel_initializer=initializer,activation=activator)(conv) BatchNorm = layers.BatchNormalization(axis=-1)(conv) pool = layers.MaxPooling2D(pool_size,padding=padding_style)(BatchNorm) elif conv_block == 'four': conv = layers.Conv2D(conv2_num,kernel_size,padding=padding_style,kernel_initializer=initializer,activation=activator)(pool) conv = layers.Conv2D(conv2_num,kernel_size,padding=padding_style,kernel_initializer=initializer,activation=activator)(conv) BatchNorm = layers.BatchNormalization(axis=-1)(conv) pool = layers.MaxPooling2D(pool_size,padding=padding_style)(BatchNorm) conv = layers.Conv2D(conv3_num,kernel_size,padding=padding_style,kernel_initializer=initializer,activation=activator)(pool) conv = layers.Conv2D(conv3_num,kernel_size,padding=padding_style,kernel_initializer=initializer,activation=activator)(conv) BatchNorm = layers.BatchNormalization(axis=-1)(conv) pool = layers.MaxPooling2D(pool_size,padding=padding_style)(BatchNorm) conv = layers.Conv2D(conv4_num,kernel_size,padding=padding_style,kernel_initializer=initializer,activation=activator)(pool) conv = layers.Conv2D(conv4_num,kernel_size,padding=padding_style,kernel_initializer=initializer,activation=activator)(conv) BatchNorm = layers.BatchNormalization(axis=-1)(conv) pool = layers.MaxPooling2D(pool_size,padding=padding_style)(BatchNorm) flat = layers.Flatten()(pool) drop = layers.Dropout(drop1_num)(flat) dense = layers.Dense(dense1_num, activation=activator, kernel_regularizer=regularizers.l1_l2(l1=l1_regular_rate,l2=l2_regular_rate))(drop) BatchNorm = layers.BatchNormalization(axis=-1)(dense) drop = layers.Dropout(drop2_num)(BatchNorm) dense = layers.Dense(dense2_num, activation=activator, kernel_regularizer=regularizers.l1_l2(l1=l1_regular_rate,l2=l2_regular_rate))(drop) output_layer = layers.Dense(len(np.unique(y_train)),activation='softmax')(dense) model = models.Model(inputs=input_layer, outputs=output_layer) if summary: model.summary() # train(self): class_weights = class_weight.compute_class_weight('balanced', np.unique(y_train), y_train.reshape(-1)) class_weights_dict = dict(enumerate(class_weights)) model.compile(optimizer=optimizer, loss=loss_type, metrics=metrics # accuracy ) result = model.fit(x=x_train, y=y_train, batch_size=batch_size, epochs=epoch, verbose=verbose, callbacks=my_callback, validation_data=(x_test, y_test), shuffle=True, class_weight=class_weights_dict ) validation_acc = np.amax(result.history['val_acc']) print('Best validation acc of epoch:', validation_acc) return {'loss': -validation_acc, 'status': STATUS_OK, 'model': model}
# 计算val_steps,这是计算需要从val_gen中抽取多少次 val_steps=(valnum-lookback)//batch_size # 查看训练集需要抽取的次数 train_steps=trainnum//batch_size from keras.callbacks import EarlyStopping early_stopping=EarlyStopping(monitor="val_loss",patience=30) #创建模型 model1=models.Sequential() model1.add(layers.Dense(512,activation="relu",input_shape=(lookback//step,data.shape[-1]))) model1.add(layers.Conv1D(filters=512,kernel_size=3,activation="relu")) model1.add(layers.MaxPooling1D(5)) model1.add(layers.Conv1D(filters=512,kernel_size=3,activation="relu")) model1.add(layers.GlobalMaxPool1D()) model1.add(layers.Dropout(0.5)) model1.add(layers.Dense(8,activation="softmax")) model1.summary() model1.compile(optimizer=optimizers.RMSprop(), loss="categorical_crossentropy", metrics=["acc"]) history1=model1.fit_generator(train_gen, steps_per_epoch=train_steps, epochs=200, validation_data=val_gen, validation_steps=val_steps, callbacks=[early_stopping]) # 绘制结果 loss=history1.history["loss"]