-
Notifications
You must be signed in to change notification settings - Fork 5
/
project3-grader.py
198 lines (150 loc) · 4.58 KB
/
project3-grader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import os
import requests
import tensorflow as tf
base_url = "http://172.17.0.1:5000"
# UPDATE: Config ----
# george
# path = "/models/damage/v1"
# path = "/models/hurricane_damage/v1"
# path = "/models/alt_L5/info"
# path = "/models/alt_L5/process"
# path = "/models/hurricane/v1"
# path = "/models/alt_lenet5"
# path = "/model/info"
# path = "/model/predict"
# path = "/info"
# path = "/predict"
# path = "/models"
# path = "/model_summary"
# path = "/classify_image"
# path = "/model/info"
# path = "/model/predict"
path = "/models/best"
# -----------
# final URL
url = f"{base_url}{path}"
# GET ----
def make_get_request():
rsp = requests.get(url)
print(f"Status code: {rsp.status_code}")
# UPDATE:
print(rsp.content)
# print(f"JSON: {rsp.json()}")
# POST -----
def get_paths():
damage_paths = os.listdir("/data/damage")
damage_paths = [f"/data/damage/{p}" for p in damage_paths]
no_damage_paths = os.listdir("/data/no_damage")
no_damage_paths = [f"/data/no_damage/{p}" for p in no_damage_paths]
return damage_paths, no_damage_paths
def do_custom_processing(path):
# from tensorflow.keras.layers.experimental.preprocessing import Rescaling
# img = tf.keras.utils.load_img(
# path,
# color_mode='rgb',
# target_size=(128,128),
# interpolation='nearest',
# keep_aspect_ratio=True
# )
# rescale = Rescaling(scale=1.0/255)
# img_res = rescale(img)
# tens = tf.convert_to_tensor(img_res)
# inp = tf.expand_dims(tens, 0, name=None).numpy().tolist()
# return inp
from PIL import Image
import numpy as np
# img = Image.open(path)
# img = np.asarray(img)
# return img.tolist()
img = Image.open(path).resize((128, 128))
img_array = np.array(img) / 255.0
img_list = np.expand_dims(img_array, axis=0).tolist()
return img_list
# from matplotlib import image
# img = image.imread(path)
# return img.tolist()
def make_post_request(path):
# UPDATE: Choose one of these ----
# A) read the raw image file in
# image = open(path, 'rb').read()
# B) alternatively, do some custom processing
image = do_custom_processing(path)
# send JSON data
rsp = requests.post(url, json={"image": image})
# rsp = requests.post(url, json={"path": path})
# -----------
# send multipart
# data = {"image": image}
# rsp = requests.post(url, files=data)
# ------
# print(f"POST status code: {rsp.status_code}")
try:
rsp.raise_for_status()
except Exception as e:
print(f"Bad status code: {e}; Response: {rsp.content}")
return None
# UPDATE: this -----
result = rsp.json()
return result
def get_prediction(result, label):
# UPDATE
probs = result['result'][0]
if probs[0] < 0.5:
prediction = "damage"
else:
prediction = "no_damage"
# if probs[0] > probs[1]:
# prediction = "damage"
# else:
# prediction = "no_damage"
if prediction == label:
return 1
else:
return 0
# prediction = result["result"]
# prediction = result["prediction"]
# if "No Damage" in prediction:
# if label == "no_damage":
# return 1
# else:
# return 0
# elif label == "damage":
# return 1
# else:
# return 0
# if prediction == "contains no damage" and label == "no_damage":
# return 1
# elif prediction == "contains damage" and label == "damage":
# return 1
# else:
# return 0
def do_full_post_test():
print("Starting full POST test suite...")
total_correct = 0
total = 0
for p in damage_paths:
total += 1
result = make_post_request(p)
if result:
prediction = get_prediction(result, "damage")
total_correct = total_correct + prediction
for p in no_damage_paths:
total += 1
result = make_post_request(p)
if result:
prediction = get_prediction(result, "no_damage")
total_correct = total_correct + prediction
accuracy = float(total_correct)/float(total)
print("Final results:")
print(f"Total correct: {total_correct}")
print(f"Total: {total}")
print(f"Accuracy: {accuracy}")
print("\n\n\n**** STARTING GRADING ****\n")
make_get_request()
damage_paths, no_damage_paths = get_paths()
# quick test of the POST
print(make_post_request(no_damage_paths[1]))
# for i in range(len(no_damage_paths)):
# print(make_post_request(no_damage_paths[i]))
# print(make_post_request(damage_paths[2]))
do_full_post_test()