-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·76 lines (57 loc) · 2.3 KB
/
test.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
#!/usr/bin/env python
import numpy as np
import chainer
import chainer.functions as F
import chainer.links as L
import chainer.datasets.image_dataset as ImageDataset
import six
import os
import cv2
from chainer import cuda, optimizers, serializers, Variable
from chainer import training
from chainer.training import extensions
import argparse
import net
from dataset import Image2ImageDataset
def main():
parser = argparse.ArgumentParser(description='chainer example of img2img learning')
parser.add_argument('--batchsize', '-b', type=int, default=1,
help='Number of images in each mini-batch')
parser.add_argument('--gpu', '-g', type=int, default=-1,
help='GPU ID (negative value indicates CPU)')
parser.add_argument('--out', '-o', default='test',
help='Directory to output the test result')
parser.add_argument('--model', '-m', default='result/model_final',
help='model to use')
parser.add_argument('--filelist', '-fl', default='filelist.dat',
help='filelist of dataset')
args = parser.parse_args()
print('GPU: {}'.format(args.gpu))
print('# batch-size: {}'.format(args.batchsize))
print('# model: {}'.format(args.model))
print('')
if args.gpu >= 0:
chainer.cuda.get_device(args.gpu).use() # Make a specified GPU current
with chainer.no_backprop_mode():
with chainer.using_config('train', False):
cnn = net.AutoENC()
serializers.load_npz(args.model, cnn)
if args.gpu >= 0:
cnn.to_gpu() # Copy the model to the GPU
dataset = Image2ImageDataset(args.filelist)
src , dst = dataset.get_example(0)
x_in = np.zeros((args.batchsize, src.shape[0], src.shape[1], src.shape[2])).astype('f')
#x_out = np.zeros((batchsize, dst.shape[1], dst.shape[1], dst.shape[2])).astype('f')
for j in range(args.batchsize):
src , dst = dataset.get_example(j)
x_in[j,:] = src
#x_out[j,:] = dst
if args.gpu >= 0:
x_in = cuda.to_gpu(x_in)
x_out = cnn(x_in)
output = x_out.data.get()
for i in range(args.batchsize):
img = dataset.post_proc(output[i])
cv2.imwrite( args.out +"/"+ dataset.get_name(i) , img )
if __name__ == '__main__':
main()