forked from ariG23498/fine-tune-paligemma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject_detection_ft.py
189 lines (158 loc) · 6.13 KB
/
object_detection_ft.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
import re
import numpy as np
import torch
from torch.utils.data import DataLoader
from transformers import AutoProcessor, PaliGemmaForConditionalGeneration
from datasets import load_dataset
from configs import object_detection_config
from paligemma_ft.data_utis import collate_fn
from paligemma_ft.model_utils import freeze_layers
from functools import partial
from matplotlib import pyplot as plt, patches
DETECT_RE = re.compile(
r"(.*?)" + r"((?:<loc\d{4}>){4})\s*" + r"([^;<>]+) ?(?:; )?",
)
def extract_objects(detection_string, image_width, image_height, unique_labels=False):
objects = []
seen_labels = set()
while detection_string:
match = DETECT_RE.match(detection_string)
if not match:
break
prefix, locations, label = match.groups()
location_values = [int(loc) for loc in re.findall(r"\d{4}", locations)]
y1, x1, y2, x2 = [value / 1024 for value in location_values]
y1, x1, y2, x2 = map(
round,
(y1 * image_height, x1 * image_width, y2 * image_height, x2 * image_width),
)
label = label.strip() # Remove trailing spaces from label
if unique_labels and label in seen_labels:
label = (label or "") + "'"
seen_labels.add(label)
objects.append(dict(xyxy=(x1, y1, x2, y2), name=label))
detection_string = detection_string[len(match.group()) :]
return objects
def draw_bbox(image, objects):
fig, ax = plt.subplots(1)
ax.imshow(image)
for obj in objects:
bbox = obj["xyxy"]
rect = patches.Rectangle(
(bbox[0], bbox[1]),
bbox[2] - bbox[0],
bbox[3] - bbox[1],
linewidth=2,
edgecolor="r",
facecolor="none",
)
ax.add_patch(rect)
plt.text(
bbox[0], bbox[1] - 10, "plate", color="red", fontsize=12, weight="bold"
)
plt.show()
def infer_on_model(model, test_batch, before_pt=True):
# hardcoding the index to get same before and after results
index = 0
# help from : https://discuss.huggingface.co/t/vitimageprocessor-output-visualization/76335/6
mean = processor.image_processor.image_mean
std = processor.image_processor.image_std
pixel_value = test_batch["pixel_values"][index].cpu().to(torch.float32)
unnormalized_image = (
pixel_value.numpy() * np.array(std)[:, None, None]
) + np.array(mean)[:, None, None]
unnormalized_image = (unnormalized_image * 255).astype(np.uint8)
unnormalized_image = np.moveaxis(unnormalized_image, 0, -1)
with torch.inference_mode():
generated_outputs = model.generate(
**test_batch, max_new_tokens=100, do_sample=False
)
generated_outputs = processor.batch_decode(
generated_outputs, skip_special_tokens=True
)
if before_pt:
# generation of the pre trained model
for element in generated_outputs:
location = element.split("\n")[1]
if location == "":
print("No bbox found")
else:
print(location)
else:
# generation of the fine tuned model
element = generated_outputs[index]
detection_string = element.split("\n")[1]
objects = extract_objects(detection_string, 224, 224, unique_labels=False)
draw_bbox(unnormalized_image, objects)
if __name__ == "__main__":
# get the device
device = "cuda:0" if torch.cuda.is_available() else "cpu"
# load the dataset
print(f"[INFO] loading {object_detection_config.DATASET_ID} from hub...")
train_dataset = load_dataset(object_detection_config.DATASET_ID, split="train")
test_dataset = load_dataset(object_detection_config.DATASET_ID, split="test")
print(f"[INFO] {len(train_dataset)=}")
print(f"[INFO] {len(test_dataset)=}")
# get the processor
print(f"[INFO] loading {object_detection_config.MODEL_ID} processor from hub...")
processor = AutoProcessor.from_pretrained(object_detection_config.MODEL_ID)
# build the data loaders
print("[INFO] building the data loaders...")
train_dataloader = DataLoader(
train_dataset,
collate_fn=partial(
collate_fn,
image_title="image",
prompt="Detect license plate.",
suffix_title="label_for_paligemma",
processor=processor,
device=device,
train=True,
),
batch_size=object_detection_config.BATCH_SIZE,
shuffle=True,
)
test_dataloader = DataLoader(
test_dataset,
collate_fn=partial(
collate_fn,
image_title="image",
prompt="Detect license plate.",
suffix_title="label_for_paligemma",
processor=processor,
device=device,
train=False,
),
batch_size=object_detection_config.BATCH_SIZE,
shuffle=False,
)
# load the pre trained model
print(f"[INFO] loading {object_detection_config.MODEL_ID} model...")
model = PaliGemmaForConditionalGeneration.from_pretrained(
object_detection_config.MODEL_ID,
torch_dtype=object_detection_config.MODEL_DTYPE,
device_map=device,
revision=object_detection_config.MODEL_REVISION,
)
# freeze the weights
print(f"[INFO] freezing the model weights...")
model = freeze_layers(model, not_to_freeze="attn")
# run model generation before fine tuning
test_batch = next(iter(test_dataloader))
infer_on_model(model, test_batch)
# fine tune the model
print("[INFO] fine tuning the model...")
optimizer = torch.optim.AdamW(
model.parameters(), lr=object_detection_config.LEARNING_RATE
)
for epoch in range(object_detection_config.EPOCHS):
for idx, batch in enumerate(train_dataloader):
outputs = model(**batch)
loss = outputs.loss
if idx % 500 == 0:
print(f"Epoch: {epoch} Iter: {idx} Loss: {loss.item():.4f}")
loss.backward()
optimizer.step()
optimizer.zero_grad()
# run model generation after fine tuning
infer_on_model(model, test_batch, before_pt=False)