-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
45 lines (42 loc) · 1.97 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
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from torchvision.transforms import transforms
from PIL import Image
from parameters import *
from loss import *
from data import *
def train_model(model, train_loader, loss_fn, optimizer, num_epochs, device):
model.train(True)
for epoch in range(num_epochs):
for i, triplet in enumerate(train_loader):
anchors, positives, negatives = triplet
anchors, positives, negatives = anchors.to(device), positives.to(device), negatives.to(device)
anchors_embedding, positives_embedding, negatives_embedding = model(anchors), model(positives), model(negatives)
loss = loss_fn(anchors_embedding, positives_embedding, negatives_embedding)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i + 1) % 100 == 0:
print(f"Epoch [{epoch + 1} / {num_epochs}], image [{i + 1} / {len(train_loader)}], loss value : {loss.item()}")
torch.save(model.state_dict(), "facenet_model.pth")
def main():
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = torch.hub.load('pytorch/vision:v0.10.0', 'mobilenet_v2', pretrained=True)
model = model.to(device)
optimizer = optim.Adam(model.parameters(), lr = LEARNING_RATE)
num_epochs = NUM_EPOCHS
transform = transforms.Compose([
transforms.Resize(IMAGE_SIZE),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
])
path = r"cropped"
dataset = TripletFaceDataset(root_dir= path, transform= transform)
loss_fn = nn.TripletMarginLoss(margin = ALPHA)
dataset = DataLoader(dataset = dataset, batch_size= BATCH_SIZE)
train_model(model = model, train_loader = dataset, loss_fn = loss_fn, optimizer= optimizer, num_epochs= num_epochs, device = device)
if __name__ == "__main__":
main()