Up データの内容 作成: 2021-04-24
更新: 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 ...


    画像キャプションの取り出し:
      情報は,~/.keras/datasets/flower_photos/LICENSE.txt にある: $ head ~/.keras/datasets/flower_photos/LICENSE.txt All images in this archive are licensed under the Creative Commons By-Attribution License, available at: https://creativecommons.org/licenses/by/2.0/ The photographers are listed below, thanks to all of them for making their work available, and please be sure to credit them for any use as per the license. daisy/7568630428_8cf0fc16ff_n.jpg CC-BY by A Guy Taking Pictures - https://www.flickr.com/photos/80901381@N04/7568630428/ daisy/7410356270_9dff4d0e2e_n.jpg CC-BY by martinak15 - https://www.flickr.com/photos/martinaphotography/7410356270/ daisy/4286053334_a75541f20b_m.jpg CC-BY by jenny downing - https://www.flickr.com/photos/jenny-pics/4286053334/ daisy/10770585085_4742b9dac3_n.jpg CC-BY by Thangaraj Kumaravel - https://www.flickr.com/photos/kumaravel/10770585085/ daisy/8759177308_951790e00d_m.jpg CC-BY by Marina del Castell - https://www.flickr.com/photos/marinadelcastell/8759177308/ daisy/4131565290_0585c4dd5a_n.jpg CC-BY by jenny downing - https://www.flickr.com/photos/jenny-pics/4131565290/

      画像ファイル名に対しこれのキャプションを抜き出す関数 caption_image を作成: >>> attributions = (data_root/"LICENSE.txt").open(encoding='utf-8').readlines()[4:] >>> attributions = [line.split(' CC-BY') for line in attributions] >>> attributions = dict(attributions) >>> def caption_image(image_path): ... image_rel = pathlib.Path(image_path).relative_to(data_root) ... return "Image (CC BY 2.0) " + ' - '.join(attributions[str(image_rel)].split(' - ')[:-1]) ... >>>

      表示: >>> for n in range(3): ... image_path = random.choice(all_image_paths) ... print(image_path) ... print(caption_image(image_path)) ... /home/pi/.keras/datasets/flower_photos/sunflowers/7012366081_019c8a17a4_m.jpg Image (CC BY 2.0) by Priscilla Jordão /home/pi/.keras/datasets/flower_photos/roses/2183357362_4b4da4b6b5.jpg Image (CC BY 2.0) by KaCey97078 /home/pi/.keras/datasets/flower_photos/tulips/16644790896_7b296ecd67_n.jpg Image (CC BY 2.0) by T.Kiya >>>


    関数 load_and_preprocess_image による元画像の変換の様子:
      >>> img_raw_path = all_image_paths[0] >>> img_raw_label = all_image_labels[0] >>> img_tensor = raw_to_tensor(img_raw_path) >>> img_final = load_and_preprocess_image( img_raw_path ) >>> import matplotlib.pyplot as plt >>> plt.imshow(img_tensor) <matplotlib.image.AxesImage object at 0x526132f0> >>> plt.show() 画像のウィンドウが開く
      ウィンドウを閉じて,pythonプロンプトに戻る >>> plt.imshow(img_final) <matplotlib.image.AxesImage object at 0x52465b10> >>> plt.grid(False) >>> plt.xlabel(caption_image(img_raw_label)) Text(0.5, 0, 'Image (CC BY 2.0) by liz west') >>> plt.title(label_names[label].title()) Text(0.5, 1.0, 'Roses') >>> plt.show() 画像のウィンドウが開く
      ウィンドウを閉じて,pythonプロンプトに戻る >>>


    チュートリアルでは,以下の流れで画像を見るようになっている。
    しかし,少なくとも python インタラクティブシェルの環境では,画像表示とはならない:


    [画像の random choice]
      >>> import random >>> random.shuffle(all_image_paths) >>> for n in range(3): ... random.choice(all_image_paths) ... '/home/pi/.keras/datasets/flower_photos/sunflowers/9302733302_2cb92cf275.jpg' '/home/pi/.keras/datasets/flower_photos/daisy/2573240560_ff7ffdd449.jpg' '/home/pi/.keras/datasets/flower_photos/roses/18760363474_a707331322_n.jpg' >>>

    [画像の表示]
      >>> import IPython.display as display >>> for n in range(3): ... image_path = random.choice(all_image_paths) ... display.display(display.Image(image_path)) ... print() ... <IPython.core.display.Image object> <IPython.core.display.Image object> <IPython.core.display.Image object> >>>

     参考: stackoverflow.com/questions : "image does not display in ipython" より:
    It appears that the code example I am working through is specifically designed to use IPython Notebook which is not the same as IPython, or interactive Python.
    To use the code above, which is from the Google TensorFlow tutorial, one needs to configure an "IPython Notebook Server" and run something called "Jupyter".