Skip to content

Commit b3c5834

Browse files
committed
Further refactor
1 parent 2fae648 commit b3c5834

File tree

9 files changed

+44
-83
lines changed

9 files changed

+44
-83
lines changed

packages/react-native-executorch/common/rnexecutorch/data_processing/ImageProcessing.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ cv::Mat convertTensorToMatrix(cv::Size size, const Tensor &tensor) {
175175
std::span<const float>(resultData, tensor.numel()), size);
176176
}
177177

178-
static cv::Size getTensorSize(const std::vector<int32_t> &tensorDims) {
178+
cv::Size getSizeOfImageFromTensorDims(const std::vector<int32_t> &tensorDims) {
179179
if (tensorDims.size() < 2) {
180180
throw std::runtime_error(
181181
"Unexpected tensor size, expected at least 2 dimentions but got: " +
@@ -185,14 +185,11 @@ static cv::Size getTensorSize(const std::vector<int32_t> &tensorDims) {
185185
return {tensorDims[tensorDims.size() - 1], tensorDims[tensorDims.size() - 2]};
186186
}
187187

188-
TensorPtr covertMatrixToTensor(const std::vector<int32_t> &tensorDims,
189-
cv::Mat &input) {
190-
191-
cv::Size tensorSize = getTensorSize(tensorDims);
188+
void adaptImageForTensor(const std::vector<int32_t> &tensorDims,
189+
cv::Mat &input) {
190+
cv::Size tensorSize = getSizeOfImageFromTensorDims(tensorDims);
192191
cv::resize(input, input, tensorSize);
193192
cv::cvtColor(input, input, cv::COLOR_BGR2RGB);
194-
195-
return getTensorFromMatrix(tensorDims, input);
196193
}
197194

198195
static cv::Scalar

packages/react-native-executorch/common/rnexecutorch/data_processing/ImageProcessing.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ cv::Mat readImageToMatrix(const std::string &imageURI);
3434
/// @brief Create an OpenCV matrix based on a tensor content
3535
cv::Mat convertTensorToMatrix(cv::Size size, const Tensor &tensor);
3636

37-
/// @brief Create a tensor based on an OpenCV matrix content. Please mind that
38-
/// for performance reasons matrix is passed by mutable reference
39-
TensorPtr covertMatrixToTensor(const std::vector<int32_t> &tensorDims,
40-
cv::Mat &input);
37+
cv::Size getSizeOfImageFromTensorDims(const std::vector<int32_t> &tensorDims);
38+
39+
/// @brief Resize and convert color of an image passed as matrix
40+
/// to fit tensor requirements
41+
void adaptImageForTensor(const std::vector<int32_t> &tensorDims,
42+
cv::Mat &input);
4143

4244
} // namespace rnexecutorch::image_processing

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,17 @@ Classification::Classification(const std::string &modelSource,
1616
throw std::runtime_error("Model seems to not take any input tensors.");
1717
}
1818
std::vector<int32_t> modelInputShape = inputShapes[0];
19-
if (modelInputShape.size() < 2) {
20-
char errorMessage[100];
21-
std::snprintf(errorMessage, sizeof(errorMessage),
22-
"Unexpected model input size, expected at least 2 dimentions "
23-
"but got: %zu.",
24-
modelInputShape.size());
25-
throw std::runtime_error(errorMessage);
26-
}
27-
modelImageSize = cv::Size(modelInputShape[modelInputShape.size() - 1],
28-
modelInputShape[modelInputShape.size() - 2]);
19+
modelImageSize =
20+
image_processing::getSizeOfImageFromTensorDims(modelInputShape);
2921
}
3022

3123
std::unordered_map<std::string_view, float>
3224
Classification::generate(std::string imageSource) {
3325
auto imageAsMatrix = image_processing::readImageToMatrix(imageSource);
34-
auto inputTensor = image_processing::covertMatrixToTensor(
35-
getAllInputShapes()[0], imageAsMatrix);
26+
const auto tensorDims = getAllInputShapes()[0];
27+
image_processing::adaptImageForTensor(tensorDims, imageAsMatrix);
28+
auto inputTensor =
29+
image_processing::getTensorFromMatrix(tensorDims, imageAsMatrix);
3630
auto forwardResult = BaseModel::forward(inputTensor);
3731
if (!forwardResult.ok()) {
3832
throw std::runtime_error(

packages/react-native-executorch/common/rnexecutorch/models/embeddings/image/ImageEmbeddings.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,17 @@ ImageEmbeddings::ImageEmbeddings(
1515
throw std::runtime_error("Model seems to not take any input tensors.");
1616
}
1717
std::vector<int32_t> modelInputShape = inputTensors[0];
18-
if (modelInputShape.size() < 2) {
19-
char errorMessage[100];
20-
std::snprintf(errorMessage, sizeof(errorMessage),
21-
"Unexpected model input size, expected at least 2 dimentions "
22-
"but got: %zu.",
23-
modelInputShape.size());
24-
throw std::runtime_error(errorMessage);
25-
}
26-
modelImageSize = cv::Size(modelInputShape[modelInputShape.size() - 1],
27-
modelInputShape[modelInputShape.size() - 2]);
18+
modelImageSize =
19+
image_processing::getSizeOfImageFromTensorDims(modelInputShape);
2820
}
2921

3022
std::shared_ptr<OwningArrayBuffer>
3123
ImageEmbeddings::generate(std::string imageSource) {
3224
auto imageAsMatrix = image_processing::readImageToMatrix(imageSource);
33-
auto inputTensor = image_processing::covertMatrixToTensor(
34-
getAllInputShapes()[0], imageAsMatrix);
25+
const auto tensorDims = getAllInputShapes()[0];
26+
image_processing::adaptImageForTensor(tensorDims, imageAsMatrix);
27+
auto inputTensor =
28+
image_processing::getTensorFromMatrix(tensorDims, imageAsMatrix);
3529

3630
auto forwardResult = BaseModel::forward(inputTensor);
3731
if (!forwardResult.ok()) {

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,8 @@ ImageSegmentation::ImageSegmentation(
2020
throw std::runtime_error("Model seems to not take any input tensors.");
2121
}
2222
std::vector<int32_t> modelInputShape = inputShapes[0];
23-
if (modelInputShape.size() < 2) {
24-
char errorMessage[100];
25-
std::snprintf(errorMessage, sizeof(errorMessage),
26-
"Unexpected model input size, expected at least 2 dimentions "
27-
"but got: %zu.",
28-
modelInputShape.size());
29-
throw std::runtime_error(errorMessage);
30-
}
31-
modelImageSize = cv::Size(modelInputShape[modelInputShape.size() - 1],
32-
modelInputShape[modelInputShape.size() - 2]);
23+
modelImageSize =
24+
image_processing::getSizeOfImageFromTensorDims(modelInputShape);
3325
numModelPixels = modelImageSize.area();
3426
}
3527

@@ -38,8 +30,10 @@ std::shared_ptr<jsi::Object> ImageSegmentation::generate(
3830
std::set<std::string, std::less<>> classesOfInterest, bool resize) {
3931
auto imageAsMatrix = image_processing::readImageToMatrix(imageSource);
4032
auto originalSize = imageAsMatrix.size();
41-
auto inputTensor = image_processing::covertMatrixToTensor(
42-
getAllInputShapes()[0], imageAsMatrix);
33+
const auto tensorDims = getAllInputShapes()[0];
34+
image_processing::adaptImageForTensor(tensorDims, imageAsMatrix);
35+
auto inputTensor =
36+
image_processing::getTensorFromMatrix(tensorDims, imageAsMatrix);
4337

4438
auto forwardResult = BaseModel::forward(inputTensor);
4539
if (!forwardResult.ok()) {

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,8 @@ ObjectDetection::ObjectDetection(
1313
throw std::runtime_error("Model seems to not take any input tensors.");
1414
}
1515
std::vector<int32_t> modelInputShape = inputTensors[0];
16-
if (modelInputShape.size() < 2) {
17-
char errorMessage[100];
18-
std::snprintf(errorMessage, sizeof(errorMessage),
19-
"Unexpected model input size, expected at least 2 dimentions "
20-
"but got: %zu.",
21-
modelInputShape.size());
22-
throw std::runtime_error(errorMessage);
23-
}
24-
modelImageSize = cv::Size(modelInputShape[modelInputShape.size() - 1],
25-
modelInputShape[modelInputShape.size() - 2]);
16+
modelImageSize =
17+
image_processing::getSizeOfImageFromTensorDims(modelInputShape);
2618
}
2719

2820
std::vector<types::Detection>
@@ -69,8 +61,10 @@ std::vector<types::Detection>
6961
ObjectDetection::generate(std::string imageSource, double detectionThreshold) {
7062
auto imageAsMatrix = image_processing::readImageToMatrix(imageSource);
7163
auto originalSize = imageAsMatrix.size();
72-
auto inputTensor = image_processing::covertMatrixToTensor(
73-
getAllInputShapes()[0], imageAsMatrix);
64+
const auto tensorDims = getAllInputShapes()[0];
65+
image_processing::adaptImageForTensor(tensorDims, imageAsMatrix);
66+
auto inputTensor =
67+
image_processing::getTensorFromMatrix(tensorDims, imageAsMatrix);
7468

7569
auto forwardResult = BaseModel::forward(inputTensor);
7670
if (!forwardResult.ok()) {

packages/react-native-executorch/common/rnexecutorch/models/ocr/Detector.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,8 @@ Detector::Detector(const std::string &modelSource,
1313
"Detector model seems to not take any input tensors.");
1414
}
1515
std::vector<int32_t> modelInputShape = inputShapes[0];
16-
if (modelInputShape.size() < 2) {
17-
throw std::runtime_error("Unexpected detector model input size, expected "
18-
"at least 2 dimensions but got: " +
19-
std::to_string(modelInputShape.size()) + ".");
20-
}
21-
modelImageSize = cv::Size(modelInputShape[modelInputShape.size() - 1],
22-
modelInputShape[modelInputShape.size() - 2]);
16+
modelImageSize =
17+
image_processing::getSizeOfImageFromTensorDims(modelInputShape);
2318
}
2419

2520
cv::Size Detector::getModelImageSize() const noexcept { return modelImageSize; }

packages/react-native-executorch/common/rnexecutorch/models/ocr/Recognizer.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@ Recognizer::Recognizer(const std::string &modelSource,
1515
throw std::runtime_error("Recognizer model has no input tensors.");
1616
}
1717
std::vector<int32_t> modelInputShape = inputShapes[0];
18-
if (modelInputShape.size() < 2) {
19-
throw std::runtime_error("Unexpected Recognizer model input shape.");
20-
}
21-
modelImageSize = cv::Size(modelInputShape[modelInputShape.size() - 1],
22-
modelInputShape[modelInputShape.size() - 2]);
18+
modelImageSize =
19+
image_processing::getSizeOfImageFromTensorDims(modelInputShape);
2320
}
2421

2522
std::pair<std::vector<int32_t>, float>

packages/react-native-executorch/common/rnexecutorch/models/style_transfer/StyleTransfer.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,8 @@ StyleTransfer::StyleTransfer(const std::string &modelSource,
1818
throw std::runtime_error("Model seems to not take any input tensors.");
1919
}
2020
std::vector<int32_t> modelInputShape = inputShapes[0];
21-
if (modelInputShape.size() < 2) {
22-
char errorMessage[100];
23-
std::snprintf(errorMessage, sizeof(errorMessage),
24-
"Unexpected model input size, expected at least 2 dimentions "
25-
"but got: %zu.",
26-
modelInputShape.size());
27-
throw std::runtime_error(errorMessage);
28-
}
29-
modelImageSize = cv::Size(modelInputShape[modelInputShape.size() - 1],
30-
modelInputShape[modelInputShape.size() - 2]);
21+
modelImageSize =
22+
image_processing::getSizeOfImageFromTensorDims(modelInputShape);
3123
}
3224

3325
std::string StyleTransfer::postprocess(const Tensor &tensor,
@@ -41,8 +33,10 @@ std::string StyleTransfer::postprocess(const Tensor &tensor,
4133
std::string StyleTransfer::generate(std::string imageSource) {
4234
auto imageAsMatrix = image_processing::readImageToMatrix(imageSource);
4335
auto originalSize = imageAsMatrix.size();
44-
auto inputTensor = image_processing::covertMatrixToTensor(
45-
getAllInputShapes()[0], imageAsMatrix);
36+
const auto tensorDims = getAllInputShapes()[0];
37+
image_processing::adaptImageForTensor(tensorDims, imageAsMatrix);
38+
auto inputTensor =
39+
image_processing::getTensorFromMatrix(tensorDims, imageAsMatrix);
4640

4741
auto forwardResult = BaseModel::forward(inputTensor);
4842
if (!forwardResult.ok()) {

0 commit comments

Comments
 (0)