-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathyolo_layer.go
56 lines (48 loc) · 1.27 KB
/
yolo_layer.go
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
package yologo
import (
"fmt"
"github.com/pkg/errors"
"gorgonia.org/gorgonia"
)
type yoloLayer struct {
masks []int
anchors [][2]int
flattenAnchors []int
inputSize int
classesNum int
ignoreThresh float32
yoloTrainer YoloTrainer
}
func (l *yoloLayer) String() string {
str := "YOLO layer: "
for m := range l.masks {
str += fmt.Sprintf("Mask->%[1]d Anchors->[%[2]d, %[3]d]", l.masks[m], l.anchors[m][0], l.anchors[m][1])
if m != len(l.masks)-1 {
str += "\t|\t"
}
}
return str
}
func (l *yoloLayer) Type() string {
return "yolo"
}
func (l *yoloLayer) ToNode(g *gorgonia.ExprGraph, input ...*gorgonia.Node) (*gorgonia.Node, error) {
inputN := input[0]
if len(inputN.Shape()) == 0 {
return nil, fmt.Errorf("Input shape for YOLO layer is nil")
}
flattenAnchorsF32 := make([]float32, len(l.flattenAnchors))
for i := range flattenAnchorsF32 {
flattenAnchorsF32[i] = float32(l.flattenAnchors[i])
}
masksIdx := make([]int, len(l.masks))
for i := range l.masks {
masksIdx[i] = i
}
yoloNode, yoloTrainer, err := YOLOv3Node(inputN, flattenAnchorsF32, masksIdx, l.inputSize, l.classesNum, l.ignoreThresh)
l.yoloTrainer = yoloTrainer
if err != nil {
return nil, errors.Wrap(err, "Can't prepare YOLOv3 operation")
}
return yoloNode, nil
}