def getTrainedModel(model_to_train, dataset_loc, metadata_loc, STORE_FOLDER, op_model_file_name, num_of_epoch=111, data_batch_size=100, training_batch_size=10, create_metadata=True): """ dataset_loc: full paths required e.g. /home/user/dtop/test/file.h5 same for metadata_loc "add kwargs: for md path" """ with loader.hf.File(dataset_loc, 'r') as f: num_epoch = num_of_epoch metadata = loader.loadMetaData(metadata_loc) nb_classes = int(metadata[0]["nb_classes"]) next_batch = loader.getNextBatch #b_size=metadata[0]["dataset_shape"][0] b_size = data_batch_size print("Batch_size:", b_size, "metadata[0]=", metadata[0]) #wait=input("Enter 1 and proceed;") y = loader.np.zeros(shape=(b_size, nb_classes)) x, y_tr = next_batch(b_size, f, metadata) x = x.astype('float32') x = x.transpose(0, 3, 1, 2) x = (x - 100) / 255 print("x:", x.shape) print("y:", y.shape) print(y_tr) #for j in range(len(y_tr)): #print() #y[j][y_tr[j][0]]=1 y = np_utils.to_categorical(y_tr, nb_classes) print("Y:", y) model_to_train.fit(x, y, batch_size=training_batch_size, nb_epoch=num_epoch) generated_model_address = STORE_FOLDER + '/' + op_model_file_name + '.h5' model_to_train.save(generated_model_address) if create_metadata: specs = metadata[0] specs.update({ "original_model_address": generated_model_address, "dataset_trained_on": dataset_loc, "num_of_epoch": num_of_epoch, "data_batch_size": data_batch_size, "training_batch_size": training_batch_size }) loader.generateMetaData( STORE_FOLDER + '/' + op_model_file_name + '_metadata.txt', specs, metadata[1]) return generated_model_address
def getTrainableData(dataset_loc, dataset_metadata_loc): with loader.hf.File(dataset_loc, 'r') as f: metadata = loader.loadMetaData(dataset_metadata_loc) nb_classes = int(metadata[0]["nb_classes"]) next_batch = loader.getNextBatch b_size = metadata[0]["dataset_shape"][0] #YOU were trapped here print("Batch_size:", b_size, "metadata[0]=", metadata[0]) y = loader.np.zeros(shape=(b_size, nb_classes)) x, y_tr = next_batch(b_size, f, metadata) x = normalizeAsDuringTraining(x) # print("x:",x.shape) # print("y:",y.shape) # print(y_tr) y = np_utils.to_categorical(y_tr, nb_classes) return (x, y)
def evaluateModel(model_loc, test_dataset_loc, test_metadata_loc, use_whole_dataset=True, percentage_used=100): with loader.hf.File(test_dataset_loc, 'r') as f: md = loader.loadMetaData(test_metadata_loc) if use_whole_dataset: x, y = loader.getNextBatch(md[0]["dataset_shape"][0], f, md) else: assert (percentage_used <= 100) num_samples = int( (md[0]["dataset_shape"][0]) * percentage_used * 0.01) x, y = loader.getNextBatch(num_samples, f, test_metadata_loc) x_test = x.transpose(0, 3, 1, 2) x_test = x_test.astype('float32') x_test = (x_test - 100) / 255 # print("X_Test.dtype",x_test.dtype) model = load_model(model_loc) pred_label = model.predict(x_test) wrong_count = 0 for i in range(x.shape[0]): #cv.putText(x[i],str(y[i][0]),(60,90), cv.FONT_HERSHEY_COMPLEX, 1,(0,0,255),1) #cv.rectangle(x[i],(55,85),(100,100),(0,255,0),3) #print(pred_label.shape) #for itr in range (pred_label.shape[0]): pr = loader.np.argmax(pred_label[i]) print("itr:", i, " Pred:", pr, end=" ") print("Actual_LAbel:", y[i][0]) if pr == y[i][0]: pass else: wrong_count += 1 accuracy = (1 - wrong_count / x.shape[0]) * 100 print("Wrong:", wrong_count, " %age Acc:", accuracy) return accuracy
def getOptimalTrainedModel(model_to_train, dataset_loc, metadata_loc, STORE_FOLDER, op_model_file_name, accuracy_increase_cutoff=1, cutoff_num_epochs_from_last_update=200, val_partition_fraction=None, val_dataset_loc=None, val_metadata_loc=None, num_of_epoch=1000000, data_batch_size=None, training_batch_size=10, create_metadata=True): """ dataset_loc: full paths required e.g. /home/user/dtop/test/file.h5 same for metadata_loc "add kwargs: for md path" """ num_epoch = num_of_epoch metadata = loader.loadMetaData(metadata_loc) if (data_batch_size == None): data_batch_size = metadata[0]["shape"][0] b_size = data_batch_size x, y = getTrainableData(dataset_loc, metadata_loc) generated_model_address = STORE_FOLDER + '/' + op_model_file_name + '.h5' if (val_partition_fraction == None and val_dataset_loc == None): save_at_intervals = ModelCheckpoint(generated_model_address, monitor='acc', save_best_only=True, mode='max') early_termination = EarlyStopping( monitor='acc', min_delta=accuracy_increase_cutoff / 100, patience=cutoff_num_epochs_from_last_update, verbose=1, mode='max') history = model_to_train.fit( x, y, batch_size=training_batch_size, nb_epoch=num_epoch, callbacks=[save_at_intervals, early_termination]) elif (val_dataset_loc != None): save_at_intervals = ModelCheckpoint(generated_model_address, monitor='val_acc', save_best_only=True, mode='max') early_termination = EarlyStopping( monitor='val_acc', min_delta=accuracy_increase_cutoff / 100, patience=cutoff_num_epochs_from_last_update, verbose=1, mode='max') assert (val_metadata_loc != None) xv, yv = getTrainableData(val_dataset_loc, val_metadata_loc) history = model_to_train.fit( x, y, batch_size=training_batch_size, nb_epoch=num_epoch, validation_data=(xv, yv), callbacks=[save_at_intervals, early_termination]) else: save_at_intervals = ModelCheckpoint(generated_model_address, monitor='val_acc', save_best_only=True, mode='max') early_termination = EarlyStopping( monitor='val_acc', min_delta=accuracy_increase_cutoff / 100, patience=cutoff_num_epochs_from_last_update, verbose=1, mode='max') history = model_to_train.fit( x, y, batch_size=training_batch_size, nb_epoch=num_epoch, validation_split=val_partition_fraction, callbacks=[save_at_intervals, early_termination]) #model_to_train.save(generated_model_address) print("In getTrainedModel History= ", history) if create_metadata: specs = metadata[0] specs.update({ "original_model_address": generated_model_address, "dataset_trained_on": dataset_loc, "num_of_epoch": num_of_epoch, "data_batch_size": data_batch_size, "training_batch_size": training_batch_size }) loader.generateMetaData( STORE_FOLDER + '/' + op_model_file_name + '_metadata', specs, metadata[1]) return generated_model_address, STORE_FOLDER + '/' + op_model_file_name + '_metadata.txt' #metadata address
def getCustomOptimalTrainedModel(model_to_train, dataset_loc, metadata_loc, STORE_FOLDER, op_model_file_name, accuracy_to_start_saving=60, save_at_accuracy_increase_of=2.5, accuracy_increase_cutoff=1, cutoff_num_epochs_from_last_update=200, val_partition_fraction=None, val_dataset_loc=None, val_metadata_loc=None, num_of_epoch=1000000, data_batch_size=None, training_batch_size=10, create_metadata=True): """ dataset_loc: full paths required e.g. /home/user/dtop/test/file.h5 same for metadata_loc "add kwargs: for md path" """ num_epoch = num_of_epoch metadata = loader.loadMetaData(metadata_loc) if (data_batch_size == None): data_batch_size = metadata[0]["shape"][0] x, y = getTrainableData(dataset_loc, metadata_loc) if (val_partition_fraction == None and val_dataset_loc == None): save_at_intervals = lambdaHelper(STORE_FOLDER, op_model_file_name, 'acc', accuracy_to_start_saving, save_at_accuracy_increase_of, accuracy_increase_cutoff, cutoff_num_epochs_from_last_update) history = model_to_train.fit(x, y, batch_size=training_batch_size, nb_epoch=num_epoch, callbacks=[save_at_intervals]) elif (val_dataset_loc != None): save_at_intervals = lambdaHelper(STORE_FOLDER, op_model_file_name, 'val_acc', accuracy_to_start_saving, save_at_accuracy_increase_of, accuracy_increase_cutoff, cutoff_num_epochs_from_last_update) assert (val_metadata_loc != None) xv, yv = getTrainableData(val_dataset_loc, val_metadata_loc) history = model_to_train.fit(x, y, batch_size=training_batch_size, nb_epoch=num_epoch, validation_data=(xv, yv), callbacks=[save_at_intervals]) else: save_at_intervals = lambdaHelper(STORE_FOLDER, op_model_file_name, 'val_acc', accuracy_to_start_saving, save_at_accuracy_increase_of, accuracy_increase_cutoff, cutoff_num_epochs_from_last_update) history = model_to_train.fit(x, y, batch_size=training_batch_size, nb_epoch=num_epoch, validation_split=val_partition_fraction, callbacks=[save_at_intervals]) print("____MODEL_LIST______\n", save_at_intervals.model_list) if (save_at_intervals.best_model_loc != None): print("Using Best One") generated_model_address = save_at_intervals.best_model_loc #STORE_FOLDER+'/'+op_model_file_name+'.h5' generated_model_md_address = generated_model_address[0:-3] + "_metadata" # print(generated_model_address) else: generated_model_address = STORE_FOLDER + "/" + op_model_file_name + ".h5" generated_model_md_address = STORE_FOLDER + "/" + op_model_file_name + "_metadata" model_to_train.save(generated_model_address) print("In getTrainedModel History= ", history) if create_metadata: specs = metadata[0] specs.update({ "original_model_address": generated_model_address, "dataset_trained_on": dataset_loc, "data_batch_size": data_batch_size, "training_batch_size": training_batch_size }) loader.generateMetaData(generated_model_md_address, specs, metadata[1]) #remove_later plot_summary = True if plot_summary: import matplotlib.pyplot as plt plt.plot(history.history["val_acc"]) plt.plot(history.history["acc"]) plt.title('model accuracy') plt.ylabel('accuracy') plt.xlabel('epoch') plt.legend(['train', 'val'], loc='upper left') plt.show() plt.figure() plt.plot(history.history['loss']) plt.plot(history.history['val_loss']) plt.title('model loss') plt.ylabel('loss') plt.xlabel('epoch') plt.legend(['train', 'val'], loc='upper left') plt.show() return generated_model_address, generated_model_md_address + '.txt' #metadata address
def labelFaces(model_loc, model_metadata_loc, image): md = loader.loadMetaData(model_metadata_loc) label_map = getLabelMap(md[1]) # print ("Metadata: ",md) img = image.copy() face_array, faces = getFacesForPrediction(img, md[0]["shape"]) model = load_model(model_loc) face_array = normalizeAsDuringTraining(face_array) pred_labels = model.predict(face_array) confusion_list = [ ] # A list of list of the form [label_with_max_score, similar_label_1, similar_label_2,...] labels_and_confidences = [ ] # list of tuples like (0,0.98) where 0 is the predicted class label, with a confidence of 98% # print("Pred_labels",pred_labels.shape) for i in range(face_array.shape[0]): max_score_label = loader.np.argmax(pred_labels[i]) max_score = pred_labels[i][max_score_label] (x, y, w, h) = faces[i] if checkStatus(max_score) == -1: loader.cv.rectangle(img, (x, y), (x + w, y + h), bfl.clr["red"], 5) loader.cv.putText( img, str(max_score_label) + ":" + label_map[max_score_label], (x, y), loader.cv.FONT_HERSHEY_COMPLEX_SMALL, 1, bfl.clr["yellow"]) elif checkStatus(max_score) == 0: similar_list = [] for j in range(pred_labels.shape[1]): similarity_level = abs(pred_labels[i][max_score_label] - pred_labels[i][j]) if similarity_level < bfl.CT["similar"]: similar_list.append(j) if len(similar_list) > 0: confusion_list.append(similar_list) loader.cv.rectangle(img, (x, y), (x + w, y + h), bfl.clr["yellow"], 5) loader.cv.putText( img, str(max_score_label) + ":" + label_map[max_score_label], (x, y), loader.cv.FONT_HERSHEY_COMPLEX_SMALL, 1, bfl.clr["blue"]) elif checkStatus(max_score) == 1: loader.cv.rectangle(img, (x, y), (x + w, y + h), bfl.clr["green"], 5) loader.cv.putText( img, str(max_score_label) + ":" + label_map[max_score_label], (x, y), loader.cv.FONT_HERSHEY_COMPLEX_SMALL, 1, bfl.clr["yellow"]) else: raise ValueError labels_and_confidences.append((max_score_label, max_score)) print(pred_labels) return { "image": img, "label_map": md[1], "predicted_labels_and_confidences": labels_and_confidences, "prediction_matrix": pred_labels, "confusion_list": confusion_list }
#MY GLOBAL CONSTANTS LOAD_PATH = '/home/aishwarya/Documents/IOP/data/images/att_faces' #load data from here STORE_PATH = '/home/aishwarya/Documents/IOP/data' #store data in this folder DSETP = STORE_PATH + '/datasets' MODLP = STORE_PATH + '/models' DSETFN = "att_faces" MFN = "model_" + DSETFN # model filename NEPOCH = 800 DBS = 500 # data batch size TBS = 10 # training batch size DMY = "no val" # dummmy dsop.createSingleBlockDataset(LOAD_PATH, DSETP, DSETFN, (50, 50, 3)) md = dsop.loadMetaData(DSETP + '/' + DSETFN + '_metadata.txt') #print(md[0],md[0]["shape"]) # #print("PATH:",DSETP+"/"+DSETFN+".h5",md[0]["shape"]) dsop.navigateDataset(DSETP + "/" + DSETFN + ".h5", md[0]["shape"], 0) dsop.partitionDataset(DSETP + "/" + DSETFN + ".h5", DSETP + "/" + DSETFN + "_metadata.txt", (60, 40)) md = dsop.loadMetaData(DSETP + '/' + DSETFN + '_train_metadata.txt') model = cmg.getModelFrame(md[0]["shape"], int(md[0]["nb_classes"])) DBS = md[0]["dataset_shape"][0] MFN = MFN + "_" + str(NEPOCH) model_path = cmg.getTrainedModel(model, DSETP + "/" + DSETFN + "_train.h5", DSETP + "/" + DSETFN + "_train_metadata.txt", MODLP, MFN, NEPOCH, DBS, TBS)
def trainModel(self): LOAD_PATH = os.path.join(image_path, current_subject_code) DSETP = dataset_path MODLP = model_path DSETFN = current_subject_code MFN = "model_" + DSETFN # model filename NEPOCH = 10 DBS = 500 # data batch size TBS = 10 # training batch size DMY = "no val" # dummmy try: winRegisterStudentPhotos.setGeometry( winRegisterStudentPhotos.frameGeometry()) winRegisterStudentPhotos.hide() print( '-------------------- Model Training Started --------------------' ) dsop.createSingleBlockDataset(LOAD_PATH, DSETP, DSETFN, (50, 50, 3)) md = dsop.loadMetaData(DSETP + '/' + DSETFN + '_metadata.txt') print(md[0], md[0]["shape"]) print("PATH:", DSETP + "/" + DSETFN + ".h5", md[0]["shape"]) dsop.partitionDataset(DSETP + "/" + DSETFN + ".h5", DSETP + "/" + DSETFN + "_metadata.txt", (80, 20)) md = dsop.loadMetaData(DSETP + '/' + DSETFN + '_train_metadata.txt') model = cmg.getModelFrame(md[0]["shape"], int(md[0]["nb_classes"]), 3) DBS = md[0]["dataset_shape"][0] MFN = MFN + "_" + str(NEPOCH) MODEL_LOC = MODLP + "/" + MFN + ".h5" TD_LOC = DSETP + "/" + DSETFN + "_test.h5" TD_MD_LOC = DSETP + "/" + DSETFN + "_test_metadata.txt" trained_model_path, model_md_path = cmg.getCustomOptimalTrainedModel( model, DSETP + "/" + DSETFN + "_train.h5", DSETP + "/" + DSETFN + "_train_metadata.txt", MODLP, MFN, 70, 2, 0.8, 15, 0.2, TD_LOC, TD_MD_LOC, 1000) print(trained_model_path) model_name = os.path.basename(trained_model_path) format_str = """UPDATE subjects SET model_name = ? WHERE subject_code = ?""" params = (model_name, current_subject_code) conn.execute(format_str, params) conn.commit() MODEL_LOC = MODLP + "/" + MFN + ".h5" TD_LOC = DSETP + "/" + DSETFN + "_test.h5" TD_MD_LOC = DSETP + "/" + DSETFN + "_test_metadata.txt" cmg.evaluateModel(trained_model_path, TD_LOC, TD_MD_LOC) print( '-------------------- Model Training Completed --------------------' ) except Exception as e: self.messageLbl.setText( 'ERROR: Some error while training model. Check console!') print("Model Training Failed...\nERRORS:") print(e) self.finishRegistration()