Table of Contents
This project was created for the 2nd Smart Cities Robotics Challenge (SciRoc) 2021. One of our team's tasks was detecting people and objects via a mounted camera on the robot. Thus creating a ros package that utilizes YOLOv5.
In order to use this package follow the next steps.
Yolov5 uses python 3.8. Check your python version using
python -V
To install python 3.8 go here. The installation of the dependencied is done using anaconda. Installing dependencies with pip is also tested and working.
The installation of the dependencies is done using anaconda. Installing dependencies with pip is also tested and working.
- Create a new virtual environment named venv
conda create -n venv python=3.8 jupyter
- Activate new virtual environment
conda activate venv
- Install pytorch. Insert your specifications for os (windows/linux), your package manager (e.g. conda/pip etc) and Compute Platform (cpu/gpu etc). Copy the installation command next to Run this command to your terminal and run it.
- Install other dependencies
sudo apt-get install python3-pip python3-yaml
pip3 install rospkg catkin_pkg
cd /path/to/ros_yolov5
pip3 install -r requirements.txt
Modify the shebang at the top of ros_yolov5.py file at ros_yolov5/scripts/ros_yolov5.py
and ros_yolov5/scripts/process_detections.py
Example
Default:
#!/path/to/venv/bin/python3
Modified:
#!/home/ronnie_coleman/venv/bin/python
or
#!/home/ronnie_coleman/miniconda3/envs/venv/bin/python
Clone the repository to your workspace.
mkdir -p "yolo_ws/src" && cd $_
git clone https://github.com/uniwa-gravastars/yolov5-rospackage.git
cd yolov5-rospackage
git submodule init
git submodule update
Then compile the package and source the workspace
cd ..
catkin_make -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3
To change the weights that Yolov5 uses, open ros_yolov5.launch
on ros_yolov5/launch/ros_yolov5.launch
and change the value of the parameter weights_path to point to your pt file. Moreover change the source_topic
to the correct camera topic.
The YOLOv5 ros package is now ready. Open a terminal, source the workspace and run:
roslaunch ros_yolov5 ros_yolov5.launch
!!ATTENTION!!
If you come across this error:
[ERROR] [1661788501.044783]: bad callback: <bound method ObjectDetector.image_callback of <__main__.ObjectDetector object at 0x7f011fe2bdc0>>
Traceback (most recent call last):
File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/topics.py", line 750, in _invoke_callback
cb(msg)
File "/home/user/catkin_ws/src/yolov5-rospackage/ros_yolov5/scripts/ros_yolov5.py", line 46, in image_callback
results = self.model(img, size=640)
File "/home/user/miniconda3/envs/venv/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1110, in _call_impl
return forward_call(*input, **kwargs)
File "/home/user/miniconda3/envs/venv/lib/python3.8/site-packages/torch/autograd/grad_mode.py", line 27, in decorate_context
return func(*args, **kwargs)
File "/home/user/catkin_ws/src/yolov5-rospackage/ros_yolov5/yolov5/models/common.py", line 278, in forward
y = self.model(x, augment, profile)[0] # forward
File "/home/user/miniconda3/envs/venv/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1110, in _call_impl
return forward_call(*input, **kwargs)
File "/home/user/catkin_ws/src/yolov5-rospackage/ros_yolov5/yolov5/models/yolo.py", line 122, in forward
return self.forward_once(x, profile, visualize) # single-scale inference, train
File "/home/user/catkin_ws/src/yolov5-rospackage/ros_yolov5/yolov5/models/yolo.py", line 153, in forward_once
x = m(x) # run
File "/home/user/miniconda3/envs/venv/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1110, in _call_impl
return forward_call(*input, **kwargs)
File "/home/user/miniconda3/envs/venv/lib/python3.8/site-packages/torch/nn/modules/upsampling.py", line 154, in forward
recompute_scale_factor=self.recompute_scale_factor)
File "/home/user/miniconda3/envs/venv/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1185, in __getattr__
raise AttributeError("'{}' object has no attribute '{}'".format(
AttributeError: 'Upsample' object has no attribute 'recompute_scale_factor'
This error is from the internal code of pytorch. The "forward" function is not working properly.
To fix this, replace in miniconda3/envs/venv/lib/python3.8/site-packages/torch/nn/modules/upsampling.py
:
def forward(self, input: Tensor) -> Tensor:
return F.interpolate(input, self.size, self.scale_factor, self.mode, self.align_corners,
recompute_scale_factor=self.recompute_scale_factor)
with:
def forward(self, input: Tensor) -> Tensor:
return F.interpolate(input, self.size, self.scale_factor, self.mode, self.align_corners)
Find fix here.
Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with an appropriate tag.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE.txt
for more information.
Special thanks to the original author of this work Kaloterakis Evangelos.