-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
130 lines (101 loc) · 3.8 KB
/
train.py
File metadata and controls
130 lines (101 loc) · 3.8 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
126
127
128
129
130
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
import numpy as np
import cv2
from glob import glob
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
from patchify import patchify
import tensorflow as tf
from tensorflow.keras.callbacks import ModelCheckpoint, CSVLogger, ReduceLROnPlateau, EarlyStopping
from vit import ViT
""" Hyperparameters """
hp = {}
hp["image_size"] = 200
hp["num_channels"] = 3
hp["patch_size"] = 25
hp["num_patches"] = (hp["image_size"]**2) // (hp["patch_size"]**2)
hp["flat_patches_shape"] = (hp["num_patches"], hp["patch_size"]*hp["patch_size"]*hp["num_channels"])
hp["batch_size"] = 32
hp["lr"] = 1e-4
hp["num_epochs"] = 500
hp["num_classes"] = 5
hp["class_names"] = ["daisy", "dandelion", "rose", "sunflower", "tulip"]
hp["num_layers"] = 12
hp["hidden_dim"] = 768
hp["mlp_dim"] = 3072
hp["num_heads"] = 12
hp["dropout_rate"] = 0.1
def create_dir(path):
if not os.path.exists(path):
os.makedirs(path)
def load_data(path, split=0.1):
images = shuffle(glob(os.path.join(path, "*", "*.jpg")))
split_size = int(len(images) * split)
train_x, valid_x = train_test_split(images, test_size=split_size, random_state=42)
train_x, test_x = train_test_split(train_x, test_size=split_size, random_state=42)
return train_x, valid_x, test_x
def process_image_label(path):
""" Reading images """
path = path.decode()
image = cv2.imread(path, cv2.IMREAD_COLOR)
image = cv2.resize(image, (hp["image_size"], hp["image_size"]))
image = image/255.0
""" Preprocessing to patches """
patch_shape = (hp["patch_size"], hp["patch_size"], hp["num_channels"])
patches = patchify(image, patch_shape, hp["patch_size"])
# patches = np.reshape(patches, (64, 25, 25, 3))
# for i in range(64):
# cv2.imwrite(f"files/{i}.png", patches[i])
patches = np.reshape(patches, hp["flat_patches_shape"])
patches = patches.astype(np.float32)
""" Label """
class_name = path.split("/")[-2]
class_idx = hp["class_names"].index(class_name)
class_idx = np.array(class_idx, dtype=np.int32)
return patches, class_idx
def parse(path):
patches, labels = tf.numpy_function(process_image_label, [path], [tf.float32, tf.int32])
labels = tf.one_hot(labels, hp["num_classes"])
patches.set_shape(hp["flat_patches_shape"])
labels.set_shape(hp["num_classes"])
return patches, labels
def tf_dataset(images, batch=32):
ds = tf.data.Dataset.from_tensor_slices((images))
ds = ds.map(parse).batch(batch).prefetch(8)
return ds
if __name__ == "__main__":
""" Seeding """
np.random.seed(42)
tf.random.set_seed(42)
""" Directory for storing files """
create_dir("files")
""" Paths """
dataset_path = "/media/nikhil/Seagate Backup Plus Drive/ML_DATASET/flowers"
model_path = os.path.join("files", "model.h5")
csv_path = os.path.join("files", "log.csv")
""" Dataset """
train_x, valid_x, test_x = load_data(dataset_path)
print(f"Train: {len(train_x)} - Valid: {len(valid_x)} - Test: {len(test_x)}")
train_ds = tf_dataset(train_x, batch=hp["batch_size"])
valid_ds = tf_dataset(valid_x, batch=hp["batch_size"])
""" Model """
model = ViT(hp)
model.compile(
loss="categorical_crossentropy",
optimizer=tf.keras.optimizers.Adam(hp["lr"], clipvalue=1.0),
metrics=["acc"]
)
callbacks = [
ModelCheckpoint(model_path, monitor='val_loss', verbose=1, save_best_only=True),
ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10, min_lr=1e-10, verbose=1),
CSVLogger(csv_path),
EarlyStopping(monitor='val_loss', patience=50, restore_best_weights=False),
]
model.fit(
train_ds,
epochs=hp["num_epochs"],
validation_data=valid_ds,
callbacks=callbacks
)
## ...