Blame view

3rdparty/opencv-4.5.4/modules/dnn/src/caffe/caffe_shrinker.cpp 2.33 KB
f4334277   Hu Chunming   提交3rdparty
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
  // This file is part of OpenCV project.
  // It is subject to the license terms in the LICENSE file found in the top-level directory
  // of this distribution and at http://opencv.org/license.html.
  //
  // Copyright (C) 2017, Intel Corporation, all rights reserved.
  // Third party copyrights are property of their respective owners.
  
  #include "../precomp.hpp"
  
  #ifdef HAVE_PROTOBUF
  #include <fstream>
  #include "caffe_io.hpp"
  #endif
  
  namespace cv { namespace dnn {
  CV__DNN_INLINE_NS_BEGIN
  
  #ifdef HAVE_PROTOBUF
  
  void shrinkCaffeModel(const String& src, const String& dst, const std::vector<String>& layersTypes)
  {
      CV_TRACE_FUNCTION();
  
      std::vector<String> types(layersTypes);
      if (types.empty())
      {
          types.push_back("Convolution");
          types.push_back("InnerProduct");
      }
  
      caffe::NetParameter net;
      ReadNetParamsFromBinaryFileOrDie(src.c_str(), &net);
  
      for (int i = 0; i < net.layer_size(); ++i)
      {
          caffe::LayerParameter* lp = net.mutable_layer(i);
          if (std::find(types.begin(), types.end(), lp->type()) == types.end())
          {
              continue;
          }
          for (int j = 0; j < lp->blobs_size(); ++j)
          {
              caffe::BlobProto* blob = lp->mutable_blobs(j);
              CV_Assert(blob->data_size() != 0);  // float32 array.
  
              Mat floats(1, blob->data_size(), CV_32FC1, (void*)blob->data().data());
              Mat halfs(1, blob->data_size(), CV_16SC1);
              convertFp16(floats, halfs);  // Convert to float16.
  
              blob->clear_data();  // Clear float32 data.
  
              // Set float16 data.
              blob->set_raw_data(halfs.data, halfs.total() * halfs.elemSize());
              blob->set_raw_data_type(caffe::FLOAT16);
          }
      }
  #if GOOGLE_PROTOBUF_VERSION < 3005000
      size_t msgSize = saturate_cast<size_t>(net.ByteSize());
  #else
      size_t msgSize = net.ByteSizeLong();
  #endif
      std::vector<uint8_t> output(msgSize);
      net.SerializeWithCachedSizesToArray(&output[0]);
  
      std::ofstream ofs(dst.c_str(), std::ios::binary);
      ofs.write((const char*)&output[0], msgSize);
      ofs.close();
  }
  
  #else
  
  void shrinkCaffeModel(const String& src, const String& dst, const std::vector<String>& types)
  {
      CV_Error(cv::Error::StsNotImplemented, "libprotobuf required to import data from Caffe models");
  }
  
  #endif  // HAVE_PROTOBUF
  
  CV__DNN_INLINE_NS_END
  }} // namespace