Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(perception_utils): add classification util function with string #2090

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "autoware_auto_perception_msgs/msg/object_classification.hpp"

#include <string>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious.

Where is this function with string argument used?
I'm not sure why the label is used as string in the implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deep learning module sometime has string config like CenterPoint, so this case need for converter.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if having class names as a string is good or not, but I got it. Thank you.

#include <vector>

namespace perception_utils
Expand Down Expand Up @@ -92,6 +93,82 @@ inline bool isLargeVehicle(const std::vector<ObjectClassification> & object_clas
auto highest_prob_label = getHighestProbLabel(object_classifications);
return isLargeVehicle(highest_prob_label);
}

inline uint8_t toLabel(const std::string & class_name)
{
if (class_name == "UNKNOWN") {
return ObjectClassification::UNKNOWN;
} else if (class_name == "CAR") {
return ObjectClassification::CAR;
} else if (class_name == "TRUCK") {
return ObjectClassification::TRUCK;
} else if (class_name == "BUS") {
return ObjectClassification::BUS;
} else if (class_name == "TRAILER") {
return ObjectClassification::TRAILER;
} else if (class_name == "MOTORCYCLE") {
return ObjectClassification::MOTORCYCLE;
} else if (class_name == "BICYCLE") {
return ObjectClassification::BICYCLE;
} else if (class_name == "PEDESTRIAN") {
return ObjectClassification::PEDESTRIAN;
} else {
throw std::runtime_error("Invalid Classification label.");
}
}

inline ObjectClassification toObjectClassification(
const std::string & class_name, float probability)
{
ObjectClassification classification;
classification.label = toLabel(class_name);
classification.probability = probability;
return classification;
}

inline std::vector<ObjectClassification> toObjectClassifications(
const std::string & class_name, float probability)
{
std::vector<ObjectClassification> classifications;
classifications.push_back(toObjectClassification(class_name, probability));
return classifications;
}

inline std::string convertLabelToString(const uint8_t label)
{
if (label == ObjectClassification::UNKNOWN) {
return "UNKNOWN";
} else if (label == ObjectClassification::CAR) {
return "CAR";
} else if (label == ObjectClassification::TRUCK) {
return "TRUCK";
} else if (label == ObjectClassification::BUS) {
return "BUS";
} else if (label == ObjectClassification::TRAILER) {
return "TRAILER";
} else if (label == ObjectClassification::MOTORCYCLE) {
return "MOTORCYCLE";
} else if (label == ObjectClassification::BICYCLE) {
return "BICYCLE";
} else if (label == ObjectClassification::PEDESTRIAN) {
return "PEDESTRIAN";
} else {
return "UNKNOWN";
}
}

inline std::string convertLabelToString(const ObjectClassification object_classification)
{
return convertLabelToString(object_classification.label);
}

inline std::string convertLabelToString(
const std::vector<ObjectClassification> object_classifications)
{
auto highest_prob_label = getHighestProbLabel(object_classifications);
return convertLabelToString(highest_prob_label);
}

} // namespace perception_utils

#endif // PERCEPTION_UTILS__OBJECT_CLASSIFICATION_HPP_
69 changes: 69 additions & 0 deletions common/perception_utils/test/src/test_object_classification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,72 @@ TEST(object_classification, test_getHighestProbClassification)
EXPECT_NEAR(classification.probability, 0.8, epsilon);
}
}

TEST(object_classification, test_fromString)
{
using autoware_auto_perception_msgs::msg::ObjectClassification;
using perception_utils::toLabel;
using perception_utils::toObjectClassification;
using perception_utils::toObjectClassifications;

// toLabel
{
EXPECT_EQ(toLabel("UNKNOWN"), ObjectClassification::UNKNOWN);
EXPECT_EQ(toLabel("CAR"), ObjectClassification::CAR);
EXPECT_EQ(toLabel("TRUCK"), ObjectClassification::TRUCK);
EXPECT_EQ(toLabel("BUS"), ObjectClassification::BUS);
EXPECT_EQ(toLabel("TRAILER"), ObjectClassification::TRAILER);
EXPECT_EQ(toLabel("MOTORCYCLE"), ObjectClassification::MOTORCYCLE);
EXPECT_EQ(toLabel("BICYCLE"), ObjectClassification::BICYCLE);
EXPECT_EQ(toLabel("PEDESTRIAN"), ObjectClassification::PEDESTRIAN);
EXPECT_THROW(toLabel(""), std::runtime_error);
}

// Classification
{
auto classification = toObjectClassification("CAR", 0.7);
EXPECT_EQ(classification.label, ObjectClassification::CAR);
EXPECT_NEAR(classification.probability, 0.7, epsilon);
}
// Classifications
{
auto classifications = toObjectClassifications("CAR", 0.7);
EXPECT_EQ(classifications.at(0).label, ObjectClassification::CAR);
EXPECT_NEAR(classifications.at(0).probability, 0.7, epsilon);
}
}

TEST(object_classification, test_convertLabelToString)
{
using autoware_auto_perception_msgs::msg::ObjectClassification;
using perception_utils::convertLabelToString;

// from label
{
EXPECT_EQ(convertLabelToString(ObjectClassification::UNKNOWN), "UNKNOWN");
EXPECT_EQ(convertLabelToString(ObjectClassification::CAR), "CAR");
EXPECT_EQ(convertLabelToString(ObjectClassification::TRUCK), "TRUCK");
EXPECT_EQ(convertLabelToString(ObjectClassification::BUS), "BUS");
EXPECT_EQ(convertLabelToString(ObjectClassification::TRAILER), "TRAILER");
EXPECT_EQ(convertLabelToString(ObjectClassification::MOTORCYCLE), "MOTORCYCLE");
EXPECT_EQ(convertLabelToString(ObjectClassification::BICYCLE), "BICYCLE");
EXPECT_EQ(convertLabelToString(ObjectClassification::PEDESTRIAN), "PEDESTRIAN");
}

// from ObjectClassification
{
auto classification = createObjectClassification(ObjectClassification::CAR, 0.8);

EXPECT_EQ(convertLabelToString(classification), "CAR");
}

// from ObjectClassifications
{
std::vector<autoware_auto_perception_msgs::msg::ObjectClassification> classifications;
classifications.push_back(createObjectClassification(ObjectClassification::CAR, 0.5));
classifications.push_back(createObjectClassification(ObjectClassification::TRUCK, 0.8));
classifications.push_back(createObjectClassification(ObjectClassification::BUS, 0.7));

EXPECT_EQ(convertLabelToString(classifications), "TRUCK");
}
}