将14个数组的列表转换为单个数组(Keras值错误)

2024-10-19 16:41:16 发布

您现在位置:Python中文网/ 问答频道 /正文

当我尝试训练我的模型时,我得到如下Keras值错误

ValueError: Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 14 arrays:

   [array([['0'],
           ['0'],
           ['0'],
           ['0'],
           ['0'],
           ['0'],
           ['0'],
           ['0'],
           ['1'],
           ['0'],
           ['0'],
           ['0'],
           ['0'],
           ['0'],
       ...

当我尝试使用以下方法重塑yïcol时:

y_col = np.stack( y_col, axis=0 )

我得到:

TypeError: If class_mode="multi_output", y_col must be a list. Received ndarray.

如果我只是想和你在一起

y_col = np.array(y_col)

我也有同样的错误

数据帧:

      Path    black  grey  green blue   etc....
0  12345.jpg    1      0       1  0
1  12345.jpg    0      0       1  0
2  12345.jpg    1      0       0  1
3  12345.jpg    0      1       0  1
4  12345.jpg    0      0       1  1
5  12345.jpg    0      0       1  1

每个图像的模型应该是一个由14个元素组成的数组[0,0,1,1,0,1,0,…],但是看起来您为每个图像传递了14个不同的数组

问题发生在CNN网络上,它能识别产品(衣服)的颜色 一种产品可以有多种颜色,例如[0.0,0,1,0,1,0,0,1]

一开始你看起来:

['beige',
 'black',
 'blue',
 'brown',
 'gray',
 'green',
 'multicolor',
 'orange',
 'pink',
 'red',
 'violet',
 'white',
 'yellow',
 'transparent']

发电机:

def get_generator(filename, number=None):
    # 
    df = pd.read_csv(filename, delimiter=' ', names=color_list, dtype="str")
    if number:
        df = df[:number]
    # 
    gen = image.ImageDataGenerator()
    # 
    directory = os.path.dirname(filename)
    # 
    return gen.flow_from_dataframe(df, directory, "path", y_col = y_col , target_size=(224, 224), batch_size=32,class_mode="multi_output")

Tags: ofthe模型numbertargetdfsizemodel
1条回答
网友
1楼 · 发布于 2024-10-19 16:41:16

y_col是包含numpy数组的列表。

y_col = [np.array([['0'], 
                   ['0'], 
                   ['0'], 
                   ['0'], 
                   ['0'], 
                   ['0'], 
                   ['0'], 
                   ['0'], 
                   ['1']])]

使用:

 y_new  = y_col[0].flatten()
 y_new = [int(y) for y in y_new]   

一行:

 y_new = [int(y) for y in y_col[0].flatten()]  

输出:

y_new                                                                                      
#[0, 0, 0, 0, 0, 0, 0, 0, 1]

相关问题 更多 >