【问题标题】:Tensorflow InvalidArgumentError : Input is emptyTensorflow InvalidArgumentError:输入为空
【发布时间】:2022-10-24 21:23:49
【问题描述】:

查看论坛后,我没有找到解决问题的方法。 我目前正在实现一种识别手写的 OCR 算法。为此,我正在使用 tensorflow,但它带来了一些困难:

  • 我有一个文件夹,其中包含我的数据集的图像
  • 我实现了一个数据集tensorflow中所有图像的读取和转换

--> 当我在我的数据集上迭代时,发生了以下错误(阻止执行任何预测算法):

for X_t,y_t in dataset_train:
  pass

InvalidArgumentError: Input is empty.
     [[{{function_node __inference_load_image_130965}}{{node DecodePng}}]] [Op:IteratorGetNext] 

我猜我的问题来自阅读我的一张图片,但我找不到错误。 我正在寻找一种解决方案来从我的张量中删除坏图像,或者避免在预处理过程中将坏图像添加到我的张量中。

我的转换功能如下:

@tf.function

def load_image(filepath):

    im = tf.io.read_file(filepath)

    im = tf.image.decode_png(im, channels=0)

    return im

@tf.function

def preprocess(filepath, imgSize=(32, 128), dataAugmentation=False, scale=0.8, isthreshold=False):

    img = load_image(filepath)/255

    # there are damaged files in IAM dataset - just use black image instead

    if img is None:

        img = tf.ones([imgSize[0], imgSize[1], 1])

        print("None")

    if dataAugmentation:

        stretch = scale*(tf.random.uniform([1], 0, 1)[0] - 0.3) # -0.5 .. +0.5

        wStretched = tf.maximum(int(float(tf.shape(img)[0]) * (1 + stretch)), 1) # random width, but at least 1

        img = tf.image.resize(img, (wStretched, tf.shape(img)[1])) # stretch horizontally by factor 0.5 .. 1.5

    (wt, ht) = imgSize

    w, h = float(tf.shape(img)[0]), float(tf.shape(img)[1])

    fx = w / wt

    fy = h / ht

    f = tf.maximum(fx, fy)

    newSize = (tf.maximum(tf.minimum(wt, int(w / f)), 1), tf.maximum(tf.minimum(ht, int(h / f)), 1)) # scale according to f (result at least 1 and at most wt or ht)

    img = tf.image.resize(img, newSize)

    dx = wt - newSize[0]

    dy = ht - newSize[1]

    if dataAugmentation :

        dx1=0

        dy1=0

        if dx!=0:

            dx1 = tf.random.uniform([1], 0, dx, tf.int32)[0]

        if dy!=0:

            dy1 = tf.random.uniform([1], 0, dy, tf.int32)[0]

        img = tf.pad(img[..., 0], [[dx1, dx-dx1], [dy1, dy-dy1]], constant_values=1)

    else :

        img = tf.pad(img[..., 0], [[0, dx], [0, dy]], constant_values=1)

    if isthreshold:

        return tf.expand_dims(1-(1-img)*tf.cast(img < 0.8, tf.float32), -1)

    return tf.expand_dims(img, -1)

您将在下面找到我的 google colab 的链接,以允许您直接执行我的代码(执行前七行): MY GOOGLE COLAB

【问题讨论】:

  • 该答案可能对您有所帮助:stackoverflow.com/a/68192520/7370153。这是一个不同的问题,但我想该解决方案也可能适用于您的情况。
  • 正是我想要的。它运行良好,感谢您的支持。

标签: python tensorflow image-processing tensorflow-datasets


【解决方案1】:

很简单。我在随机函数上使用了一些时间,结果你需要了解图像增强可以在训练神经元上进行学习,但它会给每个函数带来困难。 (我也在做一些简单的任务) - 查看你从 Fn 延伸的输出。

示例:就像婴儿一样,当你留着小胡子时,你需要告诉他们一些别的东西,这给 NN 学习造成了困难。 简单的拥抱告诉你的声音,但在屏幕后执行动作,让你的脸颊长凳并扭曲。

import os
from os.path import exists

import tensorflow as tf
import tensorflow_io as tfio

import matplotlib.pyplot as plt

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
None
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
physical_devices = tf.config.experimental.list_physical_devices('GPU')
assert len(physical_devices) > 0, "Not enough GPU hardware devices available"
config = tf.config.experimental.set_memory_growth(physical_devices[0], True)
print(physical_devices)
print(config)

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Variables
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
PATH = os.path.join('F:\datasets\downloads\Actors\train\Pikaploy', '*.tif')
PATH_2 = os.path.join('F:\datasets\downloads\Actors\train\Candidt Kibt', '*.tif')
files = tf.data.Dataset.list_files(PATH)
files_2 = tf.data.Dataset.list_files(PATH_2)

list_file = []
list_file_actual = []
list_label = []
list_label_actual = [ 'Pikaploy', 'Pikaploy', 'Pikaploy', 'Pikaploy', 'Pikaploy', 'Candidt Kibt', 'Candidt Kibt', 'Candidt Kibt', 'Candidt Kibt', 'Candidt Kibt' ]
for file in files.take(5):
    image = tf.io.read_file( file )
    image = tfio.experimental.image.decode_tiff(image, index=0)
    list_file_actual.append(image)
    image = tf.image.resize(image, [32,32], method='nearest')
    image = tfio.experimental.color.rgba_to_rgb( image, name='rgba_to_rgb' )
    list_file.append(image)
    list_label.append(1)
    
for file in files_2.take(5):
    image = tf.io.read_file( file )
    image = tfio.experimental.image.decode_tiff(image, index=0)
    list_file_actual.append(image)
    image = tf.image.resize(image, [32,32], method='nearest')
    image = tfio.experimental.color.rgba_to_rgb( image, name='rgba_to_rgb' )
    list_file.append(image)
    list_label.append(9)

checkpoint_path = "F:\models\checkpoint\" + os.path.basename(__file__).split('.')[0] + "\TF_DataSets_01.h5"
checkpoint_dir = os.path.dirname(checkpoint_path)
loggings = "F:\models\checkpoint\" + os.path.basename(__file__).split('.')[0] + "\loggings.log"

if not exists(checkpoint_dir) : 
    os.mkdir(checkpoint_dir)
    print("Create directory: " + checkpoint_dir)
    
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Class / Functions
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""

def image_augmentation( list_file ):

    list_picture = []
    icount = 0
    
    for image in list_file:
    
        g = tf.random.Generator.from_seed(1234)
        g.reset_from_seed( 1235 + icount )
        temp = tf.random.uniform( shape=(4, 1), minval=0, maxval=6, dtype=tf.dtypes.int64,seed=1235 + icount,name="random" )
        arg = tf.math.argmax( temp ).numpy()[0]
        result = temp[arg].numpy()[0]
        icount = icount + 1

        if result % 6 == 0 :
            layer = tf.keras.layers.RandomZoom(.5, .2)
            image = layer( image ).numpy()
            list_picture.append( image )
        elif result % 5 == 0 :
            image = tf.image.random_hue(image, 0.2).numpy()
            image = tf.image.random_flip_up_down(image, 1).numpy()
            list_picture.append( image )
        elif result % 4 == 0 :
            image = tf.image.random_saturation(image, 5, 10, 1).numpy()
            image = tf.image.random_flip_left_right(image, 1).numpy()
            list_picture.append( image )
        elif result % 3 == 0 :
            image = tf.image.random_flip_up_down(image, 1).numpy()
            image = tf.image.random_saturation(image, 5, 10, 1).numpy()
            list_picture.append( image )
        elif result % 2 == 0 :
            image = tf.image.random_flip_left_right(image, 1).numpy()
            image = tf.image.random_hue(image, 0.2).numpy()
            list_picture.append( image )
        else :
            list_picture.append( image )

    return list_picture

list_file = image_augmentation( list_file )
    
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: DataSet
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
dataset = tf.data.Dataset.from_tensor_slices((tf.constant(tf.cast(list_file, dtype=tf.int64), shape=(10, 1, 32, 32, 3), dtype=tf.int64),tf.constant(list_label, shape=(10, 1, 1), dtype=tf.int64)))

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Callback
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
class custom_callback(tf.keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs={}):
        # if( logs['loss'] <= 0.2 ):
            # self.model.stop_training = True
        if( logs['accuracy'] >= 0.95 ):
            self.model.stop_training = True
    
custom_callback = custom_callback()

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Initialize
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
model = tf.keras.models.Sequential([
    tf.keras.layers.InputLayer(input_shape=( 32, 32, 3 )),
    tf.keras.layers.Normalization(mean=3., variance=2.),
    tf.keras.layers.Normalization(mean=4., variance=6.),
    tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
    tf.keras.layers.MaxPooling2D((2, 2)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Reshape((128, 225)),
    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(96, return_sequences=True, return_state=False)),
    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(96)),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(192, activation='relu'),
    tf.keras.layers.Dense(10),
])

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Optimizer
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
optimizer = tf.keras.optimizers.Nadam(
    learning_rate=0.00001, beta_1=0.9, beta_2=0.999, epsilon=1e-07,
    name='Nadam'
)

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Loss Fn
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""                               
lossfn = tf.keras.losses.SparseCategoricalCrossentropy(
    from_logits=False,
    reduction=tf.keras.losses.Reduction.AUTO,
    name='sparse_categorical_crossentropy'
)

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Summary
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
model.compile(optimizer=optimizer, loss=lossfn, metrics=['accuracy'])

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: FileWriter
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
if exists(checkpoint_path) :
    model.load_weights(checkpoint_path)
    print("model load: " + checkpoint_path)
    input("Press Any Key!")

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Training
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
history = model.fit( dataset, batch_size=100, epochs=10000, callbacks=[custom_callback] )
model.save_weights(checkpoint_path)

plt.figure(figsize=(5,2))
plt.title("Actors recognitions")
for i in range(len(list_file)):
    img = tf.keras.preprocessing.image.array_to_img(
        list_file[i],
        data_format=None,
        scale=True
    )
    img_array = tf.keras.preprocessing.image.img_to_array(img)
    img_array = tf.expand_dims(img_array, 0)
    predictions = model.predict(img_array)
    score = tf.nn.softmax(predictions[0])
    plt.subplot(5, 2, i + 1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(list_file_actual[i])
    plt.xlabel(str(round(score[tf.math.argmax(score).numpy()].numpy(), 2)) + ":" +  str(list_label_actual[tf.math.argmax(score)]))
    
plt.show()

input('...')

结果:

10/10 [==============================] - 0s 27ms/step - loss: 0.4422 - accuracy: 0.9000
Epoch 21/10000
10/10 [==============================] - 0s 26ms/step - loss: 0.4113 - accuracy: 0.9000
Epoch 22/10000
10/10 [==============================] - 0s 26ms/step - loss: 0.3804 - accuracy: 0.9000
Epoch 23/10000
10/10 [==============================] - 0s 26ms/step - loss: 0.3461 - accuracy: 1.0000

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多