---
# System prepended metadata

title: Hand pose recognition with CNN

---

---
title: 'Hand pose recognition with CNN'
disqus: hackmd
---

# Hand pose recognition with CNN


# Table of Contents

[TOC]


# 1.Reading training data
---
## 1.1 enumerate_files function
```python=
def enumerate_files(dirs, path='All_gray_1_32_32', n_poses=3,n_samples=20):
    filenames,targets = [], []
    for p in dirs: #Set
        for n in range(n_poses): #3種pose,label
            for j in range(3): 
                dir_name = path+'/'+p+'/000'+str(n*3+j)+'/'#資料夾每3個換label
                for s in range(n_samples): #各個label的資料夾裡有20個samples
                    d = dir_name+'%04d/'%s
                    for f in os.listdir(d):#瀏覽資料夾
                        if f.endswith('jpg'):
                            filenames += [d+f]
                            targets.append(n)#label
    return filenames, targets
```
* `enumerate_files` 執行function以獲得每張圖的位置和Label。
* `n_poses` 總共包含3種hand poses，也就是說Label為0~2。
* `filenames` 存每張圖的位置，`targets` 存每張圖的Label。
* `p` 紀錄目前執行到哪Set。
* `n` 紀錄Label。
* `n*3+j` 由於在Set資料夾裡每3個資料夾換一個Label。


## 1.2 read_images function
```python=
def read_images(files):
    imgs = [] #存每張img
    for f in files:
        img = cv2.imread(f, cv2.IMREAD_GRAYSCALE) #以灰階方式讀入
        img = img / 255 # 正規化
        imgs.append(img)
    return imgs
```
* `read_images function` 將圖存進imgs並正規化。

## 1.3 read_datasets function
```python=
def read_datasets(datasets):
    files, labels = enumerate_files(datasets)
    list_of_arrays = read_images(files)
    return np.array(list_of_arrays), labels
```
* `read_datasets function` 獲得Labels和轉成nparray的圖。

## 1.4 run the functions
```python=
train_sets = ['Set1', 'Set2', 'Set3']
test_sets = ['Set4', 'Set5']
trn_array, trn_labels = read_datasets(train_sets)
tst_array, tst_labels = read_datasets(test_sets)
```
# 2.Reshape and One-hot encoding
---
## 2.1 Training data
```python=
from keras import utils as np_utils
train_x = []

for i in range(540):#training data有540筆資料
    train_x.append(np.reshape(trn_array[i], (32, 32, 1)))#將每張圖轉成3維
    
train_x = np.array(train_x)
y_train_oneHot = np_utils.to_categorical(trn_labels)#Label做onehot encoding
```
## 2.2 Testing data
```python=
test_x = []

for i in range(360):#testing data有360筆資料
    test_x.append(np.reshape(tst_array[i], (32, 32, 1)))
    
test_x = np.array(test_x)
y_test_oneHot = np_utils.to_categorical(tst_labels)
```


# 3.Constructing network
---


```python=
from keras import layers
from keras import models
model = models.Sequential()

model.add(layers.Conv2D(60, (3, 3), activation = 'relu',input_shape = (32,32, 1)))
model.add(layers.MaxPooling2D((2, 2)))

model.add(layers.Conv2D(60, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))

model.add(layers.Dropout(rate = 0.25))

model.add(layers.Flatten())

model.add(layers.Dense(128, activation = 'relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(3, activation = 'softmax'))

model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
```

* CNN :
    * Layer 1 : 
        * filter : 3X3,60個
        * activation: relu
        * MaxPooling : 2X2
        
    * Layer 2 : 
        * filter : 3X3,60個
        * activation: relu
        * MaxPooling : 2X2
        
    * Dropout : 0.25
    
* Dense :
    * Layer 1 : 
        * dim : 128
        * activation: relu

    * Dropout : 0.5
        
    * Layer 2 : 6
        * dim : 3
        * activation: relu


# 4.Fit and Evaluate

## 4.1 Train the model
```python=
history = model.fit(train_x, y_train_oneHot, epochs = 300, batch_size = 100)
```
* Epoch : 300
* Batch : 100
 
## 4.2 Prediction
```python=
prediction = model.predict_classes(test_x)
```
Output:
```python
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2,
       2, 0, 2, 2, 0, 0, 0, 1, 2, 0, 0, 2, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0,
       0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1,
       2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 1,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2], dtype=int64)
```
## 4.3 Evaluate
```python=
evaluate = model.evaluate(test_x, y_test_oneHot)
```
* 以test data做測試。
```python=
print(evaluate[1])
```
* `evaluate[1]`為accuracy。

Output:
```python
0.9416666626930237
```

## 4.4 Plot the loss
### function
```python=
import matplotlib.pyplot as plt
def show_train_history(train_acc):
    plt.plot(history.history[train_acc])
    plt.title('Train History')
    plt.ylabel('loss')
    plt.xlabel('Epoch')
    plt.show()
    
show_train_history('loss')
```
* ![](https://i.imgur.com/NYaj9bH.jpg)



