File size: 9,212 Bytes
999140c cbd88cd 999140c 7de8585 cbd88cd 7de8585 8390f90 cbd88cd 8390f90 cbd88cd 8390f90 cbd88cd 8390f90 999140c 8390f90 f3cff0c 488aa8f f3cff0c 8390f90 1bdb168 8390f90 1bdb168 8390f90 1bdb168 8390f90 1bdb168 8390f90 1bdb168 8390f90 1bdb168 8390f90 1bdb168 45fb2aa 1bdb168 45fb2aa 1bdb168 45fb2aa 1bdb168 45fb2aa 1bdb168 45fb2aa 1bdb168 45fb2aa 1bdb168 45fb2aa 1bdb168 45fb2aa 1600894 45fb2aa 1600894 1bdb168 45fb2aa 1600894 1bdb168 1600894 1bdb168 8390f90 45fb2aa f3cff0c 45fb2aa 92a2afb 45fb2aa 92a2afb 45fb2aa f3cff0c 45fb2aa 66ef476 45fb2aa f3cff0c 45fb2aa 66ef476 45fb2aa 66ef476 45fb2aa 66ef476 45fb2aa e3fd835 45fb2aa 1600894 45fb2aa 02a107c |
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
import csv
import os
import random
import sys
from itertools import product
import gdown
import gradio as gr
import matplotlib
import matplotlib.patches as patches
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
import torchvision.models as models
import torchvision.transforms as transforms
import torchvision.transforms.functional as TF
from matplotlib import pyplot as plt
from matplotlib.patches import ConnectionPatch
from PIL import Image
from torch.utils.data import DataLoader
from common.evaluation import Evaluator
from common.logger import AverageMeter, Logger
from data import download
from model import chmnet
from model.base.geometry import Geometry
csv.field_size_limit(sys.maxsize)
# Downloading the Model
md5 = "6b7b4d7bad7f89600fac340d6aa7708b"
gdown.cached_download(
url="https://drive.google.com/u/0/uc?id=1zsJRlAsoOn5F0GTCprSFYwDDfV85xDy6&export=download",
path="pas_psi.pt",
quiet=False,
md5=md5,
)
# Model Initialization
args = dict(
{
"alpha": [0.05, 0.1],
"benchmark": "pfpascal",
"bsz": 90,
"datapath": "../Datasets_CHM",
"img_size": 240,
"ktype": "psi",
"load": "pas_psi.pt",
"thres": "img",
}
)
model = chmnet.CHMNet(args["ktype"])
model.load_state_dict(torch.load(args["load"], map_location=torch.device("cpu")))
Evaluator.initialize(args["alpha"])
Geometry.initialize(img_size=args["img_size"])
model.eval()
# Transforms
chm_transform = transforms.Compose(
[
transforms.Resize(args["img_size"]),
transforms.CenterCrop((args["img_size"], args["img_size"])),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
]
)
chm_transform_plot = transforms.Compose(
[
transforms.Resize(args["img_size"]),
transforms.CenterCrop((args["img_size"], args["img_size"])),
]
)
# A Helper Function
to_np = lambda x: x.data.to("cpu").numpy()
# Colors for Plotting
cmap = matplotlib.cm.get_cmap("Spectral")
rgba = cmap(0.5)
colors = []
for k in range(49):
colors.append(cmap(k / 49.0))
# CHM MODEL
def run_chm(
source_image,
target_image,
selected_points,
number_src_points,
chm_transform,
display_transform,
):
# Convert to Tensor
src_img_tnsr = chm_transform(source_image).unsqueeze(0)
tgt_img_tnsr = chm_transform(target_image).unsqueeze(0)
# Selected_points = selected_points.T
keypoints = torch.tensor(selected_points).unsqueeze(0)
n_pts = torch.tensor(np.asarray([number_src_points]))
# RUN CHM ------------------------------------------------------------------------
with torch.no_grad():
corr_matrix = model(src_img_tnsr, tgt_img_tnsr)
prd_kps = Geometry.transfer_kps(corr_matrix, keypoints, n_pts, normalized=False)
# VISUALIZATION
src_points = keypoints[0].squeeze(0).squeeze(0).numpy()
tgt_points = prd_kps[0].squeeze(0).squeeze(0).cpu().numpy()
src_points_converted = []
w, h = display_transform(source_image).size
for x, y in zip(src_points[0], src_points[1]):
src_points_converted.append(
[int(x * w / args["img_size"]), int((y) * h / args["img_size"])]
)
src_points_converted = np.asarray(src_points_converted[:number_src_points])
tgt_points_converted = []
w, h = display_transform(target_image).size
for x, y in zip(tgt_points[0], tgt_points[1]):
tgt_points_converted.append(
[int(((x + 1) / 2.0) * w), int(((y + 1) / 2.0) * h)]
)
tgt_points_converted = np.asarray(tgt_points_converted[:number_src_points])
tgt_grid = []
for x, y in zip(tgt_points[0], tgt_points[1]):
tgt_grid.append([int(((x + 1) / 2.0) * 7), int(((y + 1) / 2.0) * 7)])
# VISUALIZATION
# PLOT
fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(12, 8))
# Source image plot
ax[0].imshow(display_transform(source_image))
ax[0].scatter(
src_points_converted[:, 0],
src_points_converted[:, 1],
c="blue",
edgecolors="white",
s=50,
label="Source points",
)
ax[0].set_title("Source Image with Selected Points")
ax[0].set_xticks([])
ax[0].set_yticks([])
# Target image plot
ax[1].imshow(display_transform(target_image))
ax[1].scatter(
tgt_points_converted[:, 0],
tgt_points_converted[:, 1],
c="red",
edgecolors="white",
s=50,
label="Target points",
)
ax[1].set_title("Target Image with Corresponding Points")
ax[1].set_xticks([])
ax[1].set_yticks([])
# Adding labels to points
for i, (src, tgt) in enumerate(zip(src_points_converted, tgt_points_converted)):
ax[0].text(*src, str(i), color="white", bbox=dict(facecolor="black", alpha=0.5))
ax[1].text(*tgt, str(i), color="black", bbox=dict(facecolor="white", alpha=0.7))
# Create a colormap that will generate 49 distinct colors
cmap = plt.get_cmap(
"gist_rainbow", 49
) # 'gist_rainbow' is just an example, you can choose another colormap
# Drawing lines between corresponding source and target points
# for i, (src, tgt) in enumerate(zip(src_points_converted, tgt_points_converted)):
# con = ConnectionPatch(
# xyA=tgt,
# xyB=src,
# coordsA="data",
# coordsB="data",
# axesA=ax[1],
# axesB=ax[0],
# color=cmap(i),
# linewidth=2,
# )
# ax[1].add_artist(con)
# Adding legend
ax[0].legend(loc="lower right", bbox_to_anchor=(1, -0.075))
ax[1].legend(loc="lower right", bbox_to_anchor=(1, -0.075))
plt.tight_layout()
plt.subplots_adjust(wspace=0.1, hspace=0.1)
fig.suptitle("CHM Correspondences\nUsing $\it{pas\_psi.pt}$ Weights ", fontsize=16)
return fig
# Wrapper
def generate_correspondences(
sousrce_image, target_image, min_x=1, max_x=100, min_y=1, max_y=100
):
A = np.linspace(min_x, max_x, 7)
B = np.linspace(min_y, max_y, 7)
point_list = list(product(A, B))
new_points = np.asarray(point_list, dtype=np.float64).T
return run_chm(
sousrce_image,
target_image,
selected_points=new_points,
number_src_points=49,
chm_transform=chm_transform,
display_transform=chm_transform_plot,
)
with gr.Blocks() as demo:
gr.Markdown(
"""
# Correspondence Matching with Convolutional Hough Matching Networks
Performs keypoint transform from a 7x7 gird on the source image to the target image. Use the sliders to adjust the grid.
[Original Paper](https://arxiv.org/abs/2103.16831) - [Github Page](https://github.com/juhongm999/chm)
"""
)
with gr.Row():
# Add an Image component to display the source image.
image1 = gr.Image(
height=240,
width=240,
type="pil",
label="Source Image",
)
# Add an Image component to display the target image.
image2 = gr.Image(
height=240,
width=240,
type="pil",
label="Target Image",
)
with gr.Row():
# Add a Slider component to adjust the minimum x-coordinate of the grid.
min_x = gr.Slider(
minimum=1,
maximum=240,
step=1,
value=15,
label="Min X",
)
# Add a Slider component to adjust the maximum x-coordinate of the grid.
max_x = gr.Slider(
minimum=1,
maximum=240,
step=1,
value=215,
label="Max X",
)
# Add a Slider component to adjust the minimum y-coordinate of the grid.
min_y = gr.Slider(
minimum=1,
maximum=240,
step=1,
value=15,
label="Min Y",
)
# Add a Slider component to adjust the maximum y-coordinate of the grid.
max_y = gr.Slider(
minimum=1,
maximum=240,
step=1,
value=215,
label="Max Y",
)
with gr.Row():
output_plot = gr.Plot()
gr.Examples(
[
["./examples/sample1.jpeg", "./examples/sample2.jpeg", 17, 223, 17, 223],
[
"./examples/Red_Winged_Blackbird_0012_6015.jpg",
"./examples/Red_Winged_Blackbird_0025_5342.jpg",
17,
223,
17,
223,
],
[
"./examples/Yellow_Headed_Blackbird_0026_8545.jpg",
"./examples/Yellow_Headed_Blackbird_0020_8549.jpg",
17,
223,
17,
223,
],
],
inputs=[
image1,
image2,
min_x,
max_x,
min_y,
max_y,
],
)
run_btn = gr.Button("Run")
run_btn.click(
generate_correspondences,
inputs=[image1, image2, min_x, max_x, min_y, max_y],
outputs=output_plot,
)
demo.launch()
|