import random import torch from albumentations.pytorch import ToTensorV2 import albumentations as A import cv2 import glob2 import config import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as patches from lightning_utils import YOLOv3Lightning from pytorch_grad_cam import GradCAM, EigenCAM from pytorch_grad_cam.utils.image import show_cam_on_image from pytorch_grad_cam.utils.model_targets import FasterRCNNBoxScoreTarget from utils import cells_to_bboxes, non_max_suppression cmap = plt.get_cmap("tab20b") class_labels = config.PASCAL_CLASSES height, width = config.INFERENCE_IMAGE_SIZE, config.INFERENCE_IMAGE_SIZE colors = [cmap(i) for i in np.linspace(0, 1, len(class_labels))] icons = [ 'flight', 'pedal_bike', 'flutter_dash', 'sailing', 'liquor', 'directions_bus', 'directions_car', 'pets', "chair", 'pets', 'table_restaurant', 'pets', 'bedroom_baby', 'motorcycle', 'person', 'yard', 'kebab_dining', 'chair', "train", "tvmonitor"] icons_mapping = {config.PASCAL_CLASSES[i]:icons[i] for i in range(len(icons))} model = YOLOv3Lightning() model = model.load_from_checkpoint('YoLoV3Model.ckpt', map_location=torch.device('cpu')) model.eval() scaled_anchors = ( torch.tensor(config.ANCHORS) * torch.tensor(config.S[0]).unsqueeze(1).unsqueeze(1).repeat(1, 3, 2) ).to(config.DEVICE) def get_examples(): example_images = glob2.glob('*.jpg') example_transparency = [random.choice([0.7, 0.8]) for r in range(len(example_images))] examples = [[example_images[i], example_transparency[i]] for i in range(len(example_images))] return(examples) def yolov3_reshape_transform(x): activations = [] size = x[0].size()[2:4] for x_item in x: x_permute = x_item.permute(0, 1, 4, 2, 3 ) x_permute = x_permute.reshape((x_permute.shape[0], x_permute.shape[1]*x_permute.shape[2], *x_permute.shape[3:])) activations.append(torch.nn.functional.interpolate(torch.abs(x_permute), size, mode='bilinear')) activations = torch.cat(activations, axis=1) return(activations) def infer_transform(IMAGE_SIZE=config.INFERENCE_IMAGE_SIZE): transforms = A.Compose( [ A.LongestMaxSize(max_size=IMAGE_SIZE), A.PadIfNeeded( min_height=IMAGE_SIZE, min_width=IMAGE_SIZE, border_mode=cv2.BORDER_CONSTANT ), A.Normalize(mean=[0, 0, 0], std=[1, 1, 1], max_pixel_value=255,), ToTensorV2(), ] ) return(transforms) def generate_html(): # Start the HTML string with some style and the Material Icons stylesheet classes = config.PASCAL_CLASSES html_string = """