Giter VIP home page Giter VIP logo

michaldyczko / pytorch-parametrizations Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 9 KB

PyTorch implementation of the spectral parametrization of the weights of a 2D convolutional layer, as introduced in the paper "Efficient Nonlinear Transforms for Lossy Image Compression" by Johannes Ballé, PCS 2018.

Home Page: https://pypi.org/project/pytorch-parametrizations/

License: MIT License

Python 100.00%
parametrization pytorch spectral sadam

pytorch-parametrizations's Introduction

pytorch-parametrizations

Spectral Parametrization

This module provides a PyTorch implementation of the spectral parametrization of the weights of a 2D convolutional layer, as introduced in the paper "Efficient Nonlinear Transforms for Lossy Image Compression" by Johannes Ballé, PCS 2018.

Usage

import torch
import torch.nn.utils.parametrize as parametrize

from pytorch_parametrizations import SpectralParametrization

# Create a 2D convolutional layer
conv = torch.nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, padding=1)

# Register the spectral parametrization for the weights of the layer
parametrize.register_parametrization(conv, 'weight', SpectralParametrization(conv.kernel_size), unsafe=True)


print(conv.parametrizations.weight)
# Output:
# conv.parametrizations.weight
# ParametrizationList(
#   (0): SpectralParametrization()
# )

register_spectral_parametrization

This function registers the spectral parametrization for every Conv2d and ConvTranspose2d module in a given module.

Usagev

import torch
from pytorch_parametrizations.spectral.utils import register_spectral_parametrization

# Create a module
module = torch.nn.Sequential(
    torch.nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, padding=1),
    torch.nn.ReLU(),
    torch.nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, padding=1),
    torch.nn.ReLU(),
    torch.nn.Conv2d(in_channels=64, out_channels=3, kernel_size=3, padding=1),
    torch.nn.Sigmoid()
)

# Register the spectral parametrization for every Conv2d and ConvTranspose2d module in the module
register_spectral_parametrization(module)

print(module)
# Output:
# Sequential(
#   (0): ParametrizedConv2d(
#     3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)
#     (parametrizations): ModuleDict(
#       (weight): ParametrizationList(
#         (0): SpectralParametrization()
#       )
#     )
#   )
#   (1): ReLU()
#   (2): ParametrizedConv2d(
#     64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)
#     (parametrizations): ModuleDict(
#       (weight): ParametrizationList(
#         (0): SpectralParametrization()
#       )
#     )
#   )
#   (3): ReLU()
#   (4): ParametrizedConv2d(
#     64, 3, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)
#     (parametrizations): ModuleDict(
#       (weight): ParametrizationList(
#         (0): SpectralParametrization()
#       )
#     )
#   )
#   (5): Sigmoid()
# )

# Unregister the spectral parametrization for every Conv2d and ConvTranspose2d module in the module
register_spectral_parametrization(module, undo=True)

print(module)
# Output:
# Sequential(
#   (0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
#   (1): ReLU()
#   (2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
#   (3): ReLU()
#   (4): Conv2d(64, 3, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
#   (5): Sigmoid()
# )

pytorch-parametrizations's People

Contributors

michaldyczko avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.