-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_preprocess.py
112 lines (87 loc) · 3.6 KB
/
data_preprocess.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
import numpy as np
import cv2
from skimage import exposure
from sklearn.model_selection import train_test_split
import glob
from tqdm import tqdm
def rotate_image(img, angle):
(rows, cols, ch) = img.shape
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), angle, 1)
return cv2.warpAffine(img, M, (cols, rows))
def load_blur_img(path, img_size):
img = cv2.imread(path)
angle = np.random.randint(0, 360)
img = rotate_image(img, angle)
img = cv2.blur(img, (5, 5))
img = cv2.resize(img, img_size)
return img
def load_img_class(class_path, class_label, class_size, img_size, name):
x = []
y = []
pbar = tqdm(range(class_size), position=0, leave=True)
pbar.set_description(name)
for path in class_path:
img = load_blur_img(path, img_size)
x.append(img)
y.append(class_label)
pbar.update(1)
while len(x) < class_size:
rand_idx = np.random.randint(0, len(class_path))
img = load_blur_img(class_path[rand_idx], img_size)
x.append(img)
y.append(class_label)
pbar.update(1)
pbar.close()
return x, y
def load_data(img_size, class_size, hot_dogs, not_hot_dogs):
img_size = (img_size, img_size)
x_hot_dog, y_hot_dog = load_img_class(hot_dogs, 0, class_size, img_size, name="Hotdog")
x_not_hot_dog, y_not_hot_dog = load_img_class(not_hot_dogs, 1, class_size, img_size, name="Not hotdog")
print("There are", len(x_hot_dog), "hotdog images")
print("There are", len(x_not_hot_dog), "not hotdog images")
X = np.array(x_hot_dog + x_not_hot_dog)
y = np.array(y_hot_dog + y_not_hot_dog)
return X, y
def to_gray(images):
# rgb2gray converts RGB values to grayscale values by forming a weighted sum of the R, G, and B components:
# 0.2989 * R + 0.5870 * G + 0.1140 * B
# source: https://www.mathworks.com/help/matlab/ref/rgb2gray.html
images = 0.2989 * images[:, :, :, 0] + 0.5870 * images[:, :, :, 1] + 0.1140 * images[:, :, :, 2]
return images
def normalize_images(images):
# use Histogram equalization to get a better range
# source http://scikit-image.org/docs/dev/api/skimage.exposure.html#skimage.exposure.equalize_hist
images = (images / 255.).astype(np.float32)
for i in range(images.shape[0]):
images[i] = exposure.equalize_hist(images[i])
images = images.reshape(images.shape + (1,))
return images
def preprocess_data(images):
gray_images = to_gray(images)
return normalize_images(gray_images)
def to_categorical(labels, num_classes):
"""
One-hot-encode the labels
"""
encoded_labels = np.zeros((len(labels), num_classes))
for i in range(len(labels)):
encoded_labels[i, labels[i]] = 1
return encoded_labels
def get_data():
size = 32
class_size = 20000
# class_size = 1000
rand_state = 42
np.random.seed(rand_state)
hotdogs = glob.glob('./input/seefood/train/hot_dog/**/*.jpg', recursive=True)
hotdogs += glob.glob('./input/seefood/test/hot_dog/**/*.jpg', recursive=True)
not_hotdogs = glob.glob('./input/seefood/train/not_hot_dog/**/*.jpg', recursive=True)
not_hotdogs += glob.glob('./input/seefood/test/not_hot_dog/**/*.jpg', recursive=True)
scaled_X, y = load_data(size, class_size, hotdogs, not_hotdogs)
scaled_X = preprocess_data(scaled_X)
scaled_X = scaled_X.swapaxes(1, -1)
y = to_categorical(y, 2)
X_train, X_test, y_train, y_test = train_test_split(scaled_X, y,
test_size=0.2,
random_state=None)
return X_train, X_test, y_train, y_test