英文:
how to scramble an image into regular patches?
问题
需要将图像分割成3x3或任何常规的块,然后对它们进行随机排序,最后重新组合图像。我尝试使用patchify
,但在unpatchify时出现错误。
import numpy as np
from PIL import Image
from patchify import patchify, unpatchify
import matplotlib.pyplot as plt
from random import shuffle
import torch
import torchvision
import torchvision.transforms as transforms
import requests
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Resize((224,224)),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
url = 'https://hips.hearstapps.com/hmg-prod/images/bright-forget-me-nots-royalty-free-image-1677788394.jpg'
image = Image.open(requests.get(url, stream=True).raw)
plt.imshow(transform(image).permute(1,2,0))
在洗牌或拆分成块之前,图像看起来如下所示:
patched_image = patchify(np.array(image), (100, 100, 3), 100)
shuffled_patches = np.random.shuffle(patched_image)
output_image = unpatchify(shuffled_patches, np.array(image).shape)
错误信息:
AttributeError Traceback (most recent call last)
<ipython-input-34-0b9963e642b8> in <cell line: 1>()
----> 1 output_image = unpatchify(shuffled_patches, np.array(image).shape)
/usr/local/lib/python3.10/dist-packages/patchify/__init__.py in unpatchify(patches, imsize)
52 """
53
---> 54 assert len(patches.shape) / 2 == len(
55 imsize
56 ), "The patches dimension is not equal to the original image size"
AttributeError: 'NoneType' object has no attribute 'shape'
英文:
I need to patch images into let say 3x3 patches, or any regualr patches, then shuffle them and after that reassemble the image. I tried to use patchify
but it gives eerror for unpatchify.
import numpy as np
from PIL import Image
from patchify import patchify, unpatchify
import matplotlib.pyplot as plt
from random import shuffle
import torch
import torchvision
import torchvision.transforms as transforms
import requests
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Resize((224,224)),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
url = 'https://hips.hearstapps.com/hmg-prod/images/bright-forget-me-nots-royalty-free-image-1677788394.jpg'
image = Image.open(requests.get(url, stream=True).raw)
plt.imshow(transform(image).permute(1,2,0))
I looks like the following before shuffling or patchfying
patched_image = patchify(np.array(image), (100, 100, 3), 100)
shuffled_patches = np.random.shuffle(patched_image)
output_image = unpatchify(shuffled_patches, np.array(image).shape)
The error message:
AttributeError Traceback (most recent call last)
<ipython-input-34-0b9963e642b8> in <cell line: 1>()
----> 1 output_image = unpatchify(shuffled_patches, np.array(image).shape)
/usr/local/lib/python3.10/dist-packages/patchify/__init__.py in unpatchify(patches, imsize)
52 """
53
---> 54 assert len(patches.shape) / 2 == len(
55 imsize
56 ), "The patches dimension is not equal to the original image size"
AttributeError: 'NoneType' object has no attribute 'shape'
答案1
得分: 1
np.random.shuffle
不返回任何内容,它会就地对输入进行洗牌,请参见 numpy 文档。这类似于标准例程 sort()
。
当尝试在 unpatchify
过程中使用返回的 None
时,会出现这种情况。
如果你想保留先前的状态,也许可以使用 copy 模块来在洗牌之前创建一个克隆,org_image = copy.copy(patched_image)
应该足够了。
英文:
np.random.shuffle
does not return anything, it shuffles the given input in place, see numpy documentation. This is similar to the standard routine sort()
.
This shows up, when attempting to use the returned None
during unpatchify
.
If you want to keep the previous state as well, maybe the copy module may assist in creating a clone before shuffling, org_image = copy.copy(patched_image)
should be sufficient.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论