class_mode = 'categorical', subset = subset) while True: X = gen.next() if loss == 'arcface': yield [X[0], X[1]], X[1] else: yield X[0], X[1] train_generator = mobilefacenet_input_generator(train_datagen, train_path, 'training', LOSS_TYPE) validate_generator = mobilefacenet_input_generator(train_datagen, train_path, 'validation', LOSS_TYPE) '''Loading the model & re-defining''' model = mobile_face_net_train(OLD_NUM_LABELS, loss = OLD_LOSS_TYPE) print("Reading the pre-trained model... ") model.load_weights(r'../Models/MobileFaceNet_train.h5') # model.load_weights("E:\\Python_Coding\\MobileFaceNet\\tl_model_1905270955.hdf5") print("Reading done. ") model.summary() # model.layers if OLD_NUM_LABELS == NUM_LABELS and not FC_LAYER_CHANGE and OLD_LOSS_TYPE == LOSS_TYPE: customed_model = model customed_model.summary() elif OLD_NUM_LABELS != NUM_LABELS or FC_LAYER_CHANGE or OLD_LOSS_TYPE != LOSS_TYPE: # Re-define the model model.layers.pop() # Remove the ArcFace Loss Layer model.layers.pop() # Remove the Label Input Layer
Created on Mon May 13 14:39:29 2019 @author: TMaysGGS """ '''Last updated on 2020.03.29 03:14''' '''Importing the libraries''' from tensorflow.python.keras.models import Model from Model_Structures.MobileFaceNet import mobile_face_net_train, mobile_face_net NUM_LABELS = 67960 LOSS_TYPE = 'softmax' '''Loading the training model''' model = mobile_face_net_train(NUM_LABELS, loss = LOSS_TYPE) model.load_weights('./Models/MobileFaceNet_train.h5') model.summary() pred_model = mobile_face_net() pred_model.summary() '''Extracting the weights & transfering to the prediction model''' temp_weights_list = [] for layer in model.layers: if 'dropout' in layer.name: continue temp_layer = model.get_layer(layer.name) temp_weights = temp_layer.get_weights() temp_weights_list.append(temp_weights)
sys.path.append('../') from Model_Structures.MobileFaceNet import mobile_face_net_train os.environ['CUDA_VISIBLE_DEVICES'] = '0' BATCH_SIZE = 128 NUM_LABELS = 153 # 40 m = 34363 # 8922 DATA_SPLIT = 0.01 TOTAL_EPOCHS = 30 LOSS_TYPE = 'arcface' OPTIMIZER = Adam LR = 0.001 '''Loading the model''' model = mobile_face_net_train( NUM_LABELS, loss=LOSS_TYPE) # change the loss to 'arcface' for fine-tuning if LOSS_TYPE == 'arcface': INPUT_NAME_LIST = [] for model_input in model.inputs: INPUT_NAME_LIST.append(model_input.name[:-2]) '''Importing the TFRecord(s)''' # Directory where the TFRecords files are tfrecord_save_dir = r'../Data' tfrecord_path_list = [] for file_name in os.listdir(tfrecord_save_dir): if file_name[-9:] == '.tfrecord': tfrecord_path_list.append(os.path.join(tfrecord_save_dir, file_name)) image_feature_description = { 'height': tf.io.FixedLenFeature([], tf.int64),
def CreateModel(nb_classes): model = mobile_face_net_train(nb_classes, loss='softmax') #model.compile(optimizer=Adam(lr=0.001, epsilon=1e-8), loss='categorical_crossentropy', metrics=['accuracy']) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) return model
# -*- coding: utf-8 -*- """ Created on Mon May 13 14:39:29 2019 @author: TMaysGGS """ '''Last updated on 12/24/2019 11:16''' '''Importing the libraries''' from keras.models import Model from keras.utils import plot_model from Model_Structures.MobileFaceNet import mobile_face_net_train NUM_LABELS = 67960 '''Loading the model & re-defining''' model = mobile_face_net_train(NUM_LABELS, loss='arcface') model.load_weights('./Models/MobileFaceNet_train.h5') # model.load_weights("E:\\Python_Coding\\MobileFaceNet\\model.hdf5") model.summary() model.layers # Re-define the model model.layers.pop() # Remove the ArcFace Loss Layer model.layers.pop() # Remove the Label Input Layer model.summary() model.layers[-1].outbound_nodes = [] model.outputs = [model.layers[-1].output] # Reset the output output = model.get_layer(model.layers[-1].name).output model.input # The model used for prediction
yield [X[0], X[1]], X[1] else: yield X[0], X[1] train_generator = mobilefacenet_input_generator(train_datagen, train_path, 'training', 'softmax') validate_generator = mobilefacenet_input_generator(train_datagen, train_path, 'validation', 'softmax') '''Training the Model''' # Train on multiple GPUs # from tensorflow.keras.utils import multi_gpu_model # model = multi_gpu_model(model, gpus = 2) model = mobile_face_net_train( NUM_LABELS, loss='softmax') # change the loss to 'arcface' for fine-tuning model.summary() model.layers model.compile(optimizer=Adam(lr=0.001, epsilon=1e-8), loss='categorical_crossentropy', metrics=['accuracy']) # Save the model after every epoch check_pointer = ModelCheckpoint(filepath='../Models/MobileFaceNet_train.h5', verbose=1, save_best_only=True) # Interrupt the training when the validation loss is not decreasing early_stopping = EarlyStopping(monitor='val_loss', patience=10000)
yield [X[0], X[1]], X[1] else: yield X[0], X[1] train_generator = mobilefacenet_input_generator(train_datagen, train_path, 'training', 'softmax') validate_generator = mobilefacenet_input_generator(train_datagen, train_path, 'validation', 'softmax') '''Training the Model''' # Train on multiple GPUs # from keras.utils import multi_gpu_model # model = multi_gpu_model(model, gpus = 2) model = mobile_face_net_train(NUM_LABELS, loss='softmax') model.summary() model.layers model.compile(optimizer=Adam(lr=0.001, epsilon=1e-8), loss='categorical_crossentropy', metrics=['accuracy']) # Save the model after every epoch from keras.callbacks import ModelCheckpoint check_pointer = ModelCheckpoint(filepath='../Models/MobileFaceNet_train.h5', verbose=1, save_best_only=True) # Interrupt the training when the validation loss is not decreasing from keras.callbacks import EarlyStopping
class_mode = 'categorical', subset = subset) while True: X = gen.next() if loss == 'arcface': yield [X[0], X[1]], X[1] else: yield X[0], X[1] train_generator = mobilefacenet_input_generator(train_datagen, train_path, 'training', LOSS_TYPE) validate_generator = mobilefacenet_input_generator(train_datagen, train_path, 'validation', LOSS_TYPE) '''Loading the model & re-defining''' model = mobile_face_net_train(OLD_NUM_LABELS, loss = OLD_LOSS_TYPE) print("Reading the pre-trained model... ") model.load_weights(r'../Models/MobileFaceNet_train.h5') print("Reading done. ") model.summary() # plot_model(model, to_file = r'../Models/training_model.png', show_shapes = True, show_layer_names = True) # model.layers # SoftMax-ArcFace, FC layer change/not chage if OLD_LOSS_TYPE == 'softmax' and LOSS_TYPE == 'arcface': customed_model = mobile_face_net_train(NUM_LABELS, loss = LOSS_TYPE) temp_weights_list = [] for layer in model.layers: temp_layer = model.get_layer(layer.name) temp_weights = temp_layer.get_weights() temp_weights_list.append(temp_weights)