def buildTunerModel(self, img_size): """ This function builds a base Xception model for keras tuner Parameters: img_size (int): the size of input images (img_size, img_size). Returns: model (class): the Xception model used for keras tuner. """ base_model = Xception(weights='imagenet', include_top=False, input_shape=(img_size, img_size, 3)) if not self.weights == 'imagenet': base_model.load_weights(self.weights, by_name=True) return base_model
def train_and_test(model_name, compression_type, training_size, test_size): # train model if compression_type == "basic" and test_size == 224: start = time.process_time() print("training") model = train(model_name, train_flow, valid_flow) end = time.process_time() else: start = time.process_time() model = Xception( weights=None, include_top=False, #input_shape=(224,224,3) ) model = build_model(model) print("loading weight") model.load_weights("results/Xception.ckpt") end = time.process_time() training_time = end - start FLOW_MAP = { "nearest": test_flow_nearest, "box": test_flow_box, "lanczos": test_flow_lanczos, "hamming": test_flow_hamming, } test_flow = FLOW_MAP[compression_type] # save model # np.savez("model_%s.npz" % model_name, **model) #Evaluate Model # get compression specific test flow # test_flow = start = time.process_time() train_pred = model.predict(valid_flow) train_test = valid_flow.classes train_accuracy = metrics.accuracy_score(train_test, np.round(train_pred)) # train_accuracy = history.history["accuracy"] test_pred = model.predict(test_flow) test_test = test_flow.classes test_accuracy = metrics.accuracy_score(test_test, np.round(test_pred)) print("Sample predictions: ", np.round(test_pred)) print("Sample actual labels: ", test_test) test_matrix = metrics.confusion_matrix(test_test, np.round(test_pred)).ravel() end = time.process_time() test_time = end - start print("Model: %s\n" % model_name) print("Image Compression: %s\n" % compression) print("Train Image Size: %d\n" % training_size) print("Test Image Size: %d\n" % test_size) print("Train accuracy: %f\n" % train_accuracy) print("Test accuracy: %f\n" % test_accuracy) print("True Negatives: %f\n" % test_matrix[0]) print("False Positives: %f\n" % test_matrix[1]) print("False Negatives: %f\n" % test_matrix[2]) print("True Positives: %f\n" % test_matrix[3]) print("Training Time: %s sec\n" % training_time) print("Testing Time: %s sec\n" % test_time) # save results try: with open("results/%s_results.txt" % model_name, "a") as f: f.write("Model: %s\n" % model_name) f.write("Image Compression: %s\n" % compression) f.write("Train Image Size: %d\n" % training_size) f.write("Test Image Size: %d\n" % test_size) f.write("Train accuracy: %f\n" % train_accuracy) f.write("Test accuracy: %f\n" % test_accuracy) f.write("True Negatives: %f\n" % test_matrix[0]) f.write("False Positives: %f\n" % test_matrix[1]) f.write("False Negatives: %f\n" % test_matrix[2]) f.write("True Positives: %f\n" % test_matrix[3]) f.write("Training Time: %s sec\n" % training_time) f.write("Testing Time: %s sec\n" % test_time) f.write("\n\n") f.close() except Exception as e: print(e) return { "Model": model_name, "Image Compression": compression, "Train Image Size": training_size, "Test Image Size": test_size, "Training Accuracy": train_accuracy, "Test Accuracy": test_accuracy, "True Negative": test_matrix[0], "False Positive": test_matrix[1], "False Negative": test_matrix[2], "True Positive": test_matrix[3], "Train Time": training_time, "Test Time": test_time }
path = 'C:/nmb/nmb_data/h5/5s/Xception/xception_sgd_2.h5' mc = ModelCheckpoint(path, monitor='val_loss', verbose=1, save_best_only=True) model.compile(optimizer=op, loss="sparse_categorical_crossentropy", metrics=['acc']) history = model.fit(x_train, y_train, epochs=1000, batch_size=batch_size, validation_split=0.2, callbacks=[es, lr, mc]) # 평가, 예측 # model = load_model('C:/nmb/nmb_data/h5/5s/Xception/xception_sgd_2.h5') model.load_weights('C:/nmb/nmb_data/h5/5s/Xception/xception_sgd_2.h5') result = model.evaluate(x_test, y_test, batch_size=8) print("loss : {:.5f}".format(result[0])) print("acc : {:.5f}".format(result[1])) ############################################ PREDICT #################################### pred = ['C:/nmb/nmb_data/predict_04_26/F', 'C:/nmb/nmb_data/predict_04_26/M'] count_f = 0 count_m = 0 for pred_pathAudio in pred: files = librosa.util.find_files(pred_pathAudio, ext=['wav']) files = np.asarray(files) for file in files: