99using json = nlohmann::json;
1010
1111json read_json (const std::string& filename) {
12- std::ifstream ifs (filename, std::ifstream::binary );
12+ std::ifstream ifs (filename);
1313 if (!ifs.is_open ()) {
1414 throw std::runtime_error (" Failed to open JSON file: " + filename);
1515 }
1616
17- ifs.seekg (0 , std::ios::end);
18- size_t size = ifs.tellg ();
19- ifs.seekg (0 , std::ios::beg);
20-
21- std::vector<char > buffer (size);
22- ifs.read (buffer.data (), size);
23- ifs.close ();
24-
2517 json model_data;
2618 try {
27- model_data = json::parse (buffer. begin (), buffer. end ()) ;
19+ ifs >> model_data ;
2820 } catch (const json::parse_error& e) {
2921 throw std::runtime_error (" JSON parse error: " + std::string (e.what ()));
3022 }
@@ -42,74 +34,49 @@ void extract_values_from_json(const json& j, std::vector<float>& values) {
4234 }
4335}
4436
45- void extract_values_without_bias (const json& j, std::vector<float >& values) {
46- std::vector<float > temp_values;
47- extract_values_from_json (j, temp_values);
48- size_t bias_size = 0 ;
49- if (j.is_array () && !j.empty () && j.back ().is_array ()) {
50- bias_size = j.back ().size ();
37+ void parse_json_shape (const json& j, std::vector<size_t >& shape,
38+ size_t dim = 0 ) {
39+ if (!j.is_array ()) {
40+ if (dim == 0 ) shape.push_back (0 );
41+ return ;
5142 }
52- // std::cout << "Bias size: " << bias_size << std::endl;
53- if (temp_values .size () >= bias_size ) {
54- values. assign (temp_values. begin (), temp_values. end () - bias_size );
43+
44+ if (shape .size () <= dim ) {
45+ shape. push_back (j. size () );
5546 }
56- }
5747
58- void parse_json_shape (const json& j, std::vector<size_t >& shape, size_t dim) {
59- if (dim == 0 ) {
60- if (j.is_array ()) {
61- if (j.empty ()) {
62- shape.push_back (0 );
63- return ;
64- }
65- parse_json_shape (j.front (), shape, dim + 1 );
66- } else {
67- shape.push_back (0 );
68- }
69- } else {
70- if (j.is_array ()) {
71- if (shape.size () <= dim - 1 ) {
72- shape.push_back (j.size ());
73- }
74- if (!j.empty ()) {
75- parse_json_shape (j.front (), shape, dim + 1 );
76- }
77- }
48+ if (!j.empty ()) {
49+ parse_json_shape (j[0 ], shape, dim + 1 );
7850 }
7951}
8052
81- void extract_bias_from_json (const json& j, std::vector<float >& bias) {
82- if (j.is_array ()) {
83- if (!j.empty () && j.back ().is_array ()) {
84- for (const auto & item : j.back ()) {
85- if (item.is_number ()) {
86- bias.push_back (item.get <float >());
87- }
88- }
89- }
53+ Tensor create_tensor_from_json (const json& layer_data, Type type) {
54+ if (type != Type::kFloat ) {
55+ throw std::invalid_argument (" Only float type is supported" );
9056 }
91- }
9257
93- Tensor create_tensor_from_json (const json& j, Type type) {
94- if (type == Type::kFloat ) {
95- std::vector<float > vals;
96- std::vector<size_t > shape;
97- std::vector<float > bias;
98- extract_values_without_bias (j, vals);
99- // std::cout << "Extracted values size: " << vals.size() << std::endl;
58+ std::vector<float > weights;
59+ if (layer_data.contains (" weights" ) && !layer_data[" weights" ].empty ()) {
60+ extract_values_from_json (layer_data[" weights" ], weights);
61+ }
10062
101- parse_json_shape (j, shape, 0 );
102- // std::cout << "Shape: ";
103- /* for (size_t i = 0; i < shape.size() - 1; i++) {
104- std::cout << shape[i] << ", ";
105- }
106- std::cout << shape[shape.size() - 1];
107- std::cout << std::endl;*/
63+ // Извлекаем bias (если есть)
64+ std::vector<float > bias;
65+ if (layer_data.contains (" bias" ) && !layer_data[" bias" ].empty ()) {
66+ extract_values_from_json (layer_data[" bias" ], bias);
67+ }
10868
109- extract_bias_from_json (j, bias);
110- // std::cout << "Extracted bias size: " << bias.size() << std::endl ;
111- Shape sh (shape);
112- return make_tensor< float >(vals, sh, bias );
69+ // Определяем shape
70+ std::vector< size_t > shape ;
71+ if (layer_data. contains ( " weights " )) {
72+ parse_json_shape (layer_data[ " weights " ], shape );
11373 }
114- throw std::invalid_argument (" Unsupported type or invalid JSON format" );
74+
75+ std::cout << " Extracted weights size: " << weights.size () << std::endl;
76+ std::cout << " Shape: " ;
77+ for (auto dim : shape) std::cout << dim << " " ;
78+ std::cout << std::endl;
79+ std::cout << " Extracted bias size: " << bias.size () << std::endl;
80+
81+ return make_tensor<float >(weights, Shape (shape), bias);
11582}
0 commit comments