Hyunjun

add python test code

......@@ -15,4 +15,7 @@ pickletools라는 라이브러리가 있지만 에러가 많고, 자바, 파이썬, C++ 여러 언어를
5. params.pkl->params.txt
입력처리할 때 csv파일로 읽으면 속도가 느림
이 또한 속도를 올리기 위한 컨버팅 작업의 목적에 맞지 않기 때문에 params.pkl 파일을 csv 파일이 아닌 txt 파일로 바꿈
\ No newline at end of file
이 또한 속도를 올리기 위한 컨버팅 작업의 목적에 맞지 않기 때문에 params.pkl 파일을 csv 파일이 아닌 txt 파일로 바꿈
6. python test 코드 추가
test하는 부분만 골라내기 위해 python test 코드를 추가(test.py), simple_convnet 내용 추가
\ No newline at end of file
......
......@@ -11,7 +11,7 @@ from gradient import numerical_gradient
class SimpleConvNet:
def __init__(self, input_dim=(3, 32, 32),
conv_param={'filter_num':(32, 32, 64), 'filter_size':3, 'pad':1, 'stride':1},
hidden_size=512, output_size=10, weight_init_std=0.01):
hidden_size=512, output_size=10, weight_init_std=0.01, pretrained=False):
filter_num = conv_param['filter_num']
filter_size = conv_param['filter_size']
filter_pad = conv_param['pad']
......@@ -22,28 +22,38 @@ class SimpleConvNet:
pool1_output_size = int(filter_num[1] * (conv_output_size/2) * (conv_output_size/2))
pool2_output_size = int(filter_num[2] * (conv_output_size/4) * (conv_output_size/4))
pool3_output_size = int(filter_num[2] * (conv_output_size/8) * (conv_output_size/8))
self.params = {}
self.params['W1'] = cp.array( weight_init_std * \
cp.random.randn(filter_num[0], input_dim[0], filter_size, filter_size), dtype=np.float32)
if pretrained:
weights = self.load_weights()
self.params['W1'] = cp.array(weights['W1'], dtype=np.float32)
self.params['W2'] = cp.array(weights['W2'], dtype=np.float32)
self.params['W3'] = cp.array(weights['W3'], dtype=np.float32)
self.params['W4'] = cp.array(weights['W4'], dtype=np.float32)
self.params['W5'] = cp.array(weights['W5'], dtype=np.float32)
self.params['W6'] = cp.array(weights['W6'], dtype=np.float32)
self.params['W7'] = cp.array(weights['W7'], dtype=np.float32)
else:
self.params['W1'] = cp.array( weight_init_std * \
cp.random.randn(filter_num[0], input_dim[0], filter_size, filter_size), dtype=np.float32)
self.params['W2'] = cp.array( weight_init_std * \
cp.random.randn(filter_num[1], filter_num[0], 1, 1), dtype=np.float32)
self.params['W2'] = cp.array( weight_init_std * \
cp.random.randn(filter_num[1], filter_num[0], 1, 1), dtype=np.float32)
self.params['W3'] = cp.array( weight_init_std * \
cp.random.randn(filter_num[1], 1, filter_size, filter_size), dtype=np.float32)
self.params['W3'] = cp.array( weight_init_std * \
cp.random.randn(filter_num[1], 1, filter_size, filter_size), dtype=np.float32)
self.params['W4'] = cp.array( weight_init_std * \
cp.random.randn(filter_num[2], filter_num[1], 1, 1), dtype=np.float32)
self.params['W4'] = cp.array( weight_init_std * \
cp.random.randn(filter_num[2], filter_num[1], 1, 1), dtype=np.float32)
self.params['W5'] = cp.array( weight_init_std * \
cp.random.randn(filter_num[2], 1, filter_size, filter_size), dtype=np.float32)
self.params['W5'] = cp.array( weight_init_std * \
cp.random.randn(filter_num[2], 1, filter_size, filter_size), dtype=np.float32)
self.params['W6'] = cp.array( weight_init_std * \
cp.random.randn(pool3_output_size, hidden_size), dtype=np.float32)
self.params['W6'] = cp.array( weight_init_std * \
cp.random.randn(pool3_output_size, hidden_size), dtype=np.float32)
self.params['W7'] = cp.array( weight_init_std * \
cp.random.randn(hidden_size, output_size), dtype=np.float32)
self.params['W7'] = cp.array( weight_init_std * \
cp.random.randn(hidden_size, output_size), dtype=np.float32)
self.layers = OrderedDict()
self.layers['Conv1'] = Convolution(self.params['W1'],
......@@ -83,7 +93,6 @@ class SimpleConvNet:
def predict(self, x):
for layer in self.layers.values():
x = layer.forward(x)
return x
def loss(self, x, t):
......@@ -100,12 +109,13 @@ class SimpleConvNet:
tt = t[i*batch_size:(i+1)*batch_size]
y = self.predict(tx)
y = np.argmax(y, axis=1)
print("answer : ", tt)
print("predict : ", y)
acc += np.sum(y == tt) #numpy
return acc / x.shape[0]
def gradient(self, x, t):
self.loss(x, t)
dout = 1
......@@ -133,3 +143,11 @@ class SimpleConvNet:
with open(file_name, 'wb') as f:
pickle.dump(params, f)
# 모델 가중치 불러오기 / SimpleconvNet에 pretrained 변수 추가함 : True면 가중치 읽어 적용
def load_weights(self, file_name='params.pkl'):
weights = []
with open(file_name, 'rb') as f:
weights = pickle.load(f)
return weights
......
import sys, os
sys.path.append(os.pardir)
import pickle
import numpy as cp
import numpy as np
from collections import OrderedDict
from layers import *
from gradient import numerical_gradient
class SimpleConvNet:
def __init__(self, input_dim=(3, 32, 32),
conv_param={'filter_num':(32, 32, 64), 'filter_size':3, 'pad':1, 'stride':1},
hidden_size=512, output_size=10, weight_init_std=0.01):
filter_num = conv_param['filter_num']
filter_size = conv_param['filter_size']
filter_pad = conv_param['pad']
filter_stride = conv_param['stride']
input_size = input_dim[1]
conv_output_size = (input_size - filter_size + 2*filter_pad) / filter_stride + 1
conv_data_size = int(filter_num[0] * conv_output_size * conv_output_size )
pool1_output_size = int(filter_num[1] * (conv_output_size/2) * (conv_output_size/2))
pool2_output_size = int(filter_num[2] * (conv_output_size/4) * (conv_output_size/4))
pool3_output_size = int(filter_num[2] * (conv_output_size/8) * (conv_output_size/8))
self.params = {}
self.params['W1'] = cp.array( weight_init_std * \
cp.random.randn(filter_num[0], input_dim[0], filter_size, filter_size), dtype=np.float32)
self.params['W2'] = cp.array( weight_init_std * \
cp.random.randn(filter_num[1], filter_num[0], 1, 1), dtype=np.float32)
self.params['W3'] = cp.array( weight_init_std * \
cp.random.randn(filter_num[1], 1, filter_size, filter_size), dtype=np.float32)
self.params['W4'] = cp.array( weight_init_std * \
cp.random.randn(filter_num[2], filter_num[1], 1, 1), dtype=np.float32)
self.params['W5'] = cp.array( weight_init_std * \
cp.random.randn(filter_num[2], 1, filter_size, filter_size), dtype=np.float32)
self.params['W6'] = cp.array( weight_init_std * \
cp.random.randn(pool3_output_size, hidden_size), dtype=np.float32)
self.params['W7'] = cp.array( weight_init_std * \
cp.random.randn(hidden_size, output_size), dtype=np.float32)
self.layers = OrderedDict()
self.layers['Conv1'] = Convolution(self.params['W1'],
conv_param['stride'], conv_param['pad'])
self.layers['LightNorm1'] = LightNormalization()
self.layers['Relu1'] = Relu()
self.layers['Pool1'] = Pooling(pool_h=2, pool_w=2, stride=2)
self.layers['Conv2'] = Convolution(self.params['W2'],
1, 0)
self.layers['LightNorm2'] = LightNormalization()
self.layers['Relu2'] = Relu()
self.layers['Conv3'] = DW_Convolution(self.params['W3'],
conv_param['stride'], conv_param['pad'])
self.layers['LightNorm3'] = LightNormalization()
self.layers['Relu3'] = Relu()
self.layers['Pool2'] = Pooling(pool_h=2, pool_w=2, stride=2)
self.layers['Conv4'] = Convolution(self.params['W4'],
1, 0)
self.layers['LightNorm4'] = LightNormalization()
self.layers['Relu4'] = Relu()
self.layers['Conv5'] = DW_Convolution(self.params['W5'],
conv_param['stride'], conv_param['pad'])
self.layers['LightNorm5'] = LightNormalization()
self.layers['Relu5'] = Relu()
self.layers['Pool3'] = Pooling(pool_h=2, pool_w=2, stride=2)
self.layers['Affine4'] = Affine(self.params['W6'])
self.layers['LightNorm6'] = LightNormalization()
self.layers['Relu6'] = Relu()
self.layers['Affine5'] = Affine(self.params['W7'])
self.last_layer = SoftmaxWithLoss()
def predict(self, x):
for layer in self.layers.values():
x = layer.forward(x)
return x
def loss(self, x, t):
y = self.predict(x)
return self.last_layer.forward(y, t)
def accuracy(self, x, t, batch_size=100):
if t.ndim != 1 : t = np.argmax(t, axis=1)
acc = 0.0
for i in range(int(x.shape[0] / batch_size)):
tx = x[i*batch_size:(i+1)*batch_size]
tt = t[i*batch_size:(i+1)*batch_size]
y = self.predict(tx)
y = np.argmax(y, axis=1)
acc += np.sum(y == tt) #numpy
return acc / x.shape[0]
def gradient(self, x, t):
self.loss(x, t)
dout = 1
dout = self.last_layer.backward(dout)
layers = list(self.layers.values())
layers.reverse()
for layer in layers:
dout = layer.backward(dout)
grads = {}
grads['W1'] = self.layers['Conv1'].dW
grads['W2'] = self.layers['Conv2'].dW
grads['W3'] = self.layers['Conv3'].dW
grads['W4'] = self.layers['Conv4'].dW
grads['W5'] = self.layers['Conv5'].dW
grads['W6'] = self.layers['Affine4'].dW
grads['W7'] = self.layers['Affine5'].dW
return grads
def save_params(self, file_name="params.pkl"):
params = {}
for key, val in self.params.items():
params[key] = val
with open(file_name, 'wb') as f:
pickle.dump(params, f)
from simple_convnet4 import *
from dataset.cifar10 import load_cifar10
def batch_(data, lbl, pre, size = 100):
return data[pre: pre+size], lbl[pre: pre+size]
network = SimpleConvNet(input_dim=(3,32,32),
conv_param = {'filter_num': (32, 32, 64), 'filter_size': 3, 'pad': 1, 'stride': 1},
hidden_size=512, output_size=10, weight_init_std=0.01, pretrained=True)
(x_train, t_train), (x_test, t_test) = load_cifar10(flatten=False)
print("Length of test data: ",len(x_test))
batch_size = 100
epoch = int(len(x_test) / batch_size)
acc = 0
for i in range(epoch):
t_img, t_lbl = batch_(x_test, t_test, i*batch_size, batch_size)
t = network.accuracy(t_img, t_lbl, batch_size)
acc += t * batch_size
print("Accuracy : ",str(acc / len(x_test)*100),'%')
\ No newline at end of file