@@ -72,3 +72,84 @@ inline void read_tensor(const std::string& file_name, ov::Tensor tensor, bool as
72
72
73
73
std::cout << " Closing " << file_name << std::endl;
74
74
}
75
+
76
+ // / @brief Read an npy file created in Python:
77
+ // / with open('ndarray.npy', 'wb') as file:
78
+ // / np.save(file, ndarray)
79
+ inline ov::Tensor from_npy (const std::filesystem::path& npy) {
80
+ std::ifstream fstream{npy, std::ios::binary};
81
+ fstream.seekg (0 , std::ios_base::end);
82
+ OPENVINO_ASSERT (fstream.good ());
83
+ auto full_file_size = static_cast <std::size_t >(fstream.tellg ());
84
+ fstream.seekg (0 , std::ios_base::beg);
85
+
86
+ std::string magic_string (6 , ' ' );
87
+ fstream.read (&magic_string[0 ], magic_string.size ());
88
+ OPENVINO_ASSERT (magic_string == " \x93 NUMPY" );
89
+
90
+ fstream.ignore (2 );
91
+ unsigned short header_size;
92
+ fstream.read ((char *)&header_size, sizeof (header_size));
93
+
94
+ std::string header (header_size, ' ' );
95
+ fstream.read (&header[0 ], header.size ());
96
+
97
+ int idx, from, to;
98
+
99
+ // Verify fortran order is false
100
+ const std::string fortran_key = " 'fortran_order':" ;
101
+ idx = header.find (fortran_key);
102
+ OPENVINO_ASSERT (idx != -1 );
103
+
104
+ from = header.find_last_of (' ' , idx + fortran_key.size ()) + 1 ;
105
+ to = header.find (' ,' , from);
106
+ auto fortran_value = header.substr (from, to - from);
107
+ OPENVINO_ASSERT (fortran_value == " False" );
108
+
109
+ // Verify array shape matches the input's
110
+ const std::string shape_key = " 'shape':" ;
111
+ idx = header.find (shape_key);
112
+ OPENVINO_ASSERT (idx != -1 );
113
+
114
+ from = header.find (' (' , idx + shape_key.size ()) + 1 ;
115
+ to = header.find (' )' , from);
116
+
117
+ std::string shape_data = header.substr (from, to - from);
118
+ ov::Shape _shape;
119
+
120
+ if (!shape_data.empty ()) {
121
+ shape_data.erase (std::remove (shape_data.begin (), shape_data.end (), ' ,' ), shape_data.end ());
122
+
123
+ std::istringstream shape_data_stream (shape_data);
124
+ size_t value;
125
+ while (shape_data_stream >> value) {
126
+ _shape.push_back (value);
127
+ }
128
+ }
129
+
130
+ // Verify array data type matches input's
131
+ std::string dataTypeKey = " 'descr':" ;
132
+ idx = header.find (dataTypeKey);
133
+ OPENVINO_ASSERT (-1 != idx);
134
+
135
+ from = header.find (' \' ' , idx + dataTypeKey.size ()) + 1 ;
136
+ to = header.find (' \' ' , from);
137
+ std::string type;
138
+ type = header.substr (from, to - from);
139
+
140
+ size_t _size = 0 ;
141
+ _size = full_file_size - static_cast <std::size_t >(fstream.tellg ());
142
+ ov::element::Type tensor_type;
143
+ if (" <f4" == type) {
144
+ tensor_type = ov::element::f32;
145
+ } else if (" |u1" == type) {
146
+ tensor_type = ov::element::u8;
147
+ } else {
148
+ OPENVINO_THROW (" Not implemented dtype" );
149
+ }
150
+ OPENVINO_ASSERT (_size == ov::shape_size (_shape) * tensor_type.size ());
151
+ ov::Tensor tensor{tensor_type, _shape};
152
+ fstream.read ((char *)tensor.data (), _size);
153
+ OPENVINO_ASSERT (fstream.gcount () == _size);
154
+ return tensor;
155
+ }
0 commit comments