From 01853010eae3fbd367a51eeed0f5540958cb8fe7 Mon Sep 17 00:00:00 2001 From: kalcohol <314377460@qq.com> Date: Fri, 7 Feb 2020 14:33:01 +0800 Subject: [PATCH] type conversion and MSVC _CRT_SECURE_NO_DEPRECATE warning fix (#1524) * fix type conversion warning * fix MSVC _CRT_SECURE_NO_DEPRECATE warning * type conversion: missing one * fix MSVC _CRT_SECURE_NO_DEPRECATE warning and type warning * reformat * convert google::protobuf::int64 to size_t to avoid type impl between msvc and gcc gap * remove C++11 auto * tab -> space --- tools/caffe/caffe2ncnn.cpp | 220 +++++++++++++++++++------------------ tools/ncnnoptimize.cpp | 8 +- 2 files changed, 118 insertions(+), 110 deletions(-) diff --git a/tools/caffe/caffe2ncnn.cpp b/tools/caffe/caffe2ncnn.cpp index 13ad96b78..0b1f96702 100644 --- a/tools/caffe/caffe2ncnn.cpp +++ b/tools/caffe/caffe2ncnn.cpp @@ -12,6 +12,10 @@ // CONDITIONS OF ANY KIND, either express or implied. See the License for the // specific language governing permissions and limitations under the License. +#ifdef _MSC_VER +#define _CRT_SECURE_NO_DEPRECATE +#endif + #include #include #include @@ -32,7 +36,7 @@ static inline size_t alignSize(size_t sz, int n) { - return (sz + n-1) & -n; + return (sz + n - 1) & -n; } // convert float to half precision floating point @@ -52,9 +56,9 @@ static unsigned short float2half(float value) unsigned short exponent = (tmp.u & 0x7F800000) >> 23; unsigned int significand = tmp.u & 0x7FFFFF; -// fprintf(stderr, "%d %d %d\n", sign, exponent, significand); + // fprintf(stderr, "%d %d %d\n", sign, exponent, significand); - // 1 : 5 : 10 + // 1 : 5 : 10 unsigned short fp16; if (exponent == 0) { @@ -69,7 +73,7 @@ static unsigned short float2half(float value) else { // normalized - short newexp = exponent + (- 127 + 15); + short newexp = exponent + (-127 + 15); if (newexp >= 31) { // overflow, return infinity @@ -103,15 +107,15 @@ static unsigned short float2half(float value) static signed char float2int8(float value) { float tmp; - if (value >= 0.f) tmp = value + 0.5; - else tmp = value - 0.5; + if (value >= 0.f) tmp = value + 0.5f; + else tmp = value - 0.5f; if (tmp > 127) return 127; if (tmp < -127) return -127; - return tmp; + return static_cast(tmp); } static bool read_int8scale_table(const char* filepath, std::map >& blob_int8scale_table, std::map >& weight_int8scale_table) @@ -154,11 +158,11 @@ static bool read_int8scale_table(const char* filepath, std::map s { int8_weights.resize(data_length); - int length_per_group = data_length / scales.size(); + const int length_per_group = static_cast(data_length / scales.size()); for (size_t i = 0; i < data_length; i++) { float f = data[i]; - signed char int8 = float2int8(f * scales[ i / length_per_group ]); + signed char int8 = float2int8(f * scales[i / length_per_group]); int8_weights[i] = int8; } @@ -264,17 +268,17 @@ static bool quantize_weight(float *data, size_t data_length, int quantize_level, // 3. Align data to the quantized value for (size_t i = 0; i < data_length; ++i) { - size_t table_index = int((data[i] - min_value) / strides); - table_index = std::min(table_index, quantize_level - 1); + int table_index = int((data[i] - min_value) / strides); + table_index = std::min(table_index, quantize_level - 1); - float low_value = quantize_table[table_index]; + float low_value = quantize_table[table_index]; float high_value = low_value + strides; // find a nearest value between low and high value. - float targetValue = data[i] - low_value < high_value - data[i] ? low_value : high_value; + const float targetValue = data[i] - low_value < high_value - data[i] ? low_value : high_value; table_index = int((targetValue - min_value) / strides); - table_index = std::min(table_index, quantize_level - 1); + table_index = std::min(table_index, quantize_level - 1); quantize_index.push_back(table_index); } @@ -386,11 +390,11 @@ int main(int argc, char** argv) // [layer count] [blob count] int layer_count = proto.layer_size(); std::set blob_names; - for (int i=0; isecond; -// fprintf(stderr, "%s %d\n", it->first.c_str(), it->second); + // fprintf(stderr, "%s %d\n", it->first.c_str(), it->second); ++it; } } - fprintf(pp, "%lu %lu\n", layer_count + bottom_reference.size(), blob_names.size() + splitncnn_blob_count); + fprintf(pp, "%d %d\n", int(layer_count + bottom_reference.size()), int(blob_names.size() + splitncnn_blob_count)); // populate blob_name_decorated.clear(); int internal_split = 0; - for (int i=0; i #include #include @@ -1794,7 +1798,7 @@ int NetOptimize::shape_inference() if (dims == 2) m.create(w, h); if (dims == 3) m.create(w, h, c); - ex.input(i, m); + ex.input(int(i), m); } fprintf(stderr, "shape_inference\n"); @@ -1998,7 +2002,7 @@ int NetOptimize::save(const char* parampath, const char* binpath) } if (shape_ready) { - fprintf(pp, " -23330=%d", top_count*4); + fprintf(pp, " -23330=%zd", top_count * 4); for (int j=0; jtops[j];