ValueError:检查目标时出错:预期density_44的形状为(1,),但数组的形状为(3,).他们似乎匹配 [英] ValueError: Error when checking target: expected dense_44 to have shape (1,) but got array with shape (3,). They seem to match though

查看:87
本文介绍了ValueError:检查目标时出错:预期density_44的形状为(1,),但数组的形状为(3,).他们似乎匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了几个类似的主题,涉及类似的问题.例如,等等.尽管如此,我仍然没有设法解决我的问题,为什么现在我要问社区.

I've searched several similar topics covering comparable problems. For example this, this and this, among others. Despite this, I still haven't managed to solve my issue, why I now try to ask the community.

我最终想要做的是使用CNN,并回归预测三个参数.输入是矩阵(初始大小为(3724、4073、3),在我将它们分几步进行预处理后,现在可以将其绘制为RGB图像.由于数据集的大小,我正在使用以下生成器分批(截至目前,共16个)提供CNN:

What I'm ultimately trying to do is with a CNN and regression predict three parameters. The inputs are matrices (and can now be plotted as RGB images after I've pre-processed them in several steps) with the initial size of (3724, 4073, 3). Due to the size of the data set I'm feeding the CNN in batches (of 16 as for now) using the following generator:

class My_Generator(Sequence):
""" Generates batches of training data and ground truth. Inputs are the image paths and batch size. """

def __init__(self, image_paths, batch_size, normalise=True):
    self.image_paths, self.batch_size = image_paths, batch_size
    self.normalise = normalise

def __len__(self):
    return int(np.ceil(len(self.image_paths) / float(self.batch_size)))

def __getitem__(self, idx):
    batch = self.image_paths[idx * self.batch_size:(idx + 1) * self.batch_size]        
    matrices, parameters = [], []
    for file_path in batch:
        mat, param, name = get_Matrix_and_Parameters(file_path)

        #Transform the matrix from 2D to 3D as a (mat.shape[0], mat.shape[1]) RBG image. Rescale its values to [0,1]
        mat = skimage.transform.resize(mat, (mat.shape[0]//8, mat.shape[1]//8, 3), 
                                       mode='constant', preserve_range=self.normalise) 
        param = MMscale_param(param, name)                                              # Rescale the parameters
        matrices.append(mat)
        parameters.append(param)

    MAT, PAM = np.array(matrices), np.array(parameters)
    PAM = np.reshape(PAM, (PAM.shape[0], PAM.shape[1]))
    print("Shape Matrices: {0}, Shape Parameters: {1}".format(MAT.shape, PAM.shape))
    print("Individual PAM shape: {0}".format(PAM[0,:].shape))

    return MAT, PAM

生成器还将矩阵的大小调整了8倍,因为如果不这样做,则会出现内存错误.函数 MMscale_param 只是将参数重新缩放为[0,1].

The generator is also resizing the matrices by 8 times, as I get memory errors when I don't. The function MMscale_param is simply rescaling the parameters to [0, 1].

对于要关注的矩阵,现在生成的批次的形状为(16,465,509,3),对于参数,形状为(16,3).现在,这些内容被馈入以下CNN体系结构:

The generated batches now have the shape (16, 465, 509, 3) as for the matrix to concern and (16, 3) for the parameters. These are now fed into the following CNN architecture:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 463, 507, 16)      448       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 231, 253, 16)      0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 229, 251, 32)      4640      
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 114, 125, 32)      0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 112, 123, 64)      18496     
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 56, 61, 64)        0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 54, 59, 128)       73856     
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 27, 29, 128)       0         
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 25, 27, 256)       295168    
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 12, 13, 256)       0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 39936)             0         
_________________________________________________________________
dense_1 (Dense)              (None, 1000)              39937000  
_________________________________________________________________
dense_2 (Dense)              (None, 100)               100100    
_________________________________________________________________
dense_3 (Dense)              (None, 20)                2020      
_________________________________________________________________
dense_4 (Dense)              (None, 3)                 63        
=================================================================
Total params: 40,431,791
Trainable params: 40,431,791
Non-trainable params: 0
_________________________________________________________________

如上所示,模型的最后一层期望输入为(None,3).如果我理解正确的话,那么任何"批次大小值都可以在此处替换为无",因此我的输入(16,3)或(batch_size,number_of_parameters_to_predict)应该是有效的.但是,我仍然收到以下错误消息:

As displayed above, the last layer in the model expects the input to be (None, 3). If I understand this correct, "any" batch size value could be replaced by "None" here, so my input, (16, 3) or (batch_size, number_of_parameters_to_predict), should be valid. However, I'm still getting the following error message:

ValueError: Error when checking target: expected dense_4 to have shape (1,) but got array with shape (3,)

我发现很奇怪的是,密集层 dense_4 具有形状(1,)的说法.但是,它不是在(3,)形状上方的架构中显示吗?然后,这应该与输入数组的形状(3,)很好地吻合.

What I find to be very strange is the claim that Dense layer dense_4 has shape (1, ). But isn't it displayed in the architecture above that it's a (3, ) shape? This should then fit well with the input array's shape (3, ).

我试图以几种方式重塑和/或转置数组,但没有成功.我什至卸载并重新安装了TensorFlow和Keras,以为那里出了点问题,但还是没事.

I've tried to reshape and/or transpose the array in several ways but without success. I've even uninstalled and reinstalled TensorFlow and Keras in the belief that something was wrong there, but still nothing.

但是,有效的方法是尝试仅预测三个参数之一,使我们的输入形状为(1,0). (尽管稍后会产生其他与内存相关的错误.)这实际上与我塑造 dense_4 层的方式无关,这意味着(None,1)和(None,3)都可以工作,根据我的知识有限,没有任何意义.

What does work however, is to try to only predict one of the three parameters, giving us an input shape of (1, 0). (Later yielding other, memory related, errors though.) This actually works independently of how I shape the dense_4 layer, meaning that both (None, 1) and (None, 3) works, which according to my limited knowledge, doesn't make any sense.

添加编辑;

batch_size = 16
my_training_batch_generator_NIR = My_Generator(training_paths_NIR, batch_size)
my_validation_batch_generator_NIR = My_Generator(validation_paths_NIR, batch_size)

model.compile(loss='sparse_categorical_crossentropy', optimizer='adam')

以及培训代码:

model_path = "/Models/weights.best.hdf5"
num_epochs = 10
checkpointer = ModelCheckpoint(filepath=model_path, 
                           verbose=1, 
                           save_best_only=True)

model.fit_generator(generator=my_training_batch_generator_NIR,
                steps_per_epoch=(len(validation_paths_NIR) // batch_size),
                epochs=num_epochs,
                verbose=1,
                callbacks=[checkpointer],
                validation_data=my_validation_batch_generator_NIR, 
                validation_steps=(len(validation_paths_NIR) // batch_size), 
                use_multiprocessing=True, 
                max_queue_size=1,
                workers=1)

因此,总而言之:我在将(3,)数组拟合到(3,)层中时遇到问题.然而,后者被要求具有形状(1,).我一定在这里遗漏了一些东西,因为它不可能是bug,对吗?

So, to sum up: I'm having problems fitting a (3, ) array into, what I believe is, a (3, ) layer. However, the latter is claimed to be of shape (1, ). I must be missing something out here, because it can't be a bug, can it?

任何帮助将不胜感激.

Any help would be kindly appreciated.

我在Ubuntu上使用Keras版本2.2.2和TensorFlow 1.9.0后端.

I'm using Keras version 2.2.2 with TensorFlow 1.9.0 backend on Ubuntu.

推荐答案

这是因为您正在使用损失函数.替换为

This is because of loss function you are using. Replace that with

    loss='categorical_crossentropy'

代码应该可以正常工作.

Code should work then.

这篇关于ValueError:检查目标时出错:预期density_44的形状为(1,),但数组的形状为(3,).他们似乎匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆