bks*_*shi 8 python google-colaboratory tensorflow2.0
一点背景知识,我正在加载大约 60,000 张图像以使用 colab 来训练 GAN。我已经将它们上传到 Drive 并且目录结构包含root. 我将它们加载到 colab 如下:
root = "drive/My Drive/data/images"
root = pathlib.Path(root)
list_ds = tf.data.Dataset.list_files(str(root/'*/*'))
for f in list_ds.take(3):
print(f.numpy())
Run Code Online (Sandbox Code Playgroud)
这给出了输出:
b'drive/My Drive/data/images/folder_1/2994.jpg'
b'drive/My Drive/data/images/folder_1/6628.jpg'
b'drive/My Drive/data/images/folder_2/37872.jpg'
Run Code Online (Sandbox Code Playgroud)
我进一步处理它们如下:
def process_path(file_path):
label = tf.strings.split(file_path, '/')[-2]
image = tf.io.read_file(file_path)
image = tf.image.decode_jpeg(image)
image = tf.image.convert_image_dtype(image, tf.float32)
return image#, label
ds = list_ds.map(process_path)
BUFFER_SIZE = 60000
BATCH_SIZE = 128
train_dataset = ds.shuffle(BUFFER_SIZE).batch(BATCH_SIZE)
Run Code Online (Sandbox Code Playgroud)
每个图像的大小128x128。现在遇到问题,当我尝试在 colab 中查看批处理时,执行将永远持续下去,永不停止,例如,使用以下代码:
for batch in train_dataset.take(4):
print([arr.numpy() for arr in batch])
Run Code Online (Sandbox Code Playgroud)
早些时候我认为 batch_size 可能是一个问题,所以尝试更改它但仍然是同样的问题。当我加载大量文件时,这可能是由于 colab 导致的问题吗?
或者由于使用 MNIST(28x28) 时图像的大小?如果是这样,可能的解决方案是什么?
提前致谢。
编辑:删除 shuffle 语句后,最后一行会在几秒钟内执行。所以我认为由于 BUFFER_SIZE 的 shuffle 可能是一个问题,但即使减少了 BUFFER_SIZE,它也需要很长时间才能执行。任何解决方法?
以下是我如何从我的个人 Google Drive 加载 1.12GB 压缩的 FLICKR 图像数据集。首先,我在 colab 环境中解压缩数据集。一些可以加快性能的功能是prefetch和autotune。此外,我使用本地 colab 缓存来存储处理后的图像。第一次执行大约需要 20 秒(假设您已解压缩数据集)。然后,缓存允许后续调用非常快速地加载。
假设您已授权 Google Drive API,我首先解压缩文件夹
!unzip /content/drive/My\ Drive/Flickr8k
!unzip Flickr8k_Dataset
!ls
Run Code Online (Sandbox Code Playgroud)
然后我使用了您的代码并添加了prefetch()、autotune和cache file。
import pathlib
import tensorflow as tf
def prepare_for_training(ds, cache, BUFFER_SIZE, BATCH_SIZE):
if cache:
if isinstance(cache, str):
ds = ds.cache(cache)
else:
ds = ds.cache()
ds = ds.shuffle(buffer_size=BUFFER_SIZE)
ds = ds.batch(BATCH_SIZE)
ds = ds.prefetch(buffer_size=AUTOTUNE)
return ds
AUTOTUNE = tf.data.experimental.AUTOTUNE
root = "Flicker8k_Dataset"
root = pathlib.Path(root)
list_ds = tf.data.Dataset.list_files(str(root/'**'))
for f in list_ds.take(3):
print(f.numpy())
def process_path(file_path):
label = tf.strings.split(file_path, '/')[-2]
img = tf.io.read_file(file_path)
img = tf.image.decode_jpeg(img)
img = tf.image.convert_image_dtype(img, tf.float32)
# resize the image to the desired size.
img = tf.image.resize(img, [128, 128])
return img#, label
ds = list_ds.map(process_path, num_parallel_calls=AUTOTUNE)
train_dataset = prepare_for_training(ds, cache="./custom_ds.tfcache", BUFFER_SIZE=600000, BATCH_SIZE=128)
for batch in train_dataset.take(4):
print([arr.numpy() for arr in batch])
Run Code Online (Sandbox Code Playgroud)
这是使用 keras 执行此操作的一种方法flow_from_directory()。这种方法的好处是可以避免张量流shuffle(),根据缓冲区大小,张量流可能需要处理整个数据集。Keras 为您提供了一个迭代器,您可以调用它来获取批量数据,并且内置了随机洗牌功能。
import pathlib
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
root = "Flicker8k_Dataset"
BATCH_SIZE=128
train_datagen = ImageDataGenerator(
rescale=1./255 )
train_generator = train_datagen.flow_from_directory(
directory = root, # This is the source directory for training images
target_size=(128, 128), # All images will be resized
batch_size=BATCH_SIZE,
shuffle=True,
seed=42, #for the shuffle
classes=[''])
i = 4
for batch in range(i):
[print(x[0]) for x in next(train_generator)]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1587 次 |
| 最近记录: |