Esempio n. 1
0
                    help='model file to use')
parser.add_argument('--outputHR2',
                    type=str,
                    default='73_LapSRN_R_epochs100_HR2.tif',
                    help='where to save the output image')
parser.add_argument('--outputHR4',
                    type=str,
                    default='73_LapSRN_R_epochs100_HR4.tif',
                    help='where to save the output image')
parser.add_argument('--cuda', action='store_true', help='use cuda')

opt = parser.parse_args()

print(opt)

model_r = LapSRN().cuda()
model_g = LapSRN().cuda()
model_b = LapSRN().cuda()
optimizer_r = optim.Adagrad(model_r.parameters(), lr=1e-3, weight_decay=1e-5)
optimizer_g = optim.Adagrad(model_g.parameters(), lr=1e-3, weight_decay=1e-5)
optimizer_b = optim.Adagrad(model_b.parameters(), lr=1e-3, weight_decay=1e-5)

model_r, optimizer_r, epochs_r = load_model(model_r, optimizer_r, opt.model_r)
model_g, optimizer_g, epochs_g = load_model(model_g, optimizer_g, opt.model_g)
model_b, optimizer_b, epochs_b = load_model(model_b, optimizer_b, opt.model_b)

img = Image.open(opt.input).convert('RGB')
LR_r, LR_g, LR_b = img.split()

LR_r, LR_g, LR_b = pre_deal(LR_r, LR_g, LR_b)
                        help='whether to use cuda')
    args = parser.parse_args()

    if args.cuda and not torch.cuda.is_available():
        raise Exception('No GPU found')
    device = torch.device('cuda' if args.cuda else 'cpu')
    print('Use device:', device)

    filenames = os.listdir(args.img_dir)
    image_filenames = [os.path.join(args.img_dir, x) for x in filenames \
                       if is_image_file(x)]
    image_filenames = sorted(image_filenames)

    model = LapSRN(img_channels=1,
                   upscale_factor=args.upscale_factor,
                   n_feat=10,
                   n_recursive=1,
                   local_residual='ns').to(device)
    if args.cuda:
        ckpt = torch.load(args.model)
    else:
        ckpt = torch.load(args.model, map_location='cpu')
    model.load_state_dict(ckpt['model'])

    res = {}

    for i, f in enumerate(image_filenames):
        # Read test image.
        img = Image.open(f).convert('RGB')
        width, height = img.size[0], img.size[1]
Esempio n. 3
0
    raise Exception("No GPU found, please run without --cuda")

torch.manual_seed(opt.seed)
if cuda:
    torch.cuda.manual_seed(opt.seed)
device = torch.device("cuda" if opt.cuda else "cpu")

print('===> Loading datasets')
train_set = get_training_set(opt.dataset)
val_set = get_val_set(opt.dataset)
training_data_loader = DataLoader(dataset=train_set, batch_size=opt.batchSize, shuffle=True)
val_data_loader = DataLoader(dataset=val_set, batch_size=opt.valBatchSize, shuffle=False)


print('===> Building model')
model = LapSRN().to(device)
Loss = Loss()
criterion = nn.MSELoss()
if cuda:
    Loss = Loss.cuda()
    criterion = criterion.cuda()


def train(epoch):
        epoch_loss = 0
        for iteration, batch in enumerate(training_data_loader, 1):
            LR, HR_2_target, HR_4_target = batch[0].to(device), batch[1].to(device), batch[2].to(device)

            optimizer.zero_grad()
            HR_2, HR_4 = model(LR)
                        help='upscaling factor')
    parser.add_argument('--img_channels', type=int, required=True,
                        help='# of image channels (1 for Y, 3 for RGB)')
    parser.add_argument('--input', type=str, required=True, 
                        help='input image to super resolve')
    parser.add_argument('--output', type=str, required=True,
                        help='where to save the output image')
    args = parser.parse_args()

    img = Image.open(args.input).convert('RGB')
    if args.img_channels == 1:
        img = _rgb2ycbcr(img)
        img, cb, cr = img.split()

    ckpt = torch.load(args.model, map_location='cpu')
    model = LapSRN(img_channels=args.img_channels,
                   upscale_factor=args.upscale_factor)
    model.load_state_dict(ckpt['model'])

    input = ToTensor()(img).view(1, -1, img.size[1], img.size[0])

    out = model(input)[-1]
    out_img = out.detach().numpy().squeeze().transpose(1, 2, 0)
    out_img *= 255.0
    out_img = out_img.clip(0, 255)
    print(out_img.shape)
    if args.img_channels == 1:
        out_img = Image.fromarray(np.uint8(out_img), mode='L')
        out_img_cb = cb.resize(out_img.size, Image.BICUBIC)
        out_img_cr = cr.resize(out_img.size, Image.BICUBIC)
        out_img = Image.merge('YCbCr', [out_img, out_img_cb, out_img_cr])
        out_img = _ycbcr2rgb(out_img)
Esempio n. 5
0
    raise Exception("No GPU found, please run without --cuda")

torch.manual_seed(opt.seed)
if cuda:
    torch.cuda.manual_seed(opt.seed)
device = torch.device("cuda" if opt.cuda else "cpu")

print('===> Loading datasets')
train_set = get_training_set(opt.dataset)
val_set = get_val_set(opt.dataset)
training_data_loader = DataLoader(dataset=train_set, batch_size=opt.batchSize, shuffle=True)
val_data_loader = DataLoader(dataset=val_set, batch_size=opt.valBatchSize, shuffle=False)


print('===> Loading pre_train model and Building model')
model = LapSRN().to(device)
Loss = Loss()
criterion = nn.MSELoss()
if cuda:
    Loss = Loss.cuda()
    criterion = criterion.cuda()


def train(epoch):
        epoch_loss = 0
        for _, batch in enumerate(training_data_loader, 1):
            LR, HR_2_target, HR_4_target = batch[0].to(device), batch[1].to(device), batch[2].to(device)

            optimizer.zero_grad()

            HR_2, HR_4 = model(LR)
Esempio n. 6
0

def checkpoint(epoch):
    model_out_r_path = "LapSRN_model_epoch_r_{}.pth".format(epoch)
    state_r = {'model': model_r.state_dict(), 'optimizer': optimizer_r.state_dict(), 'epoch':epoch, 'lr':lr_r}
    torch.save(state_r, model_out_r_path, _use_new_zipfile_serialization=False)
    
    model_out_g_path = "LapSRN_model_epoch_g_{}.pth".format(epoch)
    state_g = {'model': model_g.state_dict(), 'optimizer': optimizer_g.state_dict(), 'epoch':epoch, 'lr':lr_g}
    torch.save(state_g, model_out_g_path, _use_new_zipfile_serialization=False)
    
    print("Checkpoint saved to {} and {}".format(model_out_r_path, model_out_g_path))


if os.path.exists(opt.pre_model_r):
    model_r = LapSRN().to(device)
    checkpoints_r = torch.load(opt.pre_model_r)
    model_r.load_state_dict(checkpoints_r['model'])
    model_r.train()
    epoch_continue_r = checkpoints_r['epoch']
    optimizer_r = optim.Adagrad(model_r.parameters())


    model_g = LapSRN().to(device)
    checkpoints_g = torch.load(opt.pre_model_g)
    model_g.load_state_dict(checkpoints_g['model'])
    model_g.train()
    epoch_continue_g = checkpoints_g['epoch']
    optimizer_g = optim.Adagrad(model_g.parameters())

    for epoch in range(epoch_continue_g + 1, opt.nEpochs + 1):
Esempio n. 7
0
if cuda:
    torch.cuda.manual_seed(opt.seed)
device = torch.device("cuda" if opt.cuda else "cpu")

print('===> Loading datasets')
train_set = get_training_set(opt.dataset)
val_set = get_val_set(opt.dataset)
training_data_loader = DataLoader(dataset=train_set,
                                  batch_size=opt.batchSize,
                                  shuffle=True)
val_data_loader = DataLoader(dataset=val_set,
                             batch_size=opt.valBatchSize,
                             shuffle=False)

print('===> Building model')
model_r = LapSRN().to(device)
Loss_r = Loss()

model_g = LapSRN().to(device)
Loss_g = Loss()

criterion = nn.MSELoss()
if cuda:
    Loss_r = Loss_r.cuda()
    Loss_g = Loss_g.cuda()
    criterion = criterion.cuda()


def train(epoch):
    epoch_loss_r, epoch_loss_g = 0, 0
    for _, batch in enumerate(training_data_loader, 1):
Esempio n. 8
0
        img_channels=config['model']['img_channels'],
        crop_size=config['data']['hr_crop_size'])
    train_dataloader = DataLoader(dataset=train_set,
                                  batch_size=config['training']['batch_size'],
                                  shuffle=True)

    val_set = get_val_set(img_dir=config['data']['test_root'],
                          upscale_factor=config['model']['upscale_factor'],
                          img_channels=config['model']['img_channels'])
    val_dataloader = DataLoader(dataset=val_set, batch_size=1, shuffle=False)

    print('===> Building model')
    sys.stdout.flush()
    model = LapSRN(img_channels=config['model']['img_channels'],
                   upscale_factor=config['model']['upscale_factor'],
                   n_feat=config['model']['n_feat'],
                   n_recursive=config['model']['n_recursive'],
                   local_residual=config['model']['local_residual']).to(device)
    # criterion = nn.MSELoss()
    criterion = L1_Charbonnier_loss()
    optimizer = setup_optimizer(model, config)
    scheduler = setup_scheduler(optimizer, config)

    start_iter = 0
    best_val_psnr = -1

    if config['training']['resume'] != 'None':
        print('===> Reloading model')
        sys.stdout.flush()
        ckpt = torch.load(config['training']['resume'])
        model.load_state_dict(ckpt['model'])
Esempio n. 9
0
                    help='model file to use')
parser.add_argument('--outputHR2',
                    type=str,
                    default='73_LapSRN_G_epochs100_HR2.tif',
                    help='where to save the output image')
parser.add_argument('--outputHR4',
                    type=str,
                    default='73_LapSRN_G_epochs100_HR4.tif',
                    help='where to save the output image')
parser.add_argument('--cuda', action='store_true', help='use cuda')

opt = parser.parse_args()

print(opt)

model = LapSRN().cuda()
optimizer = optim.Adagrad(model.parameters(), lr=1e-3, weight_decay=1e-5)

checkpoint = torch.load(opt.model)
model.load_state_dict(checkpoint['model'])
model.eval()
optimizer.load_state_dict(checkpoint['optimizer'])
epochs = checkpoint['epoch']

transform = Compose([
    ToTensor(),
    Lambda(lambda x: x.repeat(3, 1, 1)),
])

img = Image.open(opt.input).convert('RGB')
_, LR_g, _ = img.split()