UserWarning after loading network

In the process of testing the health of the model , in the console I see a warning message, although everything works correctly.

Loading network.....
Network successfully loaded
/home/y700/Env/yolo/lib/python3.6/site-packages/torch/nn/modules/upsampling.py:129: **UserWarning: nn.Upsample is deprecated. Use nn.functional.interpolate instead**.
  warnings.warn("nn.{} is deprecated. Use nn.functional.interpolate instead.".format(self.name))
dog-cycle-car.png    predicted in  0.111 seconds
Objects Detected:    bicycle truck dog

I changed my code from
upsample=nn.Upsample(scale_factor=2;mode="nearest")
to
upsample = nn.functional.interpolate(scale_factor=2, mode="nearest")
and got this

Loading network.....
Traceback (most recent call last):
  File "detector.py", line 63, in <module>
    model = Darknet(args.cfgfile)
  File "/home/y700/projects/yolo_start/darknet.py", line 164, in __init__
    self.net_info, self.module_list = create_modules(self.blocks)
  File "/home/y700/projects/yolo_start/darknet.py", line 109, in create_modules
    upsample = nn.functional.interpolate(scale_factor=2, mode="nearest")
**TypeError: interpolate() missing 1 required positional argument: 'input'**

After I added argument ‘input’ (but I do not know what this argument means, I just wrote it)
upsample = nn.functional.interpolate(input, scale_factor=2, mode="nearest")
and got this:

Loading network.....
Traceback (most recent call last):
  File "detector.py", line 63, in <module>
    model = Darknet(args.cfgfile)
  File "/home/y700/projects/yolo_start/darknet.py", line 164, in __init__
    self.net_info, self.module_list = create_modules(self.blocks)
  File "/home/y700/projects/yolo_start/darknet.py", line 109, in create_modules
    upsample = nn.functional.interpolate(input, scale_factor=2, mode="nearest")
  File "/home/y700/Env/yolo/lib/python3.6/site-packages/torch/nn/functional.py", line 2426, in interpolate
    if input.dim() == 3 and mode == 'nearest':
**AttributeError: 'builtin_function_or_method' object has no attribute 'dim'**

Now I have no idea what to do next. These messages do not affect the operation of the model, but I want cleanliness.

1 Like

Your first warning tells you that this feature was moved from a nn.Module approach to a nn.functional approach. The first one lets you instantiate layers as objects (modules), and then call them on some input later on, using their .forward() method. E.g:

interpolate = nn.Upsample(scale_factor=2, mode='nearest')
...
output = interpolate(input)

However, the nn.functional approach literally means that they are functions to be called directly! E.g:

output = nn.functional.interpolate(input, scale_factor=2, mode='nearest')

This means that you would not instantiate this kind of layer in the create_modules method of your darknet.

Unfortunately, my knowledge is not enough to understand what I need to do with outputs in my particular case.
I did

interpolate=nn.Upsample(scale_factor=2,mode="nearest")
output=interpolate(input)
output=nn.functional.interpolate(input,scale_factor=2,mode="nearest")

module.add_module("upsample_{}".format(index),output)

but the warning is same.
Could you help me write what needs to be changed in the conflict file https://github.com/Lepiloff/pyolo/blob/master/darknet.py?

Sure, but a few explanations first:

nn.Module: very practical for learnable layers (will handle backward pass automatically once you define the forward() method), but it needs to __init__ first, and then will be called in the forward(x) method with some input tensor x.

nn.functional: shortcut functions for simple layers mostly, particularly those without learnable layers (pooling, activation functions, and so on), which you can call directly on the input tensor x.

In your case, your code uses the nn.Module approach, which is to define the layers first (here in your code) and then use it later (here in your code).

What you need to change, if you want to remove the first UserWarning that you got, is to remove lines 104-109 completely, since they are useless in the nn.functional approach (I’m assuming that the comments on lines 104-105 are part of this layer, despite the indentation).

Then, modify lines 174-175 as follows:

if module_type == "convolutional":
    x = self.module_list[i](x)
elif module_type == "upsample":
    x = nn.functional.interpolate(x, scale_factor=2, mode="nearest")
1 Like

Alex, everything works without problems. Your knowledge is worthy of respect, thank you very much for the help!!!

1 Like