-
Notifications
You must be signed in to change notification settings - Fork 0
/
training_classification.py
194 lines (156 loc) · 6.11 KB
/
training_classification.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
import torch
import os
import random
from model.model_classification import ColorCNN
import glob, os
from torch.utils.tensorboard import SummaryWriter
import cv2
#from dataset.multiproc_dataset_hdf5 import MultiProcDataset
from dataset.singleproc_dataset_hdf5 import MultiProcDataset
import string
from torchvision import transforms
#from dataset.h5py_dataset import VideoDataset
import glob
import numpy as np
from scipy.ndimage.filters import gaussian_filter
import h5py
import hdf5plugin
def loadVideos(path):
# load all .mp4-files from path into list
train_list = path + "/train_filenames.txt"
test_list =path + "/test_filenames.txt"
f = open(train_list, "r")
training_names = f.readlines()
training_names = [x.strip() for x in training_names]
f.close()
f = open(test_list, "r")
test_names = f.readlines()
test_names = [x.strip() for x in test_names]
f.close()
return training_names, test_names
def train(model, train_loader, criterion, optimizer, writer, weights, stepsTilNow=0):
i = 0
'''
for name, param in model.named_parameters():
if param.requires_grad:
writer.add_histogram(name, param, -1)
i += 1
'''
model.train()
weights = torch.tensor(weights, dtype=torch.float)
weights = weights.reshape(weights.shape[0]*weights.shape[1])
weights = weights.cuda()
step = 0
transform = transforms.Compose([
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])])
for batch in train_loader:
first_it = batch[1]
resnet_in = batch[2]
histograms = batch[3]
batch = batch[0]
resnet_in = resnet_in.cuda(non_blocking=True)
'''
for i in range(resnet_in.shape[0]):
resnet_in[i] = transform(resnet_in[i])
'''
x_in = batch.unsqueeze(dim=4).cuda(non_blocking=True)
x_in = x_in.permute(0,1,4,2,3)
# y_out_shape (batchsize, sequence_length, height, width, num_channels)
#y_out_a = batch[:,:,:,:,1].unsqueeze(dim=4).permute(0,1,4,2,3).cuda(non_blocking=True)
#y_out_b = batch[:,:,:,:,2].unsqueeze(dim=4).permute(0,1,4,2,3).cuda(non_blocking=True)
histograms = histograms.cuda(non_blocking=True)
model_out = model(x_in, first_it, resnet_in)
# out (batchsize, sequencelength, num_channels, height, width)
model_out = model_out.reshape(model_out.shape[0], model_out.shape[1], model_out.shape[2], model_out.shape[3]*model_out.shape[4])
model_out = model_out.permute(0,1,3,2)
loss = criterion(weights, model_out, histograms)
#sequenceLength = x_in.shape[1]
#batchsize = x_in.shape[0]
#loss /= (sequenceLength * batchsize)
writer.add_scalar('LOSS', loss, step)
if step % 10 ==0:
print(loss.item())
'''
if step % 100 == 0:
i = 0
for name, param in model.named_parameters():
if param.requires_grad:
writer.add_histogram(name, param, step)
i += 1
'''
optimizer.zero_grad()
loss.backward()
optimizer.step()
stepsTilNow += 1
step += 1
if step % 1000 == 0:
torch.save(model.state_dict(), "/network-ceph/pgrundmann/video_model_gru_steps_" + str(step) + ".bin")
def load_weights():
hists = glob.glob('/network-ceph/pgrundmann/maschinelles_sehen/histogram_mean/np_means/*hist.np.npy')
np_hists = []
for hist in hists:
np_hists.append(np.load(hist))
histogram = sum(np_hists)
histogram /= len(np_hists)
# merged-shape: a,b
smoothed = gaussian_filter(histogram, sigma=5)
smoothed_mixed = (0.5 * smoothed) + (0.5 / (histogram.shape[0] * histogram.shape[0]))
reciprocal = np.power(smoothed_mixed, -1)
reciprocal /= reciprocal.sum()
return reciprocal
def evaluate(model, epoch,test_loader,criterion, writer):
losses = torch.empty(0)
with torch.no_grad():
model.eval()
for batch in test_loader:
x_in = batch[0].cuda()
y_out_a = batch[1].cuda()
y_out_b = batch[2].cuda()
mask = batch[3].cuda()
model_out = model(x_in)
model_out *= mask
l1 = criterion(model_out(model_out[:,0], y_out_a))
l2 = criterion(model_out(model_out[:,1], y_out_b))
loss = (l1 + l2) / 2
losses = torch.stack((losses,loss),dim=0)
acc = torch.mean(losses, dim=0)
writer.add_scalar("Accuracy",acc,epoch)
def custom_cross_entropy(weights, input, target):
v = torch.argmax(target, dim=2)
t = weights[v]
x = target * torch.log(input)
x = x.sum(dim=3)
x = x * t
x = x.sum()
return -x
def main():
torch.multiprocessing.set_start_method('spawn')
end = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(4))
config_name = "VIDEO-MODEL-CNN-ONLY" + end
writer = SummaryWriter(log_dir="/network-ceph/pgrundmann/video_runs/" +config_name+"/",max_queue=20)
weights = load_weights()
BATCH_SIZE=2
SEQUENCE_LENGTH=64
EPOCHS=1
LR = 0.0001
criterion = custom_cross_entropy
#ds =VideoDataset(16)
path = '/network-ceph/pgrundmann/youtube_processed_small'
training_videos, test_videos = loadVideos(path)
video_list = []
with h5py.File('/network-ceph/pgrundmann/youtube_precalculated/final_dataset.hdf5', 'r') as f:
for name in f:
video_list.append(name)
dataset = MultiProcDataset(video_list, sequence_length=SEQUENCE_LENGTH)
train_loader = torch.utils.data.DataLoader(dataset,batch_size=None, num_workers=1, pin_memory=True)
model = ColorCNN()
model = model.cuda()
optimizer = torch.optim.Adam(model.parameters(), lr=LR, eps=1e-8)
for i in range(EPOCHS):
train(model,train_loader, criterion, optimizer, writer, weights)
print("Epoch finished")
torch.save(model.state_dict(), "/network-ceph/pgrundmann/video_model_" + str(i) + ".bin")
# evaluate(model,(i+1), test_loader,criterion,writer)
if __name__ == "__main__":
main()