InvalidArgumentError:无法将张量添加到批次:元素数量不匹配。形状是:[tensor]: [1], [batch]: [2] [Op:IteratorGetNext]

问题描述 投票:0回答:0

我正在尝试使用我的数据集中的验证码,但这样做会导致标题错误。

回溯(最近一次调用最后一次):文件“train.py”,第 137 行,在 train_dataset.take(1) 中用于批处理:... tensorflow.python.framework.errors_impl.InvalidArgumentError: {{function_node__wrapped__IteratorGetNext_output_types_2_device_/job:localhost/replica:0/task:0/device:CPU:0}} 无法将张量添加到批处理中:元素数量不匹配。形状是:[tensor]: 1, [batch]: [2] [Op:IteratorGetNext]

(来源:https://colab.research.google.com/github/keras-team/keras-io/blob/master/examples/vision/ipynb/captcha_ocr.ipynb#scrollTo=6tTa_FX406C5

我的 csv 文件看起来像这样:

(数据集:https://drive.google.com/file/d/1x2HRNA8FkVIqUPQlKN3kAJgsfQc0POo_/view?usp=share_link

我的文字长度是1 ~ 4。 我以为问题是文字长度不同导致的,但是如何解决呢?

我试试

1.dataset.batch(batch_size, drop_remainder = True)

2.https://stackoverflow.com/questions/71061379/invalidarguementerror-cannot-add-tensor-to-the-batch-number-of-elements-does-n

3.https://stackoverflow.com/questions/70091975/invalidargumenterror-cannot-add-tensor-to-the-batch-number-of-elements-does-no

4.batch_size = 1(会有另一个错误)

5.https://github.com/tensorflow/tensorflow/issues/40919

但都不起作用。

我的代码:

import os
import sys
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from pathlib import Path
from collections import Counter

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

df_train = pd.DataFrame(pd.read_csv("dataset/train/annotations.csv"))
df_test  = pd.DataFrame(pd.read_csv("dataset/test/sample_submission.csv"))
pathes = df_train['filename'].to_numpy()
images = np.array(['dataset/train/'+ path for path in pathes])
labels = df_train["label"].to_numpy()
characters = set(char for label in labels for char in label)
characters = sorted(list(characters))

batch_size = 16

img_width = 96
img_height = 96

downsample_factor = 4

max_length = max([len(label) for label in labels])

char_to_num = layers.StringLookup(
    vocabulary=list(characters), mask_token=None
)

num_to_char = layers.StringLookup(
    vocabulary=char_to_num.get_vocabulary(), mask_token=None, invert=True
)

def split_data(images, labels, train_size=0.9, shuffle=True):
    size = len(images)
    indices = np.arange(size)
    if shuffle:
        np.random.shuffle(indices)
    train_samples = int(size * train_size)
    x_train, y_train = images[indices[:train_samples]], labels[indices[:train_samples]]
    x_valid, y_valid = images[indices[train_samples:]], labels[indices[train_samples:]]
    return x_train, x_valid, y_train, y_valid

x_train, x_valid, y_train, y_valid = split_data(np.array(images), np.array(labels))

def encode_single_sample(img_path, label):
    img = tf.io.read_file(img_path)
    img = tf.io.decode_png(img, channels=1)
    img = tf.image.convert_image_dtype(img, tf.float32)
    img = tf.image.resize(img, [img_height, img_width])
    img = tf.transpose(img, perm=[1, 0, 2])
    label = char_to_num(tf.strings.unicode_split(label, input_encoding="UTF-8"))
    return {"image": img, "label": label}

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, drop_remainder = True)
    .prefetch(buffer_size=tf.data.AUTOTUNE)
)

_, ax = plt.subplots(4, 4, figsize=(10, 5))
for batch in train_dataset.take(1):
    images = batch["image"]
    labels = batch["label"]
    for i in range(16):
        img = (images[i] * 255).numpy().astype("uint8")
        label = tf.strings.reduce_join(num_to_char(labels[i])).numpy().decode("utf-8")
        ax[i // 4, i % 4].imshow(img[:, :, 0].T, cmap="gray")
        ax[i // 4, i % 4].set_title(label)
        ax[i // 4, i % 4].axis("off")
plt.show()
python tensorflow keras deep-learning ocr
© www.soinside.com 2019 - 2024. All rights reserved.