New to TensorFlow and trying to add new dataset (oxford_flowers102) to TensorFlow, but unsuccess

Viewed 137

I am trying to follow the sample of image classification of TensorFlow (https://www.tensorflow.org/tutorials/images/classification), and want to replace the dataset with that of oxford_flowers102 (https://www.tensorflow.org/datasets/catalog/oxford_flowers102).

I tried to change the codes a few times, but the image_count printed stays 0 and it is so strange.

import pathlib
data_dir = tf.keras.utils.get_file(origin='https://www.robots.ox.ac.uk/~vgg/data/flowers/102/102flowers.tgz',fname='102flowers', untar=True)
data_dir = pathlib.Path(data_dir)
image_count = len(list(data_dir.glob('*.jpg')))
print(image_count)

and

import pathlib
dataset_url = "https://www.robots.ox.ac.uk/~vgg/data/flowers/102/102segmentations.tgz"
data_dir = tf.keras.utils.get_file('102segmentations', origin=dataset_url, untar=True, extract=True)
data_dir = pathlib.Path(data_dir)
image_count = len(list(data_dir.glob('*/*jpg')))
print(image_count)

I also try to compare the two files with !wget

!wget https://www.robots.ox.ac.uk/~vgg/data/flowers/102/102flowers.tgz

and

!wget https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz

They come out with different formats, [application/x-gzip]and [application/x-compressed-tar] respectively, is this matter? Or what did I do wrongly?

Also, I have no idea how can I start with the Oxford dataset, it seems already finished all testing. Can anyone help? Thank you!

1 Answers

As this is Tensorflow built-in dataset, you can import this dataset as below:

!pip install tensorflow-datasets
import tensorflow as tf
import tensorflow_datasets as tfds

(train_ds, val_ds, test_ds), metadata = tfds.load(
    'oxford_flowers102',
    split=['train[:80%]', 'train[80%:90%]', 'train[90%:]'],
    with_info=True,
    as_supervised=True,
)

OR

ds = tfds.load('oxford_flowers102', split='train')
ds = ds.take(1)  # Only take a single example

for example in ds:  # example is `{'image': tf.Tensor, 'label': tf.Tensor}`
  print(list(example.keys()))
  image = example["image"]
  label = example["label"]
  print(image.shape, label)

There are several different ways available here to fetch ready-to-use datasets.

Related