File size: 5,371 Bytes
be903e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

#include <stdio.h>
#include <string.h>
#include <string>

static int copy_param(const char* parampath, FILE* outparamfp, int* total_layer_count, int* total_blob_count)
{
    // resolve model namespace from XYZ.param
    const char* lastslash = strrchr(parampath, '/');
    const char* name = lastslash == NULL ? parampath : lastslash + 1;
    const char* dot = strrchr(name, '.');
    std::string ns = dot ? std::string(name).substr(0, dot - name) : std::string(name);

    FILE* fp = fopen(parampath, "rb");

    if (!fp)
    {
        fprintf(stderr, "fopen %s failed\n", parampath);
        return -1;
    }

    int nscan = 0;
    int magic = 0;
    nscan = fscanf(fp, "%d", &magic);
    if (nscan != 1 || magic != 7767517)
    {
        fprintf(stderr, "read magic failed %d\n", nscan);
        return -1;
    }

    int layer_count = 0;
    int blob_count = 0;
    nscan = fscanf(fp, "%d %d", &layer_count, &blob_count);
    if (nscan != 2)
    {
        fprintf(stderr, "read layer_count and blob_count failed %d\n", nscan);
        return -1;
    }

    *total_layer_count += layer_count;
    *total_blob_count += blob_count;

    char line[1024];
    for (int i = 0; i < layer_count; i++)
    {
        char layer_type[33];
        char layer_name[257];
        int bottom_count = 0;
        int top_count = 0;
        nscan = fscanf(fp, "%32s %256s %d %d", layer_type, layer_name, &bottom_count, &top_count);
        if (nscan != 4)
        {
            fprintf(stderr, "read layer params failed %d\n", nscan);
            return -1;
        }

        fprintf(outparamfp, "%-24s %s/%-24s %d %d", layer_type, ns.c_str(), layer_name, bottom_count, top_count);

        for (int j = 0; j < bottom_count; j++)
        {
            char bottom_name[257];
            nscan = fscanf(fp, "%256s", bottom_name);
            if (nscan != 1)
            {
                fprintf(stderr, "read bottom_name failed %d\n", nscan);
                return -1;
            }

            fprintf(outparamfp, " %s/%s", ns.c_str(), bottom_name);
        }

        for (int j = 0; j < top_count; j++)
        {
            char top_name[257];
            nscan = fscanf(fp, "%256s", top_name);
            if (nscan != 1)
            {
                fprintf(stderr, "read top_name failed %d\n", nscan);
                return -1;
            }

            fprintf(outparamfp, " %s/%s", ns.c_str(), top_name);
        }

        // copy param dict string
        char* s = fgets(line, 1024, fp);
        if (!s)
        {
            fprintf(stderr, "read line %s failed\n", parampath);
            break;
        }

        fputs(line, outparamfp);
    }

    fclose(fp);

    return 0;
}

static int copy_bin(const char* binpath, FILE* outbinfp)
{
    FILE* fp = fopen(binpath, "rb");

    if (!fp)
    {
        fprintf(stderr, "fopen %s failed\n", binpath);
        return -1;
    }

    fseek(fp, 0, SEEK_END);
    int len = (int)ftell(fp);
    rewind(fp);

    char buffer[4096];
    int i = 0;
    for (; i + 4095 < len;)
    {
        size_t nread = fread(buffer, 1, 4096, fp);
        size_t nwrite = fwrite(buffer, 1, nread, outbinfp);
        i += (int)nwrite;
    }
    {
        size_t nread = fread(buffer, 1, len - i, fp);
        size_t nwrite = fwrite(buffer, 1, nread, outbinfp);
        i += (int)nwrite;
    }

    if (i != len)
    {
        fprintf(stderr, "copy %s incomplete\n", binpath);
    }

    fclose(fp);

    return 0;
}

int main(int argc, char** argv)
{
    if (argc < 7 || (argc - 1) % 2 != 0)
    {
        fprintf(stderr, "Usage: %s [param1] [bin1] [param2] [bin2] ... [outparam] [outbin]\n", argv[0]);
        return -1;
    }

    const char* outparampath = argv[argc - 2];
    const char* outbinpath = argv[argc - 1];

    FILE* outparamfp = fopen(outparampath, "wb");
    FILE* outbinfp = fopen(outbinpath, "wb");

    // magic
    fprintf(outparamfp, "7767517\n");

    // layer count and blob count placeholder
    // 99999 is large enough I think  --- nihui
    fprintf(outparamfp, "           \n");

    int total_layer_count = 0;
    int total_blob_count = 0;

    const int model_count = (argc - 3) / 2;

    for (int i = 0; i < model_count; i++)
    {
        const char* parampath = argv[i * 2 + 1];
        const char* binpath = argv[i * 2 + 2];

        copy_param(parampath, outparamfp, &total_layer_count, &total_blob_count);
        copy_bin(binpath, outbinfp);
    }

    // the real layer count and blob count
    rewind(outparamfp);
    fprintf(outparamfp, "7767517\n");
    fprintf(outparamfp, "%d %d", total_layer_count, total_blob_count);

    fclose(outparamfp);
    fclose(outbinfp);

    return 0;
}