Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files via upload #117

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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
Binary file added CK+_VGG19/Confusion Matrix.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
180 changes: 90 additions & 90 deletions CK.py
Original file line number Diff line number Diff line change
@@ -1,90 +1,90 @@
from __future__ import print_function
from PIL import Image
import numpy as np
import h5py
import torch.utils.data as data
class CK(data.Dataset):
"""`CK+ Dataset.
Args:
train (bool, optional): If True, creates dataset from training set, otherwise
creates from test set.
transform (callable, optional): A function/transform that takes in an PIL image
and returns a transformed version. E.g, ``transforms.RandomCrop``
there are 135,177,75,207,84,249,54 images in data
we choose 123,159,66,186,75,225,48 images for training
we choose 12,8,9,21,9,24,6 images for testing
the split are in order according to the fold number
"""
def __init__(self, split='Training', fold = 1, transform=None):
self.transform = transform
self.split = split # training set or test set
self.fold = fold # the k-fold cross validation
self.data = h5py.File('./data/CK_data.h5', 'r', driver='core')
number = len(self.data['data_label']) #981
sum_number = [0,135,312,387,594,678,927,981] # the sum of class number
test_number = [12,18,9,21,9,24,6] # the number of each class
test_index = []
train_index = []
for j in xrange(len(test_number)):
for k in xrange(test_number[j]):
if self.fold != 10: #the last fold start from the last element
test_index.append(sum_number[j]+(self.fold-1)*test_number[j]+k)
else:
test_index.append(sum_number[j+1]-1-k)
for i in xrange(number):
if i not in test_index:
train_index.append(i)
print(len(train_index),len(test_index))
# now load the picked numpy arrays
if self.split == 'Training':
self.train_data = []
self.train_labels = []
for ind in xrange(len(train_index)):
self.train_data.append(self.data['data_pixel'][train_index[ind]])
self.train_labels.append(self.data['data_label'][train_index[ind]])
elif self.split == 'Testing':
self.test_data = []
self.test_labels = []
for ind in xrange(len(test_index)):
self.test_data.append(self.data['data_pixel'][test_index[ind]])
self.test_labels.append(self.data['data_label'][test_index[ind]])
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
tuple: (image, target) where target is index of the target class.
"""
if self.split == 'Training':
img, target = self.train_data[index], self.train_labels[index]
elif self.split == 'Testing':
img, target = self.test_data[index], self.test_labels[index]
# doing this so that it is consistent with all other datasets
# to return a PIL Image
img = img[:, :, np.newaxis]
img = np.concatenate((img, img, img), axis=2)
img = Image.fromarray(img)
if self.transform is not None:
img = self.transform(img)
return img, target
def __len__(self):
if self.split == 'Training':
return len(self.train_data)
elif self.split == 'Testing':
return len(self.test_data)
from __future__ import print_function
from PIL import Image
import numpy as np
import h5py
import torch.utils.data as data


class CK(data.Dataset):
"""`CK+ Dataset.

Args:
train (bool, optional): If True, creates dataset from training set, otherwise
creates from test set.
transform (callable, optional): A function/transform that takes in an PIL image
and returns a transformed version. E.g, ``transforms.RandomCrop``

there are 135,177,75,207,84,249,54 images in data
we choose 123,159,66,186,75,225,48 images for training
we choose 12,8,9,21,9,24,6 images for testing
the split are in order according to the fold number
"""

def __init__(self, split='Training', fold = 1, transform=None):
self.transform = transform
self.split = split # training set or test set
self.fold = fold # the k-fold cross validation
self.data = h5py.File('./data/CK_data.h5', 'r', driver='core')

number = len(self.data['data_label']) #981
sum_number = [0,135,312,387,594,678,927,981] # the sum of class number
test_number = [12,18,9,21,9,24,6] # the number of each class

test_index = []
train_index = []

for j in range(len(test_number)):
for k in range(test_number[j]):
if self.fold != 10: #the last fold start from the last element
test_index.append(sum_number[j]+(self.fold-1)*test_number[j]+k)
else:
test_index.append(sum_number[j+1]-1-k)

for i in range(number):
if i not in test_index:
train_index.append(i)

print(len(train_index),len(test_index))

# now load the picked numpy arrays
if self.split == 'Training':
self.train_data = []
self.train_labels = []
for ind in range(len(train_index)):
self.train_data.append(self.data['data_pixel'][train_index[ind]])
self.train_labels.append(self.data['data_label'][train_index[ind]])

elif self.split == 'Testing':
self.test_data = []
self.test_labels = []
for ind in range(len(test_index)):
self.test_data.append(self.data['data_pixel'][test_index[ind]])
self.test_labels.append(self.data['data_label'][test_index[ind]])

def __getitem__(self, index):
"""
Args:
index (int): Index

Returns:
tuple: (image, target) where target is index of the target class.
"""
if self.split == 'Training':
img, target = self.train_data[index], self.train_labels[index]
elif self.split == 'Testing':
img, target = self.test_data[index], self.test_labels[index]
# doing this so that it is consistent with all other datasets
# to return a PIL Image
img = img[:, :, np.newaxis]
img = np.concatenate((img, img, img), axis=2)
img = Image.fromarray(img)
if self.transform is not None:
img = self.transform(img)
return img, target

def __len__(self):
if self.split == 'Training':
return len(self.train_data)
elif self.split == 'Testing':
return len(self.test_data)

12 changes: 7 additions & 5 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
A CNN based pytorch implementation on facial expression recognition (FER2013 and CK+), achieving 73.112% (state-of-the-art) in FER2013 and 94.64% in CK+ dataset

## Demos ##
![Image text](https://raw.githubusercontent.com/WuJie1010/Facial-Expression-Recognition.Pytorch/master/demo/1.png)
![Image text](https://raw.githubusercontent.com/WuJie1010/Facial-Expression-Recognition.Pytorch/master/demo/2.png)
![Image text](https://github.com/xmtybb/Facial-Expression-Recognition.Pytorch/blob/xmtybb-patch-1/images/results/3.png)
![Image text](https://github.com/xmtybb/Facial-Expression-Recognition.Pytorch/blob/xmtybb-patch-1/images/results/4.png)

## Dependencies ##
- Python 2.7
- Pytorch >=0.2.0
- Python 3.7
- Pytorch 1.8.1
- h5py (Preprocessing)
- sklearn (plot confusion matrix)

## Visualize for a test image by a pre-trained model ##
- Firstly, download the pre-trained model from https://drive.google.com/open?id=1Oy_9YmpkSKX1Q8jkOhJbz3Mc7qjyISzU (or https://pan.baidu.com/s/1gCL0TlCwKctAy_5yhzHy5Q, key: g2d3) and then put it in the "FER2013_VGG19" folder; Next, Put the test image (rename as 1.jpg) into the "images" folder, then
- Download the "FER2013_VGG19" pre-trained model from https://drive.google.com/open?id=1Oy_9YmpkSKX1Q8jkOhJbz3Mc7qjyISzU (or https://pan.baidu.com/s/1gCL0TlCwKctAy_5yhzHy5Q, key: g2d3)
-
- for CK+/VGG19
- python visualize.py

## FER2013 Dataset ##
Expand Down
Binary file added images/3.jpeg
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 images/4.jpeg
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 images/results/3.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 images/results/4.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 images/results/l.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading