Skip to content

Segformer output not determinsitic #1222

@migperfer

Description

@migperfer

Hi! Thanks for the library.

I'm using a Segformer model, and the same input through the same model is giving me always two different outputs.
I leave one example here.

# Imports
import segmentation_models_pytorch as smp
from PIL.Image import open as open_image
from PIL.Image import FLIP_LEFT_RIGHT
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import random, torch
from pathlib import Path
import torch

random.seed(0)
np.random.seed(0)
torch.manual_seed(0)
torch.cuda.manual_seed_all(0)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
torch.use_deterministic_algorithms(True)

# ... Some other code
device = "cpu"
model = smp.Segformer(**model_params)
model.load_state_dict("my/checkpoint.ckpt").to(device)

image = open_image("some/path.png")
images = [image, image]  # The same image

with torch.inference_mode():        
        images_tensor =  torch.utils.data.default_collate([inference_transform(img) for img in images])
        assert torch.allclose(images_tensor[0], images_tensor[1])  # 1st assert
        mask_predictions = torch.sigmoid(self(images_tensor.to(model.device)))
        assert torch.allclose(mask_predictions[0], mask_predictions[1])  # 2nd assert

Assert # 1 does not raise any errors (the two elements being input to the network are really the same)
However, assert # 2 raises an error, as the outputs are not the same.

Any idea of what could be happening?
Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions