[英]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.