Created
January 4, 2022 02:53
-
-
Save mayukh18/db509c226b8b8c84ecbbee8827856a13 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TripletLoss(nn.Module): | |
def __init__(self, margin=1.0): | |
super(TripletLoss, self).__init__() | |
self.margin = margin | |
def calc_euclidean(self, x1, x2): | |
return (x1 - x2).pow(2).sum(1) | |
# Distances in embedding space is calculated in euclidean | |
def forward(self, anchor, positive, negative): | |
distance_positive = self.calc_euclidean(anchor, positive) | |
distance_negative = self.calc_euclidean(anchor, negative) | |
losses = torch.relu(distance_positive - distance_negative + self.margin) | |
return losses.mean() | |
device = 'cuda' | |
# Our base model | |
model = models.resnet18().cuda() | |
optimizer = optim.Adam(model.parameters(), lr=0.001) | |
triplet_loss = TripletLoss() | |
# Training | |
for epoch in range(epochs): | |
model.train() | |
epoch_loss = 0.0 | |
for data in tqdm(train_loader): | |
optimizer.zero_grad() | |
x1,x2,x3 = data | |
e1 = model(x1.to(device)) | |
e2 = model(x2.to(device)) | |
e3 = model(x3.to(device)) | |
loss = triplet_loss(e1,e2,e3) | |
epoch_loss += loss | |
loss.backward() | |
optimizer.step() | |
print("Train Loss: {}".format(epoch_loss.item())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment