lxtGH / DecoupleSegNets

[ECCV-2020]: Improving Semantic Segmentation via Decoupled Body and Edge Supervision
370 stars 36 forks source link

TypeError: forward() got an unexpected keyword argument 'x' #31

Closed wenbeier closed 2 years ago

wenbeier commented 3 years ago

I run demo**.py with some images,this error occur:

Traceback (most recent call last): File "/media/wan/bei/2021-5/DeSegNets/demo/demo_folder_decouple.py", line 68, in pred, edge, body = net(x=img_tensor.unsqueeze(0).cuda(), img_num=img_id) File "/home/wan/anaconda3/envs/leida/lib/python3.6/site-packages/torch/nn/modules/module.py", line 889, in _call_impl result = self.forward(*input, kwargs) File "/home/wan/anaconda3/envs/leida/lib/python3.6/site-packages/torch/nn/parallel/data_parallel.py", line 165, in forward return self.module(*inputs[0], *kwargs[0]) File "/home/wan/anaconda3/envs/leida/lib/python3.6/site-packages/torch/nn/modules/module.py", line 889, in _call_impl result = self.forward(input, kwargs) TypeError: forward() got an unexpected keyword argument 'x'

My pytorch v1.8.

Stone-SL commented 3 years ago

Hi, have you solved this problem? I have the same problem

wenbeier commented 3 years ago

Hi, have you solved this problem? I have the same problem

No. We can discuss this probelm, what's your contact?

adwiii commented 3 years ago

I am also facing this issue using pytorch v1.8. Any solution?

The line also appears to be expecting 3 outputs from the call to the net:

 pred, edge, body = net(x=img_tensor.unsqueeze(0).cuda(), img_num=img_id)

None of the network implementations that I see in the repository return more than one output, and no network takes keyword arguments x or img_num so there appears to be no straightforward solution.

I replaced the offending line with:

pred = net(img_tensor.unsqueeze(0).cuda(), img_id)

and removed all references to edge and body. The code now runs to completion using this checkpoint, but the results are far from expected. Below is a representative example from Cityscapes: image

adwiii commented 3 years ago

Update, I had not changed the architecture. It now produces sensible results when running with --arch network.gffnets.DeepWV3PlusGFFNet and using this checkpoint, along with changing the block to:

for img_id, img_name in enumerate(images):
    img_dir = os.path.join(data_dir, img_name)
    img = Image.open(img_dir).convert('RGB')
    img_tensor = img_transform(img)

    # predict
    with torch.no_grad():
        # pred, edge, body = net(img_tensor.unsqueeze(0).cuda(), img_id)
        pred = net(img_tensor.unsqueeze(0).cuda(), img_id)
        print(pred.shape)
        logging.info('%04d/%04d: Inference done.' % (img_id + 1, len(images)))

    # final mask
    pred = pred.cpu().numpy().squeeze()
    pred = np.argmax(pred, axis=0)

    # final mask
    color_name = 'color_mask_' + img_name
    overlap_name = 'overlap_' + img_name

    # save colorized predictions
    colorized = args.dataset_cls.colorize_mask(pred)
    colorized.save(os.path.join(args.save_dir, color_name))

    # save colorized predictions overlapped on original images
    overlap = cv2.addWeighted(np.array(img), 0.5, np.array(colorized.convert('RGB')), 0.5, 0)
    cv2.imwrite(os.path.join(args.save_dir, overlap_name), overlap[:, :, ::-1])

Here is an example generated color_mask: image

lxtGH commented 2 years ago

@adwiii Thanks for your help. The issue is closed.