Skip to content

Instantly share code, notes, and snippets.

@aliwaqas333
Last active June 19, 2020 10:31
Show Gist options
  • Save aliwaqas333/bef23823e9d2254d7b28127df2fcd76e to your computer and use it in GitHub Desktop.
Save aliwaqas333/bef23823e9d2254d7b28127df2fcd76e to your computer and use it in GitHub Desktop.
@torch.no_grad()
def evaluate(model, val_loader):
model.eval()
outputs = [model.validation_step(batch) for batch in val_loader]
return model.validation_epoch_end(outputs)
def fit(epochs, lr, model, train_loader, val_loader, opt_func=torch.optim.SGD):
history = []
optimizer = torch.optim.SGD(model.parameters(), lr=0.001, momentum=0.9)
for epoch in range(epochs):
# Training Phase
print('epoch: ', epoch)
model.train()
train_losses = []
for batch in train_loader:
loss = model.training_step(batch)
train_losses.append(loss)
loss.backward()
optimizer.step()
optimizer.zero_grad()
# Validation phase
result = evaluate(model, val_loader)
result['train_loss'] = torch.stack(train_losses).mean().item()
model.epoch_end(epoch, result)
history.append(result)
return history
# Model (on GPU)
model = MaskDetection()
# model.load_state_dict(torch.load('../output/MaskDetection.pth'))
to_device(model, device)
history = [evaluate(model, val_dl)]
history
history = fit(5, 1e-3, model, train_dl, val_dl)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment