简体   繁体   English

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

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

Before I start I would like to point out that I am fairly new to this subject therefore I still have a lot to learn and if it is not much to ask for, I would like a clear answer so I can really grasp the idea behind.在我开始之前,我想指出我对这个主题还很陌生,因此我还有很多东西要学,如果要求不多,我想要一个明确的答案,这样我才能真正掌握背后的想法。

So my problem as it is presented in the question is about how can I feed a label that is essentially a tuple with several values in it to my fit function so that I can train my model.所以我在问题中提出的问题是关于如何将 label 提供给我的fit function 的 label,它本质上是一个包含多个值的元组,以便我可以训练我的 Z20F35E630DAF44DFA4C3F68F593。 I've tried to transform it into a numpy array and then feed it to my model using asarray function.我尝试将其转换为 numpy 数组,然后使用asarray function 将其提供给我的 model。

label = np.asarray(label)

but it gives me an error essentially saying this:但它给了我一个错误,基本上是这样说的:

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

Which makes sense since the tuple is made by 6 values and after transforming it into a numpy array i get 6 elements in it, so when i pass the image and the label which now has 6 elements, to fit function it arises this error because im essentially passing 6 labels for just one image right?这是有道理的,因为元组是由 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 has 6 features in it? 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个功能吗?

Background:背景:

So I am working with Convolutional Neural Networks ( Conv2D ) and I am trying to build a model that can identify American car plates.所以我正在使用卷积神经网络 ( Conv2D ),并且我正在尝试构建一个可以识别美国车牌的 model。 The images that I have, have only license plates with 6 number/characters and thats what goes into the labels.我拥有的图像只有带有 6 个数字/字符的车牌,这就是标签中的内容。 I have a parseImgFunction that receives the photo and returns return (image_mat,label) .我有一个parseImgFunction接收照片并返回return (image_mat,label) This label has 6 elements in it (each representing one characters/number of the plate) and is a tuple.这个 label 中有 6 个元素(每个元素代表一个字符/车牌号),是一个元组。 Basically I want to use this label in fit like its shown below so that for each image I have a label with 6 features each representing a part of the plate.基本上我想使用这个fit ,如下图所示,这样对于每张图像我都有一个 label 有 6 个特征,每个特征代表板的一部分。 Also the image that I fed to the model was already reshaped.此外,我提供给 model 的图像已经被重新塑造。

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

Thanks in advance!提前致谢!

Edit:编辑:

Sorry for not giving you the necessary code.很抱歉没有给你必要的代码。 Here is the following code that i am using:这是我正在使用的以下代码:

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)

Versions:版本:

Keras-2.3.1 Tensorflow-2.0.0 Python-3.7 Keras-2.3.1 Tensorflow-2.0.0 Python-3.7

Although I do not have code (please update the post with the code), it seems that, when you feed data to your model, your classes are not separated in samples, as provided in this comment: Value error: Input arrays should have the same number of samples as target arrays.虽然我没有代码(请用代码更新帖子),但似乎当您将数据提供给 model 时,您的类没有在示例中分开,如此评论中所提供: 值错误:输入 arrays 应该具有与目标 arrays 相同数量的样本。 Found 1600 input samples and 6400 target samples . 找到 1600 个输入样本和 6400 个目标样本

Ensure the right preprocessing is done in order to solve your problem.确保完成正确的预处理以解决您的问题。

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

相关问题 如何将实时音频实时传输到 keras model? - How can I feed live audio to a keras model in real time? 如何将多个数组提供给 keras 机器学习算法? - How can i feed multiple arrays to a keras machine learning algorithm? 如何使用 numpy 数组值在 Keras model 中输入张量? - How do I feed a tensor in Keras model using a numpy array values? 如何通过fit_generator为Keras模型提供多个输入 - How to feed Keras model with multiple inputs through fit_generator 如何将图像输入模型Keras - How to feed in images into the model Keras 如何将列字符串值分解为单个单词? 每个值由多个字符串值组成。 早期学习者在这里 - How can I break column string values into individual words? Each value consists of multiple string values. Early learner here Python:如何用元组中的值替换多次出现的字符串 - Python: How can I replace multiple occurrences of a string with values in tuple How can I use tf.keras.Model.summary to see the layers of a child model which in a father model? - How can I use tf.keras.Model.summary to see the layers of a child model which in a father model? 如何将多个值传递给 keras 加入 model - How to pass multiple values to keras joined model 如何使用ImageDataGenerator类生成训练并将其标记为keras模型的图像? - How can I use ImageDataGenerator class to generate either train and label as image for a keras model?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM