mirror of
https://github.com/opencv/opencv_contrib.git
synced 2025-10-20 04:25:42 +08:00

Author: Vitaliy Lyudvichenko Mentor: Anatoly Baksheev Squashed commits are: commit2f6d743
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Sun May 31 00:05:31 2015 +0300 Added dnn module with draft interface. commitcfb9cfa
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Wed Jun 3 17:54:13 2015 +0300 Added libprotobuf to CMake. Added some Caffe files. commita6963b4
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Wed Jun 3 22:28:12 2015 +0300 Updated CMakeLists.txt to support protobuf Messages auto generation. commit6e23d93
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Sat Jun 6 19:08:12 2015 +0300 Added simple .prototxt Caffe importer. commit77321e3
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Mon Jun 8 16:59:27 2015 +0300 Added binary .caffemodel import support. Some changes in API. Caffe source files was cleared from unnecessary code. commit24a9eff
Merge:77321e3
844c30e
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Mon Jun 8 17:01:54 2015 +0300 Merge branch 'master' of https://github.com/Itseez/opencv_contrib commit62feeec
Author: Anatoly Baksheev <no@email> Date: Mon Jun 8 19:41:11 2015 +0300 fixed compilation witn C++11 compiler commit42b36fb
Author: Anatoly Baksheev <no@email> Date: Mon Jun 8 19:48:32 2015 +0300 more compilation commit10c3a13
Author: Anatoly Baksheev <no@email> Date: Mon Jun 8 20:00:37 2015 +0300 umbrealla header commiteb95846
Author: Anatoly Baksheev <no@email> Date: Mon Jun 8 20:26:47 2015 +0300 some warnings disabled commit1fd9304
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Tue Jun 9 13:58:35 2015 +0300 Dnn API changes (removed NetConfiguration, added LayerRegister, improved Blob). commitcae0bd4
Merge:1fd9304
eb95846
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Tue Jun 9 14:25:35 2015 +0300 Merge branch 'master' of github.com:ludv1x/opencv_contrib Conflicts: modules/dnn/include/opencv2/dnn.hpp commitee837c1
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Tue Jun 9 21:36:18 2015 +0300 Updated CMakeLists.txt commit194271d
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Sun Jun 14 22:20:01 2015 +0300 Implemented allocation of DAG and it's forward pass. Added wrappers for basic layers. commited1c569
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Tue Jun 16 19:31:15 2015 +0300 [Bomb commit] Implemented 4 main layers. Changes in API. Added worked classification example from GTSRB into tests. commit51df95d
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Tue Jun 16 19:31:38 2015 +0300 Added GSTRB test data. commit2638433
Merge:51df95d
2a199bc
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Tue Jun 16 22:55:35 2015 +0300 Merge branch 'master' of https://github.com/Itseez/opencv_contrib commiteba62d5
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Thu Jun 18 16:58:57 2015 +0300 Layers implementations divided onto separated files. commit383715d
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Thu Jun 18 23:55:16 2015 +0300 libprotobuf is optional dependency now commit2c501f3
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Fri Jun 19 00:01:53 2015 +0300 Replace CMake WARNING onto STATUS commit29966ee
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Fri Jun 19 16:50:25 2015 +0300 added HAVE_PROTOBUF macro commit48ab440
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Sat Jun 20 01:59:56 2015 +0300 Changed default parameter commit09ffc43
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Sat Jun 20 11:58:56 2015 +0300 Fixed CMake and CPP build errors commiteef4d1d
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Mon Jun 22 23:49:54 2015 +0300 Added LRN and SoftMax layers, some fixes commit22272e8
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Tue Jun 23 14:27:30 2015 +0300 Many fixes. commit9cddccf
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Tue Jun 23 14:29:42 2015 +0300 Added AlexNet sample. Removed big .caffemodel files from repo. commit66fa1e1
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Tue Jun 23 17:33:48 2015 +0300 Random changes in matlab/* module was reverted commitd80c272
Author: Anatoly Baksheev <no@email> Date: Wed Jun 24 11:48:44 2015 +0300 fixed stand alone link errors commit0cd5459
Author: Anatoly Baksheev <no@email> Date: Wed Jun 24 12:25:19 2015 +0300 added incdes to project so that it could be visible in some IDEs (ex. qtcreator) commit13edaaa
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Wed Jun 24 18:25:34 2015 +0300 Added group param to convolution_layer.cpp commit49b5e5e
Merge:13edaaa
0cd5459
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Wed Jun 24 18:25:50 2015 +0300 Merge branch 'master' of github.com:ludv1x/opencv_contrib commit99b8dac
Merge:49b5e5e
8a05bdb
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Wed Jun 24 18:26:10 2015 +0300 Merge branch 'master' of https://github.com/Itseez/opencv_contrib commit0f7907d
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Wed Jun 24 19:28:55 2015 +0300 updated example commit2717765
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Wed Jun 24 20:02:31 2015 +0300 Fixed some warnings commitd117983
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Wed Jun 24 22:18:05 2015 +0300 Deleted trailing whitespaces commitcfdf060
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Thu Jun 25 12:16:11 2015 +0300 Fixed more warnings. Updated glog_emulator.hpp: commit761b037
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Fri Jun 26 17:57:55 2015 +0300 Fixed critical bug in dnn::Dict. Fixed LRN layer implementation. Added layers test. commit6fd67d4
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Wed Jul 1 12:12:49 2015 +0300 Updated test. Added and successfuly passed AlexNet reproducibility test. Small fixes. commit9838234
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Thu Jul 2 12:25:30 2015 +0300 Some layer fixes. Added concat layer and avg. pooling to sucesfully run GoogLeNet. Added concat layer, implemented average pooling to run GoogLeNet. Fixed transpose error in FullyConnected layer (hotfix in softmax layer). Added GoogleNet test and updated AlexNet test (both nets now work fine). commit6f8a73b
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Thu Jul 2 12:31:48 2015 +0300 Replaced CMake if() condition for standalone build. commit0ebe30a
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Sat Jul 4 22:41:55 2015 +0300 Blob class was significantly refactored commitd02bced
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Sun Jul 5 22:52:34 2015 +0300 More refactoring over Blob. More refactoring over Blob. Fix warnings and eliminated zlib dependency in cnpy.h commit85ad43d
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Mon Jul 6 14:51:10 2015 +0300 Generalized Blob constructor and added vector of images support. AlexNet and GoogLeNet tests updated. commit75e09fd
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Wed Jul 8 19:05:36 2015 +0300 Implement ambiguous blobs naming scheme like: "layerName[.OutputName]". Old Caffe-like blob naming scheme was deleted. commitb9e85ed
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Wed Jul 8 19:15:51 2015 +0300 fixed sign type mismatch warnings in cnpy commit9783bba
Merge:b9e85ed
6f8a73b
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Thu Jul 9 18:26:08 2015 +0300 Merge branch 'master' into work commitde4d800
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Sun Jul 12 23:19:07 2015 +0300 Added .caffemodel files downloader for tests on post-build step. commitec74678
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Thu Jul 16 20:24:12 2015 +0300 Added element-wise layers. Fixed downloader and ConcatLayer. commit0362da9
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Fri Jul 17 17:24:59 2015 +0300 Added array support for Dict. commit3aa37d2
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Sat Jul 18 01:12:08 2015 +0300 Array of parmaters support into caffe_importer.cpp commit172419e
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Sun Jul 19 21:31:22 2015 +0300 Added Reshape layer and altered importer to correctly handle its params. commit71cfae4
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Mon Jul 20 23:29:42 2015 +0300 Added Split and Slice layer. commit527fa65
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Tue Jul 21 16:10:29 2015 +0300 Rewrited concat_layer.cpp in OpenCV-style. Fixed slice layer, added test for slice, split and reshape layers. commit9b1e28e
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Thu Jul 23 00:09:15 2015 +0300 Refactored ConvolutionLayer, added Deconvolution. commitff45c22
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Thu Jul 23 21:19:25 2015 +0300 Fixed gcc build errors. commit6548839
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Sat Jul 25 12:24:54 2015 +0300 Added testdata images. Fixed Dict copy constructor. commitdb4ff21
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Sat Jul 25 22:26:29 2015 +0300 Updated classification example commit4da1046
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Tue Jul 28 16:00:07 2015 +0300 Implemented draft Torch importer commit7d795af
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Thu Jul 30 15:43:47 2015 +0300 Fixed Torch parser commit5e5cc96
Merge:6f8a73b
f9d4288
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Mon Aug 3 20:18:01 2015 +0300 Merge branch 'master' of https://github.com/Itseez/opencv_contrib commitdd15521
Merge:5e5cc96
db4ff21
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Mon Aug 3 20:18:28 2015 +0300 Merge branch 'master' of github.com:ludv1x/opencv_contrib commitc1f4410
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Mon Aug 3 20:20:00 2015 +0300 cnpy warns fix commit2905c03
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Tue Aug 4 19:16:34 2015 +0300 Multiple layers support added into Torch importer. Also DictValue was refactored. commit8df8936
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Wed Aug 5 13:14:52 2015 +0300 Added Torch blob reader and torch convolution test. commit1c220cf
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Thu Aug 6 13:29:45 2015 +0300 Added simple tests for Torch importer, fixed some importing issues. commit436d929
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Thu Aug 6 14:37:56 2015 +0300 Resolve reference counting problem in Torch importer. commitb2f4ba3
Merge:c1f4410
172fdb3
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Fri Aug 7 13:27:45 2015 +0300 Merge branch 'master' of https://github.com/Itseez/opencv_contrib commit5b08053
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Mon Aug 10 12:53:44 2015 +0300 Added ability to remove singleton dimensions in Reshape layer. Updated torch importer commitd0875b1
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Tue Aug 11 01:49:27 2015 +0300 Fixed average pooling error commitf8119ea
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Tue Aug 11 22:12:48 2015 +0300 Fixed Deconvolution layer. Added more wide layers test coverage. commitdf5eec6
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Wed Aug 12 16:54:44 2015 +0300 Added MVN layer. Renamed layer test data. commitf28effb
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Wed Aug 12 19:49:54 2015 +0300 Cleaned caffe dependencies commitb3dcc39
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Wed Aug 12 20:04:23 2015 +0300 Moved caffe.proto commit7d2e745
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Thu Aug 13 00:53:50 2015 +0300 Minor test changes commitc65d032
Merge:7d2e745
b2f4ba3
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Thu Aug 13 01:25:00 2015 +0300 Merge branch 'master' of https://github.com/ludv1x/opencv_contrib commit160d864
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Thu Aug 13 11:45:40 2015 +0300 Fixed small warn and example error commit06f949a
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Thu Aug 13 17:52:46 2015 +0300 Added initModule() procedure to explicitly init builtin layers commit23d3ede
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Mon Aug 17 15:10:04 2015 +0300 Added OpenCL im2col commit7acfda2
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Tue Aug 18 01:40:41 2015 +0300 Fixed im2col_ocl bug caused non-zero UMat offset commit9d932af
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Tue Aug 18 01:55:05 2015 +0300 Add new method to Net commit0903d79
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Wed Aug 19 01:28:44 2015 +0300 Added libprotobuf 2.5 as thridparty library commit4929177
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Wed Aug 19 01:32:51 2015 +0300 Add compiled caffe protobuf files commitfc9795b
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Wed Aug 19 18:57:12 2015 +0300 libprotobuf is a separate 3rdparty lib now commit93a372f
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Wed Aug 19 19:53:52 2015 +0300 Try fix msvc error and cmake error on android commit44e52a0
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Thu Aug 20 01:34:05 2015 +0300 Try fix msvc warnings commit519167e
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Thu Aug 20 18:51:44 2015 +0300 Refactored cmake and disabled AlexNet test commite644b5a
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Thu Aug 20 21:52:41 2015 +0300 Changed Blob::ptr and Blob::offset methods commitf07c564
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Thu Aug 20 22:53:02 2015 +0300 Add test data for layers commitfb66acb
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Fri Aug 21 01:59:17 2015 +0300 API refactoring renamed learnedParams blobs field added new fields: name and type for Layer commitf8715f3
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Sat Aug 22 18:31:53 2015 +0300 Added licence headers commit945094b
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Sun Aug 23 03:06:24 2015 +0300 Update doc, small changes in Blob methods commitc681508
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Mon Aug 24 01:45:32 2015 +0300 Add more docs commitbd242d1
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Mon Aug 24 20:44:34 2015 +0300 Add docs for remaning functions commiteced23c
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Mon Aug 24 20:44:56 2015 +0300 Updated dnn example commit57a2194
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Tue Aug 25 15:18:21 2015 +0300 Add tutorial_dnn_build commitefde664
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Tue Aug 25 17:09:00 2015 +0300 Move binary testdata from opencv_contrib to opencv_extra commite52a7ee
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Tue Aug 25 19:01:05 2015 +0300 Add bvlc_googlenet.prototxt to samples commit3154fc0
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Wed Aug 26 01:43:53 2015 +0300 Add googlenet tutorial commit467cd96
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Wed Aug 26 13:32:27 2015 +0300 Updated dnn build tutorial commit83e39a9
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Wed Aug 26 19:56:36 2015 +0300 Fix typos commit0a64a9d
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Thu Aug 27 02:30:20 2015 +0300 Changed Caffe googlenet tutorial commitd2b6011
Author: Vitaliy Lyudvichenko <ludv1x@yandex.ru> Date: Thu Aug 27 14:01:40 2015 +0300 Fix documentation warning commit248577a
Author: Anatoly Baksheev <no@email> Date: Thu Sep 3 18:33:26 2015 +0300 fixed signed/unsigned cast warning
292 lines
10 KiB
C++
292 lines
10 KiB
C++
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
//
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
// If you do not agree to this license, do not download, install,
|
|
// copy or use the software.
|
|
//
|
|
//
|
|
// License Agreement
|
|
// For Open Source Computer Vision Library
|
|
//
|
|
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
|
|
// Third party copyrights are property of their respective owners.
|
|
//
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
// are permitted provided that the following conditions are met:
|
|
//
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
// this list of conditions and the following disclaimer.
|
|
//
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
// and/or other materials provided with the distribution.
|
|
//
|
|
// * The name of the copyright holders may not be used to endorse or promote products
|
|
// derived from this software without specific prior written permission.
|
|
//
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
//
|
|
//M*/
|
|
|
|
//Copyright (C) 2011 Carl Rogers
|
|
//Released under MIT License
|
|
//license available in LICENSE file, or at http://www.opensource.org/licenses/mit-license.php
|
|
|
|
#include"cnpy.h"
|
|
#include<complex>
|
|
#include<cstdlib>
|
|
#include<algorithm>
|
|
#include<cstring>
|
|
#include<iomanip>
|
|
|
|
char cnpy::BigEndianTest() {
|
|
union
|
|
{
|
|
unsigned char x[2];
|
|
short y;
|
|
};
|
|
x[0] = 1;
|
|
x[1] = 0;
|
|
|
|
return y == 1 ? '<' : '>';
|
|
}
|
|
|
|
char cnpy::map_type(const std::type_info& t)
|
|
{
|
|
if(t == typeid(float) ) return 'f';
|
|
if(t == typeid(double) ) return 'f';
|
|
if(t == typeid(long double) ) return 'f';
|
|
|
|
if(t == typeid(int) ) return 'i';
|
|
if(t == typeid(char) ) return 'i';
|
|
if(t == typeid(short) ) return 'i';
|
|
if(t == typeid(long) ) return 'i';
|
|
if(t == typeid(long long) ) return 'i';
|
|
|
|
if(t == typeid(unsigned char) ) return 'u';
|
|
if(t == typeid(unsigned short) ) return 'u';
|
|
if(t == typeid(unsigned long) ) return 'u';
|
|
if(t == typeid(unsigned long long) ) return 'u';
|
|
if(t == typeid(unsigned int) ) return 'u';
|
|
|
|
if(t == typeid(bool) ) return 'b';
|
|
|
|
if(t == typeid(std::complex<float>) ) return 'c';
|
|
if(t == typeid(std::complex<double>) ) return 'c';
|
|
if(t == typeid(std::complex<long double>) ) return 'c';
|
|
|
|
else return '?';
|
|
}
|
|
|
|
template<> std::vector<char>& cnpy::operator+=(std::vector<char>& lhs, const std::string rhs) {
|
|
lhs.insert(lhs.end(),rhs.begin(),rhs.end());
|
|
return lhs;
|
|
}
|
|
|
|
template<> std::vector<char>& cnpy::operator+=(std::vector<char>& lhs, const char* rhs) {
|
|
//write in little endian
|
|
size_t len = strlen(rhs);
|
|
lhs.reserve(len);
|
|
for(size_t byte = 0; byte < len; byte++) {
|
|
lhs.push_back(rhs[byte]);
|
|
}
|
|
return lhs;
|
|
}
|
|
|
|
void cnpy::parse_npy_header(FILE* fp, unsigned int& word_size, unsigned int*& shape, unsigned int& ndims, bool& fortran_order) {
|
|
char buffer[256];
|
|
size_t res = fread(buffer,sizeof(char),11,fp);
|
|
if(res != 11)
|
|
throw std::runtime_error("parse_npy_header: failed fread");
|
|
std::string header = fgets(buffer,256,fp);
|
|
cnpy_assert(header[header.size()-1] == '\n');
|
|
|
|
size_t loc1, loc2;
|
|
|
|
//fortran order
|
|
loc1 = header.find("fortran_order")+16;
|
|
fortran_order = (header.substr(loc1,5) == "True" ? true : false);
|
|
|
|
//shape
|
|
loc1 = header.find("(");
|
|
loc2 = header.find(")");
|
|
std::string str_shape = header.substr(loc1+1,loc2-loc1-1);
|
|
if(str_shape[str_shape.size()-1] == ',') ndims = 1;
|
|
else ndims = (unsigned)std::count(str_shape.begin(),str_shape.end(),',')+1;
|
|
shape = new unsigned int[ndims];
|
|
for(unsigned int i = 0;i < ndims;i++) {
|
|
loc1 = str_shape.find(",");
|
|
shape[i] = atoi(str_shape.substr(0,loc1).c_str());
|
|
str_shape = str_shape.substr(loc1+1);
|
|
}
|
|
|
|
//endian, word size, data type
|
|
//byte order code | stands for not applicable.
|
|
//not sure when this applies except for byte array
|
|
loc1 = header.find("descr")+9;
|
|
bool littleEndian = (header[loc1] == '<' || header[loc1] == '|' ? true : false);
|
|
cnpy_assert(littleEndian);
|
|
|
|
//char type = header[loc1+1];
|
|
//assert(type == map_type(T));
|
|
|
|
std::string str_ws = header.substr(loc1+2);
|
|
loc2 = str_ws.find("'");
|
|
word_size = atoi(str_ws.substr(0,loc2).c_str());
|
|
}
|
|
|
|
void cnpy::parse_zip_footer(FILE* fp, unsigned short& nrecs, unsigned int& global_header_size, unsigned int& global_header_offset)
|
|
{
|
|
std::vector<char> footer(22);
|
|
fseek(fp,-22,SEEK_END);
|
|
size_t res = fread(&footer[0],sizeof(char),22,fp);
|
|
if(res != 22)
|
|
throw std::runtime_error("parse_zip_footer: failed fread");
|
|
|
|
unsigned short disk_no, disk_start, nrecs_on_disk, comment_len;
|
|
disk_no = *(unsigned short*) &footer[4];
|
|
disk_start = *(unsigned short*) &footer[6];
|
|
nrecs_on_disk = *(unsigned short*) &footer[8];
|
|
nrecs = *(unsigned short*) &footer[10];
|
|
global_header_size = *(unsigned int*) &footer[12];
|
|
global_header_offset = *(unsigned int*) &footer[16];
|
|
comment_len = *(unsigned short*) &footer[20];
|
|
|
|
cnpy_assert(disk_no == 0);
|
|
cnpy_assert(disk_start == 0);
|
|
cnpy_assert(nrecs_on_disk == nrecs);
|
|
cnpy_assert(comment_len == 0);
|
|
}
|
|
|
|
cnpy::NpyArray load_the_npy_file(FILE* fp) {
|
|
unsigned int* shape;
|
|
unsigned int ndims, word_size;
|
|
bool fortran_order;
|
|
cnpy::parse_npy_header(fp,word_size,shape,ndims,fortran_order);
|
|
unsigned long long size = 1; //long long so no overflow when multiplying by word_size
|
|
for(unsigned int i = 0;i < ndims;i++) size *= shape[i];
|
|
|
|
cnpy::NpyArray arr;
|
|
arr.word_size = word_size;
|
|
arr.shape = std::vector<unsigned int>(shape,shape+ndims);
|
|
delete[] shape;
|
|
arr.data = new char[size*word_size];
|
|
arr.fortran_order = fortran_order;
|
|
size_t nread = fread(arr.data,word_size,size,fp);
|
|
if(nread != size)
|
|
throw std::runtime_error("load_the_npy_file: failed fread");
|
|
return arr;
|
|
}
|
|
|
|
cnpy::npz_t cnpy::npz_load(std::string fname) {
|
|
FILE* fp = fopen(fname.c_str(),"rb");
|
|
|
|
if(!fp) printf("npz_load: Error! Unable to open file %s!\n",fname.c_str());
|
|
cnpy_assert(fp);
|
|
|
|
cnpy::npz_t arrays;
|
|
|
|
while(1) {
|
|
std::vector<char> local_header(30);
|
|
size_t headerres = fread(&local_header[0],sizeof(char),30,fp);
|
|
if(headerres != 30)
|
|
throw std::runtime_error("npz_load: failed fread");
|
|
|
|
//if we've reached the global header, stop reading
|
|
if(local_header[2] != 0x03 || local_header[3] != 0x04) break;
|
|
|
|
//read in the variable name
|
|
unsigned short name_len = *(unsigned short*) &local_header[26];
|
|
std::string varname(name_len,' ');
|
|
size_t vname_res = fread(&varname[0],sizeof(char),name_len,fp);
|
|
if(vname_res != name_len)
|
|
throw std::runtime_error("npz_load: failed fread");
|
|
|
|
//erase the lagging .npy
|
|
varname.erase(varname.end()-4,varname.end());
|
|
|
|
//read in the extra field
|
|
unsigned short extra_field_len = *(unsigned short*) &local_header[28];
|
|
if(extra_field_len > 0) {
|
|
std::vector<char> buff(extra_field_len);
|
|
size_t efield_res = fread(&buff[0],sizeof(char),extra_field_len,fp);
|
|
if(efield_res != extra_field_len)
|
|
throw std::runtime_error("npz_load: failed fread");
|
|
}
|
|
|
|
arrays[varname] = load_the_npy_file(fp);
|
|
}
|
|
|
|
fclose(fp);
|
|
return arrays;
|
|
}
|
|
|
|
cnpy::NpyArray cnpy::npz_load(std::string fname, std::string varname) {
|
|
FILE* fp = fopen(fname.c_str(),"rb");
|
|
|
|
if(!fp) {
|
|
throw std::runtime_error("npz_load: Error! Unable to open file " + fname + "!\n");
|
|
}
|
|
|
|
while(1) {
|
|
std::vector<char> local_header(30);
|
|
size_t header_res = fread(&local_header[0],sizeof(char),30,fp);
|
|
if(header_res != 30)
|
|
throw std::runtime_error("npz_load: failed fread");
|
|
|
|
//if we've reached the global header, stop reading
|
|
if(local_header[2] != 0x03 || local_header[3] != 0x04) break;
|
|
|
|
//read in the variable name
|
|
unsigned short name_len = *(unsigned short*) &local_header[26];
|
|
std::string vname(name_len,' ');
|
|
size_t vname_res = fread(&vname[0],sizeof(char),name_len,fp);
|
|
if(vname_res != name_len)
|
|
throw std::runtime_error("npz_load: failed fread");
|
|
vname.erase(vname.end()-4,vname.end()); //erase the lagging .npy
|
|
|
|
//read in the extra field
|
|
unsigned short extra_field_len = *(unsigned short*) &local_header[28];
|
|
fseek(fp,extra_field_len,SEEK_CUR); //skip past the extra field
|
|
|
|
if(vname == varname) {
|
|
NpyArray array = load_the_npy_file(fp);
|
|
fclose(fp);
|
|
return array;
|
|
}
|
|
else {
|
|
//skip past the data
|
|
unsigned int size = *(unsigned int*) &local_header[22];
|
|
fseek(fp,size,SEEK_CUR);
|
|
}
|
|
}
|
|
|
|
fclose(fp);
|
|
throw std::runtime_error("npz_load: Error! Variable name " + varname + " not found in " + fname + "!\n");
|
|
}
|
|
|
|
cnpy::NpyArray cnpy::npy_load(std::string fname) {
|
|
|
|
FILE* fp = fopen(fname.c_str(), "rb");
|
|
|
|
if(!fp) {
|
|
throw std::runtime_error("npy_load: Error! Unable to open file " + fname + "!\n");
|
|
}
|
|
|
|
NpyArray arr = load_the_npy_file(fp);
|
|
|
|
fclose(fp);
|
|
return arr;
|
|
}
|