【问题标题】:convolution matrix filter size vs channel length卷积矩阵滤波器大小与通道长度
【发布时间】:2018-01-26 14:44:21
【问题描述】:

我是计算机视觉的初学者。我想知道过滤器的大小和输出图像上的通道之间的关系

我的目标是了解使用卷积神经网络连接尺寸和通道的各种关系

我正在使用 tensorflow 库,我找到了一个在 cifar 10 数据集上应用 CNN 的示例,

数据集包括:-

  1. data -- 一个 10000x3072 numpy 的 uint8 数组。数组的每一行 存储 32x32 彩色图像。前 1024 个条目包含红色 通道值,接下来的 1024 是绿色,最后的 1024 是 蓝色的。图像以行优先顺序存储,因此前 32 数组的条目是第一行的红色通道值 图片。
  2. 标签 -- 包含 0-9 范围内的 10000 个数字的列表。

代码:

import tensorflow as tf


X = tf.placeholder(tf.float32,shape=[None,32,32,3])
y_true = tf.placeholder(tf.float32,shape=[None,10])



hold_prob = tf.placeholder(tf.float32)

#  Helper Functions 



def init_weight(shape,name_W):
    init_rand_dist = tf.truncated_normal(shape,stddev=0.1)
    return tf.Variable(init_rand_dist,name=name_W)

#  init bais

def init_bias(shape, name_b):
    init_bias_vals = tf.constant(value=0.1,shape=shape)
    return tf.Variable(init_bias_vals,name=name_b)

#  convolution 2d

# Conv2D

def conv2d(X,W,name_conv):
    #  X --> [batch,H,W,Channels]
    #  W --> [filter H , filter W , Channel In , Channel Out]



    return tf.nn.conv2d(X,W,strides=[1,1,1,1],padding='SAME',name=name_conv)

#  convolutional Layer with activation and bais

# Convolutional Layes

def convolutional_layer(input_x, shape,name_W,name_b,name_conv):
    W = init_weight(shape = shape,name_W=name_W)
    b = init_bias(shape = [shape[3]],name_b = name_b)


    return tf.nn.relu(conv2d(input_x,W, name_conv =name_conv) + b )

#  pooling layer

# Pooling
def max_pooling_2by2(X):
    #  X --> [batch,H,W,Channels]

    return tf.nn.max_pool(X,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')

#  Fully connected Layer

#  Normal Layer (fully connected Layer)

def normal_full_layer(input_layer,size,name_W,name_b):

    input_size = int(input_layer.get_shape()[1])
    W = init_weight([input_size,size],name_W=name_W)
    b = init_bias([size],name_b=name_b)

    return tf.matmul(input_layer , W) + b

#  Create the Layers 



convo_1 = convolutional_layer( X , shape = [4,4,3,32] , name_W = "W_conv1" , name_b = "bias_Conv1" , name_conv = "Conv_1")
convo_1_pooling = max_pooling_2by2(convo_1)



convo_2 = convolutional_layer( convo_1_pooling, shape = [4,4,32,64] , name_W = "W_conv2" , name_b = "bias_Conv2" , name_conv = "Conv_2")
convo_2_pooling = max_pooling_2by2(convo_2)

# ** Now create a flattened layer  [-1,8 \* 8 \* 64] or [-1,4096] **

convo_2_flat = tf.reshape(convo_2_pooling,[-1,8*8*64])



full_layer_one = tf.nn.relu(normal_full_layer(convo_2_flat,1024,name_W="full_layer_W",name_b="full_layer_b"))



hold_prob = tf.placeholder(tf.float32)
full_one_dropot = tf.nn.dropout(full_layer_one,keep_prob=hold_prob)



y_pred = normal_full_layer(full_one_dropot,10,name_W = 'out_W',name_b='out_b' )

#    Loss Function 

cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y_true , logits= y_pred ))

#   Optimizer Adam Optimizer. 

optimizer = tf.train.AdamOptimizer(learning_rate=0.001)
train = optimizer.minimize(cross_entropy)


init = tf.global_variables_initializer()

#  Graph Session

steps = 5000

with tf.Session() as sess:
    sess.run(init)


    print (tf.all_variables())



    for i in range (steps) :   
        batch_x , batch_y = ch.next_batch(100)
        # print(convo_2_flat.eval(feed_dict={X:batch_x, y_true:batch_y, hold_prob:1.0}).shape)


        sess.run(train,feed_dict={X:batch_x,y_true:batch_y,hold_prob:0.5})

        #  PRINT OUT A MESSAGE EVERY 100 STEPS
        if i%100 == 0:

            print('Currently on step {}'.format(i))
            print('Accuracy is:')
            #  Test the Train Model
            matches = tf.equal(tf.argmax(y_pred,1),tf.argmax(y_true,1))

            acc = tf.reduce_mean(tf.cast(matches,tf.float32))

            print(sess.run(acc,feed_dict={X:training_images,y_true:training_labels,hold_prob:1.0}))
            print('\n')

我想知道过滤器的大小,例如第一个卷积层的过滤器大小为 (4 * 4) 和通道 (32),我想知道为什么选择这些数字并级联到下一层。

最后一层的另一个例子

tf.nn.relu(normal_full_layer(convo_2_flat,1024,name_W="full_layer_W",name_b="full_layer_b"))

它将flatten层的输出调整为1024,这背后的原因是什么

因为这张图片也是

【问题讨论】:

  • 你有什么问题?
  • 对不起,如果不清楚。我的问题与convo_1 = convolutional_layer( X , shape = [4,4,3,32] , name_W = "W_conv1" , name_b = "bias_Conv1" , name_conv = "Conv_1") 中的频道大小有关。这里通道输入为 3 即 3 色 RGB,选择数字 32 作为输出滤波器通道的原因是什么
  • 我会更新问题。让它更清楚

标签: python tensorflow conv-neural-network


【解决方案1】:

过滤器大小是执行卷积的窗口大小。它通过与填充交互来影响网络的其余部分,以确定该层输出张量的高度和宽度。

输出通道听起来像:该层输出中的通道数。因此,从 3 个通道(r、g、b)开始并具有 32 个输出通道的示例层将生成类似于图像但每个像素有 32 个数字而不是 3 个数字的输出。

滤波器的大小和它们的输出通道数是卷积神经网络的重要属性,它们的选择是为了最大限度地提高准确性。不同的架构(inception、resnet、vgg 等)对要使用的过滤器和通道数做出不同的选择。

【讨论】:

  • 感谢您的回复这个答案是一个好的开始。如果错了,请让我详细说明并纠正我通道大小我将其视为特征大小,就像在普通 ML 分类器中一样,通道输出是我想从图像中收集多少特征
猜你喜欢
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-08
  • 2019-11-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多