Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

How to preprocess a single image for inference #444

Answered by SarBH
SarBH asked this question in Q&A
Discussion options

Is there a simple way to prepare a single image to go through a trained model and output the mask?

Context:
I went through your CamVid car segmentation example. Now I have a trained model.
If I wanted to simply load the model to run inference on a single example without having to create a torch.utils.data.Dataset and Dataloader, whats the best way to do this?

I was hoping to do:

preprocessing_fn = smp.encoders.get_preprocessing_fn(args.encoder, args.encoder_weights)
processed = preprocessing_fn(img)

You must be logged in to vote

UPDATE: if we want the preprocessing function to behave as i described processed = preprocessing_fn(img) you can use

    image = cv2.imread(args.sample_input)
    comp = get_preprocessing(preprocessing_fn)
    x = comp(image=image)
def get_preprocessing(preprocessing_fn):
    """Construct preprocessing transform
    
    Args:
        preprocessing_fn (callbale): data normalization function 
            (can be specific for each pretrained neural network)
    Return:
        transform: albumentations.Compose
    
    """
    
    _transform = [
        albu.Lambda(image=preprocessing_fn),
        albu.Lambda(image=to_tensor, mask=to_tensor),
    ]
    return albu.Compose(_transform)

Replies: 1 comment · 2 replies

Comment options

UPDATE: if we want the preprocessing function to behave as i described processed = preprocessing_fn(img) you can use

    image = cv2.imread(args.sample_input)
    comp = get_preprocessing(preprocessing_fn)
    x = comp(image=image)
def get_preprocessing(preprocessing_fn):
    """Construct preprocessing transform
    
    Args:
        preprocessing_fn (callbale): data normalization function 
            (can be specific for each pretrained neural network)
    Return:
        transform: albumentations.Compose
    
    """
    
    _transform = [
        albu.Lambda(image=preprocessing_fn),
        albu.Lambda(image=to_tensor, mask=to_tensor),
    ]
    return albu.Compose(_transform)

You must be logged in to vote
2 replies
@YoniChechik
Comment options

I believe this is incorrect. totensor also normalizes the image (see: https://pytorch.org/vision/stable/generated/torchvision.transforms.ToTensor.html#torchvision.transforms.ToTensor), which you dont want to do because the preprocessing_fn already does this.

@tatyana-pichugina
Comment options

Be careful,cv2 trap. cv2.imread(args.sample_input) by default reads BGR instead RGB.

Answer selected by SarBH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.