from unet import Unet n = Unet.load('models/unet3-weights-improvement-04-0.68.hdf5', num_classes=7) # n.test('/junction/dental_unet/files/train/1.bmp', 'res.bmp') n.test('/junction/dental_unet/files/train/2.bmp', 'res.bmp')
model.save(args.checkpoint, cur_iter, loss, optimizer.param_groups[0]['lr']) if best_loss > loss: best_loss = loss last_checkpoint = model.last_checkpoint( args.checkpoint, '.pth')[0] shutil.copy( last_checkpoint, os.path.join(args.checkpoint, f'best_model.pth')) if args.iter_lr and cur_iter % args.iter_lr == 0: # decay lr scheduler.step() model = Unet(3, 64) state_dict = model.load(args.checkpoint) def inference(image, mask): global model, result_dir model.eval() # 0 for hole 1 for valid mask = torch.where(mask > 0, torch.tensor(0), torch.tensor(1)) mask = torch.cat([mask, mask, mask]).float() out_img, _ = model(image.unsqueeze(0), mask.unsqueeze(0)) result = out_img.squeeze(0) * (1 - mask) + mask * image # save result time = datetime.today().strftime('%H_%M') savepoint = os.path.join(result_dir, f'{time}_{image.filename}') result_img = torchvision.transforms.ToPILImage()(result)