Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 59 additions & 21 deletions results/index.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,85 @@
# Your Name <span style="color:red">(id)</span>
# 楊皓崴 <span style="color:red">(0656706)</span>

#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

<table border=1>
<tr>
<td>
<img src="placeholder.jpg" width="24%"/>
<img src="placeholder.jpg" width="24%"/>
<img src="placeholder.jpg" width="24%"/>
<img src="placeholder.jpg" width="24%"/>
process01.py
</td>
<td>
process02.py
</td>
</tr>

<tr>
<td>
<img src="placeholder.jpg" width="24%"/>
<img src="placeholder.jpg" width="24%"/>
<img src="placeholder.jpg" width="24%"/>
<img src="placeholder.jpg" width="24%"/>
<img src="https://github.com/w95wayne10/homework1/blob/master/results/output01.PNG" width="80%"/>
</td>
<td>
<img src="https://github.com/w95wayne10/homework1/blob/master/results/output02.PNG" width="80%"/>
</td>
</tr>

</table>


嘗試增加conv層數,但沒有效果,猜測圖片縮小過多所致,但操作時間過久來不及嘗試其他尺寸..
Binary file added results/output01.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added results/output02.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
74 changes: 74 additions & 0 deletions results/preprocessing.py
Original file line number Diff line number Diff line change
@@ -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)
122 changes: 122 additions & 0 deletions results/process01.py
Original file line number Diff line number Diff line change
@@ -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
Loading