#print('utils.py, class AverageMeter()') if not os.path.exists('models'): os.mkdir('models') epoch = 0 for epoch in range(int(iterations)): # while i < int(iterations): for sample in tqdm(data_loader): images, labels = sample['images'].to(device), sample['labels'].to(device) logits = model(images) labels = labels.view(int(bs)*int(seq_length)) ##?? loss = criterion(logits, labels) optimizer.zero_grad() loss.backward() losses.update(loss.item(), images.size(0)) optimizer.step() print('epoch: {}\tLoss: {loss.val:.4f} ({loss.avg:.4f})'.format(epoch, loss=losses)) epoch += 1 if epoch % it_save == 0: torch.save({'optimizer_state_dict': optimizer.state_dict(), 'model_state_dict': model.state_dict()}, 'models/swingnet_{}.pth.tar'.format(epoch)) if epoch == iterations: break experiment.log_metrics("train_loss", losses, step=epoch)
losses = AverageMeter() if not os.path.exists("models"): os.mkdir("models") i = 0 while i < iterations: for sample in data_loader: images, labels = sample["images"].cuda(), sample["labels"].cuda() logits = model(images) labels = labels.view(bs * seq_length) loss = criterion(logits, labels) optimizer.zero_grad() loss.backward() losses.update(loss.item(), images.size(0)) optimizer.step() print( "Iteration: {}\tLoss: {loss.val:.4f} ({loss.avg:.4f})".format( i, loss=losses)) i += 1 if i % it_save == 0: torch.save( { "optimizer_state_dict": optimizer.state_dict(), "model_state_dict": model.state_dict(), }, "models/swingnet_{}.pth.tar".format(i), ) if i == iterations: break
if not os.path.exists('models'): os.mkdir('models') i = 0 while i < iterations: for sample in data_loader: images, labels = sample['images'].cuda(), sample['labels'].cuda() logits = model(images) labels = labels.view(bs * seq_length) loss = criterion(logits, labels) optimizer.zero_grad() loss.backward() losses.update(loss.item(), images.size(0)) optimizer.step() print( 'Iteration: {}\tLoss: {loss.val:.4f} ({loss.avg:.4f})'.format( i, loss=losses)) i += 1 if i % it_save == 0: torch.save( { 'optimizer_state_dict': optimizer.state_dict(), 'model_state_dict': model.state_dict() }, 'models/{}_{}.pth.tar'.format(version_name, i)) if i == iterations: break
plt.ylim([np.log(min(np.min(list_loss['loss']), np.min(list_loss['soft']), np.min(list_loss['count']))), np.log(list_loss['loss'][min(500,max(0,i-100))])]) plt.subplot(1, 2, 2) plt.hist(preds.reshape(-1).cpu().data.numpy()) plt.ylim([0,50]) plt.savefig('loss_' + version_name + '.png') plt.close('all') optimizer.zero_grad() loss.backward() losses.update(loss.item(), images.size(0)) optimizer.step() print('Iteration: {}\tLoss: {loss.val:.4f} ({loss.avg:.4f})'.format(i, loss=losses)) i += 1 if i % it_save == 0: torch.save({'optimizer_state_dict': optimizer.state_dict(), 'model_state_dict': model.state_dict()}, 'models/{}_{}.pth.tar'.format(version_name, i)) if i == iterations: break # Run evaluation import os os.system("python eval.py " + version_name)