File size: 6,803 Bytes
02ba63a
0036fb1
02ba63a
 
 
 
 
 
 
 
 
 
 
 
 
01a01d7
 
02ba63a
 
 
01a01d7
 
 
 
 
 
02ba63a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01a01d7
 
 
02ba63a
 
 
 
 
 
 
0036fb1
02ba63a
01a01d7
 
 
 
 
 
 
 
 
02ba63a
 
 
 
 
 
 
 
 
2e99060
02ba63a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01a01d7
02ba63a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01a01d7
 
 
 
 
 
 
 
 
 
2e99060
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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

# ヒートマップの生成関数
@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)
    
    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()