-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtraining.py
More file actions
112 lines (91 loc) · 3.16 KB
/
training.py
File metadata and controls
112 lines (91 loc) · 3.16 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
import os
import cv2
import torch
import torch.nn.functional as F
from torchvision import transforms
from tqdm import tqdm
from src.models.vision_encoder import VisionEncoder
from src.models.predictor import Predictor
from src.models.query_encoder import QueryEncoder
from src.models.y_encoder import YEncoder
# --------------------------------------------------
# Device (Mac-safe)
# --------------------------------------------------
if torch.cuda.is_available():
device = "cuda"
elif torch.backends.mps.is_available():
device = "mps"
else:
device = "cpu"
print("Training on:", device)
# --------------------------------------------------
# Config
# --------------------------------------------------
DATA_DIR = "data/train"
EPOCHS = 20
LR = 1e-4
SAVE_PATH = "predictor.pt"
PROMPT = "What objects are visible?"
# --------------------------------------------------
# Image transform (CLIP compatible)
# --------------------------------------------------
transform = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.48145466, 0.4578275, 0.40821073],
std=[0.26862954, 0.26130258, 0.27577711],
)
])
# --------------------------------------------------
# Models
# --------------------------------------------------
vision = VisionEncoder(device=device)
predictor = Predictor().to(device).train()
q_encoder = QueryEncoder()
y_encoder = YEncoder()
optimizer = torch.optim.Adam(predictor.parameters(), lr=LR)
# --------------------------------------------------
# Load dataset
# --------------------------------------------------
samples = []
for label in os.listdir(DATA_DIR):
class_dir = os.path.join(DATA_DIR, label)
if not os.path.isdir(class_dir):
continue
for fname in os.listdir(class_dir):
samples.append((os.path.join(class_dir, fname), label))
print(f"Loaded {len(samples)} training samples")
# --------------------------------------------------
# Encode prompt ONCE
# --------------------------------------------------
q_emb = q_encoder.encode([PROMPT]).to(device)
# --------------------------------------------------
# Training loop
# --------------------------------------------------
for epoch in range(EPOCHS):
total_loss = 0.0
for img_path, label in tqdm(samples, desc=f"Epoch {epoch+1}/{EPOCHS}"):
# Load image
img = cv2.imread(img_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = transform(img).unsqueeze(0).to(device)
# Target embedding (teacher)
target_emb = y_encoder.encode([label]).to(device)
# Forward
sv = vision(img)
pred_emb = predictor(sv, q_emb)
# Cosine loss
loss = 1 - F.cosine_similarity(pred_emb, target_emb).mean()
optimizer.zero_grad()
loss.backward()
optimizer.step()
total_loss += loss.item()
avg_loss = total_loss / len(samples)
print(f"Epoch {epoch+1} | Loss: {avg_loss:.4f}")
# --------------------------------------------------
# Save predictor
# --------------------------------------------------
torch.save(predictor.state_dict(), SAVE_PATH)
print("Saved predictor to", SAVE_PATH)