【问题标题】:keras' preprocess_input led to ValueError: Data cardinality is ambiguouskeras 的 preprocess_input 导致 ValueError: Data cardinality is ambiguous
【发布时间】:2020-12-21 09:57:03
【问题描述】:

我将 keras 用于回归模型。我正在尝试微调 VGG16。一切正常。在我添加tensorflow.keras.applications.vgg16.preprocess_input 以根据 VGG16 对图像进行预处理后,出现以下错误:

ValueError:数据基数不明确: x 尺寸:1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 尺码:422 确保所有数组都包含相同数量的样本。

加载图像后,我循环浏览主题并应用它:

from keras.preprocessing.image import img_to_array

images = []
for img in loaded_images:
    img = img_to_array(img)
    img = img.reshape((1, img.shape[0], img.shape[1], img.shape[2]))
    img = preprocess_input(img)
    images.append(img)

actual = actual.reshape(1,-1)
assert len(images) == len(actual)

之后,我将图像划分为训练集、有效集和测试集,如下所示:

编辑

train_images,test_images, train_actual_score , test_actual_score = train_test_split(images, scaled_train_mos, test_size=0.20, random_state=42)

最后我调用了fit func:

history = model.fit(x=train_images, y=train_actual_score, validation_data=(test_images, test_actual_score), epochs=30, batch_size=8, verbose=2)

更新

这里是standalone code 来重现上述问题。

【问题讨论】:

  • 你能添加一个独立的代码来重现这个问题吗?为您的图像使用生成的数据。
  • @AniketBote,你到底是什么意思?代码的哪一部分?
  • 我可以在我的系统上运行的代码,而无需下载任何会重现您的错误的数据。
  • @AniketBote 我创建了一个colab,这里是链接colab.research.google.com/drive/…

标签: python numpy tensorflow keras regression


【解决方案1】:

为了使其正常工作,我对您的代码做了一些更改:

第一: 在生成随机数据时,您生成的 img 大小为 (244,24,3),但是您的模型将输入作为 (224,224,3)

# original
im = np.random.randint(0,255,(244,244,3)) 
# changes
im = np.random.randint(0,255,(224,224,3))

第二次: 改变重塑功能:

# Original
from keras.preprocessing.image import img_to_array
images_ = []
for img in images:
    img = img_to_array(img)
    img = img.reshape(1, img.shape[0], img.shape[1], img.shape[2])
    img = preprocess_input(img)
    images_.append(img)
# changed
from keras.preprocessing.image import img_to_array

images_ = []
for img in images:
    img = img_to_array(img)
    img = img.reshape(img.shape[0], img.shape[1], img.shape[2])
    img = preprocess_input(img)
    images_.append(img)

您使用的重塑方法生成 (50, 1, 244, 244, 3) 不正确。虽然我不确定你为什么首先使用重塑,因为你的数据已经是正确的格式。

第三次: 您将分数重塑为形状 (1,50),这意味着您的训练数据仅包含 1 个样本和 50 个值。无需更改分数的形状。 (50,1) 是代表您有 50 个样本和预测值的正确形状。

第四名: 将列表转换为 numpy 数组:

train_images = np.array(train_images)
test_images = np.array(test_images)
train_score = np.array(train_score)
test_score = np.array(test_score)

查看完整的工作代码here

【讨论】:

  • 其实和我给的一样,可能是命名不太清楚。我刷了xy 并发生了同样的错误。正如我所提到的,代码正在运行,只有在我添加了 preprocess_input 之后才发生错误。
猜你喜欢
  • 2021-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-13
  • 2021-06-25
  • 2018-10-05
  • 2018-05-04
  • 1970-01-01
相关资源
最近更新 更多