Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rickkk856/c4951bfbcb1b5fa31bba6a79e2f04b9d to your computer and use it in GitHub Desktop.
Save rickkk856/c4951bfbcb1b5fa31bba6a79e2f04b9d to your computer and use it in GitHub Desktop.
Bulk Predict and Move Files - Keras Teacheable Machine
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"C:\\Users\\Pichau\\RPLAN-Toolbox\\output_80K_B_ORGANIZED\n"
]
}
],
"source": [
"%cd \"C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/\"\n",
"#https://github.com/googlecreativelab/teachablemachine-community/blob/master/snippets/markdown/image/tensorflow/keras.md"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Set up Parameters"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model Loaded\n"
]
}
],
"source": [
"import tensorflow.keras\n",
"from tensorflow.keras.preprocessing.image import img_to_array\n",
"from tensorflow.keras.models import load_model\n",
"from keras.preprocessing import image\n",
"import numpy as np\n",
"import shutil\n",
"import os\n",
"\n",
"\n",
"# Image folder to predict\n",
"folder_path = r'C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/test'\n",
"# Path to model\n",
"model_path = r'C:/Users/Pichau/Downloads/converted_keras/keras_model.h5'\n",
"# Dimensions of images\n",
"img_width, img_height = 224, 224\n",
"\n",
"\n",
"## Change where each class will be moved to\n",
"Class_Folder_00 = r'C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room'\n",
"Class_Folder_01 = r'C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/8_Room'\n",
"Class_Folder_02 = r'C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/6_Room'\n",
"\n",
"# load the trained model\n",
"model = load_model(model_path, compile = False)\n",
"model.compile(loss='binary_crossentropy',\n",
" optimizer='rmsprop',\n",
" metrics=['accuracy'])\n",
"print(\"Model Loaded\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Check if the files that will be predicted are corret"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"FILENAMES: ['0.png', '1.png', '10.png', '12.png', '13.png', '14.png', '15.png', '17.png', '18.png', '19.png', '2.png', '20.png', '21.png', '23.png', '24.png', '25.png', '26.png', '27.png', '28.png', '29.png', '3.png', '30.png', '31.png', '4.png', '5.png', '6.png', '7.png', '7_Room0.png', '7_Room1.png', '7_Room2.png', '7_Room4.png', '7_Room5.png', '7_Room6.png', '8.png', '8_Room13.png', '8_Room3.png', '8_Room7.png', '8_Room8.png', '8_Room8_Room3.png', '9.png']\n"
]
}
],
"source": [
"# List all Images to predict\n",
"file_names = []\n",
"for img in os.listdir(folder_path):\n",
" file_path = os.path.join(folder_path, img)\n",
" file_names.append(img)\n",
"\n",
"print(\"FILENAMES: \", file_names)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Predict classes and move files"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\0.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\1.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\10.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\12.png\n",
"Predicted to class = 1 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/8_Room\\13.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\14.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\15.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\17.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\18.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\19.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\2.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\20.png\n",
"Predicted to class = 1 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/8_Room\\21.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\23.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\24.png\n",
"Predicted to class = 1 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/8_Room\\25.png\n",
"Predicted to class = 1 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/8_Room\\26.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\27.png\n",
"Predicted to class = 1 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/8_Room\\28.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\29.png\n",
"Predicted to class = 1 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/8_Room\\3.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\30.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\31.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\4.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\5.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\6.png\n",
"Predicted to class = 1 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/8_Room\\7.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\7_Room0.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\7_Room1.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\7_Room2.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\7_Room4.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\7_Room5.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\7_Room6.png\n",
"Predicted to class = 1 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/8_Room\\8.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\8_Room13.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\8_Room3.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\8_Room7.png\n",
"Predicted to class = 1 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/8_Room\\8_Room8.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\8_Room8_Room3.png\n",
"Predicted to class = 0 and moved to C:/Users/Pichau/RPLAN-Toolbox/output_80K_B_ORGANIZED/7_Room\\9.png\n"
]
}
],
"source": [
"images = []\n",
"for img in os.listdir(folder_path):\n",
" file_name = img\n",
" file_path = os.path.join(folder_path, img)\n",
" \n",
" img = os.path.join(folder_path, img)\n",
" img = image.load_img(img, target_size=(img_width, img_height))\n",
" img = image.img_to_array(img)\n",
" img = np.expand_dims(img, axis=0)\n",
" images.append(img)\n",
" prediction = model.predict_classes(img)\n",
" #print(\"Predicted to class ==\", prediction)\n",
" \n",
" if prediction == int(\"0\"):\n",
" filename0 = os.path.join(Class_Folder_00, file_name)\n",
" dest = shutil.move(file_path, filename0)\n",
" print(\"Predicted to class = 0 and moved to\",filename0)\n",
" \n",
" if prediction == int(\"1\"):\n",
" filename1 = os.path.join(Class_Folder_01, file_name)\n",
" dest = shutil.move(file_path, filename1)\n",
" print(\"Predicted to class = 1 and moved to\",filename1)\n",
" \n",
" if prediction == int(\"2\"):\n",
" filename2 = os.path.join(Class_Folder_02, file_name)\n",
" dest = shutil.move(file_path, filename2)\n",
" print(\"Predicted to class = 2 and moved to\",filename2)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
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