-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcar_classifier.py
More file actions
219 lines (185 loc) · 7.51 KB
/
car_classifier.py
File metadata and controls
219 lines (185 loc) · 7.51 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
"""
================================================================================
Author: Andrea Iommi
Code Ownership:
- All Python source code in this file is written solely by the author.
Documentation Notice:
- All docstrings and inline documentation are written by ChatGPT,
but thoroughly checked and approved by the author for accuracy.
================================================================================
"""
import argparse
import json
from pathlib import Path
from torch.optim import AdamW
from torch.optim.lr_scheduler import CosineAnnealingLR
from torch.utils.data import DataLoader
from ml_utils import CustomDataset
from model.resnet_model import CNNClassifier
from training import train_classifier
def car_classifier(dataset_path:Path, metric_history:Path, model_cache:Path, new_hist:bool, batch_size:int=150,
model_name:str="resnet18", pretrained:bool=True, freeze_layers:int=0, num_epochs:int = 10,
device :str='cuda:0'):
"""
Trains a car classification model using a ResNet-based CNN architecture.
Parameters:
-----------
dataset_path (Path):
Path to the root dataset directory containing the train/valid/test splits.
Each split must be defined via CSV annotations and associated images.
metric_history (Path):
File path where the training and validation metrics will be saved in JSON format.
This allows resuming and tracking model performance across runs.
model_cache (Path):
File path where the trained model checkpoint (.pth file) will be saved.
new_hist (bool):
If True, a new empty metric history file is created, overwriting any existing file.
batch_size (int, default=150):
Number of samples per batch during training and validation.
model_name (str, default="resnet18"):
The ResNet architecture variant to use (e.g., "resnet18", "resnet34", "resnet50").
pretrained (bool, default=True):
If True, loads a pretrained ResNet model from torchvision as the base.
freeze_layers (int, default=0):
Number of initial layers to freeze (useful when fine-tuning pretrained models).
num_epochs (int, default=10):
Number of full training epochs.
device (str, default="cuda:0"):
Target device for training (e.g., "cpu", "cuda", "mps").
"""
print("--- Starting car_classifier with the following parameters ---")
print(f"Dataset Path: {dataset_path}")
print(f"Metric History Path: {metric_history}")
print(f"Model Cache Path: {model_cache}")
print(f"New History: {new_hist}")
print(f"Batch Size: {batch_size}")
print(f"Model Name: {model_name}")
print(f"Pretrained: {pretrained}")
print(f"Freeze Layers: {freeze_layers}")
print(f"Number of Epochs: {num_epochs}")
print(f"Device: {device}")
print("----------------------------------------------------------")
dataset_path.mkdir(parents=True, exist_ok=True)
metric_history.parent.mkdir(parents=True, exist_ok=True)
model_cache.parent.mkdir(parents=True, exist_ok=True)
train_dt = CustomDataset(data_path=dataset_path, portion="train")
train_loader = DataLoader(train_dt,
batch_size=batch_size, shuffle=True, num_workers=4)
valid_dt = CustomDataset(data_path=dataset_path, portion="valid")
valid_loader = DataLoader(valid_dt,
batch_size=batch_size, shuffle=False, num_workers=4)
cls = CNNClassifier(num_classes=196, model_name=model_name, pretrained=pretrained, freeze_layers=freeze_layers)
if new_hist:
with metric_history.open("w") as file:
json.dump([], file)
train_classifier(
model=cls,
dataset=(train_loader, valid_loader),
optimizer_cls=AdamW,
opt_params=dict(lr=0.0003, weight_decay=0.0001),
scheduler_cls=CosineAnnealingLR,
scheduler_params=dict(T_max=num_epochs, eta_min=1e-6),
num_epochs=num_epochs,
metric_history=metric_history,
model_cache=model_cache,
patience=5,
device=device,
state=dict()
)
def main():
"""
Entry point for the car classifier training script.
Documentation Note:
-------------------
This documentation is written by ChatGPT but reviewed and verified by the author.
The code itself is authored exclusively by the project’s author.
Functionality:
--------------
- Parses command-line arguments for dataset paths, training options, and model settings.
- Calls the `car_classifier` function with parsed arguments.
- Enables training from terminal with flexible configuration.
Example Usage:
--------------
python car_classifier.py --dataset_path datasets/car_dataset
--metric_history saved_models/resnet18/hist.json
--model_cache saved_models/resnet18/model.pth
--num_epochs 20
--batch_size 128
--device mps
```
"""
parser = argparse.ArgumentParser(description="Train a car classifier model.")
parser.add_argument(
"--dataset_path",
type=Path,
default="datasets/car_dataset",
help="Path to the dataset directory (e.g., 'datasets/car_dataset')."
)
parser.add_argument(
"--metric_history",
type=Path,
default="saved_models/resnet18/hist.json",
help="Path to save metric history JSON file (e.g., 'saved_models/resnet18/hist.json')."
)
parser.add_argument(
"--model_cache",
type=Path,
default="saved_models/resnet18/model.pth",
help="Path to cache the trained model's state dictionary (e.g., 'saved_models/resnet18/model.pth')."
)
parser.add_argument(
"--new_hist",
action="store_true",
help="If set, a new metric history file will be created, overwriting existing one."
)
parser.add_argument(
"--batch_size",
type=int,
default=150,
help="Batch size for training (default: 150)."
)
parser.add_argument(
"--model_name",
type=str,
default="resnet18",
help="Name of the model architecture to use (default: 'resnet18')."
)
parser.add_argument(
"--pretrained",
action="store_true",
help="If set, use a pretrained model from torchvision."
)
parser.add_argument(
"--freeze_layers",
type=int,
default=0,
help="Number of initial layers to freeze if using a pretrained model (default: 0)."
)
parser.add_argument(
"--num_epochs",
type=int,
default=1,
help="Number of training epochs (default: 1)."
)
parser.add_argument(
"--device",
type=str,
default="cpu", # Default to 'cpu', user can specify 'cuda' or 'mps'
help="Device to run the training on (e.g., 'cpu', 'cuda', 'mps')."
)
args = parser.parse_args()
# Call the car_classifier function with parsed arguments
car_classifier(
dataset_path=args.dataset_path,
metric_history=args.metric_history,
model_cache=args.model_cache,
new_hist=args.new_hist,
batch_size=args.batch_size,
model_name=args.model_name,
pretrained=args.pretrained,
freeze_layers=args.freeze_layers,
num_epochs=args.num_epochs,
device=args.device
)
if __name__ == "__main__":
main()