Implementing an SVM-ROS application

Following shows the aim of this project.

In this application we are going to classify a sensor data in three ways. The sensor values are assumed to be in between 0 to 30,000 and we are having a dataset which is having the sensor value mapping. For example, for a sensor value, you can assign the value belongs to 1, 2, or 3. To test the SVM, we are making another ROS node called virtual sensor node, which can publish value in between 0 to 30000. The trained SVM model can able to classify the virtual sensor value. This method can be adopted for any kind of sensors for classifying its data.

Before embedding SVM in ROS, here's some basic code in Python using sklearn to implement SVM.

The first thing is importing the sklearn and numpy modules. The sklearn module has the svm module, which is going to be used in this code, and numpy is used for making multi-dimensional arrays:

    from sklearn import svm 
    import numpy as np 

For training SVM, we need an input (predictor) and output (target); here, X is the input and y is the required output:

    X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]]) 
    y = np.array([1, 1, 2, 2]) 

After defining X and y, just create an instance of SVM Classification (SVC) object.

Feed X and y to the SVC object for training the model. After feeding X and y, we can feed an input that may not be in X, and it can predict the y value corresponding to the given input:

    model = svm.SVC(kernel='linear',C=1,gamma=1) 
    model.fit(X,y) 
    print(model.predict([[-0.8,-1]])) 

The preceding code will give an output of 1.

Now, we are going to implement a ROS application that does the same thing. Here, we are creating a virtual sensor node that can publish random values from 0 to 30,000. The ROS-SVM node will subscribe to those values and classify them using the preceding APIs. The learning in SVM is done from a CSV data file.

You can view the complete application package in section code; it's called ros_ml. Inside the ros_ml/scripts folder, you can see nodes such as ros_svm.py and virtual_sensor.py.

First, let's take a look at the virtual sensor node. The code is very simple and self-explanatory. It simply generates a random number between 0 and 30,000 and publishes it to the /sensor_read topic:

    #!/usr/bin/env python 
    import rospy 
    from std_msgs.msg import Int32 
    import random 
 
    def send_data(): 
         rospy.init_node('virtual_sensor', anonymous=True) 
      rospy.loginfo("Sending virtual sensor data") 
      pub = rospy.Publisher('sensor_read', Int32, queue_size=1) 
         rate = rospy.Rate(10) # 10hz 
 
         while not rospy.is_shutdown(): 
           sensor_reading = random.randint(0,30000) 
           pub.publish(sensor_reading) 
           rate.sleep() 
 
    if __name__ == '__main__': 
        try: 
            send_data() 
        except rospy.ROSInterruptException: 
            pass 

The next node is ros_svm.py. This node reads from a data file from a data folder inside the ros_ml package. The current data file is named pos_readings.csv, which contains the sensor and target values. Here is a snippet from that file:

    5125,5125,1 
    6210,6210,1 
    ............... 
 
    10125,10125,2 
    6410,6410,2 
    5845,5845,2 
    ................ 
 
    14325,14325,3 
    16304,16304,3 
    18232,18232,3 
    .................. 

The ros_svm.py node reads this file, trains the SVC, and predicts each value from the virtual sensor topic. The node has a class called Classify_Data(), which has methods to read the CSV file and train and predict it using scikit APIs.

We'll step through how these nodes are started:

Start roscore:

    $ roscore

Switch to the script folder of ros_ml:

    $ roscd ros_ml/scripts

Run the ROS SVM classifier node:

    $ python ros_svm.py

Run the virtual sensor in another Terminal:

    $ rosrun ros_ml virtual_sensor.py

Here is the output we get from the SVM node:

Figure 9: ROS - SVM node output
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.213.128