【问题标题】:ValueError: Input arrays should have the same number of samples as target arrays. Found 0 input samples and 121 target samplesValueError:输入数组应具有与目标数组相同数量的样本。找到 0 个输入样本和 121 个目标样本
【发布时间】:2023-03-10 17:52:01
【问题描述】:

我正在尝试进行 6 级分类。代码如下:

############### CONFIG_CNN3D ##############
RESULT_PATH = 'results3d'
MODEL_NAME = 'saved3d'
MODEL = 'cnn3d'
CHANCE = .01
TEST_TRAIN_SPLIT = .2
SIZE3D = (64, 64)
DEPTH = 3
CHANNELS = 1
BATCH_SIZE = 128
EPOCHS = 30
EXTRACT = False
############### CONFIG ##############

categories = ['Basketball', 'Biking', 'Bowling', 'Diving', 'Haircut', 'PlayingGuitar']

def load_data3d():
    ret_X = []
    ret_y = []
    for train_or_test in 'train', 'test':
        data = []
        labels = []
        for label, category in enumerate(categories):
            files = glob.glob(os.path.join(DATA_PATH, CLIP_PATH, train_or_test, category, '*.avi'))
            print("%3d. Category %-50s  %-7d files" % (label, category, len(files)))
            for file in files:
                print('Loaded',file)
                video = Video(file)
                frame_array = []
                for index in range(len(video)-5):
                    frame=video.read()
                    if config3d.CHANNELS == 1:
                        frame = im2gray(frame).reshape(*frame.shape[:-1], 1)
                    frame_array.append(frame)
                frame_array = np.array(frame_array)
                data.append(frame_array)
                labels.append(label)
        print("Shape of data:",np.array(data).shape)
        if not config3d.EXTRACT:
            X = np.array(data).transpose((1, 0))
            X = X.reshape((X.shape[0], *config3d.SIZE3D, config3d.DEPTH, config3d.CHANNELS))
            X = X / 255
        else:
            from .extractor import Extractor
            extractor = Extractor()
            X = []
            for frame_array in data:
                frame_array = extractor.extract(frame_array)
                X.append(frame_array)
            X = np.array(X)
        y = np.array(labels)
        y = np_utils.to_categorical(y, len(categories))

        print('X_%s.shape:' % train_or_test, X.shape)
        print('y_%s.shape:' % train_or_test, y.shape)

        ret_X.append(X)
        ret_y.append(y)
    return ret_X + ret_y

当我运行它时,我得到了结果

Shape of data: (121, 0)
X_train.shape: (0, 64, 64, 3, 1)
y_train.shape: (121, 6)

和错误信息:

Traceback (most recent call last):
  File "fit3d.py", line 16, in <module>
    verbose=True)
  File "C:\Users\VCTrung\anaconda3\envs\mypython3\lib\site-packages\keras\models.py", line 1002, in fit
    validation_steps=validation_steps)
  File "C:\Users\VCTrung\anaconda3\envs\mypython3\lib\site-packages\keras\engine\training.py", line 1630, in fit
    batch_size=batch_size)
  File "C:\Users\VCTrung\anaconda3\envs\mypython3\lib\site-packages\keras\engine\training.py", line 1490, in _standardize_user_data
    _check_array_lengths(x, y, sample_weights)
  File "C:\Users\VCTrung\anaconda3\envs\mypython3\lib\site-packages\keras\engine\training.py", line 220, 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 0 input samples and 121 target samples.

“ValueError:输入数组应具有与目标数组相同数量的样本。找到 0 个输入样本和 121 个目标样本”。 我不明白为什么输入是 0。请帮助我解决解决方案和对代码进行必要的修改。提前致谢。

【问题讨论】:

  • 你的 X_train 形状很荒谬。根据您的 121 个目标样本,您的 X_train 应该是 (121, H, W, C)。 (H、W、C 是输入图像的高度、宽度和通道)。

标签: python arrays tensorflow keras


【解决方案1】:

考虑到您提到的渠道和深度是相同的。您可以使用此 sn-p 替换您的代码。 X_train 的输出形状应该是 (121, 64, 64, 1)。

        X = np.array(data)
        X = X.reshape((X.shape[0], *config3d.SIZE3D, config3d.CHANNELS))
        X = X / 255

【讨论】:

  • 我收到一条错误消息:X = X.reshape((X.shape[0], *config3d.SIZE3D, config3d.CHANNELS)) ValueError: cannot reshape array of size 0 into shape (110,64,64,1)
猜你喜欢
  • 2020-08-31
  • 2019-05-30
  • 2018-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-15
  • 2017-10-26
  • 2019-05-22
相关资源
最近更新 更多