-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
76 lines (63 loc) · 1.47 KB
/
parser.go
File metadata and controls
76 lines (63 loc) · 1.47 KB
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
package cvimodelgo
import (
"encoding/binary"
"errors"
"io"
"strings"
"time"
"github.com/aisa-it/cvimodelgo/model"
)
var (
ErrUnsupportedModel = errors.New("unsupported model file")
)
type ModelHeader struct {
Magic [8]byte
BodySize uint32
Major byte
Minor byte
Md5 [16]byte
Chip [16]byte
Padding [2]byte
}
type ModelInfo struct {
Name string `json:"name"`
Target string `json:"target"`
BuildTime time.Time `json:"build_time"`
InputQuant string `json:"input_quant"`
OutputQuant string `json:"output_quant"`
Quant string `json:"quant"`
}
func ParseModelFile(r io.Reader) (*ModelInfo, error) {
// Read first 48 bytes header
header := ModelHeader{}
if err := binary.Read(r, binary.BigEndian, &header); err != nil {
return nil, err
}
if strings.ToLower(string(header.Magic[:])) != "cvimodel" {
return nil, ErrUnsupportedModel
}
// Read rest as fb
d, err := io.ReadAll(r)
if err != nil {
return nil, err
}
mdl := model.GetRootAsModel(d, 0)
info := ModelInfo{
Name: string(mdl.Name()),
Target: string(mdl.Target()),
}
info.BuildTime, _ = time.Parse("2006-01-02 15:04:05", string(mdl.BuildTime()))
var p model.Program
mdl.Programs(&p, 0)
var t model.Tensor
if p.TensorMap(&t, 0) {
info.InputQuant = t.Dtype().String()
}
if p.TensorMap(&t, 1) {
info.Quant = t.Dtype().String()
}
if p.TensorMap(&t, p.TensorMapLength()-1) {
info.OutputQuant = t.Dtype().String()
}
return &info, nil
}