TypeError: __init__() missing 1 required positional argument: 'num_features'

I was trying to denoise image using Deep Image prior. when I use ResNet as an architecture i am getting error.

INPUT = 'noise' # 'meshgrid'           get_noise function
pad = 'reflection'
OPT_OVER = 'net' # 'net,input'

reg_noise_std = 1./30. # set to 1./20. for sigma=50
LR = 0.01

OPTIMIZER='adam' # 'LBFGS'
show_every = 100
exp_weight=0.99


num_iter = 1000
input_depth = 3 
figsize = 4 
    
    
net = get_net(input_depth, 'ResNet', pad, upsample_mode='bilinear').type(dtype)


    
net_input = get_noise(input_depth, INPUT, (img_pil.size[1], img_pil.size[0])).type(dtype).detach()

# Compute number of parameters
s  = sum([np.prod(list(p.size())) for p in net.parameters()]); 
print ('Number of params: %d' % s)

# Loss
mse = torch.nn.MSELoss().type(dtype)

img_noisy_torch = np_to_torch(img_noisy_np).type(dtype)

the error:

TypeError                                 Traceback (most recent call last)
ipython-input-7-7c96e1404ffa in module()
     16 
     17 
--- 18 net = get_net(input_depth, 'ResNet', pad, upsample_mode='bilinear').type(dtype)
     19 
     20 

2 frames
/content/models/common.py in act(act_fun)
     90             assert False
     91     else:
--- 92         return act_fun()
     93 
     94 

TypeError: __init__() missing 1 required positional argument: 'num_features'

But get_net clearly doesn't have any num_features as an argument. you can see the source of get_net below.

def get_net(input_depth, NET_TYPE, pad, upsample_mode, n_channels=3, act_fun='LeakyReLU', skip_n33d=128, skip_n33u=128, skip_n11=4, num_scales=5, downsample_mode='stride'):
    if NET_TYPE == 'ResNet':
        # TODO
        net = ResNet(input_depth, 3, 10, 16, 1, nn.BatchNorm2d, False)
    elif NET_TYPE == 'skip':
        net = skip(input_depth, n_channels, num_channels_down = [skip_n33d]*num_scales if isinstance(skip_n33d, int) else skip_n33d,
                                            num_channels_up =   [skip_n33u]*num_scales if isinstance(skip_n33u, int) else skip_n33u,
                                            num_channels_skip = [skip_n11]*num_scales if isinstance(skip_n11, int) else skip_n11, 
                                            upsample_mode=upsample_mode, downsample_mode=downsample_mode,
                                            need_sigmoid=True, need_bias=True, pad=pad, act_fun=act_fun)

    elif NET_TYPE == 'texture_nets':
        net = get_texture_nets(inp=input_depth, ratios = [32, 16, 8, 4, 2, 1], fill_noise=False,pad=pad)

    elif NET_TYPE =='UNet':
        net = UNet(num_input_channels=input_depth, num_output_channels=3, 
                   feature_scale=4, more_layers=0, concat_x=False,
                   upsample_mode=upsample_mode, pad=pad, norm_layer=nn.BatchNorm2d, need_sigmoid=True, need_bias=True)
    elif NET_TYPE == 'identity':
        assert input_depth == 3
        net = nn.Sequential()
    else:
        assert False

    return net

where I am doing wrong in Deep Image Prior code.

Topic image noise pytorch model-selection deep-learning

Category Data Science


The issue is in get_net function. RestNet model is not defined correctly, try with the below code snippet.

def get_net(input_depth, NET_TYPE, pad, upsample_mode, n_channels=3, act_fun='LeakyReLU', skip_n33d=128, skip_n33u=128, skip_n11=4, num_scales=5, downsample_mode='stride'):
if NET_TYPE == 'ResNet':
    # TODO
    #net = ResNet(input_depth, 3, 10, 16, True,'LeakyReLU',True, nn.BatchNorm2d,'reflection')
    net = ResNet(
                num_input_channels=input_depth,
                num_output_channels=3,
                num_blocks=10,
                num_channels=16,
                need_residual=True,
                act_fun='LeakyReLU',
                need_sigmoid=True,
                norm_layer=nn.BatchNorm2d,
                pad='reflection')
elif NET_TYPE == 'skip':
    net = skip(input_depth, n_channels, num_channels_down = [skip_n33d]*num_scales if isinstance(skip_n33d, int) else skip_n33d,
                                        num_channels_up =   [skip_n33u]*num_scales if isinstance(skip_n33u, int) else skip_n33u,
                                        num_channels_skip = [skip_n11]*num_scales if isinstance(skip_n11, int) else skip_n11, 
                                        upsample_mode=upsample_mode, downsample_mode=downsample_mode,
                                        need_sigmoid=True, need_bias=True, pad=pad, act_fun=act_fun)

elif NET_TYPE == 'texture_nets':
    net = get_texture_nets(inp=input_depth, ratios = [32, 16, 8, 4, 2, 1], fill_noise=False,pad=pad)

elif NET_TYPE =='UNet':
    net = UNet(num_input_channels=input_depth, num_output_channels=3,feature_scale=4, more_layers=0, concat_x=False,upsample_mode=upsample_mode, pad=pad, norm_layer=nn.BatchNorm2d, need_sigmoid=True, need_bias=True)
elif NET_TYPE == 'identity':
    assert input_depth == 3
    net = nn.Sequential()
else:
    assert False

return net

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.