{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "import os\n", "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"3\"\n", "import torch\n", "import os\n", "import pandas as pd\n", "import numpy as np\n", "from PIL import Image\n", "from tqdm import tqdm\n", "import torch.nn.functional as F\n", "from extractor_sd import load_model, process_features_and_mask, get_mask\n", "from third_party.utils.utils_correspondence import co_pca, resize, find_nearest_patchs, find_nearest_patchs_replace\n", "import matplotlib.pyplot as plt\n", "import sys\n", "from extractor_dino import ViTExtractor\n", "from sklearn.decomposition import PCA as sklearnPCA\n", "import math\n", "from sklearn.cluster import KMeans\n", "from scipy.optimize import linear_sum_assignment" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "MASK = True\n", "VER = \"v1-5\"\n", "PCA = False\n", "CO_PCA = True\n", "PCA_DIMS = [256, 256, 256]\n", "SIZE =960\n", "EDGE_PAD = False\n", "\n", "FUSE_DINO = 1\n", "ONLY_DINO = 0\n", "DINOV2 = True\n", "MODEL_SIZE = 'base'\n", "DRAW_DENSE = 1\n", "DRAW_SWAP = 1\n", "TEXT_INPUT = False\n", "SEED = 42\n", "TIMESTEP = 100\n", "\n", "DIST = 'l2' if FUSE_DINO and not ONLY_DINO else 'cos'\n", "if ONLY_DINO:\n", " FUSE_DINO = True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.random.seed(SEED)\n", "torch.manual_seed(SEED)\n", "torch.cuda.manual_seed(SEED)\n", "torch.backends.cudnn.benchmark = True\n", "\n", "model, aug = load_model(diffusion_ver=VER, image_size=SIZE, num_timesteps=TIMESTEP,decoder_only=False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def compute_pair_feature(model, aug, save_path, files, category, mask=False, dist='cos', real_size=960):\n", " if type(category) == str:\n", " category = [category]\n", " img_size = 840 if DINOV2 else 244\n", " model_dict={'small':'dinov2_vits14',\n", " 'base':'dinov2_vitb14',\n", " 'large':'dinov2_vitl14',\n", " 'giant':'dinov2_vitg14'}\n", " \n", " model_type = model_dict[MODEL_SIZE] if DINOV2 else 'dino_vits8'\n", " layer = 11 if DINOV2 else 9\n", " if 'l' in model_type:\n", " layer = 23\n", " elif 'g' in model_type:\n", " layer = 39\n", " facet = 'token' if DINOV2 else 'key'\n", " stride = 14 if DINOV2 else 4\n", " device = 'cuda' if torch.cuda.is_available() else 'cpu'\n", " # indiactor = 'v2' if DINOV2 else 'v1'\n", " # model_size = model_type.split('vit')[-1]\n", " extractor = ViTExtractor(model_type, stride, device=device)\n", " patch_size = extractor.model.patch_embed.patch_size[0] if DINOV2 else extractor.model.patch_embed.patch_size\n", " num_patches = int(patch_size / stride * (img_size // patch_size - 1) + 1)\n", " \n", " input_text = \"a photo of \"+category[-1][0] if TEXT_INPUT else None\n", "\n", " current_save_results = 0\n", "\n", " N = len(files) // 2\n", " pbar = tqdm(total=N)\n", " result = []\n", " if 'Anno' in files[0]:\n", " Anno=True\n", " else:\n", " Anno=False\n", " for pair_idx in range(N):\n", "\n", " # Load image 1\n", " img1 = Image.open(files[2*pair_idx]).convert('RGB')\n", " img1_input = resize(img1, real_size, resize=True, to_pil=True, edge=EDGE_PAD)\n", " img1 = resize(img1, img_size, resize=True, to_pil=True, edge=EDGE_PAD)\n", "\n", " # Load image 2\n", " img2 = Image.open(files[2*pair_idx+1]).convert('RGB')\n", " img2_input = resize(img2, real_size, resize=True, to_pil=True, edge=EDGE_PAD)\n", " img2 = resize(img2, img_size, resize=True, to_pil=True, edge=EDGE_PAD)\n", "\n", " with torch.no_grad():\n", " if not CO_PCA: \n", " if not ONLY_DINO: \n", " img1_desc = process_features_and_mask(model, aug, img1_input, input_text=input_text, mask=False, pca=PCA).reshape(1,1,-1, num_patches**2).permute(0,1,3,2)\n", " img2_desc = process_features_and_mask(model, aug, img2_input, category[-1], input_text=input_text, mask=mask, pca=PCA).reshape(1,1,-1, num_patches**2).permute(0,1,3,2)\n", " if FUSE_DINO: \n", " img1_batch = extractor.preprocess_pil(img1)\n", " img1_desc_dino = extractor.extract_descriptors(img1_batch.to(device), layer, facet)\n", " img2_batch = extractor.preprocess_pil(img2)\n", " img2_desc_dino = extractor.extract_descriptors(img2_batch.to(device), layer, facet)\n", "\n", " else:# enter \n", " if not ONLY_DINO:# enter \n", " features1 = process_features_and_mask(model, aug, img1_input, input_text=input_text, mask=False, raw=True)\n", " features2 = process_features_and_mask(model, aug, img2_input, input_text=input_text, mask=False, raw=True)\n", " processed_features1, processed_features2 = co_pca(features1, features2, PCA_DIMS)\n", " img1_desc = processed_features1.reshape(1, 1, -1, num_patches**2).permute(0,1,3,2)\n", " img2_desc = processed_features2.reshape(1, 1, -1, num_patches**2).permute(0,1,3,2)\n", " if FUSE_DINO: # enter \n", " img1_batch = extractor.preprocess_pil(img1)\n", " img1_desc_dino = extractor.extract_descriptors(img1_batch.to(device), layer, facet)\n", " img2_batch = extractor.preprocess_pil(img2)\n", " img2_desc_dino = extractor.extract_descriptors(img2_batch.to(device), layer, facet)\n", "\n", " if dist == 'l1' or dist == 'l2': # enter\n", " # normalize the features\n", " img1_desc = img1_desc / img1_desc.norm(dim=-1, keepdim=True)\n", " img2_desc = img2_desc / img2_desc.norm(dim=-1, keepdim=True)\n", " if FUSE_DINO:\n", " img1_desc_dino = img1_desc_dino / img1_desc_dino.norm(dim=-1, keepdim=True)\n", " img2_desc_dino = img2_desc_dino / img2_desc_dino.norm(dim=-1, keepdim=True)\n", "\n", " if FUSE_DINO and not ONLY_DINO: # true \n", " # cat two features together\n", " img1_desc = torch.cat((img1_desc, img1_desc_dino), dim=-1)\n", " img2_desc = torch.cat((img2_desc, img2_desc_dino), dim=-1)\n", "\n", " if ONLY_DINO:\n", " img1_desc = img1_desc_dino\n", " img2_desc = img2_desc_dino\n", "\n", " if DRAW_DENSE:\n", " if not Anno:\n", " mask1 = get_mask(model, aug, img1, category[0])\n", " mask2 = get_mask(model, aug, img2, category[-1])\n", " if Anno:\n", " mask1 = torch.Tensor(resize(img1, img_size, resize=True, to_pil=False, edge=EDGE_PAD).mean(-1)>0).to(device)\n", " mask2 = torch.Tensor(resize(img2, img_size, resize=True, to_pil=False, edge=EDGE_PAD).mean(-1)>0).to(device)\n", " print(mask1.shape, mask2.shape,mask1.sum(), mask2.sum())\n", " if ONLY_DINO or not FUSE_DINO:\n", " img1_desc = img1_desc / img1_desc.norm(dim=-1, keepdim=True)\n", " img2_desc = img2_desc / img2_desc.norm(dim=-1, keepdim=True)\n", " \n", " img1_desc_reshaped = img1_desc.permute(0,1,3,2).reshape(-1, img1_desc.shape[-1], num_patches, num_patches)\n", " img2_desc_reshaped = img2_desc.permute(0,1,3,2).reshape(-1, img2_desc.shape[-1], num_patches, num_patches)\n", " trg_dense_output, src_color_map = find_nearest_patchs(mask2, mask1, img2, img1, img2_desc_reshaped, img1_desc_reshaped, mask=mask)\n", "\n", " if not os.path.exists(f'{save_path}/{category[0]}'):\n", " os.makedirs(f'{save_path}/{category[0]}')\n", " fig_colormap, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 8))\n", " ax1.axis('off')\n", " ax2.axis('off')\n", " ax1.imshow(src_color_map)\n", " ax2.imshow(trg_dense_output)\n", " fig_colormap.savefig(f'{save_path}/{category[0]}/{pair_idx}_colormap.png')\n", " plt.close(fig_colormap)\n", " \n", " if DRAW_SWAP:\n", " if not DRAW_DENSE:\n", " mask1 = get_mask(model, aug, img1, category[0])\n", " mask2 = get_mask(model, aug, img2, category[-1])\n", "\n", " if (ONLY_DINO or not FUSE_DINO) and not DRAW_DENSE:\n", " img1_desc = img1_desc / img1_desc.norm(dim=-1, keepdim=True)\n", " img2_desc = img2_desc / img2_desc.norm(dim=-1, keepdim=True)\n", " \n", " img1_desc_reshaped = img1_desc.permute(0,1,3,2).reshape(-1, img1_desc.shape[-1], num_patches, num_patches)\n", " img2_desc_reshaped = img2_desc.permute(0,1,3,2).reshape(-1, img2_desc.shape[-1], num_patches, num_patches)\n", " trg_dense_output, src_color_map = find_nearest_patchs_replace(mask2, mask1, img2, img1, img2_desc_reshaped, img1_desc_reshaped, mask=mask, resolution=156)\n", " if not os.path.exists(f'{save_path}/{category[0]}'):\n", " os.makedirs(f'{save_path}/{category[0]}')\n", " fig_colormap, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 8))\n", " ax1.axis('off')\n", " ax2.axis('off')\n", " ax1.imshow(src_color_map)\n", " ax2.imshow(trg_dense_output)\n", " fig_colormap.savefig(f'{save_path}/{category[0]}/{pair_idx}_swap.png')\n", " plt.close(fig_colormap)\n", " if not DRAW_SWAP and not DRAW_DENSE:\n", " result.append([img1_desc.cpu(), img2_desc.cpu()])\n", " else:\n", " result.append([img1_desc.cpu(), img2_desc.cpu(), mask1.cpu(), mask2.cpu()])\n", "\n", " pbar.update(1)\n", " return result" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def vis_pca_mask(result,save_path):\n", " # PCA visualization mask version\n", " for (feature1,feature2,mask1,mask2) in result:\n", " # feature1 shape (1,1,3600,768*2)\n", " # feature2 shape (1,1,3600,768*2)\n", " num_patches = int(math.sqrt(feature1.shape[-2]))\n", " # pca the concatenated feature to 3 dimensions\n", " feature1 = feature1.squeeze() # shape (3600,768*2)\n", " feature2 = feature2.squeeze() # shape (3600,768*2)\n", " chennel_dim = feature1.shape[-1]\n", " # resize back\n", " src_feature_reshaped = feature1.squeeze().permute(1,0).reshape(-1,num_patches,num_patches).cuda()\n", " tgt_feature_reshaped = feature2.squeeze().permute(1,0).reshape(-1,num_patches,num_patches).cuda()\n", " resized_src_mask = F.interpolate(mask1.unsqueeze(0).unsqueeze(0), size=(num_patches, num_patches), mode='nearest').squeeze().cuda()\n", " resized_tgt_mask = F.interpolate(mask2.unsqueeze(0).unsqueeze(0), size=(num_patches, num_patches), mode='nearest').squeeze().cuda()\n", " src_feature_upsampled = src_feature_reshaped * resized_src_mask.repeat(src_feature_reshaped.shape[0],1,1)\n", " tgt_feature_upsampled = tgt_feature_reshaped * resized_tgt_mask.repeat(src_feature_reshaped.shape[0],1,1)\n", " feature1=src_feature_upsampled.reshape(chennel_dim,-1).permute(1,0)\n", " feature2=tgt_feature_upsampled.reshape(chennel_dim,-1).permute(1,0)\n", "\n", " n_components=4 # the first component is to seperate the object from the background\n", " pca = sklearnPCA(n_components=n_components)\n", " feature1_n_feature2 = torch.cat((feature1,feature2),dim=0) # shape (7200,768*2)\n", " feature1_n_feature2 = pca.fit_transform(feature1_n_feature2.cpu().numpy()) # shape (7200,3)\n", " feature1 = feature1_n_feature2[:feature1.shape[0],:] # shape (3600,3)\n", " feature2 = feature1_n_feature2[feature1.shape[0]:,:] # shape (3600,3)\n", " \n", " \n", " fig, axes = plt.subplots(4, 2, figsize=(10, 14))\n", " for show_channel in range(n_components):\n", " if show_channel==0:\n", " continue\n", " # min max normalize the feature map\n", " feature1[:, show_channel] = (feature1[:, show_channel] - feature1[:, show_channel].min()) / (feature1[:, show_channel].max() - feature1[:, show_channel].min())\n", " feature2[:, show_channel] = (feature2[:, show_channel] - feature2[:, show_channel].min()) / (feature2[:, show_channel].max() - feature2[:, show_channel].min())\n", " feature1_first_channel = feature1[:, show_channel].reshape(num_patches,num_patches)\n", " feature2_first_channel = feature2[:, show_channel].reshape(num_patches,num_patches)\n", "\n", " axes[show_channel-1, 0].imshow(feature1_first_channel)\n", " axes[show_channel-1, 0].axis('off')\n", " axes[show_channel-1, 1].imshow(feature2_first_channel)\n", " axes[show_channel-1, 1].axis('off')\n", " axes[show_channel-1, 0].set_title('Feature 1 - Channel {}'.format(show_channel ), fontsize=14)\n", " axes[show_channel-1, 1].set_title('Feature 2 - Channel {}'.format(show_channel ), fontsize=14)\n", "\n", "\n", " feature1_resized = feature1[:, 1:4].reshape(num_patches,num_patches, 3)\n", " feature2_resized = feature2[:, 1:4].reshape(num_patches,num_patches, 3)\n", "\n", " axes[3, 0].imshow(feature1_resized)\n", " axes[3, 0].axis('off')\n", " axes[3, 1].imshow(feature2_resized)\n", " axes[3, 1].axis('off')\n", " axes[3, 0].set_title('Feature 1 - All Channels', fontsize=14)\n", " axes[3, 1].set_title('Feature 2 - All Channels', fontsize=14)\n", "\n", " plt.tight_layout()\n", " plt.show()\n", " fig.savefig(save_path+'/masked_pca.png', dpi=300)\n", "\n", "def vis_pca(result,save_path,src_img_path,trg_img_path):\n", " # PCA visualization\n", " for (feature1,feature2,mask1,mask2) in result:\n", " # feature1 shape (1,1,3600,768*2)\n", " # feature2 shape (1,1,3600,768*2)\n", " num_patches=int(math.sqrt(feature1.shape[2]))\n", " # pca the concatenated feature to 3 dimensions\n", " feature1 = feature1.squeeze() # shape (3600,768*2)\n", " feature2 = feature2.squeeze() # shape (3600,768*2)\n", " chennel_dim = feature1.shape[-1]\n", " # resize back\n", " h1, w1 = Image.open(src_img_path).size\n", " scale_h1 = h1/num_patches\n", " scale_w1 = w1/num_patches\n", " \n", " if scale_h1 > scale_w1:\n", " scale = scale_h1\n", " scaled_w = int(w1/scale)\n", " feature1 = feature1.reshape(num_patches,num_patches,chennel_dim)\n", " feature1_uncropped=feature1[(num_patches-scaled_w)//2:num_patches-(num_patches-scaled_w)//2,:,:]\n", " else:\n", " scale = scale_w1\n", " scaled_h = int(h1/scale)\n", " feature1 = feature1.reshape(num_patches,num_patches,chennel_dim)\n", " feature1_uncropped=feature1[:,(num_patches-scaled_h)//2:num_patches-(num_patches-scaled_h)//2,:]\n", " \n", " h2, w2 = Image.open(trg_img_path).size\n", " scale_h2 = h2/num_patches\n", " scale_w2 = w2/num_patches\n", " if scale_h2 > scale_w2:\n", " scale = scale_h2\n", " scaled_w = int(w2/scale)\n", " feature2 = feature2.reshape(num_patches,num_patches,chennel_dim)\n", " feature2_uncropped=feature2[(num_patches-scaled_w)//2:num_patches-(num_patches-scaled_w)//2,:,:]\n", " else:\n", " scale = scale_w2\n", " scaled_h = int(h2/scale)\n", " feature2 = feature2.reshape(num_patches,num_patches,chennel_dim)\n", " feature2_uncropped=feature2[:,(num_patches-scaled_h)//2:num_patches-(num_patches-scaled_h)//2,:]\n", "\n", " f1_shape=feature1_uncropped.shape[:2]\n", " f2_shape=feature2_uncropped.shape[:2]\n", " feature1 = feature1_uncropped.reshape(f1_shape[0]*f1_shape[1],chennel_dim)\n", " feature2 = feature2_uncropped.reshape(f2_shape[0]*f2_shape[1],chennel_dim)\n", " n_components=3\n", " pca = sklearnPCA(n_components=n_components)\n", " feature1_n_feature2 = torch.cat((feature1,feature2),dim=0) # shape (7200,768*2)\n", " feature1_n_feature2 = pca.fit_transform(feature1_n_feature2.cpu().numpy()) # shape (7200,3)\n", " feature1 = feature1_n_feature2[:feature1.shape[0],:] # shape (3600,3)\n", " feature2 = feature1_n_feature2[feature1.shape[0]:,:] # shape (3600,3)\n", " \n", " \n", " fig, axes = plt.subplots(4, 2, figsize=(10, 14))\n", " for show_channel in range(n_components):\n", " # min max normalize the feature map\n", " feature1[:, show_channel] = (feature1[:, show_channel] - feature1[:, show_channel].min()) / (feature1[:, show_channel].max() - feature1[:, show_channel].min())\n", " feature2[:, show_channel] = (feature2[:, show_channel] - feature2[:, show_channel].min()) / (feature2[:, show_channel].max() - feature2[:, show_channel].min())\n", " feature1_first_channel = feature1[:, show_channel].reshape(f1_shape[0], f1_shape[1])\n", " feature2_first_channel = feature2[:, show_channel].reshape(f2_shape[0], f2_shape[1])\n", "\n", " axes[show_channel, 0].imshow(feature1_first_channel)\n", " axes[show_channel, 0].axis('off')\n", " axes[show_channel, 1].imshow(feature2_first_channel)\n", " axes[show_channel, 1].axis('off')\n", " axes[show_channel, 0].set_title('Feature 1 - Channel {}'.format(show_channel + 1), fontsize=14)\n", " axes[show_channel, 1].set_title('Feature 2 - Channel {}'.format(show_channel + 1), fontsize=14)\n", "\n", "\n", " feature1_resized = feature1[:, :3].reshape(f1_shape[0], f1_shape[1], 3)\n", " feature2_resized = feature2[:, :3].reshape(f2_shape[0], f2_shape[1], 3)\n", "\n", " axes[3, 0].imshow(feature1_resized)\n", " axes[3, 0].axis('off')\n", " axes[3, 1].imshow(feature2_resized)\n", " axes[3, 1].axis('off')\n", " axes[3, 0].set_title('Feature 1 - All Channels', fontsize=14)\n", " axes[3, 1].set_title('Feature 2 - All Channels', fontsize=14)\n", "\n", " plt.tight_layout()\n", " plt.show()\n", " fig.savefig(save_path+'/pca.png', dpi=300)\n", "\n", "\n", "def perform_clustering(features, n_clusters=10):\n", " # Normalize features\n", " features = F.normalize(features, p=2, dim=1)\n", " # Convert the features to float32\n", " features = features.cpu().detach().numpy().astype('float32')\n", " # Initialize a k-means clustering index with the desired number of clusters\n", " kmeans = KMeans(n_clusters=n_clusters, random_state=0)\n", " # Train the k-means index with the features\n", " kmeans.fit(features)\n", " # Assign the features to their nearest cluster\n", " labels = kmeans.predict(features)\n", "\n", " return labels\n", "\n", "def cluster_and_match(result, save_path, n_clusters=6):\n", " for (feature1,feature2,mask1,mask2) in result:\n", " # feature1 shape (1,1,3600,768*2)\n", " num_patches = int(math.sqrt(feature1.shape[-2]))\n", " # pca the concatenated feature to 3 dimensions\n", " feature1 = feature1.squeeze() # shape (3600,768*2)\n", " feature2 = feature2.squeeze() # shape (3600,768*2)\n", " chennel_dim = feature1.shape[-1]\n", " # resize back\n", "\n", " src_feature_reshaped = feature1.squeeze().permute(1,0).reshape(-1,num_patches,num_patches).cuda()\n", " tgt_feature_reshaped = feature2.squeeze().permute(1,0).reshape(-1,num_patches,num_patches).cuda()\n", " resized_src_mask = F.interpolate(mask1.unsqueeze(0).unsqueeze(0), size=(num_patches, num_patches), mode='nearest').squeeze().cuda()\n", " resized_tgt_mask = F.interpolate(mask2.unsqueeze(0).unsqueeze(0), size=(num_patches, num_patches), mode='nearest').squeeze().cuda()\n", " src_feature_upsampled = src_feature_reshaped * resized_src_mask.repeat(src_feature_reshaped.shape[0],1,1)\n", " tgt_feature_upsampled = tgt_feature_reshaped * resized_tgt_mask.repeat(src_feature_reshaped.shape[0],1,1)\n", "\n", " feature1=src_feature_upsampled.unsqueeze(0)\n", " feature2=tgt_feature_upsampled.unsqueeze(0)\n", " \n", " w1, h1 = feature1.shape[2], feature1.shape[3]\n", " w2, h2 = feature2.shape[2], feature2.shape[3]\n", "\n", " features1_2d = feature1.reshape(feature1.shape[1], -1).permute(1, 0)\n", " features2_2d = feature2.reshape(feature2.shape[1], -1).permute(1, 0)\n", "\n", " labels_img1 = perform_clustering(features1_2d, n_clusters)\n", " labels_img2 = perform_clustering(features2_2d, n_clusters)\n", "\n", " cluster_means_img1 = [features1_2d.cpu().detach().numpy()[labels_img1 == i].mean(axis=0) for i in range(n_clusters)]\n", " cluster_means_img2 = [features2_2d.cpu().detach().numpy()[labels_img2 == i].mean(axis=0) for i in range(n_clusters)]\n", "\n", " distances = np.linalg.norm(np.expand_dims(cluster_means_img1, axis=1) - np.expand_dims(cluster_means_img2, axis=0), axis=-1)\n", " # Use Hungarian algorithm to find the optimal bijective mapping\n", " row_ind, col_ind = linear_sum_assignment(distances)\n", "\n", " relabeled_img2 = np.zeros_like(labels_img2)\n", " for i, match in zip(row_ind, col_ind):\n", " relabeled_img2[labels_img2 == match] = i\n", "\n", " labels_img1 = labels_img1.reshape(w1, h1)\n", " relabeled_img2 = relabeled_img2.reshape(w2, h2)\n", " fig, axs = plt.subplots(1, 2, figsize=(10, 5))\n", " # Plot the results\n", " ax_img1 = axs[0]\n", " axs[0].axis('off')\n", " ax_img1.imshow(labels_img1, cmap='tab20')\n", " \n", " ax_img2 = axs[1]\n", " axs[1].axis('off')\n", " ax_img2.imshow(relabeled_img2, cmap='tab20')\n", "\n", " plt.tight_layout()\n", " plt.show()\n", " fig.savefig(save_path+'/clustering.png', dpi=300)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def process_images(src_img_path,trg_img_path):\n", "\n", " categories = [['dog'], ['dog']]\n", " files = [src_img_path, trg_img_path]\n", " save_path = './results_vis' + f'/{trg_img_path.split(\"/\")[-1].split(\".\")[0]}_{src_img_path.split(\"/\")[-1].split(\".\")[0]}'\n", " result = compute_pair_feature(model, aug, save_path, files, mask=MASK, category=categories, dist=DIST)\n", " # if MASK:\n", " # vis_pca_mask(result, save_path)\n", " # cluster_and_match(result, save_path)\n", " # if 'Anno' not in src_img_path:\n", " # vis_pca(result, save_path,src_img_path,trg_img_path)\n", "\n", " return result\n", "print(model.get_features)\n", "src_img_path = \"demo_images/dog.jpg\"\n", "trg_img_path = \"demo_images/whitedog.jpg\"\n", "# result = process_images(src_img_path, trg_img_path)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "src_img_path = \"data/images/dog_Anno_00.png\"\n", "trg_img_path = \"data/images/dog_Anno_59.png\"\n", "result = process_images(src_img_path, trg_img_path)" ] } ], "metadata": { "kernelspec": { "display_name": "declip", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.0" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }