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

查看:1949
本文介绍了TypeError:"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.

要解决此问题,您可以按照一个答案的建议将文件重命名,也可以只在模式"目录下创建一个子文件夹. (即"patterndir").因此,现在您的图像文件位于... \ patterns \ patterndir

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 ...\patterns\patterndir

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,
)

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

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