From c7456800ebcc43c46dd773a720fc8cf369097e5e Mon Sep 17 00:00:00 2001 From: Amro Tork Date: Sat, 29 Feb 2020 01:00:26 +0200 Subject: [PATCH 1/3] - Update to allow iteration over files. --- classifiers.py | 7 ++++--- example.py | 23 +++++++++++++++-------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/classifiers.py b/classifiers.py index 67b79c8..c5aa35b 100644 --- a/classifiers.py +++ b/classifiers.py @@ -1,8 +1,9 @@ # -*- coding:utf-8 -*- -from keras.models import Model as KerasModel -from keras.layers import Input, Dense, Flatten, Conv2D, MaxPooling2D, BatchNormalization, Dropout, Reshape, Concatenate, LeakyReLU -from keras.optimizers import Adam + +from tensorflow.keras import Model as KerasModel +from tensorflow.keras.layers import Input, Dense, Flatten, Conv2D, MaxPooling2D, BatchNormalization, Dropout, Reshape, Concatenate, LeakyReLU +from tensorflow.keras.optimizers import Adam IMGWIDTH = 256 diff --git a/example.py b/example.py index 8269a03..d3b54dd 100644 --- a/example.py +++ b/example.py @@ -2,7 +2,9 @@ from classifiers import * from pipeline import * -from keras.preprocessing.image import ImageDataGenerator +import os + +from tensorflow.keras.preprocessing.image import ImageDataGenerator # 1 - Load the model and its pretrained weights classifier = Meso4() @@ -17,17 +19,22 @@ 'test_images', target_size=(256, 256), batch_size=1, + shuffle=False, class_mode='binary', subset='training') # 3 - Predict -X, y = generator.next() -print('Predicted :', classifier.predict(X), '\nReal class :', y) +num_iterations = 0 +for X, y in generator: + print('Predicted :', classifier.predict(X), '\nReal class :', y) + num_iterations += 1 + if num_iterations >= 4: + break # 4 - Prediction for a video dataset -classifier.load('weights/Meso4_F2F') - -predictions = compute_accuracy(classifier, 'test_videos') -for video_name in predictions: - print('`{}` video class prediction :'.format(video_name), predictions[video_name][0]) \ No newline at end of file +# classifier.load('weights/Meso4_F2F') +# +# predictions = compute_accuracy(classifier, 'test_videos') +# for video_name in predictions: +# print('`{}` video class prediction :'.format(video_name), predictions[video_name][0]) \ No newline at end of file From 96221728abbb808b1494ecfd025125e99cf1f166 Mon Sep 17 00:00:00 2001 From: Amro Tork Date: Sat, 29 Feb 2020 01:05:30 +0200 Subject: [PATCH 2/3] - Reverting back changes and iterating over files in generator. --- example.py | 9 ++++----- predict_on_directory.py | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 predict_on_directory.py diff --git a/example.py b/example.py index d3b54dd..11571bb 100644 --- a/example.py +++ b/example.py @@ -32,9 +32,8 @@ break # 4 - Prediction for a video dataset +classifier.load('weights/Meso4_F2F') -# classifier.load('weights/Meso4_F2F') -# -# predictions = compute_accuracy(classifier, 'test_videos') -# for video_name in predictions: -# print('`{}` video class prediction :'.format(video_name), predictions[video_name][0]) \ No newline at end of file +predictions = compute_accuracy(classifier, 'test_videos') +for video_name in predictions: + print('`{}` video class prediction :'.format(video_name), predictions[video_name][0]) \ No newline at end of file diff --git a/predict_on_directory.py b/predict_on_directory.py new file mode 100644 index 0000000..3817056 --- /dev/null +++ b/predict_on_directory.py @@ -0,0 +1,41 @@ +import numpy as np +from classifiers import * +from pipeline import * + +import os + +from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array + +# 1 - Load the model and its pretrained weights +classifier = Meso4() +classifier.load('weights/Meso4_DF') + +# 2 - Minimial image generator +# We did use it to read and compute the prediction by batchs on test videos +# but do as you please, the models were trained on 256x256 images in [0,1]^(n*n) + +required_size = (256, 256) +# dataGenerator = ImageDataGenerator(rescale=1./255) +# generator = dataGenerator.flow_from_directory( +# 'test_images', +# target_size=(256, 256), +# batch_size=1, +# shuffle=False, +# class_mode='binary', +# subset='training') + +# 3 - Predict + +for X, y in generator: + print('Predicted :', classifier.predict(X), '\nReal class :', y) + num_iterations += 1 + if num_iterations >= 4: + break + +# 4 - Prediction for a video dataset + +# classifier.load('weights/Meso4_F2F') +# +# predictions = compute_accuracy(classifier, 'test_videos') +# for video_name in predictions: +# print('`{}` video class prediction :'.format(video_name), predictions[video_name][0]) \ No newline at end of file From 8aff739e38263ff2eaf42486ffdeb235bfd8edf9 Mon Sep 17 00:00:00 2001 From: Amro Tork Date: Sat, 29 Feb 2020 01:17:44 +0200 Subject: [PATCH 3/3] - Adding code to predict on folder. --- predict_on_directory.py | 66 +++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 36 deletions(-) diff --git a/predict_on_directory.py b/predict_on_directory.py index 3817056..4defea6 100644 --- a/predict_on_directory.py +++ b/predict_on_directory.py @@ -1,41 +1,35 @@ +######################################################################################################################## +# Model +######################################################################################################################## + import numpy as np from classifiers import * from pipeline import * import os - -from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array - -# 1 - Load the model and its pretrained weights -classifier = Meso4() -classifier.load('weights/Meso4_DF') - -# 2 - Minimial image generator -# We did use it to read and compute the prediction by batchs on test videos -# but do as you please, the models were trained on 256x256 images in [0,1]^(n*n) - -required_size = (256, 256) -# dataGenerator = ImageDataGenerator(rescale=1./255) -# generator = dataGenerator.flow_from_directory( -# 'test_images', -# target_size=(256, 256), -# batch_size=1, -# shuffle=False, -# class_mode='binary', -# subset='training') - -# 3 - Predict - -for X, y in generator: - print('Predicted :', classifier.predict(X), '\nReal class :', y) - num_iterations += 1 - if num_iterations >= 4: - break - -# 4 - Prediction for a video dataset - -# classifier.load('weights/Meso4_F2F') -# -# predictions = compute_accuracy(classifier, 'test_videos') -# for video_name in predictions: -# print('`{}` video class prediction :'.format(video_name), predictions[video_name][0]) \ No newline at end of file +import glob +import sys +from tensorflow.keras.preprocessing.image import load_img, img_to_array + +REQUIRED_SIZE = (256, 256) + +if __name__ == "__main__": + images_dir = sys.argv[1] + + if not os.path.isdir(images_dir): + print("## Directory provided {} doesn't exist.".format(images_dir)) + exit() + + # 1 - Load the model and its pretrained weights + classifier = Meso4() + classifier.load('weights/Meso4_DF') + + # Getting files + files = glob.glob(os.path.join(images_dir, "*.jpg")) + for f in files: + im = load_img(f, target_size=REQUIRED_SIZE) + im_arr = np.expand_dims(img_to_array(im), axis=0) + im_arr /= 255.0 + print(im_arr.shape) + pred = classifier.predict(im_arr) + print("## Image {} is classified as {}".format(f, pred)) \ No newline at end of file