-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestModel.py
355 lines (278 loc) · 11.1 KB
/
testModel.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
from __future__ import absolute_import, print_function
from collections import OrderedDict
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import os
import torch
import pandas as pd
from skimage import io, transform
import numpy as np
import matplotlib.pyplot as plt
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils, datasets, models
import warnings
warnings.filterwarnings("ignore")
class PMnet_usc(Dataset):
def __init__(self, csv_file,
dir_dataset="USC/",
transform= transforms.ToTensor()):
self.ind_val = pd.read_csv(csv_file)
self.dir_dataset = dir_dataset
self.transform = transform
def __len__(self):
return len(self.ind_val)
def __getitem__(self, idx):
#Load city map
self.dir_buildings = self.dir_dataset+ "map/"
img_name_buildings = os.path.join(self.dir_buildings, str((self.ind_val.iloc[idx, 0]))) + ".png"
image_buildings = np.asarray(io.imread(img_name_buildings))
#Load Tx (transmitter):
self.dir_Tx = self.dir_dataset+ "Tx/"
img_name_Tx = os.path.join(self.dir_Tx, str((self.ind_val.iloc[idx, 0]))) + ".png"
image_Tx = np.asarray(io.imread(img_name_Tx))
#Load Rx (reciever): (not used in our training)
self.dir_Rx = self.dir_dataset+ "Rx/"
img_name_Rx = os.path.join(self.dir_Rx, str((self.ind_val.iloc[idx, 0]))) + ".png"
image_Rx = np.asarray(io.imread(img_name_Rx))
#Load Power:
self.dir_power = self.dir_dataset+ "pmap/"
img_name_power = os.path.join(self.dir_power, str(self.ind_val.iloc[idx, 0])) + ".png"
image_power = np.asarray(io.imread(img_name_power))
inputs=np.stack([image_buildings, image_Tx], axis=2)
if self.transform:
inputs = self.transform(inputs).type(torch.float32)
power = self.transform(image_power).type(torch.float32)
return [inputs , power]
try:
from encoding.nn import SyncBatchNorm
_BATCH_NORM = SyncBatchNorm
except:
_BATCH_NORM = nn.BatchNorm2d
_BOTTLENECK_EXPANSION = 4
# Conv, Batchnorm, Relu layers, basic building block.
class _ConvBnReLU(nn.Sequential):
BATCH_NORM = _BATCH_NORM
def __init__(
self, in_ch, out_ch, kernel_size, stride, padding, dilation, relu=True
):
super(_ConvBnReLU, self).__init__()
self.add_module(
"conv",
nn.Conv2d(
in_ch, out_ch, kernel_size, stride, padding, dilation, bias=False
),
)
self.add_module("bn", _BATCH_NORM(out_ch, eps=1e-5, momentum=1 - 0.999))
if relu:
self.add_module("relu", nn.ReLU())
# Bottleneck layer cinstructed from ConvBnRelu layer block, buiding block for Res layers
class _Bottleneck(nn.Module):
def __init__(self, in_ch, out_ch, stride, dilation, downsample):
super(_Bottleneck, self).__init__()
mid_ch = out_ch // _BOTTLENECK_EXPANSION
self.reduce = _ConvBnReLU(in_ch, mid_ch, 1, stride, 0, 1, True)
self.conv3x3 = _ConvBnReLU(mid_ch, mid_ch, 3, 1, dilation, dilation, True)
self.increase = _ConvBnReLU(mid_ch, out_ch, 1, 1, 0, 1, False)
self.shortcut = (
_ConvBnReLU(in_ch, out_ch, 1, stride, 0, 1, False)
if downsample
else nn.Identity()
)
def forward(self, x):
h = self.reduce(x)
h = self.conv3x3(h)
h = self.increase(h)
h += self.shortcut(x)
return F.relu(h)
# Res Layer used to costruct the encoder
class _ResLayer(nn.Sequential):
def __init__(self, n_layers, in_ch, out_ch, stride, dilation, multi_grids=None):
super(_ResLayer, self).__init__()
if multi_grids is None:
multi_grids = [1 for _ in range(n_layers)]
else:
assert n_layers == len(multi_grids)
# Downsampling is only in the first block
for i in range(n_layers):
self.add_module(
"block{}".format(i + 1),
_Bottleneck(
in_ch=(in_ch if i == 0 else out_ch),
out_ch=out_ch,
stride=(stride if i == 0 else 1),
dilation=dilation * multi_grids[i],
downsample=(True if i == 0 else False),
),
)
# Stem layer is the initial interfacing layer
class _Stem(nn.Sequential):
"""
The 1st conv layer.
Note that the max pooling is different from both MSRA and FAIR ResNet.
"""
def __init__(self, out_ch, in_ch = 2):
super(_Stem, self).__init__()
self.add_module("conv1", _ConvBnReLU(in_ch, out_ch, 7, 2, 3, 1))
self.add_module("pool", nn.MaxPool2d(in_ch, 2, 1, ceil_mode=True))
class _ImagePool(nn.Module):
def __init__(self, in_ch, out_ch):
super().__init__()
self.pool = nn.AdaptiveAvgPool2d(1)
self.conv = _ConvBnReLU(in_ch, out_ch, 1, 1, 0, 1)
def forward(self, x):
_, _, H, W = x.shape
h = self.pool(x)
h = self.conv(h)
h = F.interpolate(h, size=(H, W), mode="bilinear", align_corners=False)
return h
# Atrous spatial pyramid pooling
class _ASPP(nn.Module):
def __init__(self, in_ch, out_ch, rates):
super(_ASPP, self).__init__()
self.stages = nn.Module()
self.stages.add_module("c0", _ConvBnReLU(in_ch, out_ch, 1, 1, 0, 1))
for i, rate in enumerate(rates):
self.stages.add_module(
"c{}".format(i + 1),
_ConvBnReLU(in_ch, out_ch, 3, 1, padding=rate, dilation=rate),
)
self.stages.add_module("imagepool", _ImagePool(in_ch, out_ch))
def forward(self, x):
return torch.cat([stage(x) for stage in self.stages.children()], dim=1)
# Decoder layer constricted using these 2 blocks
def ConRu(in_channels, out_channels, kernel, padding):
return nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel, padding=padding),
nn.ReLU(inplace=True)
)
def ConRuT(in_channels, out_channels, kernel, padding):
return nn.Sequential(
nn.ConvTranspose2d(in_channels, out_channels, kernel, stride=2, padding=padding),
nn.ReLU(inplace=True)
)
class PMNet(nn.Module):
def __init__(self, n_blocks, atrous_rates, multi_grids, output_stride):
super(PMNet, self).__init__()
if output_stride == 8:
s = [1, 2, 1, 1]
d = [1, 1, 2, 4]
elif output_stride == 16:
s = [1, 2, 2, 1]
d = [1, 1, 1, 2]
# Encoder
ch = [64 * 2 ** p for p in range(6)]
self.layer1 = _Stem(ch[0])
self.layer2 = _ResLayer(n_blocks[0], ch[0], ch[2], s[0], d[0])
self.layer3 = _ResLayer(n_blocks[1], ch[2], ch[3], s[1], d[1])
self.layer4 = _ResLayer(n_blocks[2], ch[3], ch[3], s[2], d[2])
self.layer5 = _ResLayer(n_blocks[3], ch[3], ch[4], s[3], d[3], multi_grids)
self.aspp = _ASPP(ch[4], 256, atrous_rates)
concat_ch = 256 * (len(atrous_rates) + 2)
self.add_module("fc1", _ConvBnReLU(concat_ch, 512, 1, 1, 0, 1))
self.reduce = _ConvBnReLU(256, 256, 1, 1, 0, 1)
# Decoder
self.conv_up5 = ConRu(512, 512, 3, 1)
self.conv_up4 = ConRuT(512+512, 512, 3, 1)
self.conv_up3 = ConRuT(512+512, 256, 3, 1)
self.conv_up2 = ConRu(256+256, 256, 3, 1)
self.conv_up1 = ConRu(256+256, 256, 3, 1)
self.conv_up0 = ConRu(256+64, 128, 3, 1)
self.conv_up00 = nn.Sequential(
nn.Conv2d(128+2, 64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Conv2d(64, 64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Conv2d(64, 1, kernel_size=3, padding=1))
def forward(self, x):
# Encoder
x1 = self.layer1(x)
x2 = self.layer2(x1)
x3 = self.reduce(x2)
x4 = self.layer3(x3)
x5 = self.layer4(x4)
x6 = self.layer5(x5)
x7 = self.aspp(x6)
x8 = self.fc1(x7)
# Decoder
xup5 = self.conv_up5(x8)
xup5 = torch.cat([xup5, x5], dim=1)
xup4 = self.conv_up4(xup5)
xup4 = torch.cat([xup4, x4], dim=1)
xup3 = self.conv_up3(xup4)
xup3 = torch.cat([xup3, x3], dim=1)
xup2 = self.conv_up2(xup3)
xup2 = torch.cat([xup2, x2], dim=1)
xup1 = self.conv_up1(xup2)
xup1 = torch.cat([xup1, x1], dim=1)
xup0 = self.conv_up0(xup1)
xup0 = F.interpolate(xup0, size=x.shape[2:], mode="bilinear", align_corners=False)
xup0 = torch.cat([xup0, x], dim=1)
xup00 = self.conv_up00(xup0)
return xup00
class RMSELoss(nn.Module):
def __init__(self,eps=1e-6) -> None:
super().__init__()
self.mse = nn.MSELoss()
self.eps = eps
def forward(self,yhat,y):
return torch.sqrt(self.mse(yhat,y)+self.eps)
def getTrainLoader():
#ddf = pd.DataFrame(np.arange(1,5601))
#ddf.to_csv('Data_Random_large_train_V1.csv',index=False)
#print(ddf.shape)
data_usc_train = PMnet_usc(csv_file = 'Data_Random_small_train_V1.csv', dir_dataset="Dataset/")
train_loader = DataLoader(data_usc_train, batch_size=5, shuffle=True, num_workers=8, generator=torch.Generator(device='cpu'))
return train_loader
def getTestLoader():
data_usc_test = PMnet_usc(csv_file='Data_Random_small_test_V1.csv',dir_dataset="Dataset/")
test_loader = DataLoader(data_usc_test,batch_size=2)
return test_loader
if __name__=="__main__":
print(torch.cuda.is_available())
if torch.cuda.is_available():
dev = "cuda:0"
else:
dev = "cpu"
device = torch.device(dev)
print("Device ",device)
torch.cuda.empty_cache()
m = PMNet(
n_blocks=[3, 3, 27, 3],
atrous_rates=[6, 12, 18],
multi_grids=[1, 2, 4],
output_stride=16,)
m.load_state_dict(torch.load("model.pt"))
test_d = getTestLoader()
m.eval()
m.to(device)
nEpoch = 50
loss_fn = RMSELoss()
optimizer = optim.SGD(m.parameters(),lr=0.1)
ov = 0
i = 1
for x_test,y_test in test_d:
y_test_cpu = y_test
x_test = x_test.to(device)
y_test = y_test.to(device)
y_pred = m(x_test)
print("Shape: ",y_pred.shape)
y_item = y_pred
x = 0
arr = [t.detach().numpy() for t in y_item.cpu()]
#print(individual_item.shape)
path_to_save_pred = "prediction/"
path_to_save_test = "test/"
for j in range(len(arr)):
plt.imsave(path_to_save_pred+'image_pred '+'batch '+str(i)+' item '+str(j)+'.png',arr[j][0])
plt.imsave(path_to_save_test+'image_test '+'batch '+str(i)+' item '+str(j)+'.png',y_test_cpu[j][0])
i = i+ 1
loss = loss_fn(y_pred,y_test)
loss_no = loss.item()
print("Loss: ",loss_no)
ov = ov + loss_no
print("Testing: ",loss)
print("Overall : ",ov/len(test_d))