Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为自定义图像数据创建一个类似于mnist.train.next_batch(128)的函数_Python_Tensorflow_Tensorflow Datasets_Training Data - Fatal编程技术网

Python 为自定义图像数据创建一个类似于mnist.train.next_batch(128)的函数

Python 为自定义图像数据创建一个类似于mnist.train.next_batch(128)的函数,python,tensorflow,tensorflow-datasets,training-data,Python,Tensorflow,Tensorflow Datasets,Training Data,我正在尝试为我的图像数据集实现以下功能:batch\u x,batch\u y=mnist.train.next\u batch(128) 对于培训,我有1000个文件夹(文件夹的名称与它所属的“Class1”、“Class2”、“Class3”等类别相同)。每个文件夹包含500个属于单个类的图像 总之,我有500000张图片需要训练 对于我来说,将它们分成批次(随机选择图像)并输入推理模型的最佳方式是什么 我没有使用Keras,而是使用Tensorflow 1.15 这是我的代码: impor

我正在尝试为我的图像数据集实现以下功能:
batch\u x,batch\u y=mnist.train.next\u batch(128)

对于培训,我有1000个文件夹(文件夹的名称与它所属的“Class1”、“Class2”、“Class3”等类别相同)。每个文件夹包含500个属于单个类的图像

总之,我有500000张图片需要训练

对于我来说,将它们分成批次(随机选择图像)并输入推理模型的最佳方式是什么

我没有使用Keras,而是使用Tensorflow 1.15

这是我的代码:

import tensorflow as tf
tf.enable_eager_execution()
import numpy as np
import os
import pathlib
tf.__version__

AUTOTUNE = tf.data.experimental.AUTOTUNE
SHUFFLE_BUFFER_SIZE = 100
BATCH_SIZE = 128
IMG_WIDTH = 128
IMG_HEIGHT = 256
DATA_DIR = 'D:/PythonWorkspace/train'
DATA_DIR = pathlib.Path(DATA_DIR) #RD

def get_label(file_path):
  # convert the path to a list of path components
  parts = tf.strings.split(file_path, os.path.sep)
  # The second to last is the class-directory
  return parts[-2] == CLASS_NAMES

def decode_img(img):
  # convert the compressed string to a 3D uint8 tensor
  img = tf.image.decode_jpeg(img, channels=3)
  # Use `convert_image_dtype` to convert to floats in the [0,1] range.
  img = tf.image.convert_image_dtype(img, tf.float32)
  # resize the image to the desired size.
  return tf.image.resize(img, [IMG_WIDTH, IMG_HEIGHT])

def process_path(file_path):
  label = get_label(file_path)
  # load the raw data from the file as a string
  img = tf.io.read_file(file_path)
  img = decode_img(img)
  return img, label

#aataset = tf.data.Dataset.list_files(os.path.join(DATA_DIR,'*/*'))
dataset = tf.data.Dataset.list_files(str(DATA_DIR/'*/*')) #RD
for f in dataset.take(5):
  print(f.numpy())
dataset = dataset.map(process_path, num_parallel_calls=AUTOTUNE)
dataset = dataset.shuffle(buffer_size=SHUFFLE_BUFFER_SIZE)
dataset = dataset.repeat()
dataset = dataset.batch(BATCH_SIZE)
输出:

'1.15.2'

b'D:\\PythonWorkspace\\train\\1403\\5T04015F015.jpg'
b'D:\\PythonWorkspace\\train\\0525\\C3T0020F097.jpg'
b'D:\\PythonWorkspace\\train\\0005\\24T0060F004.jpg'
b'D:\\PythonWorkspace\\train\\1159\\45T0008F041.jpg'
b'D:\\PythonWorkspace\\train\\0425\\C5T0021F007.jpg'

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-b683dfeb641b> in <module>
     32 for f in dataset.take(5):
     33   print(f.numpy())
---> 34 dataset = dataset.map(process_path, num_parallel_calls=AUTOTUNE)
     35 dataset = dataset.shuffle(buffer_size=SHUFFLE_BUFFER_SIZE)
     36 dataset = dataset.repeat()

....
....
....
AttributeError: in converted code:

    <ipython-input-2-b683dfeb641b>:24 process_path  *
        label = get_label(file_path)
    <ipython-input-2-b683dfeb641b>:11 get_label  *
        parts = tf.strings.split(file_path, os.path.sep)
    d:\venv\lib\site-packages\tensorflow_core\python\ops\ragged\ragged_string_ops.py:642 strings_split_v1
        return ragged_result.to_sparse()

    AttributeError: 'Tensor' object has no attribute 'to_sparse'
'1.15.2'
b'D:\\PythonWorkspace\\train\\1403\\5T04015F015.jpg'
b'D:\\PythonWorkspace\\train\\0525\\C3T0020F097.jpg'
b'D:\\PythonWorkspace\\train\\0005\\24T0060F004.jpg'
b'D:\\PythonWorkspace\\train\\1159\\45T0008F041.jpg'
b'D:\\PythonWorkspace\\train\\0425\\C5T0021F007.jpg'
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
在里面
数据集中的f为32。取(5):
33打印(f.numpy())
--->34 dataset=dataset.map(进程路径,num\u并行调用=AUTOTUNE)
35数据集=数据集.shuffle(缓冲区大小=shuffle\u缓冲区大小)
36数据集=数据集。重复()
....
....
....
AttributeError:在转换的代码中:
:24进程路径*
标签=获取标签(文件路径)
:11获取标签*
parts=tf.strings.split(文件路径,os.path.sep)
d:\venv\lib\site packages\tensorflow\u core\python\ops\ragged\ragged\u string\u ops.py:642 strings\u split\u v1
将参差不齐的结果返回到\u稀疏()
AttributeError:“Tensor”对象没有“to_sparse”属性

您可以使用
tf.data
构建管道。例如,由以下人员提供:

AUTOTUNE=tf.data.experimental.AUTOTUNE
洗牌缓冲区大小=10000
批量大小=128
IMG_宽度=128
IMG_高度=128
数据目录=
def get_标签(文件路径):
#将路径转换为路径组件列表
parts=tf.strings.split(文件路径,os.path.sep)
#倒数第二个是类目录
返回部件[-2]==类名称
def解码img(img):
#将压缩字符串转换为3D uint8张量
img=tf.image.decode\u jpeg(img,通道=3)
#使用“convert\u image\u dtype”转换为[0,1]范围内的浮点数。
img=tf.image.convert\u image\u数据类型(img,tf.float32)
#将图像调整为所需大小。
返回tf.image.resize(img,[img\u宽度,img\u高度])
def进程路径(文件路径):
标签=获取标签(文件路径)
#以字符串形式从文件加载原始数据
img=tf.io.read\u文件(文件路径)
img=解码\u img(img)
返回img,标签
dataset=tf.data.dataset.list_文件(str(data_DIR/'*/*'))
dataset=dataset.map(进程路径,num\u并行调用=AUTOTUNE)
dataset=dataset.shuffle(缓冲区大小=shuffle\u缓冲区大小)
dataset=dataset.repeat()
dataset=dataset.batch(批次大小)

生成的
dataset
将返回一批批随机图像及其标签。

我使用了
DATA\u DIR='D:\PythonWorkspace\TrainImages'
,但有一个错误:
行中的/:'str'和'str'不支持的操作数类型:
dataset=tf.DATA.dataset.list\u文件(str(DATA\u DIR/'*/*'))
你能试试
dataset=tf.data.dataset.list_文件(os.path.join(data_DIR,*/*'))
?您必须先导入os
。谢谢,它解决了这个问题,但是从您发布的输出中我发现了另一个错误(如上面的问题所述),文件路径的格式似乎有问题
tf.strings.split
这里只需要一个
os.path.sep
AUTOTUNE = tf.data.experimental.AUTOTUNE
SHUFFLE_BUFFER_SIZE = 10000
BATCH_SIZE = 128
IMG_WIDTH = 128
IMG_HEIGHT = 128
DATA_DIR = <Your data dir>

def get_label(file_path):
  # convert the path to a list of path components
  parts = tf.strings.split(file_path, os.path.sep)
  # The second to last is the class-directory
  return parts[-2] == CLASS_NAMES

def decode_img(img):
  # convert the compressed string to a 3D uint8 tensor
  img = tf.image.decode_jpeg(img, channels=3)
  # Use `convert_image_dtype` to convert to floats in the [0,1] range.
  img = tf.image.convert_image_dtype(img, tf.float32)
  # resize the image to the desired size.
  return tf.image.resize(img, [IMG_WIDTH, IMG_HEIGHT])

def process_path(file_path):
  label = get_label(file_path)
  # load the raw data from the file as a string
  img = tf.io.read_file(file_path)
  img = decode_img(img)
  return img, label

dataset = tf.data.Dataset.list_files(str(DATA_DIR/'*/*'))
dataset = dataset.map(process_path, num_parallel_calls=AUTOTUNE)
dataset = dataset.shuffle(buffer_size=SHUFFLE_BUFFER_SIZE)
dataset = dataset.repeat()
dataset = dataset.batch(BATCH_SIZE)