def cmodel(): model = Sequential() # Dense 1 model.add(Dense(1024, input_shape=(90, ))) model.add(Activation('relu')) model.add(Dropout(0.1)) model.add(Dense(1024)) model.add(BN()) model.add(GN(0.1)) model.add(Activation('relu')) model.add(Dense(512)) model.add(BN()) model.add(GN(0.1)) model.add(Activation('relu')) model.add(Dense(num_classes)) model.add(Activation('softmax')) model.summary() sgd = SGD(lr=0.0, momentum=0.9, decay=0.0, nesterov=False) model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) return model
def setModel(num_classes, shape): model = Sequential() model.add(Conv2D(32, (3, 3), padding='same', input_shape=shape)) model.add(BN()) model.add(GN(0.3)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(64, (3, 3), padding='same')) model.add(BN()) model.add(GN(0.3)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(512, (3, 3), padding='same')) model.add(BN()) model.add(GN(0.3)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(512)) model.add(BN()) model.add(GN(0.3)) model.add(Activation('relu')) model.add(Dense(num_classes)) model.add(Activation('softmax')) sgd = SGD(lr=0.01, momentum=0.9, decay=0.0, nesterov=False) model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) return model
def model_keras(): # crea el modeloTrue model = Sequential() # primera capa model.add(Dense(128, input_shape=(87, ))) model.add(BN()) model.add(GN(0.3)) model.add(Activation('relu')) # segunda capa model.add(Dense(256)) model.add(BN()) model.add(GN(0.3)) model.add(Activation('relu')) # tercera capa model.add(Dense(256)) model.add(BN()) model.add(GN(0.3)) model.add(Activation('relu')) # capa de salida model.add(Dense(1)) model.add(GN(0.3)) model.add(Activation('relu')) # compilar el modelo model.compile(loss='mean_squared_error', optimizer='adam') return model
def basic_model(): # 1024 512 model = Sequential() model.add(Dense(2048, input_shape=(90, ))) model.add(Activation('relu')) model.add(Dropout(0.1)) model.add(Dense(1024)) model.add(BN()) model.add(GN(0.1)) model.add(Activation('relu')) model.add(Dense(512)) model.add(BN()) model.add(GN(0.1)) model.add(Activation('relu')) model.add(Dense(1)) model.add(Activation('relu')) model.summary() adam = keras.optimizers.Adam(lr=0.01, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False) model.compile(loss='mape', optimizer=adam, metrics=['mse']) return model
def setModel(num_classes): model = Sequential() model.add(Dense(512, input_shape=(784,))) model.add(BN()) model.add(GN(0.3)) model.add(Activation('relu')) model.add(Dense(512)) model.add(BN()) model.add(GN(0.3)) model.add(Activation('relu')) model.add(Dense(num_classes, activation='softmax')) sgd=SGD(lr=0.01, decay=1e-6, momentum=0.9) model.compile(loss='categorical_crossentropy',optimizer=sgd,metrics=['accuracy']) return model
def CBGN(model, filters, lname, ishape=0): if (ishape != 0): model.add(Conv2D(filters, (3, 3), padding='same', input_shape=ishape)) else: model.add(Conv2D(filters, (3, 3), padding='same')) model.add(BN()) model.add(GN(0.3)) model.add(Activation('relu')) model.add(Conv2D(filters, (3, 3), padding='same')) model.add(BN()) model.add(GN(0.3)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2), name=lname)) return model
def main(): batch_size = 1024 num_classes = 2 epochs = 500 #Load created npy arrays with vectorized images train_x = np.load("train_X_FOURTH_iter5.npy", allow_pickle=True) #Load labels train_y_orig = np.load("train_Y_FOURTH_iter5.npy", allow_pickle=True) train_x = train_x.astype("float32") train_y = train_y_orig.astype("float32") #Normalize data train_x /= 255 #One-hot vector of labels num_classes = 2 train_y = keras.utils.to_categorical(train_y, num_classes) #Data augmentation on the fly datagen = ImageDataGenerator( width_shift_range=0.1, #horizontal shift height_shift_range=0.1, #Vertical shift rotation_range=10, #Image rotation [-10, +10] degrees horizontal_flip=True #Flip the image ) #Train the datagenerator over the train set datagen.fit(train_x) """ ###### CONSTRUCTION OF MODEL ###### """ model = Sequential() model.add(Conv2D(8, (3, 3), padding='same', strides=1, input_shape=train_x.shape[1:])) model.add(Activation("relu")) model.add(Conv2D(16, (3, 3), padding='valid', strides=2)) model.add(Activation("relu")) model.add(Conv2D(32, (3, 3), padding='valid', strides=1)) model.add(Activation("relu")) model.add(Conv2D(16, (3, 3), padding='valid', strides=1)) model.add(Activation("relu")) model.add(Conv2D(8, (3, 3), padding='valid', strides=1)) model.add(Activation("relu")) model.add(Flatten()) model.add(GN(0.3)) model.add(Dense(512, activation="relu")) model.add(Dense(num_classes, activation="softmax")) model.summary() opt = SGD(lr=0.001, decay=1e-6, momentum=0.9) #Only if fine tunning a previously trained model. If so, reduce learning rate above. #model = load_model("model_faces_20_iter6_dev.hdf5") model.compile(loss='categorical_crossentropy',optimizer=opt,metrics=['accuracy']) save_model = keras.callbacks.ModelCheckpoint( "model_faces_20_iter6_dev_DA.hdf5", monitor='accuracy', verbose=1, save_best_only=True, save_weights_only=False, mode='max') history=model.fit_generator(datagen.flow(train_x, train_y,batch_size), steps_per_epoch=len(train_x) / batch_size, epochs=epochs, callbacks=[save_model], verbose=1)
height_shift_range=0.2, horizontal_flip=False, vertical_flip=True) datagen.fit(x_train) """Create Model for training""" shape = x_train.shape[1:] model = Sequential() model.add(BN(input_shape=shape)) model.add(Dropout(0.1)) model.add(Conv2D(32, (3, 3), padding='same', kernel_initializer=glorot_uniform(seed=0))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(BN()) model.add(GN(0.3)) model.add(Flatten()) model.add(Dense(256, kernel_initializer=glorot_uniform(seed=0))) model.add(Activation('relu')) model.add(BN()) model.add(Dropout(0.1)) model.add(Dense(128, kernel_initializer=glorot_uniform(seed=0))) model.add(Activation('relu')) model.add(BN()) model.add(Dropout(0.1)) model.add(Dense(1)) model.add(Activation('sigmoid'))
# convert class vectors to binary class matrices y_train = keras.utils.to_categorical(y_train, num_classes) y_test = keras.utils.to_categorical(y_test, num_classes) ## Data Augmentation with an ImageGenerator datagen = ImageDataGenerator(width_shift_range=0.1, height_shift_range=0.1, horizontal_flip=False) #Define the NN topology, a sequential model with 2 hidden layers model = Sequential() # noise in input model.add(Reshape(target_shape=(784, ), input_shape=(28, 28, 1))) model.add(GN(0.1)) model.add(Dense(1024, input_shape=(784, ))) model.add(BN()) model.add(GN(0.1)) model.add(Activation('relu')) model.add(Dropout(0.02)) model.add(Dense(1024, input_shape=(784, ))) model.add(BN()) model.add(GN(0.1)) model.add(Activation('relu')) model.add(Dropout(0.02)) model.add(Dense(1024)) model.add(BN())
rotation_range=20, zoom_range=[1.0, 1.2], horizontal_flip=True) ## DEF CNN TOPOLOGY 1 model1 = Sequential() model1 = CBGN(model1, 32, 'conv_model1_1', x_train.shape[1:]) model1 = CBGN(model1, 64, 'conv_model1_2') model1 = CBGN(model1, 128, 'conv_model1_3') model1 = CBGN(model1, 256, 'conv_model1_4') model1 = CBGN(model1, 512, 'conv_model1_5') model1.add(Flatten()) model1.add(Dense(512)) model1.add(BN()) model1.add(GN(0.3)) model1.add(Activation('relu')) model1.add(Dropout(0.5)) model1.add(Dense(num_classes)) model1.add(Activation('softmax')) model1.summary() ## OPTIM AND COMPILE opt = SGD(lr=0.1, decay=1e-6) model1.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy']) # DEFINE A LEARNING RATE SCHEDULER
'n_channels': 1, 'n_classes': 2, 'shuffle': True, 'dir_faces': './face_images/', 'dir_no_faces': './no_face_images/' } training_generator = DataGenerator(f_train, nf_train, **params) test_generator = DataGenerator(f_test, nf_test, **params) validation_generator = DataGenerator(f_val, nf_val, **params) print("Building Model") # Start building our model model = Sequential() model.add(BN(input_shape=(32, 32, 1))) model.add(GN(gn)) model.add(Conv2D(32, (3, 3), padding='same')) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(BN()) model.add(GN(gn)) model.add(Conv2D(64, (3, 3), padding='same')) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(BN()) model.add(GN(gn * 1.5)) model.add(Conv2D(128, (3, 3), padding='same')) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2)))