forked from Hollyqui/PyDepth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnn_depth.py
193 lines (163 loc) · 7.21 KB
/
cnn_depth.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
# -*- coding: utf-8 -*-
"""CNN_Depth.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1wPAgKmmtuR-VafmW2S5GfMt-mr3NZeyv
"""
import os
import torch
import torchvision
import torch.utils.data as utils
from torchvision import datasets
import torchvision.transforms as transforms
from torch.utils.data import DataLoader, Dataset
from torch.autograd import Variable
import torch.nn as nn
from torch import optim
import torch.nn.functional as F
from torchsummary import summary
import timeit
import random
import matplotlib.pyplot as plt
import numpy as np
from sklearn import preprocessing
from skimage.transform import rescale, resize, downscale_local_mean
import pytorch_ssim
height = int(480/2)
width = int(640/2)
'''Creates Random tensor that is the same size as the image (for testing)'''
def imageBatch(nb_image):
imgBatch = torch.rand(nb_image, 3, width, height)
return imgBatch
'''Creates Random tensor that is the same size as the depthmap (for testing)'''
def depthBatch(nb_image):
depthBatch = torch.rand(nb_image, width*height, 1, 1)
return depthBatch
def normalize(imageBatch):
for i in range(len(imageBatch)):
imageBatch[i] = preprocessing.normalize(imageBatch[i], norm='l2', axis=1, copy=True, return_norm=False)
return imageBatch
'''CNN doing the first stage of the Semi-Siamese Network (forms the two 'heads')'''
def firstStageCNN():
return nn.Sequential(nn.Conv2d(3, 32, kernel_size=3, stride=2), # optional: add stride
nn.ReLU(inplace=True),
nn.LocalResponseNorm(5, alpha=0.0001, beta=0.75, k=1),
nn.Conv2d(32, 62, kernel_size=3, stride=2),
nn.ReLU(inplace=True),
nn.LocalResponseNorm(5, alpha=0.0001, beta=0.75, k=1),
nn.MaxPool2d(kernel_size=3), # optional: add stride
nn.ReLU(inplace=True),
nn.Conv2d(62, 92, kernel_size=3, stride=2), # optional: add stride
nn.ReLU(inplace=True),
nn.LocalResponseNorm(5, alpha=0.0001, beta=0.75, k=1),
nn.MaxPool2d(kernel_size=3), # optional: add stride
nn.ReLU(inplace=True))
'''Form the complete network by taking the two heads and connecting them to
the body'''
class SiameseNetwork(nn.Module):
def __init__(self):
super(SiameseNetwork, self).__init__()
self.cnn1 = firstStageCNN()
self.cnn2 = firstStageCNN()
self.fc = nn.Sequential(nn.Conv2d(2208, 92, kernel_size=1),
nn.ReLU(inplace=True),
nn.Upsample(scale_factor=2, mode='nearest'),
nn.ReLU(inplace=True),
nn.Conv2d(92, 62, kernel_size=1),
nn.ReLU(inplace=True),
nn.Upsample(scale_factor=2, mode='nearest'),
nn.ReLU(inplace=True),
nn.Conv2d(62, 32, kernel_size=4),
nn.ReLU(inplace=True),
nn.Conv2d(32, width*height, kernel_size=1),
nn.ReLU(inplace=True),
nn.Softmax2d()
)
'''forwards through the first CNNs to the Main body then returns the output'''
def forward(self, input1, input2):
output1 = self.cnn1(input1)
output2 = self.cnn2(input2)
combined = torch.cat((output1.view(output1.size(0), -1),
output2.view(output2.size(0), -1)), dim=1)
combined = torch.unsqueeze(combined, 2)
combined = torch.unsqueeze(combined, 3)
out = self.fc(combined)
return out
''' Does the training of the whole dataset'''
def train(net, training_DATA_LEFT, training_DATA_RIGHT, depthMaps, EPOCHS, BATCH_SIZE):
optimizer = optim.Adam(net.parameters(), lr=0.005)
loss_function = pytorch_ssim.SSIM(window_size=11)
dataset = utils.TensorDataset(training_DATA_LEFT, training_DATA_RIGHT, depthMaps)
train_dataloader = DataLoader(dataset, shuffle=True, num_workers=0, batch_size=1)
net.zero_grad()
COUNTER = 1
avg_loss = []
print("train function was executed")
for epoch in range(EPOCHS):
for i, data in enumerate(train_dataloader):
img1, img2, depthmap = data
optimizer.zero_grad() # reset gradient
outputs = net(img1, img2)
loss = loss_function(depthmap, outputs)
print("Loss:", loss)
avg_loss.append(loss.detach())
loss.backward()
optimizer.step()
#Print out images and epoch numbers
print("Epoch number: ", COUNTER)
COUNTER += 1
avg_loss = np.array(avg_loss)
print("Average Loss:", np.mean(avg_loss))
avg_loss = []
plt.figure()
plt.imshow((outputs.view(height,width)).detach().numpy())
# plt.show()
plt.figure()
plt.imshow((depthmap.view(height,width)).detach().numpy())
# plt.show
image = img1.view(3,height,width)
plt.figure()
plt.imshow(np.swapaxes(np.swapaxes(image.detach().numpy(),0,2),0,1))
plt.show()
outputs = net(img1, img2)
img1 = img1.view(3,height,width)
plt.figure()
plt.imshow((outputs.view(height,width)).detach().numpy())
plt.figure()
plt.imshow((depthmap.view(height,width)).detach().numpy())
plt.figure()
plt.imshow(np.swapaxes(np.swapaxes(img1.detach().numpy(),0,2),0,1))
plt.show()
return net
def rescale_img(imageL, imageR, depthMap):
resizedL = []
resizedR = []
resizedDepth = []
for img in imageL:
resizedL.append(rescale(img, (1,0.5,0.5), anti_aliasing=True))
for img in imageR:
resizedR.append(rescale(img, (1,0.5,0.5), anti_aliasing=True))
for img in depthMap:
resizedDepth.append(rescale(img, 0.5, anti_aliasing=True))
return np.array(resizedL), np.array(resizedR), np.array(resizedDepth)
def main():
height = 240
width = 320
net = SiameseNetwork()
#This will import the real dataset in tensor arrays once the data is available
training_DATA_LEFT = np.load('C:/Users/szymo/Documents/left_images_numpy.npy')
training_DATA_RIGHT = np.load('C:/Users/szymo/Documents/right_images_numpy.npy')
depthMaps = np.load('C:/Users/szymo/Documents/depthmaps_numpy.npy')
training_DATA_LEFT = np.swapaxes(training_DATA_LEFT,1,3)
training_DATA_RIGHT = np.swapaxes(training_DATA_RIGHT,1,3)
training_DATA_LEFT, training_DATA_RIGHT, depthMaps = rescale_img(training_DATA_LEFT, training_DATA_RIGHT, depthMaps)
# depthMaps = normalize(depthMaps)
training_DATA_LEFT = torch.from_numpy(training_DATA_LEFT)
training_DATA_RIGHT = torch.from_numpy(training_DATA_RIGHT)
depthMaps = torch.from_numpy(depthMaps)
# reshape output
depthMaps = depthMaps.view(-1,int(width*height),1,1)
network = final = train(net, training_DATA_LEFT, training_DATA_RIGHT, depthMaps, EPOCHS = 15, BATCH_SIZE = 5)
torch.save(network, 'saved_network')
if __name__ == '__main__':
main()