-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpatch_fpvt_vis.py
179 lines (149 loc) · 5.22 KB
/
patch_fpvt_vis.py
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
import os
import matplotlib.pyplot as plt
import numpy as np
import PIL
import torch
import torch.nn.functional as F
import torchvision
import torchvision.transforms as T
from timm import create_model
from PIL import Image
from vit_pytorch.pvt_v2 import PyramidVisionTransformerV2
device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
print("device = ", device)
'''
Original implementation
image_Size=224,224
1) Split Image into Patches
The input image is split into 14 x 14 vectors with dimension of 768 by Conv2d (k=16x16) with stride=(16, 16).
2) Add Position Embeddings
Learnable position embedding vectors are added to the patch embedding vectors and fed to the transformer encoder.
3) Transformer Encoder
The embedding vectors are encoded by the transformer encoder. The dimension of input and output vectors are the same.
4) MLP (Classification)
Head The 0th output from the encoder is fed to the MLP head for classification to output the final classification results.
'''
path = 'results/ours_224/Backbone_PVTV2_Epoch_1_Batch_2860_Time_2022-06-24-01-00_checkpoint.pth'
model = PyramidVisionTransformerV2(
loss_type='ArcMarginProduct',
GPU_ID=0,
img_size=224,
depths=[3, 4, 18, 3],
patch_size=16,
num_classes=526,
in_chans=3
)
model.load_state_dict(torch.load(path))
IMG_SIZE = (224, 224)
NORMALIZE_MEAN = (0.5, 0.5, 0.5)
NORMALIZE_STD = (0.5, 0.5, 0.5)
transforms = [
T.Resize(IMG_SIZE),
T.ToTensor(),
T.Normalize(NORMALIZE_MEAN, NORMALIZE_STD),
]
transforms = T.Compose(transforms)
# Demo Image
img = PIL.Image.open('Adam_Brody_233.png')
img = img.resize((224, 224), Image.ANTIALIAS)
img_tensor = transforms(img).unsqueeze(0)
# end-to-end inference
output = model.patch_embed1(img_tensor)
# Patch embed 1
patches = model.patch_embed1(img_tensor) # patch embedding convolution
print("Image tensor: ", patches) # 28x28=784
# Image tensor: torch.Size([1, 3, 112, 112])
print("Patch embeddings: ", model.patch_embed1)
# this value coming from forward pass of Ours_FPVT torch.Size([1, 784, 64])
fig = plt.figure(figsize=(8, 8))
fig.suptitle("Visualization of today_224_patch_embed1.png", fontsize=24)
fig.add_axes()
img = np.asarray(img)
# Patch embed 1
for i in range(0, 3136):
x = i % 56
y = i // 56
patch = img[y * 4:(y + 1) * 4, x * 4:(x + 1) * 4]
ax = fig.add_subplot(56, 56, i + 1)
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)
ax.imshow(patch)
plt.savefig('today_224_patch_embed1.png', figsize=(224, 224))
'''
# patch embed 2
model.patch_embed2.img_size = 112 // 4
model.patch_embed2.patch_size = 3
model.patch_embed2.stride = 2
model.patch_embed2.in_chans = 64
model.patch_embed2.embed_dim = 128
patches = model.patch_embed2
print(patches.H)
# torch.Size([1, 196, 128])
fig = plt.figure(figsize=(8, 8))
fig.suptitle("Visualization of patch_embed2", fontsize=24)
fig.add_axes()
img = np.asarray(img)
# model.patch_embed2.proj----> Conv2d(64, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
# Patch embed 2
for i in range(0, 196): # 28 28 (number of patches in width and height) 112/4=28
x = i % 14
y = i // 14
patch = img[y * 2:(y + 1) * 2, x * 2:(x + 1) * 2]
ax = fig.add_subplot(14, 14, i + 1)
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)
ax.imshow(patch)
plt.savefig('patch_embed2_full.png', figsize=(112, 112))
output = model.patch_embed1(img_tensor)
# patch embed 3
model.patch_embed3.img_size = 112 // 8
model.patch_embed3.patch_size = 3
model.patch_embed3.stride = 2
model.patch_embed3.in_chans = 128
model.patch_embed3.embed_dim = 256
patches = model.patch_embed3
print(patches.H)
# torch.Size([1, 196, 128])
fig = plt.figure(figsize=(8, 8))
fig.suptitle("Visualization of patch_embed3", fontsize=24)
fig.add_axes()
img = np.asarray(img)
# model.patch_embed3.proj----> Conv2d(128, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
# Patch embed 2
# forwaRD PASS torch.Size([1, 49, 256])
for i in range(0, 49): # 7 7 (number of patches in width and height) 112/4=28
x = i % 7
y = i // 7
patch = img[y * 2:(y + 1) * 2, x * 2:(x + 1) * 2]
ax = fig.add_subplot(7, 7, i + 1)
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)
ax.imshow(patch)
plt.savefig('patch_embed3.png', figsize=(112, 112))
output = model.patch_embed1(img_tensor)
# patch embed 3
model.patch_embed4.img_size = 112 // 16
model.patch_embed4.patch_size = 3
model.patch_embed4.stride = 2
model.patch_embed4.in_chans = 256
model.patch_embed4.embed_dim = 512
patches = model.patch_embed3
print(patches.H)
# torch.Size([1, 196, 128])
fig = plt.figure(figsize=(8, 8))
fig.suptitle("Visualization of patch_embed3", fontsize=24)
fig.add_axes()
img = np.asarray(img)
# model.patch_embed4.proj----> Conv2d(256, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
# Patch embed 2
# stage 4: torch.Size([1, 16, 512])
for i in range(0, 16): # 7 7 (number of patches in width and height) 112/4=28
x = i % 4
y = i // 4
patch = img[y * 2:(y + 1) * 2, x * 2:(x + 1) * 2]
ax = fig.add_subplot(4, 4, i + 1)
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)
ax.imshow(patch)
plt.savefig('patch_embed4.png', figsize=(112, 112))
'''