2016-11-04 17 views
15

Tôi mới sử dụng TensorFlow và máy học. Tôi đang cố gắng để phân loại hai đối tượng một tách và một pendrive (hình ảnh jpeg). Tôi đã đào tạo và xuất khẩu một model.ckpt thành công. Bây giờ tôi đang cố gắng khôi phục model.ckpt đã lưu để dự đoán. Đây là kịch bản:TensorFlow ValueError: Không thể nạp giá trị hình dạng (64, 64, 3) cho Tensor u'Placeholder: 0 ', có hình dạng' (?, 64, 64, 3) '

import tensorflow as tf 
import math 
import numpy as np 
from PIL import Image 
from numpy import array 


# image parameters 
IMAGE_SIZE = 64 
IMAGE_CHANNELS = 3 
NUM_CLASSES = 2 

def main(): 
    image = np.zeros((64, 64, 3)) 
    img = Image.open('./IMG_0849.JPG') 

    img = img.resize((64, 64)) 
    image = array(img).reshape(64,64,3) 

    k = int(math.ceil(IMAGE_SIZE/2.0/2.0/2.0/2.0)) 
    # Store weights for our convolution and fully-connected layers 
    with tf.name_scope('weights'): 
     weights = { 
      # 5x5 conv, 3 input channel, 32 outputs each 
      'wc1': tf.Variable(tf.random_normal([5, 5, 1 * IMAGE_CHANNELS, 32])), 
      # 5x5 conv, 32 inputs, 64 outputs 
      'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])), 
      # 5x5 conv, 64 inputs, 128 outputs 
      'wc3': tf.Variable(tf.random_normal([5, 5, 64, 128])), 
      # 5x5 conv, 128 inputs, 256 outputs 
      'wc4': tf.Variable(tf.random_normal([5, 5, 128, 256])), 
      # fully connected, k * k * 256 inputs, 1024 outputs 
      'wd1': tf.Variable(tf.random_normal([k * k * 256, 1024])), 
      # 1024 inputs, 2 class labels (prediction) 
      'out': tf.Variable(tf.random_normal([1024, NUM_CLASSES])) 
     } 

    # Store biases for our convolution and fully-connected layers 
    with tf.name_scope('biases'): 
     biases = { 
      'bc1': tf.Variable(tf.random_normal([32])), 
      'bc2': tf.Variable(tf.random_normal([64])), 
      'bc3': tf.Variable(tf.random_normal([128])), 
      'bc4': tf.Variable(tf.random_normal([256])), 
      'bd1': tf.Variable(tf.random_normal([1024])), 
      'out': tf.Variable(tf.random_normal([NUM_CLASSES])) 
     } 

    saver = tf.train.Saver() 
    with tf.Session() as sess: 
     saver.restore(sess, "./model.ckpt") 
     print "...Model Loaded..." 
     x_ = tf.placeholder(tf.float32, shape=[None, IMAGE_SIZE , IMAGE_SIZE , IMAGE_CHANNELS]) 
     y_ = tf.placeholder(tf.float32, shape=[None, NUM_CLASSES]) 
     keep_prob = tf.placeholder(tf.float32) 

     init = tf.initialize_all_variables() 

     sess.run(init) 
     my_classification = sess.run(tf.argmax(y_, 1), feed_dict={x_:image}) 
     print 'Neural Network predicted', my_classification[0], "for your image" 


if __name__ == '__main__': 
    main() 

Khi tôi chạy kịch bản trên cho dự đoán tôi nhận được lỗi sau:

ValueError: Cannot feed value of shape (64, 64, 3) for Tensor u'Placeholder:0', which has shape '(?, 64, 64, 3)' 

Tôi đang làm gì sai? Và làm thế nào để tôi sửa hình dạng của mảng numpy?

Trả lời

20

image có hình dạng (64,64,3).

Trình giữ chỗ nhập liệu của bạn _x có hình dạng (?, 64,64,3).

Vấn đề là bạn đang cho người giữ chỗ ăn một giá trị có hình dạng khác.

Bạn phải cấp dữ liệu với giá trị (1, 64, 64, 3) = một loạt 1 hình ảnh.

Chỉ cần định lại lại giá trị image của bạn thành một lô có kích thước một.

image = array(img).reshape(1, 64,64,3) 

Tái bút: thực tế là giữ chỗ đầu vào chấp nhận một loạt các hình ảnh, có nghĩa là bạn có thể chạy predicions cho một loạt hình ảnh song song. Bạn có thể thử đọc nhiều hơn 1 hình ảnh (N hình ảnh) và xây dựng một loạt hình ảnh N, sử dụng một tensor có hình dạng (N, 64,64,3)

+1

Có thể bạn có nghĩa là 'image = array (img) .reshape (1, 64, 64 , 3) '. –

+0

Có lẽ bạn nên sử dụng 'np.expand_dims (img, axis = 0)' để thêm kích thước lô – powder

+0

Cảm ơn bạn. image = array (img) .reshape (1, 64, 64, 3) công việc này – Pragyan93

Các vấn đề liên quan