-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdataset_utilities.py
79 lines (63 loc) · 2.54 KB
/
dataset_utilities.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
#
# This script loads and separates the target dataset in train, validation, and test components.
# It also has a function for getting a weights dictionary to the used in the NN training,
# in case the dataset is not balanced.
#
import os
import tensorflow as tf
import numpy as np
import classify_dataset
def get_datasets(data_dir, test_data_dir, batch_size=None):
if batch_size is None:
batch_size = classify_dataset.batch_size
IMG_SIZE = classify_dataset.IMG_SIZE
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
seed=123,
image_size=classify_dataset.IMG_SIZE,
label_mode='categorical',
crop_to_aspect_ratio=False,
batch_size=batch_size)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="validation",
seed=123,
image_size=IMG_SIZE,
label_mode='categorical',
crop_to_aspect_ratio=False,
batch_size=batch_size)
test_ds = tf.keras.preprocessing.image_dataset_from_directory(
test_data_dir,
seed=123,
image_size=IMG_SIZE,
label_mode='categorical',
crop_to_aspect_ratio=False,
batch_size=batch_size)
weights_dict = get_weights_dict(data_dir, train_ds.class_names)
# to improve performance, use buffered prefetching to load images
AUTOTUNE = tf.data.experimental.AUTOTUNE
train_ds = train_ds.prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.prefetch(buffer_size=AUTOTUNE)
test_ds = test_ds.prefetch(buffer_size=AUTOTUNE)
return train_ds, val_ds, test_ds, weights_dict
def get_weights_dict(data_dir, class_names):
# As the dataset is imbalanced, is is necessary to get weights for each class
# get the number of trainval images for each class
images_by_labels = []
for i in range(len(class_names)):
for subdir, dirs, files in os.walk(os.path.join(data_dir, str(i))):
n_of_files = sum([f.endswith(".jpg") or f.endswith(".mp4") for f in files])
images_by_labels.append(n_of_files)
# calculate weights
images_by_labels = np.array(images_by_labels)
avg = np.average(images_by_labels)
weights = avg / images_by_labels
# create dictionary with weights as required for keras fit() function
weights_dict = {}
for item in range(len(weights)):
weights_dict[int(class_names[item])] = weights[item]
print("weights_dict=", weights_dict)
return weights_dict