Skip to content

Instantly share code, notes, and snippets.

@raytroop
Last active December 10, 2019 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raytroop/d2414bbb537a8f8baceaaee4fa6717e8 to your computer and use it in GitHub Desktop.
Save raytroop/d2414bbb537a8f8baceaaee4fa6717e8 to your computer and use it in GitHub Desktop.
// image convolution (3x3 conv)
int WIDTH = 1024;
int HEIGHT = 1024;
float input[(WIDTH + 2) * (HEIGHT + 2)];
float output[WIDTH * HEIGHT];
float weights[] = {1.0 / 9, 1.0 / 9, 1.0 / 9,
1.0 / 9, 1.0 / 9, 1.0 / 9,
1.0 / 9, 1.0 / 9, 1.0 / 9};
for (int j = 0; j < HEIGHT; j++)
{
for (int i = 0; i < WIDTH; i++)
{
float tmp = 0.f;
for (int jj = 0; jj < 3; jj++)
for (int ii = 0; ii < 3; ii++)
tmp += input[(j + jj) * (WIDTH + 2) + (i + ii)] * weights[jj * 3 + ii];
output[j * WIDTH + i] = tmp;
}
}
int WIDTH = 1024;
int HEIGHT = 1024;
int STRIDE = 2;
float input[(WIDTH + 2) * (HEIGHT + 2)];
float output[(WIDTH / STRIDE) * (HEIGHT / STRIDE)];
float weights[] = {1.0 / 9, 1.0 / 9, 1.0 / 9,
1.0 / 9, 1.0 / 9, 1.0 / 9,
1.0 / 9, 1.0 / 9, 1.0 / 9};
for (int j = 0; j < HEIGHT; j += STRIDE)
{
for (int i = 0; i < WIDTH; i += STRIDE)
{
float tmp = 0.f;
for (int jj = 0; jj < 3; jj++)
for (int ii = 0; ii < 3; ii++)
tmp += input[(j + jj) * (WIDTH + 2) + (i + ii)] * weights[jj * 3 + ii];
output[(j / STRIDE) * (WIDTH / STRIDE) + (i / STRIDE)] = tmp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment