简体   繁体   English

自定义数据生成器

[英]Custom data generator

I have a standard directory structure of train , validation , test , and each contain class subdirectories.我有一个标准的trainvalidationtest目录结构,每个目录结构都包含类子目录。

...
  |train
      |class A
          |1
              |1_1.raw
              |1_2.raw
              ...
          |2
              ...
      |class B
          ...
  |test
      ...

I want to use the flow_from_directory API, but all I can find is an ImageDataGenerator , and the files I have are raw numpy arrays (generated with arr.tofile(...) ).我想使用flow_from_directory API,但我只能找到一个ImageDataGenerator ,我拥有的文件是原始 numpy 数组(使用arr.tofile(...)生成)。

Is there an easy way to use ImageDataGenerator with a custom file loader?有没有一种简单的方法可以将ImageDataGenerator与自定义文件加载器一起使用?

I'm aware of flow_from_dataframe , but that doesn't seem to accomplish what I want either;我知道flow_from_dataframe ,但这似乎也没有完成我想要的; it's for reading images with more custom organization.它用于阅读具有更多自定义组织的图像。 I want a simple way to load raw binary files instead of having to re-encode 100,000s of files into jpgs with some precision loss along the way (and wasted time, etc.).我想要一种简单的方法来加载原始二进制文件,而不必将 100,000 个文件重新编码为 jpg,并且在此过程中会有一些精度损失(以及浪费时间等)。

Tensorflow is an entire ecosystem with IO capabilities and ImageDataGenerator is one of the least flexible approaches. Tensorflow 是一个具有 IO 功能的完整生态系统,而ImageDataGenerator最不灵活的方法之一。 Read here on How to Load Numpy Data in Tensorflow .在此处阅读如何在 Tensorflow 中加载 Numpy 数据

import tensorflow as tf
import numpy as np

DATA_URL = 'https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz'

path = tf.keras.utils.get_file('mnist.npz', DATA_URL)
with np.load(path) as data:
  train_examples = data['x_train']
  train_labels = data['y_train']
  test_examples = data['x_test']
  test_labels = data['y_test']

train_dataset = tf.data.Dataset.from_tensor_slices((train_examples, train_labels))
test_dataset = tf.data.Dataset.from_tensor_slices((test_examples, test_labels))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM