Skip to content

Commit

Permalink
Update inference files
Browse files Browse the repository at this point in the history
  • Loading branch information
indrapaul824 committed Mar 29, 2022
1 parent e283f47 commit 88bb805
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 14 deletions.
3 changes: 2 additions & 1 deletion requirements/req.in
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ lxml
cython
tensorflowjs
imutils
opencv-contrib-python
opencv-contrib-python
termcolor
4 changes: 3 additions & 1 deletion requirements/req.txt
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ tensorflow-io-gcs-filesystem==0.24.0
tensorflowjs==3.15.0
# via -r requirements/req.in
termcolor==1.1.0
# via tensorflow
# via
# -r requirements/req.in
# tensorflow
tf-estimator-nightly==2.8.0.dev2021122109
# via tensorflow
threadpoolctl==3.1.0
Expand Down
121 changes: 121 additions & 0 deletions vortex/hybridPred.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# import the necessary packages
from tracemalloc import start
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.models import load_model
import numpy as np
import matplotlib.pyplot as plt
import mimetypes
import argparse
import imutils
import cv2
import os

os.environ["CUDA_VISIBLE_DEVICES"] = "-1"

# font
font = cv2.FONT_HERSHEY_SIMPLEX
# Blue color in BGR
color = (0,0,255)
# Line thickness of 2 px
thickness = 3
# org
org = (50, 50)
# fontScale
fontScale = 1

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--input", required=True,
help="path to input image/text file of image filenames")
args = vars(ap.parse_args())

def get_key(my_dict, val):
for key, value in my_dict.items():
if val == value:
return key

return "Key doesn't exist"

# determine the input file type, but assume that we're working with
# single input image
filetype = mimetypes.guess_type(args["input"])[0]
imagePaths = [args["input"]]
# if the file type is a text file, then we need to process *multiple*
# images
if "text/plain" == filetype:
# load the filenames in our testing file and initialize our list
# of image paths
filenames = open(args["input"]).read().strip().split("\n")
imagePaths = []
# loop over the filenames
for f in filenames:
# construct the full path to the image filename and then
# update our image paths list
p = os.path.sep.join(["../data/processed/Obj_Det/images", f])
imagePaths.append(p)

# load our trained bounding box regressor from disk
print("[INFO] loading object detector...")
model = load_model("./artifacts/border_box/detector.h5")
print("[INFO] loaded object detector...")

print("-"*50)

print("[INFO] loading intensity estimator model...")
in_model = load_model('./artifacts/in_estimator/in_estimator.h5')
print("[INFO] loaded intensity estimator model...")

# loop over the images that we'll be testing using our bounding box
# regression model
p = 1
for imagePath in imagePaths:
# load the input image (in Keras format) from disk and preprocess
# it, scaling the pixel intensities to the range [0, 1]
image = load_img(imagePath, target_size=(224, 224))
image = img_to_array(image) / 255.0
image = np.expand_dims(image, axis=0)
# make bounding box predictions on the input image
preds = model.predict(image)[0]
(startX, startY, endX, endY) = preds
# load the input image (in OpenCV format), resize it such that it
# fits on our screen, and grab its dimensions
image = cv2.imread(imagePath)
image = imutils.resize(image, width=600)
(h, w) = image.shape[:2]
# scale the predicted bounding box coordinates based on the image
# dimensions
startX = int(startX * w)
startY = int(startY * h)
endX = int(endX * w)
endY = int(endY * h)

#crop the roi
roi = image[startY:endY,startX:endX]
roi = cv2.resize(roi,(100,100))
roi = img_to_array(roi) / 255.0
roi = np.expand_dims(roi, axis=0)
#pred intensity
classes = {'EXTREME':0, 'NORMAL':1, 'SEVERE':2, 'SUPER':3, 'VERY':4}
Y_pred = in_model.predict(roi)
Y_pred_classes = np.argmax(Y_pred,axis = 1)

# draw the predicted bounding box on the image
cv2.rectangle(image, (startX, startY), (endX, endY),(0, 255, 0), 2)
#print(imagePath)
txt = str(get_key(classes, Y_pred_classes[0]))
image2 = cv2.putText(image.copy(),txt, (startX-2,startY-4), font,fontScale, color, thickness, cv2.LINE_AA)

print("The predicted class for the given image is: ", get_key(classes, Y_pred_classes[0]))
while True:
cv2.imshow("Detection",image)
cv2.imshow("Prediction",image2)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
# cv2.imwrite('./result2/'+str(p)+'.jpg',image)
# p = p+1
# print("pred image saved")



73 changes: 73 additions & 0 deletions vortex/hybridPredict.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Hybrid Prediction - Cyclone Detection and Intensity Estimation"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[INFO] loading object detector...\n",
"2022-03-29 11:59:21.081233: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected\n",
"2022-03-29 11:59:21.081325: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: indra-pop-os\n",
"2022-03-29 11:59:21.081351: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: indra-pop-os\n",
"2022-03-29 11:59:21.081544: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:200] libcuda reported version is: 510.54.0\n",
"2022-03-29 11:59:21.081621: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:204] kernel reported version is: 510.54.0\n",
"2022-03-29 11:59:21.081646: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:310] kernel version seems to match DSO: 510.54.0\n",
"2022-03-29 11:59:21.082183: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA\n",
"To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n",
"[INFO] loaded object detector...\n",
"--------------------------------------------------\n",
"[INFO] loading intensity estimator model...\n",
"[INFO] loaded intensity estimator model...\n",
"The predicted class for the given image is: NORMAL\n"
]
}
],
"source": [
"!python hybridPred.py --input ../data/processed/threshZero_data/NORMAL/3DIMG_01DEC2020_0430_L1B_STD_IR2.jpg"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"interpreter": {
"hash": "92f8549edb95926e67d9af130c51c844e0526e8912c849c6708914c947287dc8"
},
"kernelspec": {
"display_name": "Python 3.9.7 ('vortex3.9')",
"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.9.11"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
18 changes: 9 additions & 9 deletions vortex/notebooks/Intensity Classifier .ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2767,23 +2767,23 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 27,
"id": "198823a4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2022-03-29 11:05:39.167162: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected\n",
"2022-03-29 11:05:39.167194: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: indra-pop-os\n",
"2022-03-29 11:05:39.167201: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: indra-pop-os\n",
"2022-03-29 11:05:39.167311: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:200] libcuda reported version is: 510.54.0\n",
"2022-03-29 11:05:39.167355: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:204] kernel reported version is: 510.54.0\n",
"2022-03-29 11:05:39.167364: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:310] kernel version seems to match DSO: 510.54.0\n",
"2022-03-29 11:05:39.167639: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA\n",
"2022-03-29 11:14:41.297217: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected\n",
"2022-03-29 11:14:41.297259: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: indra-pop-os\n",
"2022-03-29 11:14:41.297270: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: indra-pop-os\n",
"2022-03-29 11:14:41.297368: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:200] libcuda reported version is: 510.54.0\n",
"2022-03-29 11:14:41.297403: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:204] kernel reported version is: 510.54.0\n",
"2022-03-29 11:14:41.297413: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:310] kernel version seems to match DSO: 510.54.0\n",
"2022-03-29 11:14:41.297702: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA\n",
"To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n",
"The predicted class for the given image is: NORMAL\n"
"\u001b[32mThe predicted class for the given image is: \u001b[0m \u001b[31mNORMAL\u001b[0m\n"
]
}
],
Expand Down
Empty file added vortex/notebooks/__init__.py
Empty file.
Binary file added vortex/notebooks/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
7 changes: 4 additions & 3 deletions vortex/notebooks/in_estimator/predict.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import tensorflow as tf
from tensorflow.keras.preprocessing.image import img_to_array, load_img
from tensorflow.keras.models import load_model
from keras.preprocessing.image import img_to_array, load_img
from keras.models import load_model
import numpy as np
import mimetypes
import argparse
from termcolor import colored
import os

os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
Expand Down Expand Up @@ -55,4 +56,4 @@ def get_key(my_dict, val):
# Convert predictions classes to one hot vectors
Y_pred_classes = np.argmax(Y_pred,axis = 1)

print("The predicted class for the given image is: ", get_key(classes, Y_pred_classes[0]))
print(colored("The predicted class for the given image is: ", "green"), colored(get_key(classes, Y_pred_classes[0]), "red"))

0 comments on commit 88bb805

Please sign in to comment.