dovahcrow / patchify.py

A library that helps you split image into small, overlappable patches, and merge patches into original image.
MIT License
211 stars 25 forks source link

The unpatchify need a very small step... #14

Closed jinxiqinghuan closed 1 year ago

jinxiqinghuan commented 2 years ago

Hello, I have a problem when i useing patchify.

In my task, i want divide different size of images to (256, 256), and then input them to a (Deep learning generation) model. Last, I want unpatchify them. This is my code:


def choose_step(num1, num2):
    """Choose the max step, to acclerate the algorithm.
     求长和宽的公约数集合,满足小于256的最大公约数即为最大step。
    """
    num1 = num1 - 256
    num2 = num2 - 256
    cd1 = [a for a in range(1, num1+ 1) if num1 % a==0]
    cd2 = [a for a in range(1, num2+ 1) if num2 % a==0]
    cd = list(set(cd1) & set(cd2))
    cd = sorted(cd, reverse=True)
    for i in range(len(cd)):
        if cd[i] < 256:
            return cd[i]
        else:
            continue
    print("The max cd is 1!")
    return 1

for id in range(len(im_list)):
    # im = Image.open(im_list[id]).convert('RGB')
    im = mpimg.imread(im_list[id])
    im = np.array(im).swapaxes(0, 1)
    f_name = os.path.basename(im_list[id])
    max_step = choose_step(im.shape[0], im.shape[1])
    print(im.shape, max_step)
    im_patches = patchify(im, patch_size=(256, 256, 3), step=max_step)    
    trans = transforms.Compose(transforms_)
    for i in tqdm(range(im_patches.shape[0])):
        for j in range(im_patches.shape[1]):
            im_patch = im_patches[i][j][0]
            im_patch = trans(Image.fromarray((im_patch)))
            im_patch = im_patch.unsqueeze(0)
            im_patch = generator(im_patch.cuda())
            im_patch = im_patch.squeeze(0).cpu().detach().numpy()
            im_patch = im_patch.swapaxes(0, 2).swapaxes(0, 1)
            im_patch = (im_patch + 1) * 128
            im_patch = im_patch.astype('uint8')
            im_patches[i][j][0] = im_patch   
    out = unpatchify(im_patches, imsize=(im.shape))
    print(out.min(), out.max())
    out = out.swapaxes(0, 1)
    mpimg.imsave(output_path + "/" + f_name, out)
    print("已保存第{}张测试图片".format(id))

But, i found most of the images need a very small step, which causes the algorithm to run very slowly. There will be a lot of extra image parts that need to be synthesized. I think there may be a problem with the algorithm I chose step, but according to the requirements in your document, I can't think of other algorithm implementation methods.

antalele commented 1 year ago

you can put every image in a frame with the same dimensions, or anyway in the smallest frame allowing a comfortable step (e.g. you choose 128 as the desired step, you have an image of 300x300, q=300//128, dim=(q+1)*128 if 300%128==0 else 300)

jinxiqinghuan commented 1 year ago

Thank you very much for your suggestion. @antalele