我无法使用tf.keras.estimator.model_to_estimator
转换Keras的模型以使其在tensorflow中运行。
这是keras中的模型代码
from tensorflow.python.keras.applications.inception_v3 import InceptionV3
from tensorflow.python.keras._impl.keras.models import Model, load_model
from tensorflow.python.keras._impl.keras.layers import Input, Convolution2D, MaxPooling2D, Activation,GlobalAveragePooling2D,\
Dropout, Dropout, Flatten, Dense, Lambda, BatchNormalization
def measure_accuracy()
...
def dense_block()
...
gen_tr= ...
gen_vl= ...
input_shape = (config.IMAGE_SHAPE[0], config.IMAGE_SHAPE[1], 3)
input_tensor = Input(input_shape, name='input_tensor')
base_model = InceptionV3(input_tensor=input_tensor,weights='imagenet', include_top=False)
x = base_model.output
x = Flatten()(x)
x = dense_block(x, num_fv)
embedding = Lambda(lambda x: K.l2_normalize(x, axis=1))(x)
inputs = input_tensor
outputs = embedding
triplet_model = Model(inputs, outputs)
triplet_model.compile(loss=triplet_loss,metrics=[measure_accuracy], optimizer=Adam(0.0001))
triplet_model.fit_generator(gen_tr,
validation_data = gen_vl,
epochs=50,
verbose=2,
workers=1,
steps_per_epoch=32,
validation_steps=25)
该模型似乎在Keras中完美运行,但这里的重点是我想使用tf.dataset
作为数据管道,因此我将模型转换为tensorflow。
这是代码
model_dir = os.path.join(os.getcwd(), "models/triplet_5")
os.makedirs(model_dir, exist_ok=True)
tf_model = tf.keras.estimator.model_to_estimator(keras_model=triplet_model, model_dir=model_dir)
这是错误
FailedPreconditionError Traceback (most recent call last)
~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
1326 try:
-> 1327 return fn(*args)
1328 except errors.OpError as e:
~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
1311 return self._call_tf_sessionrun(
-> 1312 options, feed_dict, fetch_list, target_list, run_metadata)
1313
~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
1419 self._session, options, feed_dict, fetch_list, target_list,
-> 1420 status, run_metadata)
1421
~/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
515 compat.as_text(c_api.TF_Message(self.status.status)),
--> 516 c_api.TF_GetCode(self.status.status))
517 # Delete the underlying status object from memory otherwise it stays alive
FailedPreconditionError: Attempting to use uninitialized value conv2d_29/kernel
[[Node: conv2d_29/kernel/_626 = _Send[T=DT_FLOAT, client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_1366_conv2d_29/kernel", _device="/job:localhost/replica:0/task:0/device:GPU:0"](conv2d_29/kernel)]]
[[Node: batch_normalization_41/beta/_235 = _Recv[_start_time=0, client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=
这是我第一次在这里提问,所以如果我错过了你需要的一些信息,请询问