Spaces:
Sleeping
Sleeping
import gradio as gr | |
import spaces | |
import torch | |
import torch.nn.functional as F | |
from torch.utils.data import DataLoader | |
import matplotlib.pyplot as plt | |
from model_module import AutoencoderModule | |
from dataset import MyDataset, load_filenames | |
import numpy as np | |
from PIL import Image | |
import base64 | |
from io import BytesIO | |
# モデルとデータの読み込み | |
def load_model(): | |
model_path = "checkpoints/autoencoder-epoch=49-train_loss=1.01.ckpt" | |
feature_dim = 64 | |
model = AutoencoderModule(feature_dim=feature_dim) | |
state_dict = torch.load(model_path) | |
# # state_dict のキーを修正 | |
# new_state_dict = {} | |
# for key in state_dict: | |
# new_key = "model." + key | |
# new_state_dict[new_key] = state_dict[key] | |
model.load_state_dict(state_dict['state_dict']) | |
model.eval() | |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
model.to(device) | |
print("Model loaded successfully.") | |
return model, device | |
def load_data(device, img_dir="resources/trainB/", image_size=112, batch_size=32): | |
filenames = load_filenames(img_dir) | |
train_X = filenames[:1000] | |
train_ds = MyDataset(train_X, img_dir=img_dir, img_size=image_size) | |
train_loader = DataLoader( | |
train_ds, | |
batch_size=batch_size, | |
shuffle=True, | |
num_workers=0, | |
) | |
iterator = iter(train_loader) | |
x, _, _ = next(iterator) | |
x = x.to(device) | |
x = x[:,0].to(device) | |
print("Data loaded successfully.") | |
return x | |
model, device = load_model() | |
image_size = 112 | |
batch_size = 32 | |
x = load_data(device) | |
# アップロード画像の前処理 | |
def preprocess_uploaded_image(uploaded_image, image_size): | |
# ndarrayの場合はPILイメージに変換 | |
if type(uploaded_image) == np.ndarray: | |
uploaded_image = Image.fromarray(uploaded_image) | |
uploaded_image = uploaded_image.convert("RGB") | |
uploaded_image = uploaded_image.resize((image_size, image_size)) | |
uploaded_image = np.array(uploaded_image).transpose(2, 0, 1) / 255.0 | |
uploaded_image = torch.tensor(uploaded_image, dtype=torch.float32).unsqueeze(0).to(device) | |
return uploaded_image | |
# ヒートマップの生成関数 | |
def get_heatmaps(source_num, x_coords, y_coords, uploaded_image): | |
if type(uploaded_image) == str: | |
uploaded_image = Image.open(uploaded_image) | |
if type(source_num) == str: | |
source_num = int(source_num) | |
if type(x_coords) == str: | |
x_coords = int(x_coords) | |
if type(y_coords) == str: | |
y_coords = int(y_coords) | |
with torch.no_grad(): | |
dec5, _ = model(x) | |
img = x | |
feature_map = dec5 | |
batch_size = feature_map.size(0) | |
feature_dim = feature_map.size(1) | |
# アップロード画像の前処理 | |
if uploaded_image is not None: | |
uploaded_image = preprocess_uploaded_image(uploaded_image['composite'], image_size) | |
target_feature_map, _ = model(uploaded_image) | |
img = torch.cat((img, uploaded_image)) | |
feature_map = torch.cat((feature_map, target_feature_map)) | |
batch_size += 1 | |
else: | |
uploaded_image = torch.zeros(1, 3, image_size, image_size, device=device) | |
target_num = batch_size - 1 | |
x_coords = [x_coords] * batch_size | |
y_coords = [y_coords] * batch_size | |
vectors = feature_map[torch.arange(feature_map.size(0)), :, y_coords, x_coords] | |
vector = vectors[source_num] | |
reshaped_feature_map = feature_map.permute(0, 2, 3, 1).view(feature_map.size(0), -1, feature_dim) | |
batch_distance_map = F.pairwise_distance(reshaped_feature_map, vector).view(feature_map.size(0), image_size, image_size) | |
norm_batch_distance_map = 1 / torch.cosh(20 * (batch_distance_map - batch_distance_map.min()) / (batch_distance_map.max() - batch_distance_map.min())) ** 2 | |
source_map = norm_batch_distance_map[source_num] | |
target_map = norm_batch_distance_map[target_num] | |
alpha = 0.7 | |
blended_source = (1 - alpha) * img[source_num] + alpha * torch.cat(((norm_batch_distance_map[source_num] / norm_batch_distance_map[source_num].max()).unsqueeze(0), torch.zeros(2, image_size, image_size, device=device))) | |
blended_target = (1 - alpha) * img[target_num] + alpha * torch.cat(((norm_batch_distance_map[target_num] / norm_batch_distance_map[target_num].max()).unsqueeze(0), torch.zeros(2, image_size, image_size, device=device))) | |
# Matplotlibでプロットして画像として保存 | |
fig, axs = plt.subplots(2, 2, figsize=(10, 10)) | |
axs[0, 0].imshow(source_map.cpu(), cmap='hot') | |
axs[0, 0].set_title("Source Map") | |
axs[0, 1].imshow(target_map.cpu(), cmap='hot') | |
axs[0, 1].set_title("Target Map") | |
axs[1, 0].imshow(blended_source.permute(1, 2, 0).cpu()) | |
axs[1, 0].set_title("Blended Source") | |
axs[1, 1].imshow(blended_target.permute(1, 2, 0).cpu()) | |
axs[1, 1].set_title("Blended Target") | |
for ax in axs.flat: | |
ax.axis('off') | |
plt.tight_layout() | |
plt.close(fig) | |
return fig | |
with gr.Blocks() as demo: | |
# title | |
gr.Markdown("# TripletGeoEncoder Feature Map Visualization") | |
# description | |
gr.Markdown("This demo visualizes the feature maps of a TripletGeoEncoder trained on the CelebA dataset using self-supervised learning without annotations from only 1000 images. " | |
"The feature maps are visualized as heatmaps, where the source map shows the distance of each pixel in the source image to the selected pixel, and the target map shows the distance of each pixel in the target image to the selected pixel. " | |
"The blended source and target images show the source and target images with the source and target maps overlaid, respectively. " | |
"For further information, please contact me on X (formerly Twitter): @Yeq6X.") | |
input_image = gr.ImageEditor(label="Cropped Image", elem_id="input_image", crop_size=(112, 112), show_fullscreen_button=True) | |
gr.Interface( | |
get_heatmaps, | |
inputs=[ | |
gr.Slider(0, batch_size - 1, step=1, label="Source Image Index"), | |
gr.Slider(0, image_size - 1, step=1, value=image_size // 2, label="X Coordinate"), | |
gr.Slider(0, image_size - 1, step=1, value=image_size // 2, label="Y Coordinate"), | |
input_image | |
], | |
outputs="plot", | |
live=True, | |
) | |
# examples | |
gr.Markdown("# Examples") | |
gr.Examples( | |
examples=[ | |
["resources/examples/2488.jpg"], | |
["resources/examples/2899.jpg"] | |
], | |
inputs=[input_image], | |
) | |
# JavaScriptコードをロード | |
demo.launch() | |