drab commited on
Commit
7bc0418
1 Parent(s): 134b360

Simple debug version

Browse files
Files changed (1) hide show
  1. pipeline.py +40 -225
pipeline.py CHANGED
@@ -18,63 +18,63 @@ class PreTrainedPipeline():
18
 
19
  def __call__(self, inputs: "Image.Image")-> List[Dict[str, Any]]:
20
 
21
- # convert img to numpy array, resize and normalize to make the prediction
22
- img = np.array(inputs)
23
 
24
- im = tf.image.resize(img, (128, 128))
25
- im = tf.cast(im, tf.float32) / 255.0
26
- pred_mask = self.model.predict(im[tf.newaxis, ...])
27
 
28
- # take the best performing class for each pixel
29
- # the output of argmax looks like this [[1, 2, 0], ...]
30
- pred_mask_arg = tf.argmax(pred_mask, axis=-1)
31
 
32
- labels = []
33
 
34
- # convert the prediction mask into binary masks for each class
35
- binary_masks = {}
36
- mask_codes = {}
37
 
38
- # when we take tf.argmax() over pred_mask, it becomes a tensor object
39
- # the shape becomes TensorShape object, looking like this TensorShape([128])
40
- # we need to take get shape, convert to list and take the best one
41
 
42
- rows = pred_mask_arg[0][1].get_shape().as_list()[0]
43
- cols = pred_mask_arg[0][2].get_shape().as_list()[0]
44
 
45
- for cls in range(pred_mask.shape[-1]):
46
 
47
- binary_masks[f"mask_{cls}"] = np.zeros(shape = (pred_mask.shape[1], pred_mask.shape[2])) #create masks for each class
48
 
49
- for row in range(rows):
50
 
51
- for col in range(cols):
52
 
53
- if pred_mask_arg[0][row][col] == cls:
54
 
55
- binary_masks[f"mask_{cls}"][row][col] = 1
56
- else:
57
- binary_masks[f"mask_{cls}"][row][col] = 0
58
 
59
- mask = binary_masks[f"mask_{cls}"]
60
- mask *= 255
61
- img = Image.fromarray(mask.astype(np.int8), mode="L")
62
 
63
- # we need to make it readable for the widget
64
- with io.BytesIO() as out:
65
- img.save(out, format="PNG")
66
- png_string = out.getvalue()
67
- mask = base64.b64encode(png_string).decode("utf-8")
68
 
69
- mask_codes[f"mask_{cls}"] = mask
70
 
71
 
72
- # widget needs the below format, for each class we return label and mask string
73
- labels.append({
74
- "label": f"LABEL_{cls}",
75
- "mask": mask_codes[f"mask_{cls}"],
76
- "score": 1.0,
77
- })
78
 
79
  labels = [{"score":0.9509243965148926,"label":"car","box":{"xmin":142,"ymin":106,"xmax":376,"ymax":229}},
80
  {"score":0.9981777667999268,"label":"car","box":{"xmin":405,"ymin":146,"xmax":640,"ymax":297}},
@@ -85,188 +85,3 @@ class PreTrainedPipeline():
85
  {"score":0.9996274709701538,"label":"skateboard","box":{"xmin":265,"ymin":348,"xmax":440,"ymax":413}}]
86
 
87
  return labels
88
-
89
- # class PreTrainedPipeline():
90
- # def __init__(self, path: str):
91
- # # load the model
92
- # self.model = tf.saved_model.load('./saved_model')
93
-
94
- # def __call__(self, inputs: "Image.Image")-> List[Dict[str, Any]]:
95
- # image = np.array(inputs)
96
- # image = tf.cast(image, tf.float32)
97
- # image = tf.image.resize(image, [150, 150])
98
- # image = np.expand_dims(image, axis = 0)
99
- # predictions = self.model.predict(image)
100
-
101
- # labels = []
102
- # labels = [{"score":0.9509243965148926,"label":"car","box":{"xmin":142,"ymin":106,"xmax":376,"ymax":229}},{"score":0.9981777667999268,"label":"car","box":{"xmin":405,"ymin":146,"xmax":640,"ymax":297}},{"score":0.9963648915290833,"label":"car","box":{"xmin":0,"ymin":115,"xmax":61,"ymax":167}},{"score":0.974663257598877,"label":"car","box":{"xmin":155,"ymin":104,"xmax":290,"ymax":141}},{"score":0.9986898303031921,"label":"car","box":{"xmin":39,"ymin":117,"xmax":169,"ymax":188}},{"score":0.9998276233673096,"label":"person","box":{"xmin":172,"ymin":60,"xmax":482,"ymax":396}},{"score":0.9996274709701538,"label":"skateboard","box":{"xmin":265,"ymin":348,"xmax":440,"ymax":413}}]
103
-
104
- # return labels
105
-
106
-
107
- # # -----------------
108
- # def load_model():
109
- # return tf.saved_model.load('./saved_model')
110
-
111
- # def load_label_map(label_map_path):
112
- # """
113
- # Reads label map in the format of .pbtxt and parse into dictionary
114
- # Args:
115
- # label_map_path: the file path to the label_map
116
- # Returns:
117
- # dictionary with the format of {label_index: {'id': label_index, 'name': label_name}}
118
- # """
119
- # label_map = {}
120
-
121
- # with open(label_map_path, "r") as label_file:
122
- # for line in label_file:
123
- # if "id" in line:
124
- # label_index = int(line.split(":")[-1])
125
- # label_name = next(label_file).split(":")[-1].strip().strip('"')
126
- # label_map[label_index] = {"id": label_index, "name": label_name}
127
- # return label_map
128
-
129
- # def predict_class(image, model):
130
- # image = tf.cast(image, tf.float32)
131
- # image = tf.image.resize(image, [150, 150])
132
- # image = np.expand_dims(image, axis = 0)
133
- # return model.predict(image)
134
-
135
- # def plot_boxes_on_img(color_map, classes, bboxes, image_origi, origi_shape):
136
- # for idx, each_bbox in enumerate(bboxes):
137
- # color = color_map[classes[idx]]
138
-
139
- # ## Draw bounding box
140
- # cv2.rectangle(
141
- # image_origi,
142
- # (int(each_bbox[1] * origi_shape[1]),
143
- # int(each_bbox[0] * origi_shape[0]),),
144
- # (int(each_bbox[3] * origi_shape[1]),
145
- # int(each_bbox[2] * origi_shape[0]),),
146
- # color,
147
- # 2,
148
- # )
149
- # ## Draw label background
150
- # cv2.rectangle(
151
- # image_origi,
152
- # (int(each_bbox[1] * origi_shape[1]),
153
- # int(each_bbox[2] * origi_shape[0]),),
154
- # (int(each_bbox[3] * origi_shape[1]),
155
- # int(each_bbox[2] * origi_shape[0] + 15),),
156
- # color,
157
- # -1,
158
- # )
159
- # ## Insert label class & score
160
- # cv2.putText(
161
- # image_origi,
162
- # "Class: {}, Score: {}".format(
163
- # str(category_index[classes[idx]]["name"]),
164
- # str(round(scores[idx], 2)),
165
- # ),
166
- # (int(each_bbox[1] * origi_shape[1]),
167
- # int(each_bbox[2] * origi_shape[0] + 10),),
168
- # cv2.FONT_HERSHEY_SIMPLEX,
169
- # 0.3,
170
- # (0, 0, 0),
171
- # 1,
172
- # cv2.LINE_AA,
173
- # )
174
- # return image_origi
175
-
176
-
177
- # # Webpage code starts here
178
-
179
- # #TODO change this
180
- # st.title('Distribution Grid - Belgium - Equipment detection')
181
- # st.text('made by LabelFlow')
182
- # st.markdown('## Description about your project')
183
-
184
- # with st.spinner('Model is being loaded...'):
185
- # model = load_model()
186
-
187
- # # ask user to upload an image
188
- # file = st.file_uploader("Upload image", type=["jpg", "png"])
189
-
190
- # if file is None:
191
- # st.text('Waiting for upload...')
192
- # else:
193
- # st.text('Running inference...')
194
- # # open image
195
- # test_image = Image.open(file).convert("RGB")
196
- # origi_shape = np.asarray(test_image).shape
197
- # # resize image to default shape
198
- # default_shape = 320
199
- # image_resized = np.array(test_image.resize((default_shape, default_shape)))
200
-
201
- # ## Load color map
202
- # category_index = load_label_map("./label_map.pbtxt")
203
-
204
- # # TODO Add more colors if there are more classes
205
- # # color of each label. check label_map.pbtxt to check the index for each class
206
- # color_map = {
207
- # 1: [69, 109, 42],
208
- # 2: [107, 46, 186],
209
- # 3: [9, 35, 183],
210
- # 4: [27, 1, 30],
211
- # 5: [0, 0, 0],
212
- # 6: [5, 6, 7],
213
- # 7: [11, 5, 12],
214
- # 8: [209, 205, 211],
215
- # 9: [17, 17, 17],
216
- # 10: [101, 242, 50],
217
- # 11: [51, 204, 170],
218
- # 12: [106, 0, 132],
219
- # 13: [7, 111, 153],
220
- # 14: [8, 10, 9],
221
- # 15: [234, 250, 252],
222
- # 16: [58, 68, 30],
223
- # 17: [24, 178, 117],
224
- # 18: [21, 22, 21],
225
- # 19: [53, 104, 83],
226
- # 20: [12, 5, 10],
227
- # 21: [223, 192, 249],
228
- # 22: [234, 234, 234],
229
- # 23: [119, 68, 221],
230
- # 24: [224, 174, 94],
231
- # 25: [140, 74, 116],
232
- # 26: [90, 102, 1],
233
- # 27: [216, 143, 208]
234
- # }
235
-
236
- # ## The model input needs to be a tensor
237
- # input_tensor = tf.convert_to_tensor(image_resized)
238
- # ## The model expects a batch of images, so add an axis with `tf.newaxis`.
239
- # input_tensor = input_tensor[tf.newaxis, ...]
240
-
241
- # ## Feed image into model and obtain output
242
- # detections_output = model(input_tensor)
243
- # num_detections = int(detections_output.pop("num_detections"))
244
- # detections = {key: value[0, :num_detections].numpy() for key, value in detections_output.items()}
245
- # detections["num_detections"] = num_detections
246
-
247
- # ## Filter out predictions below threshold
248
- # # if threshold is higher, there will be fewer predictions
249
- # # TODO change this number to see how the predictions change
250
- # confidence_threshold = 0.6
251
- # indexes = np.where(detections["detection_scores"] > confidence_threshold)
252
-
253
- # ## Extract predicted bounding boxes
254
- # bboxes = detections["detection_boxes"][indexes]
255
- # # there are no predicted boxes
256
- # if len(bboxes) == 0:
257
- # st.error('No boxes predicted')
258
- # # there are predicted boxes
259
- # else:
260
- # st.success('Boxes predicted')
261
- # classes = detections["detection_classes"][indexes].astype(np.int64)
262
- # scores = detections["detection_scores"][indexes]
263
-
264
- # # plot boxes and labels on image
265
- # image_origi = np.array(Image.fromarray(image_resized).resize((origi_shape[1], origi_shape[0])))
266
- # image_origi = plot_boxes_on_img(color_map, classes, bboxes, image_origi, origi_shape)
267
-
268
- # # show image in web page
269
- # st.image(Image.fromarray(image_origi), caption="Image with predictions", width=400)
270
- # st.markdown("### Predicted boxes")
271
- # for idx in range(len((bboxes))):
272
- # st.markdown(f"* Class: {str(category_index[classes[idx]]['name'])}, confidence score: {str(round(scores[idx], 2))}")
 
18
 
19
  def __call__(self, inputs: "Image.Image")-> List[Dict[str, Any]]:
20
 
21
+ # # convert img to numpy array, resize and normalize to make the prediction
22
+ # img = np.array(inputs)
23
 
24
+ # im = tf.image.resize(img, (128, 128))
25
+ # im = tf.cast(im, tf.float32) / 255.0
26
+ # pred_mask = self.model.predict(im[tf.newaxis, ...])
27
 
28
+ # # take the best performing class for each pixel
29
+ # # the output of argmax looks like this [[1, 2, 0], ...]
30
+ # pred_mask_arg = tf.argmax(pred_mask, axis=-1)
31
 
32
+ # labels = []
33
 
34
+ # # convert the prediction mask into binary masks for each class
35
+ # binary_masks = {}
36
+ # mask_codes = {}
37
 
38
+ # # when we take tf.argmax() over pred_mask, it becomes a tensor object
39
+ # # the shape becomes TensorShape object, looking like this TensorShape([128])
40
+ # # we need to take get shape, convert to list and take the best one
41
 
42
+ # rows = pred_mask_arg[0][1].get_shape().as_list()[0]
43
+ # cols = pred_mask_arg[0][2].get_shape().as_list()[0]
44
 
45
+ # for cls in range(pred_mask.shape[-1]):
46
 
47
+ # binary_masks[f"mask_{cls}"] = np.zeros(shape = (pred_mask.shape[1], pred_mask.shape[2])) #create masks for each class
48
 
49
+ # for row in range(rows):
50
 
51
+ # for col in range(cols):
52
 
53
+ # if pred_mask_arg[0][row][col] == cls:
54
 
55
+ # binary_masks[f"mask_{cls}"][row][col] = 1
56
+ # else:
57
+ # binary_masks[f"mask_{cls}"][row][col] = 0
58
 
59
+ # mask = binary_masks[f"mask_{cls}"]
60
+ # mask *= 255
61
+ # img = Image.fromarray(mask.astype(np.int8), mode="L")
62
 
63
+ # # we need to make it readable for the widget
64
+ # with io.BytesIO() as out:
65
+ # img.save(out, format="PNG")
66
+ # png_string = out.getvalue()
67
+ # mask = base64.b64encode(png_string).decode("utf-8")
68
 
69
+ # mask_codes[f"mask_{cls}"] = mask
70
 
71
 
72
+ # # widget needs the below format, for each class we return label and mask string
73
+ # labels.append({
74
+ # "label": f"LABEL_{cls}",
75
+ # "mask": mask_codes[f"mask_{cls}"],
76
+ # "score": 1.0,
77
+ # })
78
 
79
  labels = [{"score":0.9509243965148926,"label":"car","box":{"xmin":142,"ymin":106,"xmax":376,"ymax":229}},
80
  {"score":0.9981777667999268,"label":"car","box":{"xmin":405,"ymin":146,"xmax":640,"ymax":297}},
 
85
  {"score":0.9996274709701538,"label":"skateboard","box":{"xmin":265,"ymin":348,"xmax":440,"ymax":413}}]
86
 
87
  return labels