|
| 1 | +package sk.ai.net.nn |
| 2 | + |
| 3 | +import sk.ai.net.Shape |
| 4 | +import sk.ai.net.Tensor |
| 5 | +import sk.ai.net.impl.DoublesTensor |
| 6 | +import sk.ai.net.rand |
| 7 | +import kotlin.math.sqrt |
| 8 | + |
| 9 | +class Conv2d( |
| 10 | + val inChannels: Int, |
| 11 | + val outChannels: Int, |
| 12 | + val kernelSize: Int, |
| 13 | + val stride: Int = 1, |
| 14 | + val padding: Int = 0, |
| 15 | + useBias: Boolean = true |
| 16 | +) { |
| 17 | + val weight: Tensor |
| 18 | + val bias: Tensor? |
| 19 | + |
| 20 | + init { |
| 21 | + // Initialize weights and bias |
| 22 | + val fanIn = inChannels * kernelSize * kernelSize |
| 23 | + val bound = 1f / sqrt(fanIn.toDouble()).toFloat() // 1/sqrt(fanIn) |
| 24 | + // Weight: uniform in [-bound, bound] |
| 25 | + weight = (((rand( |
| 26 | + Shape( |
| 27 | + outChannels, |
| 28 | + inChannels, |
| 29 | + kernelSize, |
| 30 | + kernelSize |
| 31 | + ) |
| 32 | + ) as DoublesTensor) * (2f * bound).toDouble()) as DoublesTensor) - bound.toDouble() |
| 33 | + // Bias: uniform in [-bound, bound] if enabled |
| 34 | + bias = if (useBias) { |
| 35 | + ((rand(Shape(outChannels)) as DoublesTensor) * (2f * bound).toDouble()) - bound.toDouble() |
| 36 | + } else { |
| 37 | + null |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + operator fun invoke(input: Tensor): Tensor { |
| 42 | + // Ensure input has 3D or 4D shape |
| 43 | + val shape = input.shape // assume shape is a list or array of dimensions |
| 44 | + require(shape.rank == 3 || shape.rank == 4) { |
| 45 | + "Conv2d expected 3D or 4D input tensor, but got shape ${shape}." |
| 46 | + } |
| 47 | + // Determine batch size and input dims |
| 48 | + val batchSize: Int |
| 49 | + val inC: Int |
| 50 | + val inH: Int |
| 51 | + val inW: Int |
| 52 | + if (shape.rank == 4) { |
| 53 | + batchSize = shape.dimensions[0] |
| 54 | + inC = shape[1] |
| 55 | + inH = shape[2] |
| 56 | + inW = shape[3] |
| 57 | + } else { |
| 58 | + // if 3D (C, H, W), treat as batch of size 1 |
| 59 | + batchSize = 1 |
| 60 | + inC = shape[0] |
| 61 | + inH = shape[1] |
| 62 | + inW = shape[2] |
| 63 | + } |
| 64 | + require(inC == inChannels) { |
| 65 | + "Conv2d expected input channel count $inChannels, but got $inC." |
| 66 | + } |
| 67 | + |
| 68 | + // Compute output spatial dimensions |
| 69 | + val outH = (inH + 2 * padding - kernelSize) / stride + 1 |
| 70 | + val outW = (inW + 2 * padding - kernelSize) / stride + 1 |
| 71 | + require(outH > 0 && outW > 0) { |
| 72 | + "Conv2d output size is invalid (outH=$outH, outW=$outW). Check input dimensions and padding." |
| 73 | + } |
| 74 | + |
| 75 | + // Apply padding if needed |
| 76 | + val paddedInput: Tensor = if (padding > 0) { |
| 77 | + val paddedH = inH + 2 * padding |
| 78 | + val paddedW = inW + 2 * padding |
| 79 | + val temp = Tensor.zeros(batchSize, inC, paddedH, paddedW) |
| 80 | + for (n in 0 until batchSize) { |
| 81 | + for (c in 0 until inC) { |
| 82 | + for (i in 0 until inH) { |
| 83 | + for (j in 0 until inW) { |
| 84 | + temp[n, c, i + padding, j + padding] = input[n, c, i, j] |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + temp |
| 90 | + } else { |
| 91 | + input // no padding needed |
| 92 | + } |
| 93 | + |
| 94 | + // Prepare output tensor |
| 95 | + val output = Tensor.zeros(batchSize, outChannels, outH, outW) |
| 96 | + |
| 97 | + // Convolution: iterate over batch, out channels, and output spatial positions |
| 98 | + for (n in 0 until batchSize) { |
| 99 | + for (oc in 0 until outChannels) { |
| 100 | + val biasVal = if (bias != null) bias[oc] else 0f |
| 101 | + for (i in 0 until outH) { |
| 102 | + for (j in 0 until outW) { |
| 103 | + var sum = 0f |
| 104 | + // Sum over all input channels and kernel elements |
| 105 | + for (c in 0 until inChannels) { |
| 106 | + for (ki in 0 until kernelSize) { |
| 107 | + for (kj in 0 until kernelSize) { |
| 108 | + sum += paddedInput[n, c, i * stride + ki, j * stride + kj] * |
| 109 | + weight[oc, c, ki, kj] |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + // Add bias and assign to output |
| 114 | + output[n, oc, i, j] = sum + biasVal |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + return output |
| 120 | + } |
| 121 | +} |
0 commit comments