val_loss.append(loss.item()) val_acc.append(acc.item()) postfix_dict = {"val_loss": loss.item(), "val_acc": acc.item()} val_trange.set_postfix(**postfix_dict) avg_loss = np.mean(val_loss[-600:]) avg_acc = np.mean(val_acc[-600:]) postfix = ' (Best)' if avg_acc >= best_acc else ' (Best: {})'.format( best_acc) print('Avg Val Loss: {}, Avg Val Acc: {}{}'.format(avg_loss, avg_acc, postfix)) if avg_acc >= best_acc: best_acc = avg_acc best_model = copy.deepcopy(model.state_dict()) best_generator = copy.deepcopy(generator.state_dict()) if epoch % 5 == 0 and best_model != None: torch.save(best_model, model_path) torch.save(best_generator, generator_path) print("model saved! - {}".format(model_path)) # init sampler train_sampler = BatchSampler('train', train_dataset.labels, N_way, sample_per_class, episodes) train_dataloader = DataLoader(train_dataset, batch_size=batch_size, sampler=train_sampler) # val_sampler = BatchSampler('val', val_dataset.labels, val_N_way, val_sample_per_class, episodes) # val_dataloader = DataLoader(
device=opts.device) val_loss.append(loss.item()) val_acc.append(acc.item()) avg_loss = np.mean(val_loss[-opts.iterations:]) avg_acc = np.mean(val_acc[-opts.iterations:]) postfix = ' (Best)' if avg_acc >= best_acc else ' (Best: {:.5f})'.format( best_acc) print_log( 'Avg Val Loss: {:.5f}, Avg Val Acc: {:.5f}{}'.format( avg_loss, avg_acc, postfix), opts.log_file) if avg_acc >= best_acc: best_acc = avg_acc if opts.multi_gpu: torch.save(net.module.state_dict(), opts.model_file) else: torch.save(net.state_dict(), opts.model_file) print_log('[epoch {} / iter {}] best model saved to: {}'.format( epoch, len(train_db), opts.model_file), file=opts.log_file) best_acc = avg_acc best_state = net.state_dict() # TRAINING ENDS if best_state is not None: net.load_state_dict(best_state) # fixme when multi gpu, net.module() print_log('Testing with best model ...', opts.log_file) avg_acc = list() for epoch in range(10): test_iter = iter(test_db) for batch in test_iter: x, y = batch