ここまでの経過:
$ 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)
>>> def change_range(image,label):
... return 2*image-1, label
>>> keras_ds = ds.map(change_range)
モデルは,MobileNet を入力層とする:
>>> mobile_net = tf.keras.applications.MobileNetV2(input_shape=(192, 192, 3), include_top=False)
Downloading data from https://github.com/JonathanCMitchell/
mobilenet_v2_keras/releases/download/v1.1/
mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_192_no_top.h5
9412608/9406464 [==============================] - 3s 0us/step
Traceback (most recent call last):
File "", line 1, in
File "/home/pi/venv/lib/python3.7/site-packages/
tensorflow_core/python/keras/applications/__init__.py", line 49, in wrapper
return base_fun(*args, **kwargs)
File "/home/pi/venv/lib/python3.7/site-packages/
tensorflow_core/python/keras/applications/mobilenet_v2.py", line 32, in MobileNetV2
return mobilenet_v2.MobileNetV2(*args, **kwargs)
File "/home/pi/venv/lib/python3.7/site-packages/
keras_applications/mobilenet_v2.py", line 418, in MobileNetV2
model.load_weights(weights_path)
File "/home/pi/venv/lib/python3.7/site-packages/
tensorflow_core/python/keras/engine/training.py", line 224, in load_weights
return super(Model, self).load_weights(filepath, by_name, skip_mismatch)
File "/home/pi/venv/lib/python3.7/site-packages/
tensorflow_core/python/keras/engine/network.py", line 1200, in load_weights
saving.load_weights_from_hdf5_group(f, self.layers)
File "/home/pi/venv/lib/python3.7/site-packages/
tensorflow_core/python/keras/saving/hdf5_format.py", line 651, in load_weights_from_hdf5_group
original_keras_version = f.attrs['keras_version'].decode('utf8')
AttributeError: 'str' object has no attribute 'decode'
mobile_net は,CNN モデル。
上のログは,ダウンロードした mobile_net を読み込もうとしたとき,エラーが発生したことを示す。
>>> mobile_net.trainable=False
Traceback (most recent call last):
File "", line 1, in
NameError: name 'mobile_net' is not defined
モデルの読み込みがエラー発生によりできないことは,既に見てきている:
かくして,「tf.data を使って画像をロードする」をなぞれるのは,ここまで。
備考
「tf.data を使って画像をロードする」に書かれている進行を,記しておく:
>>> mobile_net.trainable=False
Downloading data from https://storage.googleapis.com/
tensorflow/keras-applications/mobilenet_v2/
mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_192_no_top.h5
9412608/9406464 [==============================] - 1s 0us/step
>>> model = tf.keras.Sequential([
... mobile_net,
... tf.keras.layers.GlobalAveragePooling2D(),
... tf.keras.layers.Dense(len(label_names))])
...
モデルの入出力テスト:
>>> image_batch, label_batch = next(iter(keras_ds))
>>> feature_map_batch = mobile_net(image_batch)
>>> print(feature_map_batch.shape)
(32, 6, 6, 1280)
>>> logit_batch = model(image_batch).numpy()
>>> print("min logit:", logit_batch.min())
min logit: -2.0340266
>>> print("max logit:", logit_batch.max())
max logit: 3.3145566
>>> print("Shape:", logit_batch.shape)
Shape: (32, 5)
モデルのコンパイル
>>> imodel.compile(optimizer=tf.keras.optimizers.Adam(),
... loss='sparse_categorical_crossentropy',
... metrics=["accuracy"])
モデルの内容:
>>> model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
mobilenetv2_1.00_192 (Functi (None, 6, 6, 1280) 2257984
_________________________________________________________________
global_average_pooling2d (Gl (None, 1280) 0
_________________________________________________________________
dense (Dense) (None, 5) 6405
=================================================================
Total params: 2,264,389
Trainable params: 6,405
Non-trainable params: 2,257,984
_________________________________________________________________
|