GithubHelp home page GithubHelp logo

Comments (4)

qubvel avatar qubvel commented on May 12, 2024

Hi @crossknight
Add a conv2d layer with 1x1 kernel to make them equal (look at resnet architecture).

from segmentation_models.pytorch.

crossknight avatar crossknight commented on May 12, 2024

Hi @qubvel,

Thanks for your reply. I tried your solution and It seems to be working fine but I'm sure that I implemented it correctly. Could you please review my codes?

Encoder

def forward(self, x):
        x0 = self.conv1(x)
        x0 = self.bn1(x0)
        x0 = self.relu(x0)

        x1 = self.maxpool(x0)

       out_tensor = x1

        x1 = self.layer1(x1)

        in_tensor = x1
        x1 = x1 + out_tensor.to('cuda')

        x2 = self.layer2(x1)

        conv2 = nn.Conv2d(64, 128, 1).to('cuda')
        maxpool2 = nn.MaxPool2d(2).to('cuda')
        out_tensor = conv2(in_tensor)
        out_tensor = maxpool2(out_tensor)
        in_tensor = x2
        x2 = x2 + out_tensor.to('cuda')

        x3 = self.layer3(x2)

        conv3 = nn.Conv2d(128, 256, 1).to('cuda')
        maxpool3 = nn.MaxPool2d(2).to('cuda')
        out_tensor = conv3(in_tensor)
        out_tensor = maxpool3(out_tensor)
        in_tensor = x3
        x3 = x3 + out_tensor.to('cuda')

        x4 = self.layer4(x3)

        conv4 = nn.Conv2d(256, 512, 1).to('cuda')
        maxpool4 = nn.MaxPool2d(2).to('cuda')
        out_tensor = conv4(in_tensor)
        out_tensor = maxpool4(out_tensor)
        x4 = x4 + out_tensor.to('cuda’)

        return [x4, x3, x2, x1, x0]

Decoder

def forward(self, x):
        encoder_head = x[0]
        skips = x[1:]

        in_tensor = encoder_head
        m = nn.ConstantPad2d((8, 8, 8, 8), 0)
        out_tensor = self.conv1(in_tensor)
        out_tensor = m(out_tensor)

        x = self.layer1([encoder_head, skips[0]])

        in_tensor = x
        x = x + out_tensor.to('cuda')
        m = nn.ConstantPad2d((16, 16, 16, 16), 0)
        out_tensor = self.conv2(in_tensor)
        out_tensor = m(out_tensor)

        x = self.layer2([x, skips[1]])

        in_tensor = x
        x = x + out_tensor.to('cuda')
        m = nn.ConstantPad2d((32, 32, 32, 32), 0)
        out_tensor = self.conv3(in_tensor)
        out_tensor = m(out_tensor)

        x = self.layer3([x, skips[2]])

        in_tensor = x
        x = x + out_tensor.to('cuda')
        m = nn.ConstantPad2d((64, 64, 64, 64), 0)
        out_tensor = self.conv4(in_tensor)
        out_tensor = m(out_tensor)

        x = self.layer4([x, skips[3]])

        in_tensor = x
        x = x + out_tensor.to('cuda')
        m = nn.ConstantPad2d((128, 128, 128, 128), 0)
        out_tensor = self.conv5(in_tensor)
        out_tensor = m(out_tensor)

        x = self.layer5([x, None])

        x = x + out_tensor.to('cuda')

        x = self.final_conv(x)

        return x

from segmentation_models.pytorch.

qubvel avatar qubvel commented on May 12, 2024

Hi @crossknight
It is hard to read your code, look at some style guidlines/examples how to use pytorch to create a model in pytorch.
Why did you pad tensors?
Why did you move them to cuda in forward?

Make it step by step, print tensor shapes, use some tool for network architecture visualization (e.g. hiddenlayers)

from segmentation_models.pytorch.

crossknight avatar crossknight commented on May 12, 2024

Hi @qubvel

Why did you pad tensors?

  • (Decoder) because the 3rd,4th dimension of tensor are not the same size after passed the layer

Why did you move them to cuda in forward?

  • because it will shows error about data are not on the same gpu/cpu

from segmentation_models.pytorch.

Related Issues (20)

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.