Up image_label_ds 作成: 2021-04-25
更新: 2021-04-30


    ここまでの経過:
      $ source \[venv のパス\]/venv/bin/activate (venv) $ python >>> import tensorflow as tf >>> tf.enable_eager_execution() >>> TRAIN_DATA_URL = \ 'https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz' >>> data_root_orig = \ tf.keras.utils.get_file(origin=TRAIN_DATA_URL, fname='flower_photos', untar=True) >>> import pathlib >>> data_root = pathlib.Path(data_root_orig) >>> all_image_paths = list(data_root.glob('*/*')) >>> all_image_paths = [str(path) for path in all_image_paths] >>> label_names = \ ... sorted(item.name for item in data_root.glob('*/') if item.is_dir()) >>> label_to_index = \ ... dict((name, index) for index,name in enumerate(label_names)) >>> all_image_labels = \ ... [label_to_index[pathlib.Path(path).parent.name] for path in all_image_paths] >>> def raw_to_tensor(path): ... image_raw = tf.io.read_file(path) ... return tf.image.decode_image(image_raw, channels=3) ... >>> def load_and_preprocess_image(path): ... img_tensor = raw_to_tensor(path) ... img_final = tf.image.resize(img_tensor, [192, 192]) ... img_final /= 255.0 ... return image ...


  1. path dataset
      >>> path_ds = \ ... tf.data.Dataset.from_tensor_slices(all_image_paths) >>> print(path_ds) <DatasetV1Adapter shapes: (), types: tf.string>

  2. label dataset
      >>> label_ds = \ ... tf.data.Dataset.from_tensor_slices(tf.cast(all_image_labels, tf.int64)) >>> for label in label_ds.take(10): ... print(label_names[label.numpy()]) ... dandelion sunflowers roses dandelion sunflowers daisy sunflowers roses sunflowers daisy

  3. image dataset i
      >>> AUTOTUNE = tf.data.experimental.AUTOTUNE >>> image_ds = \ ... path_ds.map(load_and_preprocess_image, num_parallel_calls=AUTOTUNE) WARNING:tensorflow: AutoGraph could not transform and will run it as-is. Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: Unable to locate the source code of . Note that functions defined in certain environments, like the interactive Python shell do not expose their source code. If that is the case, you should to define them in a .py source file. If you are certain the code is graph-compatible, wrap the call using @tf.autograph.do_not_convert. Original error: could not get source code

    image_ds の画像を表示:
      >>> import matplotlib.pyplot as plt >>> plt.figure(figsize=(8,8)) <Figure size 800x800 with 0 Axes> >>> for n,image in enumerate(image_ds.take(4)): ... plt.subplot(2,2,n+1) ... plt.imshow(image) ... plt.grid(False) ... plt.xticks([]) ... plt.yticks([]) ... plt.xlabel(caption_image(all_image_paths[n])) ... plt.show() ... <AxesSubplot:> <matplotlib.image.AxesImage object at 0x617a4b70> ([], []) ([], []) Text(0.5, 0, 'Image (CC BY 2.0) by Taylor Hand')
      (画像のウィンドウが開く)


      ウィンドウを閉じる
      <AxesSubplot:> <matplotlib.image.AxesImage object at 0x5054b190> ([], []) ([], []) Text(0.5, 0, 'Image (CC BY 2.0) by Andrea_44')
      (画像のウィンドウが開く)


      ウィンドウを閉じる
      <AxesSubplot:> <matplotlib.image.AxesImage object at 0x505776b0> ([], []) ([], []) Text(0.5, 0, 'Image (CC BY 2.0) by u2tryololo')
      (画像のウィンドウが開く)


      ウィンドウを閉じる
      <AxesSubplot:> <matplotlib.image.AxesImage object at 0x4fc97c10> ([], []) ([], []) Text(0.5, 0, 'Image (CC BY 2.0) by AJ Batac')
      (画像のウィンドウが開く)


      ウィンドウを閉じる


  4. (image, label) dataset

    image_ds と label_ds は,データの順番が同じ。
    よって,zip することで (image, label) というペアのデータセットができる:
      >>> image_label_ds \ ... = tf.data.Dataset.zip((image_ds, label_ds)) >>> print(image_label_ds) <DatasetV1Adapter shapes: ((192, 192, 3), ()), types: (tf.float32, tf.int64)>