diff --git a/results/index.md b/results/index.md index 96ce61c..58fd929 100644 --- a/results/index.md +++ b/results/index.md @@ -1,47 +1,85 @@ -# Your Name (id) +# 楊皓崴 (0656706) -#Project 5: Deep Classification +#Project 1: Deep Classification ## Overview The project is related to -> quote +> [ImageNet Classification with Deep Convolutional Neural Networks](https://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks) ## Implementation -1. One - * item - * item -2. Two +1. 前處理 +圖檔過大容易耗盡deepQ資源,故將長寬縮小24倍(45*80) +整理成一般分類問題資料儲存之 -``` -Code highlights -``` +```python + file = os.path.join(path4,"Image"+str(fileN)+".png") + pngfile = Image.open(file) + pngfile.thumbnail(size, Image.ANTIALIAS) + temp = np.asarray( pngfile, dtype="uint8" ) +``` + +2. 模型結構 + * conv layers (3*3 filter) *3 + * fully connect *2 + +```python + def alex_net(_X, _weights, _biases, _dropout): + _X = tf.reshape(_X, shape=[-1, 45, 80, 3]) + + conv1 = conv2d('conv1', _X, _weights['wc1'], _biases['bc1']) + pool1 = max_pool('pool1', conv1, k=2) + norm1 = norm('norm1', pool1, lsize=4) + norm1 = tf.nn.dropout(norm1, _dropout) + + conv2 = conv2d('conv2', norm1, _weights['wc2'], _biases['bc2']) + pool2 = max_pool('pool2', conv2, k=2) + norm2 = norm('norm2', pool2, lsize=4) + norm2 = tf.nn.dropout(norm2, _dropout) + + conv3 = conv2d('conv3', norm2, _weights['wc3'], _biases['bc3']) + pool3 = max_pool('pool3', conv3, k=2) + norm3 = norm('norm3', pool3, lsize=4) + norm3 = tf.nn.dropout(norm3, _dropout) + + dense1 = tf.reshape(norm3, [-1, _weights['wd1'].get_shape().as_list()[0]]) + dense1 = tf.nn.relu(tf.matmul(dense1, _weights['wd1']) + _biases['bd1'], name='fc1') + + dense2 = tf.nn.relu(tf.matmul(dense1, _weights['wd2']) + _biases['bd2'], name='fc2') + + out = tf.matmul(dense2, _weights['out']) + _biases['out'] + return out +``` ## Installation -* Other required packages. -* How to compile from source? +* Required packages. + * tensorflow + * numpy +* How to compile from source? + * python preprocessing.py + * python process01.py / python process02.py ### Results + +
- - - - +process01.py + +process02.py
- - - - + + +
- +嘗試增加conv層數,但沒有效果,猜測圖片縮小過多所致,但操作時間過久來不及嘗試其他尺寸.. diff --git a/results/output01.PNG b/results/output01.PNG new file mode 100644 index 0000000..112564e Binary files /dev/null and b/results/output01.PNG differ diff --git a/results/output02.PNG b/results/output02.PNG new file mode 100644 index 0000000..8511907 Binary files /dev/null and b/results/output02.PNG differ diff --git a/results/preprocessing.py b/results/preprocessing.py new file mode 100644 index 0000000..be6dafd --- /dev/null +++ b/results/preprocessing.py @@ -0,0 +1,74 @@ +import numpy as np +from PIL import Image +import os + +path0 = "frames" +testData = None +trainData = None +testLabel = None +trainLabel = None +size = 1920/24, 1080/24 +lr = None +for train_test in os.listdir(path0): + path1 = os.path.join(path0, train_test) + for site in os.listdir(path1): + path2 = os.path.join(path1, site) + for num in os.listdir(path2): + path3 = os.path.join(path2, num) + for LR in os.listdir(path3): + if LR == "head": + continue + elif LR == "Lhand": + lr = "left" + else: + lr = "right" + path4 = os.path.join(path3, LR) + print(path4) + + for fileN in range(1,len(os.listdir(path4))+1): + file = os.path.join(path4,"Image"+str(fileN)+".png") + pngfile = Image.open(file) + pngfile.thumbnail(size, Image.ANTIALIAS) + temp = np.asarray( pngfile, dtype="uint8" ) + if train_test == "train": + if trainData is None: + trainData = temp.reshape([1,-1]) + else: + trainData = np.concatenate([trainData,temp.reshape([1,-1])]) + else: + if testData is None: + testData = temp.reshape([1,-1]) + else: + testData = np.concatenate([testData,temp.reshape([1,-1])]) + + if train_test == "train": + lfile = os.path.join("labels", site, "obj_"+lr+num+".npy") + temp = np.load(lfile).astype(np.int8) + if trainLabel is None: + trainLabel = temp[:len(os.listdir(path4))] + else: + trainLabel = np.append(trainLabel, temp[:len(os.listdir(path4))]) + print(trainData.shape, trainLabel.shape) + else: + if site == "lab": + lfile = os.path.join("labels", site, "obj_"+lr+str(int(num)+4)+".npy") + else: + lfile = os.path.join("labels", site, "obj_"+lr+str(int(num)+3)+".npy") + temp = np.load(lfile).astype(np.int8) + if testLabel is None: + testLabel = temp[:len(os.listdir(path4))] + else: + testLabel = np.append(testLabel, temp[:len(os.listdir(path4))]) + print(testData.shape, testLabel.shape) + +b = np.zeros((testLabel.shape[0], 24)) +b[np.arange(testLabel.shape[0]), testLabel] = 1 +testLabel = b +b = np.zeros((trainLabel.shape[0], 24)) +b[np.arange(trainLabel.shape[0]), trainLabel] = 1 +trainLabel = b +print(testData.shape, trainData.shape, testLabel.shape, trainLabel.shape) +np.save("testData",testData) +np.save("trainData",trainData) +np.save("testLabel",testLabel) +np.save("trainLabel",trainLabel) \ No newline at end of file diff --git a/results/process01.py b/results/process01.py new file mode 100644 index 0000000..c316c08 --- /dev/null +++ b/results/process01.py @@ -0,0 +1,122 @@ +import numpy as np +import os +import tensorflow as tf + +env = os.environ.get('GRAPE_DATASET_DIR') +testData = np.load(os.path.join(env,"testData.npy")) +trainData = np.load(os.path.join(env,"trainData.npy")) +testLabel = np.load(os.path.join(env,"testLabel.npy")) +trainLabel = np.load(os.path.join(env,"trainLabel.npy")) + +learning_rate = 0.001 +training_iters = 200 + +n_input = 45*80*3 +n_classes = 24 +dropout = 0.8 + +x = tf.placeholder(tf.float32, [None, n_input]) +y = tf.placeholder(tf.float32, [None, n_classes]) +keep_prob = tf.placeholder(tf.float32) + +def conv2d(name, l_input, w, b): + return tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(l_input, w, strides=[1, 1, 1, 1], padding='SAME'),b), name=name) + +def max_pool(name, l_input, k): + return tf.nn.max_pool(l_input, ksize=[1, k, k, 1], strides=[1, k, k, 1], padding='SAME', name=name) + +def norm(name, l_input, lsize=4): + return tf.nn.lrn(l_input, lsize, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name=name) + +def alex_net(_X, _weights, _biases, _dropout): + _X = tf.reshape(_X, shape=[-1, 45, 80, 3]) + + conv1 = conv2d('conv1', _X, _weights['wc1'], _biases['bc1']) + pool1 = max_pool('pool1', conv1, k=2) + norm1 = norm('norm1', pool1, lsize=4) + norm1 = tf.nn.dropout(norm1, _dropout) + + conv2 = conv2d('conv2', norm1, _weights['wc2'], _biases['bc2']) + pool2 = max_pool('pool2', conv2, k=2) + norm2 = norm('norm2', pool2, lsize=4) + norm2 = tf.nn.dropout(norm2, _dropout) + + conv3 = conv2d('conv3', norm2, _weights['wc3'], _biases['bc3']) + pool3 = max_pool('pool3', conv3, k=2) + norm3 = norm('norm3', pool3, lsize=4) + norm3 = tf.nn.dropout(norm3, _dropout) + + dense1 = tf.reshape(norm3, [-1, _weights['wd1'].get_shape().as_list()[0]]) + dense1 = tf.nn.relu(tf.matmul(dense1, _weights['wd1']) + _biases['bd1'], name='fc1') + + dense2 = tf.nn.relu(tf.matmul(dense1, _weights['wd2']) + _biases['bd2'], name='fc2') + + out = tf.matmul(dense2, _weights['out']) + _biases['out'] + return out + +weights = { + 'wc1': tf.Variable(tf.random_normal([3, 3, 3, 64])), + 'wc2': tf.Variable(tf.random_normal([3, 3, 64, 128])), + 'wc3': tf.Variable(tf.random_normal([3, 3, 128, 256])), + 'wd1': tf.Variable(tf.random_normal([6*10*256, 1024])), + 'wd2': tf.Variable(tf.random_normal([1024, 1024])), + 'out': tf.Variable(tf.random_normal([1024, 24])) +} +biases = { + 'bc1': tf.Variable(tf.random_normal([64])), + 'bc2': tf.Variable(tf.random_normal([128])), + 'bc3': tf.Variable(tf.random_normal([256])), + 'bd1': tf.Variable(tf.random_normal([1024])), + 'bd2': tf.Variable(tf.random_normal([1024])), + 'out': tf.Variable(tf.random_normal([n_classes])) +} + +pred = alex_net(x, weights, biases, keep_prob) + +cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=pred)) +optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost) + +correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1)) +accuracy = tf.reduce_sum(tf.cast(correct_pred, tf.float32)) + +init = tf.global_variables_initializer() + +N = 200 + +with tf.Session() as sess: + sess.run(init) + step = 1 + # Keep training until reach max iterations + Ist = 0 + Ien = Ist+N + while step < training_iters: + if Ien > trainData.shape[0]: + Ien = trainData.shape[0] + sess.run(optimizer, feed_dict={x: trainData[Ist:Ien], y: trainLabel[Ist:Ien], keep_prob: dropout}) + Ist+=N + Ien+=N + if Ist > trainData.shape[0]: + Ist = 0 + Ien = Ist+N + ist = 0 + ien = ist+N + temp = 0 + while ist < trainData.shape[0]: + if ien > trainData.shape[0]: + ien = trainData.shape[0] + temp += sess.run(accuracy, feed_dict={x: trainData[ist:ien], y: trainLabel[ist:ien], keep_prob: 1.}) + ist+=N + ien+=N + acc = temp/trainData.shape[0] + ist = 0 + ien = ist+N + temp = 0 + while ist < testData.shape[0]: + if ien > testData.shape[0]: + ien = testData.shape[0] + temp += sess.run(accuracy, feed_dict={x: testData[ist:ien], y: testLabel[ist:ien], keep_prob: 1.}) + ist+=N + ien+=N + v_acc = temp/testData.shape[0] + print('\nINFO:root:Epoch[%d] Train-accuracy=%f\nINFO:root:Epoch[%d] Validation-accuracy=%f' %(step, acc, step, v_acc)) + step += 1 \ No newline at end of file diff --git a/results/process02.py b/results/process02.py new file mode 100644 index 0000000..1349b06 --- /dev/null +++ b/results/process02.py @@ -0,0 +1,130 @@ +import numpy as np +import os +import tensorflow as tf + +env = os.environ.get('GRAPE_DATASET_DIR') +#env = "" +testData = np.load(os.path.join(env,"testData.npy")) +trainData = np.load(os.path.join(env,"trainData.npy")) +testLabel = np.load(os.path.join(env,"testLabel.npy")) +trainLabel = np.load(os.path.join(env,"trainLabel.npy")) + +learning_rate = 0.001 +training_iters = 20 + +n_input = 45*80*3 +n_classes = 24 +dropout = 0.8 + +x = tf.placeholder(tf.float32, [None, n_input]) +y = tf.placeholder(tf.float32, [None, n_classes]) +keep_prob = tf.placeholder(tf.float32) + +def conv2d(name, l_input, w, b): + return tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(l_input, w, strides=[1, 1, 1, 1], padding='SAME'),b), name=name) + +def max_pool(name, l_input, k): + return tf.nn.max_pool(l_input, ksize=[1, k, k, 1], strides=[1, k, k, 1], padding='SAME', name=name) + +def norm(name, l_input, lsize=4): + return tf.nn.lrn(l_input, lsize, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name=name) + +def alex_net(_X, _weights, _biases, _dropout): + _X = tf.reshape(_X, shape=[-1, 45, 80, 3]) + + conv1 = conv2d('conv1', _X, _weights['wc1'], _biases['bc1']) + pool1 = max_pool('pool1', conv1, k=2) + norm1 = norm('norm1', pool1, lsize=4) + norm1 = tf.nn.dropout(norm1, _dropout) + + conv2 = conv2d('conv2', norm1, _weights['wc2'], _biases['bc2']) + pool2 = max_pool('pool2', conv2, k=2) + norm2 = norm('norm2', pool2, lsize=4) + norm2 = tf.nn.dropout(norm2, _dropout) + + conv3 = conv2d('conv3', norm2, _weights['wc3'], _biases['bc3']) + pool3 = max_pool('pool3', conv3, k=2) + norm3 = norm('norm3', pool3, lsize=4) + norm3 = tf.nn.dropout(norm3, _dropout) + + conv4 = conv2d('conv4', norm3, _weights['wc4'], _biases['bc4']) + pool4 = max_pool('pool4', conv4, k=2) + norm4 = norm('norm4', pool4, lsize=4) + norm4 = tf.nn.dropout(norm4, _dropout) + + dense1 = tf.reshape(norm4, [-1, _weights['wd1'].get_shape().as_list()[0]]) + dense1 = tf.nn.relu(tf.matmul(dense1, _weights['wd1']) + _biases['bd1'], name='fc1') + + dense2 = tf.nn.relu(tf.matmul(dense1, _weights['wd2']) + _biases['bd2'], name='fc2') + + out = tf.matmul(dense2, _weights['out']) + _biases['out'] + return out + +weights = { + 'wc1': tf.Variable(tf.random_normal([3, 3, 3, 64])), + 'wc2': tf.Variable(tf.random_normal([3, 3, 64, 128])), + 'wc3': tf.Variable(tf.random_normal([3, 3, 128, 256])), + 'wc4': tf.Variable(tf.random_normal([3, 3, 256, 512])), + 'wd1': tf.Variable(tf.random_normal([3*5*512, 1024])), + 'wd2': tf.Variable(tf.random_normal([1024, 1024])), + 'out': tf.Variable(tf.random_normal([1024, 24])) +} +biases = { + 'bc1': tf.Variable(tf.random_normal([64])), + 'bc2': tf.Variable(tf.random_normal([128])), + 'bc3': tf.Variable(tf.random_normal([256])), + 'bc4': tf.Variable(tf.random_normal([512])), + 'bd1': tf.Variable(tf.random_normal([1024])), + 'bd2': tf.Variable(tf.random_normal([1024])), + 'out': tf.Variable(tf.random_normal([n_classes])) +} + +pred = alex_net(x, weights, biases, keep_prob) + +cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=pred)) +optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost) + +correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1)) +accuracy = tf.reduce_sum(tf.cast(correct_pred, tf.float32)) + +init = tf.global_variables_initializer() + +N = 256 + +with tf.Session() as sess: + sess.run(init) + step = 1 + # Keep training until reach max iterations + Ist = 0 + Ien = Ist+N + while step < training_iters: + if Ien > trainData.shape[0]: + Ien = trainData.shape[0] + sess.run(optimizer, feed_dict={x: trainData[Ist:Ien], y: trainLabel[Ist:Ien], keep_prob: dropout}) + Ist+=N + Ien+=N + if Ist > trainData.shape[0]: + Ist = 0 + Ien = Ist+N + ist = 0 + ien = ist+N + temp = 0 + while ist < trainData.shape[0]: + if ien > trainData.shape[0]: + ien = trainData.shape[0] + temp += sess.run(accuracy, feed_dict={x: trainData[ist:ien], y: trainLabel[ist:ien], keep_prob: 1.}) + ist+=N + ien+=N + acc = temp/trainData.shape[0] + ist = 0 + ien = ist+N + temp = 0 + while ist < testData.shape[0]: + if ien > testData.shape[0]: + ien = testData.shape[0] + temp += sess.run(accuracy, feed_dict={x: testData[ist:ien], y: testLabel[ist:ien], keep_prob: 1.}) + ist+=N + ien+=N + v_acc = temp/testData.shape[0] + print('\nINFO:root:Epoch[%d] Train-accuracy=%f\nINFO:root:Epoch[%d] Validation-accuracy=%f' %(step, acc, step, v_acc)) + step += 1 \ No newline at end of file