使用tf训练CNN模型进行隐写分析。遇到一些我无法理解的错误。
这里是代码:
x_image = tf.reshape(x, [-1, 64, 64, 1])
W_hpf = tf.get_variable('W_hpf', initializer = HPF_Kernel, dtype = tf.float32, regularizer = None)
b_hpf = tf.get_variable('b_hpf', shape = [1], dtype = tf.float32, initializer = tf.constant_initializer(0.))
output_hpf = tf.nn.conv2d(x_image, W_hpf, [1, 1, 1, 1], 'SAME') + b_hpf
W_conv1 = weight_variable([5, 5, 1, 8])
b_conv1 = bias_variable([8])
bn_conv1, update_ema1 = batchnorm(tf.abs(conv2d(output_hpf, W_conv1)), tst, iter, b_conv1)
h_conv1 = tf.nn.tanh(bn_conv1)
h_pool1 = avg_pool(h_conv1, [1, 5, 5, 1], [1, 2, 2, 1], "SAME")
W_conv2 = weight_variable([5, 5, 8, 16])
b_conv2 = bias_variable([16])
bn_conv2, update_ema2 = batchnorm(conv2d(h_pool1, W_conv2), tst, iter, b_conv2)
h_conv2 = tf.nn.tanh(bn_conv2)
h_pool2= avg_pool(h_conv2, [1, 5, 5, 1], [1, 2, 2, 1], "SAME")
W_conv3 = weight_variable([1, 1, 16, 32])
b_conv3 = bias_variable([32])
bn_conv3, update_ema3 = batchnorm(conv2d(h_pool2, W_conv3), tst, iter, b_conv3)
h_conv3 = tf.nn.relu(bn_conv3)
h_pool3 = avg_pool(h_conv3, [1, 5, 5, 1], [1, 2, 2, 1], "SAME")
W_conv4 = weight_variable([1, 1, 32, 64])
b_conv4 = bias_variable([64])
bn_conv4, update_ema4 = batchnorm(conv2d(h_pool3, W_conv4), tst, iter, b_conv4)
h_conv4 = tf.nn.relu(bn_conv4)
h_pool4 = avg_pool(h_conv4, [1, 5, 5, 1], [1, 2, 2, 1], "SAME")
W_conv5 = weight_variable([1, 1, 64, 128])
b_conv5 = bias_variable([128])
bn_conv5, update_ema5 = batchnorm(conv2d(h_pool4, W_conv5), tst, iter, b_conv5)
h_conv5 = tf.nn.relu(bn_conv5)
h_pool5 = avg_pool(h_conv5, [1, 5, 5, 1], [1, 2, 2, 1], "SAME")
W_fc1 = weight_variable([2 * 2 * 128, 64])
b_fc1 = bias_variable([64])
h_pool5_flat = tf.reshape(h_pool5, [-1, 2*2*128])
h_fc1 = tf.nn.relu(tf.matmul(h_pool5_flat, W_fc1) + b_fc1)
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
W_fc2 = weight_variable([64, 2])
b_fc2 = bias_variable([2])
y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
然后我得到这样的错误:
Traceback (most recent call last):
File "steg_low.py", line 147, in <module>
x:_images, y_: _labels, keep_prob: 1.0, iter: i, tst: False})
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 570, in eval
return _eval_using_default_session(self, feed_dict, self.graph, session)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 4455, in _eval_using_default_session
return session.run(tensors, feed_dict)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 889, in run
run_metadata_ptr)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1120, in _run
feed_dict_tensor, options, run_metadata)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1317, in _do_run
options, run_metadata)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1336, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [192] vs. [3]
[[Node: Equal = Equal[T=DT_INT64, _device="/job:localhost/replica:0/task:0/device:GPU:0"](ArgMax, ArgMax_1)]]
[[Node: Mean_1/_55 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_314_Mean_1", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
Caused by op u'Equal', defined at:
File "steg_low.py", line 133, in <module>
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_, 1))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_math_ops.py", line 1476, in equal
"Equal", x=x, y=y, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2956, in create_op
op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1470, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
InvalidArgumentError (see above for traceback): Incompatible shapes: [192] vs. [3]
[[Node: Equal = Equal[T=DT_INT64, _device="/job:localhost/replica:0/task:0/device:GPU:0"](ArgMax, ArgMax_1)]]
[[Node: Mean_1/_55 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_314_Mean_1", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
我的标签是单热编码的,但我不知道为什么批量大小变成了192。
这里是所有张量或变量的大小:
x: Tensor("Placeholder_2:0", shape=(?, 512, 512, 1), dtype=float32)
keep_prob: Tensor("Placeholder_4:0", dtype=float32)
tst: Tensor("Placeholder:0", dtype=bool)
bn_conv1: Tensor("batchnorm/add_1:0", shape=(?, 64, 64, 8), dtype=float32)
bn_conv2: Tensor("batchnorm_1/add_1:0", shape=(?, 32, 32, 16), dtype=float32)
bn_conv3: Tensor("batchnorm_2/add_1:0", shape=(?, 16, 16, 32), dtype=float32)
bn_conv4: Tensor("batchnorm_3/add_1:0", shape=(?, 8, 8, 64), dtype=float32)
bn_conv5: Tensor("batchnorm_4/add_1:0", shape=(?, 4, 4, 128), dtype=float32)
x_image: Tensor("Reshape:0", shape=(?, 64, 64, 1), dtype=float32)
W_hpf: <tf.Variable 'W_hpf:0' shape=(5, 5, 1, 1) dtype=float32_ref>
b_hpf: <tf.Variable 'b_hpf:0' shape=(1,) dtype=float32_ref>
output_hpf: Tensor("add:0", shape=(?, 64, 64, 1), dtype=float32)
W_conv1: <tf.Variable 'Variable:0' shape=(5, 5, 1, 8) dtype=float32_ref>
b_conv1: <tf.Variable 'Variable_1:0' shape=(8,) dtype=float32_ref>
h_conv1: Tensor("Tanh:0", shape=(?, 64, 64, 8), dtype=float32)
h_pool1: Tensor("AvgPool:0", shape=(?, 32, 32, 8), dtype=float32)
W_conv2: <tf.Variable 'Variable_2:0' shape=(5, 5, 8, 16) dtype=float32_ref>
b_conv2: <tf.Variable 'Variable_3:0' shape=(16,) dtype=float32_ref>
h_conv2: Tensor("Tanh_1:0", shape=(?, 32, 32, 16), dtype=float32)
h_pool2: Tensor("AvgPool_1:0", shape=(?, 16, 16, 16), dtype=float32)
W_conv3: <tf.Variable 'Variable_4:0' shape=(1, 1, 16, 32) dtype=float32_ref>
b_conv3: <tf.Variable 'Variable_5:0' shape=(32,) dtype=float32_ref>
h_conv3: Tensor("Relu:0", shape=(?, 16, 16, 32), dtype=float32)
h_pool3: Tensor("AvgPool_2:0", shape=(?, 8, 8, 32), dtype=float32)
W_conv4: <tf.Variable 'Variable_6:0' shape=(1, 1, 32, 64) dtype=float32_ref>
b_conv4: <tf.Variable 'Variable_7:0' shape=(64,) dtype=float32_ref>
h_conv4: Tensor("Relu_1:0", shape=(?, 8, 8, 64), dtype=float32)
h_pool4: Tensor("AvgPool_3:0", shape=(?, 4, 4, 64), dtype=float32)
W_conv5: <tf.Variable 'Variable_8:0' shape=(1, 1, 64, 128) dtype=float32_ref>
b_conv5: <tf.Variable 'Variable_9:0' shape=(128,) dtype=float32_ref>
h_conv5: Tensor("Relu_2:0", shape=(?, 4, 4, 128), dtype=float32)
h_pool5: Tensor("AvgPool_4:0", shape=(?, 2, 2, 128), dtype=float32)
h_pool5_flat: Tensor("Reshape_1:0", shape=(?, 512), dtype=float32)
W_fc1: <tf.Variable 'Variable_10:0' shape=(512, 64) dtype=float32_ref>
b_fc1: <tf.Variable 'Variable_11:0' shape=(64,) dtype=float32_ref>
h_fc1: Tensor("Relu_3:0", shape=(?, 64), dtype=float32)
h_fc1_drop: Tensor("dropout/mul:0", shape=(?, 64), dtype=float32)
W_fc2: <tf.Variable 'Variable_12:0' shape=(64, 2) dtype=float32_ref>
b_fc2: <tf.Variable 'Variable_13:0' shape=(2,) dtype=float32_ref>
y_conv: Tensor("add_2:0", shape=(?, 2), dtype=float32)
y_: Tensor("Placeholder_3:0", shape=(?, 2), dtype=float32)
iter: Tensor("Placeholder_1:0", dtype=int32)
y_conv的形状与我的假设相似,但批量大小变为192.
任何人都可以帮助我?
答案 0 :(得分:1)
好吧,我终于找到了原因。这是重塑方法。
以下是www.tensorflow.org上重塑方法的说明:
如果形状的一个组件是特殊值-1,则计算该维度的大小,以使总大小保持不变。
所以,我的图像是512 * 512,如果我将它们重塑为64 * 64,批量大小将变为(512/64)*(512/64)* 3 = 8 * 8 * 3 = 192使总大小保持不变。
我犯了一个愚蠢的错误。