类型错误:“ReadFile"操作的输入“文件名"的类型为 float32,与预期的字符串类型不匹配 [英] TypeError: Input 'filename' of 'ReadFile' Op has type float32 that does not match expected type of string

查看:42
本文介绍了类型错误:“ReadFile"操作的输入“文件名"的类型为 float32,与预期的字符串类型不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在此处的教程中运行此代码:https://keras.io/examples/vision/image_classification_from_scratch/

I am running this code from the tutorial here: https://keras.io/examples/vision/image_classification_from_scratch/

使用自定义数据集,如教程中那样分为 2 个数据集.但是,我收到此错误:

with a custom dataset, that is divided in 2 datasets as in the tutorial. However, I got this error:

TypeError: Input 'filename' of 'ReadFile' Op has type float32 that does not match expected type of string.

我制作了这个演员表.我试过这个:

I made this casting. I tried this:

is_jfif = str(tf.compat.as_bytes("JFIF")) in fobj.peek(10)

但就错误而言没有任何改变我整天都在想办法解决它,但没有任何成功.有人能帮我吗?谢谢...

but nothing changed as far as the error I am trying all day to figure out how to solve it, without any success. Can someone help me? Thank you...

推荐答案

我发现的最简单的方法是创建一个子文件夹并将文件复制到该子文件夹中.即假设您的文件是 0.jpg、1.jpg、2.jpg....2000.jpg 并且位于名为patterns"的目录中.

Simplest way I found is to create a subfolder and copy the files to that subfolder. i.e. Lets assume your files are 0.jpg, 1.jpg,2.jpg....2000.jpg and in directory named "patterns".

似乎 Keras API 不接受它,因为文件以数字命名,而对于 Keras,它在 float32 中.

Seems like the Keras API does not accept it as the files are named by numbers and for Keras it is in float32.

要解决这个问题,您可以按照答案的建议重命名文件,也可以简单地在patterns"下创建一个子文件夹.(即模式目录").所以现在你的图像文件位于 ...patternspatterndir

To overcome this issue, either you can rename the files as one answer suggests, or you can simply create a subfolder under "patterns" (i.e. "patterndir"). So now your image files are under ...patternspatterndir

Keras(内部)可能使用子目录名称,并且可能将其附加在图像文件的前面,从而使其成为一个字符串(例如 patterndir_01.jpg、patterndir_02.jpg)[注意这是我的解释,并不意味着这是真的]

Keras (internally) possibly using the subdirectory name and may be attaching it in front of the image file thus making it a string (sth like patterndir_01.jpg, patterndir_02.jpg) [Note this is my interpretation, does not mean that it is true]

当你这次编译它时,你会看到它可以工作,你会得到一条编译器消息:

When you compile it this time, you will see that it works and you will get a compiler message as:

Found 2001 files belonging to 1 classes.
Using 1601 files for training.
Found 2001 files belonging to 1 classes.
Using 400 files for validation.

我的代码是这样的

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

#Generate a dataset

image_size = (28, 28)
batch_size = 32

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    "patterns",
    validation_split=0.2,
    subset="training",
    seed=1337,
    image_size=image_size,
    batch_size=batch_size,
)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
    "patterns",
    validation_split=0.2,
    subset="validation",
    seed=1337,
    image_size=image_size,
    batch_size=batch_size,
)

这篇关于类型错误:“ReadFile"操作的输入“文件名"的类型为 float32,与预期的字符串类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆