int n = 50; int ks = 15; int ps = 4; float input[1500][1500]; float kernel[15][15]; float conv_output[1500][1500]; float pooling_output[368][368]; float max(float a, float b) { if (a > b) { return a; } else { return b; } } float exp(float x) { return 1 + x + (x*x)/2 + (x*x*x)/6 + (x*x*x*x)/24; } float sigmoid(float x) { return 1 / (1 + exp(-x)); } void kernel_conv_pooling(float A[][1500], float B[][1500], float C[][368], float kernel[][15], int n, int ks, int ps) { int i, j, k, l; float v; i = 0; while (i < n - ks + 1) { j = 0; while (j < n - ks + 1) { v = 0; k = 0; while (k < ks) { l = 0; while (l < ks) { v =v+ A[i + k][j + l] * kernel[k][l]; l =l+ 1; } k =k+ 1; } B[i][j] = v; j =j+ 1; } i =i+ 1; } n = n - ks + 1; i = 0; while (i < n - ks + 1) { j = 0; while (j < n - ks + 1) { v = 0; k = 0; while (k < ks) { l = 0; while (l < ks) { v =v+ B[i + k][j + l] * kernel[k][l]; l =l+ 1; } k =k+ 1; } A[i][j] = v; j =j+ 1; } i =i+ 1; } n = (n - ks + 1) / ps; i = 0; while (i < n) { j = 0; while (j < n) { v = A[i * ps][j * ps]; k = 0; while (k < ps) { l = 0; while (l < ps) { v = max(v, A[i * ps + k][j * ps + l]); l =l+ 1; } k =k+ 1; } C[i][j] = v; j =j+ 1; } i =i+ 1; } i = 0; while (i < n) { v = 0; j = 0; while (j < n) { C[i][j] = C[i][j] * sigmoid(C[i][j]); j =j+ 1; } i =i+ 1; } } int main() { int os = (n - 2 * ks + 2) / ps; getfarray(input); getfarray(kernel); starttime(); kernel_conv_pooling(input, conv_output, pooling_output, kernel, n, ks, ps); stoptime(); putfarray(os*os, pooling_output); return 0; }