Skip to content

Commit

Permalink
Merge pull request #24 from ikawaha/fix/float64-32
Browse files Browse the repository at this point in the history
Use float32 instead of float64 for model parameters
  • Loading branch information
ikawaha authored Mar 3, 2022
2 parents 83f8ead + 2dbe5cb commit 7aac58d
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 17 deletions.
1 change: 1 addition & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func Run(args []string) error {
w2x, err := engine.NewWaifu2x(opt.mode, opt.noise, []engine.Option{
engine.Verbose(opt.verbose),
engine.Parallel(opt.parallel),
engine.LogOutput(os.Stderr),
}...)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion engine/channel_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func NewChannelImage(img image.Image) (ChannelImage, bool, error) {
func NewDenormalizedChannelImage(p ImagePlane) ChannelImage {
img := NewChannelImageWidthHeight(p.Width, p.Height)
for i := range p.Buffer {
v := int(math.Round(p.Buffer[i] * 255.0))
v := int(math.Round(float64(p.Buffer[i]) * 255.0))
if v < 0 {
v = 0
} else if v > 255 {
Expand Down
12 changes: 6 additions & 6 deletions engine/image_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ const (
type ImagePlane struct {
Width int
Height int
Buffer []float64
Buffer []float32
}

// NewImagePlaneWidthHeight returns an image plane of specific width and height.
func NewImagePlaneWidthHeight(width, height int) ImagePlane {
return ImagePlane{
Width: width,
Height: height,
Buffer: make([]float64, width*height),
Buffer: make([]float32, width*height),
}
}

Expand All @@ -35,7 +35,7 @@ func NewNormalizedImagePlane(img ChannelImage) (ImagePlane, error) {
return ImagePlane{}, fmt.Errorf("invalid image channel: width*heignt=%d <> len(buffer)=%d", img.Width*img.Height, img.Buffer)
}
for i := range img.Buffer {
p.Buffer[i] = float64(img.Buffer[i]) / 255.0
p.Buffer[i] = float32(img.Buffer[i]) / 255.0
}
return p, nil
}
Expand All @@ -46,7 +46,7 @@ func (p ImagePlane) Index(width, height int) int {
}

// Value returns the value corresponding to the specified width and height of the image.
func (p ImagePlane) Value(width, height int) float64 {
func (p ImagePlane) Value(width, height int) float32 {
i := p.Index(width, height)
if i < 0 || i >= len(p.Buffer) {
panic(fmt.Errorf("width %d, height %d, Index %d, len(buf) %d", width, height, i, len(p.Buffer)))
Expand All @@ -59,7 +59,7 @@ func (p ImagePlane) Value(width, height int) float64 {
// [a0][a1][a2]
// [b0][b1][b2]
// [c0][c1][c2] where (x, y) is b1.
func (p ImagePlane) SegmentAt(x, y int) (a0, a1, a2, b0, b1, b2, c0, c1, c2 float64) {
func (p ImagePlane) SegmentAt(x, y int) (a0, a1, a2, b0, b1, b2, c0, c1, c2 float32) {
i := (x - 1) + (y-1)*p.Width
j := i + p.Width
k := j + p.Width
Expand All @@ -70,7 +70,7 @@ func (p ImagePlane) SegmentAt(x, y int) (a0, a1, a2, b0, b1, b2, c0, c1, c2 floa
}

// SetAt sets the value to the buffer corresponding to the specified width and height of the image.
func (p *ImagePlane) SetAt(width, height int, v float64) {
func (p *ImagePlane) SetAt(width, height int, v float32) {
p.Buffer[p.Index(width, height)] = v
}

Expand Down
8 changes: 4 additions & 4 deletions engine/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (

// Param represents a parameter of the model.
type Param struct {
Bias []float64 `json:"bias"` // バイアス
Bias []float32 `json:"bias"` // バイアス
KW int `json:"kW"` // フィルタの幅
KH int `json:"kH"` // フィルタの高さ
Weight [][][][]float64 `json:"weight"` // 重み
Weight [][][][]float32 `json:"weight"` // 重み
NInputPlane int `json:"nInputPlane"` // 入力平面数
NOutputPlane int `json:"nOutputPlane"` // 出力平面数
WeightVec []float64
WeightVec []float32
}

// Model represents a trained model.
Expand Down Expand Up @@ -130,7 +130,7 @@ func (m Model) setWeightVec() {
param := m[l]
// [nOutputPlane][nInputPlane][3][3]
const square = 9
vec := make([]float64, param.NInputPlane*param.NOutputPlane*9)
vec := make([]float32, param.NInputPlane*param.NOutputPlane*9)
for i := 0; i < param.NInputPlane; i++ {
for o := 0; o < param.NOutputPlane; o++ {
offset := i*param.NOutputPlane*square + o*square
Expand Down
6 changes: 3 additions & 3 deletions engine/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ func Test_setWeightVec(t *testing.T) {
}

// W[][O*I*9]
func typeW(model Model) [][]float64 {
var W [][]float64
func typeW(model Model) [][]float32 {
var W [][]float32
for l := range model {
// initialize weight matrix
param := model[l]
var vec []float64
var vec []float32
// [nOutputPlane][nInputPlane][3][3]
for i := 0; i < param.NInputPlane; i++ {
for o := 0; o < param.NOutputPlane; o++ {
Expand Down
6 changes: 3 additions & 3 deletions engine/waifu2x.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func (w Waifu2x) convertRGB(_ context.Context, imageR, imageG, imageB ChannelIma
return R, G, B, nil
}

func convolution(inputPlanes []ImagePlane, W []float64, nOutputPlane int, bias []float64) []ImagePlane {
func convolution(inputPlanes []ImagePlane, W []float32, nOutputPlane int, bias []float32) []ImagePlane {
if len(inputPlanes) == 0 {
return nil
}
Expand All @@ -250,8 +250,8 @@ func convolution(inputPlanes []ImagePlane, W []float64, nOutputPlane int, bias [
for i := 0; i < nOutputPlane; i++ {
outputPlanes[i] = NewImagePlaneWidthHeight(width-2, height-2)
}
sumValues := make([]float64, nOutputPlane)
biasValues := make([]float64, nOutputPlane)
sumValues := make([]float32, nOutputPlane)
biasValues := make([]float32, nOutputPlane)
for i := 0; i < nOutputPlane; i++ {
biasValues[i] = bias[i]
}
Expand Down

0 comments on commit 7aac58d

Please sign in to comment.