/
upsample_op.go
226 lines (198 loc) · 5.65 KB
/
upsample_op.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package yologo
import (
"fmt"
"hash"
"hash/fnv"
"github.com/chewxy/hm"
"github.com/pkg/errors"
"gorgonia.org/gorgonia"
"gorgonia.org/tensor"
)
type upsampleOp struct {
stride int
}
func newUpsampleOp(inputShape tensor.Shape, stride int) *upsampleOp {
upsampleop := &upsampleOp{
stride: stride,
}
return upsampleop
}
//Upsample2D - simply upscaling Tensor by scale factor.
/*
1, 2
3, 4
converts to
1,1,2,2
1,1,2,2
3,3,4,4,
3,3,4,4,
*/
func Upsample2D(x *gorgonia.Node, scale int) (*gorgonia.Node, error) {
if scale < 1 {
return nil, errors.Errorf("Upsample scale %v does not make sense", scale)
}
xShape := x.Shape()
op := newUpsampleOp(xShape, scale-1)
retVal, err := gorgonia.ApplyOp(op, x)
return retVal, err
}
func (op *upsampleOp) Arity() int {
return 1
}
func (op *upsampleOp) ReturnsPtr() bool { return false }
func (op *upsampleOp) CallsExtern() bool { return false }
func (op *upsampleOp) WriteHash(h hash.Hash) {
fmt.Fprintf(h, "Upsample{}(stride: (%d))", op.stride)
}
func (op *upsampleOp) Hashcode() uint32 {
h := fnv.New32a()
op.WriteHash(h)
return h.Sum32()
}
func (op *upsampleOp) String() string {
return fmt.Sprintf("Upsample{}(stride: (%d))", op.stride)
}
func (op *upsampleOp) InferShape(inputs ...gorgonia.DimSizer) (tensor.Shape, error) {
s := inputs[0].(tensor.Shape).Clone()
s[2] = s[2] * (1 + op.stride)
s[3] = s[3] * (1 + op.stride)
return s, nil
}
func (op *upsampleOp) Type() hm.Type {
a := hm.TypeVariable('a')
t := gorgonia.TensorType{Dims: 4, Of: a}
return hm.NewFnType(t, t)
}
func (op *upsampleOp) OverwritesInput() int { return -1 }
func (op *upsampleOp) checkInput(inputs ...gorgonia.Value) (tensor.Tensor, error) {
if err := checkArity(op, len(inputs)); err != nil {
return nil, err
}
var in tensor.Tensor
var ok bool
if in, ok = inputs[0].(tensor.Tensor); !ok {
return nil, errors.Errorf("Expected input to be a tensor")
}
if in.Shape().Dims() != 4 {
return nil, errors.Errorf("Expected input to have 4 dimensions")
}
return in, nil
}
func (op *upsampleOp) Do(inputs ...gorgonia.Value) (retVal gorgonia.Value, err error) {
var in tensor.Tensor
if in, err = op.checkInput(inputs...); err != nil {
return nil, err
}
inShp := in.Shape()
b, c, h, w := inShp[0], inShp[1], inShp[2], inShp[3]
out := tensor.New(tensor.Of(in.Dtype()), tensor.WithShape(b, c, h*(1+op.stride), w*(1+op.stride)), tensor.WithEngine(in.Engine()))
for bi := 0; bi < b; bi++ {
for ci := 0; ci < c; ci++ {
for hi := 0; hi < h; hi++ {
for wi := 0; wi < w; wi++ {
val, err := in.At(bi, ci, hi, wi)
if err != nil {
return nil, errors.Errorf("Error accessing input data at [%v, %v, %v, %v]", bi, ci, hi, wi)
}
hout := hi * (op.stride + 1)
wout := wi * (op.stride + 1)
for shi := 0; shi <= op.stride; shi++ {
for swi := 0; swi <= op.stride; swi++ {
out.SetAt(val, bi, ci, hout+shi, wout+swi)
}
}
}
}
}
}
return out, nil
}
func (op *upsampleOp) DiffWRT(inputs int) []bool { return []bool{true} }
func (op *upsampleOp) SymDiff(inputs gorgonia.Nodes, output, grad *gorgonia.Node) (retVal gorgonia.Nodes, err error) {
if err = checkArity(op, len(inputs)); err != nil {
return
}
input := inputs[0]
var op2 upsampleOp
op2 = *op
diff := &upsampleDiffOp{op2}
var ret *gorgonia.Node
if ret, err = gorgonia.ApplyOp(diff, input, output, grad); err != nil {
return nil, err
}
return gorgonia.Nodes{ret}, nil
}
type upsampleDiffOp struct {
upsampleOp
}
func (op *upsampleDiffOp) Arity() int { return 3 }
func (op *upsampleDiffOp) Type() hm.Type {
a := hm.TypeVariable('a')
t := gorgonia.TensorType{Dims: 4, Of: a}
return hm.NewFnType(t, t, t, t)
}
func (op *upsampleDiffOp) InferShape(inputs ...gorgonia.DimSizer) (tensor.Shape, error) {
return inputs[0].(tensor.Shape).Clone(), nil
}
func (op *upsampleDiffOp) checkInput(inputs ...gorgonia.Value) (in, pooled, pooledGrad tensor.Tensor, err error) {
if err = checkArity(op, len(inputs)); err != nil {
return
}
var ok bool
if in, ok = inputs[0].(tensor.Tensor); !ok {
err = errors.Errorf("Expected input to be a tensor")
return
}
if in.Shape().Dims() != 4 {
err = errors.Errorf("Expected input to have 4 dimensions")
return
}
if pooled, ok = inputs[1].(tensor.Tensor); !ok {
err = errors.Errorf("Expected pooled to be a tensor")
return
}
if pooledGrad, ok = inputs[2].(tensor.Tensor); !ok {
err = errors.Errorf("Expected pooledGrad to be a tensor")
return
}
return
}
func (op *upsampleDiffOp) Do(inputs ...gorgonia.Value) (retVal gorgonia.Value, err error) {
var gradIn tensor.Tensor
in, pooled, pooledGrad, err := op.checkInput(inputs...)
if err != nil {
return nil, err
}
insh := in.Shape()
gradIn = tensor.New(tensor.Of(in.Dtype()), tensor.WithShape(in.Shape().Clone()...), tensor.WithEngine(in.Engine()))
b, c, h, w := insh[0], insh[1], insh[2], insh[3]
for bi := 0; bi < b; bi++ {
for ci := 0; ci < c; ci++ {
for hi := 0; hi < h; hi++ {
for wi := 0; wi < w; wi++ {
summ := 0.
for sh := 0; sh <= op.stride; sh++ {
for sw := 0; sw <= op.stride; sw++ {
val, err := pooledGrad.At(bi, ci, hi*(op.stride+1)+sh, wi*(op.stride+1)+sw) //!
if err != nil {
return nil, errors.Errorf("Error accessing input data at [%v, %v, %v, %v]", bi, ci, hi, wi)
}
if pooled.Dtype() == tensor.Float32 {
summ += float64(val.(float32))
} else if pooled.Dtype() == tensor.Float64 {
summ += val.(float64)
}
}
}
if pooled.Dtype() == tensor.Float32 {
gradIn.SetAt(float32(summ), bi, ci, hi, wi)
}
if pooled.Dtype() == tensor.Float64 {
gradIn.SetAt(summ, bi, ci, hi, wi)
}
}
}
}
}
return gradIn, nil
}