Yesterday was mighty nightmarish in the life of this developer. I had trained a conv-net meant to classify an object I was trying to recognize and later on manipulate using vision-based control. Since
PYTORCH had tensor computation with strong GPU acceleration and differential backprop capabilities based on the
torch auto-grad system, I took advantage of its python compatibility since it would mean I could easily write my control code in
roscpp and publish vision/control topics that reduces interoperability issues when working with different Linux processes. Only that I didn’t anticipate
Python 2 and
Python 3 module import problems way ahead of time. I would give more background below.
For the record, I run
ROS 1.x (indigo bare bones) on a
ubuntu 14.04 machine with a 32GB RAM. The
pytorch developers encourage users to install
conda and typically use
python 2 will be phased out in the near future. So, I had been using
pytorch in a
conda environment that both had a
python 2 and
python 3 environment. I could easily switch environments by turning on or off whichever python version I wanted. For details on how to do this, see this doc from the folks at conda.
So far, everything was working great. For
ros applications that does not involve image processing classes such as
CvBridge, I was able to get
pytorch to talk in
python3 being unofficially supported for
ROS 1.x (see this github wiki). Getting this to work involves pip installing the necessary
ros dependencies in
python3 using this requirements.txt file. This github repo page shows how I do this.
Anyways, so I trained a conv net model in
pytorch, no big deal. I had a
roscpp node in running on a different workstation, but within the same
ros network broadcasting
RGB images on a designated topic. Given what I know, it should be easy subscribing to the image topic and forwarding the video stream through the pre-trained neural network model to obtain classification results. But boy was I wrong.
When you install
conda, it typically places the installation relative to your
anaconda install path. For me this was in
/home/$USER/anaconda3. So to be able to import
Torch and use
CvBridge class simultaneously, I installed the following modules:
pip while in the
conda environment. Then I tried to import the convnet model from a different module’s class into a
rospy module I had written.
to be able to import
CvBridgesimultaneously, I installed the following modules:
convnet.py model had entries like so:
import torch import torch.nn as nn class ResNet(object): def __init__(self, args, **kwargs) def convModel(self, arg1, arg2): ''' define some conv models ''' def forward(self, x): ''' do stuff with conv layers ''' return self.fc(prev_layer(x))
process_images.py file had an import statement like so
from convnet import ResNet ''' do stuff with imported model '''
I got weird errors like
>> Python 3.6.0 (default, Oct 26 2016, 20:30:19) [GCC 4.8.4] on linux2 Type "help", "copyright", "credits" or "license" for more information. >> No module named Torch
Huh? Country boy comes to town. But the
convnet.py model imports Torch okay. I figured the problem must be because I installed
pytorch with the
python3 version. And so I pulled the
python2 version of
pytorch from Soumith’s channel.
Now when I import, it says stuff like
convnet module xx compiled with a different Torch version. What the heck
At this moment, I stepped out for a walk, and caught a brainchild. What if I do away with the
conda build of
pytorch and instead install
pytorch from source or
It turns out that this is the most error-less prone way to import
pytorch models into a
rospy file or indeed a
python2 file. To do this, I temporarily moved my
anaconda3 folder out of
bash’s native path, pulled the latest
pytorch commit from github and then installed with
python setup.py install.
Now when I try out the above commands, everything works well.
It turns out that this is the most error-less prone way to import Pytorch models into a rospy file or indeed a python2 file. To do this, I temporarily moved my
anaconda3folder out of bash’s native path, pulled the latest pytorch commit from github and then installed with
python setup.py install.
So my two cents to the robotics community running neural net models in
tensorflow and using such models in
rospy or equivalent environments is to always go for the source installation whenever and if possible. You would save yourself a lot of headache and time-waste.