Example #1
0
def main():
    global opt
    opt = parser.parse_args()
    opt.gpuids = list(map(int, opt.gpuids))

    print(opt)

    if opt.cuda and not torch.cuda.is_available():
        raise Exception("No GPU found, please run without --cuda")
    cudnn.benchmark = True

    train_set = get_training_set(opt.upscale_factor, opt.add_noise,
                                 opt.noise_std)
    validation_set = get_validation_set(opt.upscale_factor)
    test_set = get_test_set(opt.upscale_factor)
    training_data_loader = DataLoader(dataset=train_set,
                                      num_workers=opt.threads,
                                      batch_size=opt.batch_size,
                                      shuffle=True)
    validating_data_loader = DataLoader(dataset=validation_set,
                                        num_workers=opt.threads,
                                        batch_size=opt.test_batch_size,
                                        shuffle=False)
    testing_data_loader = DataLoader(dataset=test_set,
                                     num_workers=opt.threads,
                                     batch_size=opt.test_batch_size,
                                     shuffle=False)

    model = SRCNN()
    criterion = nn.MSELoss()

    if opt.cuda:
        torch.cuda.set_device(opt.gpuids[0])
        with torch.cuda.device(opt.gpuids[0]):
            model = model.cuda()
            criterion = criterion.cuda()
        model = nn.DataParallel(model,
                                device_ids=opt.gpuids,
                                output_device=opt.gpuids[0])

    optimizer = optim.Adam(model.parameters(), lr=opt.lr)

    if opt.test:
        model_name = join("model", opt.model)
        model = torch.load(model_name)
        model = nn.DataParallel(model,
                                device_ids=opt.gpuids,
                                output_device=opt.gpuids[0])
        start_time = time.time()
        test(model, criterion, testing_data_loader)
        elapsed_time = time.time() - start_time
        print("===> average {:.2f} image/sec for processing".format(
            100.0 / elapsed_time))
        return

    for epoch in range(1, opt.epochs + 1):
        train(model, criterion, epoch, optimizer, training_data_loader)
        validate(model, criterion, validating_data_loader)
        if epoch % 10 == 0:
            checkpoint(model, epoch)
Example #2
0
def _get_xy(path_to_data=None, binary=True, upsample=True, replicate=False):
    """
    Returns Xs, Ys, shuffled.

    Keeps back a validation set that you can get via X_validation_set and Y_validation_set.
    """
    global cached_Xs
    global cached_Ys
    global cached_ptd
    global cached_binary
    if cached_Xs is not None and cached_Ys is not None and cached_ptd == path_to_data and cached_binary == binary and upsample and not replicate:
        return cached_Xs, cached_Ys
    else:
        print("Getting the data. This will take a moment...")
        Xs = [
            x for x in data.get_X_feed(
                path_to_data, upsample=upsample, replicate=replicate)
        ]
        if binary:
            Ys = np.array([
                y for y in data.get_Y_feed_binary(path_to_data,
                                                  upsample=upsample)
            ])
        else:
            Ys = np.array([
                y for y in data.get_Y_feed(Xs, path_to_data, upsample=upsample)
            ])
        Xs = np.array([x[1] for x in Xs])

        # Shuffle
        index_shuf = [i for i in range(len(Xs))]
        random.shuffle(index_shuf)
        Xs = np.array([Xs[i] for i in index_shuf])
        Ys = np.array([Ys[i] for i in index_shuf])
        assert (len(Xs) == len(Ys))

        # Keep back validation set
        global X_validation_set
        global Y_validation_set
        X_validation_set, Y_validation_set = data.get_validation_set(
            replicate=replicate)
        print("Ones in validation set:",
              len([y for y in Y_validation_set if y == 1]))
        print("Zeros in validation set:",
              len([y for y in Y_validation_set if y == 0]))

        if upsample:
            # Only cache upsampled data
            cached_Xs = Xs
            cached_Ys = Ys
            cached_ptd = path_to_data
            cached_binary = binary
        return Xs, Ys
Example #3
0
def main():
    global opt
    opt = parser.parse_args()
    opt.gpuids = list(map(int, opt.gpuids))

    print(opt)

    if opt.cuda and not torch.cuda.is_available():
        raise Exception("No GPU found, please run without --cuda")

    cudnn.benchmark = True

    if not opt.test:
        train_set = get_training_set(opt.dataset, opt.crop_size,
                                     opt.upscale_factor, opt.add_noise,
                                     opt.noise_std)
        validation_set = get_validation_set(opt.dataset, opt.crop_size,
                                            opt.upscale_factor)

    # test_set = get_test_set(
    #     opt.dataset, opt.crop_size, opt.upscale_factor)

    if not opt.test:
        training_data_loader = DataLoader(dataset=train_set,
                                          num_workers=opt.threads,
                                          batch_size=opt.batch_size,
                                          shuffle=True)
        validating_data_loader = DataLoader(dataset=validation_set,
                                            num_workers=opt.threads,
                                            batch_size=opt.test_batch_size,
                                            shuffle=False)

    # testing_data_loader = DataLoader(
    #     dataset=test_set, num_workers=opt.threads, batch_size=opt.test_batch_size, shuffle=False)

    model = VDSR()
    criterion = nn.MSELoss()

    if opt.cuda:
        torch.cuda.set_device(opt.gpuids[0])
        with torch.cuda.device(opt.gpuids[0]):
            model = model.cuda()
            criterion = criterion.cuda()

    optimizer = optim.Adam(model.parameters(),
                           lr=opt.lr,
                           weight_decay=opt.weight_decay)
    #     optimizer = optim.SGD(model.parameters(), lr=opt.lr, momentum=opt.momentum, weight_decay=opt.weight_decay)

    # if opt.test:
    #     model_name = join("model", opt.model)
    #     model = torch.load(model_name)
    #     start_time = time.time()
    #     test(model, criterion, testing_data_loader)
    #     elapsed_time = time.time() - start_time
    #     print("===> average {:.2f} image/sec for test".format(
    #         100.0/elapsed_time))
    #     return

    train_time = 0.0
    validate_time = 0.0
    for epoch in range(1, opt.epochs + 1):
        start_time = time.time()
        train(model, criterion, epoch, optimizer, training_data_loader)
        elapsed_time = time.time() - start_time
        train_time += elapsed_time
        #         print("===> {:.2f} seconds to train this epoch".format(
        #             elapsed_time))
        start_time = time.time()
        validate(model, criterion, validating_data_loader)
        elapsed_time = time.time() - start_time
        validate_time += elapsed_time
        #         print("===> {:.2f} seconds to validate this epoch".format(
        #             elapsed_time))
        if epoch % 10 == 0:
            checkpoint(model, epoch)

    print("===> average training time per epoch: {:.2f} seconds".format(
        train_time / opt.epochs))
    print("===> average validation time per epoch: {:.2f} seconds".format(
        validate_time / opt.epochs))
    print("===> training time: {:.2f} seconds".format(train_time))
    print("===> validation time: {:.2f} seconds".format(validate_time))
    print("===> total training time: {:.2f} seconds".format(train_time +
                                                            validate_time))
Example #4
0
def main():
    global opt
    opt = parser.parse_args()
    opt.gpuids = list(map(int, opt.gpuids))

    print(opt)

    if opt.cuda and not torch.cuda.is_available():
        raise Exception("No GPU found, please run without --cuda")

    cudnn.benchmark = True

    if not opt.test:
        train_set = get_training_set(opt.dataset, opt.add_noise, opt.convert_L,
                                     opt.crop)
        validation_set = get_validation_set(opt.dataset, opt.add_noise)

    test_set = get_test_set(opt.dataset)
    #test_set = get_test_set(opt.dataset, opt.add_noise)

    if not opt.test:
        training_data_loader = DataLoader(dataset=train_set,
                                          num_workers=opt.threads,
                                          batch_size=opt.batch_size,
                                          shuffle=True)
        validating_data_loader = DataLoader(dataset=validation_set,
                                            num_workers=opt.threads,
                                            batch_size=opt.test_batch_size,
                                            shuffle=False)

    testing_data_loader = DataLoader(dataset=test_set,
                                     num_workers=opt.threads,
                                     batch_size=opt.test_batch_size,
                                     shuffle=False)

    model = SVLRM()
    criterion1 = CharnonnierLoss()
    criterion2 = nn.MSELoss()
    Loss = []
    PSNR = []
    RMSE = []

    if opt.cuda:
        torch.cuda.set_device(opt.gpuids[0])
        with torch.cuda.device(opt.gpuids[0]):
            model = model.cuda()
            criterion1 = criterion1.cuda()
            criterion2 = criterion2.cuda()
        model = nn.DataParallel(model,
                                device_ids=opt.gpuids,
                                output_device=opt.gpuids[0])

    optimizer = optim.Adam(model.parameters(),
                           eps=opt.eps,
                           weight_decay=opt.weight_decay)

    if opt.test:
        model_name = join("model", opt.model)
        model = torch.load(model_name)
        model.eval()
        start_time = time.time()
        #test(model, criterion2, testing_data_loader,PSNR,RMSE)
        test(model, criterion2, testing_data_loader)
        elapsed_time = time.time() - start_time
        print("===> average {:.2f} image/sec for test".format(100.0 /
                                                              elapsed_time))
        return

    train_time = 0.0
    validate_time = 0.0
    for epoch in range(1, opt.epochs + 1):
        start_time = time.time()
        train(model, criterion1, epoch, optimizer, training_data_loader, Loss)
        elapsed_time = time.time() - start_time
        train_time += elapsed_time
        print("===> {:.2f} seconds to train this epoch".format(elapsed_time))
        if epoch % 50 == 0:
            start_time = time.time()
            validate(model, criterion2, validating_data_loader, PSNR, RMSE)
            elapsed_time = time.time() - start_time
            validate_time += elapsed_time
            print("===> {:.2f} seconds to validate this epoch".format(
                elapsed_time))
            #if epoch % 10 == 0:
            checkpoint(model, epoch)

    print("===> average training time per epoch: {:.2f} seconds".format(
        train_time / opt.epochs))
    print("===> average validation time per epoch: {:.2f} seconds".format(
        validate_time / opt.epochs))
    print("===> training time: {:.2f} seconds".format(train_time))
    print("===> validation time: {:.2f} seconds".format(validate_time))
    print("===> total training time: {:.2f} seconds".format(train_time +
                                                            validate_time))
    plt.figure(figsize=(15, 5))
    plt.subplot(131)  # 1行3列,第一个图
    plt.plot(Loss)
    plt.ylabel('Loss')
    plt.xlabel('epochs')
    plt.subplot(132)  # 1行3列.第二个图
    plt.plot(PSNR)
    plt.ylabel('PSNR')
    plt.xlabel('epochsX50')
    plt.subplot(133)  # 1行3列.第3个图
    plt.plot(RMSE)
    plt.ylabel('RMSE')
    plt.xlabel('epochsX50')
    plt.savefig("Loss_PSNR_RMSE.jpg")
    file2 = open('PSNR.txt', 'w')
    for item in PSNR:
        file2.write(str(item) + "\n")
    file2.close()