1 回答

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超4個贊
看起來這都是由于嘗試凍結(jié) TensorFlow 2.3 模型而引起的。顯然,Tensorflow 2.0+ 已棄用“凍結(jié)”概念,轉(zhuǎn)而采用“保存模型”概念。一旦發(fā)現(xiàn)這一點(diǎn),我就能夠立即將 h5/json 保存到已保存的模型 pb 中。
我仍然不確定這種格式是否針對推理進(jìn)行了優(yōu)化,所以我將對此進(jìn)行一些跟進(jìn),但由于我的問題是關(guān)于我看到的錯誤,我想我會發(fā)布導(dǎo)致問題的原因。
作為參考,這是我的 python 腳本,用于將 keras h5/json 文件轉(zhuǎn)換為 Tensorflow 保存的模型格式。
import os
from keras.models import model_from_json
import tensorflow as tf
import genericpath
from genericpath import *
def splitext(p):
p = os.fspath(p)
if isinstance(p, bytes):
sep = b'/'
extsep = b'.'
else:
sep = '/'
extsep = '.'
return genericpath._splitext(p, sep, None, extsep)
def load_model(path,custom_objects={},verbose=0):
from keras.models import model_from_json
path = splitext(path)[0]
with open('%s.json' % path,'r') as json_file:
model_json = json_file.read()
model = model_from_json(model_json, custom_objects=custom_objects)
model.load_weights('%s.h5' % path)
# if verbose: print 'Loaded from %s' % path
return model
json_file = "model.json" # the h5 file should be "model.h5"
model = load_model(json_file) # load the json/h5 pair
model.save('my_saved_model') # this is a directory name to store the saved model
添加回答
舉報(bào)