我是Python和Tensorflow的新手 . 我想实现一种简单的CNN,这是我迄今为止所做的:
import tensorflow as tf
import numpy as np
from libs import utils
import cv2
import glob
from tensorflow.python.framework.ops import reset_default_graph
reset_default_graph()
# We first get the graph that we used to compute the network
g = tf.get_default_graph()
# And can inspect everything inside of it
[op.name for op in g.get_operations()]
X = tf.placeholder(tf.float32, [None,720000])
Y = tf.placeholder(tf.int32, [None])
X_data = []
files = glob.glob ("C:/Users/Maede/Desktop/Master Thesis/imlearning/*.jpg")
for myFile in files:
print(myFile)
image = cv2.imread (myFile)
X_data.append (image)
print('X_data shape:', np.array(X_data).shape)
data=np.array(X_data)
data=np.reshape(data,(30,720000))
label=np.array([(0,1),(1,0),(0,1),(1,0),(0,1),(1,0),(0,1),(1,0),(0,1),(1,0),
(0,1),(1,0),(0,1),(1,0),(0,1),(1,0),(0,1),(1,0),(0,1),(1,0),
(0,1),(1,0),(0,1),(1,0),(0,1),(1,0),(0,1),(1,0),(0,1),(1,0)])
###########################################################
train_batch_size = 2
def random_batch():
num_images = 30
idx = np.random.choice(num_images,
size=train_batch_size,
replace=False)
x_batch = data[idx,:]
y_batch = label[idx, :]
return x_batch, y_batch
######################
#
X_tensor = tf.reshape(X, [-1, 400,600,3])
filter_size = 5
n_filters_in = 3
n_filters_out = 32
W_1 = tf.get_variable(
name='W',
shape=[filter_size, filter_size, n_filters_in, n_filters_out],
initializer=tf.random_normal_initializer())
b_1 = tf.get_variable(
name='b',
shape=[n_filters_out],
initializer=tf.constant_initializer())
h_1 = tf.nn.relu(
tf.nn.bias_add(
tf.nn.conv2d(input=X_tensor,
filter=W_1,
strides=[1, 2, 2, 1],
padding='SAME'),
b_1))
n_filters_in = 32
n_filters_out = 64
n_output = 2
W_2 = tf.get_variable(
name='W2',
shape=[filter_size, filter_size, n_filters_in, n_filters_out],
initializer=tf.random_normal_initializer())
b_2 = tf.get_variable(
name='b2',
shape=[n_filters_out],
initializer=tf.constant_initializer())
h_2 = tf.nn.relu(
tf.nn.bias_add(
tf.nn.conv2d(input=h_1,
filter=W_2,
strides=[1, 2, 2, 1],
padding='SAME'),
b_2))
# We'll now reshape so we can connect to a fully-connected/linear layer:
h_2_flat = tf.reshape(h_2, [-1, 100*150* n_filters_out])
# NOTE: This uses a slightly different version of the linear function than the lecture!
h_3, W = utils.linear(h_2_flat, 400, activation=tf.nn.relu, name='fc_1')
# NOTE: This uses a slightly different version of the linear function than the lecture!
Y_pred, W = utils.linear(h_3, n_output, activation=tf.nn.softmax, name='fc_2')
y_one_hot = tf.one_hot( Y , 2 )
cross_entropy = -tf.reduce_sum(y_one_hot * tf.log(Y_pred + 1e-12))
optimizer = tf.train.AdamOptimizer().minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(Y_pred, 1), tf.argmax(y_one_hot, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))
sess = tf.Session()
sess.run(tf.global_variables_initializer())
batch_size = 2
n_epochs = 5
for epoch_i in range(n_epochs):
for batch_xs, batch_ys in random_batch():
sess.run(optimizer, feed_dict={
X: np.array(batch_xs).reshape([1,720000]),
Y: batch_ys
})
valid = data #### DATA haie validation
print(sess.run(accuracy,
feed_dict={
X: data,
Y: label
}))
输入是30张图像,尺寸为400 * 600 * 3,我想将它们分为两类 . 问题是当我使用此命令时: X: np.array(batch_xs).reshape([1,720000]),
错误如下所示:ValueError:无法将大小为2的数组重塑为形状(1,720000)
当我使用时:
X: batch_xs
错误是:
ValueError:无法为Tensor'占位符:0'提供形状值(720000,),其形状为'(?,720000)'
我很困惑什么是batch_xs维度,为什么它在不同情况下会发生变化 .
1 回答
np.array(batch_xs)
与您的图片尺寸不同 .for batch_xs, batch_ys in random_batch()
也是一种稍微奇怪的运行代码的方式,我想这也会导致你的问题 . 您通常使用for
来迭代某个迭代 .在你的情况下,iterable就是你的函数返回的东西,一个带有
batch_xs, batch_ys
的元组 . 但是在同一步骤中,元素的第一个(!)值变为两个变量batch_xs
和batch_ys
.replace=False
在您的情况下不执行任何操作,因为您只调用函数random_batch()
一次 . 在下一次迭代中,它将再次拥有完整的数据集 .以下是您案例的简单示例:
这是Python的基础知识,要熟悉它,请查找
tuple unpacking
,iterable
和for loops
.用这个替换内部for循环,它应该工作 . 它可能不是你所期望的,但它是你应该做的代码 .
如果你想用100批次进行训练就做这样的事情
通常,您尝试删除与问题无关的代码,以便更容易找到问题 . 查找尽可能少的代码,仍然显示您的问题 . 您的问题与tensorflow无关,因此您可以删除与tensorflow相关的所有内容,以便于查找 . 您的问题与numpy和数组形状有关 .