冻结模型会降低输出精度

Freezing Model drops Output Accuracy

我有一个图像分割网络,旨在对道路和障碍物进行分类。我想冻结模型并将其用作 API。所以我使用默认的 TensorFlow 工具来冻结模型。冻结后,网络给出的输出完全不准确。

这是一个示例。

输入图像

使用检查点文件测试时的输出

冻结模型后的输出

我曾尝试使用不同版本的 tensorflow 进行冻结,但这并没有帮助。由于网络在针对检查点进行测试时表现异常,我认为问题出在冻结模型脚本中。网络使用Batch_normalisation。这可能是造成这种下降的原因,因为我看到了几个与类似性质的问题相关的问题吗?我怎样才能避免这种情况?

Here's全网

使用检查点文件进行预测

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_out

with 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)