Hyunjun

add python test code

...@@ -15,4 +15,7 @@ pickletools라는 라이브러리가 있지만 에러가 많고, 자바, 파이썬, C++ 여러 언어를 ...@@ -15,4 +15,7 @@ pickletools라는 라이브러리가 있지만 에러가 많고, 자바, 파이썬, C++ 여러 언어를
15 15
16 5. params.pkl->params.txt 16 5. params.pkl->params.txt
17 입력처리할 때 csv파일로 읽으면 속도가 느림 17 입력처리할 때 csv파일로 읽으면 속도가 느림
18 -이 또한 속도를 올리기 위한 컨버팅 작업의 목적에 맞지 않기 때문에 params.pkl 파일을 csv 파일이 아닌 txt 파일로 바꿈
...\ No newline at end of file ...\ No newline at end of file
18 +이 또한 속도를 올리기 위한 컨버팅 작업의 목적에 맞지 않기 때문에 params.pkl 파일을 csv 파일이 아닌 txt 파일로 바꿈
19 +
20 +6. python test 코드 추가
21 +test하는 부분만 골라내기 위해 python test 코드를 추가(test.py), simple_convnet 내용 추가
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -11,7 +11,7 @@ from gradient import numerical_gradient ...@@ -11,7 +11,7 @@ from gradient import numerical_gradient
11 class SimpleConvNet: 11 class SimpleConvNet:
12 def __init__(self, input_dim=(3, 32, 32), 12 def __init__(self, input_dim=(3, 32, 32),
13 conv_param={'filter_num':(32, 32, 64), 'filter_size':3, 'pad':1, 'stride':1}, 13 conv_param={'filter_num':(32, 32, 64), 'filter_size':3, 'pad':1, 'stride':1},
14 - hidden_size=512, output_size=10, weight_init_std=0.01): 14 + hidden_size=512, output_size=10, weight_init_std=0.01, pretrained=False):
15 filter_num = conv_param['filter_num'] 15 filter_num = conv_param['filter_num']
16 filter_size = conv_param['filter_size'] 16 filter_size = conv_param['filter_size']
17 filter_pad = conv_param['pad'] 17 filter_pad = conv_param['pad']
...@@ -22,28 +22,38 @@ class SimpleConvNet: ...@@ -22,28 +22,38 @@ class SimpleConvNet:
22 pool1_output_size = int(filter_num[1] * (conv_output_size/2) * (conv_output_size/2)) 22 pool1_output_size = int(filter_num[1] * (conv_output_size/2) * (conv_output_size/2))
23 pool2_output_size = int(filter_num[2] * (conv_output_size/4) * (conv_output_size/4)) 23 pool2_output_size = int(filter_num[2] * (conv_output_size/4) * (conv_output_size/4))
24 pool3_output_size = int(filter_num[2] * (conv_output_size/8) * (conv_output_size/8)) 24 pool3_output_size = int(filter_num[2] * (conv_output_size/8) * (conv_output_size/8))
25 - 25 +
26 self.params = {} 26 self.params = {}
27 - self.params['W1'] = cp.array( weight_init_std * \ 27 + if pretrained:
28 - cp.random.randn(filter_num[0], input_dim[0], filter_size, filter_size), dtype=np.float32) 28 + weights = self.load_weights()
29 + self.params['W1'] = cp.array(weights['W1'], dtype=np.float32)
30 + self.params['W2'] = cp.array(weights['W2'], dtype=np.float32)
31 + self.params['W3'] = cp.array(weights['W3'], dtype=np.float32)
32 + self.params['W4'] = cp.array(weights['W4'], dtype=np.float32)
33 + self.params['W5'] = cp.array(weights['W5'], dtype=np.float32)
34 + self.params['W6'] = cp.array(weights['W6'], dtype=np.float32)
35 + self.params['W7'] = cp.array(weights['W7'], dtype=np.float32)
36 + else:
37 + self.params['W1'] = cp.array( weight_init_std * \
38 + cp.random.randn(filter_num[0], input_dim[0], filter_size, filter_size), dtype=np.float32)
29 39
30 - self.params['W2'] = cp.array( weight_init_std * \ 40 + self.params['W2'] = cp.array( weight_init_std * \
31 - cp.random.randn(filter_num[1], filter_num[0], 1, 1), dtype=np.float32) 41 + cp.random.randn(filter_num[1], filter_num[0], 1, 1), dtype=np.float32)
32 42
33 - self.params['W3'] = cp.array( weight_init_std * \ 43 + self.params['W3'] = cp.array( weight_init_std * \
34 - cp.random.randn(filter_num[1], 1, filter_size, filter_size), dtype=np.float32) 44 + cp.random.randn(filter_num[1], 1, filter_size, filter_size), dtype=np.float32)
35 45
36 - self.params['W4'] = cp.array( weight_init_std * \ 46 + self.params['W4'] = cp.array( weight_init_std * \
37 - cp.random.randn(filter_num[2], filter_num[1], 1, 1), dtype=np.float32) 47 + cp.random.randn(filter_num[2], filter_num[1], 1, 1), dtype=np.float32)
38 48
39 - self.params['W5'] = cp.array( weight_init_std * \ 49 + self.params['W5'] = cp.array( weight_init_std * \
40 - cp.random.randn(filter_num[2], 1, filter_size, filter_size), dtype=np.float32) 50 + cp.random.randn(filter_num[2], 1, filter_size, filter_size), dtype=np.float32)
41 51
42 - self.params['W6'] = cp.array( weight_init_std * \ 52 + self.params['W6'] = cp.array( weight_init_std * \
43 - cp.random.randn(pool3_output_size, hidden_size), dtype=np.float32) 53 + cp.random.randn(pool3_output_size, hidden_size), dtype=np.float32)
44 54
45 - self.params['W7'] = cp.array( weight_init_std * \ 55 + self.params['W7'] = cp.array( weight_init_std * \
46 - cp.random.randn(hidden_size, output_size), dtype=np.float32) 56 + cp.random.randn(hidden_size, output_size), dtype=np.float32)
47 57
48 self.layers = OrderedDict() 58 self.layers = OrderedDict()
49 self.layers['Conv1'] = Convolution(self.params['W1'], 59 self.layers['Conv1'] = Convolution(self.params['W1'],
...@@ -83,7 +93,6 @@ class SimpleConvNet: ...@@ -83,7 +93,6 @@ class SimpleConvNet:
83 def predict(self, x): 93 def predict(self, x):
84 for layer in self.layers.values(): 94 for layer in self.layers.values():
85 x = layer.forward(x) 95 x = layer.forward(x)
86 -
87 return x 96 return x
88 97
89 def loss(self, x, t): 98 def loss(self, x, t):
...@@ -100,12 +109,13 @@ class SimpleConvNet: ...@@ -100,12 +109,13 @@ class SimpleConvNet:
100 tt = t[i*batch_size:(i+1)*batch_size] 109 tt = t[i*batch_size:(i+1)*batch_size]
101 y = self.predict(tx) 110 y = self.predict(tx)
102 y = np.argmax(y, axis=1) 111 y = np.argmax(y, axis=1)
112 + print("answer : ", tt)
113 + print("predict : ", y)
103 acc += np.sum(y == tt) #numpy 114 acc += np.sum(y == tt) #numpy
104 115
105 return acc / x.shape[0] 116 return acc / x.shape[0]
106 117
107 def gradient(self, x, t): 118 def gradient(self, x, t):
108 -
109 self.loss(x, t) 119 self.loss(x, t)
110 120
111 dout = 1 121 dout = 1
...@@ -133,3 +143,11 @@ class SimpleConvNet: ...@@ -133,3 +143,11 @@ class SimpleConvNet:
133 with open(file_name, 'wb') as f: 143 with open(file_name, 'wb') as f:
134 pickle.dump(params, f) 144 pickle.dump(params, f)
135 145
146 + # 모델 가중치 불러오기 / SimpleconvNet에 pretrained 변수 추가함 : True면 가중치 읽어 적용
147 + def load_weights(self, file_name='params.pkl'):
148 + weights = []
149 + with open(file_name, 'rb') as f:
150 + weights = pickle.load(f)
151 + return weights
152 +
153 +
......
1 +import sys, os
2 +sys.path.append(os.pardir)
3 +import pickle
4 +import numpy as cp
5 +import numpy as np
6 +from collections import OrderedDict
7 +from layers import *
8 +from gradient import numerical_gradient
9 +
10 +
11 +class SimpleConvNet:
12 + def __init__(self, input_dim=(3, 32, 32),
13 + conv_param={'filter_num':(32, 32, 64), 'filter_size':3, 'pad':1, 'stride':1},
14 + hidden_size=512, output_size=10, weight_init_std=0.01):
15 + filter_num = conv_param['filter_num']
16 + filter_size = conv_param['filter_size']
17 + filter_pad = conv_param['pad']
18 + filter_stride = conv_param['stride']
19 + input_size = input_dim[1]
20 + conv_output_size = (input_size - filter_size + 2*filter_pad) / filter_stride + 1
21 + conv_data_size = int(filter_num[0] * conv_output_size * conv_output_size )
22 + pool1_output_size = int(filter_num[1] * (conv_output_size/2) * (conv_output_size/2))
23 + pool2_output_size = int(filter_num[2] * (conv_output_size/4) * (conv_output_size/4))
24 + pool3_output_size = int(filter_num[2] * (conv_output_size/8) * (conv_output_size/8))
25 +
26 + self.params = {}
27 + self.params['W1'] = cp.array( weight_init_std * \
28 + cp.random.randn(filter_num[0], input_dim[0], filter_size, filter_size), dtype=np.float32)
29 +
30 + self.params['W2'] = cp.array( weight_init_std * \
31 + cp.random.randn(filter_num[1], filter_num[0], 1, 1), dtype=np.float32)
32 +
33 + self.params['W3'] = cp.array( weight_init_std * \
34 + cp.random.randn(filter_num[1], 1, filter_size, filter_size), dtype=np.float32)
35 +
36 + self.params['W4'] = cp.array( weight_init_std * \
37 + cp.random.randn(filter_num[2], filter_num[1], 1, 1), dtype=np.float32)
38 +
39 + self.params['W5'] = cp.array( weight_init_std * \
40 + cp.random.randn(filter_num[2], 1, filter_size, filter_size), dtype=np.float32)
41 +
42 + self.params['W6'] = cp.array( weight_init_std * \
43 + cp.random.randn(pool3_output_size, hidden_size), dtype=np.float32)
44 +
45 + self.params['W7'] = cp.array( weight_init_std * \
46 + cp.random.randn(hidden_size, output_size), dtype=np.float32)
47 +
48 + self.layers = OrderedDict()
49 + self.layers['Conv1'] = Convolution(self.params['W1'],
50 + conv_param['stride'], conv_param['pad'])
51 + self.layers['LightNorm1'] = LightNormalization()
52 + self.layers['Relu1'] = Relu()
53 + self.layers['Pool1'] = Pooling(pool_h=2, pool_w=2, stride=2)
54 +
55 + self.layers['Conv2'] = Convolution(self.params['W2'],
56 + 1, 0)
57 + self.layers['LightNorm2'] = LightNormalization()
58 + self.layers['Relu2'] = Relu()
59 + self.layers['Conv3'] = DW_Convolution(self.params['W3'],
60 + conv_param['stride'], conv_param['pad'])
61 + self.layers['LightNorm3'] = LightNormalization()
62 + self.layers['Relu3'] = Relu()
63 + self.layers['Pool2'] = Pooling(pool_h=2, pool_w=2, stride=2)
64 +
65 + self.layers['Conv4'] = Convolution(self.params['W4'],
66 + 1, 0)
67 + self.layers['LightNorm4'] = LightNormalization()
68 + self.layers['Relu4'] = Relu()
69 + self.layers['Conv5'] = DW_Convolution(self.params['W5'],
70 + conv_param['stride'], conv_param['pad'])
71 + self.layers['LightNorm5'] = LightNormalization()
72 + self.layers['Relu5'] = Relu()
73 + self.layers['Pool3'] = Pooling(pool_h=2, pool_w=2, stride=2)
74 +
75 + self.layers['Affine4'] = Affine(self.params['W6'])
76 + self.layers['LightNorm6'] = LightNormalization()
77 + self.layers['Relu6'] = Relu()
78 +
79 + self.layers['Affine5'] = Affine(self.params['W7'])
80 +
81 + self.last_layer = SoftmaxWithLoss()
82 +
83 + def predict(self, x):
84 + for layer in self.layers.values():
85 + x = layer.forward(x)
86 +
87 + return x
88 +
89 + def loss(self, x, t):
90 + y = self.predict(x)
91 + return self.last_layer.forward(y, t)
92 +
93 + def accuracy(self, x, t, batch_size=100):
94 + if t.ndim != 1 : t = np.argmax(t, axis=1)
95 +
96 + acc = 0.0
97 +
98 + for i in range(int(x.shape[0] / batch_size)):
99 + tx = x[i*batch_size:(i+1)*batch_size]
100 + tt = t[i*batch_size:(i+1)*batch_size]
101 + y = self.predict(tx)
102 + y = np.argmax(y, axis=1)
103 + acc += np.sum(y == tt) #numpy
104 +
105 + return acc / x.shape[0]
106 +
107 + def gradient(self, x, t):
108 +
109 + self.loss(x, t)
110 +
111 + dout = 1
112 + dout = self.last_layer.backward(dout)
113 +
114 + layers = list(self.layers.values())
115 + layers.reverse()
116 + for layer in layers:
117 + dout = layer.backward(dout)
118 +
119 + grads = {}
120 + grads['W1'] = self.layers['Conv1'].dW
121 + grads['W2'] = self.layers['Conv2'].dW
122 + grads['W3'] = self.layers['Conv3'].dW
123 + grads['W4'] = self.layers['Conv4'].dW
124 + grads['W5'] = self.layers['Conv5'].dW
125 + grads['W6'] = self.layers['Affine4'].dW
126 + grads['W7'] = self.layers['Affine5'].dW
127 + return grads
128 +
129 + def save_params(self, file_name="params.pkl"):
130 + params = {}
131 + for key, val in self.params.items():
132 + params[key] = val
133 + with open(file_name, 'wb') as f:
134 + pickle.dump(params, f)
135 +
1 +from simple_convnet4 import *
2 +from dataset.cifar10 import load_cifar10
3 +
4 +def batch_(data, lbl, pre, size = 100):
5 + return data[pre: pre+size], lbl[pre: pre+size]
6 +
7 +network = SimpleConvNet(input_dim=(3,32,32),
8 + conv_param = {'filter_num': (32, 32, 64), 'filter_size': 3, 'pad': 1, 'stride': 1},
9 + hidden_size=512, output_size=10, weight_init_std=0.01, pretrained=True)
10 +
11 +(x_train, t_train), (x_test, t_test) = load_cifar10(flatten=False)
12 +
13 +print("Length of test data: ",len(x_test))
14 +
15 +batch_size = 100
16 +epoch = int(len(x_test) / batch_size)
17 +acc = 0
18 +for i in range(epoch):
19 + t_img, t_lbl = batch_(x_test, t_test, i*batch_size, batch_size)
20 + t = network.accuracy(t_img, t_lbl, batch_size)
21 + acc += t * batch_size
22 +
23 +print("Accuracy : ",str(acc / len(x_test)*100),'%')
...\ No newline at end of file ...\ No newline at end of file