如何获取/定义图形的输入输出名称或以适当的形式冻结它

编程入门 行业动态 更新时间:2024-10-25 02:23:05
本文介绍了如何获取/定义图形的输入输出名称或以适当的形式冻结它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好,我们一直试图将模型保存为.bytes格式,以便我们可以在c#脚本中使用它.我们正在使用tensorflow 1.7.0这是我们的模型:

Hi everyone we have been trying to save our model in .bytes format so that we can use it in a c# script. We are using tensorflow 1.7.0 Here is our model:

bsize=16 # define cnn model def define_model(): # load model model = VGG16(include_top=False, input_shape=(224, 224, 3)) # mark loaded layers as not trainable for layer in model.layers: layer.trainable = False # add new classifier layers flat1 = Flatten()(model.layers[-1].output) class1 = Dense(128, activation='relu', kernel_initializer='he_uniform')(flat1) output = Dense(2, activation='softmax')(class1) # define new model model = Model(inputs=model.inputs, outputs=output) # compile model # compile model opt = Adam(lr=0.001) modelpile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy']) return model

培训:

sess=tf.Session() tf.global_variables_initializer().run(session=sess) model = define_model() # create data generator datagen = ImageDataGenerator(rescale=1.0/255.0) # prepare iterators train_it = datagen.flow_from_directory(mpath+'/train', class_mode='categorical', batch_size=bsize, target_size=(224, 224)) test_it = datagen.flow_from_directory(mpath+'/test', class_mode='categorical', batch_size=bsize, target_size=(224, 224)) # fit model history = model.fit_generator(train_it, steps_per_epoch=len(train_it), validation_data=test_it, validation_steps=len(test_it), epochs=1, verbose=0) # evaluate model _, acc = model.evaluate_generator(test_it, steps=len(test_it), verbose=0) print('> %.3f' % (acc * 100.0)) model.save_weights("weights.h5")

冻结:

K.clear_session() K.set_learning_phase(0) model = define_model() model.load_weights("weights.h5") save_dir = "./out" tf.saved_model.simple_save(K.get_session(), save_dir, inputs={"input": model.inputs[0]}, outputs={"output": model.outputs[0]}) freeze_graph.freeze_graph(None, None, None, None, model.outputs[0].op.name, None, None, os.path.join(save_dir, "frozen_model.bytes"), False, "", input_saved_model_dir=save_dir)

然后我们尝试使用以下命令查看输入和输出名称:

Then we try to see input and output names using:

model.inputs[0].name model.ouputs[0].name

最后,我们希望在c#中将此图形用作:

Finally we would like to use this graph in c# as:

.AddInput(graph["input_1_1:0"][0], tensor).Fetch(graph["output"][0]);

但是,从错误中我们可以理解,输入和输出名称是错误的.而且,只要我们打电话

However, as we understand from the error input and output names are wrong. Moreover whenever we call

model.inputs[0].name model.ouputs[0].name

即使我们将输出名称定义为name ="output"

they print different input and output names even if we define output name as name="output"

您对如何冻结此模型,获取输入和输出名称等有任何建议

Do you have any suggestions about how to freeze this model, obtain input and output names etc

致谢

推荐答案

对于有类似问题的人:

model.inputs[0].name model.ouputs[0].name

这样的收益:

input_1:0 dense_2/Softmax:0

但是您不应该在.AddInput中直接使用它们,而不是:

but you shouldnt use them directly in .AddInput so instead of:

.AddInput(graph["input_1:0"][0], tensor).Fetch(graph[dense_2/Softmax:0][0]);

这样做:

.AddInput(graph["input_1"][0], tensor).Fetch(graph[dense_2/Softmax][0]);

因此,上面的代码实际上是有效的,只是省略了名称字符串中的:0部分,仅此而已:D

So the code above actually works just omit the :0 part from the name strings and thats all :D

更多推荐

如何获取/定义图形的输入输出名称或以适当的形式冻结它

本文发布于:2023-11-16 21:13:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1607478.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:或以   输入输出   图形   定义   形式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!