Exemplo n.º 1
0
def upscale_image(cfg, src, scale_model, alpha_model=None):
    dst, alpha = split_alpha(src, scale_model)
    for i in range(int(np.ceil(np.log2(cfg.scale_ratio)))):
        six.print_('2.0x upscaling...', end=' ', flush=True)
        model = scale_model if i == 0 or alpha_model is None else alpha_model
        if model.inner_scale == 1:
            dst = iproc.nn_scaling(dst, 2)  # Nearest neighbor 2x scaling
            alpha = iproc.nn_scaling(alpha, 2)  # Nearest neighbor 2x scaling
        if cfg.tta:
            dst = reconstruct.image_tta(dst, model, cfg.tta_level,
                                        cfg.block_size, cfg.batch_size)
        else:
            dst = reconstruct.image(dst, model, cfg.block_size, cfg.batch_size)
        if alpha_model is None:
            alpha = reconstruct.image(alpha, scale_model, cfg.block_size,
                                      cfg.batch_size)
        else:
            alpha = reconstruct.image(alpha, alpha_model, cfg.block_size,
                                      cfg.batch_size)
        six.print_('OK')
    dst_w = int(np.round(src.size[0] * cfg.scale_ratio))
    dst_h = int(np.round(src.size[1] * cfg.scale_ratio))
    if dst_w != dst.size[0] or dst_h != dst.size[1]:
        six.print_('Resizing...', end=' ', flush=True)
        dst = dst.resize((dst_w, dst_h), Image.LANCZOS)
        six.print_('OK')
    if alpha is not None:
        if alpha.size[0] != dst_w or alpha.size[1] != dst_h:
            alpha = alpha.resize((dst_w, dst_h), Image.LANCZOS)
        dst.putalpha(alpha)
    return dst
Exemplo n.º 2
0
def upscale_image(cfg, src, model):
    dst = src.copy()
    six.print_('2.0x scaling...', end=' ', flush=True)
    if model.inner_scale == 1:
        dst = iproc.nn_scaling(dst, 2)  # Nearest neighbor 2x scaling
    if cfg.tta:
        dst = reconstruct.image_tta(dst, model, cfg.tta_level, cfg.block_size,
                                    cfg.batch_size)
    else:
        dst = reconstruct.image(dst, model, cfg.block_size, cfg.batch_size)
    six.print_('OK')
    return dst
Exemplo n.º 3
0
def pairwise_transform(src, cfg):
    unstable_region_offset_x = 8
    unstable_region_offset_y = unstable_region_offset_x * cfg.inner_scale
    top = cfg.offset
    bottom = cfg.crop_size - top
    y = preprocess(src, cfg)

    if cfg.method == 'scale':
        x = scale(y, cfg.downsampling_filters, cfg.resize_blur_min,
                  cfg.resize_blur_max, cfg.inner_scale == 1)
    elif cfg.method == 'noise':
        if cfg.inner_scale != 1:
            raise ValueError('inner_scale must be 1')
        x = noise(y, cfg.nr_rate, cfg.chroma_subsampling_rate, cfg.noise_level)
    elif cfg.method == 'noise_scale':
        if cfg.inner_scale == 1:
            raise ValueError('inner_scale must be > 1')
        x = noise_scale(y, cfg.downsampling_filters, cfg.resize_blur_min,
                        cfg.resize_blur_max, False, cfg.nr_rate,
                        cfg.chroma_subsampling_rate, cfg.noise_level)

    y = y[unstable_region_offset_y:y.shape[0] - unstable_region_offset_y,
          unstable_region_offset_y:y.shape[1] - unstable_region_offset_y]
    x = x[unstable_region_offset_x:x.shape[0] - unstable_region_offset_x,
          unstable_region_offset_x:x.shape[1] - unstable_region_offset_x]
    lowres_y = y.copy()
    if cfg.crop_size != cfg.in_size:
        lowres_y = iproc.nn_scaling(y, 1 / cfg.inner_scale)

    patch_x = np.zeros((cfg.patches, cfg.ch, cfg.in_size, cfg.in_size),
                       dtype=np.uint8)
    patch_y = np.zeros((cfg.patches, cfg.ch, cfg.out_size, cfg.out_size),
                       dtype=np.uint8)

    for i in range(cfg.patches):
        crop_x, crop_y = active_cropping(x, y, lowres_y, cfg.crop_size,
                                         cfg.inner_scale,
                                         cfg.active_cropping_rate,
                                         cfg.active_cropping_tries)
        if cfg.ch == 1:
            ycbcr_x = Image.fromarray(crop_x).convert('YCbCr')
            ycbcr_y = Image.fromarray(crop_y).convert('YCbCr')
            crop_x = np.array(ycbcr_x)[:, :, 0]
            crop_y = np.array(ycbcr_y)[top:bottom, top:bottom, 0]
            patch_x[i] = crop_x[np.newaxis, :, :]
            patch_y[i] = crop_y[np.newaxis, :, :]
        elif cfg.ch == 3:
            crop_y = crop_y[top:bottom, top:bottom, :]
            patch_x[i] = crop_x.transpose(2, 0, 1)
            patch_y[i] = crop_y.transpose(2, 0, 1)
    return patch_x, patch_y