2017-03-27 64 views
1

我正在使用一個Alexnet,用5個班級的花朵數據進行調整。現在,我想預測使用微調模型。下面顯示的是主代碼。張量流佔位符錯誤。

import os 
import numpy as np 
import tensorflow as tf 
from datetime import datetime 
from alexnet_flower import AlexNet 
from datagenerator import ImageDataGenerator 

from scipy.misc import imread 
from scipy.misc import imresize 
import time 
import matplotlib.image as mpimg 
from scipy.ndimage import filters 
import urllib 
from numpy import random 
from numpy import * 
import os 
from pylab import * 
import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.cbook as cbook 

from tensorflow.core.protobuf import saver_pb2 

im1 = (imread("one.png")[:,:,:3]).astype(float32) 
#print(im1.shape()) 
im1 = im1 - mean(im1) 
#im1 = imresize(im1,[227,227,3]) 
im1[:, :, 0], im1[:, :, 2] = im1[:, :, 2], im1[:, :, 0] 

im2 = (imread("two.png")[:,:,:3]).astype(float32) 
im2 = im2 - mean(im2) 
#im2 = imresize(im2,[227,227,3]) 
im2[:, :, 0], im2[:, :, 2] = im2[:, :, 2], im2[:, :, 0] 

""" 
Configuration settings 
""" 

print(im1.shape) 
num_classes = 5 
x = tf.placeholder(tf.float32, [2, 227, 227, 3]) 
#y = tf.placeholder(tf.float32, [None, num_classes]) 
keep_prob = tf.placeholder(tf.float32) 

#print(x) 

# Initialize model 
model = AlexNet(x,keep_prob,num_classes) 

# Link variable to model output 
score = model.fc8 

saver = tf.train.Saver(write_version = saver_pb2.SaverDef.V1) 

#x1 = tf.placeholder(tf.float32, (None,) + xdim) 

with tf.Session() as sess: 

    # Initialize all variables 
    sess.run(tf.global_variables_initializer()) 

    # Add the model graph to TensorBoard 

    # Load the pretrained weights into the non-trainable layer 
    saver.restore(sess,"/home/saurabh/deep_learning/tests/finetune_alexnet_with_tensorflow/model_epoch1.ckpt") 
# x1:[im1,im2] 
    print('error!!!!!!') 

    output = sess.run(score, feed_dict = {x:[im1,im2]}) 

我使用此代碼的代碼爲alexnet。我認爲alexnet代碼沒有問題,因爲我使用此代碼進行了微調。

最後我得到了這個錯誤。我試了很多來調試它,我無法理解這個問題。謝謝你的幫助。

Traceback (most recent call last): 
    File "/home/saurabh/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1021, in _do_call 
    return fn(*args) 
    File "/home/saurabh/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1003, in _run_fn 
    status, run_metadata) 
    File "/home/saurabh/anaconda3/lib/python3.6/contextlib.py", line 89, in __exit__ 
    next(self.gen) 
    File "/home/saurabh/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 469, in raise_exception_on_not_ok_status 
    pywrap_tensorflow.TF_GetCode(status)) 
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_1' with dtype float 
    [[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]] 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "finetune_prediction_flowers.py", line 81, in <module> 
    output = sess.run(score, feed_dict = {x:[im1,im2]}) 
    File "/home/saurabh/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 766, in run 
    run_metadata_ptr) 
    File "/home/saurabh/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 964, in _run 
    feed_dict_string, options, run_metadata) 
    File "/home/saurabh/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1014, in _do_run 
    target_list, options, run_metadata) 
    File "/home/saurabh/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1034, in _do_call 
    raise type(e)(node_def, op, message) 
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_1' with dtype float 
    [[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]] 

Caused by op 'Placeholder_1', defined at: 
    File "finetune_prediction_flowers.py", line 56, in <module> 
    keep_prob = tf.placeholder(tf.float32) 
    File "/home/saurabh/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 1587, in placeholder 
    name=name) 
    File "/home/saurabh/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 2043, in _placeholder 
    name=name) 
    File "/home/saurabh/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 759, in apply_op 
    op_def=op_def) 
    File "/home/saurabh/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2240, in create_op 
    original_op=self._default_original_op, op_def=op_def) 
    File "/home/saurabh/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1128, in __init__ 
    self._traceback = _extract_stack() 

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_1' with dtype float 
    [[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]] 

回答

0

的錯誤信息是非常明確的:

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_1' with dtype float 
    [[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]] 

含義:你必須養活值的佔位符keep_prob,因爲您在您的來電sess.run,這又需要keep_prob有要求score一個值。因此,只要保概率設置爲你想要的(在這裏例如0.8)的值:

with tf.Session() as sess:  
    # Load the pretrained weights into the non-trainable layer 
    saver.restore(sess,"/home/saurabh/deep_learning/tests/finetune_alexnet_with_tensorflow/model_epoch1.ckpt")  
    output = sess.run(score, feed_dict = {x:[im1,im2], keep_prob: 0.8}) 

順便說一句:如果你是從檢查點恢復,沒有必要調用sess.run(tf.global_variables_initializer())

+0

謝謝!至少錯誤消失了。你能告訴我這個概率分數用於什麼嗎?此外,我應該在代碼中做出什麼樣的改變,以獲得特定圖像的每個類的概率值而不是分數,因爲之前我使用相同的代碼來獲得概率值。提前致謝! – talos1904

+0

我得到了我必須做出的變化來獲得概率。你能告訴我爲什麼在這裏給出概率值,它有什麼用處?謝謝! – talos1904

+0

我不知道'AlexNet',但通常保持概率是指退出正則化。在退出期間,一個單位保持(即不變),概率爲「keep_prob」,並以概率「1 - keep_prob」下降。通常,退出只在訓練期間使用,因此在測試時考慮禁用退出,即將'keep_prob'設置爲1.0。但是,你將不得不檢查'AlexNet'的實現,以確保這個參數指向丟失正則化。 – kaufmanu