-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsinet.py
791 lines (643 loc) · 28.6 KB
/
sinet.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
import torch
import argparse
from apex import amp
from datetime import datetime
import os
import torch.nn.functional as F
from PIL import Image
import torch.utils.data as data
import torchvision.transforms as transforms
import cv2
import numpy as np
import imageio
import torch.nn as nn
import math
import torch.nn as nn
from torch.nn.parameter import Parameter
import scipy.stats as st
import torchvision.models as models
import tqdm
USE_CUDA = True
class BasicConv2d(nn.Module):
def __init__(self, in_planes, out_planes, kernel_size, stride=1, padding=0, dilation=1):
super(BasicConv2d, self).__init__()
self.conv = nn.Conv2d(in_planes, out_planes,
kernel_size=kernel_size, stride=stride,
padding=padding, dilation=dilation, bias=False)
self.bn = nn.BatchNorm2d(out_planes)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
x = self.conv(x)
x = self.bn(x)
return x
class RF(nn.Module):
# Revised from: Receptive Field Block Net for Accurate and Fast Object Detection, 2018, ECCV
# GitHub: https://github.com/ruinmessi/RFBNet
def __init__(self, in_channel, out_channel):
super(RF, self).__init__()
self.relu = nn.ReLU(True)
self.branch0 = nn.Sequential(
BasicConv2d(in_channel, out_channel, 1),
)
self.branch1 = nn.Sequential(
BasicConv2d(in_channel, out_channel, 1),
BasicConv2d(out_channel, out_channel, kernel_size=(1, 3), padding=(0, 1)),
BasicConv2d(out_channel, out_channel, kernel_size=(3, 1), padding=(1, 0)),
BasicConv2d(out_channel, out_channel, 3, padding=3, dilation=3)
)
self.branch2 = nn.Sequential(
BasicConv2d(in_channel, out_channel, 1),
BasicConv2d(out_channel, out_channel, kernel_size=(1, 5), padding=(0, 2)),
BasicConv2d(out_channel, out_channel, kernel_size=(5, 1), padding=(2, 0)),
BasicConv2d(out_channel, out_channel, 3, padding=5, dilation=5)
)
self.branch3 = nn.Sequential(
BasicConv2d(in_channel, out_channel, 1),
BasicConv2d(out_channel, out_channel, kernel_size=(1, 7), padding=(0, 3)),
BasicConv2d(out_channel, out_channel, kernel_size=(7, 1), padding=(3, 0)),
BasicConv2d(out_channel, out_channel, 3, padding=7, dilation=7)
)
self.conv_cat = BasicConv2d(4*out_channel, out_channel, 3, padding=1)
self.conv_res = BasicConv2d(in_channel, out_channel, 1)
def forward(self, x):
x0 = self.branch0(x)
x1 = self.branch1(x)
x2 = self.branch2(x)
x3 = self.branch3(x)
x_cat = self.conv_cat(torch.cat((x0, x1, x2, x3), dim=1))
x = self.relu(x_cat + self.conv_res(x))
return x
class PDC_SM(nn.Module):
# Partial Decoder Component (Search Module)
def __init__(self, channel):
super(PDC_SM, self).__init__()
self.relu = nn.ReLU(True)
self.upsample = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
self.conv_upsample1 = BasicConv2d(channel, channel, 3, padding=1)
self.conv_upsample2 = BasicConv2d(channel, channel, 3, padding=1)
self.conv_upsample3 = BasicConv2d(channel, channel, 3, padding=1)
self.conv_upsample4 = BasicConv2d(channel, channel, 3, padding=1)
self.conv_upsample5 = BasicConv2d(2*channel, 2*channel, 3, padding=1)
self.conv_concat2 = BasicConv2d(2*channel, 2*channel, 3, padding=1)
self.conv_concat3 = BasicConv2d(4*channel, 4*channel, 3, padding=1)
self.conv4 = BasicConv2d(4*channel, 4*channel, 3, padding=1)
self.conv5 = nn.Conv2d(4*channel, 1, 1)
def forward(self, x1, x2, x3, x4):
# print x1.shape, x2.shape, x3.shape, x4.shape
x1_1 = x1
x2_1 = self.conv_upsample1(self.upsample(x1)) * x2
x3_1 = self.conv_upsample2(self.upsample(self.upsample(x1))) * self.conv_upsample3(self.upsample(x2)) * x3
x2_2 = torch.cat((x2_1, self.conv_upsample4(self.upsample(x1_1))), 1)
x2_2 = self.conv_concat2(x2_2)
x3_2 = torch.cat((x3_1, self.conv_upsample5(self.upsample(x2_2)), x4), 1)
x3_2 = self.conv_concat3(x3_2)
x = self.conv4(x3_2)
x = self.conv5(x)
return x
class PDC_IM(nn.Module):
# Partial Decoder Component (Identification Module)
def __init__(self, channel):
super(PDC_IM, self).__init__()
self.relu = nn.ReLU(True)
self.upsample = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
self.conv_upsample1 = BasicConv2d(channel, channel, 3, padding=1)
self.conv_upsample2 = BasicConv2d(channel, channel, 3, padding=1)
self.conv_upsample3 = BasicConv2d(channel, channel, 3, padding=1)
self.conv_upsample4 = BasicConv2d(channel, channel, 3, padding=1)
self.conv_upsample5 = BasicConv2d(2*channel, 2*channel, 3, padding=1)
self.conv_concat2 = BasicConv2d(2*channel, 2*channel, 3, padding=1)
self.conv_concat3 = BasicConv2d(3*channel, 3*channel, 3, padding=1)
self.conv4 = BasicConv2d(3*channel, 3*channel, 3, padding=1)
self.conv5 = nn.Conv2d(3*channel, 1, 1)
def forward(self, x1, x2, x3):
x1_1 = x1
x2_1 = self.conv_upsample1(self.upsample(x1)) * x2
x3_1 = self.conv_upsample2(self.upsample(self.upsample(x1))) * self.conv_upsample3(self.upsample(x2)) * x3
x2_2 = torch.cat((x2_1, self.conv_upsample4(self.upsample(x1_1))), 1)
x2_2 = self.conv_concat2(x2_2)
x3_2 = torch.cat((x3_1, self.conv_upsample5(self.upsample(x2_2))), 1)
x3_2 = self.conv_concat3(x3_2)
x = self.conv4(x3_2)
x = self.conv5(x)
return x
class SINet_ResNet18(nn.Module):
# resnet based encoder decoder
def __init__(self, channel=32, opt=None):
super(SINet_ResNet18, self).__init__()
self.resnet = ResNet_2Branch()
self.downSample = nn.MaxPool2d(2, stride=2)
self.rf_low_sm = RF(128, channel)
self.rf2_sm = RF(896, channel)
self.rf3_sm = RF(768, channel)
self.rf4_sm = RF(512, channel)
self.pdc_sm = PDC_SM(channel)
self.rf2_im = RF(128, channel)
self.rf3_im = RF(256, channel)
self.rf4_im = RF(512, channel)
self.pdc_im = PDC_IM(channel)
self.upsample_2 = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
self.upsample_8 = nn.Upsample(scale_factor=8, mode='bilinear', align_corners=True)
self.SA = SA()
if self.training:
self.initialize_weights()
def forward(self, x):
# ---- feature abstraction -----
# - head
x0 = self.resnet.conv1(x)
x0 = self.resnet.bn1(x0)
x0 = self.resnet.relu(x0)
# - low-level features
x0 = self.resnet.maxpool(x0) # (BS, 64, 88, 88)
x1 = self.resnet.layer1(x0) # (BS, 64, 88, 88)
x2 = self.resnet.layer2(x1) # (BS, 512, 44, 44)
# ---- Stage-1: Search Module (SM) ----
x01 = torch.cat((x0, x1), dim=1) # (BS, 64+64, 88, 88)
x01_down = self.downSample(x01) # (BS, 320, 44, 44)
x01_sm_rf = self.rf_low_sm(x01_down) # (BS, 32, 44, 44)
x2_sm = x2 # (512, 44, 44)
x3_sm = self.resnet.layer3_1(x2_sm) # (1024, 22, 22)
x4_sm = self.resnet.layer4_1(x3_sm) # (2048, 11, 11)
x2_sm_cat = torch.cat((x2_sm,
self.upsample_2(x3_sm),
self.upsample_2(self.upsample_2(x4_sm))), dim=1) # 3584 channels
x3_sm_cat = torch.cat((x3_sm,
self.upsample_2(x4_sm)), dim=1) # 3072 channels
x2_sm_rf = self.rf2_sm(x2_sm_cat)
x3_sm_rf = self.rf3_sm(x3_sm_cat)
x4_sm_rf = self.rf4_sm(x4_sm)
camouflage_map_sm = self.pdc_sm(x4_sm_rf, x3_sm_rf, x2_sm_rf, x01_sm_rf)
# ---- Switcher: Search Attention (SA) ----
x2_sa = self.SA(camouflage_map_sm.sigmoid(), x2) # (512, 44, 44)
# ---- Stage-2: Identification Module (IM) ----
x3_im = self.resnet.layer3_2(x2_sa) # (1024, 22, 22)
x4_im = self.resnet.layer4_2(x3_im) # (2048, 11, 11)
x2_im_rf = self.rf2_im(x2_sa)
x3_im_rf = self.rf3_im(x3_im)
x4_im_rf = self.rf4_im(x4_im)
# - decoder
camouflage_map_im = self.pdc_im(x4_im_rf, x3_im_rf, x2_im_rf)
# ---- output ----
return self.upsample_8(camouflage_map_sm), self.upsample_8(camouflage_map_im)
def initialize_weights(self):
resnet18 = models.resnet18(pretrained=True)
pretrained_dict = resnet18.state_dict()
all_params = {}
for k, v in self.resnet.state_dict().items():
if k in pretrained_dict.keys():
v = pretrained_dict[k]
all_params[k] = v
elif '_1' in k:
name = k.split('_1')[0] + k.split('_1')[1]
v = pretrained_dict[name]
all_params[k] = v
elif '_2' in k:
name = k.split('_2')[0] + k.split('_2')[1]
v = pretrained_dict[name]
all_params[k] = v
# print([k for k in all_params.keys() if k not in self.resnet.state_dict().keys()])
# print([k for k in self.resnet.state_dict().keys() if k not in all_params.keys()])
assert len(all_params.keys()) == len(self.resnet.state_dict().keys())
self.resnet.load_state_dict(all_params)
print('[INFO] initialize weights from resnet18')
def _get_kernel(kernlen=16, nsig=3):
interval = (2*nsig+1.)/kernlen
x = np.linspace(-nsig-interval/2., nsig+interval/2., kernlen+1)
kern1d = np.diff(st.norm.cdf(x))
kernel_raw = np.sqrt(np.outer(kern1d, kern1d))
kernel = kernel_raw/kernel_raw.sum()
return kernel
def min_max_norm(in_):
"""
normalization
:param: in_
:return:
"""
max_ = in_.max(3)[0].max(2)[0].unsqueeze(2).unsqueeze(3).expand_as(in_)
min_ = in_.min(3)[0].min(2)[0].unsqueeze(2).unsqueeze(3).expand_as(in_)
in_ = in_ - min_
return in_.div(max_ - min_ + 1e-8)
class SA(nn.Module):
"""
holistic attention src
"""
def __init__(self):
super(SA, self).__init__()
gaussian_kernel = np.float32(_get_kernel(31, 4))
gaussian_kernel = gaussian_kernel[np.newaxis, np.newaxis, ...]
self.gaussian_kernel = Parameter(torch.from_numpy(gaussian_kernel))
def forward(self, attention, x):
soft_attention = F.conv2d(attention, self.gaussian_kernel, padding=15)
soft_attention = min_max_norm(soft_attention) # normalization
x = torch.mul(x, soft_attention.max(attention)) # mul
return x
def conv3x3(in_planes, out_planes, stride=1):
"""
3x3 convolution with padding
"""
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
padding=1, bias=False)
class BasicBlock(nn.Module):
expansion = 1
def __init__(self, inplanes, planes, stride=1, downsample=None):
super(BasicBlock, self).__init__()
self.conv1 = conv3x3(inplanes, planes, stride)
self.bn1 = nn.BatchNorm2d(planes)
self.relu = nn.ReLU(inplace=True)
self.conv2 = conv3x3(planes, planes)
self.bn2 = nn.BatchNorm2d(planes)
self.downsample = downsample
self.stride = stride
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
class Bottleneck(nn.Module):
expansion = 4
def __init__(self, inplanes, planes, stride=1, downsample=None):
super(Bottleneck, self).__init__()
self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride,
padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
# self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
# self.bn3 = nn.BatchNorm2d(planes * 4)
self.relu = nn.ReLU(inplace=True)
self.downsample = downsample
self.stride = stride
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
# out = self.relu(out)
# out = self.conv3(out)
# out = self.bn3(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
class ResNet_2Branch(nn.Module):
# ResNet18 with two branches (modified from torchvision.models.resnet (pytorch==0.4.1))
def __init__(self):
# self.inplanes = 128
self.inplanes = 64
super(ResNet_2Branch, self).__init__()
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
# M: changing resnet50 to resnet18
# number of layers is to be changed from [3, 4, 6, 3] to [2, 2, 2, 2]
self.layer1 = self._make_layer(BasicBlock, 64, 2)
self.layer2 = self._make_layer(BasicBlock, 128, 2, stride=2)
self.layer3_1 = self._make_layer(BasicBlock, 256, 2, stride=2)
self.layer4_1 = self._make_layer(BasicBlock, 512, 2, stride=2)
self.inplanes = 128
self.layer3_2 = self._make_layer(BasicBlock, 256, 2, stride=2)
self.layer4_2 = self._make_layer(BasicBlock, 512, 2, stride=2)
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def _make_layer(self, block, planes, blocks, stride=1):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(planes * block.expansion),
)
layers = []
layers.append(block(self.inplanes, planes, stride, downsample))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(self.inplanes, planes))
return nn.Sequential(*layers)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x1 = self.layer3_1(x)
x1 = self.layer4_1(x1)
x2 = self.layer3_2(x)
x2 = self.layer4_2(x2)
return x1, x2
class CamObjDataset(data.Dataset):
def __init__(self, image_root, gt_root, trainsize):
self.trainsize = trainsize
self.images = [image_root + f for f in os.listdir(image_root) if f.endswith('.jpg')]
self.gts = [gt_root + f for f in os.listdir(gt_root) if f.endswith('.jpg')
or f.endswith('.png')]
self.images = sorted(self.images)
self.gts = sorted(self.gts)
self.filter_files()
self.size = len(self.images)
self.img_transform = transforms.Compose([
transforms.Resize((self.trainsize, self.trainsize)),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])])
self.gt_transform = transforms.Compose([
transforms.Resize((self.trainsize, self.trainsize)),
transforms.ToTensor()])
def __getitem__(self, index):
image = self.rgb_loader(self.images[index])
gt = self.binary_loader(self.gts[index])
image = self.img_transform(image)
gt = self.gt_transform(gt)
return image, gt
def filter_files(self):
assert len(self.images) == len(self.gts)
images = []
gts = []
for img_path, gt_path in zip(self.images, self.gts):
img = Image.open(img_path)
gt = Image.open(gt_path)
if img.size == gt.size:
images.append(img_path)
gts.append(gt_path)
self.images = images
self.gts = gts
def rgb_loader(self, path):
with open(path, 'rb') as f:
img = Image.open(f)
return img.convert('RGB')
def binary_loader(self, path):
with open(path, 'rb') as f:
img = Image.open(f)
return img.convert('L')
def resize(self, img, gt):
assert img.size == gt.size
w, h = img.size
if h < self.trainsize or w < self.trainsize:
h = max(h, self.trainsize)
w = max(w, self.trainsize)
return img.resize((w, h), Image.BILINEAR), gt.resize((w, h), Image.NEAREST)
else:
return img, gt
def __len__(self):
return self.size
class test_dataset:
"""load test dataset (batchsize=1)"""
def __init__(self, image_root, gt_root, testsize):
self.testsize = testsize
self.images = [image_root + f for f in os.listdir(image_root) if f.endswith('.jpg')]
self.gts = [gt_root + f for f in os.listdir(gt_root) if f.endswith('.jpg') or f.endswith('.png')]
self.images = sorted(self.images)
self.gts = sorted(self.gts)
self.transform = transforms.Compose([
transforms.Resize((self.testsize, self.testsize)),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
self.gt_transform = transforms.ToTensor()
self.size = len(self.images)
self.index = 0
def load_data(self):
image = self.rgb_loader(self.images[self.index])
image = self.transform(image).unsqueeze(0)
gt = self.binary_loader(self.gts[self.index])
name = self.images[self.index].split('/')[-1]
if name.endswith('.jpg'):
name = name.split('.jpg')[0] + '.png'
self.index += 1
return image, gt, name
def rgb_loader(self, path):
with open(path, 'rb') as f:
img = Image.open(f)
return img.convert('RGB')
def binary_loader(self, path):
with open(path, 'rb') as f:
img = Image.open(f)
return img.convert('L')
class test_loader_faster(data.Dataset):
def __init__(self, image_root, testsize):
self.testsize = testsize
self.images = [image_root + f for f in os.listdir(image_root) if f.endswith('.jpg')]
self.images = sorted(self.images)
self.transform = transforms.Compose([
transforms.Resize((self.testsize, self.testsize)),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])])
self.size = len(self.images)
def __getitem__(self, index):
images = self.rgb_loader(self.images[index])
images = self.transform(images)
img_name_list = self.images[index]
return images, img_name_list
def rgb_loader(self, path):
with open(path, 'rb') as f:
img = Image.open(f)
return img.convert('RGB')
def binary_loader(self, path):
with open(path, 'rb') as f:
img = Image.open(f)
return img.convert('L')
def __len__(self):
return self.size
def get_loader(image_root, gt_root, batchsize, trainsize, shuffle=True, num_workers=0, pin_memory=True):
# `num_workers=0` for more stable training
dataset = CamObjDataset(image_root, gt_root, trainsize)
data_loader = data.DataLoader(dataset=dataset,
batch_size=batchsize,
shuffle=shuffle,
num_workers=num_workers,
pin_memory=pin_memory)
return data_loader
def eval_mae(y_pred, y):
"""
evaluate MAE (for test or validation phase)
:param y_pred:
:param y:
:return: Mean Absolute Error
"""
return torch.abs(y_pred - y).mean()
def numpy2tensor(numpy):
"""
convert numpy_array in cpu to tensor in gpu
:param numpy:
:return: torch.from_numpy(numpy).cuda()
"""
if USE_CUDA:
return torch.from_numpy(numpy).cuda()
else:
return torch.from_numpy(numpy)
def clip_gradient(optimizer, grad_clip):
"""
recalibrate the misdirection in the training
:param optimizer:
:param grad_clip:
:return:
"""
for group in optimizer.param_groups:
for param in group['params']:
if param.grad is not None:
param.grad.data.clamp_(-grad_clip, grad_clip)
def adjust_lr(optimizer, epoch, decay_rate=0.1, decay_epoch=30):
decay = decay_rate ** (epoch // decay_epoch)
for param_group in optimizer.param_groups:
param_group['lr'] *= decay
def trainer(train_loader, model, optimizer, epoch, opt, loss_func, total_step):
"""
Training iteration
:param train_loader:
:param model:
:param optimizer:
:param epoch:
:param opt:
:param loss_func:
:param total_step:
:return:
"""
model.train()
for step, data_pack in enumerate(train_loader):
optimizer.zero_grad()
images, gts = data_pack
if USE_CUDA:
images = images.cuda()
gts = gts.cuda()
cam_sm, cam_im = model(images)
loss_sm = loss_func(cam_sm, gts)
loss_im = loss_func(cam_im, gts)
loss_total = loss_sm + loss_im
with amp.scale_loss(loss_total, optimizer) as scale_loss:
scale_loss.backward()
# clip_gradient(optimizer, opt.clip)
optimizer.step()
if step % 10 == 0 or step == total_step:
print('[{}] => [Epoch Num: {:03d}/{:03d}] => [Global Step: {:04d}/{:04d}] => [Loss_s: {:.4f} Loss_i: {:0.4f}]'.
format(datetime.now(), epoch, opt.epoch, step, total_step, loss_sm.data, loss_im.data))
save_path = opt.save_model
os.makedirs(save_path, exist_ok=True)
if (epoch+1) % opt.save_epoch == 0:
torch.save(model.state_dict(), save_path + 'SINet_%d.pth' % (epoch+1))
def run_test(opt, model=None):
if model is None:
model = SINet_ResNet18()
if USE_CUDA:
model = model.cuda()
model.load_state_dict(torch.load(opt.model_path))
model.eval()
for dataset in ['COD10K', 'CAMO', 'CHAMELEON']:
save_path = opt.test_save + '/' + dataset + "/"
os.makedirs(save_path, exist_ok=True)
# NOTES:
# if you plan to inference on your customized dataset without grouth-truth,
# you just modify the params (i.e., `image_root=your_test_img_path` and `gt_root=your_test_img_path`)
# with the same filepath. We recover the original size according to the shape of grouth-truth, and thus,
# the grouth-truth map is unnecessary actually.
test_loader = test_dataset(image_root=opt.test_img_dir.format(dataset),
gt_root=opt.test_gt_dir.format(dataset),
testsize=opt.testsize)
img_count = 1
for iteration in tqdm.tqdm(range(test_loader.size)):
# load data
image, gt, name = test_loader.load_data()
gt = np.asarray(gt, np.float32)
gt /= (gt.max() + 1e-8)
image = image.cuda()
# print(image.min(), image.max())
# inference
_, cam = model(image)
# reshape and squeeze
# print(cam.min(), cam.max())
cam = F.upsample(cam, size=gt.shape, mode='bilinear', align_corners=True)
# print(cam.min(), cam.max())
cam = cam.sigmoid().data.cpu().numpy().squeeze()
# normalize
# print(cam.min(), cam.max())
cam = (cam - cam.min()) / (cam.max() - cam.min() + 1e-8)
# imageio.imwrite(save_path+name, cam)
cv2.imwrite(save_path+name, 255*cam)
# evaluate
mae = eval_mae(numpy2tensor(cam), numpy2tensor(gt))
# coarse score
# print('[Eval-Test] Dataset: {}, Image: {} ({}/{}), MAE: {}'.format(dataset, name, img_count,
# test_loader.size, mae))
img_count += 1
print("\n[Congratulations! Testing Done]")
def run_train(opt):
# TIPS: you also can use deeper network for better performance like channel=64
model_SINet = SINet_ResNet18(channel=32)
if USE_CUDA:
model_SINet = model_SINet.cuda()
print('-' * 30, model_SINet, '-' * 30)
optimizer = torch.optim.Adam(model_SINet.parameters(), opt.lr)
if opt.balanced_loss:
pos_weight = torch.tensor([10])
pos_weight = pos_weight.unsqueeze(-1).unsqueeze(-1)
if USE_CUDA:
pos_weight = pos_weight.cuda()
LogitsBCE = torch.nn.BCEWithLogitsLoss(pos_weight=pos_weight)
else:
LogitsBCE = torch.nn.BCEWithLogitsLoss()
net, optimizer = amp.initialize(model_SINet, optimizer, opt_level='O1') # NOTES: Ox not 0x
train_loader = get_loader(opt.train_img_dir, opt.train_gt_dir, batchsize=opt.batchsize,
trainsize=opt.trainsize, num_workers=12)
total_step = len(train_loader)
print('-' * 30, "\n[Training Dataset INFO]\nimg_dir: {}\ngt_dir: {}\nLearning Rate: {}\nBatch Size: {}\n"
"Training Save: {}\ntotal_num: {}\n".format(opt.train_img_dir, opt.train_gt_dir, opt.lr,
opt.batchsize, opt.save_model, total_step), '-' * 30)
for epoch_iter in range(1, opt.epoch):
adjust_lr(optimizer, epoch_iter, opt.decay_rate, opt.decay_epoch)
trainer(train_loader=train_loader, model=model_SINet,
optimizer=optimizer, epoch=epoch_iter,
opt=opt, loss_func=LogitsBCE, total_step=total_step)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--mode', type=str, default='train')
parser.add_argument('--save_model', type=str, default='./Snapshot/2020-CVPR-SINet/')
parser.add_argument('--train_img_dir', type=str, default='./source-data/Train/Image/')
parser.add_argument('--train_gt_dir', type=str, default='./source-data/Train/GT_Object/')
parser.add_argument('--test_img_dir', type=str, default='./TestDataset/{}/Imgs/')
parser.add_argument('--test_gt_dir', type=str, default='./TestDataset/{}/GT/')
parser.add_argument('--balanced_loss', default=False, action='store_true')
parser.add_argument('--model_path', type=str,
default='./Snapshot/2020-CVPR-SINet/SINet_40.pth')
parser.add_argument('--test_save', type=str,
default='./sinet_r18_predictions/')
parser.add_argument('--epoch', type=int, default=40,
help='epoch number, default=30')
parser.add_argument('--lr', type=float, default=1e-4,
help='init learning rate, try `lr=1e-4`')
parser.add_argument('--batchsize', type=int, default=36,
help='training batch size (Note: ~500MB per img in GPU)')
parser.add_argument('--trainsize', type=int, default=352,
help='the size of training image, try small resolutions for speed (like 256)')
parser.add_argument('--testsize', type=int, default=352, help='the snapshot input size')
parser.add_argument('--clip', type=float, default=0.5,
help='gradient clipping margin')
parser.add_argument('--decay_rate', type=float, default=0.1,
help='decay rate of learning rate per decay step')
parser.add_argument('--decay_epoch', type=int, default=30,
help='every N epochs decay lr')
parser.add_argument('--gpu', type=int, default=0,
help='choose which gpu you use')
parser.add_argument('--save_epoch', type=int, default=5,
help='every N epochs save your trained snapshot')
opt = parser.parse_args()
if USE_CUDA:
torch.cuda.set_device(opt.gpu)
if opt.mode == "train":
run_train(opt)
else:
run_test(opt)