From b89c80335550ee1a60840184b1fea5169456fc62 Mon Sep 17 00:00:00 2001 From: ikawaha Date: Fri, 4 Mar 2022 00:23:31 +0900 Subject: [PATCH 1/2] Set log output --- cmd/cmd.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/cmd.go b/cmd/cmd.go index 6e8bb6f..3239025 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -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 From 2dbe5cb3b0c531e39d3aa30367ebf214f9568834 Mon Sep 17 00:00:00 2001 From: ikawaha Date: Fri, 4 Mar 2022 00:24:24 +0900 Subject: [PATCH 2/2] Use float32 instead of float64 for model parameters --- engine/channel_image.go | 2 +- engine/image_plane.go | 12 ++++++------ engine/model.go | 8 ++++---- engine/model_test.go | 6 +++--- engine/waifu2x.go | 6 +++--- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/engine/channel_image.go b/engine/channel_image.go index 54790ce..07cd0e6 100644 --- a/engine/channel_image.go +++ b/engine/channel_image.go @@ -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 { diff --git a/engine/image_plane.go b/engine/image_plane.go index 7874c9c..7312c03 100644 --- a/engine/image_plane.go +++ b/engine/image_plane.go @@ -16,7 +16,7 @@ const ( type ImagePlane struct { Width int Height int - Buffer []float64 + Buffer []float32 } // NewImagePlaneWidthHeight returns an image plane of specific width and height. @@ -24,7 +24,7 @@ func NewImagePlaneWidthHeight(width, height int) ImagePlane { return ImagePlane{ Width: width, Height: height, - Buffer: make([]float64, width*height), + Buffer: make([]float32, width*height), } } @@ -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 } @@ -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))) @@ -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 @@ -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 } diff --git a/engine/model.go b/engine/model.go index 24325d9..5f1c9c7 100644 --- a/engine/model.go +++ b/engine/model.go @@ -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. @@ -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 diff --git a/engine/model_test.go b/engine/model_test.go index e50b90f..e3be5a3 100644 --- a/engine/model_test.go +++ b/engine/model_test.go @@ -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++ { diff --git a/engine/waifu2x.go b/engine/waifu2x.go index a98c037..ebc89d7 100644 --- a/engine/waifu2x.go +++ b/engine/waifu2x.go @@ -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 } @@ -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] }