Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New functionality: use torchsummary to build pytorch model with scalable input shape #80

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

ncble
Copy link

@ncble ncble commented Jul 4, 2019

(New functionality) The main function summary (from torchsummary import summary) can also be used to infer the output shape of a pytorch model. Thus, it provides a way to build pytorch model that supports any input shape like in Keras (see the example below). Apart from this, I modified the default parameter for device in summary (default to 'cpu' instead of 'cuda').

Build pytorch model with scalable input shape (like Keras)

import torch
import torch.nn as nn
from torchsummary import summary

class AutoEncoder(nn.Module):
    """
    ResNet autoencoder network that support any input shape as model in Keras
    :param img_shape (tuple, channel last): support any image input shape 
    :param state_dim: (int) latent state dimension
    """

    def __init__(self, img_shape=(3, 224, 224), state_dim=3):
        super().__init__()
        self.state_dim = state_dim
        self.img_shape = img_shape

        self.encoder_conv = nn.Sequential(
            nn.Conv2d(self.img_shape[0], 64, kernel_size=7, stride=2, padding=3, bias=False),
            nn.BatchNorm2d(64),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=3, stride=2, padding=1),

            nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1, bias=False),
            nn.BatchNorm2d(64),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=3, stride=2),

            nn.Conv2d(64, 64, kernel_size=3, stride=2, padding=1, bias=False),
            nn.BatchNorm2d(64),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=3, stride=2)
        )
        ## Without torchsummary here, it's impossible to build model with scalable input shape as Keras.
        outshape = summary(self.encoder_conv, img_shape, show=False)  # [-1, channels, high, width]
        self.img_height, self.img_width = outshape[-2:]
        self.encoder_fc = nn.Sequential(
            nn.Linear(self.img_height * self.img_width * 64, state_dim)
        )

        self.decoder_fc = nn.Sequential(
            nn.Linear(state_dim, self.img_height * self.img_width * 64)
        )

        self.decoder_conv = nn.Sequential(
            nn.ConvTranspose2d(64, 64, kernel_size=3, stride=2),
            nn.BatchNorm2d(64),
            nn.ReLU(),

            nn.ConvTranspose2d(64, 64, kernel_size=3, stride=2),
            nn.BatchNorm2d(64),
            nn.ReLU(),

            nn.ConvTranspose2d(64, 64, kernel_size=3, stride=2),
            nn.BatchNorm2d(64),
            nn.ReLU(),

            nn.ConvTranspose2d(64, 64, kernel_size=3, stride=2),
            nn.BatchNorm2d(64),
            nn.ReLU(),

            nn.ConvTranspose2d(64, self.img_shape[0], kernel_size=4, stride=2),
            nn.Tanh()
        )

    def encode(self, x):
        """
        Encode image to latent state
        """
        encoded = self.encoder_conv(x)
        encoded = encoded.view(encoded.size(0), -1)
        return self.encoder_fc(encoded)

    def decode(self, x):
        """
        Decode latent state to image
        """
        decoded = self.decoder_fc(x)
        decoded = decoded.view(x.size(0), 64, self.img_height, self.img_width)
        return self.decoder_conv(decoded)

    def forward(self, x):
        reconstruct = self.decode(self.encode(x))
        return reconstruct


img_shape = (3,128,128)
model = AutoEncoder(img_shape=img_shape, state_dim=100)
summary(model, img_shape)

img_shape = (3,256,256)
model = AutoEncoder(img_shape=img_shape, state_dim=100)
summary(model, img_shape)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant