冻结模型导致输出精度下降

我设计了一个用于分类道路和障碍物的图像分割网络。我希望冻结这个模型并将其作为API提供服务。因此,我使用了TensorFlow的默认工具来冻结模型。冻结后,网络给出的输出完全偏离且不准确。

这是一个样本。

输入图像

enter image description here

使用检查点文件测试时的输出enter image description here

冻结模型后的输出enter image description here

我尝试使用不同版本的TensorFlow来冻结模型,但这并没有帮助。由于网络在使用检查点进行测试时表现如预期那样,我认为问题出在冻结模型的脚本上。网络使用了批量归一化(Batch_normalisation)。这是否可能是精度下降的原因,因为我看到了一些类似性质的相关问题?如何避免这种情况?

这是完整的网络

使用检查点文件的预测

with tf.Graph().as_default() as graph:    images_tensor = tf.train.string_input_producer(images_list, shuffle=False)    reader = tf.WholeFileReader()    key, image_tensor = reader.read(images_tensor)    image = tf.image.decode_png(image_tensor, channels=3)    image = preprocess(image)    images = tf.train.batch([image], batch_size = 1, allow_smaller_final_batch=True)    #Create the model inference    with slim.arg_scope(ENet_arg_scope()):        logits, probabilities = ENet(images,                                     num_classes=4,                                     batch_size=1,                                     is_training=True,                                     reuse=None,                                     num_initial_blocks=num_initial_blocks,                                     stage_two_repeat=stage_two_repeat,                                     skip_connections=skip_connections)    variables_to_restore = slim.get_variables_to_restore()    saver = tf.train.Saver(variables_to_restore)    def restore_fn(sess):        return saver.restore(sess, checkpoint)    predictions = tf.argmax(probabilities, -1)    predictions = tf.cast(predictions, tf.float32)    sv = tf.train.Supervisor(logdir=None, init_fn=restore_fn)    with sv.managed_session() as sess:        for i in xrange(int(len(images_list) / 1 + 1)):            segmentations = sess.run(predictions)            for j in xrange(segmentations.shape[0]):                converted_image = grayscale_to_colour(segmentations[j],i,j)                imsave(photo_dir + "/imagelabel_%05d_edges.png" %(i*1 + j), converted_image)

从PB文件进行预测

def predict():    start = time.time()    y_out = persistent_sess.run(y, feed_dict={x: x_in})    end = time.time()    print(end-start)    return y_outwith tf.Session() as sess:    model_filename = "frozen_model_tf_version.pb"    with gfile.FastGFile(model_filename, 'rb') as f:        graph_def = tf.GraphDef()        graph_def.ParseFromString(f.read())        tf.import_graph_def(graph_def)        g_in = tf.get_default_graph()x = g_in.get_tensor_by_name('import/batch:0')y = g_in.get_tensor_by_name('import/ENet/output:0')persistent_sess = tf.Session(graph=g_in)x_in_unaltered=cv2.imread(img)x_in_unaltered = cv2.resize(x_in_unaltered,(480,360),interpolation=cv2.INTER_CUBIC)x_in = np.expand_dims(x_in_unaltered.flatten(),axis=0)predictions=predict()print(np.unique(predictions,return_counts=True))out = np.array(predictions[0],dtype=np.float32)out = np.reshape(out, [360,480])converted_image = grayscale_to_colour(out,x_in_unaltered)cv2.imwrite("out.png",converted_image)

回答:

这里的问题与is_training有关,因为您使用了dropoutbatch_norm,在预测时is_training应当设置为False。这样您可以期望得到相同的结果。

logits, probabilities = ENet(images,                                 num_classes=4,                                 batch_size=1,                                 is_training=False,                                 reuse=None,                                 num_initial_blocks=num_initial_blocks,                                 stage_two_repeat=stage_two_repeat,                                 skip_connections=skip_connections)

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注