Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pretrained model output bad segmentation result #31

Open
edvardHua opened this issue Aug 25, 2020 · 4 comments
Open

Pretrained model output bad segmentation result #31

edvardHua opened this issue Aug 25, 2020 · 4 comments

Comments

@edvardHua
Copy link

edvardHua commented Aug 25, 2020

According to Visualize_video.py, i wrote a infer.py for testing pretrained model located in result folder. But i found output result is terriable.

Below is the complete code of infer.py script.

import cv2
import json
import models
import argparse
import torch
import numpy as np

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-c', '--config', type=str)
    parser.add_argument('-ip', '--img_path', type=str)
    args = parser.parse_args()

    with open(args.config) as fin:
        config = json.load(fin)

    test_config = config['test_config']
    data_config = config['data_config']

    if test_config["loss"] == "Lovasz":
        test_config["num_classes"] = 1
        print("Use Lovasz loss ")
        Lovasz = True
    else:
        print("Use Cross Entropy loss ")
        Lovasz = False

    if test_config['Model'].startswith('Stage2'):
        model = models.__dict__[test_config["Model"]](classes=test_config["num_classes"],
                                                      p=test_config["p"], q=test_config["q"])
    elif test_config["Model"].startswith('Dnc_SINet'):
        model = models.__dict__[test_config["Model"]](
            classes=test_config["num_classes"], p=test_config["p"], q=test_config["q"],
            chnn=test_config["chnn"])

    mean = [107.304565, 115.69884, 132.35703]
    std = [63.97182, 65.1337, 68.29726]
    model.eval()

    image = cv2.imread(args.img_path)
    image = cv2.resize(image, (data_config['w'], data_config['h']))
    img = image.astype(np.float32)

    img = img.astype(np.float32)
    for j in range(3):
        img[:, :, j] -= mean[j]
    for j in range(3):
        img[:, :, j] /= std[j]

    img /= 255.
    img = img.transpose((2, 0, 1))
    img_tensor = torch.from_numpy(img)
    img_tensor = torch.unsqueeze(img_tensor, 0)

    with torch.no_grad():
        img_variable = torch.autograd.Variable(img_tensor)

        if torch.cuda.is_available():
            img_variable = img_variable.cuda()

        out = model(img_variable)

    classMap_numpy = (out[0].data.cpu() > 0).numpy()[0]
    idx_fg = (classMap_numpy == 1)
    seg_img = image * idx_fg[:, :, np.newaxis]
    cv2.imwrite("test.jpg", seg_img.astype(np.uint8))

And run with the following command

python3 infer.py -c setting/Test_ExtremeC3Net.json -ip /Users/edvardzeng/Dataset/PortraitDataset/Nukki/baidu_V1/input/43.png

Output result:

input

output

Did i missing something ?

Looking forward to your replay.

Thx.

@darius513
Copy link

@edvardHua hello, I use your code for a test, and I get the same bad result as you. Have you solved the problem? waiting for your reply

@YAwei666
Copy link

@edvardHua hello, I use your code for a test, and I get the same bad result as you. Have you solved the problem? waiting for your reply
have you solved this problem?

@RORO11
Copy link

RORO11 commented Mar 15, 2021

I also meet this problem.

@zhepherd
Copy link

miss load_state_dict? but add load_state_dict is error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants