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
287 changes: 154 additions & 133 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,139 +1,160 @@
<center>
<img src="README_files/overview.png" alt="overview" style="float:middle;">
</center>

# Deep Classification

## updates
- 9/26/2017: provide [subset of dataset](https://drive.google.com/drive/folders/0B3fKFm-j0RqeWGdXZUNRUkpybU0?usp=sharing), separated into train/test set
- 9/27/2017: in this homework, we only evaluat the performance of object classification. You can use other label for multi-task learning, etc.

## Brief
* ***+2 extra credit of the whole semester***
* Due: <b>Oct. 5</b>, 11:59pm.
* Required files: results/index.md, and code/
* [Project reference](http://aliensunmin.github.io/project/handcam/)
# 邱浩翰 <span style="color:red">(105061607)</span>

#Project 5: Deep Classification

## Overview

<center>
<img src="README_files/overview.png" alt="overview" style="float:middle;">
</center>

Recently, the technological advance of wearable devices has led to significant interests in recognizing human behaviors in daily life (i.e., uninstrumented environment). Among many devices, egocentric camera systems have drawn significant attention, since the camera is aligned with the field-of-view of wearer, it naturally captures what a person sees. These systems have shown great potential in recognizing daily activities(e.g., making meals, watching TV, etc.), estimating hand poses, generating howto videos, etc.

Despite many advantages of egocentric camera systems, there exists two main issues which are much less discussed. Firstly, hand localization is not solved especially for passive camera systems. Even for active camera systems like Kinect, hand localization is challenging when two hands are interacting or a hand is interacting with an object. Secondly, the limited field-of-view of an egocentric camera implies that hands will inevitably move outside the images sometimes.

HandCam (Fig. 1), a novel wearable camera capturing activities of hands, for recognizing human behaviors. HandCam has two main advantages over egocentric systems : (1) it avoids the need to detect hands and manipulation regions; (2) it observes the activities of hands almost at all time.

## Requirement

- Python
- [TensorFlow](https://github.com/tensorflow/tensorflow)

## Data

### Introduction

This is a [dataset](https://drive.google.com/drive/folders/0BwCy2boZhfdBdXdFWnEtNWJYRzQ) recorded by hand camera system.

The camera system consist of three wide-angle cameras, two mounted on the left and right wrists to
capture hands (referred to as HandCam) and one mounted on the head (referred to as HeadCam).

The dataset consists of 20 sets of video sequences (i.e., each set includes two HandCams and one
HeadCam synchronized videos) captured in three scenes: a small office, a mid-size lab, and a large home.)

We want to classify some kinds of hand states including free v.s. active (i.e., hands holding objects or not),
object categories, and hand gestures. At the same time, a synchronized video has two sequence need to be labeled,
the left hand states and right hand states.

For each classification task (i.e., free vs. active, object categories, or hand gesture), there are forty
sequences of data. We split the dataset into two parts, half for training, half for testing. The object instance is totally separated into training and testing.

### Zip files

`frames.zip` contains all the frames sample from the original videos by 6fps.

`labels.zip` conatins the labels for all frames.

FA : free vs. active (only 0/1)

obj: object categories (24 classes, including free)

ges: hand gesture (13 gestures, including free)


### Details of obj. and ges.

```
Obj = { 'free':0,
'computer':1,
'cellphone':2,
'coin':3,
'ruler':4,
'thermos-bottle':5,
'whiteboard-pen':6,
'whiteboard-eraser':7,
'pen':8,
'cup':9,
'remote-control-TV':10,
'remote-control-AC':11,
'switch':12,
'windows':13,
'fridge':14,
'cupboard':15,
'water-tap':16,
'toy':17,
'kettle':18,
'bottle':19,
'cookie':20,
'book':21,
'magnet':22,
'lamp-switch':23}

Ges= { 'free':0,
'press'1,
'large-diameter':2,
'lateral-tripod':3,
'parallel-extension':4,
'thumb-2-finger':5,
'thumb-4-finger':6,
'thumb-index-finger':7,
'precision-disk':8,
'lateral-pinch':9,
'tripod':10,
'medium-wrap':11,
'light-tool':12}
```

## Writeup
The project is related to
* handcam object classification
* VGG16
* Reference to:

code
>https://github.com/kevin28520/My-TensorFlow-tutorials

vgg16
>https://arxiv.org/abs/1409.1556
## Implementation
1. Load data
* image & label
```
for dirPath, dirNames, fileNames in os.walk(label_path):
for i in range(len(train_label_file)):
train_labels = np.hstack((train_labels, np.load(label_path + train_label_file[i], mmap_mode='r')))

for dirPath, dirNames, fileNames in os.walk(label_path):
for i in range(len(test_label_file)):
test_labels = np.hstack((test_labels, np.load(label_path + test_label_file[i], mmap_mode='r')))

for i in range(len(train_image_file)):
for dirPath, dirNames, fileNames in os.walk(train_image_path + train_image_file[i]):
for f in fileNames:
train_images.append(os.path.join(dirPath, f))

for i in range(len(test_image_file)):
for dirPath, dirNames, fileNames in os.walk(test_image_path + test_image_file[i]):
for f in fileNames:
test_images.append(os.path.join(dirPath, f))
```


2. Training
* Optimizer
```
loss_func = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = y_logits, labels= y_label))#tools.loss(y_logits, y_label)
optimizer = tf.train.AdamOptimizer(LR).minimize(loss_func)

```

3. Testing
* Accuracy_evaluation
```
correct_prediction = tf.equal(tf.argmax(y_logits, 1), tf.argmax(y_label, 1))
accuracy = tf.cast(tf.reduce_mean(tf.cast(correct_prediction, dtype=tf.float32)), dtype=tf.float32)#tools.accuracy(y_logits, y_label)

```

4. Architecture
* VGG16
```
def VGG16N(x, n_classes, is_pretrain=True):

You are required to implement a **deep-learning-based method** to recognize hand states (free vs. active hands, hand gestures, object categories). Moreover, You might need to further take advantage of both HandCam and HeadCam. You will have to compete the performance with your classmates, so try to use as many techniques as possible to improve. **Your score will based on the performance ranking.**

For this project, and all other projects, you must do a project report in results folder using [Markdown](https://help.github.com/articles/markdown-basics). We provide you with a placeholder [index.md](./results/index.md) document which you can edit. In the report you will describe your algorithm and any decisions you made to write your algorithm a particular way. Then, you will describe how to run your code and if your code depended on other packages. You also need to show and discuss the results of your algorithm. Discuss any extra credit you did, and clearly show what contribution it had on the results (e.g. performance with and without each extra credit component).

You should also include the precision-recall curve of your final classifier and any interesting variants of your algorithm.

## Rubric
<ul>
<li> 40 pts: According to performance ranking in class </li>
<li> 60 pts: Outperform the AlexNet baseline </li>
<li> -5*n pts: Lose 5 points for every time (after the first) you do not follow the instructions for the hand in format </li>
</ul>

## Get start & hand in
* Publicly fork version (+2 extra points)
- [Fork the homework](https://education.github.com/guide/forks) to obtain a copy of the homework in your github account
- [Clone the homework](http://gitref.org/creating/#clone) to your local space and work on the code locally
- Commit and push your local code to your github repo
- Once you are done, submit your homework by [creating a pull request](https://help.github.com/articles/creating-a-pull-request)

* [Privately duplicated version](https://help.github.com/articles/duplicating-a-repository)
- Make a bare clone
- mirror-push to new repo
- [make new repo private](https://help.github.com/articles/making-a-private-repository-public)
- [add aliensunmin as collaborator](https://help.github.com/articles/adding-collaborators-to-a-personal-repository)
- [Clone the homework](http://gitref.org/creating/#clone) to your local space and work on the code locally
- Commit and push your local code to your github repo
- I will clone your repo after the due date

## Credits
Assignment designed by Cheng-Sheng Chan. Contents in this handout are from <a href="https://drive.google.com/file/d/0BwCy2boZhfdBM0ZDTV9lZW1rZzg/view">Chan et al.</a>.
with tf.name_scope('VGG16'):

x = tools.conv('conv1_1', x, 64, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
x = tools.conv('conv1_2', x, 64, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
with tf.name_scope('pool1'):
x = tools.pool('pool1', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True)

x = tools.conv('conv2_1', x, 128, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
x = tools.conv('conv2_2', x, 128, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
with tf.name_scope('pool2'):
x = tools.pool('pool2', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True)



x = tools.conv('conv3_1', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
x = tools.conv('conv3_2', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
x = tools.conv('conv3_3', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
with tf.name_scope('pool3'):
x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True)


x = tools.conv('conv4_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
x = tools.conv('conv4_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
x = tools.conv('conv4_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
with tf.name_scope('pool4'):
x = tools.pool('pool4', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True)


x = tools.conv('conv5_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
x = tools.conv('conv5_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
x = tools.conv('conv5_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
with tf.name_scope('pool5'):
x = tools.pool('pool5', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True)


x = tools.FC_layer('fc6', x, out_nodes=4096)
#with tf.name_scope('batch_norm1'):
#x = tools.batch_norm(x)
x = tools.FC_layer('fc7', x, out_nodes=4096)
#with tf.name_scope('batch_norm2'):
#x = tools.batch_norm(x)
x = tools.FC_layer('fc8', x, out_nodes=n_classes)

return x
```

6. Using pretrained weights or not
```
pre_trained_weights = 'vgg16.npy'
tools.load_with_skip(pre_trained_weights, sess, ['fc6', 'fc7', 'fc8'])
```

7. Parameters
* BATCH_SIZE = 12
* INPUT_WIDTH = 224
* INPUT_HEIGHT = 224
* EPOCH = 50
* LR = 10**(-4) # learning rate
* NUM_CLASS = 24

## Installation
* import VGG and tools (for network)
* Set the dataset directory
* switch train or test with comment
```
#for test : comment line261~321
'''
# strat trainng
startTime = time()
init = tf.global_variables_initializer()


with tf.Session() as sess:
sess.run(init)
.
.
.
# duration calculating
duration = time()-startTime
print('duration = ', duration)
#### End of training ####
'''
```

### Results

| Learning Rate |Loss| Testing Accurancy |
| --- | --- | --- |
| 0.001 | 2.67885 | 52.61% |



* Training time

> about 12 hours with GTX 1080 Ti
Binary file modified README_files/overview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
102 changes: 102 additions & 0 deletions code/VGG.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@

import tensorflow as tf
import tools

#%%
def VGG16(x, n_classes, is_pretrain=True):

x = tools.conv('conv1_1', x, 64, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
x = tools.conv('conv1_2', x, 64, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
x = tools.pool('pool1', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True)

x = tools.conv('conv2_1', x, 128, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
x = tools.conv('conv2_2', x, 128, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
x = tools.pool('pool2', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True)

x = tools.conv('conv3_1', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
x = tools.conv('conv3_2', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
x = tools.conv('conv3_3', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True)

x = tools.conv('conv4_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
x = tools.conv('conv4_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
x = tools.conv('conv4_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True)

x = tools.conv('conv5_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
x = tools.conv('conv5_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
x = tools.conv('conv5_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True)

x = tools.FC_layer('fc6', x, out_nodes=4096)
#x = tools.batch_norm(x)
x = tools.FC_layer('fc7', x, out_nodes=4096)
#x = tools.batch_norm(x)
x = tools.FC_layer('fc8', x, out_nodes=n_classes)

return x





#%% TO get better tensorboard figures!

def VGG16N(x, n_classes, is_pretrain=True):

with tf.name_scope('VGG16'):

x = tools.conv('conv1_1', x, 64, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
x = tools.conv('conv1_2', x, 64, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
with tf.name_scope('pool1'):
x = tools.pool('pool1', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True)

x = tools.conv('conv2_1', x, 128, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
x = tools.conv('conv2_2', x, 128, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
with tf.name_scope('pool2'):
x = tools.pool('pool2', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True)



x = tools.conv('conv3_1', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
x = tools.conv('conv3_2', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
x = tools.conv('conv3_3', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
with tf.name_scope('pool3'):
x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True)


x = tools.conv('conv4_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
x = tools.conv('conv4_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
x = tools.conv('conv4_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
with tf.name_scope('pool4'):
x = tools.pool('pool4', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True)


x = tools.conv('conv5_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
x = tools.conv('conv5_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
x = tools.conv('conv5_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain)
with tf.name_scope('pool5'):
x = tools.pool('pool5', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True)


x = tools.FC_layer('fc6', x, out_nodes=4096)
#with tf.name_scope('batch_norm1'):
#x = tools.batch_norm(x)
x = tools.FC_layer('fc7', x, out_nodes=4096)
#with tf.name_scope('batch_norm2'):
#x = tools.batch_norm(x)
x = tools.FC_layer('fc8', x, out_nodes=n_classes)

return x



#%%








Loading