-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_classification_ode.py
More file actions
269 lines (218 loc) · 8.39 KB
/
main_classification_ode.py
File metadata and controls
269 lines (218 loc) · 8.39 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# type: ignore
## Dataset Things
from torchvision.datasets.imagenet import ImageFolder
from torchvision.datasets import CIFAR100, CIFAR10
from datasets.collator import Collator
import utils
from train import train_classification_task
from test import test_classification_task
from transformers import (
AutoConfig,
ViTForImageClassification,
ViTConfig,
ResNetForImageClassification,
ViTImageProcessor,
)
from models.wrapper_ode_new import NeuralODEIntrepretation
from models.ode_transformer_gpt import ViTNeuralODE
from models.macaron import ViTMacaron
from models.ode_resnet import ODEResNet
## Common packages
import torch
from torch.utils.data import DataLoader
from torch.optim import AdamW
from transformers.optimization import (
get_cosine_schedule_with_warmup,
get_cosine_with_hard_restarts_schedule_with_warmup,
)
## Typing Packages
## Configuration Package
from omegaconf import DictConfig
from hydra import initialize, compose
## Experiment Tracking packages
import tqdm
## Common packages
import os
import wandb
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
def main(cfg: DictConfig):
device = "cuda" if torch.cuda.is_available() else "cpu"
if cfg.log_wandb == True:
wandb.login()
config = dict(cfg.setup.dict)
config.update(dict(cfg.modeling.inputs))
wandb_logger = wandb.init(
project=cfg.setup.wandb.project,
group=cfg.setup.wandb.group,
name=cfg.setup.wandb.name,
config=config,
settings=wandb.Settings(code_dir="./models/"),
)
else:
wandb_logger = False
if cfg.data.dataset.name == "cifar100":
train_dataset = CIFAR100(
root=cfg.data.dataset.dataset_path, download=False, train=True
)
validation_dataset = CIFAR100(
root=cfg.data.dataset.dataset_path, download=False, train=False
)
elif cfg.data.dataset.name == "cifar10":
train_dataset = CIFAR10(
root=cfg.data.dataset.dataset_path, download=False, train=True
)
validation_dataset = CIFAR10(
root=cfg.data.dataset.dataset_path, download=False, train=False
)
else:
train_dataset = ImageFolder(root=cfg.data.dataset.dataset_path + "/train")
validation_dataset = ImageFolder(root=cfg.data.dataset.dataset_path + "/val")
if cfg.modeling.type == "vit":
model = ViTNeuralODE(**cfg.modeling.inputs)
elif cfg.modeling.type == "macaron":
model = ViTMacaron(**cfg.modeling.inputs)
else:
model = ODEResNet(**cfg.modeling.inputs)
teacher_model = ViTForImageClassification.from_pretrained("facebook/dino-vitb16")
model.patch_embed.cls_token = teacher_model.vit.embeddings.cls_token
model.patch_embed.cls_token.requires_grad = False
# model.patch_embed.proj.weight.data.copy_(
# teacher_model.vit.embeddings.patch_embeddings.projection.weight.data
# )
# for param in model.patch_embed.proj.parameters():
# param.requires_grad = False
save_path = "/data/users/cboned/checkpoints"
checkpoint_name = f"{save_path}/VIT_ODE_CIFAR100.pt"
if cfg.infer_from_checkpoint:
weight_to_update = torch.load(checkpoint_name, weights_only=True)
for w in weight_to_update.keys():
if model.state_dict().get(w) is not None:
model.state_dict()[w].data.copy_(weight_to_update[w])
model_parameters = sum(p.numel() for p in model.parameters() if p.requires_grad)
if wandb_logger:
wandb_logger.log({"model_parameters": model_parameters})
print(
"Training Model with a total parameters of", model_parameters / 1e6, "Millions"
)
model = model.to(device)
processor = ViTImageProcessor.from_pretrained("facebook/dino-vitb16")
collator = Collator(processor)
train_dloader = DataLoader(
train_dataset,
**cfg.data.collator.train,
collate_fn=collator.classification_collate_fn,
)
val_dloader = DataLoader(
validation_dataset,
collate_fn=collator.classification_collate_fn,
**cfg.data.collator.val,
)
initial_lr = 1e-4
optimizer = torch.optim.AdamW(
model.parameters(),
lr=initial_lr,
weight_decay=5e-2,
)
## ** Scheduler
steps_per_epoch = len(train_dloader) # Number of batches per epoch
total_steps = cfg.setup.dict.epochs * steps_per_epoch
# Warmup steps (e.g., 10% of total steps)
warmup_steps = int(0.1 * total_steps)
num_cycles = cfg.setup.dict.epochs // 100
optimal_loss = 0.0
save_path = "/data/users/cboned/checkpoints"
os.makedirs(save_path, exist_ok=True)
checkpoint_name = f"{save_path}/{cfg.modeling.checkpoint_name}.pt"
scheduler = get_cosine_with_hard_restarts_schedule_with_warmup(
optimizer,
num_warmup_steps=warmup_steps,
num_training_steps=total_steps,
num_cycles=num_cycles,
)
optimal_loss = 0.0
save_path = "/data/users/cboned/checkpoints"
os.makedirs(save_path, exist_ok=True)
checkpoint_name = f"{save_path}/{cfg.modeling.checkpoint_name}.pt"
criterion = torch.nn.CrossEntropyLoss(label_smoothing=0.01)
if cfg.log_wandb == True:
wandb_logger.watch(model, log="all")
for epoch in tqdm.tqdm(
range(1, cfg.setup.dict.epochs),
desc="Training Procedure",
position=0,
leave=False,
):
if epoch > 100:
for p in model.patch_embed.proj.parameters():
p.requires_grad = True
_, train_loss = train_classification_task(
dataloader=train_dloader,
model=model,
optimizer=optimizer,
criterion=criterion,
wandb_logger=wandb_logger,
scheduler=scheduler,
epoch=epoch,
num_accumulation_steps=cfg.setup.dict.accumulation_steps,
log_every=cfg.setup.dict.log_every,
)
print(f"Loss Epoch: {epoch} Value: {train_loss}")
if ((epoch) % 1) == 0:
model, loss_validation, acc_validation = test_classification_task(
dataloader=val_dloader,
model=model,
criterion=criterion,
wandb_logger=wandb_logger,
)
updated, optimal_loss = utils.update_and_save_model_pt(
previous_metric=optimal_loss,
actual_metric=acc_validation,
model=model,
optimizer=optimizer,
lr_scheduler=optimizer.param_groups[0]["lr"],
checkpoint_path=checkpoint_name,
compare=">",
)
if updated:
print(
f"Model Updated: Validation Loss Epoch: {0} Value: {acc_validation} Optimal_loss: {optimal_loss}"
)
print("End of training")
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"-c",
"--config_file",
required=True,
help="Yaml Config file with the configuration for the models to run, for now the models to run are [defoDeTR, DeTR, CondDetr, Yolo]",
)
parser.add_argument(
"-cp",
"--config_path",
required=True,
help="path where the yaml configs are stored",
)
args = parser.parse_args()
import os
from PIL import Image, UnidentifiedImageError
def delete_corrupted_images(root_dir):
num_deleted = 0
for root, _, files in os.walk(root_dir):
for file in files:
if file.lower().endswith((".jpg", ".jpeg", ".png", ".bmp", ".gif")):
file_path = os.path.join(root, file)
try:
with Image.open(file_path) as img:
img.verify() # Verifies that it's an image
except (UnidentifiedImageError, IOError, OSError) as e:
print(f"Deleting corrupted image: {file_path} — Reason: {e}")
os.remove(file_path)
num_deleted += 1
print(f"\nFinished. Deleted {num_deleted} corrupted images.")
# delete_corrupted_images("/data/users/cboned/data/Generic/Imagenet1k/train")
# delete_corrupted_images("/data/users/cboned/data/Generic/Imagenet1k/val")
with initialize(version_base="1.3.2", config_path=args.config_path):
cfg = compose(config_name=args.config_file)
main(cfg=cfg)