perdict fail when use prediction_utils.py
when I use prediction_utils.py to predict one picture , the resulte always same value , no mater which images I slect, and my code is this, why?
def predict_proba(graph, ops, X, run): """Predict probabilities with a fitted model.
Arguments:
graph: A Tensorflow graph.
ops: A dict of ops of the graph.
X: A numpy array of shape [n_samples, image_size, image_size, 3]
and of type 'float32'.
run: An integer that determines a folder where a fitted model
is saved.
Returns:
predictions: A numpy array of shape [n_samples, n_classes]
and of type 'float32'.
"""
sess = tf.Session(graph=graph)
ops['saver'].restore(sess, os.path.join('saved', 'run' + str(run) + '/model'))
feed_dict = {'inputs/X:0': X, 'control/is_training:0': False}
predictions = sess.run(ops['predictions'], feed_dict)
aa=predictions.tolist()
bb = aa[0]
index = bb.index(max(bb))
print('+++++++++++++++\n',predictions,'\n++++++++++++',predictions.shape)
print('+++++++++++++++\n',index,'\n++++++++++++',predictions.shape)
sess.close()
return predictions
if name == 'main':
graph, ops = get_shufflenet(
FLAGS.initial_lr, FLAGS.weight_decay,
FLAGS.groups, FLAGS.dropout,
FLAGS.complexity_scale_factor
)
image = Image.open('/home/coolpad/juzhitao/shufflenet/tiny-imagenet-300/training/n01443537/val_68.JPEG')
image1 = Image.open('/home/coolpad/juzhitao/shufflenet/tiny-imagenet-300/training/n01644900/val_294.JPEG')
image2 = Image.open('/home/coolpad/juzhitao/shufflenet/tiny-imagenet-300/training/n01629819/val_23.JPEG')
image3 = Image.open('/home/coolpad/juzhitao/shufflenet/tiny-imagenet-300/training/n01641577/val_130.JPEG')
image4 = Image.open('/home/coolpad/juzhitao/shufflenet/tiny-imagenet-300/training/n12267677/n01629819_3.JPEG')
array = np.asarray(image, dtype='uint8')
array1 = np.asarray(image1, dtype='uint8')
array2 = np.asarray(image2, dtype='uint8')
array3 = np.asarray(image3, dtype='uint8')
array4 = np.asarray(image4, dtype='uint8')
X=[array]
predict_proba(graph, ops, X, 0)
X must be a numpy array of
shape [n_samples, image_size, image_size, 3]
and of type 'float32'.
X = np.expand_dims(array.astype('float32'), 0)
I use X = np.expand_dims(array.astype('float32'), 0), but the result is same:
code: image = Image.open("/home/coolpad/juzhitao/shufflenet/tiny-imagenet-200/val/images/val_%d.JPEG"%i) print("/home/coolpad/juzhitao/shufflenet/tiny-imagenet-200/val/images/val_%d.JPEG"%i) image = image.resize((56, 56), Image.ANTIALIAS) array = np.asarray(image, dtype=np.float32) X = np.expand_dims(array.astype('float32'), 0) print(X.shape) predict_proba(graph, ops, X, 0) log: /home/coolpad/juzhitao/shufflenet/tiny-imagenet-200/val/images/val_8.JPEG (1, 56, 56, 3) 2017-11-14 09:13:13.840019: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1060 6GB, pci bus id: 0000:01:00.0, compute capability: 6.1) +++++++++++++++ class: 134 ++++++++++++ (1, 200)
should I need change image file val_8.JPEG to TFRecordDataset first?
No.
So, your problem is that for any picture your model predicts the same class? Are the probabilities also the same?
yes, I input picture val_0.jpg to val_9.jpg, the predictions is same class 134,probabilities is also same ,about 0.9998
Hi I change code from feed_dict = {'inputs/X:0': X, 'control/is_training:0': False} to feed_dict = {'inputs/X:0': X}, and log is different: it will not same class when input different pictures, but probabilities seems to low
/home/coolpad/juzhitao/shufflenet/tiny-imagenet-200/val/images/val_2.JPEG (1, 56, 56, 3) +++++++++++++++ class is: 102 probabilities is: 0.01933763548731804 ++++++++++++ /home/coolpad/juzhitao/shufflenet/tiny-imagenet-200/val/images/val_3.JPEG (1, 56, 56, 3) +++++++++++++++ class is: 189 probabilities is: 0.017242174595594406 ++++++++++++ /home/coolpad/juzhitao/shufflenet/tiny-imagenet-200/val/images/val_4.JPEG (1, 56, 56, 3) +++++++++++++++ class is: 37 probabilities is: 0.016667740419507027 ++++++++++++ /home/coolpad/juzhitao/shufflenet/tiny-imagenet-200/val/images/val_5.JPEG (1, 56, 56, 3) +++++++++++++++ class is: 102 probabilities is: 0.016243750229477882 ++++++++++++ /home/coolpad/juzhitao/shufflenet/tiny-imagenet-200/val/images/val_6.JPEG (1, 56, 56, 3) +++++++++++++++ class is: 71 probabilities is: 0.02110777422785759 ++++++++++++ /home/coolpad/juzhitao/shufflenet/tiny-imagenet-200/val/images/val_7.JPEG (1, 56, 56, 3) +++++++++++++++ class is: 146 probabilities is: 0.016122067347168922 ++++++++++++ /home/coolpad/juzhitao/shufflenet/tiny-imagenet-200/val/images/val_8.JPEG (1, 56, 56, 3) +++++++++++++++ class is: 131 probabilities is: 0.015028638765215874 ++++++++++++
but if I input same class pictures, it will predict to different class if use feed_dict = {'inputs/X:0': X}
if {'inputs/X:0': X} can not be recogenize in _get_data()? in this function it is only can read train_file and val_file, both are tfrecords format, if input a ori JPEG picture, the output data in _get_data function is can be used in model?
If you don't set control/is_training:0': False then dropout will be working.
And predictions will be slightly different each time.
Ok, it seems that there is a possibility of a bug somewhere in my code. Thank you for reporting your problems. I will try to reproduce your problems. Did you change any parameters in the model?
No, I am not change any parameters, I only try to test to predict one picture
if set "control/is_training:0" , it load data from val_file in _get_data?
Hi do you find the reason?please share to me
Hi. I tested my code again and it works fine.
For an example of prediction see make_prediction.ipynb .
Maybe the problem was that you have to normalize the image to [0, 1] range before feeding it to the network.