File size: 18,373 Bytes
bc4aaf8 | 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 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | import os
import torch
import os
import pandas as pd
import numpy as np
from PIL import Image
from tqdm import tqdm
import torch.nn.functional as F
from extractor_sd import load_model, load_sd_backbone, process_features_and_mask, get_mask
from open_clip.transform import ResizeMaxSize,_convert_to_rgb
from torchvision.transforms import ToTensor
from third_party.utils.utils_correspondence import co_pca, resize, find_nearest_patchs, find_nearest_patchs_replace
import matplotlib.pyplot as plt
import sys
from detectron2.data import transforms as T
from extractor_dino import ViTExtractor
from sklearn.decomposition import PCA as sklearnPCA
import math
from sklearn.cluster import KMeans
from scipy.optimize import linear_sum_assignment
from torchvision import transforms
def preprocess_pil(pil_image):
prep = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225))
])
prep_img = prep(pil_image)[None, ...]
return prep_img
MASK = True
VER = "v1-5"
PCA = False
CO_PCA = True
PCA_DIMS = [256, 256, 256]
SIZE =960
EDGE_PAD = False
FUSE_DINO = 1
ONLY_DINO = 0
MODEL_SIZE = 'base'
DRAW=1
TEXT_INPUT = False
SEED = 42
TIMESTEP = 100
DIST = 'l2' if FUSE_DINO and not ONLY_DINO else 'cos'
if ONLY_DINO:
FUSE_DINO = True
np.random.seed(SEED)
torch.manual_seed(SEED)
torch.cuda.manual_seed(SEED)
torch.backends.cudnn.benchmark = True
sd_transform=transforms.Compose([
ResizeMaxSize(960, fill=0),
_convert_to_rgb,
ToTensor(),
])
model = load_sd_backbone(diffusion_ver=VER, image_size=SIZE, num_timesteps=TIMESTEP, decoder_only=False)
odise, aug = load_model(diffusion_ver=VER, image_size=SIZE, num_timesteps=TIMESTEP,decoder_only=False)
def compute_pair_feature(model, aug, save_path, files, category, mask=False, dist='cos', real_size=960):
img_size = 840
stride = 14
device = 'cuda' if torch.cuda.is_available() else 'cpu'
# extractor=torch.hub.load('/mnt/SSD8T/home/wjj/.cache/torch/hub/facebookresearch_dinov2_main', 'dinov2_vitb14', source='local').to(device).eval()
extractor = ViTExtractor('dinov2_vitb14', 14, device=device)
patch_size = 14
num_patches = int(patch_size / stride * (img_size // patch_size - 1) + 1)
# Load image 1
# img1_input = resize(img1, real_size, resize=True, to_pil=True, edge=EDGE_PAD)
img1 = Image.open(files[0])
sd_input1=sd_transform(img1).to(device).unsqueeze(0)
img1 = resize(img1, img_size, resize=True, to_pil=True, edge=EDGE_PAD)
# Load image 2
# img2_input = resize(img2, real_size, resize=True, to_pil=True, edge=EDGE_PAD)
img2 = Image.open(files[1])
sd_input2 = sd_transform(img2).to(device).unsqueeze(0)
img2 = resize(img2, img_size, resize=True, to_pil=True, edge=EDGE_PAD)
result = []
with torch.no_grad():
# features1 = process_features_and_mask(model, aug, img1_input, input_text=input_text, mask=False, raw=True)
# features2 = process_features_and_mask(model, aug, img2_input, input_text=input_text, mask=False, raw=True)
features1=model(sd_input1,raw=True)
features2=model(sd_input2,raw=True)
processed_features1, processed_features2 = co_pca(features1, features2, PCA_DIMS)
img1_desc = processed_features1.reshape(1, 1, -1, num_patches**2).permute(0,1,3,2)
img2_desc = processed_features2.reshape(1, 1, -1, num_patches**2).permute(0,1,3,2)
img1_batch = preprocess_pil(img1).to(device)
# img1_desc_dino = extractor.get_intermediate_layers(img1_batch)[0].unsqueeze(1)
img1_desc_dino = extractor.extract_descriptors(img1_batch, 11, 'token')
img2_batch = preprocess_pil(img2).to(device)
# img2_desc_dino = extractor.get_intermediate_layers(img2_batch)[0].unsqueeze(1)
img2_desc_dino = extractor.extract_descriptors(img2_batch, 11, 'token')
img1_desc = img1_desc / img1_desc.norm(dim=-1, keepdim=True)
img2_desc = img2_desc / img2_desc.norm(dim=-1, keepdim=True)
img1_desc_dino = img1_desc_dino / img1_desc_dino.norm(dim=-1, keepdim=True)
img2_desc_dino = img2_desc_dino / img2_desc_dino.norm(dim=-1, keepdim=True)
img1_desc = torch.cat((img1_desc, img1_desc_dino), dim=-1)
img2_desc = torch.cat((img2_desc, img2_desc_dino), dim=-1)
if DRAW:
mask1 = get_mask(odise, aug, img1, category[0])
mask2 = get_mask(odise, aug, img2, category[-1])
img1_desc_reshaped = img1_desc.permute(0,1,3,2).reshape(-1, img1_desc.shape[-1], num_patches, num_patches)
img2_desc_reshaped = img2_desc.permute(0,1,3,2).reshape(-1, img2_desc.shape[-1], num_patches, num_patches)
trg_dense_output, src_color_map = find_nearest_patchs(mask2, mask1, img2, img1, img2_desc_reshaped, img1_desc_reshaped, mask=mask)
if not os.path.exists(f'{save_path}/{category[0]}'):
os.makedirs(f'{save_path}/{category[0]}')
fig_colormap, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 8))
ax1.axis('off')
ax2.axis('off')
ax1.imshow(src_color_map)
ax2.imshow(trg_dense_output)
fig_colormap.savefig(f'{save_path}/{category[0]}/_colormap.png')
plt.close(fig_colormap)
img1_desc_reshaped = img1_desc.permute(0,1,3,2).reshape(-1, img1_desc.shape[-1], num_patches, num_patches)
img2_desc_reshaped = img2_desc.permute(0,1,3,2).reshape(-1, img2_desc.shape[-1], num_patches, num_patches)
trg_dense_output, src_color_map = find_nearest_patchs_replace(mask2, mask1, img2, img1, img2_desc_reshaped, img1_desc_reshaped, mask=mask, resolution=156)
if not os.path.exists(f'{save_path}/{category[0]}'):
os.makedirs(f'{save_path}/{category[0]}')
fig_colormap, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 8))
ax1.axis('off')
ax2.axis('off')
ax1.imshow(src_color_map)
ax2.imshow(trg_dense_output)
fig_colormap.savefig(f'{save_path}/{category[0]}/_swap.png')
plt.close(fig_colormap)
result.append([img1_desc.cpu(), img2_desc.cpu(), mask1.cpu(), mask2.cpu()])
return result
def vis_pca_mask(result,save_path):
# PCA visualization mask version
for (feature1,feature2,mask1,mask2) in result:
# feature1 shape (1,1,3600,768*2)
# feature2 shape (1,1,3600,768*2)
num_patches = int(math.sqrt(feature1.shape[-2]))
# pca the concatenated feature to 3 dimensions
feature1 = feature1.squeeze() # shape (3600,768*2)
feature2 = feature2.squeeze() # shape (3600,768*2)
chennel_dim = feature1.shape[-1]
# resize back
src_feature_reshaped = feature1.squeeze().permute(1,0).reshape(-1,num_patches,num_patches).cuda()
tgt_feature_reshaped = feature2.squeeze().permute(1,0).reshape(-1,num_patches,num_patches).cuda()
resized_src_mask = F.interpolate(mask1.unsqueeze(0).unsqueeze(0), size=(num_patches, num_patches), mode='nearest').squeeze().cuda()
resized_tgt_mask = F.interpolate(mask2.unsqueeze(0).unsqueeze(0), size=(num_patches, num_patches), mode='nearest').squeeze().cuda()
src_feature_upsampled = src_feature_reshaped * resized_src_mask.repeat(src_feature_reshaped.shape[0],1,1)
tgt_feature_upsampled = tgt_feature_reshaped * resized_tgt_mask.repeat(src_feature_reshaped.shape[0],1,1)
feature1=src_feature_upsampled.reshape(chennel_dim,-1).permute(1,0)
feature2=tgt_feature_upsampled.reshape(chennel_dim,-1).permute(1,0)
n_components=4 # the first component is to seperate the object from the background
pca = sklearnPCA(n_components=n_components)
feature1_n_feature2 = torch.cat((feature1,feature2),dim=0) # shape (7200,768*2)
feature1_n_feature2 = pca.fit_transform(feature1_n_feature2.cpu().numpy()) # shape (7200,3)
feature1 = feature1_n_feature2[:feature1.shape[0],:] # shape (3600,3)
feature2 = feature1_n_feature2[feature1.shape[0]:,:] # shape (3600,3)
fig, axes = plt.subplots(4, 2, figsize=(10, 14))
for show_channel in range(n_components):
if show_channel==0:
continue
# min max normalize the feature map
feature1[:, show_channel] = (feature1[:, show_channel] - feature1[:, show_channel].min()) / (feature1[:, show_channel].max() - feature1[:, show_channel].min())
feature2[:, show_channel] = (feature2[:, show_channel] - feature2[:, show_channel].min()) / (feature2[:, show_channel].max() - feature2[:, show_channel].min())
feature1_first_channel = feature1[:, show_channel].reshape(num_patches,num_patches)
feature2_first_channel = feature2[:, show_channel].reshape(num_patches,num_patches)
axes[show_channel-1, 0].imshow(feature1_first_channel)
axes[show_channel-1, 0].axis('off')
axes[show_channel-1, 1].imshow(feature2_first_channel)
axes[show_channel-1, 1].axis('off')
axes[show_channel-1, 0].set_title('Feature 1 - Channel {}'.format(show_channel ), fontsize=14)
axes[show_channel-1, 1].set_title('Feature 2 - Channel {}'.format(show_channel ), fontsize=14)
feature1_resized = feature1[:, 1:4].reshape(num_patches,num_patches, 3)
feature2_resized = feature2[:, 1:4].reshape(num_patches,num_patches, 3)
axes[3, 0].imshow(feature1_resized)
axes[3, 0].axis('off')
axes[3, 1].imshow(feature2_resized)
axes[3, 1].axis('off')
axes[3, 0].set_title('Feature 1 - All Channels', fontsize=14)
axes[3, 1].set_title('Feature 2 - All Channels', fontsize=14)
plt.tight_layout()
plt.show()
fig.savefig(save_path+'/masked_pca.png', dpi=300)
def vis_pca(result,save_path,src_img_path,trg_img_path):
# PCA visualization
for (feature1,feature2,mask1,mask2) in result:
# feature1 shape (1,1,3600,768*2)
# feature2 shape (1,1,3600,768*2)
num_patches=int(math.sqrt(feature1.shape[2]))
# pca the concatenated feature to 3 dimensions
feature1 = feature1.squeeze() # shape (3600,768*2)
feature2 = feature2.squeeze() # shape (3600,768*2)
chennel_dim = feature1.shape[-1]
# resize back
h1, w1 = Image.open(src_img_path).size
scale_h1 = h1/num_patches
scale_w1 = w1/num_patches
if scale_h1 > scale_w1:
scale = scale_h1
scaled_w = int(w1/scale)
feature1 = feature1.reshape(num_patches,num_patches,chennel_dim)
feature1_uncropped=feature1[(num_patches-scaled_w)//2:num_patches-(num_patches-scaled_w)//2,:,:]
else:
scale = scale_w1
scaled_h = int(h1/scale)
feature1 = feature1.reshape(num_patches,num_patches,chennel_dim)
feature1_uncropped=feature1[:,(num_patches-scaled_h)//2:num_patches-(num_patches-scaled_h)//2,:]
h2, w2 = Image.open(trg_img_path).size
scale_h2 = h2/num_patches
scale_w2 = w2/num_patches
if scale_h2 > scale_w2:
scale = scale_h2
scaled_w = int(w2/scale)
feature2 = feature2.reshape(num_patches,num_patches,chennel_dim)
feature2_uncropped=feature2[(num_patches-scaled_w)//2:num_patches-(num_patches-scaled_w)//2,:,:]
else:
scale = scale_w2
scaled_h = int(h2/scale)
feature2 = feature2.reshape(num_patches,num_patches,chennel_dim)
feature2_uncropped=feature2[:,(num_patches-scaled_h)//2:num_patches-(num_patches-scaled_h)//2,:]
f1_shape=feature1_uncropped.shape[:2]
f2_shape=feature2_uncropped.shape[:2]
feature1 = feature1_uncropped.reshape(f1_shape[0]*f1_shape[1],chennel_dim)
feature2 = feature2_uncropped.reshape(f2_shape[0]*f2_shape[1],chennel_dim)
n_components=3
pca = sklearnPCA(n_components=n_components)
feature1_n_feature2 = torch.cat((feature1,feature2),dim=0) # shape (7200,768*2)
feature1_n_feature2 = pca.fit_transform(feature1_n_feature2.cpu().numpy()) # shape (7200,3)
feature1 = feature1_n_feature2[:feature1.shape[0],:] # shape (3600,3)
feature2 = feature1_n_feature2[feature1.shape[0]:,:] # shape (3600,3)
fig, axes = plt.subplots(4, 2, figsize=(10, 14))
for show_channel in range(n_components):
# min max normalize the feature map
feature1[:, show_channel] = (feature1[:, show_channel] - feature1[:, show_channel].min()) / (feature1[:, show_channel].max() - feature1[:, show_channel].min())
feature2[:, show_channel] = (feature2[:, show_channel] - feature2[:, show_channel].min()) / (feature2[:, show_channel].max() - feature2[:, show_channel].min())
feature1_first_channel = feature1[:, show_channel].reshape(f1_shape[0], f1_shape[1])
feature2_first_channel = feature2[:, show_channel].reshape(f2_shape[0], f2_shape[1])
axes[show_channel, 0].imshow(feature1_first_channel)
axes[show_channel, 0].axis('off')
axes[show_channel, 1].imshow(feature2_first_channel)
axes[show_channel, 1].axis('off')
axes[show_channel, 0].set_title('Feature 1 - Channel {}'.format(show_channel + 1), fontsize=14)
axes[show_channel, 1].set_title('Feature 2 - Channel {}'.format(show_channel + 1), fontsize=14)
feature1_resized = feature1[:, :3].reshape(f1_shape[0], f1_shape[1], 3)
feature2_resized = feature2[:, :3].reshape(f2_shape[0], f2_shape[1], 3)
axes[3, 0].imshow(feature1_resized)
axes[3, 0].axis('off')
axes[3, 1].imshow(feature2_resized)
axes[3, 1].axis('off')
axes[3, 0].set_title('Feature 1 - All Channels', fontsize=14)
axes[3, 1].set_title('Feature 2 - All Channels', fontsize=14)
plt.tight_layout()
plt.show()
fig.savefig(save_path+'/pca.png', dpi=300)
def perform_clustering(features, n_clusters=10):
# Normalize features
features = F.normalize(features, p=2, dim=1)
# Convert the features to float32
features = features.cpu().detach().numpy().astype('float32')
# Initialize a k-means clustering index with the desired number of clusters
kmeans = KMeans(n_clusters=n_clusters, random_state=0)
# Train the k-means index with the features
kmeans.fit(features)
# Assign the features to their nearest cluster
labels = kmeans.predict(features)
return labels
def cluster_and_match(result, save_path, n_clusters=6):
for (feature1,feature2,mask1,mask2) in result:
# feature1 shape (1,1,3600,768*2)
num_patches = int(math.sqrt(feature1.shape[-2]))
# pca the concatenated feature to 3 dimensions
feature1 = feature1.squeeze() # shape (3600,768*2)
feature2 = feature2.squeeze() # shape (3600,768*2)
chennel_dim = feature1.shape[-1]
# resize back
src_feature_reshaped = feature1.squeeze().permute(1,0).reshape(-1,num_patches,num_patches).cuda()
tgt_feature_reshaped = feature2.squeeze().permute(1,0).reshape(-1,num_patches,num_patches).cuda()
resized_src_mask = F.interpolate(mask1.unsqueeze(0).unsqueeze(0), size=(num_patches, num_patches), mode='nearest').squeeze().cuda()
resized_tgt_mask = F.interpolate(mask2.unsqueeze(0).unsqueeze(0), size=(num_patches, num_patches), mode='nearest').squeeze().cuda()
src_feature_upsampled = src_feature_reshaped * resized_src_mask.repeat(src_feature_reshaped.shape[0],1,1)
tgt_feature_upsampled = tgt_feature_reshaped * resized_tgt_mask.repeat(src_feature_reshaped.shape[0],1,1)
feature1=src_feature_upsampled.unsqueeze(0)
feature2=tgt_feature_upsampled.unsqueeze(0)
w1, h1 = feature1.shape[2], feature1.shape[3]
w2, h2 = feature2.shape[2], feature2.shape[3]
features1_2d = feature1.reshape(feature1.shape[1], -1).permute(1, 0)
features2_2d = feature2.reshape(feature2.shape[1], -1).permute(1, 0)
labels_img1 = perform_clustering(features1_2d, n_clusters)
labels_img2 = perform_clustering(features2_2d, n_clusters)
cluster_means_img1 = [features1_2d.cpu().detach().numpy()[labels_img1 == i].mean(axis=0) for i in range(n_clusters)]
cluster_means_img2 = [features2_2d.cpu().detach().numpy()[labels_img2 == i].mean(axis=0) for i in range(n_clusters)]
distances = np.linalg.norm(np.expand_dims(cluster_means_img1, axis=1) - np.expand_dims(cluster_means_img2, axis=0), axis=-1)
# Use Hungarian algorithm to find the optimal bijective mapping
row_ind, col_ind = linear_sum_assignment(distances)
relabeled_img2 = np.zeros_like(labels_img2)
for i, match in zip(row_ind, col_ind):
relabeled_img2[labels_img2 == match] = i
labels_img1 = labels_img1.reshape(w1, h1)
relabeled_img2 = relabeled_img2.reshape(w2, h2)
fig, axs = plt.subplots(1, 2, figsize=(10, 5))
# Plot the results
ax_img1 = axs[0]
axs[0].axis('off')
ax_img1.imshow(labels_img1, cmap='tab20')
ax_img2 = axs[1]
axs[1].axis('off')
ax_img2.imshow(relabeled_img2, cmap='tab20')
plt.tight_layout()
plt.show()
fig.savefig(save_path+'/clustering.png', dpi=300)
def process_images(src_img_path,trg_img_path):
categories = [['dog'], ['dog']]
files = [src_img_path, trg_img_path]
save_path = './results_vis' + f'/{trg_img_path.split("/")[-1].split(".")[0]}_{src_img_path.split("/")[-1].split(".")[0]}'
result = compute_pair_feature(model, aug, save_path, files, mask=MASK, category=categories, dist=DIST)
if MASK:
vis_pca_mask(result, save_path)
cluster_and_match(result, save_path)
if 'Anno' not in src_img_path:
vis_pca(result, save_path,src_img_path,trg_img_path)
return result
src_img_path = "/mnt/SSD8T/home/wjj/code/sd-dino/data/images/dog_00.jpg"
trg_img_path = "/mnt/SSD8T/home/wjj/code/sd-dino/data/images/dog_59.jpg"
result = process_images(src_img_path, trg_img_path) |