How do we customize conv_layer since it requires in/out channels which are dynamically used (notebook 09+)

I’m trying to do some testing to compare partial convolutional padding.
However, I’m unclear how we can readily modify the current conv_layer class, at least by passing in different arguments for the settings, as it requires ni, nf as the first two params, and those are dynamically generated…meaning you can’t supply them at init?

Specifically - from notebook9 - here’s the current code for a run:
learn,run = get_learn_run(nfs, data, 0.4, conv_layer, cbs=cbfs)

If I do even a simple change like turning off bn for the conv layer by adjusting the bn setting :
learn,run = get_learn_run(nfs, data, 0.4, conv_layer(bn=False), cbs=cbfs))

then it will err with ni/nf as positional arguments that are not set.
TypeError Traceback (most recent call last)
in
----> 1 learn,run = get_learn_run(nfs, data, 0.4, conv_layer2(bn=False), cbs=cbfs)

TypeError: conv_layer() missing 2 required positional arguments: ‘ni’ and 'nf’

But ni/nf are dynamically added as the input/outputs change each layer, so we can’t supply those directly at init time. See my little debug test here:

adding partial layer
ni = 3
nf = 16


adding partial layer
ni = 16
nf = 32


adding partial layer
ni = 32
nf = 64


adding partial layer
ni = 64
nf = 32


adding partial layer
ni = 32
nf = 64


adding partial layer
ni = 64
nf = 128


adding partial layer
ni = 128
nf = 256


Here’s the code for conv_layer (from notebook 7…took me a while to chase that definition down as conv_layer is used all over the place in many permutations, but 07 is the exported one):

def conv_layer(ni, nf, ks=3, stride=2, bn=True, **kwargs):
    layers = [nn.Conv2d(ni, nf, ks, padding=ks//2, stride=stride, bias=not bn),
              GeneralRelu(**kwargs)]
    if bn: layers.append(nn.BatchNorm2d(nf, eps=1e-5, momentum=0.1))
    return nn.Sequential(*layers)

thus, besides creating a new class with each modification (i.e. conv_layer_partial, conv_layer_reflection, etc), how do we pass different settings into the current conv_layer class on a per training basis?

All the examples so far just use ‘conv_layer’ with defaults and don’t adjust the settings from the passed in params.
So, it’s not clear how to do it and based on my initial testing, the dynamic ni/nf params seem to block one from passing in new settings, at least the way I would expect to do it at init time.

Thanks for any info!

You’ll need to use functools.partial.

1 Like

Thanks for the fast answer Jeremy!
I was pushing off learning/using the partials, but I’ll dig into it today then, and then try and setup the padding testing.

Much appreciated.

1 Like

I got it working now with partials :slight_smile:
Thanks again!