-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinspection.py
104 lines (81 loc) · 2.88 KB
/
inspection.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
#!/usr/bin/env python
"""Example code of learning a large scale convnet from ILSVRC2012 dataset.
Prerequisite: To run this example, crop the center of ILSVRC2012 training and
validation images and scale them to 256x256, and make two lists of space-
separated CSV whose first column is full path to image and second column is
zero-origin label (this format is same as that used by Caffe's ImageDataLayer).
"""
from __future__ import print_function
import argparse
import datetime
import json
import multiprocessing
import random
import sys
import threading
import time
import numpy as np
from PIL import Image
import six
#import six.moves.cPickle as pickle
import cPickle as pickle
from six.moves import queue
import chainer
import matplotlib.pyplot as plt
import numpy as np
import math
import chainer.functions as F
import chainer.links as L
from chainer.links import caffe
from matplotlib.ticker import *
from chainer import serializers
parser = argparse.ArgumentParser(
description='Image inspection using chainer')
parser.add_argument('image', help='Path to inspection image file')
parser.add_argument('--model','-m',default='model', help='Path to model file')
parser.add_argument('--mean', default='mean.npy',
help='Path to the mean file (computed by compute_mean.py)')
args = parser.parse_args()
def read_image(path, center=False, flip=False):
image = np.asarray(Image.open(path)).transpose(2, 0, 1)
if center:
top = left = cropwidth / 2
else:
top = random.randint(0, cropwidth - 1)
left = random.randint(0, cropwidth - 1)
bottom = model.insize + top
right = model.insize + left
image = image[:, top:bottom, left:right].astype(np.float32)
image -= mean_image[:, top:bottom, left:right]
image /= 255
if flip and random.randint(0, 1) == 0:
return image[:, :, ::-1]
else:
return image
import nin
mean_image = pickle.load(open(args.mean, 'rb'))
model = nin.NIN()
serializers.load_npz('tl.model', model)
cropwidth = 256 - model.insize
model.to_cpu()
def predict(net, x):
h = F.max_pooling_2d(F.relu(net.conv1(x)), 3, stride=2)
h = F.max_pooling_2d(F.relu(net.conv2(h)), 3, stride=2)
h = F.max_pooling_2d(F.relu(net.conv3(h)), 3, stride=2)
h = net.conv4(F.dropout(h, train=True))
h = F.reshape(F.average_pooling_2d(h, 6), (x.data.shape[0], 1024))
return F.softmax(h)
#setattr(model, 'predict', predict)
img = read_image(args.image)
x = np.ndarray(
(1, 3, model.insize, model.insize), dtype=np.float32)
x[0]=img
x = chainer.Variable(np.asarray(x), volatile='on')
score = predict(model,x)
#score=cuda.to_cpu(score.data)
categories = np.loadtxt("labels.txt", str, delimiter="\t")
top_k = 2
prediction = zip(score.data[0].tolist(), categories)
prediction.sort(cmp=lambda x, y: cmp(x[0], y[0]), reverse=True)
for rank, (score, name) in enumerate(prediction[:top_k], start=1):
print('#%d | %s | %4.1f%%' % (rank, name, score * 100000))