keras转tensorflow后,报错:Attempting to use uninitialized value batch_normalization_14/moving_mean

keras转tensorflow,使用tensorflow模型预测时,报错:

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value batch_normalization_14/moving_mean

[[Node: batch_normalization_14/moving_mean/read = Identity[T=DT_FLOAT, _class=["loc:@batch_normalization_14/cond/FusedBatchNorm/Switch_3"], _device="/job:localhost/replica:0/task:0/device:GPU:0"](batch_normalization_14/moving_mean)]]

[[Node: predictions/Softmax/_5 = _Recv[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_576_predictions/Softmax", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

解决方法:

在sess后,添加sess初始化:sess.run(tf.global_variables_initializer())

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

拓展:如果是keras下报类似的错,则添加如下代码:

keras.backend.get_session().run(tf.initialize_all_variables())

猜你喜欢

转载自blog.csdn.net/weixin_41770169/article/details/93191941