|
| 1 | +#include <FastConv2D.hpp> |
| 2 | + |
| 3 | +#include <Math.hpp> |
| 4 | + |
| 5 | +#include <iostream> |
| 6 | +#include <stdexcept> |
| 7 | + |
| 8 | +FastConv2D::FastConv2D(std::vector<std::vector<double>> kernel, int inputWidth, int inputHeight) : Conv2D(kernel, inputWidth, inputHeight) { |
| 9 | + int kernelWidth = kernel.size(); |
| 10 | + int kernelHeight = kernel[0].size(); |
| 11 | + |
| 12 | + fftw_complex* kernelIn = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * inputWidth * inputHeight); |
| 13 | + _kernel_fft = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * inputWidth * inputHeight); |
| 14 | + fftw_plan kernelPlan = fftw_plan_dft_2d(inputWidth, inputHeight, kernelIn, _kernel_fft, FFTW_FORWARD, FFTW_PATIENT); |
| 15 | + if (kernelWidth != inputWidth || kernelHeight != inputHeight) { |
| 16 | + throw std::invalid_argument("Kernel size must match input size for FastConv2D"); |
| 17 | + } |
| 18 | + for (int x = 0; x < inputWidth; x++) { |
| 19 | + for (int y = 0; y < inputHeight; y++) { |
| 20 | + kernelIn[x * inputHeight + y][0] = kernel[x][y]; |
| 21 | + kernelIn[x * inputHeight + y][1] = 0; |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + fftw_execute(kernelPlan); |
| 26 | + fftw_destroy_plan(kernelPlan); |
| 27 | + fftw_free(kernelIn); |
| 28 | + |
| 29 | + _grid_fft = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * inputWidth * inputHeight); |
| 30 | + _input_fft = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * inputWidth * inputHeight); |
| 31 | + |
| 32 | + _gridPlan = fftw_plan_dft_2d(inputWidth, inputHeight, _input_fft, _grid_fft, FFTW_FORWARD, FFTW_PATIENT); |
| 33 | + _inversePlan = fftw_plan_dft_2d(inputWidth, inputHeight, _grid_fft, _input_fft, FFTW_BACKWARD, FFTW_PATIENT); |
| 34 | +} |
| 35 | + |
| 36 | +FastConv2D::~FastConv2D() { |
| 37 | + fftw_destroy_plan(_gridPlan); |
| 38 | + fftw_destroy_plan(_inversePlan); |
| 39 | + fftw_free(_grid_fft); |
| 40 | + fftw_free(_input_fft); |
| 41 | + fftw_free(_kernel_fft); |
| 42 | +} |
| 43 | + |
| 44 | +std::vector<std::vector<double>> FastConv2D::apply(std::vector<std::vector<double>> input) { |
| 45 | + int inputWidth = input.size(); |
| 46 | + int inputHeight = input[0].size(); |
| 47 | + |
| 48 | + for (int x = 0; x < inputWidth; x++) { |
| 49 | + for (int y = 0; y < inputHeight; y++) { |
| 50 | + _input_fft[x * inputHeight + y][0] = input[x][y]; |
| 51 | + _input_fft[x * inputHeight + y][1] = 0; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + fftw_execute(_gridPlan); |
| 56 | + |
| 57 | + for (int x = 0; x < inputWidth; x++) { |
| 58 | + for (int y = 0; y < inputHeight; y++) { |
| 59 | + double real = _grid_fft[x * inputHeight + y][0] * _kernel_fft[x * inputHeight + y][0] - _grid_fft[x * inputHeight + y][1] * _kernel_fft[x * inputHeight + y][1]; |
| 60 | + double imag = _grid_fft[x * inputHeight + y][0] * _kernel_fft[x * inputHeight + y][1] + _grid_fft[x * inputHeight + y][1] * _kernel_fft[x * inputHeight + y][0]; |
| 61 | + _grid_fft[x * inputHeight + y][0] = real; |
| 62 | + _grid_fft[x * inputHeight + y][1] = imag; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + fftw_execute(_inversePlan); |
| 67 | + |
| 68 | + std::vector<std::vector<double>> output(inputWidth, std::vector<double>(inputHeight)); |
| 69 | + for (int x = 0; x < inputWidth; x++) { |
| 70 | + for (int y = 0; y < inputHeight; y++) { |
| 71 | + output[x][y] = _input_fft[x * inputHeight + y][0] / (inputWidth * inputHeight); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return roll(output, -1, -1); |
| 76 | +} |
0 commit comments