使用TensorFlow进行Cifar数据集实验,使用官方提供的cifar.py下载数据报错:module ‘cifar10’ has no attribute ‘maybe_download_and_extract’
这段时间学习TensorFlow的使用,按照官方教程进行Cifar数据集上的实验。看到有代码cifar10.maybe_download_and_extract()用来下载数据,但是实际运行时报错cifar.py中没有该函数。
在cifar10.py中找到这样一行函数:
images, labels = cifar10_input.distorted_inputs(batch_size=FLAGS.batch_size)
然后在cifar10_input.py中找到函数disorted_inputs:
return _get_images_labels(batch_size, tfds.Split.TRAIN, distords=True)
再跳转:
def _get_images_labels(batch_size, split, distords=False):
"""Returns Dataset for given split."""
dataset = tfds.load(name='cifar10', split=split)
scope = 'data_augmentation' if distords else 'input'
with tf.name_scope(scope):
dataset = dataset.map(DataPreprocessor(distords), num_parallel_calls=10)
# Dataset is small enough to be fully loaded on memory:
dataset = dataset.prefetch(-1)
dataset = dataset.repeat().batch(batch_size)
iterator = dataset.make_one_shot_iterator()
images_labels = iterator.get_next()
images, labels = images_labels['input'], images_labels['target']
tf.summary.image('images', images)
return images, labels
其中第一句不就正是数据的加载吗,也就是说在这个代码中使用了tfds.load加载了cifar数据集。
后来查资料知道tfds是tensorflow官方新给出的工具包tensorflow_datasets,用这个包可以轻松的下载很多数据集。之后我也打算学习一下这个包的使用。
因此,在使用cifar.py搭建模型,训练数据时不必在专门下载数据,其代码中已经内置好了下载。