imrkash commited on
Commit
68c241e
·
verified ·
1 Parent(s): 09738e0

Upload efficientNet-algo/predict.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. efficientNet-algo/predict.py +64 -0
efficientNet-algo/predict.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Id=[]
2
+ import numpy as np # linear algebra
3
+ import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
4
+ import tensorflow as tf
5
+ import matplotlib.pyplot as plt
6
+ from PIL import Image
7
+ import os
8
+ IMAGE_SIZE=256
9
+ BATCH_SIZE=32
10
+ CHANNELS=3
11
+ PATH_NAME='../uploads'
12
+
13
+ classes = [ "cocci" , "healthy" , "ncd" , "salmo" , ]
14
+
15
+ def load_test(path_name):
16
+
17
+ for dirname, _, filenames in os.walk(path_name):
18
+ for filename in filenames:
19
+ Id.append(os.path.join(dirname, filename))
20
+ Id[:5]
21
+
22
+ test=pd.DataFrame()
23
+ test=test.assign(filename=Id)
24
+ test.head()
25
+ return test
26
+ def predict(path_name = '../uploads'):
27
+ PATH_NAME=path_name
28
+ # Assuming model, classes, and test are already defined
29
+ result = []
30
+ test= load_test(path_name)
31
+ # Number of rows and columns in the grid
32
+ num_cols = 3
33
+ num_rows = (len(test.filename) + num_cols - 1) // num_cols
34
+
35
+ # Create a figure with the appropriate size
36
+ plt.figure(figsize=(15, 5 * num_rows))
37
+
38
+ for index, i in enumerate(test.filename):
39
+ img = Image.open(i).convert('RGB')
40
+ img = img.resize((300, 300 * img.size[1] // img.size[0]), Image.Resampling.LANCZOS)
41
+ inp_numpy = np.array(img)[None]
42
+ inp = tf.constant(inp_numpy, dtype='float32')
43
+
44
+ # Get model predictions
45
+ model = tf.saved_model.load('./EfficientNet-model')
46
+ class_scores = model(inp)[0].numpy()
47
+ predicted_class_index = class_scores.argmax()
48
+
49
+ predicted_class = classes[predicted_class_index]
50
+ confidence = class_scores[predicted_class_index]
51
+
52
+ # Append result
53
+ # result.append(predicted_class)
54
+ result.append({"label":predicted_class, "accuracy":confidence})
55
+
56
+ # Plotting the image in the grid
57
+ # plt.subplot(num_rows, num_cols, index + 1)
58
+ # plt.imshow(img)
59
+ # plt.title(f"{predicted_class}\nConfidence: {confidence:.2f}\nFilename: {i}")
60
+ # plt.axis('off')
61
+ return result
62
+ # # Show the grid
63
+ # plt.tight_layout()
64
+ # plt.show()