Skip to content

Commit

Permalink
Merge branch 'master' into gomachine/tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
owulveryck committed Aug 14, 2020
2 parents 21e5033 + b49aa45 commit f7774c1
Show file tree
Hide file tree
Showing 28 changed files with 2,234 additions and 0 deletions.
73 changes: 73 additions & 0 deletions examples/tiny-yolo-v3-coco/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Tiny YOLO v3

## Table of Contents

- [About](#about)
- [Theory](#theory)
- [Run example](#run-example)
- [Benchmark](#benchmark)

## About
This is an example of Tiny YOLO v3 neural network.

**Note: do not try to use common yolov3, because shortcut layer is not implemented here**

Folder `model` contains yolov3-tiny.cfg on which file `yolov3_tiny.go` based.

Folder `data` contains image file `dog_416x416.jpg` - this is scaled to 416x416 image for make it better understanding of how net works.

## Theory
You can read about this network [here](https://pjreddie.com/darknet/yolo/).

Architecture of network:
```
0 Convolutional 16 3 × 3/1 416 × 416 × 3 416 × 416 × 16
1 Maxpool 2 × 2/2 416 × 416 × 16 208 × 208 × 16
2 Convolutional 32 3 × 3/1 208 × 208 × 16 208 × 208 × 32
3 Maxpool 2 × 2/2 208 × 208 × 32 104 × 104 × 32
4 Convolutional 64 3 × 3/1 104 × 104 × 32 104 × 104 × 64
5 Maxpool 2 × 2/2 104 × 104 × 64 52 × 52 × 64
6 Convolutional 128 3 × 3/1 52 × 52 × 64 52 × 52 × 128
7 Maxpool 2 × 2/2 52 × 52 × 128 26 × 26 × 128
8 Convolutional 256 3 × 3/1 26 × 26 × 128 26 × 26 × 256
9 Maxpool 2 × 2/2 26 × 26 × 256 13 × 13 × 256
10 Convolutional 512 3 × 3/1 13 × 13 × 256 13 × 13 × 512
11 Maxpool 2 × 2/1 13 × 13 × 512 13 × 13 × 512
12 Convolutional 1024 3 × 3/1 13 × 13 × 512 13 × 13 × 1024
13 Convolutional 256 1 × 1/1 13 × 13 × 1024 13 × 13 × 256
14 Convolutional 512 3 × 3/1 13 × 13 × 256 13 × 13 × 512
15 Convolutional 255 1 × 1/1 13 × 13 × 512 13 × 13 × 255
16 YOLO
17 Route 13
18 Convolutional 128 1 × 1/1 13 × 13 × 256 13 × 13 × 128
19 Up‐sampling 2 × 2/1 13 × 13 × 128 26 × 26 × 128
20 Route 19 8
21 Convolutional 256 3 × 3/1 13 × 13 × 384 13 × 13 × 256
22 Convolutional 255 1 × 1/1 13 × 13 × 256 13 × 13 × 256
23 YOLO
```

You can see source code for each layer's implementation in corresponding files:

Convolution - https://github.com/gorgonia/gorgonia/blob/master/nn.go#L237

Maxpool - https://github.com/gorgonia/gorgonia/blob/master/nn.go#L332

Up-sampling - https://github.com/gorgonia/gorgonia/blob/master/op_upsample.go

Route - [route](route_layer.go)

YOLO - [op_yolo](../../op_yolo.go)

## Run example
How to run:
```go
go run .
```

What you can expect to see:

![std out](output.png)

## Benchmark
Benchmark for network's feedforward function provided [here](main_benchmark_test.go)
70 changes: 70 additions & 0 deletions examples/tiny-yolo-v3-coco/conv_layer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"fmt"

"github.com/pkg/errors"
"gorgonia.org/gorgonia"
"gorgonia.org/tensor"
)

type convLayer struct {
filters int
padding int
kernelSize int
stride int
activation string
activationReLUCoef float64
batchNormalize int
bias bool
biases []float32

layerIndex int

convNode *gorgonia.Node
biasNode *gorgonia.Node
}

func (l *convLayer) String() string {
return fmt.Sprintf(
"Convolution layer: Filters->%[1]d Padding->%[2]d Kernel->%[3]dx%[3]d Stride->%[4]d Activation->%[5]s Batch->%[6]d Bias->%[7]t",
l.filters, l.padding, l.kernelSize, l.stride, l.activation, l.batchNormalize, l.bias,
)
}

func (l *convLayer) Type() string {
return "convolutional"
}

func (l *convLayer) ToNode(g *gorgonia.ExprGraph, input ...*gorgonia.Node) (*gorgonia.Node, error) {

convOut, err := gorgonia.Conv2d(input[0], l.convNode, tensor.Shape{l.kernelSize, l.kernelSize}, []int{l.padding, l.padding}, []int{l.stride, l.stride}, []int{1, 1})
if err != nil {
return &gorgonia.Node{}, errors.Wrap(err, "Can't prepare convolution operation")
}

shp := convOut.Shape()
iters := shp.TotalSize() / len(l.biases)
dataF32 := []float32{}
for b := 0; b < len(l.biases); b++ {
for j := 0; j < iters; j++ {
dataF32 = append(dataF32, l.biases[b])
}
}
biasTensor := tensor.New(tensor.WithBacking(dataF32), tensor.WithShape(shp...))
l.biasNode = gorgonia.NewTensor(g, tensor.Float32, 4, gorgonia.WithShape(shp...), gorgonia.WithName(fmt.Sprintf("bias_%d", l.layerIndex)), gorgonia.WithValue(biasTensor))

biasOut, err := gorgonia.Add(convOut, l.biasNode)
if err != nil {
return &gorgonia.Node{}, errors.Wrap(err, "Can't prepare bias add operation")
}

if l.activation == "leaky" {
activationOut, err := gorgonia.LeakyRelu(biasOut, l.activationReLUCoef)
if err != nil {
return &gorgonia.Node{}, errors.Wrap(err, "Can't prepare activation operation")
}
return activationOut, nil
}
return convOut, nil
}
Binary file added examples/tiny-yolo-v3-coco/data/dog_416x416.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 2 additions & 0 deletions examples/tiny-yolo-v3-coco/data/test_yolo_op/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
16 0.01 0.01 0.1947 0.1971
58 0.625 0.96 0.5865 0.1995
182 changes: 182 additions & 0 deletions examples/tiny-yolo-v3-coco/data/yolov3-tiny.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
[net]
# Testing
batch=1
subdivisions=1
# Training
# batch=64
# subdivisions=2
width=416
height=416
channels=3
momentum=0.9
decay=0.0005
angle=0
saturation = 1.5
exposure = 1.5
hue=.1

learning_rate=0.001
burn_in=1000
max_batches = 500200
policy=steps
steps=400000,450000
scales=.1,.1

[convolutional]
batch_normalize=1
filters=16
size=3
stride=1
pad=1
activation=leaky

[maxpool]
size=2
stride=2

[convolutional]
batch_normalize=1
filters=32
size=3
stride=1
pad=1
activation=leaky

[maxpool]
size=2
stride=2

[convolutional]
batch_normalize=1
filters=64
size=3
stride=1
pad=1
activation=leaky

[maxpool]
size=2
stride=2

[convolutional]
batch_normalize=1
filters=128
size=3
stride=1
pad=1
activation=leaky

[maxpool]
size=2
stride=2

[convolutional]
batch_normalize=1
filters=256
size=3
stride=1
pad=1
activation=leaky

[maxpool]
size=2
stride=2

[convolutional]
batch_normalize=1
filters=512
size=3
stride=1
pad=1
activation=leaky

[maxpool]
size=2
stride=1

[convolutional]
batch_normalize=1
filters=1024
size=3
stride=1
pad=1
activation=leaky

###########

[convolutional]
batch_normalize=1
filters=256
size=1
stride=1
pad=1
activation=leaky

[convolutional]
filters=512
size=3
stride=1
pad=1
activation=leaky
batch_normalize=1

[convolutional]
size=1
stride=1
pad=1
filters=255
activation=linear



[yolo]
mask = 3,4,5
anchors = 10,14, 23,27, 37,58, 81,82, 135,169, 344,319
classes=80
num=6
jitter=.3
ignore_thresh = .7
truth_thresh = 1
random=1

[route]
layers = -4

[convolutional]
batch_normalize=1
filters=128
size=1
stride=1
pad=1
activation=leaky

[upsample]
stride=2

[route]
layers = -1, 8

[convolutional]
batch_normalize=1
filters=256
size=3
stride=1
pad=1
activation=leaky

[convolutional]
size=1
stride=1
pad=1
filters=255
activation=linear

[yolo]
mask = 0,1,2
anchors = 10,14, 23,27, 37,58, 81,82, 135,169, 344,319
classes=80
num=6
jitter=.3
ignore_thresh = .7
truth_thresh = 1
random=1
Binary file not shown.
Loading

0 comments on commit f7774c1

Please sign in to comment.