Up MobileNet の入力とするための変換 作成: 2021-04-26
更新: 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 ... >>> path_ds = \ ... tf.data.Dataset.from_tensor_slices(all_image_paths) >>> label_ds = \ ... tf.data.Dataset.from_tensor_slices(tf.cast(all_image_labels, tf.int64)) >>> AUTOTUNE = tf.data.experimental.AUTOTUNE >>> image_ds = \ ... path_ds.map(load_and_preprocess_image, num_parallel_calls=AUTOTUNE) >>> image_label_ds \ ... = tf.data.Dataset.zip((image_ds, label_ds)) >>> ds = image_label_ds.apply( ... tf.data.experimental.shuffle_and_repeat(buffer_size=image_count)) >>> BATCH_SIZE = 32 >>>> ds = ds.batch(BATCH_SIZE) >>>> ds = ds.prefetch(buffer_size=AUTOTUNE)


    構築するモデルは,入力層に MobileNet を用いるとする。
    その MobileNet は,入力が [-1,1] の範囲に正規化されていることを想定している。
    ds は画素値が [0, 1] の範囲なので,これを [-1,1] の範囲に変換する:
      >>> def change_range(image,label): ... return 2*image-1, label >>> keras_ds = ds.map(change_range) WARNING:tensorflow:AutoGraph could not transform <function change_range at 0x6071dae0> 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 <function change_range at 0x6071dae0>. 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 WARNING: AutoGraph could not transform <function change_range at 0x6071dae0> 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 <function change_range at 0x6071dae0>. 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