zoom_range=0.2, horizontal_flip=True) test_datagen = ImageDataGenerator(rescale=1./255) training_set = train_datagen.flow_from_directory( 'C:/Users/Shubham/Desktop/MLOps-Workspace/Images/train/', target_size=(224, 224), batch_size=32, class_mode='binary') test_set = test_datagen.flow_from_directory( 'C:/Users/Shubham/Desktop/MLOps-Workspace/Images/validation/', target_size=(224, 224), batch_size=32, class_mode='binary') model.fit( training_set, steps_per_epoch=70, epochs=1, validation_data=test_set, validation_steps=10) # In[15]: #Saving and loading the model model.save('face_recog_mobilenet.h5') from keras.models import load_model classifier = load_model('face_recog_mobilenet.h5') # In[26]:
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)
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) predict = modelo.predict(img) if predict[0][0] < 0.7: plt.title(str(100 - predict[0][0] * 100) + "% MASCARILLA") plt.imshow(img_1.astype("uint8")) plt.show()
# Let's train the model using RMSprop model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy']) x_train = x_train.astype('float32') x_test = x_test.astype('float32') x_train /= 255 x_test /= 255 if not data_augmentation: print('Not using data augmentation.') model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_test, y_test), shuffle=True) else: print('Using real-time data augmentation.') # This will do preprocessing and realtime data augmentation: datagen = ImageDataGenerator( featurewise_center=False, # set input mean to 0 over the dataset samplewise_center=False, # set each sample mean to 0 featurewise_std_normalization=False, # divide inputs by std of the dataset samplewise_std_normalization=False, # divide each input by its std zca_whitening=False, # apply ZCA whitening rotation_range=0, # randomly rotate images in the range (degrees, 0 to 180) width_shift_range=0.1, # randomly shift images horizontally (fraction of total width) height_shift_range=0.1, # randomly shift images vertically (fraction of total height) horizontal_flip=True, # randomly flip images