nn

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 21, 2020 License: BSD-2-Clause Imports: 5 Imported by: 0

Documentation

Overview

Package nn implements a small collection of neural network layers for denoising auto-encoders.

It is designed for inference, not for training.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvOutputSize

func ConvOutputSize(height, width, kernelSize, stride int) (heightOut, widthOut int)

ConvOutputSize gets the output dimensions from a convolution operation.

func DeconvOutputSize

func DeconvOutputSize(height, width, kernelSize, stride int) (heightOut, widthOut int)

DeconvOutputSize gets the output dimensions from a transposed convolution operation.

func Groups

func Groups(t *Tensor, numGroups int, f func(groupIdx, dataIdx int))

Groups iterates over the entries of t in order, but adds a groupIdx parameter indicating which group each component belongs to for group normalization.

func Patches

func Patches(t *Tensor, kernelSize, stride int, f func(int, *Tensor))

Patches extracts image patches for a convolution of the given kernel size and stride, and calls f with each patch.

It may call f from multiple Goroutines concurrently.

The patches may be passed to f in any order. The index passed as the first argument to f goes left to right, top to bottom, so that patches are indexed like the pixels of an output image.

Types

type Bias

type Bias struct {
	Data []float32
}

A Bias layer adds a per-channel constant to a Tensor.

func (*Bias) Apply

func (b *Bias) Apply(t *Tensor) *Tensor

Apply adds the bias to the Tensor.

type Bilateral

type Bilateral struct {
	KernelSize int
	SigmaBlur  float64
	SigmaDiff  float64
}

Bilateral is a bilateral filtering layer.

func (*Bilateral) Apply

func (b *Bilateral) Apply(t *Tensor) *Tensor

Apply applies the bilateral filter and returns a Tensor of the same shape as t.

type Conv

type Conv struct {
	OutDepth   int
	InDepth    int
	KernelSize int
	Stride     int
	Weights    []float32
}

Conv is a 2D convolution operator.

It contains weights of the shape:

[out_depth x in_depth x kernel_size x kernel_size]

func (*Conv) Apply

func (c *Conv) Apply(t *Tensor) *Tensor

Apply applies the convolution to a Tensor.

The resulting Tensor's size is determined by ConvOutputSize().

type Deconv

type Deconv struct {
	OutDepth   int
	InDepth    int
	KernelSize int
	Stride     int
	Weights    []float32
}

Deconv is a 2D transposed convolution operator.

It contains weights of the shape:

[in_depth x out_depth x kernel_size x kernel_size]

func (*Deconv) Apply

func (d *Deconv) Apply(t *Tensor) *Tensor

Apply applies the transposed convolution to a Tensor.

The resulting Tensor's size is determined by DeconvOutputSize().

type GroupNorm

type GroupNorm struct {
	NumGroups int
}

GroupNorm implements the normalization step of group normalization.

func (*GroupNorm) Apply

func (g *GroupNorm) Apply(t *Tensor) *Tensor

Apply applies the normalization step.

type Layer

type Layer interface {
	Apply(t *Tensor) *Tensor
}

A Layer is a Tensor operation.

type Mul

type Mul struct {
	Data []float32
}

A Mul layer multiplies a per-channel mask to a Tensor.

func (*Mul) Apply

func (m *Mul) Apply(t *Tensor) *Tensor

Apply multiplies the mask to the Tensor.

type NN

type NN []Layer

An NN is a special Layer that composes multiple other Layers.

func (NN) Apply

func (n NN) Apply(t *Tensor) *Tensor

Apply applies all the layers in order.

type Pad

type Pad struct {
	Top    int
	Right  int
	Bottom int
	Left   int
}

A Pad layer pads input Tensors.

func NewPad

func NewPad(t, r, b, l int) *Pad

NewPad creates a Pad with the given values.

func (*Pad) Apply

func (p *Pad) Apply(t *Tensor) *Tensor

Apply pads the Tensor.

type ReLU

type ReLU struct{}

A ReLU layer applies the rectified linear unit.

func (ReLU) Apply

func (r ReLU) Apply(t *Tensor) *Tensor

Apply applies the rectified linear unit.

type Residual

type Residual []Layer

Residual is a special Layer that composes multiple other Layers and adds the output to the input.

func (Residual) Apply

func (r Residual) Apply(t *Tensor) *Tensor

Apply applies the layers in order and adds the output to the original input.

type SpatialConv

type SpatialConv struct {
	Depth      int
	KernelSize int
	Stride     int
	Weights    []float32
}

SpatialConv is a spatial-only 2D convolution operator. Unlike Conv, it uses a separate depthwise filter for each channel of the input; channels are not mixed.

It contains weights of the shape:

[depth x kernel_size x kernel_size]

func (*SpatialConv) Apply

func (s *SpatialConv) Apply(t *Tensor) *Tensor

Apply applies the convolution to a Tensor.

The resulting Tensor's size is determined by ConvOutputSize().

type Tensor

type Tensor struct {
	Height int
	Width  int
	Depth  int

	Data []float32
}

Tensor is a 3D array of numbers.

It is arranged as [height x width x depth], with the outer dimension being height.

func NewTensor

func NewTensor(height, width, depth int) *Tensor

NewTensor creates a zero tensor.

func NewTensorRGB

func NewTensorRGB(img image.Image) *Tensor

NewTensorRGB creates an RGB Tensor from an image.

func (*Tensor) Add

func (t *Tensor) Add(s float32) *Tensor

Add adds a scalar to every entry.

func (*Tensor) At

func (t *Tensor) At(y, x, z int) *float32

At gets a pointer to the given coordinate.

func (*Tensor) Pad

func (t *Tensor) Pad(top, right, bottom, left int) *Tensor

Pad creates a zero-padded version of the Tensor.

func (*Tensor) RGB

func (t *Tensor) RGB() image.Image

RGB creates an RGB image out of the Tensor.

If the tensor does not have three channels, this will panic().

func (*Tensor) Unpad

func (t *Tensor) Unpad(top, right, bottom, left int) *Tensor

Unpad cuts out the edges of the Tensor, effectively inverting the operation done by Pad.

type Unpad

type Unpad struct {
	Top    int
	Right  int
	Bottom int
	Left   int
}

An Unpad layer unpads (crops) input Tensors.

func NewUnpad

func NewUnpad(t, r, b, l int) *Unpad

NewUnpad creates an Unpad with the given values.

func (*Unpad) Apply

func (u *Unpad) Apply(t *Tensor) *Tensor

Apply unpads (crops) the Tensor.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL