繁体   English   中英

如何将包含多个值(它是一个元组)的 label 提供给 keras model

[英]How can I feed a label which consists in multiple values (it is a tuple) to a keras model

在我开始之前,我想指出我对这个主题还很陌生,因此我还有很多东西要学,如果要求不多,我想要一个明确的答案,这样我才能真正掌握背后的想法。

所以我在问题中提出的问题是关于如何将 label 提供给我的fit function 的 label,它本质上是一个包含多个值的元组,以便我可以训练我的 Z20F35E630DAF44DFA4C3F68F593。 我尝试将其转换为 numpy 数组,然后使用asarray function 将其提供给我的 model。

label = np.asarray(label)

但它给了我一个错误,基本上是这样说的:

ValueError: Input arrays should have the same number of samples as target arrays. Found 1 input samples and 6 target samples.

这是有道理的,因为元组是由 6 个值组成的,并且在将其转换为 numpy 数组后,我得到了 6 个元素,所以当我传递图像和 label 现在有 6 个元素时,以fit ZC1C4252074AB7A94FC14 它出现此错误基本上只为一张图像传递了 6 个标签,对吗? So my question is, how can I feed the label, with those 6 features which represent different parts of the image that i want the model to be able to recognise, to the fit function so that the model can be trained based on that label which有6个功能吗?

背景:

所以我正在使用卷积神经网络 ( Conv2D ),并且我正在尝试构建一个可以识别美国车牌的 model。 我拥有的图像只有带有 6 个数字/字符的车牌,这就是标签中的内容。 我有一个parseImgFunction接收照片并返回return (image_mat,label) 这个 label 中有 6 个元素(每个元素代表一个字符/车牌号),是一个元组。 基本上我想使用这个fit ,如下图所示,这样对于每张图像我都有一个 label 有 6 个特征,每个特征代表板的一部分。 此外,我提供给 model 的图像已经被重新塑造。

history = model.fit(image, label, epochs=1, steps_per_epoch=100)

提前致谢!

编辑:

很抱歉没有给你必要的代码。 这是我正在使用的以下代码:

dataset = tf.data.TFRecordDataset('american_car_plates.tfrecords')

feature_description = {'first': tf.io.FixedLenFeature([], tf.int64),
            'second': tf.io.FixedLenFeature([], tf.int64),
            'third':  tf.io.FixedLenFeature([], tf.int64),
            'forth': tf.io.FixedLenFeature([], tf.int64),
            'fifth':  tf.io.FixedLenFeature([], tf.int64),
            'sixth': tf.io.FixedLenFeature([], tf.int64),
            'raw': tf.io.FixedLenFeature([], tf.string),
        }



def parseImgFunction(proto):
  aux = tf.io.parse_single_example(proto,  feature_description)
  raw = aux['raw']

  first = aux['first']
  second = aux['second']
  third = aux['third']
  forth = aux['forth']
  fifth = aux['fifth']
  sixth = aux['sixth']
  full_label = (first, second, third, forth, fifth, sixth)
  label = full_label

  image = tf.io.decode_jpeg(raw, channels=1)
  image = tf.cast(image, dtype=tf.float32)
  image_mat = 1 / 255 * image


  return (image_mat,label)

mapped_images = dataset.map(parseImgFunction)

it = iter(mapped_images)
image_mat, label = next(it)
im = tf.squeeze(image_mat).numpy()

im = im.reshape([-1, 620, 420, 1])
label = np.asarray(label)


input = Input(shape=(620, 420, 1))
conv1 = Conv2D(16, kernel_size=(3, 3), activation='relu')(input)
max1 = MaxPooling2D((2, 2))(conv1)
drop1 = Dropout(0.2)(max1)
conv2 = Conv2D(24, kernel_size=(3, 3), activation='relu')(drop1)
max2 = MaxPooling2D((2, 2))(conv2)
drop2 = Dropout(0.2)(max2)
flat1 = Flatten()(drop2)
dense1 = Dense(128, activation='relu')(flat1)
drop3 = Dropout(0.2)(dense1)
out = Dense(1, activation='relu')(drop3)
model = Model(input, out)
print(model.summary())

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
history = model.fit(im, label, epochs=1, steps_per_epoch=100)

版本:

Keras-2.3.1 Tensorflow-2.0.0 Python-3.7

虽然我没有代码(请用代码更新帖子),但似乎当您将数据提供给 model 时,您的类没有在示例中分开,如此评论中所提供: 值错误:输入 arrays 应该具有与目标 arrays 相同数量的样本。 找到 1600 个输入样本和 6400 个目标样本

确保完成正确的预处理以解决您的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM