Python 3.x 三维CNN参数计算
我有一个使用tensorflow库编写的CNN代码:Python 3.x 三维CNN参数计算,python-3.x,tensorflow,Python 3.x,Tensorflow,我有一个使用tensorflow库编写的CNN代码: x_img = tf.placeholder(tf.float32) y_label = tf.placeholder(tf.float32) def convnet_3d(x_img, W): conv_3d_layer = tf.nn.conv3d(x_img, W, strides=[1,1,1,1,1], padding='VALID') return conv_3d_layer d
x_img = tf.placeholder(tf.float32)
y_label = tf.placeholder(tf.float32)
def convnet_3d(x_img, W):
conv_3d_layer = tf.nn.conv3d(x_img, W, strides=[1,1,1,1,1], padding='VALID')
return conv_3d_layer
def maxpool_3d(x_img):
maxpool_3d_layer = tf.nn.max_pool3d(x_img, ksize=[1,2,2,2,1], strides=[1,2,2,2,1], padding='VALID')
return maxpool_3d_layer
def convolutional_neural_network(x_img):
weights = {'W_conv1_layer':tf.Variable(tf.random_normal([3,3,3,1,32])),
'W_conv2_layer':tf.Variable(tf.random_normal([3,3,3,32,64])),
'W_fc_layer':tf.Variable(tf.random_normal([409600,1024])),
'W_out_layer':tf.Variable(tf.random_normal([1024, num_classes]))}
biases = {'b_conv1_layer':tf.Variable(tf.random_normal([32])),
'b_conv2_layer':tf.Variable(tf.random_normal([64])),
'b_fc_layer':tf.Variable(tf.random_normal([1024])),
'b_out_layer':tf.Variable(tf.random_normal([num_classes]))}
x_img = tf.reshape(x_img, shape=[-1, img_x, img_y, img_z, 1])
conv1_layer = tf.nn.relu(convnet_3d(x_img, weights['W_conv1_layer']) + biases['b_conv1_layer'])
conv1_layer = maxpool_3d(conv1_layer)
conv2_layer = tf.nn.relu(convnet_3d(conv1_layer, weights['W_conv2_layer']) + biases['b_conv2_layer'])
conv2_layer = maxpool_3d(conv2_layer)
fc_layer = tf.reshape(conv2_layer,[-1, 409600])
fc_layer = tf.nn.relu(tf.matmul(fc_layer, weights['W_fc_layer'])+biases['b_fc_layer'])
fc_layer = tf.nn.dropout(fc_layer, keep_rate)
output_layer = tf.matmul(fc_layer, weights['W_out_layer'])+biases['b_out_layer']
return output_layer
我的输入图像x_img是25x25x25(3d图像),我对代码有一些疑问:
1.“W_conv1_层”中的[3,3,3,1,32]是否表示[宽度x高度x深度x通道x过滤器数量]
2-在“W_conv2_layer”中,权重为[3,3,3,32,64],为什么输出为64?我知道3x3x3是过滤器大小,32是来自第一层的输入
3-在“W_fc_layer”中,权重为[4096001024],1024是fc层中的节点数,但这个神奇的数字“409600”从何而来
4-在图像进入conv层之前,我们为什么需要重塑图像
x_img = tf.reshape(x_img, shape=[-1, img_x, img_y, img_z, 1])
所有答案都可以在中找到
在转到更复杂的内容之前,您应该尝试在图像上实现一个简单的convnet。我要补充的是,我不会以这种方式编写代码,特别是对于占位符,我很惊讶它居然可以工作。事先添加一些关于占位符形状的信息是一个很好的做法。谢谢您的回答,告诉我如何为占位符编写代码,我知道409600来自maxpool3d图层,但我如何计算它?例如,如果图像更改为32x32x32,您的填充是有效的,因此在每个空间维度上,您减去过滤器维度,然后添加1(用于最大池的卷积),您将每个空间维度的整数除以2。对于二维CNN和三维CNN,您的问题已经得到了无数次的回答。我必须补充一点,如果您不确定,您可以运行每个张量来查看它们的形状。