def make_model(): model = MobileNet(input_shape=(224, 224, 3), alpha=1., weights=None, classes=2) model.compile(optimizer=Adam(lr=0.002), loss='categorical_crossentropy', metrics=[categorical_crossentropy, categorical_accuracy]) print(model.summary()) return model
def build_model(mode, model_name=None, model_path=None): clear_session() if mode == 'train': img = Input(shape=(224, 224, 3)) if model_name == 'DenseNet121': model = DenseNet121(include_top=False, weights='imagenet', input_tensor=img, input_shape=None, pooling='avg') elif model_name == 'MobileNet': model = MobileNet(include_top=False, weights='imagenet', input_tensor=img, input_shape=None, pooling='avg') elif model_name == 'Xception': model = Xception(include_top=False, weights='imagenet', input_tensor=img, input_shape=None, pooling='avg') elif model_name == 'ResNet50': model = ResNet50(include_top=False, weights='imagenet', input_tensor=img, input_shape=None, pooling='avg') final_layer = model.layers[-1].output dense_layer_1 = Dense(128, activation='relu')( final_layer) # funcion activacion output_layer = Dense(10, activation='sigmoid')( dense_layer_1) # funcion activacion model = Model(input=img, output=output_layer) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) elif mode == 'inference': model = load_model(model_path) return model
def create_model(self): from helper import precision, recall, f1_score input = Input(shape=(dim[0], dim[1], 3), name='input') model = MobileNet(input_shape=dim, alpha=1, depth_multiplier=1, dropout=self.dropout_global, include_top=False, weights=self.w, input_tensor=None) if self.trainable: for i in model.layers[:len(model.layers) - self.trainable]: i.trainable = False x = model.output input = model.input optimizer = SGD(learning_rate=self.eta) else: optimizer = Adam(lr=self.eta, amsgrad=self.amsgrad) x = model(input) x = GlobalAveragePooling2D()(x) nodes = 2048 x = Dense(nodes, activation='relu', kernel_regularizer=l2(self.regulizer))(x) x = Dropout(self.dropout)(x) for i in range(self.layer_params): nodes = int(nodes / 2) x = Dense(nodes, activation='relu', kernel_regularizer=l2(self.regulizer))(x) x = Dropout(self.dropout)(x) z = Dense(units=self.classes, activation='softmax')(x) model = Model(inputs=input, outputs=z) if not self.train_mode: model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy', precision, recall, f1_score]) else: model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy']) print(model.summary()) return model, optimizer
def build_small_mobnet(width=28, height=28, alpha=0.25, verbose=True): dim = (height, width, 3) input = Input(shape=dim, name='input') model = MobileNet(input_shape=dim, alpha=alpha, depth_multiplier=1, include_top=False, weights='imagenet', input_tensor=None) x = model(input) x = GlobalAveragePooling2D()(x) x = Dense(units=512, activation='relu', kernel_regularizer=l2(0.01))(x) x = Dropout(0.2)(x) z = Dense(6)(x) z = Activation('softmax')(z) model = Model(inputs=input, outputs=z) model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy']) if verbose == True: print(model.summary()) return model
# In[11]: #telling the input and output layers model= Model(inputs=model.input , outputs = model_output) # In[12]: #compiling the model model.compile(optimizer='adam' , loss='binary_crossentropy' , metrics=['accuracy']) # In[14]: #Training the Model after augementation from keras.preprocessing.image import ImageDataGenerator train_datagen = ImageDataGenerator( rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) test_datagen = ImageDataGenerator(rescale=1./255) training_set = train_datagen.flow_from_directory(
batch_size=128, class_mode='binary') test_datagen = ImageDataGenerator(rescale=1. / 255) data_test = test_datagen.flow_from_directory("DATOS/test", target_size=(128, 128), class_mode='binary') model = MobileNet(input_shape=(128, 128, 3), include_top=False, pooling='avg') x = model.output x = Dense(512, activation="relu")(x) predict = Dense(1, activation="sigmoid")(x) model = Model(inputs=model.input, outputs=predict) adam = Adam(lr=0.0001) model.compile(optimizer=adam, loss="binary_crossentropy", metrics=["accuracy"]) model.summary() history = model.fit(data_train, epochs=10, validation_data=data_test, verbose=1) model.save("/Clasificador.h5") def visualizar_img_test(path, modelo): img = cv2.imread(path) img_1 = cv2.resize(img, (128, 128)) img = (img_1 / 255) img = np.expand_dims(img, axis=0)
model_path = os.path.join(save_dir, model_name) weight_path = os.path.join(save_dir, weight_name) x_train, y_train, x_test, y_test = image_crop.read_data() x_train_resized = image_crop.resize_imgs(x_train) y = to_categorical(y_train, num_classes=34) model = MobileNet(include_top=True, weights=None, classes=34, pooling='max', input_shape=(150, 150, 3)) checkpoint = ModelCheckpoint(filepath=os.path.join( save_dir, 'MobileNet_weight.{epoch:02d}-{loss:.2f}.hdf5'), verbose=1) opt = RMSprop(lr=2e-5) model.compile(optimizer=opt, loss=losses.categorical_crossentropy, metrics=[metrics.categorical_accuracy]) model.fit(x_train_resized, y, epochs=10, batch_size=36, callbacks=[checkpoint]) model.save(model_path) model.save_weights(weight_path)
checkpoint=ModelCheckpoint("face_recog.h5", monitor="val_loss", mode="min", save_best_only=True, verbose=1) earlystop=EarlyStopping(monitor="val_loss", min_delta=0, patience=3, verbose=1, restore_best_weights=True) callbacks=[earlystop,checkpoint] model.compile(loss='binary_crossentropy', optimizer=RMSprop(lr=0.0001), metrics= ['accuracy']) nb_train_samples=800 nb_validation_sample=150 epochs=10 batch_size=16 history=model.fit_generator( train_generator, steps_per_epoch=nb_train_samples //batch_size, epochs=epochs, callbacks=callbacks, validation_data=validation_generator, validation_steps=nb_validation_sample // batch_size)
mode="min", save_best_only=True, verbose=1) earlystop = EarlyStopping(monitor='val_loss', min_delta=0, patience=3, verbose=1, restore_best_weights=True) # we put our call backs into a callback list callbacks = [earlystop, checkpoint] # We use a very small learning rate model.compile(loss='categorical_crossentropy', optimizer=RMSprop(lr=0.001), metrics=['accuracy']) # Enter the number of training and validation samples here nb_train_samples = 1097 nb_validation_samples = 272 # We only train 5 EPOCHS epochs = 3 batch_size = 16 history = model.fit_generator(train_generator, steps_per_epoch=nb_train_samples // batch_size, epochs=epochs, callbacks=callbacks, validation_data=validation_generator,
def create(layers, optimizer, monitoring, bestModel, lr, epochs, batch, validation, dataset, transf): if transf == 'True': transf = True else: transf = False model_name = layers.split('#')[0] layers_list = layers.split('#')[1].split('--') classPath = path.replace('/scripts/python', '') + '/uploads/' + dataset classes = os.listdir(classPath) number_of_classes = len(classes) imgs = os.listdir(classPath + '/' + classes[0]) if imgs[0].split('.')[-1] == 'dcm': im = parse(classPath + '/' + classes[0] + '/' + imgs[0]) else: im = cv2.imread(classPath + '/' + classes[0] + '/' + imgs[0], 0) imshape0 = im.shape[0] imshape1 = im.shape[1] input_format = (imshape0, imshape1, 1) inputs = Input(shape=input_format, name='Input') try: saveToFile('\n########################################\n') saveToFile('Dataset: ' + dataset + '\n') if dataset != '': # checking the last layer that provides the number of classes last_layer_index = len(layers_list) - 1 for i in range(len(layers_list)): if layers_list[i].split('-')[0] == 'Conv2D' or layers_list[ i].split('-')[0] == 'Dense': last_layer_index = i saveToFile('Optimizer: ' + optimizer + '\n') saveToFile('Monitoring parameter: ' + monitoring.split('_')[0] + ' ' + monitoring.split('_')[1] + '\n') if monitoring == 'Validation_loss': monitoring = 'val_loss' elif monitoring == 'Training_loss': monitoring = 'loss' elif monitoring == 'Validation_accuracy': monitoring = 'val_acc' else: monitoring = 'acc' saveToFile('Save best model: ' + bestModel + '\n') if bestModel == 'No': bestModel = False else: bestModel = True saveToFile('Learning rate: ' + lr + '\n') saveToFile('Number of epochs: ' + epochs + '\n') saveToFile('Batch size (%): ' + batch + '\n') saveToFile('Validation dataset size (%): ' + validation + '\n') if model_name == 'InceptionV3': model = InceptionV3(weights=None, classes=number_of_classes, input_shape=input_format) saveToFile('Using predefined model: InceptionV3\n') elif model_name == 'MobileNet': model = MobileNet(weights=None, classes=number_of_classes, input_shape=input_format, dropout=0.3) saveToFile('Using predefined model: MobileNet\n') elif model_name == 'VGG16': model = VGG16(weights=None, classes=number_of_classes, input_shape=input_format) saveToFile('Using predefined model: VGG16\n') else: saveToFile('Layers:\n') counter = 0 for layer in layers_list: if layer.split('-')[0] == 'Activation': act_func = layer.split('-')[1].lower() layer_name = layer.split('-')[2].lower() if counter == 0: x = Activation(act_func, name=layer_name)(inputs) else: x = Activation(act_func, name=layer_name)(x) saveToFile(' x = Activation(' + act_func + ', name=' + layer_name + ')(x)\n') elif layer.split('-')[0] == 'BatchNormalization': axis = int(layer.split('-')[1]) layer_name = layer.split('-')[2].lower() if counter == 0: x = BatchNormalization(axis=axis)(inputs) else: x = BatchNormalization(axis=axis)(x) saveToFile(' x = BatchNormalization(axis=' + str(axis) + ')(x)\n') elif layer.split('-')[0] == 'Conv2D': act_func = layer.split('-')[1].lower() filters = int(layer.split('-')[2]) kernel_height = int(layer.split('-')[3]) kernel_width = int(layer.split('-')[4]) stride_height = int(layer.split('-')[5]) stride_width = int(layer.split('-')[6]) padding = layer.split('-')[7].lower() layer_name = layer.split('-')[8].lower() if counter == 0: x = Conv2D(filters, (kernel_height, kernel_width), strides=(stride_height, stride_width), padding=padding, name=layer_name, activation=act_func)(inputs) else: if counter == last_layer_index: filters = number_of_classes x = Conv2D(filters, (kernel_height, kernel_width), strides=(stride_height, stride_width), padding=padding, name=layer_name, activation=act_func)(x) saveToFile(' x = Conv2D(' + str(filters) + ', (' + str(kernel_height) + ', ' + str(kernel_width) + '), strides=(' + str(stride_height) + ', ' + str(stride_width) + '), padding=' + padding + ', name=' + layer_name + ', activation=' + act_func + ')(x)\n') elif layer.split('-')[0] == 'Dense': act_func = layer.split('-')[1].lower() units = int(layer.split('-')[2]) layer_name = layer.split('-')[3].lower() if counter == 0: x = Dense(units, activation=act_func, name=layer_name)(inputs) else: if counter == last_layer_index: units = number_of_classes x = Dense(units, activation=act_func, name=layer_name)(x) saveToFile(' x = Dense(' + str(units) + ', activation=' + act_func + ', name=' + layer_name + ')(x)\n') elif layer.split('-')[0] == 'Dropout': rate = float(layer.split('-')[1]) seed = layer.split('-')[2] layer_name = layer.split('-')[3].lower() if seed != 'None': if counter == 0: x = Dropout(rate=rate, seed=int(seed))(inputs) else: x = Dropout(rate=rate, seed=int(seed))(x) saveToFile(' x = Dropout(' + str(rate) + ', seed=' + seed + ')(x)\n') else: if counter == 0: x = Dropout(rate=rate)(inputs) else: x = Dropout(rate=rate)(x) saveToFile(' x = Dropout(' + str(rate) + ')(x)\n') elif layer.split('-')[0] == 'Fire_function': act_func = layer.split('-')[1].lower() filters_squeezed = int(layer.split('-')[2]) filters_expanded = int(layer.split('-')[3]) kernel_height = int(layer.split('-')[4]) kernel_width = int(layer.split('-')[5]) stride_height = int(layer.split('-')[6]) stride_width = int(layer.split('-')[7]) padding = layer.split('-')[8].lower() layer_name = layer.split('-')[9].lower() if counter == 0: x = fire_module(inputs, layer_name, squeeze=filters_squeezed, expand=filters_expanded, activation=act_func, kernel_height=kernel_height, kernel_width=kernel_width, stride_height=stride_height, stride_width=stride_width, padding=padding) else: x = fire_module(x, layer_name, squeeze=filters_squeezed, expand=filters_expanded, activation=act_func, kernel_height=kernel_height, kernel_width=kernel_width, stride_height=stride_height, stride_width=stride_width, padding=padding) saveToFile(' fire_module(x, ' + layer_name + ', squeeze=' + str(filters_squeezed) + ', expand=' + str(filters_expanded) + ', activation=' + act_func + ', kernel_height=' + str(kernel_height) + ', kernel_width=' + str(kernel_width) + ', stride_height=' + str(stride_height) + ', stride_width=' + str(stride_width) + ', padding=' + padding + ')\n') elif layer.split('-')[0] == 'Flatten': layer_name = layer.split('-')[1].lower() if counter == 0: x = Flatten()(inputs) else: x = Flatten()(x) saveToFile(' x = Flatten()(x)\n') elif layer.split('-')[0] == 'GlobalAveragePooling2D': layer_name = layer.split('-')[1].lower() if counter == 0: x = GlobalAveragePooling2D()(inputs) else: x = GlobalAveragePooling2D()(x) saveToFile(' x = GlobalAveragePooling2D()(x)\n') elif layer.split('-')[0] == 'MaxPool2D': pool_height = int(layer.split('-')[1]) pool_width = int(layer.split('-')[2]) stride_height = int(layer.split('-')[3]) stride_width = int(layer.split('-')[4]) padding = layer.split('-')[5].lower() layer_name = layer.split('-')[6].lower() if counter == 0: x = MaxPool2D(pool_size=(pool_height, pool_width), strides=(stride_height, stride_width), padding=padding, name=layer_name)(inputs) else: x = MaxPool2D(pool_size=(pool_height, pool_width), strides=(stride_height, stride_width), padding=padding, name=layer_name)(x) saveToFile(' x = MaxPool2D(pool_size=(' + str(pool_height) + ', ' + str(pool_width) + '), strides=(' + str(stride_height) + ', ' + str(stride_width) + '), padding=' + padding + ', name=' + layer_name + ')(x)\n') counter += 1 model = Model(inputs=inputs, outputs=x) model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy']) saveToFile('Model built successfully\n') data, labels = getClasses( path.replace('/scripts/python', '') + '/uploads/' + dataset) # from cmd print(data.shape) print(labels.shape) saveToFile( 'Creating validation dataset and assigning batch size...\n') # creating validation dataset randomly if float(validation) != 0: data, data_val, labels, labels_val = train_test_split( data, labels, test_size=float(validation) / 100, random_state=42) saveToFile(' Training dataset size: ' + str(data.shape[0]) + '\n') saveToFile(' Validation dataset size: ' + str(data_val.shape[0]) + '\n') val_data = (data_val, labels_val) else: val_data = None # batch size batch_size = int(float(batch) * data.shape[0] / 100) if batch_size == 0: batch_size = 1 # else: # for i in range(data.shape[0]): # if data.shape[0] % batch_size == 0: # break # else: # batch_size += 1 saveToFile(' Batch size: ' + str(batch_size) + '\n') train(model, data, labels, int(epochs), val_data=val_data, batch_size=batch_size, check=monitoring, dataset_folder='default', best_model=bestModel, min_lr=float(lr), model_name=model_name, transf=transf) global outputFolder saveToFile('Dataset trained successfully -> Output folder: ' + outputFolder + '\n') saveToFile('########################################\n') except Exception as e: saveToFile(str(e) + '\n')
zoom_range = 0.2, horizontal_flip = True) validation_datagen_1 = ImageDataGenerator() history_2 = pre_trained_model_1.fit_generator(train_datagen_1.flow(train_x, train_y, batch_size=32), steps_per_epoch=len(train_x) / 32, epochs=10, validation_data=validation_datagen_1.flow(val_x, val_y, batch_size=32), validation_steps=len(val_x) / 32)X # Try MobileNet transfer learning from keras.applications import MobileNet from keras.applications.mobilenet import preprocess_input model_3 = MobileNet(input_shape=(100, 100, 3), alpha=1., weights=None, classes=5005) model_3.compile(loss='categorical_crossentropy', optimizer="adam", metrics=['accuracy']) from tensorflow.keras.preprocessing.image import ImageDataGenerator train_datagen_2 = ImageDataGenerator(rotation_range = 40, width_shift_range = 0.2, height_shift_range = 0.2, shear_range = 0.2, zoom_range = 0.2, horizontal_flip = True) validation_datagen_2 = ImageDataGenerator() history_3 =model_3.fit_generator(train_datagen_2.flow(X, yy, batch_size=32), steps_per_epoch=len(X) / 32, epochs=10)
valid_batches = ImageDataGenerator(preprocessing_function = preprocess_func).flow_from_directory(valid_path,target_size=(224,224),batch_size=20) from keras.applications import MobileNet model = MobileNet(weights='../input/mobilenet-h5/mobilenet_1_0_128_tf.h5') model.summary() type(model) x = model.layers[-6].output predictions = Dense(29,activation='softmax')(x) model = Model(inputs = model.input, outputs=predictions) model.summary() for layer in model.layers[:-23]: layer.trainable = False model.compile(Adam(lr = 0.001),loss = 'categorical_crossentropy',metrics = ['accuracy']) model.fit_generator(train_batches, steps_per_epoch = 3627, validation_data = valid_batches, validation_steps = 723, epochs = 10, verbose = 2) model.save('sign_language.h5') ####### model build and saved upto here from keras.models import load_model class SignLanguageModel(): Signs = ["A", "B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z", "del", "nothing", "space"]
def create(layers, optimizer, dataset, img): model_name = layers.split('#')[0] layers_list = layers.split('#')[1].split('--') img_path = path.replace('/scripts/python', '') + '/uploads/' + dataset + '/' + img classPath = path.replace('/scripts/python', '') + '/uploads/' + dataset classes = os.listdir(classPath) number_of_classes = len(classes) imgs = os.listdir(classPath + '/' + classes[0]) if imgs[0].split('.')[-1] == 'dcm': im = parse(classPath + '/' + classes[0] + '/' + imgs[0]) else: im = cv2.imread(classPath + '/' + classes[0] + '/' + imgs[0], 0) imshape0 = im.shape[0] imshape1 = im.shape[1] input_format=(imshape0, imshape1, 1) inputs = Input(shape=input_format, name='Input') try: if dataset != '': # checking the last layer that provides the number of classes last_layer_index = len(layers_list)-1 for i in range(len(layers_list)): if layers_list[i].split('-')[0] == 'Conv2D' or layers_list[i].split('-')[0] == 'Dense': last_layer_index = i counter = 0 if model_name == 'InceptionV3': model = InceptionV3(weights=None, classes=number_of_classes, input_shape=input_format) saveToFile('Using predefined model: InceptionV3\n') elif model_name == 'MobileNet': model = MobileNet(weights=None, classes=number_of_classes, input_shape=input_format, dropout = 0.3) saveToFile('Using predefined model: MobileNet\n') elif model_name == 'VGG16': model = VGG16(weights=None, classes=number_of_classes, input_shape=input_format) saveToFile('Using predefined model: VGG16\n') else: saveToFile('Layers:\n') for layer in layers_list: if layer.split('-')[0] == 'Activation': act_func = layer.split('-')[1].lower() layer_name = layer.split('-')[2].lower() if counter == 0: x = Activation(act_func, name=layer_name)(inputs) else: x = Activation(act_func, name=layer_name)(x) saveToFile(' x = Activation(' + act_func + ', name=' + layer_name + ')(x)\n') elif layer.split('-')[0] == 'BatchNormalization': axis = int(layer.split('-')[1]) layer_name = layer.split('-')[2].lower() if counter == 0: x = BatchNormalization(axis=axis)(inputs) else: x = BatchNormalization(axis=axis)(x) saveToFile(' x = BatchNormalization(axis=' + str(axis) + ')(x)\n') elif layer.split('-')[0] == 'Conv2D': act_func = layer.split('-')[1].lower() filters = int(layer.split('-')[2]) kernel_height = int(layer.split('-')[3]) kernel_width = int(layer.split('-')[4]) stride_height = int(layer.split('-')[5]) stride_width = int(layer.split('-')[6]) padding = layer.split('-')[7].lower() layer_name = layer.split('-')[8].lower() if counter == 0: x = Conv2D(filters, (kernel_height, kernel_width), strides=(stride_height, stride_width), padding=padding, name=layer_name, activation=act_func)(inputs) else: if counter == last_layer_index: filters = number_of_classes x = Conv2D(filters, (kernel_height, kernel_width), strides=(stride_height, stride_width), padding=padding, name=layer_name, activation=act_func)(x) saveToFile(' x = Conv2D(' + str(filters) + ', (' + str(kernel_height) + ', ' + str(kernel_width) + '), strides=(' + str(stride_height) + ', ' + str(stride_width) + '), padding=' + padding + ', name=' + layer_name + ', activation=' + act_func + ')(x)\n') elif layer.split('-')[0] == 'Dense': act_func = layer.split('-')[1].lower() units = int(layer.split('-')[2]) layer_name = layer.split('-')[3].lower() if counter == 0: x = Dense(units, activation=act_func, name=layer_name)(inputs) else: if counter == last_layer_index: units = number_of_classes x = Dense(units, activation=act_func, name=layer_name)(x) saveToFile(' x = Dense(' + str(units) + ', activation=' + act_func + ', name=' + layer_name + ')(x)\n') elif layer.split('-')[0] == 'Dropout': rate = float(layer.split('-')[1]) seed = layer.split('-')[2] layer_name = layer.split('-')[3].lower() if seed != 'None': if counter == 0: x = Dropout(rate=rate, seed=int(seed))(inputs) else: x = Dropout(rate=rate, seed=int(seed))(x) saveToFile(' x = Dropout(' + str(rate) + ', seed=' + seed + ')(x)\n') else: if counter == 0: x = Dropout(rate=rate)(inputs) else: x = Dropout(rate=rate)(x) saveToFile(' x = Dropout(' + str(rate) + ')(x)\n') elif layer.split('-')[0] == 'Fire_function': act_func = layer.split('-')[1].lower() filters_squeezed = int(layer.split('-')[2]) filters_expanded = int(layer.split('-')[3]) kernel_height = int(layer.split('-')[4]) kernel_width = int(layer.split('-')[5]) stride_height = int(layer.split('-')[6]) stride_width = int(layer.split('-')[7]) padding = layer.split('-')[8].lower() layer_name = layer.split('-')[9].lower() if counter == 0: x = fire_module(inputs, layer_name, squeeze=filters_squeezed, expand=filters_expanded, activation=act_func, kernel_height=kernel_height, kernel_width=kernel_width, stride_height=stride_height, stride_width=stride_width, padding=padding) else: x = fire_module(x, layer_name, squeeze=filters_squeezed, expand=filters_expanded, activation=act_func, kernel_height=kernel_height, kernel_width=kernel_width, stride_height=stride_height, stride_width=stride_width, padding=padding) saveToFile(' fire_module(x, ' + layer_name + ', squeeze=' + str(filters_squeezed) + ', expand=' + str(filters_expanded) + ', activation=' + act_func + ', kernel_height=' + str(kernel_height) + ', kernel_width=' + str(kernel_width) + ', stride_height=' + str(stride_height) + ', stride_width=' + str(stride_width) + ', padding=' + padding + ')\n') elif layer.split('-')[0] == 'Flatten': layer_name = layer.split('-')[1].lower() if counter == 0: x = Flatten()(inputs) else: x = Flatten()(x) saveToFile(' x = Flatten()(x)\n') elif layer.split('-')[0] == 'GlobalAveragePooling2D': layer_name = layer.split('-')[1].lower() if counter == 0: x = GlobalAveragePooling2D()(inputs) else: x = GlobalAveragePooling2D()(x) saveToFile(' x = GlobalAveragePooling2D()(x)\n') elif layer.split('-')[0] == 'MaxPool2D': pool_height = int(layer.split('-')[1]) pool_width = int(layer.split('-')[2]) stride_height = int(layer.split('-')[3]) stride_width = int(layer.split('-')[4]) padding = layer.split('-')[5].lower() layer_name = layer.split('-')[6].lower() if counter == 0: x = MaxPool2D(pool_size=(pool_height, pool_width), strides=(stride_height, stride_width), padding=padding, name=layer_name)(inputs) else: x = MaxPool2D(pool_size=(pool_height, pool_width), strides=(stride_height, stride_width), padding=padding, name=layer_name)(x) saveToFile(' x = MaxPool2D(pool_size=(' + str(pool_height) + ', ' + str(pool_width) + '), strides=(' + str(stride_height) + ', ' + str(stride_width) + '), padding=' + padding + ', name=' + layer_name + ')(x)\n') counter += 1 model = Model(inputs=inputs, outputs=x) model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy']) saveToFile('Model built successfully\n') data = create_tensor(img_path) # from cmd print(data.shape) return model, data, counter-1 except Exception as e: saveToFile(str(e) + '\n')
axs[1].set_ylabel('MLogLoss') axs[1].set_xlabel('Epoch') axs[1].grid() axs[1].legend(loc=0) fig.savefig(address_hist, dpi=300) plt.show(); img_gen = Generate_Data(train_num = 128*4) length = get_char_length() input_shape = (IMAGE_HEIGHT,IMAGE_WIDTH,CHANNEL) model = MobileNet(input_shape=input_shape,alpha=1.,weights=None,classes=CHAR_NUM*length) #parallel_model = multi_gpu_model(model, 4) adam = keras.optimizers.Adam(lr = 0.005, beta_1=0.9, beta_2=0.999,decay=0.01) model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=[accuracy,categorical_accuracy,categorical_crossentropy]) cbk = MyCbk(model) tensorboard = TensorBoard(log_dir=address_tensorboard,histogram_freq=0) eva = Evaluate(img_gen.test()) moniter = ReduceLROnPlateau(monitor='val_accuracy', factor=0.5, patience=5, mode='max', cooldown=3, verbose=1) hists = [] hist = model.fit_generator(generator=img_gen.next_train(), steps_per_epoch = 600, epochs = 16, validation_data = img_gen.next_val(), validation_steps = 1, verbose = 1, callbacks = [cbk,eva,moniter,tensorboard]) hists.append(hist) draw_hist(hists)
None, path_prefix=images_path, vertical_flip_probability=0, grayscale=grayscale, do_random_crop=do_random_crop) # model parameters/compilation #model = mini_XCEPTION(input_shape, num_classes) model = MobileNet(input_shape=input_shape, include_top=True, alpha=0.25, classes=num_classes, weights=None) model.compile(optimizer='nadam', loss='categorical_crossentropy', metrics=['accuracy']) model.summary() # model callbacks early_stop = EarlyStopping('val_loss', patience=patience) reduce_lr = ReduceLROnPlateau('val_loss', factor=0.1, patience=int(patience / 2), verbose=1) csv_logger = CSVLogger(log_file_path, append=False) model_names = trained_models_path + '.{epoch:02d}-{val_acc:.2f}.hdf5' model_checkpoint = ModelCheckpoint(model_names, monitor='val_loss', verbose=1, save_best_only=True,
ax.imshow((-x[i, :, :, 0] + 1)/2, cmap=plt.cm.gray) ax.axis('off') plt.tight_layout() fig.savefig('gs.png', dpi=300) plt.show(); MODEL_WEIGHTS_FILE = inDir + '/Prav_Model13.h5' callbacks = [ ReduceLROnPlateau(monitor='val_categorical_accuracy', factor=0.5, patience=5, mode='max', cooldown=3, verbose=1), ModelCheckpoint(MODEL_WEIGHTS_FILE, monitor='val_categorical_accuracy', save_best_only=True, verbose=1), ] model = MobileNet(input_shape=(size, size, 1), alpha=1., weights=None, classes=NCATS) model.compile(optimizer=Adam(lr=learning_rate), loss='categorical_crossentropy', metrics=[categorical_crossentropy, categorical_accuracy, top_3_accuracy]) print(model.summary()) model.fit_generator( train_datagen, steps_per_epoch=STEPS, # initial_epoch = initial_epoch, epochs=EPOCHS, verbose=1, validation_data=(x_valid, y_valid), callbacks = callbacks ) model.load_weights(MODEL_WEIGHTS_FILE)
from keras.applications import MobileNet from keras.backend import categorical_crossentropy from keras.callbacks import TensorBoard, ModelCheckpoint from dataset import SmokeGifSequence if __name__ == '__main__': input_shape = (299, 299, 3) # hdf = "m1.h5" hdf = "temporal_vg_smoke_v1.h5" m = MobileNet(input_shape=(299, 299, 20), weights=None, classes=2) # load_model(hdf) m.load_weights(hdf) m.compile("adam", categorical_crossentropy, metrics=["accuracy"]) # plot_model(m, show_shapes=True) m.summary() # data_dir = "/blender/storage/datasets/smoking/gifs/" data_dir = "/blender/storage/datasets/vg_smoke" train_seq = SmokeGifSequence(data_dir, neg_txt='negatives.txt', pos_txt='positives.txt', input_shape_hwc=input_shape, only_temporal=True) # val_seq = SmokeGifSequence(data_dir, neg_txt='validate_neg.txt', pos_txt='validate_pos.txt', # input_shape_hwc=input_shape, # only_temporal=True) log_dir = os.path.join("./logs", os.path.basename(hdf))