-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmax_pooling_layer.go
38 lines (32 loc) · 1 KB
/
max_pooling_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
package yologo
import (
"fmt"
"github.com/pkg/errors"
"gorgonia.org/gorgonia"
"gorgonia.org/tensor"
)
type maxPoolingLayer struct {
size int
stride int
}
func (l *maxPoolingLayer) String() string {
return fmt.Sprintf("Maxpooling layer: Size->%[1]d Stride->%[2]d", l.size, l.stride)
}
func (l *maxPoolingLayer) Type() string {
return "maxpool"
}
func (l *maxPoolingLayer) ToNode(g *gorgonia.ExprGraph, inputs ...*gorgonia.Node) (*gorgonia.Node, error) {
shp := inputs[0].Shape()
if shp[2]%2 == 0 {
maxpoolOut, err := gorgonia.MaxPool2D(inputs[0], tensor.Shape{l.size, l.size}, []int{0, 0}, []int{l.stride, l.stride})
if err != nil {
return &gorgonia.Node{}, errors.Wrap(err, "Can't prepare max pooling operation")
}
return maxpoolOut, nil
}
maxpoolOut, err := gorgonia.MaxPool2D(inputs[0], tensor.Shape{l.size, l.size}, []int{0, 1, 0, 1}, []int{l.stride, l.stride})
if err != nil {
return &gorgonia.Node{}, errors.Wrap(err, "Can't prepare max pooling operation")
}
return maxpoolOut, nil
}