python - 如何将矢量 reshape 为 TensorFlow 的过滤器?

标签 python numpy tensorflow

我想将一些由另一个网络训练的权重传输到 TensorFlow,这些权重存储在像这样的单个向量中:

[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]

通过使用 numpy,我可以将它 reshape 为两个 3 x 3 过滤器,如下所示:

1 2 3     9  10 11
3 4 5     12 13 14
6 7 8     15 16 17

因此,我的过滤器的形状是(1,2,3,3)。然而,在 TensorFlow 中,过滤器的形状是 (3,3,2,1):

tf_weights = tf.Variable(tf.random_normal([3,3,2,1]))

将 tf_weights reshape 为预期的形状后,权重变得一团糟,无法获得预期的卷积结果。

具体来说,当图像或滤镜的形状是[number,channel,size,size]时,我写了一个卷积函数,它给出了正确的答案,但它太慢了:

def convol(images,weights,biases,stride):
    """
    Args:
      images:input images or features, 4-D tensor
      weights:weights, 4-D tensor
      biases:biases, 1-D tensor
      stride:stride, a float number
    Returns:
      conv_feature: convolved feature map
    """
    image_num = images.shape[0] #the number of input images or feature maps
    channel = images.shape[1] #channels of an image,images's shape should be like [n,c,h,w]
    weight_num = weights.shape[0] #number of weights, weights' shape should be like [n,c,size,size]
    ksize = weights.shape[2]
    h = images.shape[2]
    w = images.shape[3]
    out_h = (h+np.floor(ksize/2)*2-ksize)/2+1
    out_w = out_h

    conv_features = np.zeros([image_num,weight_num,out_h,out_w])
    for i in range(image_num):
        image = images[i,...,...,...]
        for j in range(weight_num):
            sum_convol_feature = np.zeros([out_h,out_w])
            for c in range(channel):
                #extract a single channel image
                channel_image = image[c,...,...]
                #pad the image
                padded_image = im_pad(channel_image,ksize/2)
                #transform this image to a vector
                im_col = im2col(padded_image,ksize,stride)

                weight = weights[j,c,...,...]
                weight_col = np.reshape(weight,[-1])
                mul = np.dot(im_col,weight_col)
                convol_feature = np.reshape(mul,[out_h,out_w])
                sum_convol_feature = sum_convol_feature + convol_feature
            conv_features[i,j,...,...] = sum_convol_feature + biases[j]
    return conv_features

相反,通过像这样使用 tensorflow 的 conv2d:

img = np.zeros([1,3,224,224])
img = img - 1
img = np.rollaxis(img, 1, 4)

weight_array = googleNet.layers[1].weights
weight_array = np.reshape(weight_array,[64,3,7,7])

biases_array = googleNet.layers[1].biases

tf_weight = tf.Variable(weight_array)

tf_img = tf.Variable(img)
tf_img = tf.cast(tf_img,tf.float32)

tf_biases = tf.Variable(biases_array)

conv_feature = tf.nn.bias_add(tf.nn.conv2d(tf_img,tf_weight,strides=[1,2,2,1],padding='SAME'),tf_biases)
sess = tf.Session()
sess.run(tf.initialize_all_variables())
feautre = sess.run(conv_feature)

我得到的feature map是错误的。

最佳答案

不要使用np.reshape。它可能 mess up the order of your values .

使用np.rollaxis相反:

>>> a = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18])
>>> a = a.reshape((1,2,3,3))
>>> a
array([[[[ 1,  2,  3],
         [ 4,  5,  6],
         [ 7,  8,  9]],

        [[10, 11, 12],
         [13, 14, 15],
         [16, 17, 18]]]])
>>> b = np.rollaxis(a, 1, 4)
>>> b.shape
(1, 3, 3, 2)
>>> b = np.rollaxis(b, 0, 4)
>>> b.shape
(3, 3, 2, 1)

请注意,尺寸为 3 的两个轴的顺序没有改变。如果我要标记它们,两个 rollaxis 操作会导致形状更改为 (1, 2, 31, 32) -> (1, 31, 32, 2) -> (31, 32, 2 , 1).您的最终数组如下所示:

>>> b
array([[[[ 1],
         [10]],

        [[ 2],
         [11]],

        [[ 3],
         [12]]],


       [[[ 4],
         [13]],

        [[ 5],
         [14]],

        [[ 6],
         [15]]],


       [[[ 7],
         [16]],

        [[ 8],
         [17]],

        [[ 9],
         [18]]]])

关于python - 如何将矢量 reshape 为 TensorFlow 的过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34725157/

相关文章:

java - 如何使用带有多个搜索词的 appengine 搜索 api 进行搜索?

python - 在 Django 中显示数据

python - 使用 fft2 reshape RGB 滤镜

python - NumPy:有限的累积总和

python - 成人收入数据集神经网络的训练精度较低

python - 无法创建 django 模型的新实例

python - 在python中滚动数组

python - 我的一些 Python 包无法在 OSX 控制台中运行

java - Java 上的 tensorflow : how to perform RGB to BGR operation?

c++ - 如何为 Windows 独立应用程序部署经过 Tensorflow 训练的模型进行推理