2
0

main.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include <string.h>
  6. #include <errno.h>
  7. #include <math.h>
  8. #include <limits.h>
  9. #include <float.h>
  10. #include <sys/stat.h>
  11. #include <sys/types.h>
  12. #include "./config.h"
  13. typedef float Layer[HEIGHT][WIDTH];
  14. static inline int clampi(int x, int low, int high)
  15. {
  16. if (x < low) x = low;
  17. if (x > high) x = high;
  18. return x;
  19. }
  20. void layer_fill_rect(Layer layer, int x, int y, int w, int h, float value)
  21. {
  22. assert(w > 0);
  23. assert(h > 0);
  24. int x0 = clampi(x, 0, WIDTH-1);
  25. int y0 = clampi(y, 0, HEIGHT-1);
  26. int x1 = clampi(x0 + w - 1, 0, WIDTH-1);
  27. int y1 = clampi(y0 + h - 1, 0, HEIGHT-1);
  28. for (int y = y0; y <= y1; ++y) {
  29. for (int x = x0; x <= x1; ++x) {
  30. layer[y][x] = value;
  31. }
  32. }
  33. }
  34. void layer_fill_circle(Layer layer, int cx, int cy, int r, float value)
  35. {
  36. assert(r > 0);
  37. int x0 = clampi(cx - r, 0, WIDTH-1);
  38. int y0 = clampi(cy - r, 0, HEIGHT-1);
  39. int x1 = clampi(cx + r, 0, WIDTH-1);
  40. int y1 = clampi(cy + r, 0, HEIGHT-1);
  41. for (int y = y0; y <= y1; ++y) {
  42. for (int x = x0; x <= x1; ++x) {
  43. int dx = x - cx;
  44. int dy = y - cy;
  45. if (dx*dx + dy*dy <= r*r) {
  46. layer[y][x] = value;
  47. }
  48. }
  49. }
  50. }
  51. void layer_save_as_ppm(Layer layer, const char *file_path)
  52. {
  53. FILE *f = fopen(file_path, "wb");
  54. if (f == NULL) {
  55. fprintf(stderr, "ERROR: could not open file %s: %m\n",
  56. file_path);
  57. exit(1);
  58. }
  59. fprintf(f, "P6\n%d %d 255\n", WIDTH * PPM_SCALER, HEIGHT * PPM_SCALER);
  60. for (int y = 0; y < HEIGHT * PPM_SCALER; ++y) {
  61. for (int x = 0; x < WIDTH * PPM_SCALER; ++x) {
  62. float s = (layer[y / PPM_SCALER][x / PPM_SCALER] + PPM_RANGE) / (2.0f * PPM_RANGE);
  63. char pixel[3] = {
  64. (char) floorf(PPM_COLOR_INTENSITY * (1.0f - s)),
  65. (char) floorf(PPM_COLOR_INTENSITY * (1.0f - s)),
  66. (char) floorf(PPM_COLOR_INTENSITY * s),
  67. };
  68. fwrite(pixel, sizeof(pixel), 1, f);
  69. }
  70. }
  71. fclose(f);
  72. }
  73. void layer_save_as_bin(Layer layer, const char *file_path)
  74. {
  75. FILE *f = fopen(file_path, "wb");
  76. if (f == NULL) {
  77. fprintf(stderr, "ERROR: could not open file %s: %m", file_path);
  78. exit(1);
  79. }
  80. fwrite(layer, sizeof(Layer), 1, f);
  81. fclose(f);
  82. }
  83. void layer_load_from_bin(Layer layer, const char *file_path)
  84. {
  85. (void) layer;
  86. (void) file_path;
  87. assert(0 && "TODO: layer_load_from_bin is not implemented yet!");
  88. }
  89. float feed_forward(Layer inputs, Layer weights)
  90. {
  91. float output = 0.0f;
  92. for (int y = 0; y < HEIGHT; ++y) {
  93. for (int x = 0; x < WIDTH; ++x) {
  94. output += inputs[y][x] * weights[y][x];
  95. }
  96. }
  97. return output;
  98. }
  99. void add_inputs_from_weights(Layer inputs, Layer weights)
  100. {
  101. for (int y = 0; y < HEIGHT; ++y) {
  102. for (int x = 0; x < WIDTH; ++x) {
  103. weights[y][x] += inputs[y][x];
  104. }
  105. }
  106. }
  107. void sub_inputs_from_weights(Layer inputs, Layer weights)
  108. {
  109. for (int y = 0; y < HEIGHT; ++y) {
  110. for (int x = 0; x < WIDTH; ++x) {
  111. weights[y][x] -= inputs[y][x];
  112. }
  113. }
  114. }
  115. int rand_range(int low, int high)
  116. {
  117. assert(low < high);
  118. return rand() % (high - low) + low;
  119. }
  120. void layer_random_rect(Layer layer)
  121. {
  122. layer_fill_rect(layer, 0, 0, WIDTH, HEIGHT, 0.0f);
  123. int x = rand_range(0, WIDTH);
  124. int y = rand_range(0, HEIGHT);
  125. int w = WIDTH - x;
  126. if (w < 2) w = 2;
  127. w = rand_range(1, w);
  128. int h = HEIGHT - x;
  129. if (h < 2) h = 2;
  130. h = rand_range(1, h);
  131. layer_fill_rect(layer, x, y, w, h, 1.0f);
  132. }
  133. void layer_random_circle(Layer layer)
  134. {
  135. layer_fill_rect(layer, 0, 0, WIDTH, HEIGHT, 0.0f);
  136. int cx = rand_range(0, WIDTH);
  137. int cy = rand_range(0, HEIGHT);
  138. int r = INT_MAX;
  139. if (r > cx) r = cx;
  140. if (r > cy) r = cy;
  141. if (r > WIDTH - cx) r = WIDTH - cx;
  142. if (r > HEIGHT - cy) r = HEIGHT - cy;
  143. if (r < 2) r = 2;
  144. r = rand_range(1, r);
  145. layer_fill_circle(layer, cx, cy, r, 1.0f);
  146. }
  147. int train_pass(Layer inputs, Layer weights)
  148. {
  149. static char file_path[256];
  150. static int count = 0;
  151. int adjusted = 0;
  152. for (int i = 0; i < SAMPLE_SIZE; ++i) {
  153. layer_random_rect(inputs);
  154. if (feed_forward(inputs, weights) > BIAS) {
  155. sub_inputs_from_weights(inputs, weights);
  156. snprintf(file_path, sizeof(file_path), DATA_FOLDER"/weights-%03d.ppm", count++);
  157. printf("[INFO] saving %s\n", file_path);
  158. layer_save_as_ppm(weights, file_path);
  159. adjusted += 1;
  160. }
  161. layer_random_circle(inputs);
  162. if (feed_forward(inputs, weights) < BIAS) {
  163. add_inputs_from_weights(inputs, weights);
  164. snprintf(file_path, sizeof(file_path), DATA_FOLDER"/weights-%03d.ppm", count++);
  165. printf("[INFO] saving %s\n", file_path);
  166. layer_save_as_ppm(weights, file_path);
  167. adjusted += 1;
  168. }
  169. }
  170. return adjusted;
  171. }
  172. int check_pass(Layer inputs, Layer weights)
  173. {
  174. int adjusted = 0;
  175. for (int i = 0; i < SAMPLE_SIZE; ++i) {
  176. layer_random_rect(inputs);
  177. if (feed_forward(inputs, weights) > BIAS) {
  178. adjusted += 1;
  179. }
  180. layer_random_circle(inputs);
  181. if (feed_forward(inputs, weights) < BIAS) {
  182. adjusted += 1;
  183. }
  184. }
  185. return adjusted;
  186. }
  187. static Layer inputs;
  188. static Layer weights;
  189. int main(void)
  190. {
  191. printf("[INFO] creating %s\n", DATA_FOLDER);
  192. if (mkdir(DATA_FOLDER, 0755) < 0 && errno != EEXIST) {
  193. fprintf(stderr, "ERROR: could not create folder %s: %s", DATA_FOLDER,
  194. strerror(errno));
  195. exit(1);
  196. }
  197. srand(CHECK_SEED);
  198. int adj = check_pass(inputs, weights);
  199. printf("[INFO] fail rate of untrained model is %f\n", adj / (SAMPLE_SIZE * 2.0));
  200. for (int i = 0; i < TRAIN_PASSES; ++i) {
  201. srand(TRAIN_SEED);
  202. int adj = train_pass(inputs, weights);
  203. printf("[INFO] Pass %d: adjusted %d times\n", i, adj);
  204. if (adj <= 0) break;
  205. }
  206. srand(CHECK_SEED);
  207. adj = check_pass(inputs, weights);
  208. printf("[INFO] fail rate of trained model is %f\n", adj / (SAMPLE_SIZE * 2.0));
  209. return 0;
  210. }