-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvgg.py
142 lines (123 loc) · 5.28 KB
/
vgg.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
import chainer
import chainer.functions as F
import chainer.links as L
import skimage.io as io
import numpy as np
from chainer import utils
class VGG(chainer.Chain):
def __init__(self,category_num=10):
initializer = chainer.initializers.HeNormal()
super(VGG,self).__init__(
conv1_1 = L.Convolution2D(3,64,3,1,1,initialW=initializer),
conv1_2 = L.Convolution2D(64,128,3,1,1,initialW=initializer),
conv2_1 = L.Convolution2D(128,128,3,1,1,initialW=initializer),
conv2_2 = L.Convolution2D(128,256,3,1,1,initialW=initializer),
conv3_1 = L.Convolution2D(256,256,3,1,1,initialW=initializer),
conv3_2 = L.Convolution2D(256,256,3,1,1,initialW=initializer),
conv3_3 = L.Convolution2D(256,256,3,1,1,initialW=initializer),
conv3_4 = L.Convolution2D(256,512,3,1,1,initialW=initializer),
conv4_1 = L.Convolution2D(512,512,3,1,1,initialW=initializer),
conv4_2 = L.Convolution2D(512,512,3,1,1,initialW=initializer),
conv4_3 = L.Convolution2D(512,512,3,1,1,initialW=initializer),
conv4_4 = L.Convolution2D(512,512,3,1,1,initialW=initializer),
fc1 = L.Linear(25088,4096),
fc2 = L.Linear(4096,4096),
fc3 = L.Linear(4096,category_num),
)
def __call__(self,x,train=True):
#x = chainer.Variable(x)
h = F.relu(self.conv1_1(x))
h = F.relu(self.conv1_2(h))
h = F.max_pooling_2d(h,2,stride=2,pad=0)
h = F.relu(self.conv2_1(h))
h = F.relu(self.conv2_2(h))
h = F.max_pooling_2d(h,2,stride=2,pad=0)
h = F.relu(self.conv3_1(h))
h = F.relu(self.conv3_2(h))
h = F.relu(self.conv3_3(h))
h = F.relu(self.conv3_4(h))
h = F.max_pooling_2d(h,2,stride=2,pad=0)
h = F.relu(self.conv4_1(h))
h = F.relu(self.conv4_2(h))
h = F.relu(self.conv4_3(h))
h = F.relu(self.conv4_4(h))
h = F.max_pooling_2d(h,2,stride=2,pad=0)
h = F.relu(self.fc1(h))
h = F.dropout(h,ratio=0.5,train=train)
h = F.relu(self.fc2(h))
h = F.dropout(h,ratio=0.5,train=train)
h = F.relu(self.fc3(h))
return h
def calc_loss(self,y,t):
loss = F.softmax_cross_entropy(y,t)
return loss
def accuracy_of_each_category(self,y,t):
y.to_cpu()
t.to_cpu()
categories = set(t.data)
accuracy = {}
for category in categories:
supervise_indices = np.where(t.data==category)[0]
predict_result_of_category = np.argmax(y.data[supervise_indices],axis=1)
countup = len(np.where(predict_result_of_category==category)[0])
accuracy[category] = countup
return accuracy
class VGG_A(chainer.Chain):
def __init__(self,category_num=10):
initializer = chainer.initializers.HeNormal()
super(VGG_A,self).__init__(
conv1_1 = L.Convolution2D(3,64,3,1,1,initialW=initializer),
conv2_1 = L.Convolution2D(64,128,3,1,1,initialW=initializer),
conv3_1 = L.Convolution2D(128,256,3,1,1,initialW=initializer),
conv3_2 = L.Convolution2D(256,256,3,1,1,initialW=initializer),
conv4_1 = L.Convolution2D(256,512,3,1,1,initialW=initializer),
conv4_2 = L.Convolution2D(512,512,3,1,1,initialW=initializer),
fc1 = L.Convolution2D(512,4096,1,1,0,initialW=initializer),
fc2 = L.Convolution2D(4096,4096,1,1,0,initialW=initializer),
fc3 = L.Convolution2D(4096,category_num,1,1,0,initialW=initializer),
# fc1 = L.Linear(100352,4096),
# fc2 = L.Linear(4096,4096),
# fc3 = L.Linear(4096,category_num),
)
def __call__(self,x,train=True):
#x = chainer.Variable(x)
h = F.relu(self.conv1_1(x))
h = F.max_pooling_2d(h,2,stride=2,pad=0)
h = F.relu(self.conv2_1(h))
h = F.max_pooling_2d(h,2,stride=2,pad=0)
h = F.relu(self.conv3_1(h))
h = F.relu(self.conv3_2(h))
h = F.max_pooling_2d(h,2,stride=2,pad=0)
h = F.relu(self.conv4_1(h))
h = F.relu(self.conv4_2(h))
h = F.max_pooling_2d(h,2,stride=2,pad=0)
h = F.relu(self.fc1(h))
h = F.dropout(h,ratio=0.5,train=train)
h = F.relu(self.fc2(h))
h = F.dropout(h,ratio=0.5,train=train)
h = F.relu(self.fc3(h))
num, categories, y, x = h.data.shape
# global average pooling
h = F.reshape(F.average_pooling_2d(h, (y, x)), (num, categories))
return h
def calc_loss(self,y,t):
loss = F.softmax_cross_entropy(y,t)
return loss
def accuracy_of_each_category(self,y,t):
y.to_cpu()
t.to_cpu()
categories = set(t.data)
accuracy = {}
for category in categories:
supervise_indices = np.where(t.data==category)[0]
predict_result_of_category = np.argmax(y.data[supervise_indices],axis=1)
countup = len(np.where(predict_result_of_category==category)[0])
accuracy[category] = countup
return accuracy
if __name__ == "__main__":
imgpath = "/Users/suguru/Desktop/test.jpg"
img = io.imread(imgpath)
img = np.asarray(img).transpose(2,0,1).astype(np.float32)/255.
img = img[np.newaxis]
ex = model(img)
print(ex)