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

我设计了一个用于分类道路和障碍物的图像分割网络。我希望冻结这个模型并将其作为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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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