Tensorflow 问题 tensorflow.python.framework.errors_impl.InvalidArgumentError: Inpu

大米饭盖不住四喜丸子 2016-12-21 07:15:18
加精
报错如下:
tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 134400 values, but the requested shape requires a multiple of 1152
[[Node: Reshape_1 = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/gpu:0"](MaxPool_2, Reshape_1/shape)]]
Caused by op u'Reshape_1', defined at:
File "/usr/lib/wingide5/bin/wingdb.py", line 10, in <module>
"""
File "/usr/lib/wingide5/bin/wingdb.py", line 747, in main
def main():
File "/usr/lib/wingide5/bin/wingdb.py", line 656, in DebugFile
def DebugFile(netserver, server, filename, err, fs_encoding, sys_path=None):


[color=#993366][size=24px]代码如下:
#coding:utf-8
import string,os,sys
import numpy as np
import matplotlib.pyplot as plt
import scipy.io
import random
import csv
import tensorflow as tf
import pandas as pd


filename = "/home/yc/prid_2011_test/ptest.csv"

data = pd.read_csv(filename,dtype='a')
label = np.array(data['pid'])
img_data = np.array(data['pixels'])
N_sample = label.size
Face_data = np.zeros((N_sample, 128*64))
Face_label=np.zeros((N_sample,40),dtype=int)
for i in range(N_sample):
x = img_data[i]

x = np.fromstring(x, dtype=float, sep=' ')

x_max = x.max()
x = x/(x_max+0.0001)
Face_data[i] = x
Face_label[i, label[i]] = 1
#img_x = np.reshape(x, (128, 64))
#plt.subplot(10,10,i+1)
#plt.axis('off')
#plt.imshow(img_x, plt.cm.gray)
#plt.show()
train_num = 4000
test_num = 2000

train_x = Face_data [0:train_num, :]
train_y = Face_label [0:train_num, :]

test_x = Face_data [train_num : train_num+test_num, :]
test_y = Face_label [train_num : train_num+test_num, :]

print ("All is well")

batch_size = 50
train_batch_num = train_num/batch_size
test_batch_num = test_num/batch_size
train_epoch = 100

learning_rate = 0.001
# Network Parameters
n_input = 8192 # data input (img shape: 48*48)
n_classes = 40 # total classes
dropout = 0.5 # Dropout, probability to keep units
7
# tf Graph input

x = tf.placeholder(tf.float32, [None, n_input])
y = tf.placeholder(tf.float32, [None, n_classes])
keep_prob = tf.placeholder(tf.float32) #dropout (keep probability)

# Create some wrappers for simplicity

def conv2d(x, W, b, strides=1):
# Conv2D wrapper, with bias and relu activation
x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='VALID')
x = tf.nn.bias_add(x, b)
return tf.nn.relu(x)

def maxpool2d(x, k=2):
# MaxPool2D wrapper
return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1],
padding='VALID')

# Create model
def conv_net(x, weights, biases, dropout):
# Reshape input picture
print 'begin net'
print 'x1',x
x = tf.reshape(x, shape=[-1, 128, 64, 1])
print x
# Convolution Layer
conv1 = conv2d(x, weights['wc1'], biases['bc1'])
# Max Pooling (down-sampling)
conv1 = maxpool2d(conv1, k=2)

# Convolution Layer
conv2 = conv2d(conv1, weights['wc2'], biases['bc2'])
# Max Pooling (down-sampling)
conv2 = maxpool2d(conv2, k=2)

# Convolution Layer
conv3 = conv2d(conv2, weights['wc3'], biases['bc3'])
# Max Pooling (down-sampling)
conv3 = maxpool2d(conv3, k=2)

# Fully connected layer
# Reshape conv2 output to fit fully connected layer input
fc1 = tf.reshape(conv3, [-1, weights['wd1'].get_shape().as_list()[0]])
fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1'])
fc1 = tf.nn.relu(fc1)

# Apply Dropout
fc1 = tf.nn.dropout(fc1, dropout)

# Output, class prediction
out = tf.add(tf.matmul(fc1, weights['out']), biases['out'])

return out

# Store layers weight & bias
weights = {
# 3x3 conv, 1 input, 128 outputs
'wc1': tf.Variable(tf.random_normal([3, 3, 1, 128])),
# 3x3 conv, 128 inputs, 64 outputs
'wc2': tf.Variable(tf.random_normal([3, 3, 128, 64])),
# 3x3 conv, 64 inputs, 32 outputs
'wc3': tf.Variable(tf.random_normal([3, 3, 64, 32])),
# fully connected,
'wd1': tf.Variable(tf.random_normal([6*6*32, 200])),
# 1024 inputs, 10 outputs (class prediction)
'out': tf.Variable(tf.random_normal([200, n_classes]))
}


biases = {
'bc1': tf.Variable(tf.random_normal([128])),

'bc2': tf.Variable(tf.random_normal([64])),

'bc3': tf.Variable(tf.random_normal([32])),

'bd1': tf.Variable(tf.random_normal([200])),

'out': tf.Variable(tf.random_normal([n_classes]))
}

# Construct model
pred = conv_net(x, weights, biases, keep_prob)

# Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
print 'optimizer',optimizer
# Evaluate model
correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

# Initializing the variables
init = tf.initialize_all_variables()

Train_ind = np.arange(train_num)
Test_ind = np.arange(test_num)

with tf.Session() as sess:
print 'begin session'
sess.run(init)
print 'session--ok'
for epoch in range(0, train_epoch):

Total_test_loss = 0
Total_test_acc = 0

for train_batch in range (0, train_batch_num):
print 'train---sample',train_batch
sample_ind = Train_ind[train_batch * batch_size:(train_batch + 1) * batch_size]

batch_x = train_x[sample_ind, :]
print batch_x

batch_y = train_y[sample_ind, :]
print batch_y
print 'session run begin'
# Run optimization op (backprop)
print ts
sess.run(optimizer, feed_dict={x: batch_x, y: batch_y,
keep_prob: dropout})
print 'session run end 01'
if train_batch % batch_size == 0:
# Calculate loss and accuracy

loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x,
y: batch_y,
keep_prob: 1.})

print("Epoch: " + str(epoch+1) + ", Batch: "+ str(train_batch) + ", Loss= " + \
"{:.3f}".format(loss) + ", Training Accuracy= " + \
"{:.3f}".format(acc))

# Calculate test loss and test accuracy
for test_batch in range (0, test_batch_num):
sample_ind = Test_ind[test_batch * batch_size:(test_batch + 1) * batch_size]
batch_x = test_x[sample_ind, :]
batch_y = test_y[sample_ind, :]
test_loss, test_acc = sess.run([cost, accuracy], feed_dict={x: batch_x,
y: batch_y,
keep_prob: 1.})
Total_test_lost = Total_test_loss + test_loss
Total_test_acc =Total_test_acc + test_acc



Total_test_acc = Total_test_acc/test_batch_num
Total_test_loss =Total_test_lost/test_batch_num

print("Epoch: " + str(epoch + 1) + ", Test Loss= " + \
"{:.3f}".format(Total_test_loss) + ", Test Accuracy= " + \
"{:.3f}".format(Total_test_acc))


plt.subplot(2,1,1)
plt.ylabel('Test loss')
plt.plot(Total_test_loss, 'r')
plt.subplot(2,1,2)
plt.ylabel('Test Accuracy')
plt.plot(Total_test_acc, 'r')


plt.show()
print "All is well"
[/size]
[/color]
...全文
29879 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sweetyIT 2017-12-26
  • 打赏
  • 举报
回复
请问楼主怎么解决的呀 我也遇到这个问题啦
LeoXu1990 2017-10-17
  • 打赏
  • 举报
回复
训练集和测试集划分的不够好, 数据太少很难处好的结果. 可以考虑做预处理增加数据量. 一般训练:测试 8:2 或者交叉验证法.
qq_23254633 2017-09-08
  • 打赏
  • 举报
回复
I have meet this question, can you tell me how to solve the problem.my QQ or wechat is 1612617365, thank you very much.
  • 打赏
  • 举报
回复
[/code] 修改后可以运行,损失函数不收敛,还有待解决
  • 打赏
  • 举报
回复
想训练自己的cnn模型,输入是64×128的图片,4000张训练,2000张测试,
shiter 2016-12-21
  • 打赏
  • 举报
回复
能说说业务逻辑么,这个不太会,推荐一下

4,445

社区成员

发帖
与我相关
我的任务
社区描述
图形图像/机器视觉
社区管理员
  • 机器视觉
  • 迪菲赫尔曼
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧