-
Notifications
You must be signed in to change notification settings - Fork 217
/
Copy pathfunctions.py
284 lines (220 loc) · 11.9 KB
/
functions.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import os
import numpy as np
from PIL import Image
from torch.utils import data
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.models as models
import torchvision.transforms as transforms
from tqdm import tqdm
## ------------------- label conversion tools ------------------ ##
def labels2cat(label_encoder, list):
return label_encoder.transform(list)
def labels2onehot(OneHotEncoder, label_encoder, list):
return OneHotEncoder.transform(label_encoder.transform(list).reshape(-1, 1)).toarray()
def onehot2labels(label_encoder, y_onehot):
return label_encoder.inverse_transform(np.where(y_onehot == 1)[1]).tolist()
def cat2labels(label_encoder, y_cat):
return label_encoder.inverse_transform(y_cat).tolist()
## ---------------------- Dataloaders ---------------------- ##
# for CRNN- varying lengths (frames)
class Dataset_CRNN_varlen(data.Dataset):
"Characterizes a dataset for PyTorch"
def __init__(self, data_path, lists, labels, set_frame, transform=None):
"Initialization"
self.data_path = data_path
self.labels = labels
self.folders, self.video_len = list(zip(*lists))
self.set_frame = set_frame
self.transform = transform
def __len__(self):
"Denotes the total number of samples"
return len(self.folders)
def __getitem__(self, index):
"Generates one sample of data"
# select sample
selected_folder = self.folders[index]
video_len = self.video_len[index]
select = np.arange(self.set_frame['begin'], self.set_frame['end'] + 1, self.set_frame['skip'])
img_size = self.transform.__dict__['transforms'][0].__dict__['size'] # get image resize from Transformation
channels = len(self.transform.__dict__['transforms'][2].__dict__['mean']) # get number of channels from Transformation
selected_frames = np.intersect1d(np.arange(1, video_len + 1), select) if self.set_frame['begin'] < video_len else []
# Load video frames
X_padded = torch.zeros((len(select), channels, img_size[0], img_size[1])) # input size: (frames, channels, image size x, image size y)
for i, f in enumerate(selected_frames):
frame = Image.open(os.path.join(self.data_path, selected_folder, 'frame{:06d}.jpg'.format(f)))
frame = self.transform(frame) if self.transform is not None else frame # impose transformation if exists
X_padded[i, :, :, :] = frame
y = torch.LongTensor([self.labels[index]]) # (labels) LongTensor are for int64 instead of FloatTensor
video_len = torch.LongTensor([video_len])
return X_padded, video_len, y
## ---------------------- end of Dataloaders ---------------------- ##
## -------------------- (reload) model prediction ---------------------- ##
def CRNN_final_prediction(model, device, loader):
cnn_encoder, rnn_decoder = model
cnn_encoder.eval()
rnn_decoder.eval()
all_y_pred = []
with torch.no_grad():
for batch_idx, (X, y) in enumerate(tqdm(loader)):
# distribute data to device
X = X.to(device)
output = rnn_decoder(cnn_encoder(X))
y_pred = output.max(1, keepdim=True)[1] # location of max log-probability as prediction
all_y_pred.extend(y_pred.cpu().data.squeeze().numpy().tolist())
return all_y_pred
## -------------------- end of model prediction ---------------------- ##
## ------------------------ CRNN module ---------------------- ##
def conv2D_output_size(img_size, padding, kernel_size, stride):
# compute output shape of conv2D
outshape = (np.floor((img_size[0] + 2 * padding[0] - (kernel_size[0] - 1) - 1) / stride[0] + 1).astype(int),
np.floor((img_size[1] + 2 * padding[1] - (kernel_size[1] - 1) - 1) / stride[1] + 1).astype(int))
return outshape
# 2D CNN encoder train from scratch (no transfer learning)
class EncoderCNN(nn.Module):
def __init__(self, img_x=90, img_y=120, fc_hidden1=512, fc_hidden2=512, drop_p=0.3, CNN_embed_dim=300):
super(EncoderCNN, self).__init__()
self.img_x = img_x
self.img_y = img_y
self.CNN_embed_dim = CNN_embed_dim
# CNN architechtures
self.ch1, self.ch2, self.ch3, self.ch4 = 32, 64, 128, 256
self.k1, self.k2, self.k3, self.k4 = (5, 5), (3, 3), (3, 3), (3, 3) # 2d kernal size
self.s1, self.s2, self.s3, self.s4 = (2, 2), (2, 2), (2, 2), (2, 2) # 2d strides
self.pd1, self.pd2, self.pd3, self.pd4 = (0, 0), (0, 0), (0, 0), (0, 0) # 2d padding
# conv2D output shapes
self.conv1_outshape = conv2D_output_size((self.img_x, self.img_y), self.pd1, self.k1, self.s1) # Conv1 output shape
self.conv2_outshape = conv2D_output_size(self.conv1_outshape, self.pd2, self.k2, self.s2)
self.conv3_outshape = conv2D_output_size(self.conv2_outshape, self.pd3, self.k3, self.s3)
self.conv4_outshape = conv2D_output_size(self.conv3_outshape, self.pd4, self.k4, self.s4)
# fully connected layer hidden nodes
self.fc_hidden1, self.fc_hidden2 = fc_hidden1, fc_hidden2
self.drop_p = drop_p
self.conv1 = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=self.ch1, kernel_size=self.k1, stride=self.s1, padding=self.pd1),
nn.BatchNorm2d(self.ch1, momentum=0.01),
nn.ReLU(inplace=True),
# nn.MaxPool2d(kernel_size=2),
)
self.conv2 = nn.Sequential(
nn.Conv2d(in_channels=self.ch1, out_channels=self.ch2, kernel_size=self.k2, stride=self.s2, padding=self.pd2),
nn.BatchNorm2d(self.ch2, momentum=0.01),
nn.ReLU(inplace=True),
# nn.MaxPool2d(kernel_size=2),
)
self.conv3 = nn.Sequential(
nn.Conv2d(in_channels=self.ch2, out_channels=self.ch3, kernel_size=self.k3, stride=self.s3, padding=self.pd3),
nn.BatchNorm2d(self.ch3, momentum=0.01),
nn.ReLU(inplace=True),
# nn.MaxPool2d(kernel_size=2),
)
self.conv4 = nn.Sequential(
nn.Conv2d(in_channels=self.ch3, out_channels=self.ch4, kernel_size=self.k4, stride=self.s4, padding=self.pd4),
nn.BatchNorm2d(self.ch4, momentum=0.01),
nn.ReLU(inplace=True),
# nn.MaxPool2d(kernel_size=2),
)
self.drop = nn.Dropout2d(self.drop_p)
self.pool = nn.MaxPool2d(2)
self.fc1 = nn.Linear(self.ch4 * self.conv4_outshape[0] * self.conv4_outshape[1], self.fc_hidden1) # fully connected layer, output k classes
self.fc2 = nn.Linear(self.fc_hidden1, self.fc_hidden2)
self.fc3 = nn.Linear(self.fc_hidden2, self.CNN_embed_dim) # output = CNN embedding latent variables
def forward(self, x_3d):
cnn_embed_seq = []
for t in range(x_3d.size(1)):
# CNNs
x = self.conv1(x_3d[:, t, :, :, :])
x = self.conv2(x)
x = self.conv3(x)
x = self.conv4(x)
x = x.view(x.size(0), -1) # flatten the output of conv
# FC layers
x = F.relu(self.fc1(x))
# x = F.dropout(x, p=self.drop_p, training=self.training)
x = F.relu(self.fc2(x))
x = F.dropout(x, p=self.drop_p, training=self.training)
x = self.fc3(x)
cnn_embed_seq.append(x)
# swap time and sample dim such that (sample dim, time dim, CNN latent dim)
cnn_embed_seq = torch.stack(cnn_embed_seq, dim=0).transpose_(0, 1)
# cnn_embed_seq: shape=(batch, time_step, input_size)
return cnn_embed_seq
# 2D CNN encoder using ResNet-152 pretrained
class ResCNNEncoder(nn.Module):
def __init__(self, fc_hidden1=512, fc_hidden2=512, drop_p=0.3, CNN_embed_dim=300):
"""Load the pretrained ResNet-152 and replace top fc layer."""
super(ResCNNEncoder, self).__init__()
self.fc_hidden1, self.fc_hidden2 = fc_hidden1, fc_hidden2
self.drop_p = drop_p
resnet = models.resnet152(pretrained=True)
modules = list(resnet.children())[:-1] # delete the last fc layer.
self.resnet = nn.Sequential(*modules)
self.fc1 = nn.Linear(resnet.fc.in_features, fc_hidden1)
self.bn1 = nn.BatchNorm1d(fc_hidden1, momentum=0.01)
self.fc2 = nn.Linear(fc_hidden1, fc_hidden2)
self.bn2 = nn.BatchNorm1d(fc_hidden2, momentum=0.01)
self.fc3 = nn.Linear(fc_hidden2, CNN_embed_dim)
def forward(self, x_3d):
cnn_embed_seq = []
for t in range(x_3d.size(1)):
# ResNet CNN
with torch.no_grad():
x = self.resnet(x_3d[:, t, :, :, :]) # ResNet
x = x.view(x.size(0), -1) # flatten output of conv
# FC layers
x = self.bn1(self.fc1(x))
x = F.relu(x)
x = self.bn2(self.fc2(x))
x = F.relu(x)
x = F.dropout(x, p=self.drop_p, training=self.training)
x = self.fc3(x)
cnn_embed_seq.append(x)
# swap time and sample dim such that (sample dim, time dim, CNN latent dim)
cnn_embed_seq = torch.stack(cnn_embed_seq, dim=0).transpose_(0, 1)
# cnn_embed_seq: shape=(batch, time_step, input_size)
return cnn_embed_seq
class DecoderRNN_varlen(nn.Module):
def __init__(self, CNN_embed_dim=300, h_RNN_layers=3, h_RNN=256, h_FC_dim=128, drop_p=0.3, num_classes=50):
super(DecoderRNN_varlen, self).__init__()
self.RNN_input_size = CNN_embed_dim
self.h_RNN_layers = h_RNN_layers # RNN hidden layers
self.h_RNN = h_RNN # RNN hidden nodes
self.h_FC_dim = h_FC_dim
self.drop_p = drop_p
self.num_classes = num_classes
self.LSTM = nn.LSTM(
input_size=self.RNN_input_size,
hidden_size=self.h_RNN,
num_layers=h_RNN_layers,
batch_first=True, # input & output will has batch size as 1s dimension. e.g. (batch, time_step, input_size)
)
self.fc1 = nn.Linear(self.h_RNN, self.h_FC_dim)
self.fc2 = nn.Linear(self.h_FC_dim, self.num_classes)
def forward(self, x_RNN, x_lengths):
N, T, n = x_RNN.size()
# print('x_RNN.size:', x_RNN.size(), 'x_lengths:', x_lengths)
for i in range(N):
if x_lengths[i] < T:
x_RNN[i, x_lengths[i]:, :] = torch.zeros(T - x_lengths[i], n, dtype=torch.float, device=x_RNN.device)
x_lengths[x_lengths > T] = T
lengths_ordered, perm_idx = x_lengths.sort(0, descending=True)
# use input of descending length
packed_x_RNN = torch.nn.utils.rnn.pack_padded_sequence(x_RNN[perm_idx], lengths_ordered, batch_first=True)
self.LSTM.flatten_parameters()
packed_RNN_out, (h_n_sorted, h_c_sorted) = self.LSTM(packed_x_RNN, None)
""" h_n shape (n_layers, batch, hidden_size), h_c shape (n_layers, batch, hidden_size) """
""" None represents zero initial hidden state. RNN_out has shape=(batch, time_step, output_size) """
RNN_out, _ = torch.nn.utils.rnn.pad_packed_sequence(packed_RNN_out, batch_first=True)
RNN_out = RNN_out.contiguous()
# RNN_out = RNN_out.view(-1, RNN_out.size(2))
# reverse back to original sequence order
_, unperm_idx = perm_idx.sort(0)
RNN_out = RNN_out[unperm_idx]
# FC layers
x = self.fc1(RNN_out[:, -1, :]) # choose RNN_out at the last time step
x = F.relu(x)
x = F.dropout(x, p=self.drop_p, training=self.training)
x = self.fc2(x)
return x
## ---------------------- end of CRNN module ---------------------- ##