-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_imagenet.cpp
224 lines (171 loc) · 6.43 KB
/
node_imagenet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <ros/ros.h>
#include <sensor_msgs/Image.h>
#include <vision_msgs/Classification2D.h>
#include <vision_msgs/VisionInfo.h>
#include <jetson-inference/imageNet.h>
#include <jetson-utils/cudaMappedMemory.h>
#include "image_converter.h"
#include <unordered_map>
// globals
imageNet* net = NULL;
imageConverter* cvt = NULL;
ros::Publisher* classify_pub = NULL;
vision_msgs::VisionInfo info_msg;
// callback triggered when a new subscriber connected to vision_info topic
void info_connect( const ros::SingleSubscriberPublisher& pub )
{
ROS_INFO("new subscriber '%s' connected to vision_info topic '%s', sending VisionInfo msg", pub.getSubscriberName().c_str(), pub.getTopic().c_str());
pub.publish(info_msg);
}
// callback triggered when recieved a new image on input topic
void img_callback( const sensor_msgs::ImageConstPtr& input )
{
// convert the image to reside on GPU
if( !cvt || !cvt->Convert(input) )
{
ROS_INFO("failed to convert %ux%u %s image", input->width, input->height, input->encoding.c_str());
return;
}
// classify the image
float confidence = 0.0f;
const int img_class = net->Classify(cvt->ImageGPU(), cvt->GetWidth(), cvt->GetHeight(), &confidence);
// verify the output
if( img_class >= 0 )
{
ROS_INFO("classified image, %f %s (class=%i)", confidence, net->GetClassDesc(img_class), img_class);
// create the classification message
vision_msgs::Classification2D msg;
vision_msgs::ObjectHypothesis obj;
obj.id = img_class;
obj.score = confidence;
msg.results.push_back(obj); // TODO optionally add source image to msg
// publish the classification message
classify_pub->publish(msg);
}
else
{
// an error occurred if the output class is < 0
ROS_ERROR("failed to classify %ux%u image", input->width, input->height);
}
}
// node main loop
int main(int argc, char **argv)
{
ros::init(argc, argv, "imagenet");
ros::NodeHandle nh;
ros::NodeHandle private_nh("~");
/*
* retrieve parameters
*/
std::string class_labels_path;
std::string prototxt_path;
std::string model_path;
std::string model_name;
bool use_model_name = false;
// determine if custom model paths were specified
if( !private_nh.getParam("prototxt_path", prototxt_path) ||
!private_nh.getParam("model_path", model_path) ||
!private_nh.getParam("class_labels_path", class_labels_path) )
{
// without custom model, use one of the built-in pretrained models
private_nh.param<std::string>("model_name", model_name, "googlenet");
use_model_name = true;
}
/*
* load image recognition network
*/
if( use_model_name )
{
// determine which built-in model was requested
imageNet::NetworkType model = imageNet::NetworkTypeFromStr(model_name.c_str());
if( model == imageNet::CUSTOM )
{
ROS_ERROR("invalid built-in pretrained model name '%s', defaulting to googlenet", model_name.c_str());
model = imageNet::GOOGLENET;
}
// create network using the built-in model
net = imageNet::Create(model);
}
else
{
// create network using custom model paths
net = imageNet::Create(prototxt_path.c_str(), model_path.c_str(), NULL, class_labels_path.c_str());
}
if( !net )
{
ROS_ERROR("failed to load imageNet model");
return 0;
}
/*
* create the class labels parameter vector
*/
std::hash<std::string> model_hasher; // hash the model path to avoid collisions on the param server
std::string model_hash_str = std::string(net->GetModelPath()) + std::string(net->GetClassPath());
const size_t model_hash = model_hasher(model_hash_str);
ROS_INFO("model hash => %zu", model_hash);
ROS_INFO("hash string => %s", model_hash_str.c_str());
// obtain the list of class descriptions
std::vector<std::string> class_descriptions;
const uint32_t num_classes = net->GetNumClasses();
for( uint32_t n=0; n < num_classes; n++ )
class_descriptions.push_back(net->GetClassDesc(n));
// create the key on the param server
std::string class_key = std::string("class_labels_") + std::to_string(model_hash);
private_nh.setParam(class_key, class_descriptions);
// populate the vision info msg
std::string node_namespace = private_nh.getNamespace();
ROS_INFO("node namespace => %s", node_namespace.c_str());
info_msg.database_location = node_namespace + std::string("/") + class_key;
info_msg.database_version = 0;
info_msg.method = net->GetModelPath();
ROS_INFO("class labels => %s", info_msg.database_location.c_str());
/*
* create an image converter object
*/
cvt = new imageConverter();
if( !cvt )
{
ROS_ERROR("failed to create imageConverter object");
return 0;
}
/*
* advertise publisher topics
*/
ros::Publisher pub = private_nh.advertise<vision_msgs::Classification2D>("classification", 5);
classify_pub = &pub; // we need to publish from the subscriber callback
// the vision info topic only publishes upon a new connection
ros::Publisher info_pub = private_nh.advertise<vision_msgs::VisionInfo>("vision_info", 1, (ros::SubscriberStatusCallback)info_connect);
/*
* subscribe to image topic
*/
//image_transport::ImageTransport it(nh); // BUG - stack smashing on TX2?
//image_transport::Subscriber img_sub = it.subscribe("image", 1, img_callback);
ros::Subscriber img_sub = private_nh.subscribe("image_in", 5, img_callback);
/*
* wait for messages
*/
ROS_INFO("imagenet node initialized, waiting for messages");
ros::spin();
return 0;
}