-
Notifications
You must be signed in to change notification settings - Fork 2
/
helper_functions.py
149 lines (127 loc) · 4.93 KB
/
helper_functions.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import numpy as np
from numpy import expand_dims
import matplotlib.pyplot as plt
import pandas as pd
from IPython.display import clear_output
from time import sleep
import os
from sklearn import metrics
import seaborn as sns
import cv2
from math import sin, cos, pi
from keras.layers import Conv2D,Dropout,Dense,Flatten
from keras.models import Sequential
from keras.layers.advanced_activations import LeakyReLU
from keras.models import Sequential, Model
from keras.layers import Activation, Convolution2D, MaxPooling2D, BatchNormalization, Flatten, Dense, Dropout, Conv2D,MaxPool2D, ZeroPadding2D
from keras.models import model_from_json
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import ImageDataGenerator
import tensorflow
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications.resnet50 import preprocess_input
import torch
from torch.utils.data import Dataset, DataLoader
from torch.utils.data.sampler import SubsetRandomSampler
from torchvision import transforms, utils
from torch import nn, optim
import torch.nn.functional as F
import warnings
warnings.filterwarnings('ignore')
#Read data
#sample = pd.read_csv(data_dir + 'SampleSubmission.csv')
#train_data = pd.read_csv(data_dir + 'training.csv')
#test_data = pd.read_csv(data_dir + 'test.csv')
data_dir = 'data_files/'
lookid_data = pd.read_csv(data_dir + 'IdLookupTable.csv')
sample_sub = pd.read_csv(data_dir + 'SampleSubmission.csv')
train_data1 = pd.read_parquet(data_dir + 'train_data_1.gzip')
train_data2 = pd.read_parquet(data_dir + 'train_data_2.gzip')
train_data = pd.concat([train_data1, train_data2])
test_data = pd.read_parquet(data_dir + 'test_data.gzip')
#No missing features
clean_data = train_data.copy().dropna()
IMG_SIZE = 96 # image size 96 x 96 pixels
def show_keypoints(image, keypoints):
'''
Show image with keypoints
Args:
image (array-like or PIL image): The image data. (M, N)
keypoints (array-like): The keypoits data. (N, 2)
'''
plt.imshow(image, cmap='gray')
if len(keypoints):
plt.scatter(keypoints[:, 0], keypoints[:, 1], s=24, marker ='.', c='r')
def show_images(df, indxs, ncols=5, figsize=(15,10), with_keypoints=True):
'''
Show images with keypoints in grids
Args:
df (DataFrame): data (M x N)
idxs (iterators): list, Range, Indexes
ncols (integer): number of columns (images by rows)
figsize (float, float): width, height in inches
with_keypoints (boolean): True if show image with keypoints
'''
plt.figure(figsize=figsize)
nrows = len(indxs) // ncols + 1
for i, idx in enumerate(indxs):
image = np.fromstring(df.loc[idx, 'Image'], sep=' ').astype(np.float32)\
.reshape(-1, IMG_SIZE)
if with_keypoints:
keypoints = df.loc[idx].drop('Image').values.astype(np.float32)\
.reshape(-1, 2)
else:
keypoints = []
plt.subplot(nrows, ncols, i + 1)
plt.title(f'Sample #{idx}')
plt.axis('off')
plt.tight_layout()
show_keypoints(image, keypoints)
plt.show()
def get_features(df, dim=2):
"""
Input train or test dataframe and number of dimensions you want features in.
Returns vector of features (pixel intensities for all examples)
TODO: divided by 255 for scaling?
"""
images_list = []
df1 = df.copy()
df1.reset_index(inplace = True)
for i in range(0, df1.shape[0]):
image = df1["Image"][i].split(' ')
image = ["0" if x == '' else x for x in image]
images_list.append(image)
images_array = np.array(images_list, dtype="float")
if dim==2:
images_features = images_array.reshape(-1, 96, 96, 1)
else:
images_features = images_array
return images_features
def get_labels(df):
'''
Input only test dataframe
Returns vector of labels (num_examples by 30 column vector of X,Y coords for face keypoints)
Grabbing the corresponding training labels
'''
labels_df = df.copy()
labels_df = labels_df.drop('Image',axis = 1)
y_train = []
for i in range(0,len(labels_df)):
y = labels_df.iloc[i,:]
y_train.append(y)
return np.array(y_train,dtype = 'float')
def plot_img(image, label, axis):
'''
Simplfied version of show_images
Shows one image
'''
image = image.reshape(96,96)
axis.imshow(image, cmap='gray')
axis.scatter(label[0::2], label[1::2], s=24, marker ='.', c='r')
class Normalize(object):
'''Normalize input images'''
def __call__(self, sample):
image, keypoints = sample['image'], sample['keypoints']
return {'image': image / 255., # scale to [0, 1]
'keypoints': keypoints}