python - 值错误 : Input arrays should have the same number of samples as target arrays. 找到 166 个输入样本和 4 个目标样本

标签 python python-3.x deep-learning classification

我正在使用 Keras DL 库对图像数据集进行分类。我在尝试训练模型时遇到错误。

我正在处理的数据集没有大量数据,因此训练集示例包含 166 张图像。我不确定这个错误,但我认为我必须以某种方式更改标签集的形状才能修复它。这是代码:

import tensorflow as tf
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D


DIR = '/home/.../'
IMG_H = 256
IMG_W = 256
IMG_CH = 1


loadFile = DIR + 'Img.npz'
X = np.load(loadFile)
trainImgSet = X['trainImgSet']
trainLabelSet = X['trainLabelSet']
testImgSet = X['testImgSet']

print('Shape of trainImgSet: {}'.format(trainImgSet.shape))
print('Shape of trainLabelSet: {}'.format(trainLabelSet))
#print('Shape of testImgSet:{}'.format(testImgSet))


model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(256, (3, 3), input_shape = (IMG_H, IMG_W, IMG_CH)))
model.add(tf.keras.layers.Activation('relu'))
model.add(tf.keras.layers.MaxPooling2D(pool_size=(1, 1)))

model.add(tf.keras.layers.Conv2D(256, (3, 3)))
model.add(tf.keras.layers.Activation('relu'))
model.add(tf.keras.layers.MaxPooling2D(pool_size=(1, 1)))

model.add(tf.keras.layers.Flatten())

model.add(tf.keras.layers.Dense(64))

model.add(tf.keras.layers.Dense(1))
model.add(tf.keras.layers.Activation('sigmoid'))

model.compile(loss='binary_crossentropy', 
                optimizer='adam',
                metrics=['accuracy'])

model.summary()

#train the CNN
model.fit(trainImgSet, trainLabelSet, batch_size=10, epochs=5, validation_split=0.1)



Here is the error:
Traceback (most recent call last):
  File "/home/Code/DeepCl.py", line 49, in <module>
    model.fit(trainImgSet, trainLabelSet, batch_size=10, epochs=5, validation_split=0.1)
  File "anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1536, in fit
    validation_split=validation_split)
  File "/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 992, in _standardize_user_data
    class_weight, batch_size)
  File "/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1169, in _standardize_weights
    training_utils.check_array_lengths(x, y, sample_weights)
  File "/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_utils.py", line 426, in check_array_lengths
    'and ' + str(list(set_y)[0]) + ' target samples.')
ValueError: Input arrays should have the same number of samples as target arrays. Found 166 input samples and 4 target samples.

最佳答案

在这里,

  • 训练样本数不等于标签数。

  • 有 144 个训练样本,但只有 4 个标签。

  • 训练数据和测试数据的形状必须具有相同数量的样本。

  • 例如。训练数据的形状为 ( 100 , 256 , 256 , 1 )。测试数据的形状应为 ( 100 , 1 )

关于python - 值错误 : Input arrays should have the same number of samples as target arrays. 找到 166 个输入样本和 4 个目标样本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53888320/

相关文章:

python - 未绑定(bind)本地错误 : local variable 'error' referenced before assignment

Python 拆分 url 以查找图像名称和扩展名

python - 使用 python 绑定(bind)安装 dlib

python - PyTorch 中 RNN(nn.LSTM、nn.GRU 等)的输出 h_n 是如何构造的?

python - 仅在覆盆子上的 Opencv 黑色图像

python - 获得所需数据后,如何关闭 Python 2.5.2 Popen 子进程?

python - 关于背景透明 .png 格式 OpenCV 与 Python 的问题

html - 使用 Flask 作为后端时,在 div 的文本中呈现换行符?

python - 为什么 Keras 不返回 lstm 层中细胞状态的完整序列?

python - 在 Tensorflow 上训练卷积神经网络时 GPU 内存不足