-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmy_utils.py
More file actions
126 lines (86 loc) · 3.13 KB
/
my_utils.py
File metadata and controls
126 lines (86 loc) · 3.13 KB
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
import shutil
import matplotlib.pyplot as plt
import numpy as np
import os
import glob
from sklearn.model_selection import train_test_split
import shutil
import csv
from tensorflow.keras.preprocessing.image import ImageDataGenerator
def display_some_examples(examples, labels):
plt.figure(figsize=(10,10))
for i in range(25):
idx = np.random.randint(0, examples.shape[0]-1)
img = examples[idx]
label = labels[idx]
plt.subplot(5,5, i+1)
plt.title(str(label))
plt.tight_layout()
plt.imshow(img, cmap='gray')
plt.show()
def split_data(path_to_data, path_to_save_train, path_to_save_val, split_size=0.1):
folders = os.listdir(path_to_data)
for folder in folders:
full_path = os.path.join(path_to_data, folder)
images_paths = glob.glob(os.path.join(full_path, '*.png'))
x_train, x_val = train_test_split(images_paths, test_size=split_size)
for x in x_train:
path_to_folder = os.path.join(path_to_save_train, folder)
if not os.path.isdir(path_to_folder):
os.makedirs(path_to_folder)
shutil.copy(x, path_to_folder)
for x in x_val:
path_to_folder = os.path.join(path_to_save_val, folder)
if not os.path.isdir(path_to_folder):
os.makedirs(path_to_folder)
shutil.copy(x, path_to_folder)
def order_test_set(path_to_images, path_to_csv):
try:
with open(path_to_csv, 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for i, row in enumerate(reader):
if i==0:
continue
img_name = row[-1].replace('Test/', '')
label = row[-2]
path_to_folder = os.path.join(path_to_images, label)
if not os.path.isdir(path_to_folder):
os.makedirs(path_to_folder)
img_full_path = os.path.join(path_to_images, img_name)
shutil.move(img_full_path, path_to_folder)
except:
print('[INFO] : Error reading csv file')
def create_generators(batch_size, train_data_path, val_data_path, test_data_path):
train_preprocessor = ImageDataGenerator(
rescale = 1 / 255.,
rotation_range=10,
width_shift_range=0.1
)
test_preprocessor = ImageDataGenerator(
rescale = 1 / 255.,
)
train_generator = train_preprocessor.flow_from_directory(
train_data_path,
class_mode="categorical",
target_size=(60,60),
color_mode='rgb',
shuffle=True,
batch_size=batch_size
)
val_generator = test_preprocessor.flow_from_directory(
val_data_path,
class_mode="categorical",
target_size=(60,60),
color_mode="rgb",
shuffle=False,
batch_size=batch_size,
)
test_generator = test_preprocessor.flow_from_directory(
test_data_path,
class_mode="categorical",
target_size=(60,60),
color_mode="rgb",
shuffle=False,
batch_size=batch_size,
)
return train_generator, val_generator, test_generator