-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
157 lines (120 loc) · 5.54 KB
/
model.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
import torch.nn as nn
import torch
import cv2
import matplotlib.pyplot as plt
from torchvision import transforms
from PIL import Image
from src.utils import square_euclidean_metric
""" Optional conv block """
def conv_block(in_channels, out_channels):
return nn.Sequential(
nn.Conv2d(in_channels, out_channels, 3, padding=1),
nn.BatchNorm2d(out_channels),
nn.ReLU(),
)
""" Define your own model """
def conv_block_new(in_channels, out_channels, kern_size, stride, padding):
return nn.Sequential(
nn.Conv2d(in_channels, out_channels, kern_size, stride=stride, padding=padding),
nn.BatchNorm2d(out_channels),
nn.ReLU(),
)
def conv_block_new_new(in_channels, out_channels, kern_size, stride, padding):
return nn.Sequential(
nn.Conv2d(in_channels, out_channels, kern_size, stride=stride, padding=padding),
nn.BatchNorm2d(out_channels),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.ReLU(),
)
class Flatten(nn.Module):
def __init__(self):
super(Flatten, self).__init__()
def forward(self, x):
return x.view(x.size(0), -1)
class ImageAndHistEncoder(nn.Module):
def __init__(self, x_dim=3, hid_dim=64, z_dim=64):
super().__init__()
self.layer1_in1 = conv_block_new_new(x_dim, int(hid_dim/2), 3, 1, 1)
self.layer2_in1 = conv_block_new_new(int(hid_dim/2), int(hid_dim/2), 3, 1, 1)
self.layer1_in2 = conv_block_new(1, int(hid_dim/2), 3, 1, 1)
self.layer2_in2 = conv_block_new(int(hid_dim/2), int(hid_dim/2), 3, 1, 1)
self.layer3 = conv_block_new_new(hid_dim, hid_dim, 3, 1, 1)
self.layer4 = conv_block_new_new(hid_dim, hid_dim, 3, 1, 1)
self.layer5 = conv_block_new(hid_dim, hid_dim, 3, 1, 1)
self.layer6 = conv_block_new_new(hid_dim, z_dim, 3, 1, 1)
def forward(self, data_shot, data_query):
x = self.layer1_in1(data_shot)
x = self.layer2_in1(x)
y = self.layer1_in2(data_query)
y = self.layer2_in2(y)
z = torch.cat([x, y], dim=1)
z = self.layer3(z)
z = self.layer4(z)
z = self.layer5(z)
z = self.layer6(z)
return z
class FewShotModel(nn.Module):
def __init__(self, x_dim=3, hid_dim=64, z_dim=64):
super().__init__()
self.encoder = ImageAndHistEncoder(x_dim=x_dim, hid_dim=hid_dim, z_dim=z_dim)
def forward(self, data_shot, data_query, data_shot_hist, data_query_hist): # 그냥 20개 classification 문제로 바꾸기
shot_vec = self.encoder(data_shot, data_shot_hist)
query_vec = self.encoder(data_query, data_query_hist)
shot_vec = shot_vec.reshape((25, -1))
shot_vec_mean = shot_vec.reshape((5, 5, -1)).mean(1)
query_vec = query_vec.reshape((20, -1))
embedding_vector_unavg = square_euclidean_metric(query_vec, shot_vec)
embedding_vector = square_euclidean_metric(query_vec, shot_vec_mean)
return embedding_vector, embedding_vector_unavg
class FirstFewShotModel(nn.Module):
def __init__(self, x_dim=3, hid_dim=64, z_dim=64):
super().__init__()
self.encoder = nn.Sequential(
conv_block_new(x_dim, hid_dim, 3, 3, 1),
conv_block_new(hid_dim, hid_dim, 3, 3, 1),
conv_block_new(hid_dim, hid_dim, 3, 3, 1),
conv_block_new(hid_dim, z_dim, 3, 3, 1),
Flatten()
)
def forward(self, data_shot, data_query):
shot_vec = self.encoder(data_shot)
query_vec = self.encoder(data_query)
shot_vec = shot_vec.reshape((5, 5, -1))
shot_vec = shot_vec.mean(1)
embedding_vector = square_euclidean_metric(query_vec, shot_vec)
return embedding_vector
class SecondFewShotModel(nn.Module):
def __init__(self, x_dim=3, hid_dim=64, z_dim=64):
super().__init__()
self.encoder = nn.Sequential(
conv_block_new(x_dim, int(hid_dim/2), kern_size=3, stride=1, padding=1),
nn.MaxPool2d(kernel_size=2, stride=2),
conv_block_new(int(hid_dim/2), int(hid_dim/2), 3, 1, 1),
nn.MaxPool2d(kernel_size=2, stride=2),
conv_block_new(int(hid_dim/2), hid_dim, 3, 1, 1),
nn.MaxPool2d(kernel_size=2, stride=2),
conv_block_new(hid_dim, z_dim, 3, 1, 1),
nn.MaxPool2d(kernel_size=2, stride=2),
Flatten()
)
def forward(self, data_shot, data_query):
shot_vec = self.encoder(data_shot)
query_vec = self.encoder(data_query)
shot_vec = shot_vec.reshape((5, 5, -1))
shot_vec = shot_vec.mean(1)
embedding_vector = square_euclidean_metric(query_vec, shot_vec)
return embedding_vector
class FewShotModelasdasdasdasd(nn.Module):
def __init__(self, x_dim=3, hid_dim=64, z_dim=64):
super().__init__()
self.encoder = ImageAndHistEncoder(x_dim=x_dim, hid_dim=hid_dim, z_dim=z_dim)
def forward(self, data_shot, data_query, data_shot_hist, data_query_hist): # 그냥 20개 classification 문제로 바꾸기
shot_vec = self.encoder(data_shot, data_shot_hist)
query_vec = self.encoder(data_query, data_query_hist)
shot_vec_mean = shot_vec.reshape((5, 5, -1)).mean(1)
query_vec = query_vec.reshape((20, -1))
embedding_vector = square_euclidean_metric(query_vec, shot_vec_mean)
# shot_vec_mean = shot_vec.reshape((5, 5, -1)).mean(1)
# shot_vec_concat = torch.cat(shot_vec, shot_vec_mean) ###############################
# embedding_vector = square_euclidean_metric(query_vec, shot_vec_concat)
return embedding_vector