Skip to content

Commit

Permalink
update scale implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nihui committed Feb 10, 2021
1 parent dacd97d commit 77534d9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ Usage: waifu2x-ncnn-vulkan -i infile -o outfile [options]...
-i input-path input image path (jpg/png/webp) or directory
-o output-path output image path (jpg/png/webp) or directory
-n noise-level denoise level (-1/0/1/2/3, default=0)
-s scale upscale ratio (1/2, default=2)
-s scale upscale ratio (1/2/4/8/16/32, default=2)
-t tile-size tile size (>=32/0=auto, default=0) can be 0,0,0 for multi-gpu
-m model-path waifu2x model path (default=models-cunet)
-g gpu-id gpu device to use (default=0) can be 0,1,2 for multi-gpu
-g gpu-id gpu device to use (-1=cpu, default=auto) can be 0,1,2 for multi-gpu
-j load:proc:save thread count for load/proc/save (default=1:2:2) can be 1:2,2,2:2 for multi-gpu
-x enable tta mode
-f format output image format (jpg/png/webp, default=ext/png)
Expand Down
59 changes: 35 additions & 24 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ static void print_usage()
fprintf(stdout, " -i input-path input image path (jpg/png/webp) or directory\n");
fprintf(stdout, " -o output-path output image path (jpg/png/webp) or directory\n");
fprintf(stdout, " -n noise-level denoise level (-1/0/1/2/3, default=0)\n");
fprintf(stdout, " -s scale upscale ratio (1/2/4/8/16/..., default=2)\n");
fprintf(stdout, " -s scale upscale ratio (1/2/4/8/16/32, default=2)\n");
fprintf(stdout, " -t tile-size tile size (>=32/0=auto, default=0) can be 0,0,0 for multi-gpu\n");
fprintf(stdout, " -m model-path waifu2x model path (default=models-cunet)\n");
fprintf(stdout, " -g gpu-id gpu device to use (-1=cpu, default=auto) can be 0,1,2 for multi-gpu\n");
Expand All @@ -126,8 +126,6 @@ class Task

ncnn::Mat inimage;
ncnn::Mat outimage;

int scale_run_count;
};

class TaskQueue
Expand Down Expand Up @@ -272,7 +270,6 @@ void* load(void* args)
{
Task v;
v.id = i;
v.scale_run_count = log2((double)scale);
v.webp = webp;
v.inpath = imagepath;
v.outpath = ltp->output_files[i];
Expand Down Expand Up @@ -311,14 +308,12 @@ class ProcThreadParams
{
public:
const Waifu2x* waifu2x;
int verbose;
};

void* proc(void* args)
{
const ProcThreadParams* ptp = (const ProcThreadParams*)args;
const Waifu2x* waifu2x = ptp->waifu2x;
const int verbose = ptp->verbose;

for (;;)
{
Expand All @@ -329,24 +324,41 @@ void* proc(void* args)
if (v.id == -233)
break;

for (int i = 0; i < v.scale_run_count; i++)
const int scale = v.outimage.w / v.inimage.w;
int scale_run_count = 0;
if (scale == 1 || scale == 2)

This comment has been minimized.

Copy link
@Geoxor

Geoxor Mar 8, 2021

use switch here instead like this

switch (scale) {
case 1:
case 2:
scale_run_count = 1;
break;
case 4:
scale_run_count = 2;
break;
default:
scale_run_count = 1;
}

{
scale_run_count = 1;
}
if (scale == 4)
{
scale_run_count = 2;
}
if (scale == 8)
{
scale_run_count = 3;
}
if (scale == 16)
{
scale_run_count = 4;
}
if (scale == 32)
{
scale_run_count = 5;
}

This comment has been minimized.

Copy link
@Olimp666

Olimp666 Mar 4, 2021

bruh you could just use scale_run_count=log2(scale) or at least use else if

for (int i = 0; i < scale_run_count; i++)
{
if (i != 0)
if (i == scale_run_count - 1)
{
v.inimage.clone_from(v.outimage);
waifu2x->process(v.inimage, v.outimage);
}
v.outimage.create(v.inimage.w * 2, v.inimage.h * 2, (size_t)v.inimage.elemsize, (int)v.inimage.elemsize);

if (verbose)
else
{
#if _WIN32
fwprintf(stdout, L"scaling %ix%i %i to %ix%i %i\n", v.inimage.w, v.inimage.h, v.inimage.elemsize, v.outimage.w, v.outimage.h, v.outimage.elemsize);
#else
fprintf(stdout, "scaling %ix%i %i to %ix%i %i\n", v.inimage.w, v.inimage.h, v.inimage.elemsize, v.outimage.w, v.outimage.h, v.outimage.elemsize);
#endif
ncnn::Mat tmpimage(v.inimage.w * 2, v.inimage.h * 2, (size_t)v.inimage.elemsize, (int)v.inimage.elemsize);
waifu2x->process(v.inimage, tmpimage);
v.inimage = tmpimage;
}

waifu2x->process(v.inimage, v.outimage);
}

tosave.put(v);
Expand Down Expand Up @@ -568,7 +580,7 @@ int main(int argc, char** argv)
return -1;
}

if (scale < 1 || fmod(log2((double)scale), 1) != 0.0) // If is smaller than 1 or not a power of 2
if (!(scale == 1 || scale == 2 || scale == 4 || scale == 8 || scale == 16 || scale == 32))
{
fprintf(stderr, "invalid scale argument\n");
return -1;
Expand Down Expand Up @@ -708,7 +720,7 @@ int main(int argc, char** argv)
{
prepadding = 28;
}
else // if scale is bigger than 1
else if (scale == 2 || scale == 4 || scale == 8 || scale == 16 || scale == 32)
{
prepadding = 18;
}
Expand Down Expand Up @@ -740,7 +752,7 @@ int main(int argc, char** argv)
swprintf(parampath, 256, L"%s/noise%d_model.param", model.c_str(), noise);
swprintf(modelpath, 256, L"%s/noise%d_model.bin", model.c_str(), noise);
}
else // if scale is bigger than 1
else if (scale == 2 || scale == 4 || scale == 8 || scale == 16 || scale == 32)
{
swprintf(parampath, 256, L"%s/noise%d_scale2.0x_model.param", model.c_str(), noise);
swprintf(modelpath, 256, L"%s/noise%d_scale2.0x_model.bin", model.c_str(), noise);
Expand All @@ -758,7 +770,7 @@ int main(int argc, char** argv)
sprintf(parampath, "%s/noise%d_model.param", model.c_str(), noise);
sprintf(modelpath, "%s/noise%d_model.bin", model.c_str(), noise);
}
else // if scale is bigger than 1
else if (scale == 2 || scale == 4 || scale == 8 || scale == 16 || scale == 32)
{
sprintf(parampath, "%s/noise%d_scale2.0x_model.param", model.c_str(), noise);
sprintf(modelpath, "%s/noise%d_scale2.0x_model.bin", model.c_str(), noise);
Expand Down Expand Up @@ -896,7 +908,6 @@ int main(int argc, char** argv)
for (int i=0; i<use_gpu_count; i++)
{
ptp[i].waifu2x = waifu2x[i];
ptp[i].verbose = verbose;
}

std::vector<ncnn::Thread*> proc_threads(total_jobs_proc);
Expand Down

1 comment on commit 77534d9

@TilCreator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused about this commit since I don't really see an improvement, it mostly hard codes the "is a power of 2" check on multiple positions and adds a third buffer to the proc function. Am I overlooking something?

Please sign in to comment.