【发布时间】:2018-01-07 15:11:40
【问题描述】:
我正在尝试创建一个之前在 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)
我将其从原始分辨率压缩为 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
【问题讨论】:
标签: python-3.x tensorflow conv-neural-network image-recognition