-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
79 lines (62 loc) · 2.09 KB
/
train.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
import torch
from PIL import Image
from torch.utils.data import Dataset
import torchvision.transforms as T
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class AvgMeter:
def __init__(self, name="Metric"):
self.name = name
self.reset()
def reset(self):
self.avg, self.sum, self.count = [0] * 3
def update(self, val, count=1):
self.count += count
self.sum += val * count
self.avg = self.sum / self.count
def __repr__(self):
text = f"{self.name}: {self.avg:.4f}"
return text
def get_lr(optimizer):
for param_group in optimizer.param_groups:
return param_group["lr"]
class CLIPDataset(Dataset):
def __init__(self, texts, images, transform=None):
self.texts = texts
self.images = images
self.transform = transform
def __len__(self):
return len(self.texts)
def __getitem__(self, idx):
text = self.texts[idx]
image = Image.open(self.images[idx])
if self.transform:
image = self.transform(image)
else:
to_tensor = T.ToTensor()
image = to_tensor(image)
return text, image
# # Create sample array of texts and images
# texts = ["A photo of a dog", "A photo of a bird"]
# images = ["images/dog.jpeg", "images/bird.jpg"]
#
# # Create dataset and dataloader
# dataset = CLIPDataset(texts, images)
# dataloader = DataLoader(dataset, batch_size=1)
def train_step(model, data, optimizer):
# Move model to a device
model.train()
text, image = data
optimizer.zero_grad()
logits, loss, _, _ = model(image, text)
loss.backward()
optimizer.step()
return loss.item()
def train(model, dataloader, optimizer, num_epochs=10):
loss_meter = AvgMeter()
for epoch in range(num_epochs):
for i, data in enumerate(dataloader):
loss = train_step(model, data, optimizer)
loss_meter.update(loss, count=1)
loss = loss_meter.avg
print(f"Epoch {epoch}, Batch {i}, Loss: {loss}")
print(f"Epoch {epoch}, Loss: {loss_meter.avg}")