我正在尝试在新图像集上重新训练 inception v3。
当我尝试保存模型时,我收到一个错误。
我努力了:
tf.keras.models.save_model(model, filename)
和
model.save(filename)
和
tf.contrib.saved_model.save_keras_model(model, filename)
都给我一个类似的错误,Module has no ' name '
我附上了与问题相关的代码。
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import os
import sys
import numpy as np
import tensorflow as tf
import tensorflow_hub as hub
import matplotlib.pyplot as plt
FLAGS = None
def create_model(m, img_data):
# load feature extractor (inception_v3)
features_extractor_layer = tf.keras.layers.Lambda(m, input_shape=img_data.image_shape)
# make pre-trained layers un-trainable
features_extractor_layer.trainable = False
print(features_extractor_layer.name)
# add new activation layer to train to our classes
model = tf.keras.Sequential([
features_extractor_layer,
tf.keras.layers.Dense(img_data.num_classes, activation='softmax')
])
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
return model
def get_and_gen_images(module):
"""
get images from image directory or url
:param module: module (to get required image size info
:return: batched image data
"""
data_name = os.path.splitext(os.path.basename(FLAGS.image_dir_or_url))[0]
print("data: ", data_name)
# download images to cache if not already
if FLAGS.image_dir_or_url.startswith('https://'):
data_root = tf.keras.utils.get_file(data_name,
FLAGS.image_dir_or_url,
untar=True,
cache_dir=os.getcwd())
else: # specify directory with images
data_root = tf.keras.utils.get_file(data_name,
FLAGS.image_dir_or_url)
# get image size for specific module
image_size = hub.get_expected_image_size(module)
# TODO: this is where to add noise, rotations, shifts, etc.
image_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1. / 255, validation_split=0.2)
# create image stream
train_image_data = image_generator.flow_from_directory(str(data_root),
target_size=image_size,
batch_size=FLAGS.batch_size,
subset='training')
validation_image_data = image_generator.flow_from_directory(str(data_root),
target_size=image_size,
batch_size=FLAGS.batch_size,
subset='validation')
return train_image_data, validation_image_data
# load module (will download from url or directory_
module = hub.Module(FLAGS.tfhub_module)
# generate image stream
train_image_data, validation_image_data = get_and_gen_images(module)
model = create_model(module, train_image_data)
model.summary()
file = FLAGS.saved_model_dir + "/modelname.h5"
model.save(file)
这应该保存一个“.h5”模型文件,但我收到一个命名错误:
Traceback (most recent call last):
File "/home/raphy/projects/vmi/tf_cpu/retrain.py", line 305, in <module>
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
File "/home/raphy/.local/lib/python3.6/site-packages/tensorflow/python/platform/app.py", line 125, in run
_sys.exit(main(argv))
File "/home/raphy/projects/vmi/tf_cpu/retrain.py", line 205, in main
model.save(file)
File "/home/raphy/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/sequential.py", line 319, in save
save_model(self, filepath, overwrite, include_optimizer)
File "/home/raphy/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/saving.py", line 105, in save_model
'config': model.get_config()
File "/home/raphy/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/sequential.py", line 326, in get_config
'config': layer.get_config()
File "/home/raphy/.local/lib/python3.6/site-packages/tensorflow/python/keras/layers/core.py", line 756, in get_config
function = self.function.__name__
AttributeError: 'Module' object has no attribute '__name__'
我想以 tf_hub 模型的格式保存模型。