Writing a simple code to understand how convolutions work.
Is there any basic step that is wrong?
I know that the pixel values will be wrong but how to fix this? Is it an option or an extra operation?
import * as tf from "@tensorflow/tfjs-node"
import { readFile, writeFile } from "node:fs/promises"
async function mainModule() {
const img = tf.node.decodeImage(await readFile("./greyscale.png"), 1) as tf.Tensor3D;
const result = img.div(255).conv2d(
tf.tensor4d([-1, 2, -1,
-1, 2, -1,
-1, 2, -1,
], [1, 1, 3, 3], "float32").div(6) as tf.Tensor4D, 1, "same") as tf.Tensor3D
const data = await tf.node.encodePng(result)
await writeFile("./result.png", data)
}
mainModule()