【问题标题】:How to convert a tensorflow model to a pytorch model?如何将 tensorflow 模型转换为 pytorch 模型?
【发布时间】:2020-08-25 01:51:06
【问题描述】:

我是 pytorch 的新手。这是张量流模型的架构,我想将其转换为 pytorch 模型。

我已经完成了大部分代码,但对一些地方感到困惑。

1) 在tensorflow中,Conv2D函数将filter作为输入。但是,在pytorch中,该函数将输入通道和输出通道的大小作为输入。那么如何找到输入通道和输出通道的等效数量,提供过滤器的大小。

2) 在 tensorflow 中,dense layer 有一个称为“nodes”的参数。但是,在pytorch中,同一层有2个不同的输入(输入参数的大小和目标参数的大小),我如何根据节点的数量来确定它们。

这是张量流代码。

from keras.utils import to_categorical
from keras.models import Sequential, load_model
from keras.layers import Conv2D, MaxPool2D, Dense, Flatten, Dropout

model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(5,5), activation='relu', input_shape=X_train.shape[1:]))
model.add(Conv2D(filters=32, kernel_size=(5,5), activation='relu'))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Dropout(rate=0.25))
model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu'))
model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu'))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Dropout(rate=0.25))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(rate=0.5))
model.add(Dense(43, activation='softmax'))

这是我的代码:

import torch.nn.functional as F
import torch



# The network should inherit from the nn.Module
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        # Define 2D convolution layers
        # 3: input channels, 32: output channels, 5: kernel size, 1: stride
        self.conv1 = nn.Conv2d(3, 32, 5, 1)   # The size of input channel is 3 because all images are coloured
        self.conv2 = nn.Conv2d(32, 64, 5, 1)
        self.conv3 = nn.Conv2d(64, 128, 3, 1)
        self.conv3 = nn.Conv2d(128, 256, 3, 1)
        # It will 'filter' out some of the input by the probability(assign zero)
        self.dropout1 = nn.Dropout2d(0.25)
        self.dropout2 = nn.Dropout2d(0.5)
        # Fully connected layer: input size, output size
        self.fc1 = nn.Linear(36864, 128)
        self.fc2 = nn.Linear(128, 10)

    # forward() link all layers together,
    def forward(self, x):
        x = self.conv1(x)
        x = F.relu(x)
        x = self.conv2(x)
        x = F.relu(x)
        x = F.max_pool2d(x, 2)
        x = self.dropout1(x)
        x = self.conv3(x)
        x = F.relu(x)
        x = self.conv4(x)
        x = F.relu(x)
        x = F.max_pool2d(x, 2)
        x = self.dropout1(x)
        x = torch.flatten(x, 1)
        x = self.fc1(x)
        x = F.relu(x)
        x = self.dropout2(x)
        x = self.fc2(x)
        output = F.log_softmax(x, dim=1)
        return output

提前致谢!

【问题讨论】:

    标签: python tensorflow machine-learning computer-vision pytorch


    【解决方案1】:

    1) 在 pytorch 中,我们将输入通道和输出通道作为输入。在您的第一层中,输入通道将是图像中颜色通道的数量。之后,它总是与上一层的输出通道相同(输出通道由 Tensorflow 中的 filters 参数指定)。

    2)。 Pytorch 有点烦人,因为在展平你的 conv 输出时,你必须自己计算形状。你可以使用一个方程来计算这个(???=(?−?+2?)/?+1),或者制作一个形状计算函数来获得通过网络的转换部分的虚拟图像的形状.此参数将是您输入参数的大小;您的输出参数的大小将只是您想要在下一个全连接层中的节点数。

    【讨论】:

    • 谢谢!澄清一下,说当前输出通道=过滤器参数+以前的输出通道是否正确?例如,当 filter 参数为 32 时,PyTorch 中第一层的大小将为 Conv2d(3, 32, 5, 1);第二层是 Conv2d(32, 64, 5, 1);第三层是 Conv2d(64, 96, 5, 1)。
    • @Cici 实际上并非如此。输出通道参数完全独立于输入通道。一旦您将内核应用于图像,您对具有任意数量通道的图像应用的每个过滤器最终都会成为具有单个通道的新图像。然后,您将这些过滤器堆叠在一起,使最终图像具有 (# of filters) 个通道。
    猜你喜欢
    • 2020-08-25
    • 2019-05-06
    • 2018-10-05
    • 2019-04-14
    • 2021-07-14
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 2023-01-31
    相关资源
    最近更新 更多