-
Notifications
You must be signed in to change notification settings - Fork 91
/
test_processor.cc
124 lines (100 loc) · 4.73 KB
/
test_processor.cc
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include <vector>
#include <tuple>
#include <fstream>
#include <filesystem>
#include "gtest/gtest.h"
#include "ortx_cpp_helper.h"
#include "shared/api/image_processor.h"
using namespace ort_extensions;
const char* test_image_paths[] = {"data/processor/standard_s.jpg", "data/processor/australia.jpg", "data/processor/exceltable.png"};
const size_t test_image_count = sizeof(test_image_paths) / sizeof(test_image_paths[0]);
TEST(ProcessorTest, TestPhi3VImageProcessing) {
auto [input_data, n_data] = ort_extensions::LoadRawImages(test_image_paths, test_image_count);
// {"data/processor/standard_s.jpg", "data/processor/australia.jpg", "data/processor/exceltable.png"});
auto proc = OrtxObjectPtr<ImageProcessor>(OrtxCreateProcessor, "data/processor/phi_3_image.json");
ortc::Tensor<float>* pixel_values;
ortc::Tensor<int64_t>* image_sizes;
ortc::Tensor<int64_t>* num_img_tokens;
auto [status, r] = proc->PreProcess(ort_extensions::span(input_data.get(), (size_t)n_data), &pixel_values,
&image_sizes, &num_img_tokens);
ASSERT_TRUE(status.IsOk());
int64_t expected_image_size[] = {1344, 1344, 1008, 1344, 1008, 1680};
int64_t expected_num_token[] = {2509, 1921, 2353};
ASSERT_EQ(pixel_values->Shape(), std::vector<int64_t>({3, 17, 3, 336, 336}));
ASSERT_EQ(image_sizes->Shape(), std::vector<int64_t>({3, 2}));
ASSERT_EQ(num_img_tokens->Shape(), std::vector<int64_t>({3, 1}));
// compare the image sizes
for (size_t i = 0; i < 3; i++) {
ASSERT_EQ(image_sizes->Data()[i * 2], expected_image_size[i * 2]);
ASSERT_EQ(image_sizes->Data()[i * 2 + 1], expected_image_size[i * 2 + 1]);
ASSERT_EQ(num_img_tokens->Data()[i], expected_num_token[i]);
}
proc->ClearOutputs(&r);
}
TEST(ProcessorTest, TestCLIPImageProcessing) {
OrtxObjectPtr<OrtxRawImages> raw_images{};
extError_t err = OrtxLoadImages(ort_extensions::ptr(raw_images), test_image_paths, test_image_count, nullptr);
ASSERT_EQ(err, kOrtxOK);
OrtxObjectPtr<OrtxProcessor> processor;
err = OrtxCreateProcessor(ort_extensions::ptr(processor), "data/processor/clip_image.json");
if (err != kOrtxOK) {
std::cout << "Error: " << OrtxGetLastErrorMessage() << std::endl;
}
ASSERT_EQ(err, kOrtxOK);
OrtxObjectPtr<OrtxTensorResult> result;
err = OrtxImagePreProcess(processor.get(), raw_images.get(), ort_extensions::ptr(result));
ASSERT_EQ(err, kOrtxOK);
OrtxTensor* tensor;
err = OrtxTensorResultGetAt(result.get(), 0, &tensor);
ASSERT_EQ(err, kOrtxOK);
const float* data{};
const int64_t* shape{};
size_t num_dims;
err = OrtxGetTensorData(tensor, reinterpret_cast<const void**>(&data), &shape, &num_dims);
ASSERT_EQ(err, kOrtxOK);
ASSERT_EQ(num_dims, 4);
}
TEST(ProcessorTest, TestMLlamaImageProcessing) {
OrtxObjectPtr<OrtxRawImages> raw_images{};
extError_t err = OrtxLoadImages(ort_extensions::ptr(raw_images), test_image_paths, test_image_count, nullptr);
ASSERT_EQ(err, kOrtxOK);
OrtxObjectPtr<OrtxProcessor> processor;
err = OrtxCreateProcessor(ort_extensions::ptr(processor), "data/processor/mllama/llama_3_image.json");
if (err != kOrtxOK) {
std::cout << "Error: " << OrtxGetLastErrorMessage() << std::endl;
}
ASSERT_EQ(err, kOrtxOK);
OrtxObjectPtr<OrtxTensorResult> result;
err = OrtxImagePreProcess(processor.get(), raw_images.get(), ort_extensions::ptr(result));
ASSERT_EQ(err, kOrtxOK);
OrtxTensor* tensor;
err = OrtxTensorResultGetAt(result.get(), 0, &tensor);
ASSERT_EQ(err, kOrtxOK);
const float* data{};
const int64_t* shape{};
size_t num_dims;
err = OrtxGetTensorData(tensor, reinterpret_cast<const void**>(&data), &shape, &num_dims);
ASSERT_EQ(err, kOrtxOK);
ASSERT_EQ(num_dims, 5);
err = OrtxTensorResultGetAt(result.get(), 1, &tensor);
ASSERT_EQ(err, kOrtxOK);
const int64_t* int_data{};
err = OrtxGetTensorData(tensor, reinterpret_cast<const void**>(&int_data), &shape, &num_dims);
ASSERT_EQ(err, kOrtxOK);
ASSERT_EQ(num_dims, 2);
ASSERT_EQ(std::vector<int64_t>(int_data, int_data + 3), std::vector<int64_t>({6, 6, 1}));
err = OrtxTensorResultGetAt(result.get(), 2, &tensor);
ASSERT_EQ(err, kOrtxOK);
err = OrtxGetTensorData(tensor, reinterpret_cast<const void**>(&int_data), &shape, &num_dims);
ASSERT_EQ(err, kOrtxOK);
ASSERT_EQ(num_dims, 2);
ASSERT_EQ(std::vector<int64_t>(shape, shape + num_dims), std::vector<int64_t>({3, 4}));
err = OrtxTensorResultGetAt(result.get(), 3, &tensor);
ASSERT_EQ(err, kOrtxOK);
err = OrtxGetTensorData(tensor, reinterpret_cast<const void**>(&int_data), &shape, &num_dims);
ASSERT_EQ(err, kOrtxOK);
ASSERT_EQ(num_dims, 2);
ASSERT_EQ(std::vector<int64_t>(int_data, int_data + 3), std::vector<int64_t>({4, 4, 1}));
}