ValueError: WARNING ⚠️ torch.Tensor inputs should be BCHW i.e. shape(1, 3, 640, 640) divisible by stride 32. Input shape(1, 3, 720, 1280) is incompatible. #14128
Replies: 1 comment
-
@4833R11Y45 hello, Thank you for sharing the details of your issue. The error you're encountering is due to the input tensor shape not being compatible with the model's requirements. Specifically, YOLO models expect input tensors to have dimensions that are divisible by the model's stride, which is typically 32. To resolve this, you need to ensure that your input frames are resized to dimensions that meet this requirement. In your code, you are resizing the frames to Here's a revised version of your code snippet to ensure the input tensor shape is correct: import streamlit as st
import firebase_admin
from firebase_admin import credentials, db
import cv2
import tempfile
import folium
from streamlit_folium import folium_static
import torch
import numpy as np
from ultralytics import YOLO
def resize_frame(frame, size=(640, 640)):
"""Resize frame to specific dimensions."""
return cv2.resize(frame, size, interpolation=cv2.INTER_LINEAR)
if not firebase_admin._apps:
cred_path = r".json"
cred = credentials.Certificate(cred_path)
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://f.firebaseio.com/'
})
# Load YOLOv8 model and set device
model = YOLO(r"best.pt")
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model.to(device)
print(f'Using device: {device}')
st.title('UAV Vehicle Detection and Tracking')
video_file = st.file_uploader("Upload a video", type=["mp4", "avi", "mov"])
if video_file:
tfile = tempfile.NamedTemporaryFile(delete=False)
tfile.write(video_file.read())
video_path = tfile.name
st.video(video_path)
process_button = st.button('Detect Vehicles')
if process_button:
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
st.error("Failed to open video file!")
st.stop()
frame_rate = int(cap.get(cv2.CAP_PROP_FPS))
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out_path = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4').name
out = cv2.VideoWriter(out_path, fourcc, frame_rate, (640, 640))
stframe = st.empty()
while cap.isOpened():
success, frame = cap.read()
if not success:
break
frame = resize_frame(frame)
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Ensure the input tensor is in the correct shape
input_frame = torch.from_numpy(frame_rgb).permute(2, 0, 1).float().div(255.0).unsqueeze(0).to(device)
results = model(input_frame)
result_frame = results[0].plot()
if result_frame.dtype != np.uint8:
result_frame = (result_frame * 255).astype(np.uint8)
out.write(cv2.cvtColor(result_frame, cv2.COLOR_RGB2BGR))
stframe.image(result_frame, channels="RGB", use_column_width=True)
cap.release()
out.release()
st.video(out_path) Please ensure that your input tensor is correctly shaped and normalized before passing it to the model. Additionally, make sure you are using the latest version of the Ultralytics package to benefit from any recent bug fixes and improvements. If the issue persists, providing a minimum reproducible example can greatly help in diagnosing the problem. You can find more information on creating a reproducible example here. Feel free to reach out if you have any further questions or need additional assistance. 😊 |
Beta Was this translation helpful? Give feedback.
-
Code:
import streamlit as st
import firebase_admin
from firebase_admin import credentials, db
import cv2
import tempfile
import folium
from streamlit_folium import folium_static
import torch
import numpy as np
from ultralytics import YOLO
def resize_frame(frame, size=(640, 640)):
"""Resize frame to specific dimensions."""
return cv2.resize(frame, size, interpolation=cv2.INTER_LINEAR)
if not firebase_admin._apps:
cred_path = r".json"
cred = credentials.Certificate(cred_path)
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://f.firebaseio.com/'
})
Load YOLOv8 model and set device
model = YOLO(r"best.pt")
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model.to(device)
print(f'Using device: {device}')
st.title('UAV Vehicle Detection and Tracking')
video_file = st.file_uploader("Upload a video", type=["mp4", "avi", "mov"])
if video_file:
tfile = tempfile.NamedTemporaryFile(delete=False)
tfile.write(video_file.read())
video_path = tfile.name
st.video(video_path)
process_button = st.button('Detect Vehicles')
ValueError: WARNING ⚠ torch.Tensor inputs should be BCHW i.e. shape(1, 3, 640, 640) divisible by stride 32. Input shape(1, 3, 720, 1280) is incompatible.
Traceback:
File "C:\Users\abeer\anaconda3\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 589, in _run_script
exec(code, module.dict)
File "C:\FYP\Research\YOLOv8\app.py", line 62, in
results = model.predict(frame)
^^^^^^^^^^^^^^^^^^^^
File "C:\Users\abeer\anaconda3\Lib\site-packages\ultralytics\engine\model.py", line 442, in predict
return self.predictor.predict_cli(source=source) if is_cli else self.predictor(source=source, stream=stream)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\abeer\anaconda3\Lib\site-packages\ultralytics\engine\predictor.py", line 168, in call
return list(self.stream_inference(source, model, *args, **kwargs)) # merge list of Result into one
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\abeer\anaconda3\Lib\site-packages\torch\utils_contextlib.py", line 35, in generator_context
response = gen.send(None)
^^^^^^^^^^^^^^
File "C:\Users\abeer\anaconda3\Lib\site-packages\ultralytics\engine\predictor.py", line 226, in stream_inference
self.setup_source(source if source is not None else self.args.source)
File "C:\Users\abeer\anaconda3\Lib\site-packages\ultralytics\engine\predictor.py", line 198, in setup_source
self.dataset = load_inference_source(
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\abeer\anaconda3\Lib\site-packages\ultralytics\data\build.py", line 192, in load_inference_source
dataset = LoadTensor(source)
^^^^^^^^^^^^^^^^^^
File "C:\Users\abeer\anaconda3\Lib\site-packages\ultralytics\data\loaders.py", line 464, in init
self.im0 = self._single_check(im0)
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\abeer\anaconda3\Lib\site-packages\ultralytics\data\loaders.py", line 482, in _single_check
raise ValueError(s)
Beta Was this translation helpful? Give feedback.
All reactions