├── .gitignore ├── LICENSE.txt ├── README.md ├── argsort.go ├── c2g ├── caffe2go.go ├── interfaces.go ├── setup.go └── utils.go ├── caffe ├── caffe.pb.go └── caffe.proto ├── example ├── lenet.caffemodel ├── main.go └── mnist_zero.png ├── images └── .gitkeep ├── labels └── .gitkeep ├── layers ├── base.go ├── convolution.go ├── convolution_test.go ├── dropout.go ├── dropout_test.go ├── fullconnect.go ├── fullconnect_test.go ├── layer.go ├── lrn.go ├── lrn_test.go ├── pooling.go ├── pooling_test.go ├── relu.go ├── relu_test.go ├── softmax.go ├── softmax_test.go ├── utils.go └── utils_test.go ├── main.go ├── mean.txt ├── models └── .gitkeep └── network └── network.go /.gitignore: -------------------------------------------------------------------------------- 1 | ### https://raw.github.com/github/gitignore/9f6724149b9a0a861b402683f6c50c5f085d130b/go.gitignore 2 | 3 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 4 | *.o 5 | *.a 6 | *.so 7 | 8 | # Folders 9 | _obj 10 | _test 11 | 12 | # Architecture specific extensions/prefixes 13 | *.[568vq] 14 | [568vq].out 15 | 16 | *.cgo1.go 17 | *.cgo2.c 18 | _cgo_defun.c 19 | _cgo_gotypes.go 20 | _cgo_export.* 21 | 22 | _testmain.go 23 | 24 | *.exe 25 | *.test 26 | *.prof 27 | 28 | # Output of the go coverage tool, specifically when used with LiteIDE 29 | *.out 30 | 31 | caffe2go 32 | models/* 33 | images/* 34 | labels/* 35 | run.sh 36 | !*.gitkeep 37 | vendor 38 | example/example 39 | *.pprof 40 | dist 41 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016, Alphard Alshua 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 5 | 6 | 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 7 | 8 | 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 9 | 10 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Caffe2Go 2 | 3 | Caffe2Go evaluate caffemodel with Golang 4 | 5 | ## Usage 6 | 7 | Command line interface 8 | 9 | ``` 10 | ./caffe2go -i images/plane.jpg -m models/nin\_imagenet.caffemodel -l labels/synset\_words.txt -s 224 -mf means.txt 11 | ``` 12 | 13 | Options 14 | 15 | ``` 16 | age of ./caffe2go: 17 | -cpuProf string 18 | Filename for CPU profiling. 19 | -i string 20 | Path for image. 21 | -l string 22 | Path for labels. 23 | -m string 24 | Path for caffemodel. 25 | -memProf string 26 | Filename for Memory profiling. 27 | -mf string 28 | Meanfile path 29 | -s uint 30 | Input Shape 31 | 32 | ``` 33 | 34 | Use the library on your own software 35 | 36 | ```go 37 | package main 38 | 39 | import ( 40 | "fmt" 41 | _ "image/jpeg" 42 | _ "image/png" 43 | 44 | "github.com/Rompei/caffe2go/c2g" 45 | ) 46 | 47 | func main() { 48 | caffe2go, err := c2g.NewCaffe2Go("lenet.caffemodel") 49 | if err != nil { 50 | panic(err) 51 | } 52 | output, err := caffe2go.Predict("mnist_zero.png", 28, nil) 53 | if err != nil { 54 | panic(err) 55 | } 56 | 57 | for i := range output { 58 | fmt.Printf("%d: %f\n", i, output[i][0][0]) 59 | } 60 | } 61 | ``` 62 | 63 | ## Supported layers 64 | 65 | Now supports the layers below 66 | 67 | ``` 68 | Convolution 69 | Pooling 70 | ReLU 71 | FullyConnected 72 | Dropout 73 | Softmax 74 | LRN 75 | ``` 76 | 77 | ## License 78 | 79 | [BSD-2](https://opensource.org/licenses/BSD-2-Clause) 80 | -------------------------------------------------------------------------------- /argsort.go: -------------------------------------------------------------------------------- 1 | // https://github.com/gonum/floats/blob/master/floats.go 2 | package main 3 | 4 | import ( 5 | "sort" 6 | ) 7 | 8 | // argsort is a helper that implements sort.Interface, as used by 9 | // Argsort. 10 | type argsort struct { 11 | s []float32 12 | inds []int 13 | } 14 | 15 | func (a argsort) Len() int { 16 | return len(a.s) 17 | } 18 | 19 | func (a argsort) Less(i, j int) bool { 20 | return a.s[i] > a.s[j] 21 | } 22 | 23 | func (a argsort) Swap(i, j int) { 24 | a.s[i], a.s[j] = a.s[j], a.s[i] 25 | a.inds[i], a.inds[j] = a.inds[j], a.inds[i] 26 | } 27 | 28 | // Argsort sorts the elements of s while tracking their original order. 29 | // At the conclusion of Argsort, s will contain the original elements of s 30 | // but sorted in increasing order, and inds will contain the original position 31 | // of the elements in the slice such that dst[i] = origDst[inds[i]]. 32 | // It panics if the lengths of dst and inds do not match. 33 | func Argsort(dst []float32, inds []int) { 34 | if len(dst) != len(inds) { 35 | panic("floats: length of inds does not match length of slice") 36 | } 37 | for i := range dst { 38 | inds[i] = i 39 | } 40 | 41 | a := argsort{s: dst, inds: inds} 42 | sort.Sort(a) 43 | } 44 | -------------------------------------------------------------------------------- /c2g/caffe2go.go: -------------------------------------------------------------------------------- 1 | package c2g 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "image" 7 | "image/color" 8 | "io/ioutil" 9 | "log" 10 | "os" 11 | 12 | "github.com/golang/protobuf/proto" 13 | "github.com/lon9/caffe2go/caffe" 14 | "github.com/lon9/caffe2go/layers" 15 | "github.com/lon9/caffe2go/network" 16 | "github.com/nfnt/resize" 17 | ) 18 | 19 | // Caffe2Go is interface of caffe2go. 20 | type Caffe2Go struct { 21 | Network *network.Network 22 | } 23 | 24 | // NewCaffe2Go is constructor. 25 | func NewCaffe2Go(modelPath string) (*Caffe2Go, error) { 26 | data, err := ioutil.ReadFile(modelPath) 27 | if err != nil { 28 | log.Fatalln(err) 29 | } 30 | var netParameter caffe.NetParameter 31 | if err = proto.Unmarshal(data, &netParameter); err != nil { 32 | log.Fatalln(err) 33 | } 34 | var net network.Network 35 | if len(netParameter.GetLayer()) != 0 { 36 | showLayers(netParameter.GetLayer()) 37 | for i := range netParameter.GetLayer() { 38 | switch netParameter.GetLayer()[i].GetType() { 39 | case layers.InnerProduct: 40 | fmt.Println(layers.InnerProduct) 41 | fcLayer, err := SetupFullconnect(netParameter.GetLayer()[i]) 42 | if err != nil { 43 | return nil, err 44 | } 45 | net.Add(fcLayer) 46 | fmt.Println() 47 | case layers.Convolution: 48 | fmt.Println(layers.Convolution) 49 | convLayer := SetupConvolution(netParameter.GetLayer()[i]) 50 | net.Add(convLayer) 51 | fmt.Println() 52 | case layers.Pooling: 53 | fmt.Println(layers.Pooling) 54 | poolLayer := SetupPooling(netParameter.GetLayer()[i]) 55 | net.Add(poolLayer) 56 | fmt.Println() 57 | case layers.Dropout: 58 | fmt.Println(layers.Dropout) 59 | dropoutLayer := SetupDropout(netParameter.GetLayer()[i]) 60 | net.Add(dropoutLayer) 61 | fmt.Println() 62 | case layers.Softmax: 63 | fmt.Println(caffe.V1LayerParameter_SOFTMAX) 64 | softMaxLayer := SetupSoftmaxLoss(netParameter.GetLayer()[i]) 65 | net.Add(softMaxLayer) 66 | fmt.Println() 67 | case layers.ReLU: 68 | fmt.Println(layers.ReLU) 69 | reluLayer := SetupReLU(netParameter.GetLayer()[i]) 70 | net.Add(reluLayer) 71 | fmt.Println() 72 | case layers.SoftmaxWithLoss: 73 | fmt.Println(layers.SoftmaxWithLoss) 74 | softmaxLossLayer := SetupSoftmaxLoss(netParameter.GetLayer()[i]) 75 | net.Add(softmaxLossLayer) 76 | fmt.Println() 77 | case layers.Lrn: 78 | fmt.Println(layers.Lrn) 79 | lrnLayer := SetupLRN(netParameter.GetLayer()[i]) 80 | net.Add(lrnLayer) 81 | fmt.Println() 82 | } 83 | } 84 | } else { 85 | showV1Layers(netParameter.GetLayers()) 86 | for i := range netParameter.GetLayers() { 87 | switch netParameter.GetLayers()[i].GetType() { 88 | case caffe.V1LayerParameter_INNER_PRODUCT: 89 | fmt.Println(caffe.V1LayerParameter_INNER_PRODUCT) 90 | fcLayer, err := SetupFullconnect(netParameter.GetLayers()[i]) 91 | if err != nil { 92 | return nil, err 93 | } 94 | net.Add(fcLayer) 95 | fmt.Println() 96 | case caffe.V1LayerParameter_CONVOLUTION: 97 | fmt.Println(caffe.V1LayerParameter_CONVOLUTION) 98 | convLayer := SetupConvolution(netParameter.GetLayers()[i]) 99 | net.Add(convLayer) 100 | fmt.Println() 101 | case caffe.V1LayerParameter_POOLING: 102 | fmt.Println(caffe.V1LayerParameter_POOLING) 103 | poolLayer := SetupPooling(netParameter.GetLayers()[i]) 104 | net.Add(poolLayer) 105 | fmt.Println() 106 | case caffe.V1LayerParameter_DROPOUT: 107 | fmt.Println(caffe.V1LayerParameter_DROPOUT) 108 | dropoutLayer := SetupDropout(netParameter.GetLayers()[i]) 109 | net.Add(dropoutLayer) 110 | fmt.Println() 111 | case caffe.V1LayerParameter_SOFTMAX: 112 | fmt.Println(caffe.V1LayerParameter_SOFTMAX) 113 | softMaxLayer := SetupSoftmaxLoss(netParameter.GetLayers()[i]) 114 | net.Add(softMaxLayer) 115 | fmt.Println() 116 | case caffe.V1LayerParameter_RELU: 117 | fmt.Println(caffe.V1LayerParameter_RELU) 118 | reluLayer := SetupReLU(netParameter.GetLayers()[i]) 119 | net.Add(reluLayer) 120 | fmt.Println() 121 | case caffe.V1LayerParameter_SOFTMAX_LOSS: 122 | fmt.Println(caffe.V1LayerParameter_SOFTMAX_LOSS) 123 | softmaxLossLayer := SetupSoftmaxLoss(netParameter.GetLayers()[i]) 124 | net.Add(softmaxLossLayer) 125 | fmt.Println() 126 | case caffe.V1LayerParameter_LRN: 127 | fmt.Println(caffe.V1LayerParameter_LRN) 128 | lrnLayer := SetupLRN(netParameter.GetLayers()[i]) 129 | net.Add(lrnLayer) 130 | fmt.Println() 131 | } 132 | } 133 | } 134 | return &Caffe2Go{ 135 | Network: &net, 136 | }, nil 137 | } 138 | 139 | // Predict start network. 140 | func (c2g *Caffe2Go) Predict(imagePath string, size uint, means []float32) ([][][]float32, error) { 141 | reader, err := os.Open(imagePath) 142 | if err != nil { 143 | return nil, err 144 | } 145 | defer reader.Close() 146 | 147 | img, _, err := image.Decode(reader) 148 | if err != nil { 149 | return nil, err 150 | } 151 | img = resize.Resize(size, size, img, resize.Lanczos3) 152 | input := im2vec(img, means) 153 | //input, err = crop(input, 224) 154 | if err != nil { 155 | return nil, err 156 | } 157 | return c2g.Network.Predict(input) 158 | } 159 | 160 | func im2vec(img image.Image, means []float32) [][][]float32 { 161 | bounds := img.Bounds() 162 | width := bounds.Max.X 163 | height := bounds.Max.Y 164 | var res [][][]float32 165 | if img.ColorModel() == color.GrayModel { 166 | res = make([][][]float32, 1) 167 | res[0] = make([][]float32, height) 168 | } else { 169 | res = make([][][]float32, 3) 170 | for i := 0; i < 3; i++ { 171 | res[i] = make([][]float32, height) 172 | } 173 | } 174 | for y := 0; y < height; y++ { 175 | for i := 0; i < len(res); i++ { 176 | res[i][y] = make([]float32, width) 177 | } 178 | for x := 0; x < width; x++ { 179 | c := img.At(x, y) 180 | if img.ColorModel() == color.GrayModel { 181 | grayColor := img.ColorModel().Convert(c) 182 | if means != nil { 183 | res[0][y][x] = float32(grayColor.(color.Gray).Y) - means[0] 184 | } else { 185 | res[0][y][x] = float32(grayColor.(color.Gray).Y) 186 | } 187 | } else { 188 | r, g, b, _ := c.RGBA() 189 | if means != nil { 190 | res[0][y][x] = (float32(r)/255 - means[0]) 191 | res[1][y][x] = (float32(g)/255 - means[1]) 192 | res[2][y][x] = (float32(b)/255 - means[2]) 193 | } else { 194 | res[0][y][x] = (float32(r) / 255) 195 | res[1][y][x] = (float32(g) / 255) 196 | res[2][y][x] = (float32(b) / 255) 197 | } 198 | } 199 | } 200 | } 201 | return res 202 | } 203 | 204 | func crop(tensor [][][]float32, l int) ([][][]float32, error) { 205 | w := len(tensor[0][0]) 206 | h := len(tensor[0]) 207 | if h < l || w < l { 208 | return nil, errors.New("Length is mismatched") 209 | } 210 | var w1, h1 int 211 | if w > h { 212 | w1 = l * w / h 213 | h1 = l 214 | } else { 215 | w1 = l 216 | h1 = l * h / w 217 | } 218 | sx := (w1 - l) / 2 219 | sy := (h1 - l) / 2 220 | res := make([][][]float32, len(tensor)) 221 | for i := range tensor { 222 | res[i] = make([][]float32, l) 223 | for j, s1 := range tensor[i][sy : sy+l] { 224 | res[i][j] = s1[sx : sx+l] 225 | } 226 | } 227 | return res, nil 228 | } 229 | -------------------------------------------------------------------------------- /c2g/interfaces.go: -------------------------------------------------------------------------------- 1 | package c2g 2 | 3 | import "github.com/lon9/caffe2go/caffe" 4 | 5 | // LayerParameter is alias for LayerParameter. 6 | type LayerParameter interface { 7 | GetName() string 8 | GetBottom() []string 9 | GetTop() []string 10 | GetLossWeight() []float32 11 | GetBlobs() []*caffe.BlobProto 12 | GetInclude() []*caffe.NetStateRule 13 | GetExclude() []*caffe.NetStateRule 14 | GetTransformParam() *caffe.TransformationParameter 15 | GetLossParam() *caffe.LossParameter 16 | GetAccuracyParam() *caffe.AccuracyParameter 17 | GetArgmaxParam() *caffe.ArgMaxParameter 18 | GetConcatParam() *caffe.ConcatParameter 19 | GetContrastiveLossParam() *caffe.ContrastiveLossParameter 20 | GetConvolutionParam() *caffe.ConvolutionParameter 21 | GetDataParam() *caffe.DataParameter 22 | GetDropoutParam() *caffe.DropoutParameter 23 | GetDummyDataParam() *caffe.DummyDataParameter 24 | GetEltwiseParam() *caffe.EltwiseParameter 25 | GetExpParam() *caffe.ExpParameter 26 | GetHdf5DataParam() *caffe.HDF5DataParameter 27 | GetHdf5OutputParam() *caffe.HDF5OutputParameter 28 | GetHingeLossParam() *caffe.HingeLossParameter 29 | GetImageDataParam() *caffe.ImageDataParameter 30 | GetInfogainLossParam() *caffe.InfogainLossParameter 31 | GetInnerProductParam() *caffe.InnerProductParameter 32 | GetLrnParam() *caffe.LRNParameter 33 | GetMemoryDataParam() *caffe.MemoryDataParameter 34 | GetMvnParam() *caffe.MVNParameter 35 | GetPoolingParam() *caffe.PoolingParameter 36 | GetPowerParam() *caffe.PowerParameter 37 | GetReluParam() *caffe.ReLUParameter 38 | GetSigmoidParam() *caffe.SigmoidParameter 39 | GetSoftmaxParam() *caffe.SoftmaxParameter 40 | GetSliceParam() *caffe.SliceParameter 41 | GetTanhParam() *caffe.TanHParameter 42 | GetThresholdParam() *caffe.ThresholdParameter 43 | GetWindowDataParam() *caffe.WindowDataParameter 44 | } 45 | 46 | // Parameter is alias of parameter. 47 | type Parameter interface { 48 | GetKernelH() uint32 49 | GetKernelW() uint32 50 | GetKernelSize() uint32 51 | GetStrideH() uint32 52 | GetStrideW() uint32 53 | GetStride() uint32 54 | GetPad() uint32 55 | GetPadH() uint32 56 | GetPadW() uint32 57 | } 58 | 59 | // Blob is alias of Blob. 60 | type Blob interface { 61 | GetNum() int32 62 | GetShape() *caffe.BlobShape 63 | GetHeight() int32 64 | GetWidth() int32 65 | GetChannels() int32 66 | } 67 | -------------------------------------------------------------------------------- /c2g/setup.go: -------------------------------------------------------------------------------- 1 | package c2g 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | 7 | "github.com/lon9/caffe2go/layers" 8 | ) 9 | 10 | // SetupConvolution setups ConvolutionLayer from caffe model. 11 | func SetupConvolution(layer LayerParameter) *layers.ConvolutionLayer { 12 | blobs := layer.GetBlobs() 13 | param := layer.GetConvolutionParam() 14 | kernelSize := getKernelSize(param) 15 | fmt.Println("KernelSize: ", kernelSize) 16 | stride := getStride(param) 17 | fmt.Println("Stride: ", stride) 18 | pad := getPad(param) 19 | fmt.Println("Padding: ", pad) 20 | num := getNnum(blobs[0]) 21 | channels := getChannels(blobs[0]) 22 | nIn := uint32(channels) * param.GetGroup() 23 | fmt.Println("nInput: ", nIn) 24 | nOut := uint32(num) 25 | fmt.Println("nOutput: ", nOut) 26 | fmt.Println("Group: ", param.GetGroup()) 27 | biasTerm := param.GetBiasTerm() 28 | fmt.Println("BiasTerm: ", biasTerm) 29 | convLayer := layers.NewConvolutionLayer(layer.GetName(), layers.Convolution, nIn, nOut, kernelSize, stride, pad, biasTerm) 30 | idx := 0 31 | // Calculate kernelsize 32 | for i := 0; i < int(nOut); i++ { 33 | for j := 0; j < int(nIn); j++ { 34 | for k := 0; k < int(kernelSize); k++ { 35 | for l := 0; l < int(kernelSize); l++ { 36 | convLayer.Weights[i][j][k][l] = blobs[0].GetData()[idx] 37 | idx++ 38 | } 39 | } 40 | } 41 | } 42 | if biasTerm { 43 | convLayer.Bias = blobs[1].GetData() 44 | } 45 | return convLayer 46 | } 47 | 48 | // SetupFullconnect setups FullConnectLayer. 49 | func SetupFullconnect(layer LayerParameter) (*layers.FullconnectLayer, error) { 50 | param := layer.GetInnerProductParam() 51 | blobs := layer.GetBlobs() 52 | width, err := getWidth(blobs[0]) 53 | if err != nil { 54 | return nil, err 55 | } 56 | fmt.Println("Width: ", width) 57 | height, err := getHeight(blobs[0]) 58 | if err != nil { 59 | return nil, err 60 | } 61 | fmt.Println("Height: ", height) 62 | biasTerm := param.GetBiasTerm() 63 | fmt.Println("BiasTerm: ", biasTerm) 64 | fcLayer := layers.NewFullconnectLayer(layer.GetName(), layers.InnerProduct, width, height, biasTerm) 65 | weights := blobs[0].GetData() 66 | for i := 0; i < len(weights)/int(width); i++ { 67 | fcLayer.Weights[i] = weights[i*int(width) : i*int(width)+int(width)] 68 | } 69 | fmt.Println(len(fcLayer.Weights), len(fcLayer.Weights[0])) 70 | if biasTerm { 71 | fcLayer.Bias = blobs[1].GetData() 72 | } 73 | 74 | return fcLayer, nil 75 | } 76 | 77 | // SetupPooling setups PoolingLayer from caffe model. 78 | func SetupPooling(layer LayerParameter) *layers.PoolingLayer { 79 | param := layer.GetPoolingParam() 80 | kernelSize := getKernelSize(param) 81 | fmt.Println("KernelSize: ", kernelSize) 82 | stride := getStride(param) 83 | fmt.Println("Stride: ", stride) 84 | pad := getPad(param) 85 | fmt.Println("Padding: ", pad) 86 | return layers.NewPoolingLayer(layer.GetName(), layers.Pooling, kernelSize, stride, pad) 87 | } 88 | 89 | // SetupDropout setups DropoutLayer from caffe model. 90 | func SetupDropout(layer LayerParameter) *layers.DropoutLayer { 91 | param := layer.GetDropoutParam() 92 | ratio := param.GetDropoutRatio() 93 | fmt.Println(ratio) 94 | return layers.NewDropoutLayer(layer.GetName(), layers.Dropout, ratio) 95 | } 96 | 97 | // SetupReLU setups ReLULayer from caffe model. 98 | func SetupReLU(layer LayerParameter) *layers.ReLULayer { 99 | param := layer.GetReluParam() 100 | slope := param.GetNegativeSlope() 101 | fmt.Println("Slope: ", slope) 102 | reluLayer := layers.NewReLULayer(layer.GetName(), layers.ReLU, slope) 103 | return reluLayer 104 | } 105 | 106 | // SetupSoftmaxLoss setups SoftmaxLossLayer from caffe model. 107 | func SetupSoftmaxLoss(layer LayerParameter) *layers.SoftmaxLossLayer { 108 | return layers.NewSoftmaxLossLayer(layer.GetName(), layers.SoftmaxWithLoss) 109 | } 110 | 111 | // SetupLRN setups LRNLayer from caffe model. 112 | func SetupLRN(layer LayerParameter) *layers.LRN { 113 | param := layer.GetLrnParam() 114 | k := param.GetK() 115 | fmt.Println("k: ", k) 116 | localSize := param.GetLocalSize() 117 | fmt.Println("LocalSize: ", localSize) 118 | alpha := param.GetAlpha() 119 | fmt.Println("alpha: ", alpha) 120 | beta := param.GetBeta() 121 | fmt.Println("beta: ", beta) 122 | return layers.NewLRNLayer(layer.GetName(), layers.Lrn, int(localSize), float64(k), float64(alpha), float64(beta)) 123 | } 124 | 125 | func getKernelSize(param Parameter) int { 126 | if kernelH := param.GetKernelH(); kernelH > 0 { 127 | return int(kernelH) 128 | } 129 | return int(param.GetKernelSize()) 130 | } 131 | 132 | func getStride(param Parameter) int { 133 | if strideH := param.GetStrideH(); strideH > 0 { 134 | return int(strideH) 135 | } 136 | return int(param.GetStride()) 137 | } 138 | 139 | func getPad(param Parameter) int { 140 | if padH := param.GetPadH(); padH > 0 { 141 | return int(padH) 142 | } 143 | return int(param.GetPad()) 144 | } 145 | 146 | func getNnum(blob Blob) int { 147 | if num := blob.GetNum(); num > 0 { 148 | return int(num) 149 | } 150 | return int(blob.GetShape().GetDim()[0]) 151 | } 152 | 153 | func getChannels(blob Blob) int { 154 | if channels := blob.GetChannels(); channels > 0 { 155 | return int(channels) 156 | } 157 | return int(blob.GetShape().GetDim()[1]) 158 | } 159 | 160 | func getWidth(blob Blob) (int, error) { 161 | if width := blob.GetWidth(); width > 0 { 162 | return int(width), nil 163 | } 164 | if len(blob.GetShape().GetDim()) == 2 { 165 | return int(blob.GetShape().GetDim()[1]), nil 166 | } 167 | if len(blob.GetShape().GetDim()) == 4 { 168 | return int(blob.GetShape().GetDim()[3]), nil 169 | } 170 | return 0, errors.New("Width is not defined.") 171 | } 172 | 173 | func getHeight(blob Blob) (int, error) { 174 | if height := blob.GetHeight(); height > 0 { 175 | return int(height), nil 176 | } 177 | if len(blob.GetShape().GetDim()) == 2 { 178 | return int(blob.GetShape().GetDim()[0]), nil 179 | } 180 | if len(blob.GetShape().GetDim()) == 4 { 181 | return int(blob.GetShape().GetDim()[2]), nil 182 | } 183 | return 0, errors.New("Height is not defined.") 184 | } 185 | -------------------------------------------------------------------------------- /c2g/utils.go: -------------------------------------------------------------------------------- 1 | package c2g 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/lon9/caffe2go/caffe" 7 | ) 8 | 9 | func showV1Layers(layers []*caffe.V1LayerParameter) { 10 | for i := range layers { 11 | fmt.Println(layers[i].GetType()) 12 | } 13 | } 14 | 15 | func showLayers(layers []*caffe.LayerParameter) { 16 | for i := range layers { 17 | fmt.Println(layers[i].GetType()) 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /caffe/caffe.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package caffe; 4 | 5 | // Specifies the shape (dimensions) of a Blob. 6 | message BlobShape { 7 | repeated int64 dim = 1 [packed = true]; 8 | } 9 | 10 | message BlobProto { 11 | optional BlobShape shape = 7; 12 | repeated float data = 5 [packed = true]; 13 | repeated float diff = 6 [packed = true]; 14 | 15 | // 4D dimensions -- deprecated. Use "shape" instead. 16 | optional int32 num = 1 [default = 0]; 17 | optional int32 channels = 2 [default = 0]; 18 | optional int32 height = 3 [default = 0]; 19 | optional int32 width = 4 [default = 0]; 20 | } 21 | 22 | // The BlobProtoVector is simply a way to pass multiple blobproto instances 23 | // around. 24 | message BlobProtoVector { 25 | repeated BlobProto blobs = 1; 26 | } 27 | 28 | message Datum { 29 | optional int32 channels = 1; 30 | optional int32 height = 2; 31 | optional int32 width = 3; 32 | // the actual image data, in bytes 33 | optional bytes data = 4; 34 | optional int32 label = 5; 35 | // Optionally, the datum could also hold float data. 36 | repeated float float_data = 6; 37 | // If true data contains an encoded image that need to be decoded 38 | optional bool encoded = 7 [default = false]; 39 | } 40 | 41 | message FillerParameter { 42 | // The filler type. 43 | optional string type = 1 [default = 'constant']; 44 | optional float value = 2 [default = 0]; // the value in constant filler 45 | optional float min = 3 [default = 0]; // the min value in uniform filler 46 | optional float max = 4 [default = 1]; // the max value in uniform filler 47 | optional float mean = 5 [default = 0]; // the mean value in Gaussian filler 48 | optional float std = 6 [default = 1]; // the std value in Gaussian filler 49 | // The expected number of non-zero output weights for a given input in 50 | // Gaussian filler -- the default -1 means don't perform sparsification. 51 | optional int32 sparse = 7 [default = -1]; 52 | } 53 | 54 | message NetParameter { 55 | optional string name = 1; // consider giving the network a name 56 | // The input blobs to the network. 57 | repeated string input = 3; 58 | // The shape of the input blobs. 59 | repeated BlobShape input_shape = 8; 60 | 61 | // 4D input dimensions -- deprecated. Use "shape" instead. 62 | // If specified, for each input blob there should be four 63 | // values specifying the num, channels, height and width of the input blob. 64 | // Thus, there should be a total of (4 * #input) numbers. 65 | repeated int32 input_dim = 4; 66 | 67 | // Whether the network will force every layer to carry out backward operation. 68 | // If set False, then whether to carry out backward is determined 69 | // automatically according to the net structure and learning rates. 70 | optional bool force_backward = 5 [default = false]; 71 | // The current "state" of the network, including the phase, level, and stage. 72 | // Some layers may be included/excluded depending on this state and the states 73 | // specified in the layers' include and exclude fields. 74 | optional NetState state = 6; 75 | 76 | // Print debugging information about results while running Net::Forward, 77 | // Net::Backward, and Net::Update. 78 | optional bool debug_info = 7 [default = false]; 79 | 80 | // The layers that make up the net. Each of their configurations, including 81 | // connectivity and behavior, is specified as a LayerParameter. 82 | repeated LayerParameter layer = 100; // ID 100 so layers are printed last. 83 | 84 | // DEPRECATED: use 'layer' instead. 85 | repeated V1LayerParameter layers = 2; 86 | } 87 | 88 | // NOTE 89 | // Update the next available ID when you add a new SolverParameter field. 90 | // 91 | // SolverParameter next available ID: 36 (last added: clip_gradients) 92 | message SolverParameter { 93 | ////////////////////////////////////////////////////////////////////////////// 94 | // Specifying the train and test networks 95 | // 96 | // Exactly one train net must be specified using one of the following fields: 97 | // train_net_param, train_net, net_param, net 98 | // One or more test nets may be specified using any of the following fields: 99 | // test_net_param, test_net, net_param, net 100 | // If more than one test net field is specified (e.g., both net and 101 | // test_net are specified), they will be evaluated in the field order given 102 | // above: (1) test_net_param, (2) test_net, (3) net_param/net. 103 | // A test_iter must be specified for each test_net. 104 | // A test_level and/or a test_stage may also be specified for each test_net. 105 | ////////////////////////////////////////////////////////////////////////////// 106 | 107 | // Proto filename for the train net, possibly combined with one or more 108 | // test nets. 109 | optional string net = 24; 110 | // Inline train net param, possibly combined with one or more test nets. 111 | optional NetParameter net_param = 25; 112 | 113 | optional string train_net = 1; // Proto filename for the train net. 114 | repeated string test_net = 2; // Proto filenames for the test nets. 115 | optional NetParameter train_net_param = 21; // Inline train net params. 116 | repeated NetParameter test_net_param = 22; // Inline test net params. 117 | 118 | // The states for the train/test nets. Must be unspecified or 119 | // specified once per net. 120 | // 121 | // By default, all states will have solver = true; 122 | // train_state will have phase = TRAIN, 123 | // and all test_state's will have phase = TEST. 124 | // Other defaults are set according to the NetState defaults. 125 | optional NetState train_state = 26; 126 | repeated NetState test_state = 27; 127 | 128 | // The number of iterations for each test net. 129 | repeated int32 test_iter = 3; 130 | 131 | // The number of iterations between two testing phases. 132 | optional int32 test_interval = 4 [default = 0]; 133 | optional bool test_compute_loss = 19 [default = false]; 134 | // If true, run an initial test pass before the first iteration, 135 | // ensuring memory availability and printing the starting value of the loss. 136 | optional bool test_initialization = 32 [default = true]; 137 | optional float base_lr = 5; // The base learning rate 138 | // the number of iterations between displaying info. If display = 0, no info 139 | // will be displayed. 140 | optional int32 display = 6; 141 | // Display the loss averaged over the last average_loss iterations 142 | optional int32 average_loss = 33 [default = 1]; 143 | optional int32 max_iter = 7; // the maximum number of iterations 144 | optional string lr_policy = 8; // The learning rate decay policy. 145 | optional float gamma = 9; // The parameter to compute the learning rate. 146 | optional float power = 10; // The parameter to compute the learning rate. 147 | optional float momentum = 11; // The momentum value. 148 | optional float weight_decay = 12; // The weight decay. 149 | // regularization types supported: L1 and L2 150 | // controlled by weight_decay 151 | optional string regularization_type = 29 [default = "L2"]; 152 | // the stepsize for learning rate policy "step" 153 | optional int32 stepsize = 13; 154 | // the stepsize for learning rate policy "multistep" 155 | repeated int32 stepvalue = 34; 156 | 157 | // Set clip_gradients to >= 0 to clip parameter gradients to that L2 norm, 158 | // whenever their actual L2 norm is larger. 159 | optional float clip_gradients = 35 [default = -1]; 160 | 161 | optional int32 snapshot = 14 [default = 0]; // The snapshot interval 162 | optional string snapshot_prefix = 15; // The prefix for the snapshot. 163 | // whether to snapshot diff in the results or not. Snapshotting diff will help 164 | // debugging but the final protocol buffer size will be much larger. 165 | optional bool snapshot_diff = 16 [default = false]; 166 | // the mode solver will use: 0 for CPU and 1 for GPU. Use GPU in default. 167 | enum SolverMode { 168 | CPU = 0; 169 | GPU = 1; 170 | } 171 | optional SolverMode solver_mode = 17 [default = GPU]; 172 | // the device_id will that be used in GPU mode. Use device_id = 0 in default. 173 | optional int32 device_id = 18 [default = 0]; 174 | // If non-negative, the seed with which the Solver will initialize the Caffe 175 | // random number generator -- useful for reproducible results. Otherwise, 176 | // (and by default) initialize using a seed derived from the system clock. 177 | optional int64 random_seed = 20 [default = -1]; 178 | 179 | // Solver type 180 | enum SolverType { 181 | SGD = 0; 182 | NESTEROV = 1; 183 | ADAGRAD = 2; 184 | } 185 | optional SolverType solver_type = 30 [default = SGD]; 186 | // numerical stability for AdaGrad 187 | optional float delta = 31 [default = 1e-8]; 188 | 189 | // If true, print information about the state of the net that may help with 190 | // debugging learning problems. 191 | optional bool debug_info = 23 [default = false]; 192 | 193 | // If false, don't save a snapshot after training finishes. 194 | optional bool snapshot_after_train = 28 [default = true]; 195 | } 196 | 197 | // A message that stores the solver snapshots 198 | message SolverState { 199 | optional int32 iter = 1; // The current iteration 200 | optional string learned_net = 2; // The file that stores the learned net. 201 | repeated BlobProto history = 3; // The history for sgd solvers 202 | optional int32 current_step = 4 [default = 0]; // The current step for learning rate 203 | } 204 | 205 | enum Phase { 206 | TRAIN = 0; 207 | TEST = 1; 208 | } 209 | 210 | message NetState { 211 | optional Phase phase = 1 [default = TEST]; 212 | optional int32 level = 2 [default = 0]; 213 | repeated string stage = 3; 214 | } 215 | 216 | message NetStateRule { 217 | // Set phase to require the NetState have a particular phase (TRAIN or TEST) 218 | // to meet this rule. 219 | optional Phase phase = 1; 220 | 221 | // Set the minimum and/or maximum levels in which the layer should be used. 222 | // Leave undefined to meet the rule regardless of level. 223 | optional int32 min_level = 2; 224 | optional int32 max_level = 3; 225 | 226 | // Customizable sets of stages to include or exclude. 227 | // The net must have ALL of the specified stages and NONE of the specified 228 | // "not_stage"s to meet the rule. 229 | // (Use multiple NetStateRules to specify conjunctions of stages.) 230 | repeated string stage = 4; 231 | repeated string not_stage = 5; 232 | } 233 | 234 | // Specifies training parameters (multipliers on global learning constants, 235 | // and the name and other settings used for weight sharing). 236 | message ParamSpec { 237 | // The names of the parameter blobs -- useful for sharing parameters among 238 | // layers, but never required otherwise. To share a parameter between two 239 | // layers, give it a (non-empty) name. 240 | optional string name = 1; 241 | 242 | // Whether to require shared weights to have the same shape, or just the same 243 | // count -- defaults to STRICT if unspecified. 244 | optional DimCheckMode share_mode = 2; 245 | enum DimCheckMode { 246 | // STRICT (default) requires that num, channels, height, width each match. 247 | STRICT = 0; 248 | // PERMISSIVE requires only the count (num*channels*height*width) to match. 249 | PERMISSIVE = 1; 250 | } 251 | 252 | // The multiplier on the global learning rate for this parameter. 253 | optional float lr_mult = 3 [default = 1.0]; 254 | 255 | // The multiplier on the global weight decay for this parameter. 256 | optional float decay_mult = 4 [default = 1.0]; 257 | } 258 | 259 | // NOTE 260 | // Update the next available ID when you add a new LayerParameter field. 261 | // 262 | // LayerParameter next available layer-specific ID: 132 (last added: prelu_param) 263 | message LayerParameter { 264 | optional string name = 1; // the layer name 265 | optional string type = 2; // the layer type 266 | repeated string bottom = 3; // the name of each bottom blob 267 | repeated string top = 4; // the name of each top blob 268 | 269 | // The train / test phase for computation. 270 | optional Phase phase = 10; 271 | 272 | // The amount of weight to assign each top blob in the objective. 273 | // Each layer assigns a default value, usually of either 0 or 1, 274 | // to each top blob. 275 | repeated float loss_weight = 5; 276 | 277 | // Specifies training parameters (multipliers on global learning constants, 278 | // and the name and other settings used for weight sharing). 279 | repeated ParamSpec param = 6; 280 | 281 | // The blobs containing the numeric parameters of the layer. 282 | repeated BlobProto blobs = 7; 283 | 284 | // Rules controlling whether and when a layer is included in the network, 285 | // based on the current NetState. You may specify a non-zero number of rules 286 | // to include OR exclude, but not both. If no include or exclude rules are 287 | // specified, the layer is always included. If the current NetState meets 288 | // ANY (i.e., one or more) of the specified rules, the layer is 289 | // included/excluded. 290 | repeated NetStateRule include = 8; 291 | repeated NetStateRule exclude = 9; 292 | 293 | // Parameters for data pre-processing. 294 | optional TransformationParameter transform_param = 100; 295 | 296 | // Parameters shared by loss layers. 297 | optional LossParameter loss_param = 101; 298 | 299 | // Layer type-specific parameters. 300 | // 301 | // Note: certain layers may have more than one computational engine 302 | // for their implementation. These layers include an Engine type and 303 | // engine parameter for selecting the implementation. 304 | // The default for the engine is set by the ENGINE switch at compile-time. 305 | optional AccuracyParameter accuracy_param = 102; 306 | optional ArgMaxParameter argmax_param = 103; 307 | optional ConcatParameter concat_param = 104; 308 | optional ContrastiveLossParameter contrastive_loss_param = 105; 309 | optional ConvolutionParameter convolution_param = 106; 310 | optional DataParameter data_param = 107; 311 | optional DropoutParameter dropout_param = 108; 312 | optional DummyDataParameter dummy_data_param = 109; 313 | optional EltwiseParameter eltwise_param = 110; 314 | optional ExpParameter exp_param = 111; 315 | optional HDF5DataParameter hdf5_data_param = 112; 316 | optional HDF5OutputParameter hdf5_output_param = 113; 317 | optional HingeLossParameter hinge_loss_param = 114; 318 | optional ImageDataParameter image_data_param = 115; 319 | optional InfogainLossParameter infogain_loss_param = 116; 320 | optional InnerProductParameter inner_product_param = 117; 321 | optional LRNParameter lrn_param = 118; 322 | optional MemoryDataParameter memory_data_param = 119; 323 | optional MVNParameter mvn_param = 120; 324 | optional PoolingParameter pooling_param = 121; 325 | optional PowerParameter power_param = 122; 326 | optional PReLUParameter prelu_param = 131; 327 | optional PythonParameter python_param = 130; 328 | optional ReLUParameter relu_param = 123; 329 | optional SigmoidParameter sigmoid_param = 124; 330 | optional SoftmaxParameter softmax_param = 125; 331 | optional SliceParameter slice_param = 126; 332 | optional TanHParameter tanh_param = 127; 333 | optional ThresholdParameter threshold_param = 128; 334 | optional WindowDataParameter window_data_param = 129; 335 | } 336 | 337 | // Message that stores parameters used to apply transformation 338 | // to the data layer's data 339 | message TransformationParameter { 340 | // For data pre-processing, we can do simple scaling and subtracting the 341 | // data mean, if provided. Note that the mean subtraction is always carried 342 | // out before scaling. 343 | optional float scale = 1 [default = 1]; 344 | // Specify if we want to randomly mirror data. 345 | optional bool mirror = 2 [default = false]; 346 | // Specify if we would like to randomly crop an image. 347 | optional uint32 crop_size = 3 [default = 0]; 348 | // mean_file and mean_value cannot be specified at the same time 349 | optional string mean_file = 4; 350 | // if specified can be repeated once (would substract it from all the channels) 351 | // or can be repeated the same number of times as channels 352 | // (would subtract them from the corresponding channel) 353 | repeated float mean_value = 5; 354 | } 355 | 356 | // Message that stores parameters shared by loss layers 357 | message LossParameter { 358 | // If specified, ignore instances with the given label. 359 | optional int32 ignore_label = 1; 360 | // If true, normalize each batch across all instances (including spatial 361 | // dimesions, but not ignored instances); else, divide by batch size only. 362 | optional bool normalize = 2 [default = true]; 363 | } 364 | 365 | // Message that stores parameters used by AccuracyLayer 366 | message AccuracyParameter { 367 | // When computing accuracy, count as correct by comparing the true label to 368 | // the top k scoring classes. By default, only compare to the top scoring 369 | // class (i.e. argmax). 370 | optional uint32 top_k = 1 [default = 1]; 371 | 372 | // The "label" axis of the prediction blob, whose argmax corresponds to the 373 | // predicted label -- may be negative to index from the end (e.g., -1 for the 374 | // last axis). For example, if axis == 1 and the predictions are 375 | // (N x C x H x W), the label blob is expected to contain N*H*W ground truth 376 | // labels with integer values in {0, 1, ..., C-1}. 377 | optional int32 axis = 2 [default = 1]; 378 | 379 | // If specified, ignore instances with the given label. 380 | optional int32 ignore_label = 3; 381 | } 382 | 383 | // Message that stores parameters used by ArgMaxLayer 384 | message ArgMaxParameter { 385 | // If true produce pairs (argmax, maxval) 386 | optional bool out_max_val = 1 [default = false]; 387 | optional uint32 top_k = 2 [default = 1]; 388 | } 389 | 390 | // Message that stores parameters used by ConcatLayer 391 | message ConcatParameter { 392 | // The axis along which to concatenate -- may be negative to index from the 393 | // end (e.g., -1 for the last axis). Other axes must have the 394 | // same dimension for all the bottom blobs. 395 | // By default, ConcatLayer concatenates blobs along the "channels" axis (1). 396 | optional int32 axis = 2 [default = 1]; 397 | 398 | // DEPRECATED: alias for "axis" -- does not support negative indexing. 399 | optional uint32 concat_dim = 1 [default = 1]; 400 | } 401 | 402 | // Message that stores parameters used by ContrastiveLossLayer 403 | message ContrastiveLossParameter { 404 | //margin for dissimilar pair 405 | optional float margin = 1 [default = 1.0]; 406 | } 407 | 408 | // Message that stores parameters used by ConvolutionLayer 409 | message ConvolutionParameter { 410 | optional uint32 num_output = 1; // The number of outputs for the layer 411 | optional bool bias_term = 2 [default = true]; // whether to have bias terms 412 | // Pad, kernel size, and stride are all given as a single value for equal 413 | // dimensions in height and width or as Y, X pairs. 414 | optional uint32 pad = 3 [default = 0]; // The padding size (equal in Y, X) 415 | optional uint32 pad_h = 9 [default = 0]; // The padding height 416 | optional uint32 pad_w = 10 [default = 0]; // The padding width 417 | optional uint32 kernel_size = 4; // The kernel size (square) 418 | optional uint32 kernel_h = 11; // The kernel height 419 | optional uint32 kernel_w = 12; // The kernel width 420 | optional uint32 group = 5 [default = 1]; // The group size for group conv 421 | optional uint32 stride = 6 [default = 1]; // The stride (equal in Y, X) 422 | optional uint32 stride_h = 13; // The stride height 423 | optional uint32 stride_w = 14; // The stride width 424 | optional FillerParameter weight_filler = 7; // The filler for the weight 425 | optional FillerParameter bias_filler = 8; // The filler for the bias 426 | enum Engine { 427 | DEFAULT = 0; 428 | CAFFE = 1; 429 | CUDNN = 2; 430 | } 431 | optional Engine engine = 15 [default = DEFAULT]; 432 | } 433 | 434 | // Message that stores parameters used by DataLayer 435 | message DataParameter { 436 | enum DB { 437 | LEVELDB = 0; 438 | LMDB = 1; 439 | } 440 | // Specify the data source. 441 | optional string source = 1; 442 | // Specify the batch size. 443 | optional uint32 batch_size = 4; 444 | // The rand_skip variable is for the data layer to skip a few data points 445 | // to avoid all asynchronous sgd clients to start at the same point. The skip 446 | // point would be set as rand_skip * rand(0,1). Note that rand_skip should not 447 | // be larger than the number of keys in the database. 448 | optional uint32 rand_skip = 7 [default = 0]; 449 | optional DB backend = 8 [default = LEVELDB]; 450 | // DEPRECATED. See TransformationParameter. For data pre-processing, we can do 451 | // simple scaling and subtracting the data mean, if provided. Note that the 452 | // mean subtraction is always carried out before scaling. 453 | optional float scale = 2 [default = 1]; 454 | optional string mean_file = 3; 455 | // DEPRECATED. See TransformationParameter. Specify if we would like to randomly 456 | // crop an image. 457 | optional uint32 crop_size = 5 [default = 0]; 458 | // DEPRECATED. See TransformationParameter. Specify if we want to randomly mirror 459 | // data. 460 | optional bool mirror = 6 [default = false]; 461 | // Force the encoded image to have 3 color channels 462 | optional bool force_encoded_color = 9 [default = false]; 463 | } 464 | 465 | // Message that stores parameters used by DropoutLayer 466 | message DropoutParameter { 467 | optional float dropout_ratio = 1 [default = 0.5]; // dropout ratio 468 | } 469 | 470 | // Message that stores parameters used by DummyDataLayer. 471 | // DummyDataLayer fills any number of arbitrarily shaped blobs with random 472 | // (or constant) data generated by "Fillers" (see "message FillerParameter"). 473 | message DummyDataParameter { 474 | // This layer produces N >= 1 top blobs. DummyDataParameter must specify 1 or N 475 | // shape fields, and 0, 1 or N data_fillers. 476 | // 477 | // If 0 data_fillers are specified, ConstantFiller with a value of 0 is used. 478 | // If 1 data_filler is specified, it is applied to all top blobs. If N are 479 | // specified, the ith is applied to the ith top blob. 480 | repeated FillerParameter data_filler = 1; 481 | repeated BlobShape shape = 6; 482 | 483 | // 4D dimensions -- deprecated. Use "shape" instead. 484 | repeated uint32 num = 2; 485 | repeated uint32 channels = 3; 486 | repeated uint32 height = 4; 487 | repeated uint32 width = 5; 488 | } 489 | 490 | // Message that stores parameters used by EltwiseLayer 491 | message EltwiseParameter { 492 | enum EltwiseOp { 493 | PROD = 0; 494 | SUM = 1; 495 | MAX = 2; 496 | } 497 | optional EltwiseOp operation = 1 [default = SUM]; // element-wise operation 498 | repeated float coeff = 2; // blob-wise coefficient for SUM operation 499 | 500 | // Whether to use an asymptotically slower (for >2 inputs) but stabler method 501 | // of computing the gradient for the PROD operation. (No effect for SUM op.) 502 | optional bool stable_prod_grad = 3 [default = true]; 503 | } 504 | 505 | // Message that stores parameters used by ExpLayer 506 | message ExpParameter { 507 | // ExpLayer computes outputs y = base ^ (shift + scale * x), for base > 0. 508 | // Or if base is set to the default (-1), base is set to e, 509 | // so y = exp(shift + scale * x). 510 | optional float base = 1 [default = -1.0]; 511 | optional float scale = 2 [default = 1.0]; 512 | optional float shift = 3 [default = 0.0]; 513 | } 514 | 515 | // Message that stores parameters used by HDF5DataLayer 516 | message HDF5DataParameter { 517 | // Specify the data source. 518 | optional string source = 1; 519 | // Specify the batch size. 520 | optional uint32 batch_size = 2; 521 | 522 | // Specify whether to shuffle the data. 523 | // If shuffle == true, the ordering of the HDF5 files is shuffled, 524 | // and the ordering of data within any given HDF5 file is shuffled, 525 | // but data between different files are not interleaved; all of a file's 526 | // data are output (in a random order) before moving onto another file. 527 | optional bool shuffle = 3 [default = false]; 528 | } 529 | 530 | // Message that stores parameters used by HDF5OutputLayer 531 | message HDF5OutputParameter { 532 | optional string file_name = 1; 533 | } 534 | 535 | message HingeLossParameter { 536 | enum Norm { 537 | L1 = 1; 538 | L2 = 2; 539 | } 540 | // Specify the Norm to use L1 or L2 541 | optional Norm norm = 1 [default = L1]; 542 | } 543 | 544 | // Message that stores parameters used by ImageDataLayer 545 | message ImageDataParameter { 546 | // Specify the data source. 547 | optional string source = 1; 548 | // Specify the batch size. 549 | optional uint32 batch_size = 4; 550 | // The rand_skip variable is for the data layer to skip a few data points 551 | // to avoid all asynchronous sgd clients to start at the same point. The skip 552 | // point would be set as rand_skip * rand(0,1). Note that rand_skip should not 553 | // be larger than the number of keys in the database. 554 | optional uint32 rand_skip = 7 [default = 0]; 555 | // Whether or not ImageLayer should shuffle the list of files at every epoch. 556 | optional bool shuffle = 8 [default = false]; 557 | // It will also resize images if new_height or new_width are not zero. 558 | optional uint32 new_height = 9 [default = 0]; 559 | optional uint32 new_width = 10 [default = 0]; 560 | // Specify if the images are color or gray 561 | optional bool is_color = 11 [default = true]; 562 | // DEPRECATED. See TransformationParameter. For data pre-processing, we can do 563 | // simple scaling and subtracting the data mean, if provided. Note that the 564 | // mean subtraction is always carried out before scaling. 565 | optional float scale = 2 [default = 1]; 566 | optional string mean_file = 3; 567 | // DEPRECATED. See TransformationParameter. Specify if we would like to randomly 568 | // crop an image. 569 | optional uint32 crop_size = 5 [default = 0]; 570 | // DEPRECATED. See TransformationParameter. Specify if we want to randomly mirror 571 | // data. 572 | optional bool mirror = 6 [default = false]; 573 | optional string root_folder = 12 [default = ""]; 574 | } 575 | 576 | // Message that stores parameters InfogainLossLayer 577 | message InfogainLossParameter { 578 | // Specify the infogain matrix source. 579 | optional string source = 1; 580 | } 581 | 582 | // Message that stores parameters used by InnerProductLayer 583 | message InnerProductParameter { 584 | optional uint32 num_output = 1; // The number of outputs for the layer 585 | optional bool bias_term = 2 [default = true]; // whether to have bias terms 586 | optional FillerParameter weight_filler = 3; // The filler for the weight 587 | optional FillerParameter bias_filler = 4; // The filler for the bias 588 | 589 | // The first axis to be lumped into a single inner product computation; 590 | // all preceding axes are retained in the output. 591 | // May be negative to index from the end (e.g., -1 for the last axis). 592 | optional int32 axis = 5 [default = 1]; 593 | } 594 | 595 | // Message that stores parameters used by LRNLayer 596 | message LRNParameter { 597 | optional uint32 local_size = 1 [default = 5]; 598 | optional float alpha = 2 [default = 1.]; 599 | optional float beta = 3 [default = 0.75]; 600 | enum NormRegion { 601 | ACROSS_CHANNELS = 0; 602 | WITHIN_CHANNEL = 1; 603 | } 604 | optional NormRegion norm_region = 4 [default = ACROSS_CHANNELS]; 605 | optional float k = 5 [default = 1.]; 606 | } 607 | 608 | // Message that stores parameters used by MemoryDataLayer 609 | message MemoryDataParameter { 610 | optional uint32 batch_size = 1; 611 | optional uint32 channels = 2; 612 | optional uint32 height = 3; 613 | optional uint32 width = 4; 614 | } 615 | 616 | // Message that stores parameters used by MVNLayer 617 | message MVNParameter { 618 | // This parameter can be set to false to normalize mean only 619 | optional bool normalize_variance = 1 [default = true]; 620 | 621 | // This parameter can be set to true to perform DNN-like MVN 622 | optional bool across_channels = 2 [default = false]; 623 | } 624 | 625 | // Message that stores parameters used by PoolingLayer 626 | message PoolingParameter { 627 | enum PoolMethod { 628 | MAX = 0; 629 | AVE = 1; 630 | STOCHASTIC = 2; 631 | } 632 | optional PoolMethod pool = 1 [default = MAX]; // The pooling method 633 | // Pad, kernel size, and stride are all given as a single value for equal 634 | // dimensions in height and width or as Y, X pairs. 635 | optional uint32 pad = 4 [default = 0]; // The padding size (equal in Y, X) 636 | optional uint32 pad_h = 9 [default = 0]; // The padding height 637 | optional uint32 pad_w = 10 [default = 0]; // The padding width 638 | optional uint32 kernel_size = 2; // The kernel size (square) 639 | optional uint32 kernel_h = 5; // The kernel height 640 | optional uint32 kernel_w = 6; // The kernel width 641 | optional uint32 stride = 3 [default = 1]; // The stride (equal in Y, X) 642 | optional uint32 stride_h = 7; // The stride height 643 | optional uint32 stride_w = 8; // The stride width 644 | enum Engine { 645 | DEFAULT = 0; 646 | CAFFE = 1; 647 | CUDNN = 2; 648 | } 649 | optional Engine engine = 11 [default = DEFAULT]; 650 | // If global_pooling then it will pool over the size of the bottom by doing 651 | // kernel_h = bottom->height and kernel_w = bottom->width 652 | optional bool global_pooling = 12 [default = false]; 653 | } 654 | 655 | // Message that stores parameters used by PowerLayer 656 | message PowerParameter { 657 | // PowerLayer computes outputs y = (shift + scale * x) ^ power. 658 | optional float power = 1 [default = 1.0]; 659 | optional float scale = 2 [default = 1.0]; 660 | optional float shift = 3 [default = 0.0]; 661 | } 662 | 663 | // Message that stores parameters used by PythonLayer 664 | message PythonParameter { 665 | optional string module = 1; 666 | optional string layer = 2; 667 | } 668 | 669 | // Message that stores parameters used by ReLULayer 670 | message ReLUParameter { 671 | // Allow non-zero slope for negative inputs to speed up optimization 672 | // Described in: 673 | // Maas, A. L., Hannun, A. Y., & Ng, A. Y. (2013). Rectifier nonlinearities 674 | // improve neural network acoustic models. In ICML Workshop on Deep Learning 675 | // for Audio, Speech, and Language Processing. 676 | optional float negative_slope = 1 [default = 0]; 677 | enum Engine { 678 | DEFAULT = 0; 679 | CAFFE = 1; 680 | CUDNN = 2; 681 | } 682 | optional Engine engine = 2 [default = DEFAULT]; 683 | } 684 | 685 | // Message that stores parameters used by SigmoidLayer 686 | message SigmoidParameter { 687 | enum Engine { 688 | DEFAULT = 0; 689 | CAFFE = 1; 690 | CUDNN = 2; 691 | } 692 | optional Engine engine = 1 [default = DEFAULT]; 693 | } 694 | 695 | // Message that stores parameters used by SliceLayer 696 | message SliceParameter { 697 | // The axis along which to slice -- may be negative to index from the end 698 | // (e.g., -1 for the last axis). 699 | // By default, SliceLayer concatenates blobs along the "channels" axis (1). 700 | optional int32 axis = 3 [default = 1]; 701 | repeated uint32 slice_point = 2; 702 | 703 | // DEPRECATED: alias for "axis" -- does not support negative indexing. 704 | optional uint32 slice_dim = 1 [default = 1]; 705 | } 706 | 707 | // Message that stores parameters used by SoftmaxLayer, SoftmaxWithLossLayer 708 | message SoftmaxParameter { 709 | enum Engine { 710 | DEFAULT = 0; 711 | CAFFE = 1; 712 | CUDNN = 2; 713 | } 714 | optional Engine engine = 1 [default = DEFAULT]; 715 | 716 | // The axis along which to perform the softmax -- may be negative to index 717 | // from the end (e.g., -1 for the last axis). 718 | // Any other axes will be evaluated as independent softmaxes. 719 | optional int32 axis = 2 [default = 1]; 720 | } 721 | 722 | // Message that stores parameters used by TanHLayer 723 | message TanHParameter { 724 | enum Engine { 725 | DEFAULT = 0; 726 | CAFFE = 1; 727 | CUDNN = 2; 728 | } 729 | optional Engine engine = 1 [default = DEFAULT]; 730 | } 731 | 732 | // Message that stores parameters used by ThresholdLayer 733 | message ThresholdParameter { 734 | optional float threshold = 1 [default = 0]; // Strictly positive values 735 | } 736 | 737 | // Message that stores parameters used by WindowDataLayer 738 | message WindowDataParameter { 739 | // Specify the data source. 740 | optional string source = 1; 741 | // For data pre-processing, we can do simple scaling and subtracting the 742 | // data mean, if provided. Note that the mean subtraction is always carried 743 | // out before scaling. 744 | optional float scale = 2 [default = 1]; 745 | optional string mean_file = 3; 746 | // Specify the batch size. 747 | optional uint32 batch_size = 4; 748 | // Specify if we would like to randomly crop an image. 749 | optional uint32 crop_size = 5 [default = 0]; 750 | // Specify if we want to randomly mirror data. 751 | optional bool mirror = 6 [default = false]; 752 | // Foreground (object) overlap threshold 753 | optional float fg_threshold = 7 [default = 0.5]; 754 | // Background (non-object) overlap threshold 755 | optional float bg_threshold = 8 [default = 0.5]; 756 | // Fraction of batch that should be foreground objects 757 | optional float fg_fraction = 9 [default = 0.25]; 758 | // Amount of contextual padding to add around a window 759 | // (used only by the window_data_layer) 760 | optional uint32 context_pad = 10 [default = 0]; 761 | // Mode for cropping out a detection window 762 | // warp: cropped window is warped to a fixed size and aspect ratio 763 | // square: the tightest square around the window is cropped 764 | optional string crop_mode = 11 [default = "warp"]; 765 | // cache_images: will load all images in memory for faster access 766 | optional bool cache_images = 12 [default = false]; 767 | // append root_folder to locate images 768 | optional string root_folder = 13 [default = ""]; 769 | } 770 | 771 | // DEPRECATED: use LayerParameter. 772 | message V1LayerParameter { 773 | repeated string bottom = 2; 774 | repeated string top = 3; 775 | optional string name = 4; 776 | repeated NetStateRule include = 32; 777 | repeated NetStateRule exclude = 33; 778 | enum LayerType { 779 | NONE = 0; 780 | ABSVAL = 35; 781 | ACCURACY = 1; 782 | ARGMAX = 30; 783 | BNLL = 2; 784 | CONCAT = 3; 785 | CONTRASTIVE_LOSS = 37; 786 | CONVOLUTION = 4; 787 | DATA = 5; 788 | DECONVOLUTION = 39; 789 | DROPOUT = 6; 790 | DUMMY_DATA = 32; 791 | EUCLIDEAN_LOSS = 7; 792 | ELTWISE = 25; 793 | EXP = 38; 794 | FLATTEN = 8; 795 | HDF5_DATA = 9; 796 | HDF5_OUTPUT = 10; 797 | HINGE_LOSS = 28; 798 | IM2COL = 11; 799 | IMAGE_DATA = 12; 800 | INFOGAIN_LOSS = 13; 801 | INNER_PRODUCT = 14; 802 | LRN = 15; 803 | MEMORY_DATA = 29; 804 | MULTINOMIAL_LOGISTIC_LOSS = 16; 805 | MVN = 34; 806 | POOLING = 17; 807 | POWER = 26; 808 | RELU = 18; 809 | SIGMOID = 19; 810 | SIGMOID_CROSS_ENTROPY_LOSS = 27; 811 | SILENCE = 36; 812 | SOFTMAX = 20; 813 | SOFTMAX_LOSS = 21; 814 | SPLIT = 22; 815 | SLICE = 33; 816 | TANH = 23; 817 | WINDOW_DATA = 24; 818 | THRESHOLD = 31; 819 | } 820 | optional LayerType type = 5; 821 | repeated BlobProto blobs = 6; 822 | repeated string param = 1001; 823 | repeated DimCheckMode blob_share_mode = 1002; 824 | enum DimCheckMode { 825 | STRICT = 0; 826 | PERMISSIVE = 1; 827 | } 828 | repeated float blobs_lr = 7; 829 | repeated float weight_decay = 8; 830 | repeated float loss_weight = 35; 831 | optional AccuracyParameter accuracy_param = 27; 832 | optional ArgMaxParameter argmax_param = 23; 833 | optional ConcatParameter concat_param = 9; 834 | optional ContrastiveLossParameter contrastive_loss_param = 40; 835 | optional ConvolutionParameter convolution_param = 10; 836 | optional DataParameter data_param = 11; 837 | optional DropoutParameter dropout_param = 12; 838 | optional DummyDataParameter dummy_data_param = 26; 839 | optional EltwiseParameter eltwise_param = 24; 840 | optional ExpParameter exp_param = 41; 841 | optional HDF5DataParameter hdf5_data_param = 13; 842 | optional HDF5OutputParameter hdf5_output_param = 14; 843 | optional HingeLossParameter hinge_loss_param = 29; 844 | optional ImageDataParameter image_data_param = 15; 845 | optional InfogainLossParameter infogain_loss_param = 16; 846 | optional InnerProductParameter inner_product_param = 17; 847 | optional LRNParameter lrn_param = 18; 848 | optional MemoryDataParameter memory_data_param = 22; 849 | optional MVNParameter mvn_param = 34; 850 | optional PoolingParameter pooling_param = 19; 851 | optional PowerParameter power_param = 21; 852 | optional ReLUParameter relu_param = 30; 853 | optional SigmoidParameter sigmoid_param = 38; 854 | optional SoftmaxParameter softmax_param = 39; 855 | optional SliceParameter slice_param = 31; 856 | optional TanHParameter tanh_param = 37; 857 | optional ThresholdParameter threshold_param = 25; 858 | optional WindowDataParameter window_data_param = 20; 859 | optional TransformationParameter transform_param = 36; 860 | optional LossParameter loss_param = 42; 861 | optional V0LayerParameter layer = 1; 862 | } 863 | 864 | // DEPRECATED: V0LayerParameter is the old way of specifying layer parameters 865 | // in Caffe. We keep this message type around for legacy support. 866 | message V0LayerParameter { 867 | optional string name = 1; // the layer name 868 | optional string type = 2; // the string to specify the layer type 869 | 870 | // Parameters to specify layers with inner products. 871 | optional uint32 num_output = 3; // The number of outputs for the layer 872 | optional bool biasterm = 4 [default = true]; // whether to have bias terms 873 | optional FillerParameter weight_filler = 5; // The filler for the weight 874 | optional FillerParameter bias_filler = 6; // The filler for the bias 875 | 876 | optional uint32 pad = 7 [default = 0]; // The padding size 877 | optional uint32 kernelsize = 8; // The kernel size 878 | optional uint32 group = 9 [default = 1]; // The group size for group conv 879 | optional uint32 stride = 10 [default = 1]; // The stride 880 | enum PoolMethod { 881 | MAX = 0; 882 | AVE = 1; 883 | STOCHASTIC = 2; 884 | } 885 | optional PoolMethod pool = 11 [default = MAX]; // The pooling method 886 | optional float dropout_ratio = 12 [default = 0.5]; // dropout ratio 887 | 888 | optional uint32 local_size = 13 [default = 5]; // for local response norm 889 | optional float alpha = 14 [default = 1.]; // for local response norm 890 | optional float beta = 15 [default = 0.75]; // for local response norm 891 | optional float k = 22 [default = 1.]; 892 | 893 | // For data layers, specify the data source 894 | optional string source = 16; 895 | // For data pre-processing, we can do simple scaling and subtracting the 896 | // data mean, if provided. Note that the mean subtraction is always carried 897 | // out before scaling. 898 | optional float scale = 17 [default = 1]; 899 | optional string meanfile = 18; 900 | // For data layers, specify the batch size. 901 | optional uint32 batchsize = 19; 902 | // For data layers, specify if we would like to randomly crop an image. 903 | optional uint32 cropsize = 20 [default = 0]; 904 | // For data layers, specify if we want to randomly mirror data. 905 | optional bool mirror = 21 [default = false]; 906 | 907 | // The blobs containing the numeric parameters of the layer 908 | repeated BlobProto blobs = 50; 909 | // The ratio that is multiplied on the global learning rate. If you want to 910 | // set the learning ratio for one blob, you need to set it for all blobs. 911 | repeated float blobs_lr = 51; 912 | // The weight decay that is multiplied on the global weight decay. 913 | repeated float weight_decay = 52; 914 | 915 | // The rand_skip variable is for the data layer to skip a few data points 916 | // to avoid all asynchronous sgd clients to start at the same point. The skip 917 | // point would be set as rand_skip * rand(0,1). Note that rand_skip should not 918 | // be larger than the number of keys in the database. 919 | optional uint32 rand_skip = 53 [default = 0]; 920 | 921 | // Fields related to detection (det_*) 922 | // foreground (object) overlap threshold 923 | optional float det_fg_threshold = 54 [default = 0.5]; 924 | // background (non-object) overlap threshold 925 | optional float det_bg_threshold = 55 [default = 0.5]; 926 | // Fraction of batch that should be foreground objects 927 | optional float det_fg_fraction = 56 [default = 0.25]; 928 | 929 | // optional bool OBSOLETE_can_clobber = 57 [default = true]; 930 | 931 | // Amount of contextual padding to add around a window 932 | // (used only by the window_data_layer) 933 | optional uint32 det_context_pad = 58 [default = 0]; 934 | 935 | // Mode for cropping out a detection window 936 | // warp: cropped window is warped to a fixed size and aspect ratio 937 | // square: the tightest square around the window is cropped 938 | optional string det_crop_mode = 59 [default = "warp"]; 939 | 940 | // For ReshapeLayer, one needs to specify the new dimensions. 941 | optional int32 new_num = 60 [default = 0]; 942 | optional int32 new_channels = 61 [default = 0]; 943 | optional int32 new_height = 62 [default = 0]; 944 | optional int32 new_width = 63 [default = 0]; 945 | 946 | // Whether or not ImageLayer should shuffle the list of files at every epoch. 947 | // It will also resize images if new_height or new_width are not zero. 948 | optional bool shuffle_images = 64 [default = false]; 949 | 950 | // For ConcatLayer, one needs to specify the dimension for concatenation, and 951 | // the other dimensions must be the same for all the bottom blobs. 952 | // By default it will concatenate blobs along the channels dimension. 953 | optional uint32 concat_dim = 65 [default = 1]; 954 | 955 | optional HDF5OutputParameter hdf5_output_param = 1001; 956 | } 957 | 958 | // Message that stores parameters used by PReLULayer 959 | message PReLUParameter { 960 | // Parametric ReLU described in K. He et al, Delving Deep into Rectifiers: 961 | // Surpassing Human-Level Performance on ImageNet Classification, 2015. 962 | 963 | // Initial value of a_i. Default is a_i=0.25 for all i. 964 | optional FillerParameter filler = 1; 965 | // Whether or not slope paramters are shared across channels. 966 | optional bool channel_shared = 2 [default = false]; 967 | } 968 | -------------------------------------------------------------------------------- /example/lenet.caffemodel: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lon9/caffe2go/64cb8c3e6162259faeddf2bd55f724340fb78fad/example/lenet.caffemodel -------------------------------------------------------------------------------- /example/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | _ "image/jpeg" 6 | _ "image/png" 7 | 8 | "github.com/lon9/caffe2go/c2g" 9 | ) 10 | 11 | func main() { 12 | caffe2go, err := c2g.NewCaffe2Go("lenet.caffemodel") 13 | if err != nil { 14 | panic(err) 15 | } 16 | output, err := caffe2go.Predict("mnist_zero.png", 28, nil) 17 | if err != nil { 18 | panic(err) 19 | } 20 | 21 | for i := range output { 22 | fmt.Printf("%d: %f\n", i, output[i][0][0]) 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /example/mnist_zero.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lon9/caffe2go/64cb8c3e6162259faeddf2bd55f724340fb78fad/example/mnist_zero.png -------------------------------------------------------------------------------- /images/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lon9/caffe2go/64cb8c3e6162259faeddf2bd55f724340fb78fad/images/.gitkeep -------------------------------------------------------------------------------- /labels/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lon9/caffe2go/64cb8c3e6162259faeddf2bd55f724340fb78fad/labels/.gitkeep -------------------------------------------------------------------------------- /layers/base.go: -------------------------------------------------------------------------------- 1 | package layers 2 | 3 | import ( 4 | "errors" 5 | ) 6 | 7 | // BaseLayer is base struct of Layers. 8 | type BaseLayer struct { 9 | Name string 10 | Type string 11 | } 12 | 13 | // NewBaseLayer is constructor. 14 | func NewBaseLayer(name, t string) *BaseLayer { 15 | return &BaseLayer{ 16 | Name: name, 17 | Type: t, 18 | } 19 | } 20 | 21 | // GetName is method to return name of the layer. 22 | func (b *BaseLayer) GetName() string { 23 | return b.Name 24 | } 25 | 26 | // GetType is method to return type of the layer. 27 | func (b *BaseLayer) GetType() string { 28 | return b.Type 29 | } 30 | 31 | // Forward is base function of Forward. 32 | func (b *BaseLayer) Forward(input [][][]float32) ([][][]float32, error) { 33 | return input, errors.New("Forward is not implemented") 34 | } 35 | -------------------------------------------------------------------------------- /layers/convolution.go: -------------------------------------------------------------------------------- 1 | package layers 2 | 3 | import ( 4 | "github.com/lon9/exmat" 5 | mymat "github.com/lon9/mat" 6 | "gonum.org/v1/gonum/mat" 7 | ) 8 | 9 | // ConvolutionLayer is layer of Convolution. 10 | type ConvolutionLayer struct { 11 | *BaseLayer 12 | NInput uint32 13 | NOutput uint32 14 | KernelSize int 15 | Stride int 16 | Padding int 17 | Weights [][][][]float32 18 | Bias []float32 19 | BiasTerm bool 20 | } 21 | 22 | // NewConvolutionLayer is constructor. 23 | func NewConvolutionLayer(name, t string, nInput, nOutput uint32, kernelSize, stride, padding int, biasTerm bool) *ConvolutionLayer { 24 | w := make([][][][]float32, nOutput) 25 | for i := 0; i < int(nOutput); i++ { 26 | w[i] = make([][][]float32, nInput) 27 | for j := 0; j < int(nInput); j++ { 28 | w[i][j] = make([][]float32, kernelSize) 29 | for k := 0; k < int(kernelSize); k++ { 30 | w[i][j][k] = make([]float32, kernelSize) 31 | } 32 | } 33 | } 34 | return &ConvolutionLayer{ 35 | BaseLayer: NewBaseLayer(name, t), 36 | NInput: nInput, 37 | NOutput: nOutput, 38 | KernelSize: kernelSize, 39 | Stride: stride, 40 | Padding: padding, 41 | Weights: w, 42 | BiasTerm: biasTerm, 43 | } 44 | } 45 | 46 | // Forward forwards a step. 47 | func (conv *ConvolutionLayer) Forward(input [][][]float32) ([][][]float32, error) { 48 | if conv.Padding > 0 { 49 | doneCh := make(chan bool, len(input)) 50 | for i := range input { 51 | go func(i int, doneCh chan bool) { 52 | in := ConvertMatrix(input[i]) 53 | inExMat := exmat.NewExMatFromDense(in) 54 | input[i] = ConvertMat64(inExMat.ZeroPadding(conv.Padding)) 55 | doneCh <- true 56 | }(i, doneCh) 57 | } 58 | for i := 0; i < len(input); i++ { 59 | <-doneCh 60 | } 61 | close(doneCh) 62 | } 63 | in := ConvertMatrix(Im2Col(input, conv.KernelSize, conv.Stride)) 64 | kernels := make([][]float32, conv.NOutput) 65 | doneCh := make(chan bool, conv.NOutput) 66 | for i := 0; i < int(conv.NOutput); i++ { 67 | go func(i int, doneCh chan bool) { 68 | kernels[i] = Im2Col(conv.Weights[i], conv.KernelSize, conv.Stride)[0] 69 | doneCh <- true 70 | }(i, doneCh) 71 | } 72 | for i := 0; i < int(conv.NOutput); i++ { 73 | <-doneCh 74 | } 75 | close(doneCh) 76 | kernelMatrix := ConvertMatrix(kernels) 77 | var out mat.Dense 78 | out.Mul(in, kernelMatrix.T()) 79 | output := make([][][]float32, conv.NOutput) 80 | rows := (len(input[0])-conv.KernelSize)/conv.Stride + 1 81 | cols := (len(input[0][0])-conv.KernelSize)/conv.Stride + 1 82 | outTransposed := out.T() 83 | r, c := outTransposed.Dims() 84 | errCh := make(chan error, r) 85 | for i := 0; i < r; i++ { 86 | go func(i int, errCh chan error) { 87 | part := make([][]float32, 1) 88 | part[0] = make([]float32, c) 89 | for j := 0; j < c; j++ { 90 | part[0][j] = float32(outTransposed.At(i, j)) 91 | } 92 | res, err := mymat.NewMatrix(part).Reshape(uint(rows), uint(cols)) 93 | if err != nil { 94 | errCh <- err 95 | return 96 | } 97 | output[i] = res.M 98 | errCh <- nil 99 | }(i, errCh) 100 | } 101 | for i := 0; i < r; i++ { 102 | if err := <-errCh; err != nil { 103 | return nil, err 104 | } 105 | } 106 | close(errCh) 107 | 108 | if conv.BiasTerm { 109 | doneCh := make(chan bool, len(output)) 110 | for i := range output { 111 | go func(idx int) { 112 | m := ConvertMatrix(output[idx]) 113 | var res mat.Dense 114 | res.Apply(func(i, j int, v float64) float64 { 115 | return v + float64(conv.Bias[idx]) 116 | }, m) 117 | output[idx] = ConvertMat64(&res) 118 | doneCh <- true 119 | }(i) 120 | } 121 | for range output { 122 | <-doneCh 123 | } 124 | close(doneCh) 125 | } 126 | 127 | return output, nil 128 | } 129 | -------------------------------------------------------------------------------- /layers/convolution_test.go: -------------------------------------------------------------------------------- 1 | package layers 2 | 3 | import "testing" 4 | 5 | func TestConvolution(t *testing.T) { 6 | var input = [][][]float32{ 7 | { 8 | {0, 0, 0, 0, 0, 0, 0}, 9 | {0, 1, 0, 0, 1, 0, 0}, 10 | {0, 0, 1, 0, 2, 2, 0}, 11 | {0, 1, 2, 2, 1, 0, 0}, 12 | {0, 0, 0, 0, 1, 2, 0}, 13 | {0, 0, 0, 0, 2, 0, 0}, 14 | {0, 0, 0, 0, 0, 0, 0}, 15 | }, 16 | { 17 | {0, 0, 0, 0, 0, 0, 0}, 18 | {0, 1, 2, 0, 2, 1, 0}, 19 | {0, 0, 1, 1, 2, 2, 0}, 20 | {0, 0, 2, 0, 1, 1, 0}, 21 | {0, 2, 2, 2, 1, 2, 0}, 22 | {0, 2, 0, 0, 1, 2, 0}, 23 | {0, 0, 0, 0, 0, 0, 0}, 24 | }, 25 | { 26 | {0, 0, 0, 0, 0, 0, 0}, 27 | {0, 2, 0, 0, 2, 1, 0}, 28 | {0, 0, 1, 1, 0, 2, 0}, 29 | {0, 0, 1, 2, 0, 0, 0}, 30 | {0, 0, 1, 2, 1, 1, 0}, 31 | {0, 1, 1, 0, 1, 1, 0}, 32 | {0, 0, 0, 0, 0, 0, 0}, 33 | }, 34 | } 35 | 36 | var filters = [][][][]float32{ 37 | { 38 | { 39 | {0, 1, 1}, 40 | {1, -1, -1}, 41 | {-1, 0, -1}, 42 | }, 43 | { 44 | {0, 1, 0}, 45 | {-1, 0, -1}, 46 | {-1, 1, -1}, 47 | }, 48 | { 49 | {1, 1, 0}, 50 | {0, 1, -1}, 51 | {1, 1, 0}, 52 | }, 53 | }, 54 | { 55 | { 56 | {1, 1, 1}, 57 | {-1, -1, 0}, 58 | {0, 0, -1}, 59 | }, 60 | { 61 | {1, 0, 1}, 62 | {1, 0, 1}, 63 | {-1, 1, 0}, 64 | }, 65 | { 66 | {0, 0, 0}, 67 | {1, 0, 0}, 68 | {1, -1, 0}, 69 | }, 70 | }, 71 | } 72 | 73 | var ans = [][][]float32{ 74 | { 75 | {-3, -10, 0}, 76 | {-5, 4, 8}, 77 | {2, 2, 8}, 78 | }, 79 | { 80 | {0, 2, 1}, 81 | {5, 4, 7}, 82 | {2, 6, 4}, 83 | }, 84 | } 85 | 86 | conv := ConvolutionLayer{ 87 | NInput: 3, 88 | NOutput: 2, 89 | KernelSize: 3, 90 | Stride: 2, 91 | Padding: 0, 92 | Weights: filters, 93 | } 94 | 95 | output, err := conv.Forward(input) 96 | if err != nil { 97 | t.Fatal(err) 98 | } 99 | 100 | for i := range output { 101 | for j := range output[i] { 102 | for k := range output[i][j] { 103 | if output[i][j][k] != ans[i][j][k] { 104 | t.Log(output) 105 | t.Fatal("not same") 106 | } 107 | } 108 | } 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /layers/dropout.go: -------------------------------------------------------------------------------- 1 | package layers 2 | 3 | import "gonum.org/v1/gonum/mat" 4 | 5 | // DropoutLayer is layer of Dropout. 6 | type DropoutLayer struct { 7 | *BaseLayer 8 | Ratio float32 9 | } 10 | 11 | // NewDropoutLayer is constructor. 12 | func NewDropoutLayer(name, t string, ratio float32) *DropoutLayer { 13 | return &DropoutLayer{ 14 | BaseLayer: NewBaseLayer(name, t), 15 | Ratio: ratio, 16 | } 17 | } 18 | 19 | // Forward fowards a step. 20 | func (d *DropoutLayer) Forward(input [][][]float32) ([][][]float32, error) { 21 | r := float64(d.Ratio) 22 | doneCh := make(chan bool, len(input)) 23 | for i := range input { 24 | go func(idx int) { 25 | t := ConvertMatrix(input[idx]) 26 | var out mat.Dense 27 | out.Apply(func(i, j int, v float64) float64 { 28 | return v * r 29 | }, t) 30 | input[idx] = ConvertMat64(&out) 31 | doneCh <- true 32 | }(i) 33 | } 34 | for range input { 35 | <-doneCh 36 | } 37 | close(doneCh) 38 | return input, nil 39 | } 40 | -------------------------------------------------------------------------------- /layers/dropout_test.go: -------------------------------------------------------------------------------- 1 | package layers 2 | 3 | import "testing" 4 | 5 | func TestDropout(t *testing.T) { 6 | input := [][][]float32{ 7 | { 8 | {0, 1, -1}, 9 | {-100, 23, 43}, 10 | {1.234, -0.22, 0.5}, 11 | }, 12 | } 13 | 14 | dropout := DropoutLayer{ 15 | Ratio: 0.5, 16 | } 17 | 18 | output, err := dropout.Forward(input) 19 | if err != nil { 20 | t.Fatal(err) 21 | } 22 | 23 | t.Log(output) 24 | } 25 | -------------------------------------------------------------------------------- /layers/fullconnect.go: -------------------------------------------------------------------------------- 1 | package layers 2 | 3 | import ( 4 | "gonum.org/v1/gonum/mat" 5 | ) 6 | 7 | // FullconnectLayer is a layer. 8 | type FullconnectLayer struct { 9 | *BaseLayer 10 | Width int 11 | Height int 12 | Weights [][]float32 13 | BiasTerm bool 14 | Bias []float32 15 | } 16 | 17 | // NewFullconnectLayer is constructor. 18 | func NewFullconnectLayer(name, t string, width, height int, biasTerm bool) *FullconnectLayer { 19 | w := make([][]float32, height) 20 | 21 | return &FullconnectLayer{ 22 | BaseLayer: NewBaseLayer(name, t), 23 | Width: width, 24 | Height: height, 25 | BiasTerm: biasTerm, 26 | Weights: w, 27 | } 28 | } 29 | 30 | // Forward forawards a step. 31 | func (f *FullconnectLayer) Forward(input [][][]float32) ([][][]float32, error) { 32 | in := make([]float64, f.Width) 33 | 34 | idx := 0 35 | for i := range input { 36 | for j := range input[i] { 37 | for k := range input[i][j] { 38 | in[idx] = float64(input[i][j][k]) 39 | idx++ 40 | } 41 | } 42 | } 43 | 44 | inMat := mat.NewDense(f.Width, 1, in) 45 | weights := ConvertMatrix(f.Weights) 46 | var out mat.Dense 47 | out.Mul(weights, inMat) 48 | 49 | output := make([][][]float32, f.Height) 50 | for i := range output { 51 | output[i] = make([][]float32, 1) 52 | for j := range output[i] { 53 | output[i][j] = make([]float32, 1) 54 | for k := range output[i][j] { 55 | output[i][j][k] = float32(out.At(i, 0)) + f.Bias[i] 56 | } 57 | } 58 | } 59 | 60 | return output, nil 61 | } 62 | -------------------------------------------------------------------------------- /layers/fullconnect_test.go: -------------------------------------------------------------------------------- 1 | package layers 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func TestFullconnect(t *testing.T) { 8 | input := [][][]float32{ 9 | { 10 | {1}, 11 | }, 12 | { 13 | {2}, 14 | }, 15 | { 16 | {3}, 17 | }, 18 | } 19 | 20 | weights := [][]float32{ 21 | {1, 2, 3}, 22 | {4, 5, 6}, 23 | {7, 8, 9}, 24 | {10, 11, 12}, 25 | {13, 14, 15}, 26 | } 27 | 28 | bias := []float32{ 29 | 1, 2, 1, 0, 0, 30 | } 31 | 32 | fc := FullconnectLayer{ 33 | Width: 3, 34 | Height: 5, 35 | BiasTerm: true, 36 | Weights: weights, 37 | Bias: bias, 38 | } 39 | 40 | ans := [][][]float32{ 41 | { 42 | {15}, 43 | }, 44 | { 45 | {34}, 46 | }, 47 | { 48 | {51}, 49 | }, 50 | { 51 | {68}, 52 | }, 53 | { 54 | {86}, 55 | }, 56 | } 57 | 58 | output, err := fc.Forward(input) 59 | if err != nil { 60 | t.Fatal(err) 61 | } 62 | 63 | for i := range output { 64 | for j := range output[i] { 65 | for k := range output[i][j] { 66 | if ans[i][j][k] != output[i][j][k] { 67 | t.Error("not same") 68 | t.Log(output) 69 | } 70 | } 71 | } 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /layers/layer.go: -------------------------------------------------------------------------------- 1 | package layers 2 | 3 | const ( 4 | Data = "Data" 5 | Convolution = "Convolution" 6 | InnerProduct = "InnerProduct" 7 | Pooling = "Pooling" 8 | ReLU = "ReLU" 9 | Dropout = "Dropout" 10 | SoftmaxWithLoss = "SoftmaxWithLoss" 11 | Softmax = "Softmax" 12 | Lrn = "Lrn" 13 | ) 14 | 15 | // Layer is object of network layer. 16 | type Layer interface { 17 | GetName() string 18 | GetType() string 19 | Forward([][][]float32) ([][][]float32, error) 20 | } 21 | -------------------------------------------------------------------------------- /layers/lrn.go: -------------------------------------------------------------------------------- 1 | package layers 2 | 3 | import ( 4 | "math" 5 | 6 | "gonum.org/v1/gonum/mat" 7 | ) 8 | 9 | // LRN is Local Response Normalization. 10 | type LRN struct { 11 | *BaseLayer 12 | N int 13 | K float64 14 | Alpha float64 15 | Beta float64 16 | } 17 | 18 | // NewLRNLayer is constructor. 19 | func NewLRNLayer(name, t string, n int, k, alpha, beta float64) *LRN { 20 | return &LRN{ 21 | BaseLayer: NewBaseLayer(name, t), 22 | N: n, 23 | K: k, 24 | Alpha: alpha, 25 | Beta: beta, 26 | } 27 | } 28 | 29 | // Forward forwards one step of the network. 30 | func (lrn *LRN) Forward(input [][][]float32) ([][][]float32, error) { 31 | output := make([][][]float32, len(input)) 32 | doneCh := make(chan bool, len(input)) 33 | for k := range input { 34 | go func(idx int) { 35 | s := int(math.Max(0.0, float64(idx-lrn.N/2))) 36 | e := int(math.Min(float64(len(input)-1), float64(idx+lrn.N/2))) 37 | o := ConvertMatrix(input[idx]) 38 | var res mat.Dense 39 | res.Apply(func(i, j int, v float64) float64 { 40 | sum := 0.0 41 | for l := s; l < e; l++ { 42 | sum += float64(input[l][i][j] * input[l][i][j]) 43 | } 44 | return v / (float64(lrn.K) + lrn.Alpha*sum) 45 | }, o) 46 | output[idx] = ConvertMat64(&res) 47 | doneCh <- true 48 | }(k) 49 | } 50 | for range input { 51 | <-doneCh 52 | } 53 | close(doneCh) 54 | return output, nil 55 | } 56 | -------------------------------------------------------------------------------- /layers/lrn_test.go: -------------------------------------------------------------------------------- 1 | package layers 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/lon9/mat" 7 | ) 8 | 9 | func TestLRN(t *testing.T) { 10 | lrn := LRN{ 11 | N: 5, 12 | K: 2, 13 | Alpha: 0.0005, 14 | Beta: 0.75, 15 | } 16 | 17 | input := make([][][]float32, 96) 18 | for i := range input { 19 | m := mat.Random(64, 64) 20 | input[i] = m.M 21 | } 22 | 23 | res, err := lrn.Forward(input) 24 | if err != nil { 25 | t.Error(err) 26 | } 27 | t.Log(res) 28 | } 29 | -------------------------------------------------------------------------------- /layers/pooling.go: -------------------------------------------------------------------------------- 1 | package layers 2 | 3 | import "github.com/lon9/exmat" 4 | 5 | // PoolingLayer is layer of Pooling. 6 | type PoolingLayer struct { 7 | *BaseLayer 8 | KernelSize int 9 | Stride int 10 | Padding int 11 | } 12 | 13 | // NewPoolingLayer is constructor. 14 | func NewPoolingLayer(name, t string, kernelSize, stride, padding int) *PoolingLayer { 15 | return &PoolingLayer{ 16 | BaseLayer: NewBaseLayer(name, t), 17 | KernelSize: kernelSize, 18 | Stride: stride, 19 | Padding: padding, 20 | } 21 | } 22 | 23 | // Forward fowards a step. 24 | func (pool *PoolingLayer) Forward(input [][][]float32) ([][][]float32, error) { 25 | output := make([][][]float32, len(input)) 26 | doneCh := make(chan bool, len(input)) 27 | for i := range input { 28 | go func(i int, doneCh chan bool) { 29 | rows := len(input[i]) 30 | cols := len(input[i][0]) 31 | t := make([]float64, rows*cols) 32 | for j := range input[i] { 33 | for k := range input[i][j] { 34 | t[cols*j+k] = float64(input[i][j][k]) 35 | } 36 | } 37 | in := exmat.NewExMat(rows, cols, t) 38 | var out exmat.ExMat 39 | out.Pooling(pool.KernelSize, pool.Stride, exmat.Max, in) 40 | output[i] = ConvertMat64(&out) 41 | doneCh <- true 42 | }(i, doneCh) 43 | } 44 | for range input { 45 | <-doneCh 46 | } 47 | close(doneCh) 48 | return output, nil 49 | } 50 | -------------------------------------------------------------------------------- /layers/pooling_test.go: -------------------------------------------------------------------------------- 1 | package layers 2 | 3 | import "testing" 4 | 5 | func TestPooling(t *testing.T) { 6 | input := [][][]float32{ 7 | { 8 | {1, 1, 2, 4}, 9 | {5, 6, 7, 8}, 10 | {3, 2, 1, 0}, 11 | {1, 2, 3, 4}, 12 | }, 13 | } 14 | 15 | ans := [][][]float32{ 16 | { 17 | {6, 8}, 18 | {3, 4}, 19 | }, 20 | } 21 | 22 | pool := PoolingLayer{ 23 | KernelSize: 2, 24 | Stride: 2, 25 | Padding: 0, 26 | } 27 | 28 | output, err := pool.Forward(input) 29 | if err != nil { 30 | t.Fatal(err) 31 | } 32 | 33 | for i := range output { 34 | for j := range output[i] { 35 | for k := range output[i][j] { 36 | if output[i][j][k] != ans[i][j][k] { 37 | t.Log(output) 38 | t.Fatal("not same") 39 | } 40 | } 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /layers/relu.go: -------------------------------------------------------------------------------- 1 | package layers 2 | 3 | import ( 4 | "math" 5 | 6 | "gonum.org/v1/gonum/mat" 7 | ) 8 | 9 | // ReLULayer is layer of ReLU. 10 | type ReLULayer struct { 11 | *BaseLayer 12 | Slope float32 13 | } 14 | 15 | // NewReLULayer is constructor. 16 | func NewReLULayer(name, t string, slope float32) *ReLULayer { 17 | return &ReLULayer{ 18 | BaseLayer: NewBaseLayer(name, t), 19 | Slope: slope, 20 | } 21 | } 22 | 23 | // Forward forwards a step. 24 | func (r *ReLULayer) Forward(input [][][]float32) ([][][]float32, error) { 25 | doneCh := make(chan bool, len(input)) 26 | for i := range input { 27 | go func(idx int) { 28 | t := ConvertMatrix(input[idx]) 29 | var out mat.Dense 30 | out.Apply(func(i, j int, v float64) float64 { 31 | return math.Max(0, v) 32 | }, t) 33 | input[idx] = ConvertMat64(&out) 34 | doneCh <- true 35 | }(i) 36 | } 37 | for range input { 38 | <-doneCh 39 | } 40 | close(doneCh) 41 | return input, nil 42 | } 43 | -------------------------------------------------------------------------------- /layers/relu_test.go: -------------------------------------------------------------------------------- 1 | package layers 2 | 3 | import "testing" 4 | 5 | func TestReLU(t *testing.T) { 6 | input := [][][]float32{ 7 | { 8 | {0, 1, -1}, 9 | {-100, 23, 43}, 10 | {1.234, -0.22, 0.5}, 11 | }, 12 | } 13 | relu := ReLULayer{} 14 | output, err := relu.Forward(input) 15 | if err != nil { 16 | t.Fatal(err) 17 | } 18 | 19 | ans := [][][]float32{ 20 | { 21 | {0, 1, 0}, 22 | {0, 23, 43}, 23 | {1.234, 0, 0.5}, 24 | }, 25 | } 26 | 27 | for i := range output { 28 | for j := range output[i] { 29 | for k := range output[i][j] { 30 | if output[i][j][k] != ans[i][j][k] { 31 | t.Log(output) 32 | t.Fatal("not same") 33 | } 34 | } 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /layers/softmax.go: -------------------------------------------------------------------------------- 1 | package layers 2 | 3 | import ( 4 | "math" 5 | ) 6 | 7 | // SoftmaxLossLayer is layer of Softmax loss. 8 | type SoftmaxLossLayer struct { 9 | *BaseLayer 10 | } 11 | 12 | // NewSoftmaxLossLayer is constructor. 13 | func NewSoftmaxLossLayer(name, t string) *SoftmaxLossLayer { 14 | return &SoftmaxLossLayer{ 15 | BaseLayer: NewBaseLayer(name, t), 16 | } 17 | } 18 | 19 | // Forward forwards a step. 20 | func (s *SoftmaxLossLayer) Forward(input [][][]float32) ([][][]float32, error) { 21 | total := float32(0.0) 22 | max := float32(0.0) 23 | 24 | // Calculate maximum. 25 | for i := range input { 26 | if max < input[i][0][0] { 27 | max = input[i][0][0] 28 | } 29 | } 30 | 31 | // Calculate total. 32 | for i := range input { 33 | input[i][0][0] = float32(math.Exp(float64(input[i][0][0] - max))) 34 | total += input[i][0][0] 35 | } 36 | 37 | for i := range input { 38 | input[i][0][0] = input[i][0][0] / total 39 | } 40 | 41 | return input, nil 42 | } 43 | -------------------------------------------------------------------------------- /layers/softmax_test.go: -------------------------------------------------------------------------------- 1 | package layers 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/lon9/mat" 7 | ) 8 | 9 | func TestSoftmax(t *testing.T) { 10 | input := make([][][]float32, 1000) 11 | for i := 0; i < 1000; i++ { 12 | input[i] = mat.Random(1, 1).M 13 | } 14 | t.Log(input) 15 | 16 | softMax := SoftmaxLossLayer{} 17 | output, err := softMax.Forward(input) 18 | if err != nil { 19 | t.Fatal(err) 20 | } 21 | t.Log(output) 22 | } 23 | -------------------------------------------------------------------------------- /layers/utils.go: -------------------------------------------------------------------------------- 1 | package layers 2 | 3 | import ( 4 | "gonum.org/v1/gonum/mat" 5 | ) 6 | 7 | // Im2Col converts image 3D tensor to matrix. 8 | func Im2Col(img [][][]float32, kernelSize, stride int) [][]float32 { 9 | colSize := kernelSize * kernelSize * len(img) 10 | rows := (len(img[0])-kernelSize)/stride + 1 11 | cols := (len(img[0][0])-kernelSize)/stride + 1 12 | res := make([][]float32, rows*cols) 13 | idx1 := 0 14 | doneCh := make(chan bool, rows*cols) 15 | for y := 0; y < rows; y++ { 16 | for x := 0; x < cols; x++ { 17 | go func(x, y, idx1 int) { 18 | col := make([]float32, colSize) 19 | idx2 := 0 20 | sy := y * stride 21 | sx := x * stride 22 | for c := range img { 23 | for i := sy; i < sy+kernelSize; i++ { 24 | for j := sx; j < sx+kernelSize; j++ { 25 | col[idx2] = img[c][i][j] 26 | idx2++ 27 | } 28 | } 29 | } 30 | res[idx1] = col 31 | doneCh <- true 32 | }(x, y, idx1) 33 | idx1++ 34 | } 35 | } 36 | for i := 0; i < rows*cols; i++ { 37 | <-doneCh 38 | } 39 | close(doneCh) 40 | return res 41 | } 42 | 43 | // ConvertMatrix converts slice of vector to mat64.Matrix 44 | func ConvertMatrix(m [][]float32) *mat.Dense { 45 | cols := len(m[0]) 46 | flatten := make([]float64, len(m)*cols) 47 | for i := range m { 48 | for j := range m[i] { 49 | flatten[cols*i+j] = float64(m[i][j]) 50 | } 51 | } 52 | return mat.NewDense(len(m), cols, flatten) 53 | } 54 | 55 | // ConvertMat64 converts mat64 matrix to [][]float32 56 | func ConvertMat64(m mat.Matrix) [][]float32 { 57 | r, c := m.Dims() 58 | res := make([][]float32, r) 59 | for i := 0; i < r; i++ { 60 | res[i] = make([]float32, c) 61 | for j := 0; j < c; j++ { 62 | res[i][j] = float32(m.At(i, j)) 63 | } 64 | } 65 | return res 66 | } 67 | -------------------------------------------------------------------------------- /layers/utils_test.go: -------------------------------------------------------------------------------- 1 | package layers 2 | 3 | import ( 4 | "testing" 5 | 6 | mymat "github.com/lon9/mat" 7 | "gonum.org/v1/gonum/mat" 8 | ) 9 | 10 | func TestIm2Col(t *testing.T) { 11 | m := [][][]float32{ 12 | { 13 | {0, 0, 0, 0, 0, 0, 0}, 14 | {0, 0, 2, 0, 0, 1, 0}, 15 | {0, 1, 2, 0, 0, 1, 0}, 16 | {0, 2, 2, 1, 2, 2, 0}, 17 | {0, 0, 0, 1, 2, 1, 0}, 18 | {0, 2, 1, 1, 1, 0, 0}, 19 | {0, 0, 0, 0, 0, 0, 0}, 20 | }, 21 | { 22 | {0, 0, 0, 0, 0, 0, 0}, 23 | {0, 0, 2, 0, 0, 1, 0}, 24 | {0, 1, 2, 0, 0, 1, 0}, 25 | {0, 2, 2, 1, 2, 2, 0}, 26 | {0, 0, 0, 1, 2, 1, 0}, 27 | {0, 2, 1, 1, 1, 0, 0}, 28 | {0, 0, 0, 0, 0, 0, 0}, 29 | }, 30 | { 31 | {0, 0, 0, 0, 0, 0, 0}, 32 | {0, 0, 2, 0, 0, 1, 0}, 33 | {0, 1, 2, 0, 0, 1, 0}, 34 | {0, 2, 2, 1, 2, 2, 0}, 35 | {0, 0, 0, 1, 2, 1, 0}, 36 | {0, 2, 1, 1, 1, 0, 0}, 37 | {0, 0, 0, 0, 0, 0, 0}, 38 | }, 39 | } 40 | 41 | res := mymat.NewMatrix(Im2Col(m, 3, 2)) 42 | 43 | a := mymat.NewMatrix([][]float32{ 44 | {0, 0, 0, 0, 0, 2, 0, 1, 2, 0, 0, 0, 0, 0, 2, 0, 1, 2, 0, 0, 0, 0, 0, 2, 0, 1, 2}, 45 | {0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0}, 46 | {0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0}, 47 | {0, 1, 2, 0, 2, 2, 0, 0, 0, 0, 1, 2, 0, 2, 2, 0, 0, 0, 0, 1, 2, 0, 2, 2, 0, 0, 0}, 48 | {2, 0, 0, 2, 1, 2, 0, 1, 2, 2, 0, 0, 2, 1, 2, 0, 1, 2, 2, 0, 0, 2, 1, 2, 0, 1, 2}, 49 | {0, 1, 0, 2, 2, 0, 2, 1, 0, 0, 1, 0, 2, 2, 0, 2, 1, 0, 0, 1, 0, 2, 2, 0, 2, 1, 0}, 50 | {0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 0}, 51 | {0, 1, 2, 1, 1, 1, 0, 0, 0, 0, 1, 2, 1, 1, 1, 0, 0, 0, 0, 1, 2, 1, 1, 1, 0, 0, 0}, 52 | {2, 1, 0, 1, 0, 0, 0, 0, 0, 2, 1, 0, 1, 0, 0, 0, 0, 0, 2, 1, 0, 1, 0, 0, 0, 0, 0}, 53 | }) 54 | 55 | if !res.Equals(a) { 56 | t.Error("not same") 57 | a.Show() 58 | res.Show() 59 | } 60 | 61 | } 62 | 63 | func TestConvertMatrix(t *testing.T) { 64 | src := [][]float32{ 65 | {1, 2, 3, 4}, 66 | {5, 6, 7, 8}, 67 | {9, 10, 11, 12}, 68 | } 69 | 70 | dst := ConvertMatrix(src) 71 | r, c := dst.Dims() 72 | if r != len(src) || c != len(src[0]) { 73 | t.Error("not same") 74 | } 75 | 76 | for i := range src { 77 | for j := range src[i] { 78 | if src[i][j] != float32(dst.At(i, j)) { 79 | t.Error("not same") 80 | } 81 | } 82 | } 83 | } 84 | 85 | func TestConvertMat64(t *testing.T) { 86 | src := mat.NewDense(3, 4, []float64{ 87 | 1, 2, 3, 4, 88 | 5, 6, 7, 8, 89 | 9, 10, 11, 12, 90 | }) 91 | ans := [][]float32{ 92 | {1, 2, 3, 4}, 93 | {5, 6, 7, 8}, 94 | {9, 10, 11, 12}, 95 | } 96 | 97 | res := ConvertMat64(src) 98 | for i := range ans { 99 | for j := range ans[i] { 100 | if res[i][j] != ans[i][j] { 101 | t.Error("not same") 102 | } 103 | } 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/csv" 5 | "flag" 6 | "fmt" 7 | _ "image/jpeg" 8 | _ "image/png" 9 | "io/ioutil" 10 | "log" 11 | "os" 12 | "runtime" 13 | "runtime/pprof" 14 | "strconv" 15 | "strings" 16 | "time" 17 | 18 | "github.com/lon9/caffe2go/c2g" 19 | ) 20 | 21 | func loadMeans(meanFile string) ([]float32, error) { 22 | b, err := os.Open(meanFile) 23 | if err != nil { 24 | return nil, err 25 | } 26 | r := csv.NewReader(b) 27 | means, err := r.Read() 28 | if err != nil { 29 | return nil, err 30 | } 31 | res := make([]float32, len(means)) 32 | for i := range means { 33 | out, err := strconv.ParseFloat(means[i], 32) 34 | if err != nil { 35 | return nil, err 36 | } 37 | res[i] = float32(out) 38 | } 39 | return res, nil 40 | } 41 | 42 | func main() { 43 | 44 | runtime.GOMAXPROCS(runtime.NumCPU()) 45 | 46 | var ( 47 | modelPath string 48 | imagePath string 49 | labelPath string 50 | shape uint 51 | meanFile string 52 | cpuProf string 53 | memProf string 54 | ) 55 | 56 | flag.StringVar(&modelPath, "m", "", "Path for caffemodel.") 57 | flag.StringVar(&imagePath, "i", "", "Path for image.") 58 | flag.StringVar(&labelPath, "l", "", "Path for labels.") 59 | flag.StringVar(&meanFile, "mf", "", "Meanfile path") 60 | flag.UintVar(&shape, "s", 0, "Input Shape") 61 | flag.StringVar(&cpuProf, "cpuProf", "", "Filename for CPU profiling.") 62 | flag.StringVar(&memProf, "memProf", "", "Filename for Memory profiling.") 63 | flag.Parse() 64 | 65 | if modelPath == "" || imagePath == "" || shape == 0 { 66 | log.Fatalln("Option is not enough.") 67 | } 68 | 69 | var means []float32 70 | var err error 71 | if meanFile != "" { 72 | means, err = loadMeans(meanFile) 73 | if err != nil { 74 | log.Fatalln(err) 75 | } 76 | } 77 | 78 | caffe2go, err := c2g.NewCaffe2Go(modelPath) 79 | if err != nil { 80 | log.Fatalln(err) 81 | } 82 | start := time.Now() 83 | if cpuProf != "" { 84 | cf, err := os.Create(cpuProf) 85 | if err != nil { 86 | log.Fatalln(err) 87 | } 88 | defer cf.Close() 89 | pprof.StartCPUProfile(cf) 90 | } 91 | output, err := caffe2go.Predict(imagePath, shape, means) 92 | if err != nil { 93 | log.Fatalln(err) 94 | } 95 | if memProf != "" { 96 | pprof.StopCPUProfile() 97 | mf, err := os.Create(memProf) 98 | if err != nil { 99 | log.Fatalln(err) 100 | } 101 | defer mf.Close() 102 | pprof.WriteHeapProfile(mf) 103 | } 104 | fmt.Printf("Done in %fs\n", time.Now().Sub(start).Seconds()) 105 | 106 | if labelPath != "" { 107 | result := make([]float32, len(output)) 108 | for i := range output { 109 | result[i] = output[i][0][0] 110 | } 111 | indice := make([]int, len(result)) 112 | labelData, err := ioutil.ReadFile(labelPath) 113 | if err != nil { 114 | log.Fatalln(err) 115 | } 116 | labels := strings.Split(string(labelData), "\n") 117 | 118 | Argsort(result, indice) 119 | for i := range indice[:10] { 120 | fmt.Printf("%f:%s\n", result[i], labels[indice[i]]) 121 | } 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /mean.txt: -------------------------------------------------------------------------------- 1 | 103.939,116.779,123.68 2 | -------------------------------------------------------------------------------- /models/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lon9/caffe2go/64cb8c3e6162259faeddf2bd55f724340fb78fad/models/.gitkeep -------------------------------------------------------------------------------- /network/network.go: -------------------------------------------------------------------------------- 1 | package network 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/lon9/caffe2go/layers" 7 | ) 8 | 9 | // Network have netword definition. 10 | type Network struct { 11 | layers []layers.Layer 12 | } 13 | 14 | // Add adds layer to network. 15 | func (n *Network) Add(layer layers.Layer) { 16 | n.layers = append(n.layers, layer) 17 | } 18 | 19 | // Predict forwards network. 20 | func (n *Network) Predict(input [][][]float32) (output [][][]float32, err error) { 21 | for i := range n.layers { 22 | fmt.Println(n.layers[i].GetName()) 23 | fmt.Println(len(input), len(input[0])) 24 | input, err = n.layers[i].Forward(input) 25 | if err != nil { 26 | return 27 | } 28 | } 29 | output = input 30 | return 31 | } 32 | --------------------------------------------------------------------------------