繁体   English   中英

为 Conv2d 层手动分配权重

[英]Manually assigning weights for Conv2d layer

我有一个包含许多 conv2d 层的模型。 我将模型转换为 Tflite 模型。 转换后我得到了单个 conv2d 的权重。 权重的形状看起来像这样

# code
w2 = get_variable(interpreter, 1)
print(w2.shape)

# output
(16, 3, 3, 3)

w2 是我从 tflite 模型得到的 conv2d 层的权重。

# looking at weights
tf.constant(w2)
# out

<tf.Tensor: shape=(16, 3, 3, 3), dtype=float32, numpy=
array([[[[-0.09935276,  0.02673087,  0.01329462],
         [-0.15000243,  0.12058315,  0.06234892],
         [-0.04185663, -0.11198951, -0.02449715]],

        [[-0.01043741,  0.00516671, -0.04251045],
         [ 0.09123346, -0.18056516, -0.15848799],
         [ 0.13060766, -0.07997198, -0.01930575]],

        [[-0.03572255, -0.01315425,  0.08955526],
         [ 0.16559589,  0.03411882,  0.0018566 ],
         [-0.14274003,  0.1362513 ,  0.02790332]]],


       [[[-0.18470907, -0.08563003, -0.1520263 ],
         [-0.04288448, -0.18342438, -0.15801121],
         [-0.03374813,  0.06371641,  0.03502055]],

现在是我使用命令 model.weights 从模型文件中得到的权重。

# code
model_layer = model.get_layer(index = 1)
model_layer.weights[0]

# out
<tf.Variable 'conv2d/kernel:0' shape=(3, 3, 3, 16) dtype=float32, numpy=
array([[[[-0.09935276, -0.18470907, -0.16035978, -0.00957598,
           0.12404141,  0.09072036,  0.08940545,  0.16788253,
          -0.09028493, -0.07161955,  0.05057701,  0.00413197,
           0.12936822,  0.13274643, -0.11566465,  0.06050111],
         [ 0.02673087, -0.08563003,  0.15529695, -0.16517243,
           0.09419081,  0.03450985,  0.05399269,  0.06663677,
          -0.1096884 ,  0.11150008, -0.14434202,  0.08073789,
          -0.00857992,  0.17634535, -0.1686475 , -0.02407928],
         [ 0.01329462, -0.1520263 , -0.16246322, -0.06716946,
           0.18214822, -0.13206367, -0.05873053,  0.13359356,
           0.13813934, -0.05382906,  0.1032899 ,  0.03165779,
           0.01169366, -0.11587013, -0.18203613, -0.10081998]],

        [[-0.15000243, -0.04288448,  0.03991991,  0.05653304,
          -0.08553669,  0.0082473 , -0.12359683, -0.01954196,
           0.15206149, -0.07700901,  0.10358813,  0.04298429,
           0.04496023, -0.1466851 ,  0.05197817,  0.1237444 ],

我尝试将 w2(16,3,3,3) 转换为我想要的形状 my_w2(3,3,3,16) 的方法。

# Method 1
# code

tf.transpose(tf.constant(w2))

# out
<tf.Tensor: shape=(3, 3, 3, 16), dtype=float32, numpy=
array([[[[-0.09935276, -0.18470907, -0.16035978, -0.00957598,
           0.12404141,  0.09072036,  0.08940545,  0.16788253,
          -0.09028493, -0.07161955,  0.05057701,  0.00413197,
           0.12936822,  0.13274643, -0.11566465,  0.06050111],
         [-0.01043741, -0.01095065, -0.13822603,  0.00533092,
          -0.02210169,  0.12576985, -0.1342443 , -0.15337837,
           0.15577388,  0.17446613, -0.17040835,  0.08397743,
           0.11096796, -0.08405711, -0.06032752,  0.01366897],
         [-0.03572255, -0.07657725, -0.18410352,  0.08384639,
          -0.07809233, -0.06835755,  0.12235427,  0.00525343,
           0.04881094, -0.10404772, -0.16282201, -0.15634196,
          -0.07554363, -0.10617974, -0.11948892, -0.07697168]],

        [[-0.15000243, -0.04288448,  0.03991991,  0.05653304,
          -0.08553669,  0.0082473 , -0.12359683, -0.01954196,
           0.15206149, -0.07700901,  0.10358813,  0.04298429,
           0.04496023, -0.1466851 ,  0.05197817,  0.1237444 ],

# Method 2
# code
tf.image.transpose(tf.constant(w2))

#out
<tf.Tensor: shape=(16, 3, 3, 3), dtype=float32, numpy=
array([[[[-0.09935276,  0.02673087,  0.01329462],
         [-0.01043741,  0.00516671, -0.04251045],
         [-0.03572255, -0.01315425,  0.08955526]],

        [[-0.15000243,  0.12058315,  0.06234892],
         [ 0.09123346, -0.18056516, -0.15848799],
         [ 0.16559589,  0.03411882,  0.0018566 ]],

        [[-0.04185663, -0.11198951, -0.02449715],
         [ 0.13060766, -0.07997198, -0.01930575],
         [-0.14274003,  0.1362513 ,  0.02790332]]],


       [[[-0.18470907, -0.08563003, -0.1520263 ],
         [-0.01095065,  0.13471746,  0.16735196],
         [-0.07657725,  0.14455187,  0.07566869]],

我想要的是转换我的权重 w2 以便将其分配给我的图层的正确方法。

出于优化原因,TFlite 会更改 Conv2D 权重形状。 我无法在文档中找到它,但在这里,在最后一条评论中,他们解释了“标准”和“精简”张量流形状实现之间的区别:

在标准 tensorflow 中,Conv2D 权重形状是 HWIO,意思是(filter_height, filter_width, input_channels, output_channels)

TFlite 实现是 OHWI,意思是(output_channels, filter_height, filter_width, input_channels)

为了解决您的问题,我们需要对轴重新排序。 直观地说,从 OHWI 到 HWIO,我们只需要将“O”从 OHWI 移动到最后一个索引 (3),其余的轴应该转到较低的索引:

Index:      0    1    2    3
            O    H    W    I

Reordering axes we have

New index:  1    2    3    0
            H    W    I    O

为此,我们可以使用tf.transpose但指定我们希望如何交换轴,正如刚才所讨论的:

tf.transpose(tf.constant(w2), (1,2,3,0) )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM