-
Notifications
You must be signed in to change notification settings - Fork 35
/
minimal_finetune_cars.py
205 lines (170 loc) · 6.57 KB
/
minimal_finetune_cars.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
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
import os
import random
from functools import partial
from pathlib import Path
import numpy as np
import pandas as pd
import torch
from func_to_script import script
from PIL import Image
from pytorch_accelerated.callbacks import (
EarlyStoppingCallback,
SaveBestModelCallback,
get_default_callbacks,
)
from pytorch_accelerated.schedulers import CosineLrScheduler
from torch.utils.data import Dataset
from yolov7 import create_yolov7_model
from yolov7.dataset import Yolov7Dataset, create_yolov7_transforms, yolov7_collate_fn
from yolov7.evaluation import CalculateMeanAveragePrecisionCallback
from yolov7.loss_factory import create_yolov7_loss
from yolov7.trainer import Yolov7Trainer, filter_eval_predictions
def load_cars_df(annotations_file_path, images_path):
all_images = sorted(set([p.parts[-1] for p in images_path.iterdir()]))
image_id_to_image = {i: im for i, im in enumerate(all_images)}
image_to_image_id = {v: k for k, v, in image_id_to_image.items()}
annotations_df = pd.read_csv(annotations_file_path)
annotations_df.loc[:, "class_name"] = "car"
annotations_df.loc[:, "has_annotation"] = True
# add 100 empty images to the dataset
empty_images = sorted(set(all_images) - set(annotations_df.image.unique()))
non_annotated_df = pd.DataFrame(list(empty_images)[:100], columns=["image"])
non_annotated_df.loc[:, "has_annotation"] = False
non_annotated_df.loc[:, "class_name"] = "background"
df = pd.concat((annotations_df, non_annotated_df))
class_id_to_label = dict(
enumerate(df.query("has_annotation == True").class_name.unique())
)
class_label_to_id = {v: k for k, v in class_id_to_label.items()}
df["image_id"] = df.image.map(image_to_image_id)
df["class_id"] = df.class_name.map(class_label_to_id)
file_names = tuple(df.image.unique())
random.seed(42)
validation_files = set(random.sample(file_names, int(len(df) * 0.2)))
train_df = df[~df.image.isin(validation_files)]
valid_df = df[df.image.isin(validation_files)]
lookups = {
"image_id_to_image": image_id_to_image,
"image_to_image_id": image_to_image_id,
"class_id_to_label": class_id_to_label,
"class_label_to_id": class_label_to_id,
}
return train_df, valid_df, lookups
class CarsDatasetAdaptor(Dataset):
def __init__(
self,
images_dir_path,
annotations_dataframe,
transforms=None,
):
self.images_dir_path = Path(images_dir_path)
self.annotations_df = annotations_dataframe
self.transforms = transforms
self.image_idx_to_image_id = {
idx: image_id
for idx, image_id in enumerate(self.annotations_df.image_id.unique())
}
self.image_id_to_image_idx = {
v: k for k, v, in self.image_idx_to_image_id.items()
}
def __len__(self) -> int:
return len(self.image_idx_to_image_id)
def __getitem__(self, index):
image_id = self.image_idx_to_image_id[index]
image_info = self.annotations_df[self.annotations_df.image_id == image_id]
file_name = image_info.image.values[0]
assert image_id == image_info.image_id.values[0]
image = Image.open(self.images_dir_path / file_name).convert("RGB")
image = np.array(image)
image_hw = image.shape[:2]
if image_info.has_annotation.any():
xyxy_bboxes = image_info[["xmin", "ymin", "xmax", "ymax"]].values
class_ids = image_info["class_id"].values
else:
xyxy_bboxes = np.array([])
class_ids = np.array([])
if self.transforms is not None:
transformed = self.transforms(
image=image, bboxes=xyxy_bboxes, labels=class_ids
)
image = transformed["image"]
xyxy_bboxes = np.array(transformed["bboxes"])
class_ids = np.array(transformed["labels"])
return image, xyxy_bboxes, class_ids, image_id, image_hw
DATA_PATH = Path("/".join(Path(__file__).absolute().parts[:-2])) / "data/cars"
@script
def main(
data_path: str = DATA_PATH,
image_size: int = 640,
pretrained: bool = True,
num_epochs: int = 30,
batch_size: int = 8,
):
# Load data
data_path = Path(data_path)
images_path = data_path / "training_images"
annotations_file_path = data_path / "annotations.csv"
train_df, valid_df, lookups = load_cars_df(annotations_file_path, images_path)
num_classes = 1
# Create datasets
train_ds = CarsDatasetAdaptor(
images_path,
train_df,
)
eval_ds = CarsDatasetAdaptor(images_path, valid_df)
train_yds = Yolov7Dataset(
train_ds,
create_yolov7_transforms(training=True, image_size=(image_size, image_size)),
)
eval_yds = Yolov7Dataset(
eval_ds,
create_yolov7_transforms(training=False, image_size=(image_size, image_size)),
)
# Create model, loss function and optimizer
model = create_yolov7_model(
architecture="yolov7", num_classes=num_classes, pretrained=pretrained
)
loss_func = create_yolov7_loss(model, image_size=image_size)
optimizer = torch.optim.SGD(
model.parameters(), lr=0.01, momentum=0.9, nesterov=True
)
# Create trainer and train
trainer = Yolov7Trainer(
model=model,
optimizer=optimizer,
loss_func=loss_func,
filter_eval_predictions_fn=partial(
filter_eval_predictions, confidence_threshold=0.01, nms_threshold=0.3
),
callbacks=[
CalculateMeanAveragePrecisionCallback.create_from_targets_df(
targets_df=valid_df.query("has_annotation == True")[
["image_id", "xmin", "ymin", "xmax", "ymax", "class_id"]
],
image_ids=set(valid_df.image_id.unique()),
iou_threshold=0.2,
),
SaveBestModelCallback(watch_metric="map", greater_is_better=True),
EarlyStoppingCallback(
early_stopping_patience=3,
watch_metric="map",
greater_is_better=True,
early_stopping_threshold=0.001,
),
*get_default_callbacks(progress_bar=True),
],
)
trainer.train(
num_epochs=num_epochs,
train_dataset=train_yds,
eval_dataset=eval_yds,
per_device_batch_size=batch_size,
create_scheduler_fn=CosineLrScheduler.create_scheduler_fn(
num_warmup_epochs=5,
num_cooldown_epochs=5,
k_decay=2,
),
collate_fn=yolov7_collate_fn,
)
if __name__ == "__main__":
main()