-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
38 lines (31 loc) · 1.24 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
from keras.models import load_model
from pandas.io.parsers import read_csv
from sklearn.utils import shuffle
def load_data(test=False):
"""
Loads data from FTEST if *test* is True, otherwise from FTRAIN.
Important that the files are in a `data` directory
"""
FTRAIN = 'data/training.csv'
FTEST = 'data/test.csv'
fname = FTEST if test else FTRAIN
df = read_csv(os.path.expanduser(fname)) # load dataframes
# The Image column has pixel values separated by space; convert
# the values to numpy arrays:
df['Image'] = df['Image'].apply(lambda im: np.fromstring(im, sep=' '))
df = df.dropna() # drop all rows that have missing values in them
X = np.vstack(df['Image'].values) / 255. # scale pixel values to [0, 1] (Normalizing)
X = X.astype(np.float32)
X = X.reshape(-1, 96, 96, 1) # return each images as 96 x 96 x 1
if not test: # only FTRAIN has target columns
y = df[df.columns[:-1]].values
y = (y - 48) / 48 # scale target coordinates to [-1, 1] (Normalizing)
X, y = shuffle(X, y, random_state=42) # shuffle train data
y = y.astype(np.float32)
else:
y = None
return X, y