Example #1
0
# print(model)
cat = Image.open("/home/francesco/Desktop/cat.jpg")
# resize the image and make it a tensor
input = Compose([Resize((224, 224)), ToTensor(), image_net_preprocessing])(cat)
# add 1 dim for batch
input = input.unsqueeze(0)
# call mirror with the input and the model
layers = list(model.children())
# layer = layers[4][2]
# print(layer)


def imshow(tensor):
    tensor = tensor.squeeze()
    if len(tensor.shape) > 2: tensor = tensor.permute(1, 2, 0)
    img = tensor.cpu().numpy()
    plt.imshow(img, cmap='gray')
    plt.show()


model.eval()

vis = GradCam(model.to(device), device)
img = vis(input.to(device),
          None,
          target_class=None,
          postprocessing=image_net_postprocessing,
          guide=False)

with torch.no_grad():
    imshow(img[0])
Example #2
0
                    help='model to visualise (default: food)')
parser.add_argument('--i',
                    type=str,
                    default='nan',
                    help='image to visualise (default: nan)')
args = parser.parse_args()

# Device configuration
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# image loader
image = Image.open(args.i)
image = image.convert('RGB')
input = Compose([Resize((224, 224)),
                 ToTensor(), image_net_preprocessing])(image).unsqueeze(0)
input = input.to(device)

# model
model_name = "resnet"
feature_extract = True
num_classes = 2
model, _ = model.initialize_model(model_name,
                                  feature_extract,
                                  num_classes,
                                  use_pretrained=True)
model = model.to(device)


def test():
    MODEL_PATH = 'backup/' + args.tag + '/checkpoint.pt'
    if device == torch.device('cpu'):