Exemplo n.º 1
0
def main():

    h = 360
    w = 256
    c = 3
    #val_psnr=0
    net = kerasModel2.SN(h, w)
    net.build(input_shape=(None, h, w, c))
    net.load_weights(CP_Dir)
    # Make a dummy prediction to get the input shape
    start = time.time()
    for i, f in zip(range(len(v_scan_blur)), v_scan_blur):
        #if (i % 10) == 9:
        dir_num = str("%03d_" % (i // 100))
        name = str("%08d" % (i % 100))
        test2 = cv2.imread(f)
        test2 = cv2.resize(test2, (256, 360), interpolation=cv2.INTER_CUBIC)
        test = data.normalize(test2)
        test = np.expand_dims(test, axis=0)
        pred = net.predict(test, batch_size=1)
        pred = pred.squeeze(0)
        pred = data.unnormalize(pred)
        #val_psnr = val_psnr + BB(test2, pred)
        #cv2.imwrite(save_files[a], pred)
        cv2.imwrite(
            "D:/ntire2020/Deblur/ntire-2020-deblur-mobile-master/jisu/" +
            dir_num + name + ".png", pred)
        print("%s" % f)
    print("%.4f sec took for testing" % (time.time() - start))
Exemplo n.º 2
0
def main():

    h = 720
    w = 1280
    c = 3
    net = kerasModel.SN(h, w)
    net.build(input_shape=(None, h, w, c))
    net.load_weights(CP_Dir)
    # Make a dummy prediction to get the input shape
    for i, f in zip(range(len(v_scan_blur)), v_scan_blur):
        if (i % 10) == 9:
            dir_num = str("%03d_" % (i // 100))
            name = str("%08d" % (i % 100))
            final = np.zeros((h, w, c))
            for mode in range(8):

                test = cv2.imread(f)
                test = self_ensemble(test, mode, 1)
                test = data.normalize(test)
                test = np.expand_dims(test, axis=0)
                pred = net.predict(test, batch_size=1)
                pred = pred.squeeze(0)
                pred = data.unnormalize(pred)
                pred = self_ensemble(pred, mode, 0)
                final = final + pred
            #cv2.imwrite(save_files[a], pred)
            cv2.imwrite(
                "D:/ntire2020/Deblur/ntire-2020-deblur-mobile-master/validation2/"
                + dir_num + name + ".png", final / 8)
            print("%s" % f)
Exemplo n.º 3
0
def val_loss():
    '''prints the final validiation loss of the model'''
    random_l = data.val_l.cpu().numpy()
    random.shuffle(random_l)
    random_l = torch.Tensor(random_l).long().cuda()

    with torch.no_grad():
        z, _ = cinn(data.val_x, data.val_l)

        recon = cinn.reverse_sample(z, data.val_l).cpu().numpy()

        fake = cinn.reverse_sample(z, random_l).cpu().numpy()

    nrow = 20
    ncol = 30
    n_imgs = 3
    full_image = np.zeros((28 * nrow, 28 * ncol * n_imgs))

    for s in range(nrow * ncol):
        i, j = s // ncol, s % ncol
        # Show src Image
        src_show = data.val_x[s, 0].cpu().numpy()
        src_show = data.unnormalize(src_show)
        full_image[28 * i:28 * (i + 1),
                   28 * j * n_imgs:28 * (j * n_imgs + 1)] = src_show

        # Show recon Image
        rec_show = recon[s, 0]
        rec_show = data.unnormalize(rec_show)
        full_image[28 * i:28 * (i + 1),
                   28 * (j * n_imgs + 1):28 * (j * n_imgs + 2)] = 1 - rec_show

        # Show random label Image
        fake_show = fake[s, 0]
        fake_show = data.unnormalize(fake_show)
        full_image[28 * i:28 * (i + 1),
                   28 * (j * n_imgs + 2):28 * (j * n_imgs + 3)] = 1 - fake_show

    full_image = np.clip(full_image, 0, 1)
    plt.title(
        F'Left: val source image ; Mid: Recon Image Right: Random Label image')
    plt.imshow(full_image, vmin=0, vmax=1, cmap='gray')
    cv2.imwrite("source-vs-rec.png", full_image * 255.0)
Exemplo n.º 4
0
def predict(instance, dat):
    with tf.Graph().as_default():
        # Build a Graph that computes predictions from the inference model.
        logits = ops.inference(tf.constant(instance),
                               dat.emb_size,
                               FLAGS.hidden1,
                               FLAGS.hidden2,
                               1)  # keep_prob

        sess = tf.Session()
        # Restore variables from disk.
        restore_variables(sess)
        predictions = sess.run(logits)
        return data.unnormalize(predictions)
Exemplo n.º 5
0
    def color_single(bw_img, n_show=11, save_as=None, subplot_args=None):
        '''colorize a sinlge black-and-white image.
        bw_img:     1x28x28 bw image
        n_show:     how many samples to generate
        save_as:    if not None: save image filename
        subplot_args:   If not None: use plt.sublot(*subplot_args) instead of plt.figure()'''

        with torch.no_grad():
            cond_features = cond_net.model.features(
                bw_img.expand(c.batch_size, -1, -1, -1))
            cond = torch.cat([
                bw_img.expand(c.batch_size, 1, *c.img_dims),
                cond_features.view(c.batch_size, c.cond_width, 1, 1).expand(
                    -1, -1, *c.img_dims)
            ],
                             dim=1)

        z = sample_outputs(1.0)

        with torch.no_grad():
            colored = data.unnormalize(
                model.model(z, cond, rev=True)[:n_show].data.cpu().numpy())

        imgs = [torch.cat([bw_img] * 3, 0).cpu().numpy()]
        imgs += list(colored)

        img_show = img_tile(imgs, (1, n_show + 1))
        img_show = np.clip(img_show, 0, 1)

        if subplot_args:
            plt.subplot(*subplot_args)
        else:
            plt.figure()

        plt.imshow(img_show)
        plt.xticks([])
        plt.yticks([])
        if save_as:
            plt.imsave(save_as, img_show)
Exemplo n.º 6
0
def show_samples(label):
    '''produces and shows cINN samples for a given label (0-9)'''

    N_samples = 100
    l = torch.cuda.LongTensor(N_samples)
    l[:] = label

    z = 1.0 * torch.randn(N_samples, model.ndim_total).cuda()

    with torch.no_grad():
        samples = cinn.reverse_sample(z, l).cpu().numpy()
        samples = data.unnormalize(samples)

    full_image = np.zeros((28 * 10, 28 * 10))

    for k in range(N_samples):
        i, j = k // 10, k % 10
        full_image[28 * i:28 * (i + 1), 28 * j:28 * (j + 1)] = samples[k, 0]

    full_image = np.clip(full_image, 0, 1)
    plt.figure()
    plt.title(F'Generated digits for c={label}')
    plt.imshow(full_image, vmin=0, vmax=1, cmap='gray')
Exemplo n.º 7
0
# check if a model has been previously trained
already_trained = os.path.exists(load_path)
if not (args.train or already_trained):
    check_if_ok_to_continue('Model has not been trained. '
                            'Train it now (this may take several hours)? ')
    args.train = True

dataset = model.load_data(args.dataset)
if args.train:
    model.run_training(dataset)

# predict a rating for the user
if args.user_id and (args.movie or args.top):
    instance = dataset.get_ratings(args.user_id)
    ratings = data.unnormalize(instance.ravel())
    output = model.predict(instance, dataset).ravel()
    if args.movie:
        col = dataset.get_col(args.movie)
        rating = output[col]

        # purty stars
        num_stars = int(round(rating * 2))
        stars = ''.join(u'\u2605' for _ in range(num_stars))
        stars += ''.join(u'\u2606' for _ in range(10 - num_stars))

        print("The model predicts that user %s will rate "
              "movie number %s: "
              % (args.user_id, args.movie))
        print('%1.2f / 5' % rating)
        print(stars)
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-p', '--path', type=str, default='REDS')
    parser.add_argument('-m',
                        '--model_file',
                        type=str,
                        default='models/deblur.tflite')
    parser.add_argument('-t', '--test', action='store_true')
    parser.add_argument('-s', '--save_results', action='store_true')
    parser.add_argument('-256', '--use_256', action='store_true')
    args = parser.parse_args()

    interpreter = tf.lite.Interpreter(model_path=args.model_file, )
    interpreter.allocate_tensors()
    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()

    # check the type of the input tensor
    floating_model = input_details[0]['dtype'] == np.float32

    if args.test:
        input_dir = path.join(args.path, 'test', 'test_blur')
        target_dir = None
        save_dir = path.join('example', 'test')
    else:
        input_dir = path.join(args.path, 'val', 'val_blur')
        target_dir = path.join(args.path, 'val', 'val_sharp')
        save_dir = path.join('example', 'val')

    if args.save_results:
        os.makedirs(save_dir, exist_ok=True)

    scan = lambda x, y: glob.glob(path.join(x, y, '*.png'))
    input_list = [scan(input_dir, d) for d in os.listdir(input_dir)]
    input_list = sorted([it for it in itertools.chain(*input_list)])
    if target_dir is None:
        target_list = [None for _ in input_list]
    else:
        target_list = [scan(target_dir, d) for d in os.listdir(target_dir)]
        target_list = sorted([it for it in itertools.chain(*target_list)])

    # NxHxWxC, H:1, W:2
    psnr_avg = 0
    tq = tqdm.tqdm(zip(input_list, target_list), total=len(input_list))
    for input_path, target_path in tq:
        input_img = imageio.imread(input_path)
        if target_path is not None:
            target_img = imageio.imread(target_path)

        if floating_model:
            input_img = data.normalize(input_img)

        input_data = np.expand_dims(input_img, axis=0)
        if args.use_256:
            # Split the image into 256 x 256 patches
            blur_list = []
            ps = 256
            _, h, w, _ = input_data.shape
            nh = math.ceil(h / ps)
            nw = math.ceil(w / ps)

            mh = (nh * ps - h) // (nh - 1)
            mw = (nw * ps - w) // (nw - 1)
            for ih in range(nh):
                ph = ih * (ps - mh)
                for iw in range(nw):
                    pw = iw * (ps - mw)
                    patch = input_data[..., ph:ph + ps, pw:pw + ps, :]
                    blur_list.append(patch)
        else:
            blur_list = [input_data]

        output_list = []
        for x in blur_list:
            interpreter.set_tensor(input_details[0]['index'], x)
            interpreter.invoke()
            output_data = interpreter.get_tensor(output_details[0]['index'])
            output_data = np.squeeze(output_data)
            output_list.append(output_data)

        if args.use_256:
            cnt = 0
            row = []
            for ih in range(nh):
                col = []
                for iw in range(nw):
                    y = output_list[cnt]
                    if iw == 0:
                        col.append(y[:, :ps - mw // 2])
                    elif iw == nw - 1:
                        col.append(y[:, mw // 2:])
                    else:
                        col.append(y[:, mw // 2:ps - mw // 2])

                    cnt += 1

                col_cat = np.concatenate(col, axis=1)
                if ih == 0:
                    row.append(col_cat[:ps - mh // 2])
                elif ih == nh - 1:
                    row.append(col_cat[mh // 2:])
                else:
                    row.append(col_cat[mh // 2:ps - mh // 2])

            result = np.concatenate(row, axis=0)
        else:
            result = output_list[0]

        if floating_model:
            result = data.unnormalize(result)

        if target_path is not None:
            psnr_avg += metric.psnr_np(result, target_img)
            if args.save_results:
                save_as = input_path.replace(input_dir, save_dir)
                os.makedirs(path.dirname(save_as), exist_ok=True)
                imageio.imwrite(save_as, result)

    if target_path is not None:
        print('Avg. PSNR: {:.2f}'.format(psnr_avg / len(input_list)))
        if i_epoch > 1 - c.pre_low_lr:
            viz.show_loss(epoch_losses, logscale=False)
            output_orig = output.cpu()
            viz.show_hist(output_orig)

        with torch.no_grad():
            samples = sample_outputs(c.sampling_temperature)

            if not c.colorize:
                cond = test_cond

            rev_imgs = model.model(samples, cond, rev=True)
            ims = [rev_imgs]

        viz.show_imgs(*list(data.unnormalize(i) for i in ims))

        model.model.zero_grad()

        if (i_epoch % c.checkpoint_save_interval) == 0:
            model.save(c.filename + '_checkpoint_%.4i' %
                       (i_epoch * (1 - c.checkpoint_save_overwrite)))

    model.save(c.filename)

except:
    if c.checkpoint_on_error:
        model.save(c.filename + '_ABORT')

    raise