How to convert decoder with attribute initialization with dictionary

username: Wajahat_Akhtar
cooked: <p>I am trying to convert the decoder using torch.jit.script  but I am facing some error as below.</p>
<h2><strong>My module</strong></h2>
<pre><code class="lang-python">import numpy as np
import torch
import torch.nn as nn

from collections import OrderedDict
from layers import *

class DepthDecoder(nn.Module):
    def __init__(self, num_ch_enc, scales=range(4), num_output_channels=1, use_skips=True):
        super(DepthDecoder, self).__init__()

        self.num_output_channels = num_output_channels
        self.use_skips = use_skips
        self.upsample_mode = 'nearest'
        self.scales = scales

        self.num_ch_enc = num_ch_enc
        self.num_ch_dec = np.array([16, 32, 64, 128, 256])

        # decoder
        self.convs = OrderedDict()
        for i in range(4, -1, -1):
            # upconv_0
            num_ch_in = self.num_ch_enc[-1] if i == 4 else self.num_ch_dec[i + 1]
            num_ch_out = self.num_ch_dec[i]
            self.convs[("upconv", i, 0)] = ConvBlock(num_ch_in, num_ch_out)

            # upconv_1
            num_ch_in = self.num_ch_dec[i]
            if self.use_skips and i &gt; 0:
                num_ch_in += self.num_ch_enc[i - 1]
            num_ch_out = self.num_ch_dec[i]
            self.convs[("upconv", i, 1)] = ConvBlock(num_ch_in, num_ch_out)

        for s in self.scales:
            self.convs[("dispconv", s)] = Conv3x3(self.num_ch_dec[s], self.num_output_channels)

        self.decoder = nn.ModuleList(list(self.convs.values()))
        self.sigmoid = nn.Sigmoid()

    def forward(self, input_features):
        outputs = {}

        # decoder
        x = input_features[-1]
        for i in range(4, -1, -1):
            x = self.convs[("upconv", i, 0)](x)
            x = [upsample(x)]
            if self.use_skips and i &gt; 0:
                x += [input_features[i - 1]]
            x = torch.cat(x, 1)
            x = self.convs[("upconv", i, 1)](x)
            if i in self.scales:
                outputs[("disp", i)] = self.sigmoid(self.convs[("dispconv", i)](x))

        return outputs

num_enc_channels = np.array([ 64,  64, 128, 256, 512])
depth_decoder = DepthDecoder( num_ch_enc= num_enc_channels , scales=range(4))
traced_script_module_decoder = torch.jit.script(depth_decoder)
traced_script_module_decoder.save('new-decoder.pt')
</code></pre>
<p><strong>Error :</strong></p>
<pre><code class="lang-auto">  File "C:\Users\lib\site-packages\torch\jit\_recursive.py", line 259, in create_methods_from_stubs
    concrete_type._create_methods(defs, rcbs, defaults)
RuntimeError: 
Module 'DepthDecoder' has no attribute 'convs' (**This attribute exists on the Python module, but we failed to convert Python type: 'OrderedDict' to a TorchScript type**.):
  File "C:\Users\networks\depth_decoder.py", line 55
        x = input_features[-1]
        for i in range(4, -1, -1):
            x = self.convs[("upconv", i, 0)](x)
                ~~~~~~~~~~ &lt;--- HERE
            x = [upsample(x)]
            if self.use_skips and i &gt; 0:
</code></pre>

username: driazati
cooked: <p>You’ll have to make a couple changes that may be tricky. For dictionaries the only keys that are supported are <code>str</code>, <code>int</code>, and <code>float</code>, so using a tuple as the key won’t work in TorchScript. Since the <code>range(4, -1, -1)</code> you’re using to create <code>self.convs</code> is not going to change no matter how you initialize <code>DepthDecoder</code>, I’d recommend unrolling that loop and adding them as submodules to <code>DepthDecoder</code>. So something like:</p>
<pre><code class="lang-python">self.upconv_0_0 = ConvBlock(...)
self.upconv_0_1 = ConvBlock(...)
</code></pre>
<p>Unfortunately TorchScript is pretty picky about <code>nn.Module</code>s inside of containers, so things like <code>self.convs["upconv1"]</code> currently aren’t supported (but this is something we’re working on fixing).</p>