File size: 3,348 Bytes
08ec965 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
"""
Vis utilities. Code adapted from LOST: https://github.com/valeoai/LOST
"""
import cv2
import numpy as np
from PIL import Image
import cv2
import scipy
import numpy as np
import torch
import torch.nn as nn
from utils import unnormalize_images
import matplotlib.pyplot as plt
def visualize_img(image):
image = image.clone().detach().cpu().numpy()
#image = np.uint8(np.transpose(image, (1,2,0)))
print(f'image shape: {image.shape}')
image = np.uint8(image*256)
cv2.imwrite('./test/test_img.png', image)
def visualize_fms(image, mask, seed, im_name, dim, scales, folder, save=True):
w_featmap, h_featmap = dim
image = image.clone().detach().cpu().numpy()
image = np.uint8(np.transpose(image, (1,2,0))*256)
mask = mask.reshape(w_featmap,h_featmap)
mask = mask.reshape(w_featmap*h_featmap)
mask[seed] = 3
mask = mask.reshape(w_featmap, h_featmap)
mask = scipy.ndimage.zoom(mask, scales, order=0, mode='nearest')
if save:
pltname1 = f"{folder}/LOST_{im_name}.png"
#Image.fromarray(image).save(pltname1)
cv2.imwrite(pltname1, image)
# print(f"Predictions saved at {pltname1}.")
pltname2 = f"{folder}/Mask_{im_name}.png"
plt.imsave(fname=pltname2, arr=mask)
# print(f"Predictions saved at {pltname2}.")
def visualize_eigvec(eigvec, folder, im_name, dim, scales, save=True):
print(f'eigvec shape: {eigvec.shape}, scales: {scales}, dim: {dim}')
eigvec = scipy.ndimage.zoom(eigvec, scales, order=0, mode='nearest')
if save:
pltname= f"{folder}/{im_name}_ours_att.jpg"
plt.imsave(fname=pltname, arr=eigvec, cmap='cividis')
def visualize_predictions_gt(image, pred, gt, im_name, seed, dim, scales, folder, output_name='ours_box', save=True):
"""
Visualization of the predicted box and the corresponding seed patch.
"""
image = unnormalize_images(image)
image = image[0].clone().detach().cpu().numpy()
image = np.transpose(image, (1,2,0))
image = np.uint8(image*256)
image = np.ascontiguousarray(image, dtype=np.uint8)
# Plot the box
cv2.rectangle(
image,
(int(pred[0]), int(pred[1])),
(int(pred[2]), int(pred[3])),
(255, 0, 0), 2 # RED
)
# Plot the ground truth box
if len(gt>1):
for i in range(len(gt)):
cv2.rectangle(
image,
(int(gt[i][0]), int(gt[i][1])),
(int(gt[i][2]), int(gt[i][3])),
(0, 0, 255), 3, #BLUE
)
if save:
pltname = f"{folder}/{im_name}_{output_name}.jpg"
Image.fromarray(image).save(pltname)
# print(f"Predictions saved at {pltname}.")
return image
def visualize_attn_LOST(A, dims, scales, folder, im_name):
"""
Visualization of the maps presented in Figure 2 of the paper.
"""
w_featmap, h_featmap = dims
# Binarized similarity
binA = A.copy()
binA[binA < 0] = 0
binA[binA > 0] = 1
# Save inverse degree
im_deg = (
nn.functional.interpolate(
torch.from_numpy(1 / binA.sum(-1)).reshape(1, 1, w_featmap, h_featmap),
scale_factor=scales,
mode="nearest",
)[0][0].cpu().numpy()
)
plt.imsave(fname=f"{folder}/{im_name}_lost_attn.jpg", arr=im_deg, cmap='cividis')
|