jentz2909 commited on
Commit
733d9e3
·
1 Parent(s): 184f43a

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +22 -32
  2. 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 transformers import TFAutoModelForImageClassification
6
- import torch
7
 
8
- path = [['0229_01.jpg'], ['0385_01.jpg'], ['0067_01.jpg']]
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, fuzzy=True)
13
 
14
- with open(output_path, 'r') as file:
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) / 255. # All images will be rescaled by 1./255
22
- img = tf.image.resize(img, (IMG_SIZE, IMG_SIZE), method='bilinear')
23
- return img
24
 
25
- # Load your Hugging Face model
26
- model = TFAutoModelForImageClassification.from_pretrained("final_model.h5")
27
 
28
  def predict_fn(img):
29
- img = img.convert('RGB')
30
- img_data = _normalize_img(img)
31
- x = np.array(img_data)
32
- x = np.expand_dims(x, axis=0)
33
-
34
- # Convert the input to a torch tensor
35
- inputs = torch.tensor(x)
36
-
37
- # Perform inference using the Hugging Face model
38
- with torch.no_grad():
39
- outputs = model(inputs)
40
-
41
- # Get the predicted labels and their scores
42
- predicted_labels = torch.argsort(outputs.logits[0], descending=True)[:3]
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 ------------------------------------