1
// Tencent is pleased to support the open source community by making ncnn available.
3
// Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
5
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6
// in compliance with the License. You may obtain a copy of the License at
8
// https://opensource.org/licenses/BSD-3-Clause
10
// Unless required by applicable law or agreed to in writing, software distributed
11
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13
// specific language governing permissions and limitations under the License.
19
static int copy_param(const char* parampath, FILE* outparamfp, int* total_layer_count, int* total_blob_count)
21
// resolve model namespace from XYZ.param
22
const char* lastslash = strrchr(parampath, '/');
23
const char* name = lastslash == NULL ? parampath : lastslash + 1;
24
const char* dot = strrchr(name, '.');
25
std::string ns = dot ? std::string(name).substr(0, dot - name) : std::string(name);
27
FILE* fp = fopen(parampath, "rb");
31
fprintf(stderr, "fopen %s failed\n", parampath);
37
nscan = fscanf(fp, "%d", &magic);
38
if (nscan != 1 || magic != 7767517)
40
fprintf(stderr, "read magic failed %d\n", nscan);
46
nscan = fscanf(fp, "%d %d", &layer_count, &blob_count);
49
fprintf(stderr, "read layer_count and blob_count failed %d\n", nscan);
53
*total_layer_count += layer_count;
54
*total_blob_count += blob_count;
57
for (int i = 0; i < layer_count; i++)
63
nscan = fscanf(fp, "%32s %256s %d %d", layer_type, layer_name, &bottom_count, &top_count);
66
fprintf(stderr, "read layer params failed %d\n", nscan);
70
fprintf(outparamfp, "%-24s %s/%-24s %d %d", layer_type, ns.c_str(), layer_name, bottom_count, top_count);
72
for (int j = 0; j < bottom_count; j++)
74
char bottom_name[257];
75
nscan = fscanf(fp, "%256s", bottom_name);
78
fprintf(stderr, "read bottom_name failed %d\n", nscan);
82
fprintf(outparamfp, " %s/%s", ns.c_str(), bottom_name);
85
for (int j = 0; j < top_count; j++)
88
nscan = fscanf(fp, "%256s", top_name);
91
fprintf(stderr, "read top_name failed %d\n", nscan);
95
fprintf(outparamfp, " %s/%s", ns.c_str(), top_name);
98
// copy param dict string
99
char* s = fgets(line, 1024, fp);
102
fprintf(stderr, "read line %s failed\n", parampath);
106
fputs(line, outparamfp);
114
static int copy_bin(const char* binpath, FILE* outbinfp)
116
FILE* fp = fopen(binpath, "rb");
120
fprintf(stderr, "fopen %s failed\n", binpath);
124
fseek(fp, 0, SEEK_END);
125
int len = (int)ftell(fp);
130
for (; i + 4095 < len;)
132
size_t nread = fread(buffer, 1, 4096, fp);
133
size_t nwrite = fwrite(buffer, 1, nread, outbinfp);
137
size_t nread = fread(buffer, 1, len - i, fp);
138
size_t nwrite = fwrite(buffer, 1, nread, outbinfp);
144
fprintf(stderr, "copy %s incomplete\n", binpath);
152
int main(int argc, char** argv)
154
if (argc < 7 || (argc - 1) % 2 != 0)
156
fprintf(stderr, "Usage: %s [param1] [bin1] [param2] [bin2] ... [outparam] [outbin]\n", argv[0]);
160
const char* outparampath = argv[argc - 2];
161
const char* outbinpath = argv[argc - 1];
163
FILE* outparamfp = fopen(outparampath, "wb");
164
FILE* outbinfp = fopen(outbinpath, "wb");
167
fprintf(outparamfp, "7767517\n");
169
// layer count and blob count placeholder
170
// 99999 is large enough I think --- nihui
171
fprintf(outparamfp, " \n");
173
int total_layer_count = 0;
174
int total_blob_count = 0;
176
const int model_count = (argc - 3) / 2;
178
for (int i = 0; i < model_count; i++)
180
const char* parampath = argv[i * 2 + 1];
181
const char* binpath = argv[i * 2 + 2];
183
copy_param(parampath, outparamfp, &total_layer_count, &total_blob_count);
184
copy_bin(binpath, outbinfp);
187
// the real layer count and blob count
189
fprintf(outparamfp, "7767517\n");
190
fprintf(outparamfp, "%d %d", total_layer_count, total_blob_count);