首页 > 解决方案 > 奇怪的张量流问题:排名下降

问题描述

我不确定这里发生了什么。我一直在阅读有关此错误的信息,并将其解释为与图像重塑有关的错误。由于某种原因,最后 3 个等级丢失了。数据集的每个图像的宽度和高度都未标准化。处理后的图像应该是一个正方形,这是 scikit 的调整大小功能无法填充空间的地方。相反,它将基于最大维度的按比例缩放的集合放入 train_X 中。这是流程的一个想法。

def read_img(file):  
    img = skimage.io.imread(img_folder + file)
    img = skimage.transform.resize(img, (img_height, img_width), mode='reflect')

    return img[:,:,:img_channels] #the last 3 ranks meant to fill in the traceback

read_img 函数输入到 train_X。

train_X = np.stack(train_['file'].apply(read_img))

这是追溯。

Traceback (most recent call last):
File "A:\anoth\...\newmodel.py", line 196, in <module>
generator, train_X, val_X, test_X, train_y, val_y, test_y = 
prepare2train(train_, val_, test_, 'Category')
File "A:\anoth\...\newmodel.py", line 192, in prepare2train
generator.fit(train_X,augment=True, rounds=50, seed=43)
File "A:\anoth\Anaconda\lib\site-packages\keras_preprocessing\image.py", line 1347, in fit
'Got array with shape: ' + str(x.shape))
ValueError: Input to `.fit()` should have rank 4. Got array with shape: (6848,)

我是否正确理解了这个问题?如果是这样,为什么 train_X 会下降最后 3 个等级?我该如何解决这个问题?

标签: pythontensorflowmachine-learningkerascomputer-vision

解决方案


img = skimage.transform.resize(img, (img_height, img_width), mode='reflect') 由于某种原因没有适当地调整图像大小。在 python 之外调整图像大小并且不使用这行代码解决了排名下降的问题。


推荐阅读