PaddlePaddle / PaddleSeg

Easy-to-use image segmentation library with awesome pre-trained model zoo, supporting wide-range of practical tasks in Semantic Segmentation, Interactive Segmentation, Panoptic Segmentation, Image Matting, 3D Segmentation, etc.
https://arxiv.org/abs/2101.06175
Apache License 2.0
8.68k stars 1.68k forks source link

getting raw logits from trained model #3610

Closed snachi2s closed 8 months ago

snachi2s commented 10 months ago

问题确认 Search before asking

请提出你的问题 Please ask your question

Hi, I trained Unet++ model with my custom dataset and I want to do some post-processing over the predicted masks. Is there any way to get the predictions (in the form of logits or arrays) during inference?

Following is the code I used for API inferencing,

from paddleseg.models import unet_plusplus
import paddle 
import paddleseg.transforms as T

model = unet_plusplus.UNetPlusPlus(
    num_classes=4, 
    in_channels=3, 
    use_deconv=False, 
    pretrained=None,
    align_corners=False,
    is_ds=True)

model_path = 'trained_logs/unet++/best_model/model.pdparams'
image_list = ['data/image.png']

if model_path:
    para_state_dict = paddle.load(model_path)  
    model.set_dict(para_state_dict)            # Load parameters
    print('Loaded trained params of model successfully')
else:
    raise ValueError('The model_path is wrong: {}'.format(model_path))

transforms = T.Compose([
    T.Resize(target_size=(256,256)),
    T.Normalize()
])

from paddleseg.core import predict
predict(
    model,
    model_path=model_path,
    transforms=transforms,
    image_list=image_list,
    save_dir='best_unet++_model',
)

Thanks!!

shiyutang commented 8 months ago

You can modify the predict.py file and get the pred logit from here.https://github.com/PaddlePaddle/PaddleSeg/blob/8ab72b7ec284f211fa9dbc401da486a3e16fb95c/paddleseg/core/predict.py#L118