Skip to content

Commit dbe77df

Browse files
refactor: C++ consts to SCREAMING_SNAKE_CASE. (#515)
## Description Previously, the constants in C++ code were named using camelCase, which were also used for standard variables. Not only it was weird and uncommon practice, but also could easily be very misleading and confusing. For instance, some function arguments in the `DetectorUtils` file shared names with defined constants, making it very difficult to distinguish between them. I was deciding between usage of SCREAMING_SNAKE_CASE and k-prefix approach, which is sometimes recommended in modern c++ (see for example [google guidelines](https://google.github.io/styleguide/cppguide.html#Constant_Names)). Disadvantage of SCREAMING_CASE_SNAKE that is commonly mentioned, is that it looks exactly the same as preprocessor macro definition, but in our case I don't believe it is confusing, because we do not really use C macros. I have decided that SCREAMING_SNAKE_CASE fits better our purposes, it is widely recognized and easily distinguishable from other variables. ### Introduces a breaking change? - [ ] Yes - [x] No ### Type of change - [ ] Bug fix (change which fixes an issue) - [ ] New feature (change which adds functionality) - [ ] Documentation update (improves or adds clarity to existing documentation) - [x] Other (chores, tests, code style improvements etc.) ### Tested on - [x] iOS - [ ] Android ### Testing instructions <!-- Provide step-by-step instructions on how to test your changes. Include setup details if necessary. --> ### Screenshots <!-- Add screenshots here, if applicable --> ### Related issues Closes #501 ### Checklist - [ ] I have performed a self-review of my code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have updated the documentation accordingly - [ ] My changes generate no new warnings ### Additional notes <!-- Include any additional information, assumptions, or context that reviewers might need to understand this PR. -->
1 parent b53f3ee commit dbe77df

19 files changed

Lines changed: 86 additions & 83 deletions

File tree

packages/react-native-executorch/common/rnexecutorch/host_objects/JsiConversions.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,10 @@ inline jsi::Value getJsiValue(const std::vector<Detection> &detections,
369369
bbox.setProperty(runtime, "y2", detections[i].y2);
370370

371371
detection.setProperty(runtime, "bbox", bbox);
372-
detection.setProperty(runtime, "label",
373-
jsi::String::createFromAscii(
374-
runtime, cocoLabelsMap.at(detections[i].label)));
372+
detection.setProperty(
373+
runtime, "label",
374+
jsi::String::createFromAscii(runtime,
375+
COCO_LABELS_MAP.at(detections[i].label)));
375376
detection.setProperty(runtime, "score", detections[i].score);
376377
array.setValueAtIndex(runtime, i, detection);
377378
}

packages/react-native-executorch/common/rnexecutorch/models/classification/Classification.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,21 @@ Classification::postprocess(const Tensor &tensor) {
4949
static_cast<const float *>(tensor.const_data_ptr()), tensor.numel());
5050
std::vector<float> resultVec(resultData.begin(), resultData.end());
5151

52-
if (resultVec.size() != imagenet1k_v1_labels.size()) {
52+
if (resultVec.size() != IMAGENET1K_V1_LABELS.size()) {
5353
char errorMessage[100];
5454
std::snprintf(
5555
errorMessage, sizeof(errorMessage),
5656
"Unexpected classification output size, was expecting: %zu classes "
5757
"but got: %zu classes",
58-
imagenet1k_v1_labels.size(), resultVec.size());
58+
IMAGENET1K_V1_LABELS.size(), resultVec.size());
5959
throw std::runtime_error(errorMessage);
6060
}
6161

6262
numerical::softmax(resultVec);
6363

6464
std::unordered_map<std::string_view, float> probs;
6565
for (std::size_t cl = 0; cl < resultVec.size(); ++cl) {
66-
probs[imagenet1k_v1_labels[cl]] = resultVec[cl];
66+
probs[IMAGENET1K_V1_LABELS[cl]] = resultVec[cl];
6767
}
6868

6969
return probs;

packages/react-native-executorch/common/rnexecutorch/models/classification/Constants.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <string_view>
55

66
namespace rnexecutorch {
7-
inline constexpr std::array<std::string_view, 1000> imagenet1k_v1_labels = {
7+
inline constexpr std::array<std::string_view, 1000> IMAGENET1K_V1_LABELS = {
88
"tench, Tinca tinca",
99
"goldfish, Carassius auratus",
1010
"great white shark, white shark, man-eater, man-eating shark, Carcharodon "

packages/react-native-executorch/common/rnexecutorch/models/image_segmentation/Constants.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <string_view>
55

66
namespace rnexecutorch {
7-
inline constexpr std::array<std::string_view, 21> deeplabv3_resnet50_labels = {
7+
inline constexpr std::array<std::string_view, 21> DEEPLABV3_RESNET50_LABELS = {
88
"BACKGROUND", "AEROPLANE", "BICYCLE", "BIRD", "BOAT",
99
"BOTTLE", "BUS", "CAR", "CAT", "CHAIR",
1010
"COW", "DININGTABLE", "DOG", "HORSE", "MOTORBIKE",

packages/react-native-executorch/common/rnexecutorch/models/image_segmentation/ImageSegmentation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ std::shared_ptr<jsi::Object> ImageSegmentation::postprocess(
101101
auto buffersToReturn = std::make_shared<std::unordered_map<
102102
std::string_view, std::shared_ptr<OwningArrayBuffer>>>();
103103
for (std::size_t cl = 0; cl < numClasses; ++cl) {
104-
if (classesOfInterest.contains(deeplabv3_resnet50_labels[cl])) {
105-
(*buffersToReturn)[deeplabv3_resnet50_labels[cl]] = resultClasses[cl];
104+
if (classesOfInterest.contains(DEEPLABV3_RESNET50_LABELS[cl])) {
105+
(*buffersToReturn)[DEEPLABV3_RESNET50_LABELS[cl]] = resultClasses[cl];
106106
}
107107
}
108108

packages/react-native-executorch/common/rnexecutorch/models/image_segmentation/ImageSegmentation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class ImageSegmentation : public BaseModel {
3636
std::shared_ptr<OwningArrayBuffer>>>
3737
classesToOutput);
3838

39-
static constexpr std::size_t numClasses{deeplabv3_resnet50_labels.size()};
39+
static constexpr std::size_t numClasses{DEEPLABV3_RESNET50_LABELS.size()};
4040
cv::Size modelImageSize;
4141
std::size_t numModelPixels;
4242
};

packages/react-native-executorch/common/rnexecutorch/models/object_detection/Constants.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
#include <unordered_map>
55

66
namespace rnexecutorch {
7-
const std::unordered_map<int, std::string> cocoLabelsMap = {
7+
inline constexpr float IOU_THRESHOLD = 0.55;
8+
9+
inline const std::unordered_map<int, std::string> COCO_LABELS_MAP = {
810
{1, "PERSON"}, {2, "BICYCLE"}, {3, "CAR"},
911
{4, "MOTORCYCLE"}, {5, "AIRPLANE"}, {6, "BUS"},
1012
{7, "TRAIN"}, {8, "TRUCK"}, {9, "BOAT"},

packages/react-native-executorch/common/rnexecutorch/models/object_detection/Utils.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "Utils.h"
2+
#include "Constants.h"
23

34
namespace rnexecutorch {
45
float intersectionOverUnion(const Detection &a, const Detection &b) {
@@ -48,7 +49,7 @@ std::vector<Detection> nonMaxSuppression(std::vector<Detection> detections) {
4849
std::remove_if(labelDetections.begin(), labelDetections.end(),
4950
[&current](const Detection &other) {
5051
return intersectionOverUnion(current, other) >
51-
iouThreshold;
52+
IOU_THRESHOLD;
5253
}),
5354
labelDetections.end());
5455
}

packages/react-native-executorch/common/rnexecutorch/models/object_detection/Utils.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ struct Detection {
1212
float score;
1313
};
1414

15-
inline constexpr float iouThreshold = 0.55;
16-
1715
float intersectionOverUnion(const Detection &a, const Detection &b);
1816
std::vector<Detection> nonMaxSuppression(std::vector<Detection> detections);
1917
} // namespace rnexecutorch

packages/react-native-executorch/common/rnexecutorch/models/ocr/Constants.h

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,30 @@
55

66
namespace rnexecutorch::ocr {
77

8-
inline constexpr float textThreshold = 0.4;
9-
inline constexpr float textThresholdVertical = 0.3;
10-
inline constexpr float linkThreshold = 0.4;
11-
inline constexpr float lowTextThreshold = 0.7;
12-
inline constexpr float centerThreshold = 0.5;
13-
inline constexpr float distanceThreshold = 2.0;
14-
inline constexpr float heightThreshold = 2.0;
15-
inline constexpr float singleCharacterCenterThreshold = 0.3;
16-
inline constexpr float lowConfidenceThreshold = 0.3;
17-
inline constexpr float adjustContrast = 0.2;
18-
inline constexpr int32_t minSideThreshold = 15;
19-
inline constexpr int32_t maxSideThreshold = 30;
20-
inline constexpr int32_t recognizerHeight = 64;
21-
inline constexpr int32_t largeRecognizerWidth = 512;
22-
inline constexpr int32_t mediumRecognizerWidth = 256;
23-
inline constexpr int32_t smallRecognizerWidth = 128;
24-
inline constexpr int32_t smallVerticalRecognizerWidth = 64;
25-
inline constexpr int32_t maxWidth =
26-
largeRecognizerWidth + (largeRecognizerWidth * 0.15);
27-
inline constexpr int32_t minSize = 20;
28-
inline constexpr int32_t singleCharacterMinSize = 70;
29-
inline constexpr int32_t recognizerImageSize = 1280;
30-
inline constexpr int32_t verticalLineThreshold = 20;
8+
inline constexpr float TEXT_THRESHOLD = 0.4;
9+
inline constexpr float TEXT_THRESHOLD_VERTICAL = 0.3;
10+
inline constexpr float LINK_THRESHOLD = 0.4;
11+
inline constexpr float LOW_TEXT_THRESHOLD = 0.7;
12+
inline constexpr float CENTER_THRESHOLD = 0.5;
13+
inline constexpr float DISTANCE_THRESHOLD = 2.0;
14+
inline constexpr float HEIGHT_THRESHOLD = 2.0;
15+
inline constexpr float SINGLE_CHARACTER_CENTER_THRESHOLD = 0.3;
16+
inline constexpr float LOW_CONFIDENCE_THRESHOLD = 0.3;
17+
inline constexpr float ADJUST_CONTRAST = 0.2;
18+
inline constexpr int32_t MIN_SIDE_THRESHOLD = 15;
19+
inline constexpr int32_t MAX_SIDE_THRESHOLD = 30;
20+
inline constexpr int32_t RECOGNIZER_HEIGHT = 64;
21+
inline constexpr int32_t LARGE_RECOGNIZER_WIDTH = 512;
22+
inline constexpr int32_t MEDIUM_RECOGNIZER_WIDTH = 256;
23+
inline constexpr int32_t SMALL_RECOGNIZER_WIDTH = 128;
24+
inline constexpr int32_t SMALL_VERTICAL_RECOGNIZER_WIDTH = 64;
25+
inline constexpr int32_t MAX_WIDTH =
26+
LARGE_RECOGNIZER_WIDTH + (LARGE_RECOGNIZER_WIDTH * 0.15);
27+
inline constexpr int32_t SINGLE_CHARACTER_MIN_SIZE = 70;
28+
inline constexpr int32_t RECOGNIZER_IMAGE_SIZE = 1280;
29+
inline constexpr int32_t VERTICAL_LINE_THRESHOLD = 20;
3130

32-
inline const cv::Scalar mean(0.485, 0.456, 0.406);
33-
inline const cv::Scalar variance(0.229, 0.224, 0.225);
31+
inline const cv::Scalar MEAN(0.485, 0.456, 0.406);
32+
inline const cv::Scalar VARIANCE(0.229, 0.224, 0.225);
3433

3534
} // namespace rnexecutorch::ocr

0 commit comments

Comments
 (0)