AttributeError: 'VoxResNet' object has no attribute 'to_gpu'

Safa_Mathlouthi: <p>I’m a newer to pytorch. I run a model by pytorch But I am getting error as  ‘VoxResNet’ object has no attribute ‘to_gpu’</p>
<p>my model :</p>
<pre><code class="lang-auto">class VoxResNet(nn.Module):
    DEPTH = 6

    def __init__(self, n_classes=2):
        super().__init__()
        resnet = torchvision.models.resnet.resnet50(pretrained=True)
        down_blocks = []
        up_blocks = []
        self.input_block = nn.Sequential(*list(resnet.children()))[:3]
        self.input_pool = list(resnet.children())[3]
        for bottleneck in list(resnet.children()):
            if isinstance(bottleneck, nn.Sequential):
                down_blocks.append(bottleneck)
        self.down_blocks = nn.ModuleList(down_blocks)
        self.bridge = Bridge(2048, 2048)
        up_blocks.append(UpBlockForUNetWithResNet50(2048, 1024))
        up_blocks.append(UpBlockForUNetWithResNet50(1024, 512))
        up_blocks.append(UpBlockForUNetWithResNet50(512, 256))
        up_blocks.append(UpBlockForUNetWithResNet50(in_channels=128 + 64, out_channels=128,
                                                    up_conv_in_channels=256, up_conv_out_channels=128))
        up_blocks.append(UpBlockForUNetWithResNet50(in_channels=64 + 3, out_channels=64,
                                                    up_conv_in_channels=128, up_conv_out_channels=64))

        self.up_blocks = nn.ModuleList(up_blocks)

        self.out = nn.Conv2d(64, n_classes, kernel_size=1, stride=1)

    def forward(self, x, with_output_feature_map=False):
        pre_pools = dict()
        pre_pools[f"layer_0"] = x
        x = self.input_block(x)
        pre_pools[f"layer_1"] = x
        x = self.input_pool(x)

        for i, block in enumerate(self.down_blocks, 2):
            x = block(x)
            if i == (VoxResNet.DEPTH - 1):
                continue
            pre_pools[f"layer_{i}"] = x

        x = self.bridge(x)

        for i, block in enumerate(self.up_blocks, 1):
            key = f"layer_{VoxResNet.DEPTH - 1 - i}"
            x = block(x, pre_pools[key])
        output_feature_map = x
        x = self.out(x)
        del pre_pools
        if with_output_feature_map:
            return x, output_feature_map
        else:
            return x

model = VoxResNet().cuda()
inp = torch.rand((2, 3, 512, 512)).cuda()
out = model(inp)
</code></pre>
<p>error :</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "train.py", line 169, in &lt;module&gt;
    main()
  File "train.py", line 90, in main
    vrn.to_gpu()
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 591, in __getattr__
    type(self).__name__, name))
AttributeError: 'VoxResNet' object has no attribute 'to_gpu'
</code></pre>

dejanbatanjac: <aside class="quote no-group" data-username="Safa_Mathlouthi" data-post="1" data-topic="58417">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/ac91a4/48.png" class="avatar"> Safa_Mathlouthi:</div>
<blockquote>
<p>to_gpu</p>
</blockquote>
</aside>
<p>Hi can you provide the feedback where you found the VoxResNet architecture that you are using?</p>
<p>Also, try to alter your <code>train.py</code> line 90 to read <code>vrn.to('cuda')</code> or <code>vrn.to(0)</code> if this helps.</p>

Safa_Mathlouthi: <aside class="quote no-group" data-username="dejanbatanjac" data-post="2" data-topic="58417">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/77aa72/48.png" class="avatar"> dejanbatanjac:</div>
<blockquote>
<p>to(0)</p>
</blockquote>
</aside>
<p>i found the VoxResNet architecture in github<br>
this is the link where i found it  <a href="https://github.com/Ryo-Ito/brain_segmentation" rel="noopener nofollow ugc">https://github.com/Ryo-Ito/brain_segmentation</a><br>
but I made modifications according to my needs</p>
<p>and when I try vrn.to (‘cuda’) or vrn.to (0) I get this error</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "train.py", line 169, in &lt;module&gt;
    main()
  File "train.py", line 94, in main
    optimizer.setup(vrn)
  File "/usr/local/lib/python3.6/dist-packages/chainer/optimizer.py", line 629, in setup
    super(GradientMethod, self).setup(link)
  File "/usr/local/lib/python3.6/dist-packages/chainer/optimizer.py", line 417, in setup
    raise TypeError('optimization target must be a link')
TypeError: optimization target must be a link
</code></pre>

dejanbatanjac: <p>Hey <a class="mention" href="/u/safa_mathlouthi">@Safa_Mathlouthi</a>, in this project they used <a href="https://github.com/chainer/chainer" rel="nofollow noopener">Chainer</a>. <a href="https://github.com/chainer/chainer" rel="nofollow noopener">Chainer</a> is PyTorch opponent so you will not get much on this forum ;).</p>
<p>Check the</p>
<pre><code class="lang-auto">Requirements

    python 3.6
    chainer v2
    cupy
    dipy
    nibabel
    numpy
    pandas
    SimpleITK
</code></pre>
<p>No PyTorch in there.</p>

Safa_Mathlouthi: <p>i know that, but i implemented this code by modifying the model  with this model :</p>
<pre><code class="lang-auto">import torch 
import torch.nn as nn 
import torchvision.models as models
  
import torchvision
resnet = torchvision.models.resnet.resnet50(pretrained=True)

class ConvBlock(nn.Module):
    """
    Helper module that consists of a Conv -&gt; BN -&gt; ReLU
    """

    def __init__(self, in_channels, out_channels, padding=1, kernel_size=3, stride=1, with_nonlinearity=True):
        super().__init__()
        self.conv = nn.Conv2d(in_channels, out_channels, padding=padding, kernel_size=kernel_size, stride=stride)
        self.bn = nn.BatchNorm2d(out_channels)
        self.relu = nn.ReLU()
        self.with_nonlinearity = with_nonlinearity

    def forward(self, x):
        x = self.conv(x)
        x = self.bn(x)
        if self.with_nonlinearity:
            x = self.relu(x)
        return x


class Bridge(nn.Module):
    """
    This is the middle layer of the UNet which just consists of some
    """

    def __init__(self, in_channels, out_channels):
        super().__init__()
        self.bridge = nn.Sequential(
            ConvBlock(in_channels, out_channels),
            ConvBlock(out_channels, out_channels)
        )

    def forward(self, x):
        return self.bridge(x)


class UpBlockForUNetWithResNet50(nn.Module):
    """
    Up block that encapsulates one up-sampling step which consists of Upsample -&gt; ConvBlock -&gt; ConvBlock
    """

    def __init__(self, in_channels, out_channels, up_conv_in_channels=None, up_conv_out_channels=None,
                 upsampling_method="conv_transpose"):
        super().__init__()

        if up_conv_in_channels == None:
            up_conv_in_channels = in_channels
        if up_conv_out_channels == None:
            up_conv_out_channels = out_channels

        if upsampling_method == "conv_transpose":
            self.upsample = nn.ConvTranspose2d(up_conv_in_channels, up_conv_out_channels, kernel_size=2, stride=2)
        elif upsampling_method == "bilinear":
            self.upsample = nn.Sequential(
                nn.Upsample(mode='bilinear', scale_factor=2),
                nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1)
            )
        self.conv_block_1 = ConvBlock(in_channels, out_channels)
        self.conv_block_2 = ConvBlock(out_channels, out_channels)

    def forward(self, up_x, down_x):
        """
        :param up_x: this is the output from the previous up block
        :param down_x: this is the output from the down block
        :return: upsampled feature map
        """
        x = self.upsample(up_x)
        x = torch.cat([x, down_x], 1)
        x = self.conv_block_1(x)
        x = self.conv_block_2(x)
        return x


class VoxResNet(nn.Module):
    DEPTH = 6

    def __init__(self, n_classes=2):
        super().__init__()
        resnet = torchvision.models.resnet.resnet50(pretrained=True)
        down_blocks = []
        up_blocks = []
        self.input_block = nn.Sequential(*list(resnet.children()))[:3]
        self.input_pool = list(resnet.children())[3]
        for bottleneck in list(resnet.children()):
            if isinstance(bottleneck, nn.Sequential):
                down_blocks.append(bottleneck)
        self.down_blocks = nn.ModuleList(down_blocks)
        self.bridge = Bridge(2048, 2048)
        up_blocks.append(UpBlockForUNetWithResNet50(2048, 1024))
        up_blocks.append(UpBlockForUNetWithResNet50(1024, 512))
        up_blocks.append(UpBlockForUNetWithResNet50(512, 256))
        up_blocks.append(UpBlockForUNetWithResNet50(in_channels=128 + 64, out_channels=128,
                                                    up_conv_in_channels=256, up_conv_out_channels=128))
        up_blocks.append(UpBlockForUNetWithResNet50(in_channels=64 + 3, out_channels=64,
                                                    up_conv_in_channels=128, up_conv_out_channels=64))

        self.up_blocks = nn.ModuleList(up_blocks)

        self.out = nn.Conv2d(64, n_classes, kernel_size=1, stride=1)

    def forward(self, x, with_output_feature_map=False):
        pre_pools = dict()
        pre_pools[f"layer_0"] = x
        x = self.input_block(x)
        pre_pools[f"layer_1"] = x
        x = self.input_pool(x)

        for i, block in enumerate(self.down_blocks, 2):
            x = block(x)
            if i == (VoxResNet.DEPTH - 1):
                continue
            pre_pools[f"layer_{i}"] = x

        x = self.bridge(x)

        for i, block in enumerate(self.up_blocks, 1):
            key = f"layer_{VoxResNet.DEPTH - 1 - i}"
            x = block(x, pre_pools[key])
        output_feature_map = x
        x = self.out(x)
        del pre_pools
        if with_output_feature_map:
            return x, output_feature_map
        else:
            return x

model = VoxResNet().cuda()
inp = torch.rand((2, 3, 512, 512)).cuda()
out = model(inp)
</code></pre>

dejanbatanjac: <p>I would first introduce that you are migrating from Chainer to PyTorch to add a context of your question.</p>
<aside class="quote no-group" data-username="Safa_Mathlouthi" data-post="3" data-topic="58417">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/ac91a4/48.png" class="avatar"> Safa_Mathlouthi:</div>
<blockquote>
<pre><code class="lang-auto">File "train.py", line 94, in main
    optimizer.setup(vrn)
</code></pre>
</blockquote>
</aside>
<p>I don’t have experience with Chainer, but in PyTorch you we don’t have <code>setup </code>method on an optimizer, so you would need to remove what you have and create <a href="https://pytorch.org/docs/stable/optim.html#torch.optim.Adam" rel="noopener nofollow ugc">Adam optimizer in Pytorch</a> something like this:</p>
<pre><code class="lang-auto">from torch.optim import *
o = Adam(model.parameters(), lr=lr)
</code></pre>
<p>And in general you go step by step resolving all other errors.</p>