Spaces:
Sleeping
Sleeping
File size: 7,603 Bytes
02ba63a 19d010a 8ecd333 19d010a 26ba1d3 02ba63a 19d010a 02ba63a 00e1057 50f5011 02ba63a 00e1057 26ba1d3 02ba63a 19d010a 02ba63a 19d010a 02ba63a 19d010a 02ba63a 26ba1d3 19d010a 02ba63a 8ecd333 00e1057 8ecd333 23281d5 26ba1d3 50f5011 156c303 26ba1d3 00e1057 26ba1d3 156c303 19d010a 02ba63a 01a01d7 19d010a 01a01d7 2e99060 19d010a d9cd7b1 19d010a 2e99060 19d010a 2e99060 19d010a 2e99060 ff3dcda 00e1057 ff3dcda 9d42859 02ba63a |
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 |
import gradio as gr
import torch
import torch.nn.functional as F
from torch.utils.data import DataLoader
import matplotlib.pyplot as plt
from model_module import AutoencoderModule
import numpy as np
from PIL import Image
import base64
from io import BytesIO
import dataset
from dataset import MyDataset, ImageKeypointDataset, load_filenames, load_keypoints
import utils
import spaces
def load_model(model_path, feature_dim):
model = AutoencoderModule(feature_dim=feature_dim)
state_dict = torch.load(model_path)
if "state_dict" in state_dict:
model.load_state_dict(state_dict['state_dict'])
model.eval()
else:
# 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(new_state_dict)
model.eval()
model.to(device)
print(f"{model_path} loaded successfully.")
return model
image_size = 112
batch_size = 32
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
models_info = [{"name": "ae_model_tf_2024-03-05_00-35-21.pth", "feature_dim": 32},
{"name": "autoencoder-epoch=09-train_loss=1.00.ckpt", "feature_dim": 64},
{"name": "autoencoder-epoch=29-train_loss=1.01.ckpt", "feature_dim": 64},
{"name": "autoencoder-epoch=49-train_loss=1.01.ckpt", "feature_dim": 64}]
models = []
for model_info in models_info:
model_name = model_info["name"]
feature_dim = model_info["feature_dim"]
model_path = f"checkpoints/{model_name}"
model = load_model(model_path, feature_dim)
models.append(model)
def load_data(img_dir="resources/trainB/", image_size=112, batch_size=256):
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
def load_keypoints(img_dir="resources/trainB/", image_size=112, batch_size=32):
filenames = load_filenames(img_dir)
train_X = filenames[:1000]
keypoints = dataset.load_keypoints('resources/DataList.json')
image_points_ds = ImageKeypointDataset(train_X, keypoints, img_dir='resources/trainB/', img_size=image_size)
image_points_loader = DataLoader(image_points_ds, batch_size=batch_size, shuffle=False)
iterator = iter(image_points_loader)
test_imgs, points = next(iterator)
test_imgs = test_imgs.to(device)
points = points.to(device)*(image_size)
print("Keypoints loaded successfully.")
return test_imgs, points
# ヒートマップの生成関数
@spaces.GPU
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)
dec5, _ = model(x)
feature_map = dec5
# アップロード画像の前処理
if uploaded_image is not None:
uploaded_image = utils.preprocess_uploaded_image(uploaded_image['composite'], image_size)
else:
uploaded_image = torch.zeros(1, 3, image_size, image_size).to(device)
target_feature_map, _ = model(uploaded_image)
img = torch.cat((x, uploaded_image))
feature_map = torch.cat((feature_map, target_feature_map))
source_map, target_map, blended_source, blended_target = utils.get_heatmaps(img, feature_map, source_num, x_coords, y_coords, uploaded_image)
keypoint_maps, blended_tensors = utils.get_keypoint_heatmaps(target_feature_map, mean_vector_list, points.size(1), uploaded_image)
# Matplotlibでプロットして画像として保存
fig, axs = plt.subplots(2, 3, figsize=(10, 6))
axs[0, 0].imshow(source_map, cmap='hot')
axs[0, 0].set_title("Source Map")
axs[0, 1].imshow(target_map, cmap='hot')
axs[0, 1].set_title("Target Map")
axs[0, 2].imshow(keypoint_maps[0], cmap='hot')
axs[0, 2].set_title("Keypoint Map")
axs[1, 0].imshow(blended_source.permute(1, 2, 0))
axs[1, 0].set_title("Blended Source")
axs[1, 1].imshow(blended_target.permute(1, 2, 0))
axs[1, 1].set_title("Blended Target")
axs[1, 2].imshow(blended_tensors[0].permute(1, 2, 0))
axs[1, 2].set_title("Blended Keypoint")
for ax in axs.flat:
ax.axis('off')
plt.tight_layout()
plt.close(fig)
return fig
def setup(model_info, input_image=None):
global model, x, test_imgs, points, mean_vector_list
# str -> dictに変換
if type(model_info) == str:
model_info = eval(model_info)
index = models_info.index(model_info)
model = models[index]
x = load_data()
test_imgs, points = load_keypoints()
feature_map, _ = model(test_imgs)
mean_vector_list = utils.get_mean_vector(feature_map, points)
if input_image is not None:
fig = get_heatmaps(0, image_size // 2, image_size // 2, input_image)
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.")
gr.Markdown("## Heatmap Visualization")
input_image = gr.ImageEditor(label="Cropped Image", elem_id="input_image", crop_size=(112, 112), show_fullscreen_button=True)
output_plot = gr.Plot(value=None, elem_id="output_plot", show_label=False)
with gr.Row():
with gr.Column():
with gr.Row():
model_name = gr.Dropdown(
choices=[str(model_info) for model_info in models_info],
container=False
)
load_button = gr.Button("Load Model")
load_button.click(setup, inputs=[model_name, input_image], outputs=[output_plot])
with gr.Row():
pass
inference = 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=output_plot,
live=True,
flagging_mode="never"
)
# examples
gr.Markdown("# Examples")
gr.Examples(
examples=[
["resources/examples/2488.jpg"],
["resources/examples/2899.jpg"]
],
inputs=[input_image],
)
if __name__ == "__main__":
setup(models_info[0])
# JavaScriptコードをロード
demo.launch()
|