-
Notifications
You must be signed in to change notification settings - Fork 36
/
dogcat_data.py
46 lines (41 loc) · 1.46 KB
/
dogcat_data.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
import os
import glob
import subprocess
from keras.preprocessing.image import ImageDataGenerator
def get_nb_files(directory):
"""Get number of files by searching directory recursively"""
if not os.path.exists(directory):
return 0
cnt = 0
for r, dirs, files in os.walk(directory):
for dr in dirs:
cnt += len(glob.glob(os.path.join(r, dr + "/*")))
return cnt
# data prep
def generators(preprocessing_function, img_width, img_height, batch_size=32, binary=False, shuffle=True,
train_dir="../dogcat-data/train", val_dir="../dogcat-data/validation"):
train_datagen = ImageDataGenerator(
preprocessing_function=preprocessing_function,
rotation_range=30,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True
)
test_datagen = ImageDataGenerator(preprocessing_function=preprocessing_function)
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode="binary" if binary else "categorical",
shuffle=shuffle
)
validation_generator = test_datagen.flow_from_directory(
val_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode="binary" if binary else "categorical",
shuffle=shuffle
)
return train_generator, validation_generator