来自预先训练的InceptionV3 CNN的随机结果

时间:2018-01-07 15:11:41

标签: python-3.x tensorflow conv-neural-network image-recognition

我试图创建一个以前在Imagenet上训练的InceptionV3 CNN。虽然检查点的创建和加载似乎工作正常,但结果似乎是随机的,因为每次运行脚本时,我都会得到不同的结果,即使我没有改变任何东西。网络是从头开始重新创建的,加载相同的未更改的网络并对相同的图像进行分类(根据我的理解,即使它无法确定图像实际是什么,仍然会导致相同的结果。)

我只是注意到,即使我尝试在同一个脚本执行中多次对同一图像进行分类,我也会得到随机结果。

我像这样使用

创建CNN
from tensorflow.contrib.slim.nets import inception as nn_architecture
from tensorflow.contrib import slim

with slim.arg_scope([slim.conv2d, slim.fully_connected], normalizer_fn=slim.batch_norm,
                    normalizer_params={'updates_collections': None}): ## this is a fix for an issue where the model doesn't fit the checkpoint https://github.com/tensorflow/models/issues/2977
    logits, endpoints = nn_architecture.inception_v3(input,  # input
                                                     1001, #NUM_CLASSES, #num classes
                                                     # num classes #maybe set to 0 or none to ommit logit layer and return input for logit layer instead.
                                                     True,  # is training (dropout = zero if false for eval
                                                     0.8,  # dropout keep rate
                                                     16,  # min depth
                                                     1.0,  # depth multiplayer
                                                     layers_lib.softmax,  # prediction function
                                                     True,  # spatial squeeze
                                                     tf.AUTO_REUSE,
                                                     # reuse, use get variable to get variables directly... probably
                                                     'InceptionV3')  # scope

之后我像这样加载the imagenet trained checkpoint

saver = tf.train.Saver()
saver.restore(sess, CHECKPOINT_PATH)

然后我通过对此图像car on white background

进行分类来验证它是否正常工作

我将它的原始分辨率压缩到299x299,这是网络的输入

from skimage import io

car = io.imread("data/car.jpg")
car_scaled = zoom(car, [299 / car.shape[0], 299 / car.shape[1], 1])

car_cnnable = np.array([car_scaled])

然后我尝试对图像进行分类,并尽可能打印图像所属的类别。

predictions = sess.run(logits, feed_dict={images: car_cnnable})
predictions = np.squeeze(predictions) #shape (1, 1001) to shape (1001)  

print(np.argmax(predictions))
print(predictions[np.argmax(predictions)])

该类是(或似乎是)随机的,并且可能性也有所不同。 我的最后几次处决是:

Class - likelihood 
899 - 0.98858
660 - 0.887204
734 - 0.904047
675 - 0.886952

以下是我的完整代码:https://gist.github.com/Syzygy2048/ddb8602652b547a71316ee0febfddbef

1 个答案:

答案 0 :(得分:0)

由于我将isTraining设置为true,因此每次使用网络时都会应用辍学率。我的印象是这只发生在反向传播过程中。

要使其正常工作,代码应为

logits, endpoints = nn_architecture.inception_v3(input,  # input
                                                 1001, #NUM_CLASSES, #num classes
                                                 # num classes #maybe set to 0 or none to ommit logit layer and return input for logit layer instead.
                                                 False,  # is training (dropout = zero if false for eval
                                                 0.8,  # dropout keep rate
                                                 16,  # min depth
                                                 1.0,  # depth multiplayer
                                                 layers_lib.softmax,  # prediction function
                                                 True,  # spatial squeeze
                                                 tf.AUTO_REUSE,
                                                 # reuse, use get variable to get variables directly... probably
                                                 'InceptionV3')  # scope