[英]Cannot add tensor to the batch: number of elements does not match. Shapes are: [tensor]: [585,1024,3], [batch]: [600,799,3]
[英]InvalidArgumentError: Cannot add tensor to the batch: number of elements does not match. Shapes are: [tensor]: [4], [batch]: [5] [Op:IteratorGetNext]
任務:Keras 驗證碼或 model 訓練。
問題:我正在嘗試從我的驗證集中打印驗證碼,但這樣做會導致以下錯誤
InvalidArgumentError Traceback (most recent call last)
<ipython-input-6-df1fce607804> in <module>()
1
2 #_, ax = plt.subplots(1, 4, figsize=(10, 5))
----> 3 for batch in validation_dataset.take(1):
4 images = batch["image"]
5 labels = batch["label"]
3 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py in raise_from_not_ok_status(e, name)
7105 def raise_from_not_ok_status(e, name):
7106 e.message += (" name: " + name if name is not None else "")
-> 7107 raise core._status_to_exception(e) from None # pylint: disable=protected-access
7108
7109
InvalidArgumentError: Cannot add tensor to the batch: number of elements does not match. Shapes are: [tensor]: [4], [batch]: [5] [Op:IteratorGetNext]
打印我的 output 的代碼,這是我嘗試過的:
#_, ax = plt.subplots(1, 4, figsize=(10, 5))
for batch in validation_dataset.take(1):
images = batch["image"]
labels = batch["label"]
for i in range(batch_size):
img = (images[i] * 255).numpy().astype("uint8")
label = tf.strings.reduce_join(num_to_char(labels[i])).numpy().decode("utf-8")
plt.title(label)
plt.imshow(img[:, :, 0].T, cmap="gray")
plt.show()
對於這個任務,我嘗試設置批量大小 1,但我想用更高的批量大小訓練我的 model 我的批量大小 = 16
# Mapping integers back to original characters
num_to_char = layers.StringLookup(
vocabulary=char_to_num.get_vocabulary(), mask_token=None, invert=True
)
這是取自 tensorflow 數據集文檔的代碼,用於在 tf 創建數據集對象中將數據轉換為數據集類型
train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train]))
train_dataset = (
train_dataset.map(
encode_single_sample, num_parallel_calls=tf.data.AUTOTUNE
)
.batch(batch_size)
.prefetch(buffer_size=tf.data.AUTOTUNE).repeat(10)
)
validation_dataset = tf.data.Dataset.from_tensor_slices((x_valid, y_valid]))
validation_dataset = (
validation_dataset.map(
encode_single_sample, num_parallel_calls=tf.data.AUTOTUNE
)
.batch(batch_size)
.prefetch(buffer_size=tf.data.AUTOTUNE)
)
此代碼確實讀取圖像並預處理圖像以使所有圖像具有統一的形狀 Function 編碼單個樣本
def encode_single_sample(img_path, label):
# 1. Read image
img = tf.io.read_file(img_path)
# 2. Decode and convert to grayscale
img = tf.io.decode_png(img, channels=3)
# 3. Convert to float32 in [0, 1] range
img = tf.image.convert_image_dtype(img, tf.float32)
# 4. Resize to the desired size
img = tf.image.resize(img, [img_height, img_width])
# 5. Transpose the image because we want the time
# dimension to correspond to the width of the image.
img = tf.transpose(img, perm=[1, 0, 2])
# 6. Map the characters in label to numbers
label = char_to_num(tf.strings.unicode_split(label, input_encoding="UTF-8"))
# 7. Return a dict as our model is expecting two inputs
return {"image": img, "label": label}
編輯:關於數據:這是來自數據的樣本。 它類似於keras ocr 示例數據集。 盡管圖像大小各不相同,但驗證碼模式的可變性非常小。 它維護 5 個長度的單詞,中間有 1 個或兩個數字。 單詞總是大寫的。
我哪里錯了?
使用此處提供的 Colab 筆記本,我能夠可視化來自validation_set
的樣本。 該錯誤可能是由於某些 Tensorflow 版本沖突。 另外,我不確定這是否是錯字,但您絕對應該從以下內容中刪除方括號:
train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train]))
和
validation_dataset = tf.data.Dataset.from_tensor_slices((x_valid, y_valid]))
此外,如果您按照我鏈接的教程進行操作,則應使用 1 個頻道而不是 3 個:
img = tf.io.decode_png(img, channels=3)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.