-
Notifications
You must be signed in to change notification settings - Fork 18
/
testing.py
102 lines (77 loc) · 3.94 KB
/
testing.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
import os
import torch
import numpy as np
from torch import nn
from torch.autograd import Variable
import torchvision
import torchvision.datasets as dsets
from torch.utils.data import DataLoader
import torchvision.transforms as transforms
import utils
from PIL import Image
from arch import define_Gen
from data_utils import VOCDataset, CityscapesDataset, ACDCDataset, get_transformation
root = './data/VOC2012test/VOC2012'
root_cityscapes = './data/Cityscape'
root_acdc = './data/ACDC'
def test(args):
### For selecting the number of channels
if args.dataset == 'voc2012':
n_channels = 21
elif args.dataset == 'cityscapes':
n_channels = 20
elif args.dataset == 'acdc':
n_channels = 4
transform = get_transformation((args.crop_height, args.crop_width), resize=True, dataset=args.dataset)
## let the choice of dataset configurable
if args.dataset == 'voc2012':
test_set = VOCDataset(root_path=root, name='test', ratio=0.5, transformation=transform, augmentation=None)
elif args.dataset == 'cityscapes':
test_set = CityscapesDataset(root_path=root_cityscapes, name='test', ratio=0.5, transformation=transform, augmentation=None)
elif args.dataset == 'acdc':
test_set = ACDCDataset(root_path=root_acdc, name='test', ratio=0.5, transformation=transform, augmentation=None)
test_loader = DataLoader(test_set, batch_size=args.batch_size, shuffle=False)
Gsi = define_Gen(input_nc=3, output_nc=n_channels, ngf=args.ngf, netG='resnet_9blocks_softmax',
norm=args.norm, use_dropout= not args.no_dropout, gpu_ids=args.gpu_ids)
### activation_softmax
activation_softmax = nn.Softmax2d()
if(args.model == 'supervised_model'):
### loading the checkpoint
try:
ckpt = utils.load_checkpoint('%s/latest_supervised_model.ckpt' % (args.checkpoint_dir))
Gsi.load_state_dict(ckpt['Gsi'])
except:
print(' [*] No checkpoint!')
### run
Gsi.eval()
for i, (image_test, image_name) in enumerate(test_loader):
image_test = utils.cuda(image_test, args.gpu_ids)
seg_map = Gsi(image_test)
seg_map = activation_softmax(seg_map)
prediction = seg_map.data.max(1)[1].squeeze_(1).squeeze_(0).cpu().numpy() ### To convert from 22 --> 1 channel
for j in range(prediction.shape[0]):
new_img = prediction[j] ### Taking a particular image from the batch
new_img = utils.colorize_mask(new_img, args.dataset) ### So as to convert it back to a paletted image
### Now the new_img is PIL.Image
new_img.save(os.path.join(args.results_dir+'/supervised/'+image_name[j]+'.png'))
print('Epoch-', str(i+1), ' Done!')
elif(args.model == 'semisupervised_cycleGAN'):
### loading the checkpoint
try:
ckpt = utils.load_checkpoint('%s/latest_semisuper_cycleGAN.ckpt' % (args.checkpoint_dir))
Gsi.load_state_dict(ckpt['Gsi'])
except:
print(' [*] No checkpoint!')
### run
Gsi.eval()
for i, (image_test, image_name) in enumerate(test_loader):
image_test = utils.cuda(image_test, args.gpu_ids)
seg_map = Gsi(image_test)
seg_map = activation_softmax(seg_map)
prediction = seg_map.data.max(1)[1].squeeze_(1).squeeze_(0).cpu().numpy() ### To convert from 22 --> 1 channel
for j in range(prediction.shape[0]):
new_img = prediction[j] ### Taking a particular image from the batch
new_img = utils.colorize_mask(new_img, args.dataset) ### So as to convert it back to a paletted image
### Now the new_img is PIL.Image
new_img.save(os.path.join(args.results_dir+'/unsupervised/'+image_name[j]+'.png'))
print('Epoch-', str(i+1), ' Done!')