That error is almost never about your frame.astype(np.float32) / 255.0 line — it’s about PyTorch not recognizing the NumPy C-API type it’s being handed.
In practice, this happens when your environment has a NumPy mismatch / duplicate install (very commonly: conda NumPy + pip NumPy, or NumPy 2.x sitting next to a 1.x numpy-base). PyTorch then sees something that prints like numpy.ndarray, but it isn’t the exact ABI/type it expects, so torch.from_numpy() throws the super-confusing:
TypeError: expected np.ndarray (got numpy.ndarray)
This exact symptom has been reported in PyTorch issue threads and often traces back to mixed NumPy installs. citeturn1view0
Run these in the same environment you run run.py:
python -c "import numpy, torch; print('numpy', numpy.__version__, numpy.__file__); print('torch', torch.__version__)"
pip show numpy | sed -n '1,8p'
python -c "import pkgutil; print([m.module_finder for m in pkgutil.iter_modules() if m.name=='numpy'][:5])"
If you’re in conda, also:
conda list | egrep '^(numpy|numpy-base|torch)\s'
If you see both numpy and numpy-base with different major versions (or numpy coming from pip inside conda), that’s your culprit (this pattern shows up in real repro environments). citeturn1view0
Do this (cleanest):
pip uninstall -y numpy # run twice in case it was installed multiple ways
conda install -y "numpy<2" # or a specific 1.26.x
If you prefer strict pinning:
conda install -y numpy=1.26.4
(NumPy 1.26.x is widely compatible with many torch builds; the key is “one NumPy, not two”.)
Reinstall NumPy cleanly:
pip install --no-cache-dir "numpy<2"
Then re-test:
python -c "import numpy, torch; a=numpy.array([1,2,3]); print(torch.from_numpy(a))"
Right before the failing line, add a quick debug print:
x = transform({'image': frame_list[frame_id+i].astype(np.float32) / 255.0})['image']
print(type(x), getattr(x, "dtype", None), getattr(x, "shape", None))
- If
type(x) is already a torch.Tensor, then you should remove torch.from_numpy(...) and just do x.unsqueeze(...).
- If it’s a NumPy array, proceed with the environment fix above.
This copies data (slower, more memory), but sometimes bypasses the strict NumPy bridge:
cur = transform({'image': ...})['image']
cur = torch.tensor(cur) # copies
I’d treat this as a temporary workaround — the environment mismatch will likely bite elsewhere.
If you paste the output of:
python -c "import numpy, torch; print(numpy.__version__, numpy.__file__); print(torch.__version__)"
conda list | egrep '^(numpy|numpy-base|torch)\s' (if using conda)
…I can tell you exactly which package(s) to remove/pin.