在我正在做的 coursera 指导项目中,讲师使用from skimage.transform import rescaleimage_rescaled = rescale(rescale(image,0.5),2.0)扭曲图像。在我自己的设备上发生的错误(并且在项目的 jupyter notebook 上没有出现,可能是由于模块和 python 的版本不同)是通道的数量增加image_rescaled了1.例如=>images_normal.shape = (256,256,256,3)和images_with_twice_reshape.shape=(256,256,256,4)如果我使用,则不会出现此问题rescaled(rescale(image,2.0),0.5)。这是在较新版本的 python/skimage 中使用还是我做错了什么?对于其他参考(没有从源代码中删除任何内容,但用#s 突出显示了重要部分):import osimport refrom scipy import ndimage, miscfrom skimage.transform import resize, rescalefrom matplotlib import pyplotimport numpy as npdef train_batches(just_load_dataset=False): batches = 256 # Number of images to have at the same time in a batch batch = 0 # Number if images in the current batch (grows over time and then resets for each batch) batch_nb = 0 # Batch current index ep = 4 # Number of epochs images = [] x_train_n = [] x_train_down = [] x_train_n2 = [] # Resulting high res dataset x_train_down2 = [] # Resulting low res dataset for root, dirnames, filenames in os.walk("data/cars_train.nosync"): for filename in filenames: if re.search("\.(jpg|jpeg|JPEG|png|bmp|tiff)$", filename): filepath = os.path.join(root, filename) image = pyplot.imread(filepath) if len(image.shape) > 2: image_resized = resize(image, (256, 256)) # Resize the image so that every image is the same size#########################通过上面的代码,我得到了x_train_n2.shape = (256,256,256,3)和x_train_down2.shape=(256,256,256,4)。
1 回答
湖上湖
TA贡献2003条经验 获得超2个赞
我能够按如下方式重现您的问题:
import numpy as np
from skimage.transform import resize, rescale
image = np.random.random((512, 512, 3))
resized = resize(image, (256, 256))
rescaled2x = rescale(
rescale(resized, 0.5),
2,
)
print(rescaled2x.shape)
# prints (256, 256, 4)
问题是resize可以推断你的最终维度是通道/RGB,因为你给它一个 2D 形状。rescale,另一方面,将您的数组视为形状为 (256, 256, 3) 的 3D 图像,它下降到 (128, 128, 2),也沿着颜色进行插值,就好像它们是另一个空间维度一样,然后上采样到 (256, 256, 4)。
如果您查看rescale文档,您会发现“多通道”参数,描述为:
图像的最后一个轴是被解释为多通道还是另一个空间维度。
所以,更新我的代码:
rescaled2x = rescale(
rescale(resized, 0.5, multichannel=True),
2,
multichannel=True,
)
print(rescaled2x.shape)
# prints (256, 256, 3)
添加回答
举报
0/150
提交
取消