taltaf9133 commited on
Commit
26ed034
·
1 Parent(s): 9123d17

adding predict file

Browse files
Files changed (1) hide show
  1. predict.py +74 -0
predict.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from os import listdir
2
+ import sys
3
+ from pathlib import Path
4
+
5
+ import numpy as np
6
+ sys.path.append(str(Path(__file__).resolve().parent.parent))
7
+
8
+ from typing import Union
9
+ import pandas as pd
10
+ import tensorflow as tf
11
+
12
+ from plant_leave_diseases_model import __version__ as _version
13
+ from plant_leave_diseases_model.config.core import TRAINED_MODEL_DIR, config
14
+ from plant_leave_diseases_model.processing.data_manager import convert_image_to_array, get_class_file_list, get_model_file_name_path, load_leaf_disease_dataset, load_model, load_test_dataset
15
+ from plant_leave_diseases_model.processing.data_setup import test_directory,class_file_path
16
+
17
+
18
+ def make_prediction(*, test_dir_img_file_path) -> dict:
19
+ """Make a prediction using a saved model """
20
+ image_list=[]
21
+
22
+ #x_test, y_test = load_leaf_disease_dataset(test_dir_img_file_path)
23
+ root_dir = listdir(test_dir_img_file_path)
24
+ for plant_image in root_dir :
25
+ print("plant_image::",plant_image)
26
+ img_file_path=str(test_dir_img_file_path)+"/"+str(plant_image)
27
+ if plant_image.endswith(".jpg") == True or plant_image.endswith(".JPG") == True:
28
+ image_list.append(convert_image_to_array(img_file_path))
29
+
30
+ np_image_list = np.array(image_list, dtype=np.float16) / config.model_config.scaling_factor
31
+
32
+ print("np_image_list.shape:",np_image_list.shape)
33
+ results = {"predictions": None, "version": _version}
34
+
35
+ load_model_and_predict(np_image_list)
36
+ pred_labels = model_file_name = get_model_file_name_path()
37
+
38
+ results = {"predictions": pred_labels, "version": _version}
39
+
40
+
41
+ return results
42
+
43
+ def load_model_and_predict(x_test):
44
+ model_file_name = get_model_file_name_path()
45
+
46
+ print("loading mode file:",model_file_name)
47
+
48
+ model = load_model(file_name = model_file_name)
49
+
50
+ predictions = model.predict(x_test,verbose = 0)
51
+
52
+ ###########################################
53
+ # Geting array of master class array
54
+ ###########################################
55
+ master_class_arr=get_class_file_list(class_file_path)
56
+ print("master_class_arr:",master_class_arr)
57
+
58
+ print(predictions)
59
+ pred_labels = []
60
+ for pred in predictions:
61
+ np.multiply(np.array(pred), 100000)
62
+ max_index = np.multiply(np.array(pred), 100000).argmax()
63
+ leaf_pred_label=master_class_arr[max_index]
64
+ print("load_model_and_predict:max_index::",max_index,"master_class_arr:",master_class_arr[max_index])
65
+ pred_labels.append(leaf_pred_label)
66
+
67
+ return pred_labels
68
+
69
+ if __name__ == "__main__":
70
+
71
+ # Define directory where test images are loaded
72
+ test_dir_img_file_path=test_directory+"/Apple___Apple_scab"
73
+ print("test_dir_img_file_path::",test_dir_img_file_path)
74
+ make_prediction(test_dir_img_file_path = test_dir_img_file_path)