Skip to content

Commit

Permalink
prediction pipeline created
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyIITR committed Jan 4, 2024
1 parent b28840c commit 4861ab7
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 2 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ scipy
Flask
Flask-Cors
google-cloud-storage
requests
-e .
239 changes: 239 additions & 0 deletions research/trails.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "21f6b22f-6f08-40a8-a161-636847421958",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from PIL import Image"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "6bd4cd50-08c4-4603-a8a3-0eb9ac435666",
"metadata": {},
"outputs": [],
"source": [
"os.chdir(\"../\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "c89c80e8-4c2c-4cb0-8146-90e25f1b65d1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'/home/lucky/Documents/projects/MyProject/smoke_detection'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pwd"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "3bd102a3-ce32-4e62-8812-1ba6ff2f978f",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2024-01-04 22:17:37.107698: I external/local_tsl/tsl/cuda/cudart_stub.cc:31] Could not find cuda drivers on your machine, GPU will not be used.\n",
"2024-01-04 22:17:37.144805: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered\n",
"2024-01-04 22:17:37.144834: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered\n",
"2024-01-04 22:17:37.145880: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n",
"2024-01-04 22:17:37.151404: I external/local_tsl/tsl/cuda/cudart_stub.cc:31] Could not find cuda drivers on your machine, GPU will not be used.\n",
"2024-01-04 22:17:37.151900: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n",
"To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n",
"2024-01-04 22:17:37.915131: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n"
]
}
],
"source": [
"from cnnClassifier.pipeline.predict import PredictionPipeline"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "27f695ee-b870-40f4-a8f8-7f9f38d0497f",
"metadata": {},
"outputs": [],
"source": [
"url = \"https://img.freepik.com/free-photo/young-man-smoking_144627-29295.jpg\""
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "6e4ede48-d531-4b0e-83ff-e74cf308c39e",
"metadata": {},
"outputs": [],
"source": [
"import requests"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "3e1e62d6-3bee-485d-bae3-9756159c78a9",
"metadata": {},
"outputs": [],
"source": [
"response = requests.get(url)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "c661cd06-1a88-40c0-99b1-7f1505879269",
"metadata": {},
"outputs": [],
"source": [
"image_bytes = response.content"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "141ceb01-2d89-48c9-b7ab-00e74e4f712b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2024-01-04 22:17:39,290: INFO: common: yaml file: config/config.yaml loaded successfully]\n"
]
}
],
"source": [
"pp = PredictionPipeline()"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "ee4cbc61-2b25-451d-8a00-2cf2c1168ebc",
"metadata": {},
"outputs": [],
"source": [
"image_bytes = pp.download_image(url2)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "e28cd59d-0637-41f0-9476-479563d2f037",
"metadata": {},
"outputs": [],
"source": [
"img = pp.preprocess_image(image_bytes)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "fa2ae359-1aaa-4158-b822-180f1a1ecdeb",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1, 224, 224, 3)"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"img.shape"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "7d135df6-7b7b-46ff-aca5-e71343a712ce",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Smoking with probability 94.80263590812683\n",
"Non-Smoking with probability 3.4822791814804077\n"
]
},
{
"data": {
"text/plain": [
"{'SmokingProbability': 94.80263590812683,\n",
" 'NonSmokingProbability': 3.4822791814804077}"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pp.predict(img)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "56ae954a-a727-4b35-a99c-1075da6aae31",
"metadata": {},
"outputs": [],
"source": [
"url2 = \"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQZ2FnaBLHoNCw4OM00db5ahJdvs_LXEo45OQ&usqp=CAU\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "38599440-8ddc-4666-b697-2598d34c64f5",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Empty file removed research/trials.ipynb
Empty file.
4 changes: 2 additions & 2 deletions scores.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"loss": 0.12362710386514664,
"accuracy": 0.9508928656578064
"loss": 0.12056513875722885,
"accuracy": 0.9553571343421936
}
43 changes: 43 additions & 0 deletions src/cnnClassifier/pipeline/predict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from PIL import Image
from tensorflow.keras.models import load_model
import numpy as np
from cnnClassifier.config.configuration import ConfigurationManager
from io import BytesIO
import requests
from cnnClassifier.constants import CONFIG_FILE_PATH
from cnnClassifier.utils.common import read_yaml


class PredictionPipeline:
def __init__(self):
self.config = read_yaml(CONFIG_FILE_PATH)
# load model
self.model = load_model(self.config.training.trained_model_path)
# self.params = read_yaml(PARAMS_FILE_PATH)

@staticmethod
def preprocess_image(image_bytes, img_shape=224):
"""Will take the image bytes and return the processed image"""
image_data = BytesIO(image_bytes)
image = Image.open(image_data)
image = image.convert("RGB")
image = image.resize((img_shape, img_shape))
image_array = np.array(image)
img = np.expand_dims(image_array, axis=0)
return img

@staticmethod
def download_image(url):
""" will return image binary data"""
response = requests.get(url)
image_bytes = response.content
return image_bytes

def predict(self, img):
prediction = self.model.predict(img, verbose=0)
# predicted_label = np.argmax(prediction)
print(f"Smoking with probability {prediction[0][1] * 100}")
print(f"Non-Smoking with probability {prediction[0][0] * 100}")

return {"SmokingProbability": prediction[0][1] * 100,
"NonSmokingProbability": prediction[0][0]*100}

0 comments on commit 4861ab7

Please sign in to comment.