Upload 2 files
Browse files- app.py +22 -32
- requirements.txt +0 -1
app.py
CHANGED
|
@@ -2,49 +2,39 @@ import gradio as gr
|
|
| 2 |
import tensorflow as tf
|
| 3 |
import gdown
|
| 4 |
import numpy as np
|
| 5 |
-
from
|
| 6 |
-
import torch
|
| 7 |
|
| 8 |
-
path
|
| 9 |
|
| 10 |
URL = 'https://drive.google.com/file/d/1TpQmvq2R8nHy9CQaVb0jtMMuRQA0Cw0L/view?usp=drive_link'
|
| 11 |
output_path = 'Labels_train_abd.txt'
|
| 12 |
-
gdown.download(URL, output_path, quiet=False,
|
| 13 |
|
| 14 |
-
with open(output_path,
|
| 15 |
LABELS = [x.strip() for x in file.readlines()]
|
| 16 |
|
| 17 |
num_classes = 4000
|
| 18 |
IMG_SIZE = 128
|
| 19 |
|
| 20 |
def _normalize_img(img):
|
| 21 |
-
img = tf.cast(img, tf.float32)
|
| 22 |
-
img = tf.image.resize(img,
|
| 23 |
-
return img
|
| 24 |
|
| 25 |
-
|
| 26 |
-
model = TFAutoModelForImageClassification.from_pretrained("final_model.h5")
|
| 27 |
|
| 28 |
def predict_fn(img):
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
top3_scores = outputs.logits[0][predicted_labels]
|
| 44 |
-
|
| 45 |
-
# Map the labels to their corresponding names
|
| 46 |
-
top3_labels = [LABELS[i] for i in predicted_labels]
|
| 47 |
-
|
| 48 |
-
return {label: str(score) for label, score in zip(top3_labels, top3_scores)}
|
| 49 |
-
|
| 50 |
-
gr.Interface(predict_fn, gr.inputs.Image(type='pil'), outputs="label", examples=path).launch()
|
|
|
|
| 2 |
import tensorflow as tf
|
| 3 |
import gdown
|
| 4 |
import numpy as np
|
| 5 |
+
from keras.models import load_model
|
|
|
|
| 6 |
|
| 7 |
+
path = [['0229_01.jpg'], ['0385_01.jpg'], ['0067_01.jpg']]
|
| 8 |
|
| 9 |
URL = 'https://drive.google.com/file/d/1TpQmvq2R8nHy9CQaVb0jtMMuRQA0Cw0L/view?usp=drive_link'
|
| 10 |
output_path = 'Labels_train_abd.txt'
|
| 11 |
+
gdown.download(URL, output_path, quiet=False,fuzzy=True)
|
| 12 |
|
| 13 |
+
with open(output_path,'r') as file:
|
| 14 |
LABELS = [x.strip() for x in file.readlines()]
|
| 15 |
|
| 16 |
num_classes = 4000
|
| 17 |
IMG_SIZE = 128
|
| 18 |
|
| 19 |
def _normalize_img(img):
|
| 20 |
+
img = tf.cast(img, tf.float32)/255. # All images will be rescaled by 1./255
|
| 21 |
+
img = tf.image.resize(img, (IMG_SIZE, IMG_SIZE), method= 'bilinear')
|
| 22 |
+
return (img)
|
| 23 |
|
| 24 |
+
model = load_model("final_model.h5")
|
|
|
|
| 25 |
|
| 26 |
def predict_fn(img):
|
| 27 |
+
img = img.convert('RGB')
|
| 28 |
+
img_data = _normalize_img(img)
|
| 29 |
+
x = np.array(img_data)
|
| 30 |
+
x = np.expand_dims(x, axis=0)
|
| 31 |
+
temp = model.predict(x)
|
| 32 |
+
|
| 33 |
+
idx = np.argsort(np.squeeze(temp))[::-1]
|
| 34 |
+
top3_value = np.asarray([temp[0][i] for i in idx[0:3]])
|
| 35 |
+
top3_idx = idx[0:3]
|
| 36 |
+
|
| 37 |
+
return {LABELS[i]:str(v) for i,v in zip(top3_idx,top3_value)}
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
gr.Interface(predict_fn, gr.inputs.Image(type='pil'), outputs='label', examples=path,).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -9,7 +9,6 @@ scipy>=1.4.1
|
|
| 9 |
gradio>=3.36.1
|
| 10 |
tensorflow==2.12.0
|
| 11 |
tensorflow-datasets==4.9.2
|
| 12 |
-
transformers==4.12.0
|
| 13 |
|
| 14 |
|
| 15 |
# Plotting ------------------------------------
|
|
|
|
| 9 |
gradio>=3.36.1
|
| 10 |
tensorflow==2.12.0
|
| 11 |
tensorflow-datasets==4.9.2
|
|
|
|
| 12 |
|
| 13 |
|
| 14 |
# Plotting ------------------------------------
|