1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-30 07:43:03 +04:00

Merge pull request #28837 from varun-jaiswal17:gemma3-tokenizer

Add Gemma3 tokenizer support for dnn #28837

- Adds Gemma3 tokenizer support
- Implements character-level BPE
- Adds 6 tests covering English, phrase, mixed case, numbers, special tokens, and encode/decode
- add gemma3_inference.py

Merge with: 
- **Companion PR**  : https://github.com/opencv/opencv_extra/pull/1346
- forward pass bug in gemma3_inference.py : https://github.com/opencv/opencv/pull/28836

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
Varun Jaiswal
2026-04-29 12:30:27 +05:30
committed by GitHub
parent c83b86eb57
commit faf34f95af
10 changed files with 565 additions and 11 deletions
+47
View File
@@ -1685,6 +1685,53 @@ TEST(Core_InputOutput, FileStorage_json_bool)
fs.release();
}
TEST(Core_InputOutput, FileStorage_json_unicode_escape)
{
// Test \uXXXX Unicode escape sequences in JSON strings
std::string test = R"({
"ascii": "\u0041\u0042\u0043",
"copyright": "\u00A9",
"chinese": "\u4E2D",
"emoji_base": "\u263A",
"mixed": "Hello \u4E16\u754C"
})";
FileStorage fs(test, FileStorage::READ | FileStorage::MEMORY);
// Test ASCII characters (\u0041=A, \u0042=B, \u0043=C)
ASSERT_TRUE(fs["ascii"].isString());
ASSERT_EQ((std::string)fs["ascii"], "ABC");
// Test 2-byte UTF-8 character (\u00A9=©)
ASSERT_TRUE(fs["copyright"].isString());
std::string copyright_str = (std::string)fs["copyright"];
ASSERT_EQ(copyright_str.size(), 2u); // © is 2 bytes in UTF-8
ASSERT_EQ((unsigned char)copyright_str[0], 0xC2);
ASSERT_EQ((unsigned char)copyright_str[1], 0xA9);
// Test 3-byte UTF-8 character (\u4E2D=中)
ASSERT_TRUE(fs["chinese"].isString());
std::string chinese_str = (std::string)fs["chinese"];
ASSERT_EQ(chinese_str.size(), 3u); // 中 is 3 bytes in UTF-8
ASSERT_EQ((unsigned char)chinese_str[0], 0xE4);
ASSERT_EQ((unsigned char)chinese_str[1], 0xB8);
ASSERT_EQ((unsigned char)chinese_str[2], 0xAD);
// Test another 3-byte character (\u263A=☺)
ASSERT_TRUE(fs["emoji_base"].isString());
std::string emoji_str = (std::string)fs["emoji_base"];
ASSERT_EQ(emoji_str.size(), 3u);
ASSERT_EQ((unsigned char)emoji_str[0], 0xE2);
ASSERT_EQ((unsigned char)emoji_str[1], 0x98);
ASSERT_EQ((unsigned char)emoji_str[2], 0xBA);
// Test mixed ASCII and Unicode
ASSERT_TRUE(fs["mixed"].isString());
std::string mixed = (std::string)fs["mixed"];
ASSERT_EQ(mixed.substr(0, 6), "Hello "); // First 6 chars are "Hello "
fs.release();
}
TEST(Core_InputOutput, FileStorage_free_file_after_exception)
{
const std::string fileName = cv::tempfile("FileStorage_free_file_after_exception_test.yml");