2016-11-24 6 views
0

が私を助けてください...私はは「テンソル 'プレースホルダ:0' の形状の値(50、2352)養うことができません '(?、784)'、形状をしている」sess.runエラーを失敗した

を学びますチュートリアルのエキスパートに基づく私の独自のデータを使用してテンソルフローに変換します。私のコード次

: とValueError:テンソル「プレースホルダ:0」の形状(50、2352)の値を供給することはできません

#datasets define 
NUM_CLASSES = 65535 
IMAGE_SIZE = 28 
IMAGE_PIXELS = IMAGE_SIZE*IMAGE_SIZE*1 

#read datasets 
with open(FLAGS.train, 'r') as f: # train.txt 
    train_image = [] 
    train_label = [] 
    num = 0 
    for line in f: 
     if num == 500: 
      break 
     line = line.rstrip() 
     l = line.split(',') 
     print(l[0]) 
     img = cv2.imread(l[0]) 
     img = cv2.resize(img, (IMAGE_SIZE, IMAGE_SIZE)) 
     train_image.append(img.flatten().astype(np.float32)/255.0) 
     tmp = np.zeros(NUM_CLASSES) 
     tmp[int(l[1])] = 1 
     train_label.append(tmp) 
     num += 1 
    train_image = np.asarray(train_image) 
    train_label = np.asarray(train_label) 
    train_len = len(train_image) 

def inference(images_placeholder, keep_prob): 
    def weight_variable(shape): 
     initial = tf.truncated_normal(shape, stddev=0.1) 
     return tf.Variable(initial) 
def bias_variable(shape): 
    initial = tf.constant(0.1, shape=shape) 
    return tf.Variable(initial) 
def conv2d(x, W): 
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') 
def max_pool_2x2(x): 
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], 
          strides=[1, 2, 2, 1], padding='SAME') 
x_images = tf.reshape(images_placeholder, [-1, IMAGE_SIZE, IMAGE_SIZE, 1]) 
with tf.name_scope('conv1') as scope: 
    W_conv1 = weight_variable([5, 5, 1, 32]) 
    b_conv1 = bias_variable([32]) 
    h_conv1 = tf.nn.relu(conv2d(x_images, W_conv1) + b_conv1) 
with tf.name_scope('pool1') as scope: 
    h_pool1 = max_pool_2x2(h_conv1) 
with tf.name_scope('conv2') as scope: 
    W_conv2 = weight_variable([5, 5, 32, 64]) 
    b_conv2 = bias_variable([64]) 
    h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) 
with tf.name_scope('pool2') as scope: 
    h_pool2 = max_pool_2x2(h_conv2) 
with tf.name_scope('fc1') as scope: 
    W_fc1 = weight_variable([7*7*64, 1024]) 
    b_fc1 = bias_variable([1024]) 
    h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64]) 
    h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) 
    h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) 
with tf.name_scope('fc2') as scope: 
    W_fc2 = weight_variable([1024, NUM_CLASSES]) 
    b_fc2 = bias_variable([NUM_CLASSES]) 
with tf.name_scope('softmax') as scope: 
    y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) 
return y_conv 

#learn 
with tf.Graph().as_default(): 
    images_placeholder = tf.placeholder("float", shape=(None, IMAGE_PIXELS)) 
    labels_placeholder = tf.placeholder("float", shape=(None, NUM_CLASSES)) 
    keep_prob = tf.placeholder("float") 

    logits = inference(images_placeholder, keep_prob) 
    loss_value = loss(logits, labels_placeholder) 
    train_op = training(loss_value, FLAGS.learning_rate) 
    print("train_op =", train_op) 

    acc = accuracy(logits, labels_placeholder) 

    saver = tf.train.Saver() 
    sess = tf.Session() 
    sess.run(tf.initialize_all_variables()) 
    summary_op = tf.merge_all_summaries() 
    summary_writer = tf.train.SummaryWriter(FLAGS.train_dir, sess.graph_def) 

    if train_len % FLAGS.batch_size is 0: 
     train_batch = train_len/FLAGS.batch_size 
    else: 
     train_batch = (train_len/FLAGS.batch_size)+1 
    print("train_batch = %d",str(train_batch)) 
    for step in range(FLAGS.max_steps): 
     for i in range(int(train_batch)): 
      batch = FLAGS.batch_size*i 
      batch_plus = FLAGS.batch_size*(i+1) 
      print("batch_plus =", batch_plus) 
      if batch_plus > train_len: batch_plus = train_len 
      sess.run(train_op, feed_dict={ 
        images_placeholder: train_image[batch:batch_plus], 
        labels_placeholder: train_label[batch:batch_plus], 
        keep_prob: 0.5}) 

     if step % 10 == 0: 
      train_accuracy = 0.0 
      for i in range(train_batch): 
       batch = FLAGS.batch_size*i 
       batch_plus = FLAGS.batch_size*(i+1) 
       if batch_plus > train_len: batch_plus = train_len 
       train_accuracy += sess.run(acc, feed_dict={ 
              images_placeholder: train_image[batch:batch_plus], 
              labels_placeholder: train_label[batch:batch_plus], 
              keep_prob: 1.0}) 
       if i is not 0: train_accuracy /= 2.0 
      #summary_str = sess.run(summary_op, feed_dict={ 
      # images_placeholder: train_image, 
      # labels_placeholder: train_label, 
      # keep_prob: 1.0}) 
      #summary_writer.add_summary(summary_str, step) 
      print("step %d, training accuracy %g",(step, train_accuracy)) 

if test_len % FLAGS.batch_size is 0: 
    test_batch = test_len/FLAGS.batch_size 
else: 
    test_batch = (test_len/FLAGS.batch_size)+1 
    print("test_batch = ",str(test_batch)) 
    test_accuracy = 0.0 
for i in range(test_batch): 
    batch = FLAGS.batch_size*i 
    batch_plus = FLAGS.batch_size*(i+1) 
    if batch_plus > train_len: batch_plus = train_len 
    test_accuracy += sess.run(acc, feed_dict={ 
           images_placeholder: test_image[batch:batch_plus], 
           labels_placeholder: test_label[batch:batch_plus], 
           keep_prob: 1.0}) 
    if i is not 0: test_accuracy /= 2.0 
print("test accuracy %g",(test_accuracy)) 
save_path = saver.save(sess, FLAGS.save_model) 

が、私は私にエラーを与えて、それを実行しようとすると、その形がありますか?(?、784) '

私は何か小さいものを見落としているように感じますが、私はそれを見ません。

+1

あなたの 'images_placeholder'はあなたが定義した形のために形(?、784)を持っています:' shape =(None、IMAGE_PIXELS) '。しかし、ある時点では、 'train_image [batch:batch_plus]'(あるいはおそらく 'test_image [batch:batch_plus]')には形(50,2352)があります。 – Neal

+0

通知するのに役立つ何か:2352はちょうど3回784です。 – Neal

+0

train_imageにもかかわらず、私は2352を理解していません。 – TNT

答えて

0

編集:申し訳ありません私の以前の分析を読んでも間違っていました。

2352/3 = 784、私はあなたが1ピクセルの強度の代わりに3 rgbカラーチャンネルを保持していると思う(またはresize関数はデフォルトで出力に3色のチャンネルを持つ)。

mnistの例はかなり複雑ですが、データの読み込みには表示されません。テンソルフロー/ contrib/learn/python/learn/datasets/mnist.pyを参照してください。 。彼らは4Dテンソル[index、x、y、depth]で画像を読み込み、インデックスのサブセットを取るminibatchを見ることができます。

幸運を祈る!

関連する問題