-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreator_dataset.py
84 lines (62 loc) · 2.69 KB
/
Creator_dataset.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
import os
from importlib_metadata import re
import tensorflow as tf
import pandas as pd
import matplotlib.pyplot as plt
from tensorflow import keras
from tensorflow.keras import layers
import glob
# Source
# https://github.com/aladdinpersson/Machine-Learning-Collection/blob/master/ML/TensorFlow/Basics/tutorial18-customdata-images/2_csv_file.py
# https://www.youtube.com/watch?v=q7ZuZ8ZOErE
class Creator_dataset():
def __init__(self, batchsize):
self.batchsize = batchsize
def create_dataset(self, dir_name, file_name, split=False):
directory = "02_created_data/" + dir_name + "/"
path_csv = directory + file_name + ".csv"
df = pd.read_csv(path_csv)
file_paths = df["file_name"].values
labels = df["label"].values
dataset = tf.data.Dataset.from_tensor_slices((file_paths, labels))
def read_image(image_file, label):
image = tf.io.read_file(directory + image_file)
image = tf.image.decode_image(image, channels=3, dtype=tf.float32)
return image, label
dataset = dataset.map(read_image).batch(self.batchsize)
print('-'*20)
print("Dataset " + str(dir))
print(file_name + "\t" + str(len(file_paths)) + "\t" + str(len(dataset)) + " bt. x " + str(self.batchsize) + " bt.size" + ": done")
print('-'*20)
if (split):
train_size = int(0.8 * len(dataset))
val_size = int(0.1 * len(dataset))
train_ds = dataset.take(train_size)
val_ds = dataset.skip(train_size).take(val_size)
test_ds = dataset.skip(train_size).skip(val_size)
print("Train\t\t" + str(len(list(train_ds))) + " bt. x " + str(self.batchsize) + " bt.size" + ": done")
print("Val\t\t" + str(len(list(val_ds))) + " bt. x " + str(self.batchsize) + " bt.size" + ": done")
print("Test\t\t" + str(len(list(test_ds))) + " bt. x " + str(self.batchsize) + " bt.size" + ": done")
return train_ds, val_ds, test_ds
return dataset
if __name__ == "__main__":
creator_dataset = Creator_dataset(32)
# ds_train = creator_dataset.create_dataset("occupancy", "occupancy_test")
ds_train = creator_dataset.create_dataset("piece", "piece_test")
# Notes
# def process_path(file_path):
# label = get_label(file_path)
# img = tf.io.read_file(file_path)
# img = tf.image.decode_jpeg(img, channels=3)
# img = tf.image.convert_image_dtype(img, tf.float32)
# img = tf.image.resize(img, size=(180, 180))
# return img, label
# ds_train = ds_train.map(read_image)#.batch(self.batchsize)
# directory = "02_created_data/MH/"
# ds_train = tf.keras.utils.image_dataset_from_directory(
# directory,
# validation_split=0.2,
# subset="training",
# seed=123,
# image_size=(150, 150),
# batch_size=32)