class FeatureExtractor: def __init__(self): with graph.as_default(): self.model = Xception( weights="imagenet", classes=1000, classifier_activation="softmax", ) def extract(self, img): """ Extract a deep feature from an input image Args: img: from PIL.Image.open(path) or tensorflow.keras.preprocessing.image.load_img(path) Returns: feature (np.ndarray): deep feature with the shape=(4096, ) """ img = Image.open(img) img = img.resize( (299, 299)) # Xception must take a 299x299 img as an input img = img.convert('RGB') # Make sure img is color x = image.img_to_array( img) # To np.array. Height x Width x Channel. dtype=float32 x = np.expand_dims( x, axis=0 ) # (H, W, C)->(1, H, W, C), where the first elem is the number of img x = preprocess_input(x) # Subtracting avg values for each pixel with graph.as_default(): set_session(sess) feature = self.model.predict(x)[0] # (1, 4096) -> (4096, ) return feature / np.linalg.norm(feature) # Normalize
def predict(img): model = Xception(weights="imagenet", include_top=True, input_tensor=Input(shape=(224, 224, 3))) img = img_to_array(img) img = img.reshape((-1, img.shape[0], img.shape[1], 3)) / 255 rez = model.predict(img) rez = pd.DataFrame(decode_predictions(rez)[0], columns=['_', 'breed', 'prct']).drop(columns='_').sort_values( by='prct', ascending=False) np.array(rez) return rez
files = librosa.util.find_files(pred_pathAudio, ext=['wav']) files = np.asarray(files) for file in files: name = os.path.basename(file) length = len(name) name = name[0] y, sr = librosa.load(file, sr=22050) mels = librosa.feature.melspectrogram(y, sr=sr, hop_length=128, n_fft=512) pred_mels = librosa.amplitude_to_db(mels, ref=np.max) pred_mels = pred_mels.reshape(1, pred_mels.shape[0], pred_mels.shape[1]) y_pred = model.predict(pred_mels) y_pred_label = np.argmax(y_pred) if y_pred_label == 0: # 여성이라고 예측 print(file, '{:.4f} 의 확률로 여자입니다.', format((y_pred[0][0]) * 100)) if name == 'F': count_f = count_f + 1 else: # 남성이라고 예측 print(file, '{:.4f} 의 확률로 남자입니다.', format((y_pred[0][1]) * 100)) if name == 'M': count_m = count_m + 1 print("43개 여성 목소리 중 " + str(count_f) + "개 정답") print("43개 남성 목소리 중 " + str(count_m) + "개 정답") end = datetime.now() time = end - start_now print("작업 시간 : ", time)
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 }
# Read until video is completed while(cap.isOpened()): # Capture frame-by-frame c += 1 ret, frame = cap.read() if ret == True: # Display the resulting frame frame = cv2.resize(frame,(299,299)) #cv2.imshow('Frame',frame) if c <= 200: frame = img_to_array(frame) frame = frame.reshape((1, frame.shape[0], frame.shape[1], frame.shape[2])) frame = preprocess_input(frame) features = model.predict(frame, verbose=0) print(features,c) F.append(features) #print(np.argmax(features)) else: break # Press Q on keyboard to exit #if cv2.waitKey(25) & 0xFF == ord('q'): # break #plt.plot(len(features),features) #plt.pause(0.05) # Break the loop else: break