我想将keras训练的模型集成到张量流网络中以帮助训练张量流模型,即,不需要再次训练keras模型,它只是为张量流模型产生损失 . 下图是基本网络架构 .
以下代码用于生成keras模型的损失 . 但是,不幸的是,它未能加载预训练的keras模型,尽管它没有给出任何错误 . 即,代码可以工作,但它只是加载随机模型(变量的随机值),它返回一个随机值,而不是由预训练模型生成的值 .
def keras_loss(sess,prediction):
#import package
from keras.models import Model
from keras.models import Sequential
from keras.layers import Dense, Dropout,InputLayer
from keras.applications.inception_resnet_v2 import InceptionResNetV2
from keras.applications.inception_resnet_v2 import preprocess_input
from keras.preprocessing.image import load_img, img_to_array
from keras import backend as K
K.set_session(sess) #sess is the tensorflow model
K.set_learning_phase(False)
base_model = InceptionResNetV2 (input_shape=(None, None, 3), include_top=False, pooling='avg', weights=None)
x = Dropout(0.75)(base_model.output)
x = Dense(10, activation='softmax')(x)
model = Model(base_model.input, x) #load the pretrained model
model.load_weights('./inception_resnet_weights.h5')
score = model(prediction) #prediction is a tf tensor
return score
我在stackoverflow上阅读了相关的指令:1 Calling a Keras model on a TensorFlow tensor but keep weights [2] Tensorflow op in Keras model [3] Implementing a tensorflow graph into a Keras model
但它仍然无法解决我的问题 . 任何人都可以帮助我吗?提前致谢
1 回答
来自keras docs:
您想使用
weights='imagenet'
而不是None
.