pytorch / vision

Datasets, Transforms and Models specific to Computer Vision
https://pytorch.org/vision
BSD 3-Clause "New" or "Revised" License
16.1k stars 6.94k forks source link

inference gives arbitrary output from trained model on `np.ones` #8304

Closed innat closed 7 months ago

innat commented 7 months ago

🐛 Describe the bug

I tired to do some simple inference using torchvision.models.video.swin3d_t model with input of np.ones but each time I run the inference the output gets changed.

import torch
import numpy as np
import torchvision

torch_model = torchvision.models.video.swin3d_t(
    pretrained=True
) 

np_input = np.ones(((1, 3, 32, 224, 224))).astype('float32')
torch_input = torch.from_numpy(np_input)

def run():
    with torch.no_grad():
        out_pt = torch_model(torch_input)
        print(out_pt[0, :5])

run()
tensor([ 0.2096,  0.8295,  0.2413, -1.1546, -0.0913])

run()
tensor([ 0.0429,  0.8538,  0.3214, -1.2695,  0.0097])

run()
tensor([-0.0245,  0.6934,  0.5590, -1.1861,  0.2010])

Versions

torchvision.version == 0.16.0+cu121

NicolasHug commented 7 months ago

Hi @innat , use .eval() on the model. Also preferably use the weights= API:

from torchvision.models.video import Swin3D_T_Weights
torch_model = torchvision.models.video.swin3d_t(
    weights=Swin3D_T_Weights.KINETICS400_V1
).eval()