def test_imagenet_heatmap(): print("Testing heatmap mode with imagenet head...") model = build_resnet_50(fully_convolutional=True) images = load_test_image_paths() for image_path in images: if "ipod" in image_path: dim = int(224 * 1) pred = predict(model, image_path, target_size=(dim, dim)) heatmap = pred[0, :, :, 605] mean, std = np.mean(heatmap), np.std(heatmap) threshold = mean + 2 * std assert (threshold > 0) heatmap[heatmap < threshold] = 0 heatmap[heatmap >= threshold] = 1 print("Binary ipod heatmap for ipod.jpg, scaled to {}x{}:".format( dim, dim)) print(heatmap) print("Compare ipod.jpg to see that it localizes the ipod!") true_heatmap = np.array([[0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 1., 1., 0.], [0., 0., 0., 0., 1., 1., 0.], [0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0.]]) assert (np.allclose(true_heatmap, heatmap))
def test_custom_head_heatmap(): print("Testing fully convolutial mode with custom head...") model = build_resnet_50(fully_convolutional=True, cls_head='custom') images = load_test_image_paths() for image_path in images: if "cat3" in image_path: dim = int(224 * 1.3) pred = predict(model, image_path, target_size=(dim, dim)) heatmap = pred[0, :, :, 0] mean, std = np.mean(heatmap), np.std(heatmap) threshold = mean + 2 * std assert (threshold > 0) heatmap[heatmap < threshold] = 0 heatmap[heatmap >= threshold] = 1 print("Binary cat heatmap for cat3.jpg, scaled to {}x{}:".format(dim, dim)) print(heatmap) print("Compare cat3.jpg to see that it localizes the cat!") true_heatmap = np.array([[0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 1., 0., 0., 0., 0.], [0., 0., 0., 1., 1., 1., 0., 0., 0.], [0., 0., 0., 1., 1., 1., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0.]]) assert (np.allclose(true_heatmap, heatmap))
def extract_features(extension, img_size): extractor = Extractor(extension) image_files, features = extractor.load_latest_extractions() if image_files is None or features is None: name = "{}x{}_{}".format(img_size[0], img_size[1], "feature-maps") model = build_resnet_50(cls_head=None) image_files, features = extractor.extract(name, model, img_size) print("Features extracted. Shape: {}".format(features.shape)) return image_files, features
def test_imagenet_classification(): print("Testing classification mode with imagenet head...") model = build_resnet_50() images = load_test_image_paths() preds = [] for image_path in images: preds.append(predict(model, image_path)) predictios = np.vstack(preds) for i, top5 in enumerate(decode_predictions(predictios)): current_image = images[i] current_class_label = IMAGE_CLASS_LABELS[current_image] class_labels_predictions = [pred[1] for pred in top5] print("Image: {} | Actual: {} | Top5 predictions: {}".format( current_image, current_class_label, class_labels_predictions)) assert (current_class_label in class_labels_predictions)
def test_custom_head_classification(): print("Testing classfication mode with custom head...") model = build_resnet_50(cls_head='custom') images = load_test_image_paths() preds = [] for image_path in images: preds.append(predict(model, image_path)) predictios = np.vstack(preds) for i, pred in enumerate(predictios): current_image = images[i] is_cat = "cat" in current_image print("Image: {} | Cat score: {:3.5f} | No-cat score {:3.5f}".format( current_image, pred[0], pred[1])) if is_cat: assert (pred[0] > 0.95) assert (pred[1] < 0.05) else: assert (pred[0] < 0.05) assert (pred[1] > 0.95)
parser.add_argument("-ws", "--width", nargs="?", type=int, default=224, help="Image width") args = parser.parse_args() return args def extract(name, model, extension, img_size): extractor = Extractor(extension) image_files, features = extractor.extract(name, model, img_size) def filename_base(args): return "{}x{}-FCN_{}-{}" \ .format(args.height, args.width, args.fully_convolutional, args.cls_head) if __name__ == "__main__": args = parse_args() img_size = (args.height, args.width) model = build_resnet_50(args.fully_convolutional, 'imagenet', args.cls_head) name = filename_base(args) extract(name, model, args.extension, img_size)