简体   繁体   English

Tensorflow (Keras) 到 PyTorch Conv2D 转换

[英]Tensorflow (Keras) to PyTorch Conv2D conversion

I'm trying to translate a custom UNET implementation from Tensorflow to PyTorch.我正在尝试将自定义 UNET 实现从 Tensorflow 转换为 PyTorch。 I've encountered some problems with the Conv2D layers.我在使用 Conv2D 图层时遇到了一些问题。

I know there could be some trouble with padding, it tried this and this but it didn't help.我知道填充可能会有一些问题,它尝试了这个这个,但它没有帮助。

My conversion code looks like this:我的转换代码如下所示:

from keras.layers import Conv2D
from torch import nn

import torch
import pandas as pd
import numpy as np

img = np.random.rand(1, 256, 256, 1)

## TF Init
conv_tf = Conv2D(
    64, 3, activation="relu", padding="same", kernel_initializer="he_normal", 
)

conv_tf(img)
conv_tf.bias = np.random.rand(64)

## PT Init + copy weights
conv_torch = nn.Conv2d(
    in_channels=1, out_channels=64, kernel_size=3, padding="same", bias=True
)
conv_torch.weight = nn.parameter.Parameter(
    torch.Tensor(conv_tf.weights[0].numpy().transpose(3, 2, 0, 1))
)
conv_torch.bias = nn.parameter.Parameter(torch.Tensor(conv_tf.bias))

conv_torch = nn.Sequential(
    conv_torch,
    nn.ReLU()
)

If I try to run a tensor through the models, the result is close but not the same (with huge differences between a few points)如果我尝试通过模型运行张量,结果很接近但不一样(几个点之间存在巨大差异)

pred_tf = conv_tf(img).numpy()
pred_pt = conv_torch(torch.Tensor(img).reshape(1, 1, 256, 256)).detach().numpy().reshape(pred_tf.shape)

pred_tf.mean()
#0.7202551

pred_pt.mean()
#0.7202549
TF - PT TF-PT
count数数 4.1943e+06 4.1943e+06
mean意思是 -2.2992e-09 -2.2992e-09
std性病 0.969716 0.969716
min分钟 -3.85477 -3.85477
25% 25% -0.641259 -0.641259
50% 50% 0 0
75% 75% 0.641266 0.641266
max最大限度 3.8742 3.8742

Any idea?任何想法? Thanks谢谢

You suspect padding.你怀疑填充。 This can be easily verified: compare pred_tf and pred_pt only on interior pixels: discard a band (1 pix wide, in your case) around the image.这可以很容易地验证:仅在内部像素上比较pred_tfpred_pt :丢弃图像周围的一条带(在您的情况下为 1 像素宽)。
If the interior pixels are identical - then it is a padding issue.如果内部像素相同 - 那么这是一个填充问题。

However, given your diff values, I suspect this is not the case.但是,鉴于您的差异值,我怀疑情况并非如此。

Maybe there is an issue with reflecting/transposing the kernel weights between the two methods?也许在两种方法之间反射/转置 kernel 权重存在问题? Try working with non-square kernels: eg, 3x5 instead of 3x3.尝试使用非方形内核:例如,3x5 而不是 3x3。

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

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