Code Monkey home page Code Monkey logo

pytorch_convnd's Introduction

convNd and convTransposeNd in Pytorch

This n-dimensional convolution is based on recursivly creating a convNd with many conv(N-1)d, until reaching conv3d, where the Pytorch implementation is used. . Also, passing a flag is_transposed=True to the convNd function will result in a convTransposeNd operation.

The following convolution features are available:

  • bias
  • stride
  • padding
  • output_padding
  • groups Todos:
  • dilation

Examples

The main.py contains a convNd test where N=3, in this cased based on multiple conv2d operations. In this way, the functionality of convNd can be compared with the Pytorch conv3d and convTranspose3d operator. In mainNd.py, an example with a 5D convolution is presented.

convNd is an extended version of the conv4d from Timothy Gebhard's code.

The convNd was used as a conv4d layer inside the LFMNet. A CNN capable of reconstructing 3D stacks from Light-field microscopy images.

Usage

Example in 5D

import torch
from convNd import convNd

# define basic layer info
inChans = 2
outChans = 4
weight = torch.rand(1)[0]
bias = torch.rand(1)[0]

# create input tensor
x = torch.rand(1, inChans, 5, 5, 5, 5, 5).cuda()
conv5d = convNd(
    in_channels=inChans, 
    out_channels=outChans,
    num_dims=5, 
    kernel_size=3, 
    stride=(2,1,1,1,1), 
    padding=0, 
    padding_mode='zeros',
    output_padding=0,
    is_transposed=False,
    use_bias=True, 
    groups=2,
    kernel_initializer=lambda x: torch.nn.init.constant_(x, weight), 
    bias_initializer=lambda x: torch.nn.init.constant_(x, bias)).cuda()


# Create tranposed 5D convolution
# note the is_tranposed parameter
convT5d = convNd(
    in_channels=inChans,
    out_channels=outChans,
    num_dims=5,
    kernel_size=3,
    stride=(2,1,1,1,1),
    padding=0,
    padding_mode='zeros',
    output_padding=0,
    is_transposed=True,
    use_bias=True,
    groups=2,
    kernel_initializer=lambda x: torch.nn.init.constant_(x, weight), 
    bias_initializer=lambda x: torch.nn.init.constant_(x, bias)).cuda()

# apply conv5d
xConv = conv5d(x)
print(xConv.shape)

# apply convTranspose5d
xTConv = convT5d(x)
print(xTConv.shape)

pytorch_convnd's People

Contributors

pvjosue avatar 7iw avatar st0nedb 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.