Created
July 2, 2021 20:11
-
-
Save rickkk856/c4951bfbcb1b5fa31bba6a79e2f04b9d to your computer and use it in GitHub Desktop.
Bulk Predict and Move Files - Keras Teacheable Machine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow.keras | |
from tensorflow.keras.preprocessing.image import img_to_array | |
from tensorflow.keras.models import load_model | |
from keras.preprocessing import image | |
import numpy as np | |
import shutil | |
import os | |
## Set up Parameters | |
# Image folder to predict | |
folder_path = r'C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/test' | |
# Path to model | |
model_path = r'C:/Users/Pichau/Downloads/converted_keras/keras_model.h5' | |
# Dimensions of images | |
img_width, img_height = 224, 224 | |
## Change where each class will be moved to | |
Class_Folder_00 = r'C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room' | |
Class_Folder_01 = r'C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/8_Room' | |
Class_Folder_02 = r'C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/6_Room' | |
# load the trained model | |
model = load_model(model_path, compile = False) | |
model.compile(loss='binary_crossentropy', | |
optimizer='rmsprop', | |
metrics=['accuracy']) | |
print("Model Loaded") | |
## PREDICT IMAGES AND MOVE FILES | |
images = [] | |
for img in os.listdir(folder_path): | |
file_name = img | |
file_path = os.path.join(folder_path, img) | |
img = os.path.join(folder_path, img) | |
img = image.load_img(img, target_size=(img_width, img_height)) | |
img = image.img_to_array(img) | |
img = np.expand_dims(img, axis=0) | |
images.append(img) | |
prediction = model.predict_classes(img) | |
#print("Predicted to class ==", prediction) | |
if prediction == int("0"): | |
filename0 = os.path.join(Class_Folder_00, file_name) | |
dest = shutil.move(file_path, filename0) | |
print("Predicted to class = 0 and moved to",filename0) | |
if prediction == int("1"): | |
filename1 = os.path.join(Class_Folder_01, file_name) | |
dest = shutil.move(file_path, filename1) | |
print("Predicted to class = 1 and moved to",filename1) | |
if prediction == int("2"): | |
filename2 = os.path.join(Class_Folder_02, file_name) | |
dest = shutil.move(file_path, filename2) | |
print("Predicted to class = 2 and moved to",filename2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment