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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| def main(args): cfg=load_json(args.config)
seed=5 random.seed(seed) torch.manual_seed(seed) np.random.seed(seed) torch.cuda.manual_seed(seed) torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = False
device = torch.device('cuda')
image_size=cfg['image_size'] batch_size=cfg['batch_size'] train_dataset=SBI_Dataset(phase='train',image_size=image_size) val_dataset=SBI_Dataset(phase='val',image_size=image_size) train_loader=torch.utils.data.DataLoader(train_dataset, batch_size=batch_size//2, shuffle=True, collate_fn=train_dataset.collate_fn, num_workers=4, pin_memory=True, drop_last=True, worker_init_fn=train_dataset.worker_init_fn ) val_loader=torch.utils.data.DataLoader(val_dataset, batch_size=batch_size, shuffle=False, collate_fn=val_dataset.collate_fn, num_workers=4, pin_memory=True, worker_init_fn=val_dataset.worker_init_fn ) model=Detector() model=model.to('cuda') iter_loss=[] train_losses=[] test_losses=[] train_accs=[] test_accs=[] val_accs=[] val_losses=[] n_epoch=cfg['epoch'] lr_scheduler=LinearDecayLR(model.optimizer, n_epoch, int(n_epoch/4*3)) last_loss=99999
now=datetime.now() save_path='output/{}_'.format(args.session_name)+now.strftime(os.path.splitext(os.path.basename(args.config))[0])+'_'+now.strftime("%m_%d_%H_%M_%S")+'/' os.mkdir(save_path) os.mkdir(save_path+'weights/') os.mkdir(save_path+'logs/') logger = log(path=save_path+"logs/", file="losses.logs")
criterion=nn.CrossEntropyLoss()
last_auc=0 last_val_auc=0 weight_dict={} n_weight=5 for epoch in range(n_epoch): np.random.seed(seed + epoch) train_loss=0. train_acc=0. model.train(mode=True) for step,data in enumerate(tqdm(train_loader)): img=data['img'].to(device, non_blocking=True).float() target=data['label'].to(device, non_blocking=True).long() output=model.training_step(img, target) loss=criterion(output,target) loss_value=loss.item() iter_loss.append(loss_value) train_loss+=loss_value acc=compute_accuray(F.log_softmax(output,dim=1),target) train_acc+=acc lr_scheduler.step() train_losses.append(train_loss/len(train_loader)) train_accs.append(train_acc/len(train_loader))
log_text="Epoch {}/{} | train loss: {:.4f}, train acc: {:.4f}, ".format( epoch+1, n_epoch, train_loss/len(train_loader), train_acc/len(train_loader), )
model.train(mode=False) val_loss=0. val_acc=0. output_dict=[] target_dict=[] np.random.seed(seed) for step,data in enumerate(tqdm(val_loader)): img=data['img'].to(device, non_blocking=True).float() target=data['label'].to(device, non_blocking=True).long() with torch.no_grad(): output=model(img) loss=criterion(output,target) loss_value=loss.item() iter_loss.append(loss_value) val_loss+=loss_value acc=compute_accuray(F.log_softmax(output,dim=1),target) val_acc+=acc output_dict+=output.softmax(1)[:,1].cpu().data.numpy().tolist() target_dict+=target.cpu().data.numpy().tolist() val_losses.append(val_loss/len(val_loader)) val_accs.append(val_acc/len(val_loader)) val_auc=roc_auc_score(target_dict,output_dict) log_text+="val loss: {:.4f}, val acc: {:.4f}, val auc: {:.4f}".format( val_loss/len(val_loader), val_acc/len(val_loader), val_auc )
if len(weight_dict)<n_weight: save_model_path=os.path.join(save_path+'weights/',"{}_{:.4f}_val.tar".format(epoch+1,val_auc)) weight_dict[save_model_path]=val_auc torch.save({ "model":model.state_dict(), "optimizer":model.optimizer.state_dict(), "epoch":epoch },save_model_path) last_val_auc=min([weight_dict[k] for k in weight_dict])
elif val_auc>=last_val_auc: save_model_path=os.path.join(save_path+'weights/',"{}_{:.4f}_val.tar".format(epoch+1,val_auc)) for k in weight_dict: if weight_dict[k]==last_val_auc: del weight_dict[k] os.remove(k) weight_dict[save_model_path]=val_auc break torch.save({ "model":model.state_dict(), "optimizer":model.optimizer.state_dict(), "epoch":epoch },save_model_path) last_val_auc=min([weight_dict[k] for k in weight_dict]) logger.info(log_text) if __name__=='__main__':
parser=argparse.ArgumentParser() parser.add_argument(dest='config') parser.add_argument('-n',dest='session_name') args=parser.parse_args() main(args)
|