Spaces:
Build error
Build error
File size: 4,882 Bytes
bb3ea39 dd504c0 880da41 dd504c0 402b433 880da41 402b433 880da41 497a5c7 bb3ea39 f4b82b2 9651aac e7c9542 dd504c0 402b433 f4b82b2 c9dadbf f4b82b2 351ead9 f4b82b2 c9dadbf f4b82b2 9651aac c9dadbf 4e8ced7 70e3f4a c712472 f4b82b2 497a5c7 f4b82b2 c9dadbf 402b433 7c408ba 402b433 dd504c0 402b433 dd504c0 402b433 dd504c0 402b433 dd504c0 402b433 dd504c0 402b433 dd504c0 402b433 f4b82b2 402b433 1a2db09 dd504c0 f4b82b2 1a2db09 f4b82b2 1a2db09 |
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
import gradio as gr
import cv2
import torch
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib import colors
from mpl_toolkits.axes_grid1 import ImageGrid
from torchvision import transforms
import fire_network
import numpy as np
from PIL import Image
# Possible Scales for multiscale inference
scales = [2.0, 1.414, 1.0, 0.707, 0.5, 0.353, 0.25]
device = 'cpu'
# Load net
state = torch.load('fire.pth', map_location='cpu')
state['net_params']['pretrained'] = None # no need for imagenet pretrained model
net = fire_network.init_network(**state['net_params']).to(device)
net.load_state_dict(state['state_dict'])
transform = transforms.Compose([
transforms.Resize(1024),
transforms.ToTensor(),
transforms.Normalize(**dict(zip(["mean", "std"], net.runtime['mean_std'])))
])
# which sf
sf_idx_ = [55, 14, 5, 4, 52, 57, 40, 9]
col = plt.get_cmap('tab10')
def generate_matching_superfeatures(im1, im2, scale_id=6, threshold=50):
im1_tensor = transform(im1).unsqueeze(0)
im2_tensor = transform(im2).unsqueeze(0)
# im1_cv = cv2.imread(im1)
# im2_cv = cv2.imread(im2)
# extract features
with torch.no_grad():
output1 = net.get_superfeatures(im1_tensor.to(device), scales=[scale_id])
feats1 = output1[0][0]
attns1 = output1[1][0]
strenghts1 = output1[2][0]
output2 = net.get_superfeatures(im2_tensor.to(device), scales=[scale_id])
feats2 = output2[0][0]
attns2 = output2[1][0]
strenghts2 = output2[2][0]
print(feats1.shape, feats2.shape)
print(attns1.shape, attns2.shape)
print(strenghts1.shape, strenghts2.shape)
# Store all binary SF att maps to show them all at once in the end
all_att_bin1 = []
all_att_bin2 = []
for n, i in enumerate(sf_idx_):
# all_atts[n].append(attn[j][scale_id][0,i,:,:].numpy())
att_heat = np.array(attns1[0,i,:,:].numpy(), dtype=np.float32)
att_heat = np.uint8(att_heat / np.max(att_heat[:]) * 255.0)
att_heat_bin = np.where(att_heat>threshold, 255, 0)
all_att_bin1.append(att_heat_bin)
att_heat = np.array(attns2[0,i,:,:].numpy(), dtype=np.float32)
att_heat = np.uint8(att_heat / np.max(att_heat[:]) * 255.0)
att_heat_bin = np.where(att_heat>threshold, 255, 0)
all_att_bin2.append(att_heat_bin)
fin_img = []
img1rsz = np.copy(im1)
print(img1rsz.size)
for j, att in enumerate(all_att_bin1):
att = cv2.resize(att, im1.size, interpolation=cv2.INTER_NEAREST)
# att = cv2.resize(att, imgz[i].shape[:2][::-1], interpolation=cv2.INTER_CUBIC)
# att = cv2.resize(att, imgz[i].shape[:2][::-1])
# att = att.resize(shape)
# att = resize(att, im1.size)
mask2d = zip(*np.where(att==255))
for m,n in mask2d:
col_ = col.colors[j] if j < 7 else col.colors[j+1]
if j == 0: col_ = col.colors[9]
col_ = 255*np.array(colors.to_rgba(col_))[:3]
img1rsz[m,n, :] = col_[::-1]
fin_img.append(img1rsz)
img2rsz = np.copy(im2)
for j, att in enumerate(all_att_bin2):
att = cv2.resize(att, im2.size, interpolation=cv2.INTER_NEAREST)
# att = cv2.resize(att, imgz[i].shape[:2][::-1], interpolation=cv2.INTER_CUBIC)
# # att = cv2.resize(att, imgz[i].shape[:2][::-1])
# att = att.resize(im2.shape)
# print('att:', att.shape)
mask2d = zip(*np.where(att==255))
for m,n in mask2d:
col_ = col.colors[j] if j < 7 else col.colors[j+1]
if j == 0: col_ = col.colors[9]
col_ = 255*np.array(colors.to_rgba(col_))[:3]
img2rsz[m,n, :] = col_[::-1]
fin_img.append(img2rsz)
fig = plt.figure(figsize=(12,25))
grid = ImageGrid(fig, 111, nrows_ncols=(2, 1), axes_pad=0.1)
for ax, img in zip(grid, fin_img):
ax.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
ax.axis('scaled')
ax.axis('off')
plt.tight_layout()
fig.suptitle("Matching SFs", fontsize=16)
return fig
# GRADIO APP
title = "Visualizing Super-features"
description = "TBD"
article = "<p style='text-align: center'><a href='https://github.com/naver/fire' target='_blank'>Original Github Repo</a></p>"
iface = gr.Interface(
fn=generate_matching_superfeatures,
inputs=[
gr.inputs.Image(shape=(1024, 1024), type="pil"),
gr.inputs.Image(shape=(1024, 1024), type="pil"),
gr.inputs.Slider(minimum=1, maximum=7, step=1, default=2, label="Scale"),
gr.inputs.Slider(minimum=1, maximum=255, step=25, default=50, label="Binarizatio Threshold")],
outputs="plot",
enable_queue=True,
title=title,
description=description,
article=article,
examples=[["chateau_1.png", "chateau_2.png", 6, 50]],
)
iface.launch()
|