def get_bottleneck(train_datagen, val_datagen): """ Use a pretrained convolutional model to extract the bottleneck features """ model_bottleneck = MobileNet(weights='imagenet', include_top=False, input_shape=(IMG_HEIGHT, IMG_WIDTH, 3)) for layer in model_bottleneck.layers: layer.trainable = False # Get bottleneck features print('\nImage generators:') train_bottleneck_generator = train_datagen.flow_from_directory( TRAIN_DIR, color_mode='rgb', target_size=(IMG_HEIGHT, IMG_WIDTH), batch_size=BATCH_SIZE, class_mode=None, shuffle=False, ) val_bottleneck_generator = val_datagen.flow_from_directory( VALIDATION_DIR, color_mode='rgb', target_size=(IMG_HEIGHT, IMG_WIDTH), batch_size=BATCH_SIZE, class_mode=None, shuffle=False, ) print('\n Extracting bottleneck features:') train_bottleneck = model_bottleneck.predict(train_bottleneck_generator, verbose=1) val_bottleneck = model_bottleneck.predict(val_bottleneck_generator, verbose=1) train_labels = train_bottleneck_generator.classes val_labels = val_bottleneck_generator.classes return model_bottleneck, train_bottleneck, val_bottleneck, train_labels, val_labels
class wheelDetectorTransferLearning(): def __init__(self): #load the trained convolutional neural network self.ML_model = None with open('models/mobileNet4wheels.cpickle', 'rb') as f: self.ML_model = pickle.load(f) # load the MobileNet network and initialize the label encoder print("[INFO] loading MobileNet network...") self.MobileNet = MobileNet(weights="imagenet", include_top=False) # create the keynames of the dictionary self.fieldnames = ["feat_{}".format(i) for i in range(0, 7 * 7 * 1024)] def prepare_image(self, img): img = cv2.resize(img, (224, 244)) img = tf.keras.preprocessing.image.array_to_img(img, data_format=None, scale=True, dtype=None) img_array = image.img_to_array(img) img_array_expanded_dims = np.expand_dims(img_array, axis=0) prepared_image = tf.keras.applications.mobilenet.preprocess_input( img_array_expanded_dims) return prepared_image def get_cnn_feature(self, prepared_image): features = self.MobileNet.predict(prepared_image) flat_feature_vector = features.reshape(7 * 7 * 1024) return flat_feature_vector def testImage(self, img): #print("begin single CNN+ML") prepared_image = self.prepare_image(img) flat_feature_vector = self.get_cnn_feature(prepared_image) dictionary = {} for item in range(len(self.fieldnames)): dictionary[self.fieldnames[item]] = flat_feature_vector[item] # make predictions on the current set of features, output = self.ML_model.predict_proba_one(dictionary) #print("finish single CNN+ML") return output
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[file.rfind('\\') + 1:], '여자입니다.') if name == 'F': count_f += 1 else: # 남성이라고 예측 print(file[file.rfind('\\') + 1:], '남자입니다.') if name == 'M': count_m += 1 print("43개 여성 목소리 중 " + str(count_f) + "개 정답") print("43개 남성 목소리 중 " + str(count_m) + "개 정답") end = datetime.now() time = end - start_now print("작업 시간 : ", time)
fig, axs = plt.subplots(nrows=2, sharex=True, figsize=(16, 10)) axs[0].plot(hist_df.val_categorical_accuracy, lw=5, label='Validation Accuracy') axs[0].plot(hist_df.categorical_accuracy, lw=5, label='Training Accuracy') axs[0].set_ylabel('Accuracy') axs[0].set_xlabel('Epoch') axs[0].grid() axs[0].legend(loc=0) axs[1].plot(hist_df.val_categorical_crossentropy, lw=5, label='Validation MLogLoss') axs[1].plot(hist_df.categorical_crossentropy, lw=5, label='Training MLogLoss') axs[1].set_ylabel('MLogLoss') axs[1].set_xlabel('Epoch') axs[1].grid() axs[1].legend(loc=0) fig.savefig('hist.png', dpi=300) plt.show(); valid_predictions = model.predict(x_valid, batch_size=128, verbose=1) map3 = mapk(valid_df[['y']].values, preds2catids(valid_predictions).values) print('Map3: {:.3f}'.format(map3)) test = pd.read_csv(os.path.join(INPUT_DIR, 'test_simplified.csv')) test.head() x_test = df_to_image_array_xd(test, size) print(test.shape, x_test.shape) print('Test array memory {:.2f} GB'.format(x_test.nbytes / 1024.**3 )) test_predictions = model.predict(x_test, batch_size=128, verbose=1) top3 = preds2catids(test_predictions) top3.head() top3.shape cats = list_all_categories() id2cat = {k: cat.replace(' ', '_') for k, cat in enumerate(cats)}
monitor='val_categorical_accuracy', mode='max', save_best_only=True, verbose=1) ] hists = [] hist = model.fit_generator(train_datagen, steps_per_epoch=STEPS, epochs=EPOCHS, verbose=1, validation_data=(x_valid, y_valid), callbacks=callbacks) hists.append(hist) valid_predictions = model.predict(x_valid, batch_size=256, verbose=1) map3 = mapk(valid_df[['y']].values, preds2catids(valid_predictions).values) print('Map3: {:.3f}'.format(map3)) test = pd.read_csv('test_simplified.csv') test.head() x_test = df_to_image_array_xd(test, size) x_test_flip = np.flip(x_test, 2) print(test.shape, x_test.shape) print('Test array memory {:.2f} GB'.format(x_test.nbytes / 1024.**3)) # TTA test_predictions1 = model.predict(x_test, batch_size=128, verbose=1) test_predictions2 = model.predict(x_test_flip, batch_size=128, verbose=1) final_predictions = np.average([test_predictions1, test_predictions2], axis=0,
class CNN_tester(): def __init__(self): #load the trained convolutional neural network self.ML_model = None with open('models/mobileNet4wheels.cpickle', 'rb') as f: self.ML_model = pickle.load(f) # load the MobileNet network and initialize the label encoder print("[INFO] loading MobileNet network...") self.MobileNet = MobileNet(weights="imagenet", include_top=False) # create the keynames of the dictionary self.fieldnames = ["feat_{}".format(i) for i in range(0, 7 * 7 * 1024)] def prepare_image(self, img): img = cv2.resize(img, (224, 244)) img = tf.keras.preprocessing.image.array_to_img(img, data_format=None, scale=True, dtype=None) img_array = image.img_to_array(img) img_array_expanded_dims = np.expand_dims(img_array, axis=0) prepared_image = tf.keras.applications.mobilenet.preprocess_input( img_array_expanded_dims) return prepared_image def get_cnn_feature(self, prepared_image): features = self.MobileNet.predict(prepared_image) flat_feature_vector = features.reshape(7 * 7 * 1024) return flat_feature_vector def testImage(self, flat_feature_vector): dictionary = {} for item in range(len(self.fieldnames)): dictionary[self.fieldnames[item]] = flat_feature_vector[item] # make predictions on the current set of features, output = self.ML_model.predict_proba_one(dictionary) return output def runCNN_and_ML_on_dataset(self): carwheels = 0 none_carwheels = 0 for image in imagePaths: img = image img = cv2.imread(img) img = self.prepare_image(img) flat_feature_vector = self.get_cnn_feature(img) result = self.testImage(flat_feature_vector) print(result) if result[True] >= 0.5: carwheels += 1 else: none_carwheels += 1 print("the number of car wheels: " + str(carwheels)) print("the number of none car wheels: " + str(none_carwheels)) print("total number of images: " + str(len(imagePaths))) return carwheels, none_carwheels def display_image(self): for image in imagePaths: img = image img = cv2.imread(img) cv2.imshow("Car wheel Detection", img) cv2.waitKey(0)
# -*- coding: utf-8 -*- # organize imports import tensorflow as tf import os from tensorflow.keras.preprocessing.image import ImageDataGenerator from tensorflow.keras.applications import MobileNet, MobileNetV2, ResNet50, VGG16, VGG19 images_train_path = os.path.join(os.getcwd(), 'emotions') images_train = ImageDataGenerator(rescale=1./255) images_generator = images_train.flow_from_directory(images_train_path, target_size=(350, 350)) mobilenet = MobileNet() prediction = mobilenet.predict(images_generator) mobilenetv2 = MobileNetV2() prediction = mobilenetv2.predict(images_generator) resnet50 = ResNet50() prediction = resnet50.predict(images_generator) vgg16 = VGG16() prediction = vgg16.predict(images_generator) vgg19 = VGG19()
# while ensuring the image is resized to 224x224 pixels image = load_img(imagePath, target_size=(224, 224)) image = img_to_array(image) # preprocess the image by (1) expanding the dimensions and # (2) subtracting the mean RGB pixel intensity from the # ImageNet dataset image = np.expand_dims(image, axis=0) image = preprocess_input(image) # add the image to the batch batchImages.append(image) # pass the images through the network and use the outputs as # our actual features, then reshape the features into a # flattened volume batchImages_np = np.vstack(batchImages) features = model.predict(batchImages_np, batch_size=config.BATCH_SIZE) features = features.reshape((features.shape[0], 7 * 7 * 1024)) ## making a second variable batchImages_np is neccessary as batchImages.append doesn't work on np arrays. # loop over the class labels and extracted features for (label, vec) in zip(batchLabels, features): # construct a row that exists of the class label and # extracted features vec = ",".join([str(v) for v in vec]) outcsv.write("{},{}\n".format(label, vec)) # close the CSV file outcsv.close() # serialize the label encoder to disk f = open(config.LE_PATH, "wb") f.write(pickle.dumps(le)) f.close()
ModelCheckpoint('model_vf.h5', monitor='val_top_3_accuracy', mode='max', save_best_only=True, save_weights_only=True), ] hist = model.fit_generator(train_datagen, steps_per_epoch=STEPS, epochs=EPOCHS, verbose=1, validation_data=(x_valid, y_valid), callbacks=callbacks) print('Calculating MAP') valid_predictions = model.predict(x_valid, batch_size=128, verbose=2) map3 = mapk(valid_df[['y']].values, preds2catids(valid_predictions).values) print('Map3: {:.3f}'.format(map3)) print('Creating test files') INPUT_DIR_TEST = '/home/akhilesh_narapareddy/apm_test_data/' test = pd.read_csv(os.path.join(INPUT_DIR_TEST, 'test_simplified.csv')) test_split = np.array_split(test, 10) final_test = {} for i in range(10): final_test[i] = df_to_image_array_xd(test_split[i], size) print('Predicting from test files') test_final = {} for i in range(10): test_final[i] = model.predict(final_test[i], batch_size=128, verbose=2)