-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvolume.go
386 lines (314 loc) · 8.58 KB
/
volume.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package robonet
import (
"errors"
"fmt"
//"github.com/Kunde21/numgo"
"github.com/gonum/matrix/mat64"
"log"
"math"
"math/rand"
)
// Volume is a basic type to hold the layer's info
type Volume interface {
//New() Volume
//NewFull() Volume
//NewRand() Volume
Apply(Kernel, int, int)
Collumns() int
Depth() int
Elems() int
EqualSize(Volume) bool
Equals(Volume) bool
GetAt(int, int, int) float64
Max() float64
Min() float64
MulElem(Volume)
Norm(float64)
PointReflect()
Print()
Reflect()
Rows() int
SetAll(Volume)
SetAt(int, int, int, float64)
Shape() (int, int, int)
SimilarTo(Volume, float64) bool
SubVolume(int, int, int, int) Volume
SubVolumePadded(int, int, int, int) Volume
}
// D3Volume implements Volume
type D3Volume struct {
Fields []mat64.Dense
}
//SetAll sets all values of the volume from another equal-sized volume
func (vol *D3Volume) SetAll(v Volume) {
if v == nil {
panic("tried to set Volume with nil input")
}
if !EqualVolDim(vol, v) {
fmt.Printf("set %vx%vx%v with %vx%vx%v", vol.Rows(), vol.Collumns(), vol.Depth(), v.Rows(), v.Collumns(), v.Depth())
log.Fatal(errors.New("D3Volumedimensions do not match"))
}
*vol = *v.(*D3Volume)
}
//Shape returns the extend in every dimension of a D3Volume
func (vol *D3Volume) Shape() (int, int, int) {
d := len(vol.Fields)
if d != 0 {
r, c := vol.Fields[0].Dims()
return r, c, d
}
return 0, 0, 0
}
//Apply applys the given kernel to the whole volume, returnung a D3Volume with 1 depth
func (vol *D3Volume) Apply(kern Kernel, strideR, strideC int) {
r, c, _ := vol.Shape()
r2, c2, _ := kern.Shape()
if r%strideR != 0 || c%strideC != 0 {
log.Fatal(errors.New("strides not applicable for this volume size"))
}
res := New(r/strideR, c/strideC, 1)
for i := 0; i < r/strideR; i++ {
for j := 0; j < c/strideC; j++ {
res.SetAt(i, j, 0, kern.Apply(vol.SubVolumePadded(i*strideR, j*strideC, r2, c2)))
}
}
//TODO normalize
*vol = *res
}
//Norm normalizes the volume to a given maximum and 0
func (vol *D3Volume) Norm(max float64) {
volmin := vol.Min()
for r := 0; r < vol.Rows(); r++ {
for c := 0; c < vol.Collumns(); c++ {
for d := 0; d < vol.Depth(); d++ {
vol.SetAt(r, c, d, (vol.GetAt(r, c, d))-volmin)
}
}
}
volmin = vol.Min()
volmax := vol.Max()
for r := 0; r < vol.Rows(); r++ {
for c := 0; c < vol.Collumns(); c++ {
for d := 0; d < vol.Depth(); d++ {
val := ((vol.GetAt(r, c, d)) * max / volmax)
vol.SetAt(r, c, d, val)
}
}
}
}
//New generates a D3Volume of fixed size filled with zeros
func New(r, c, d int) *D3Volume {
v := new(D3Volume)
v.Fields = []mat64.Dense{}
for i := 0; i < d; i++ {
v.Fields = append(v.Fields, *mat64.NewDense(r, c, nil))
}
return v
}
//NewWithData generates a D3Volume of fixed size filled with custom data
func NewWithData(r, c, d int, data []float64) *D3Volume {
if len(data) != r*c*d {
fmt.Printf("supplied %v data items for %v length", len(data), r*c*d)
panic("data length does not match dimessions")
}
v := New(r, c, d)
count := 0
for id := 0; id < d; id++ {
for ir := 0; ir < r; ir++ {
for ic := 0; ic < c; ic++ {
v.SetAt(ir, ic, id, data[count])
count++
}
}
}
return v
}
//NewRand generates a D3Volume of fixed size filled with values between 0 and 1
func NewRand(r, c, d int) *D3Volume {
data := []float64{}
for i := 0; i < r*c*d; i++ {
data = append(data, rand.Float64())
}
return NewWithData(r, c, d, data)
}
//NewFull generates a D3Volume of fixed size filled with values between 0 and 1
func NewFull(r, c, d int, fil float64) Volume {
data := []float64{}
for i := 0; i < r*c*d; i++ {
data = append(data, fil)
}
return NewWithData(r, c, d, data)
}
//SubVolumePadded returns a part of the original D3Volume. cR and cC determine the center of copying, r and c the size of the subvolume.
//If the size exceeds the underlying volume the submodule is filled(padded with Zeros.
func (vol D3Volume) SubVolumePadded(cR, cC, r, c int) Volume {
if r%2 == 0 || c%2 == 0 {
log.Fatal(errors.New("Even dimensions not allowed for subvolumes"))
}
sub := New(r, c, vol.Depth())
for id := 0; id < sub.Depth(); id++ {
for ir := 0; ir < sub.Rows(); ir++ {
for ic := 0; ic < sub.Collumns(); ic++ {
cordR := ir + cR + (-r+1)/2
cordC := ic + cC + (-c+1)/2
if cordR < 0 || cordR > vol.Rows()-1 || cordC < 0 || cordC > vol.Collumns()-1 {
} else {
sub.SetAt(ir, ic, id, vol.GetAt(cordR, cordC, id))
}
}
}
}
return sub
}
//SubVolume returns a part of the original D3Volume. tR and tC determine the center of copying, r and c the size of the subvolume.
//If the size exceeds the underlying volume the an error will be thrown, padding is not allowed.
func (vol *D3Volume) SubVolume(tR, tC, r, c int) Volume {
if tR+r > vol.Rows() || tC+c > vol.Collumns() {
log.Fatal(errors.New("D3Volume: Subvolume size exceeds volume dimensions"))
}
sub := New(r, c, vol.Depth())
for ir := 0; ir < sub.Rows(); ir++ {
for ic := 0; ic < sub.Collumns(); ic++ {
for id := 0; id < sub.Depth(); id++ {
sub.SetAt(ir, ic, id, vol.GetAt(tR+ir, tC+ic, id))
}
}
}
return sub
}
//Equals compares the volume to another volume
func (vol *D3Volume) Equals(in Volume) bool {
return vol.SimilarTo(in, 0)
}
//GetAt returns the element of the volume at a given position
func (vol *D3Volume) GetAt(r, c, d int) float64 {
return vol.Fields[d].At(r, c)
}
//SetAt sets the element of a volume at a given position
func (vol *D3Volume) SetAt(r, c, d int, val float64) {
if r >= vol.Rows() || c >= vol.Collumns() || d >= vol.Depth() {
fmt.Printf("SetAt request out of bounds (RxCxD) = %vx%vx%v requested for (RxCxD) = %vx%vx%vx", r, c, d, vol.Rows(), vol.Collumns(), vol.Depth())
panic("out od bounds")
log.Fatal(errors.New("robonet.D3Volume: setAt out of bounds"))
}
vol.Fields[d].Set(r, c, val)
}
//Print prints the D3Volume to the console in a pretty format
func (vol *D3Volume) Print() {
for i := range vol.Fields {
fa := mat64.Formatted(&vol.Fields[i], mat64.Prefix(" "))
fmt.Printf("Layer %v:\n\n %v\n\n", i, fa)
}
}
// Rows of the D3Volume
func (vol *D3Volume) Rows() int {
r, _, _ := vol.Shape()
return r
}
// Collumns of the D3Volume
func (vol *D3Volume) Collumns() int {
_, c, _ := vol.Shape()
return c
}
//Depth of the D3Volume
func (vol *D3Volume) Depth() int {
_, _, d := vol.Shape()
return d
}
//EqualSize checks if the size of two volumes are the same
func (vol *D3Volume) EqualSize(a Volume) bool {
if a == nil {
return false
}
i1, i2, i3 := vol.Shape()
e1, e2, e3 := a.Shape()
return Equal3Dim(i1, i2, i3, e1, e2, e3)
}
//PointReflect calculates the pointreflection of a volume
func (vol *D3Volume) PointReflect() {
r, c, d := vol.Shape()
temp := New(c, r, d)
for id := 0; id < d; id++ {
for ir := 0; ir < r; ir++ {
for ic := 0; ic < c; ic++ {
temp.SetAt(ic, ir, id, vol.GetAt(ir, ic, id))
}
}
}
*vol = *temp
}
//Reflect calculates the reflectio of a volume (left-right)
func (vol *D3Volume) Reflect() {
r, c, d := vol.Shape()
temp := New(r, c, d)
for id := 0; id < d; id++ {
for ir := 0; ir < r; ir++ {
for ic := 0; ic < c; ic++ {
temp.SetAt(ir, ic, id, vol.GetAt(ir, c-(ic+1), id))
}
}
}
*vol = *temp
}
//MulElem multiplies the volume with another volume element-wise
func (vol *D3Volume) MulElem(v1 Volume) {
r, c, d := vol.Shape()
res := New(r, c, d)
for i := 0; i < r; i++ {
for j := 0; j < c; j++ {
for k := 0; k < d; k++ {
res.SetAt(i, j, k, vol.GetAt(i, j, k)*v1.GetAt(i, j, k))
}
}
}
*vol = *res
}
//Max returns the hightest number in a volume
func (vol D3Volume) Max() float64 {
max := 0.0
for i := 0; i < vol.Rows(); i++ {
for j := 0; j < vol.Collumns(); j++ {
for k := 0; k < vol.Depth(); k++ {
if vol.GetAt(i, j, k) > max {
max = vol.GetAt(i, j, k)
}
}
}
}
return max
}
//Min returns the lowest number in a volume
func (vol D3Volume) Min() float64 {
min := 0.0
for i := 0; i < vol.Rows(); i++ {
for j := 0; j < vol.Collumns(); j++ {
for k := 0; k < vol.Depth(); k++ {
if vol.GetAt(i, j, k) < min {
min = vol.GetAt(i, j, k)
}
}
}
}
return min
}
//SimilarTo compares two volumes with a given threshold
func (vol *D3Volume) SimilarTo(in Volume, threshold float64) bool {
if !vol.EqualSize(in) {
return false
}
r, c, d := vol.Shape()
for i1 := 0; i1 < r; i1++ {
for i2 := 0; i2 < c; i2++ {
for i3 := 0; i3 < d; i3++ {
if math.Abs(math.Abs(vol.GetAt(i1, i2, i3))-math.Abs(in.GetAt(i1, i2, i3))) > threshold {
return false
}
}
}
}
return true
}
//Elems returns the number of elements in a volume
func (vol *D3Volume) Elems() int {
return vol.Rows() * vol.Collumns() * vol.Depth()
}