def test_identical(self, x, score, rep): """ Ensure that non_max_suppression works correctly for identical boxes and that ordering doesn't matter. """ x = x.cumsum(axis=1) x = x.repeat(rep).reshape(x.shape[1], rep).T score = np.array([score] * rep) idx = np.random.randint(len(x)) score[idx] = 1000 nms = non_max_suppression(x, score) msg = "non_max_suppression failed to produce the expected output when all detections are identical" assert_array_equal(nms, np.array([idx]), msg) nms = non_max_suppression(x, score, threshold=1) msg = "non_max_suppression failed to produce the expected output for identical detections with threshold 1" assert_array_equal(nms, np.array(range(len(x))), msg)
def scan_fridge(fridge_image, model, classifying_model): training_mean = 200.62128 training_std = 42.149155 device = 'cuda:1' if torch.cuda.is_available() else 'cpu' img = np.copy(fridge_image).astype(np.float32) img-=training_mean img/= training_std img = img[np.newaxis] img = torch.tensor(img.transpose(0, 3, 1, 2)).to(device) #print(img.shape) assert img.shape == torch.Size([1, 3, 800, 497]), "The fridge image is not in the right shape" out_cls, out_reg = model(img) box_preds, class_preds, scores = compute_detections(out_cls.squeeze(), out_reg.squeeze(), feature_map_width=13, anchor_box_step=40, anchor_box_size=80) #Check this threshold = 0 keep = scores > threshold box_preds = box_preds[keep] class_preds = class_preds[keep] scores = scores[keep] keep_idxs = non_max_suppression(box_preds, scores, threshold=0.1) box_preds = box_preds[keep_idxs] class_preds = class_preds[keep_idxs] #print(box_preds) food_images = [] food_coords = [] for class_pred, box_pred in zip(class_preds, box_preds): if class_pred > 0: x1, y1, x2, y2 = box_pred if x1 < 0 or y1 < 0 or x2 >= 497 or y2 >= 800: continue cropped_image = crop_image(fridge_image.swapaxes(0,2).swapaxes(1,2),x1,y1) #print("cropped image shape", cropped_image.shape) food_images.append(cropped_image) food_coords.append((x1,y1)) #print("one item in food_images", food_images[2].shape) #print("coords", food_coords) food_images = np.array(food_images) #print("all images shape", food_images.shape) if len(food_images)==0: return [] #print(food_images.shape, food_images) classes = classifying_model(food_images) #print(classes.shape) #print(food_coords[0][0]) #print(food_coords[0][1]) #print(labels[np.argmax(classes[0].flatten())]) #return food_images, [(food_coords[i][0], food_coords[i][1], labels[np.argmax(classes[i].flatten())], "food") for i in range(len(classes))] #Check this: CHANGE back to this LATER return [Item(food_coords[i][0], food_coords[i][1], labels[np.argmax(classes[i].flatten())], "food",None)for i in range(len(classes))]
def test_known_results(self, threshold, desired_nms): """ Ensures that non_max_suppression works correctly for known values. """ boxes = np.array([[0, 0, 1, 1], [0.5, 0.5, 0.9, 0.9]]) scores = np.array([0, 1]) actual_nms = non_max_suppression(boxes, scores, threshold=threshold) assert_array_equal(actual_nms, desired_nms)
def test_empty(self): """ Ensure that non_max_suppression works correctly with zero detections. """ x = np.empty((0, 4)) scores = np.empty((0, )) nms = non_max_suppression(x, scores) msg = "non_max_suppression failed to produce the expected output for zero detections" assert nms.shape == (0, ), msg
def test_shapes(self, boxes: ndarray, data: st.SearchStrategy): """ Ensure that non_max_suppression produces the correct shape output, even for empty inputs. """ scores = boxes[:, 4] boxes = boxes[:, :4].cumsum( axis=1) # ensures no 0-width or -height boxes N = scores.shape[0] nms = non_max_suppression(boxes, scores) assert ( nms.shape[0] <= N ) # we're suppressing, so we can never end up with more things than we started with assert nms.ndim == 1
def scan_fridge(fridge_image, training_mean, training_std): device = 'cuda:1' if torch.cuda.is_available() else 'cpu' f = open("Actual_Model.p","rb") model = pickle.load(f) f.close() img = np.copy(fridge_image).astype(np.float32) img-=training_mean img/= training_std img = img[np.newaxis] img = torch.tensor(img.transpose(0, 3, 1, 2)).to(device) assert img.shape == torch.Size([1, 3, 800, 497]), "The fridge image is not in the right shape" out_cls, out_reg = model(img) box_preds, class_preds, scores = compute_detections(out_cls.squeeze(), out_reg.squeeze(), feature_map_width=13, anchor_box_step=40, anchor_box_size=80) keep = scores > threshold box_preds = box_preds[keep] class_preds = class_preds[keep] scores = scores[keep] keep_idxs = non_max_suppression(box_preds, scores, threshold=0.1) box_preds = box_preds[keep_idxs] class_preds = class_preds[keep_idxs] food_images = [] food_coords = [] for class_pred, box_pred in zip(class_preds, box_preds): if class_pred > 0: #print("Here") x1, y1, x2, y2 = box_pred food_images.append(crop_image(fridge_image,x1,y1)) food_cords.append((x1,y1)) food_images = np.array(food_images) f = open("classifying_model1", "rb") classifying_model = pickle.load(f) f.close() classes = classifying_model(food_images) return [Item(food_coords[i][0], food_coords[i][1], labels[classes[i]], "food")]
def test_single_detections(self, x: ndarray, score): """ Ensure that a single detection is not suppressed. """ nms = non_max_suppression(x, np.array([score])) msg = "non_max_suppression failed to produce the expected output for a single detection" assert_array_equal(nms, np.array([0]), msg)