forked from tsoding/nn.h
-
Notifications
You must be signed in to change notification settings - Fork 0
/
img2nn.c
206 lines (172 loc) · 6.47 KB
/
img2nn.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <assert.h>
#include <stdio.h>
#include <float.h>
#include <raylib.h>
#include "stb_image.h"
#include "stb_image_write.h"
#define NN_IMPLEMENTATION
#define NN_ENABLE_GYM
#include "nn.h"
char *args_shift(int *argc, char ***argv)
{
assert(*argc > 0);
char *result = **argv;
(*argc) -= 1;
(*argv) += 1;
return result;
}
int main(int argc, char **argv)
{
const char *program = args_shift(&argc, &argv);
if (argc <= 0) {
fprintf(stderr, "Usage: %s <input>\n", program);
fprintf(stderr, "ERROR: no input file is provided\n");
return 1;
}
const char *img_file_path = args_shift(&argc, &argv);
int img_width, img_height, img_comp;
uint8_t *img_pixels = (uint8_t *)stbi_load(img_file_path, &img_width, &img_height, &img_comp, 0);
if (img_pixels == NULL) {
fprintf(stderr, "ERROR: could not read image %s\n", img_file_path);
return 1;
}
if (img_comp != 1) {
fprintf(stderr, "ERROR: %s is %d bits image. Only 8 bit grayscale images are supported\n", img_file_path, img_comp*8);
return 1;
}
printf("%s size %dx%d %d bits\n", img_file_path, img_width, img_height, img_comp*8);
Mat t = mat_alloc(img_width*img_height, 3);
for (int y = 0; y < img_height; ++y) {
for (int x = 0; x < img_width; ++x) {
size_t i = y*img_width + x;
MAT_AT(t, i, 0) = (float)x/(img_width - 1);
MAT_AT(t, i, 1) = (float)y/(img_height - 1);
MAT_AT(t, i, 2) = img_pixels[i]/255.f;
}
}
Mat ti = {
.rows = t.rows,
.cols = 2,
.stride = t.stride,
.es = &MAT_AT(t, 0, 0),
};
Mat to = {
.rows = t.rows,
.cols = 1,
.stride = t.stride,
.es = &MAT_AT(t, 0, ti.cols),
};
// MAT_PRINT(ti);
// MAT_PRINT(to);
size_t arch[] = {2, 7, 7, 1};
NN nn = nn_alloc(arch, ARRAY_LEN(arch));
NN g = nn_alloc(arch, ARRAY_LEN(arch));
nn_rand(nn, -1, 1);
size_t WINDOW_FACTOR = 80;
size_t WINDOW_WIDTH = (16*WINDOW_FACTOR);
size_t WINDOW_HEIGHT = (9*WINDOW_FACTOR);
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "gym");
SetTargetFPS(60);
Plot plot = {0};
Image preview_image = GenImageColor(img_width, img_height, BLACK);
Texture2D preview_texture = LoadTextureFromImage(preview_image);
Image original_image = GenImageColor(img_width, img_height, BLACK);
for (size_t y = 0; y < (size_t) img_height; ++y) {
for (size_t x = 0; x < (size_t) img_width; ++x) {
uint8_t pixel = img_pixels[y*img_width + x];
ImageDrawPixel(&original_image, x, y, CLITERAL(Color) { pixel, pixel, pixel, 255 });
}
}
Texture2D original_texture = LoadTextureFromImage(original_image);
size_t epoch = 0;
size_t max_epoch = 100*1000;
size_t epochs_per_frame = 103;
float rate = 1.0f;
bool paused = true;
while (!WindowShouldClose()) {
if (IsKeyPressed(KEY_SPACE)) {
paused = !paused;
}
if (IsKeyPressed(KEY_R)) {
epoch = 0;
nn_rand(nn, -1, 1);
plot.count = 0;
}
for (size_t i = 0; i < epochs_per_frame && !paused && epoch < max_epoch; ++i) {
nn_backprop(nn, g, ti, to);
nn_learn(nn, g, rate);
epoch += 1;
da_append(&plot, nn_cost(nn, ti, to));
}
BeginDrawing();
Color background_color = {0x18, 0x18, 0x18, 0xFF};
ClearBackground(background_color);
{
int w = GetRenderWidth();
int h = GetRenderHeight();
int rw = w/3;
int rh = h*2/3;
int rx = 0;
int ry = h/2 - rh/2;
gym_plot(plot, rx, ry, rw, rh);
rx += rw;
gym_render_nn(nn, rx, ry, rw, rh);
rx += rw;
float scale = 10;
for (size_t y = 0; y < (size_t) img_height; ++y) {
for (size_t x = 0; x < (size_t) img_width; ++x) {
MAT_AT(NN_INPUT(nn), 0, 0) = (float)x/(img_width - 1);
MAT_AT(NN_INPUT(nn), 0, 1) = (float)y/(img_height - 1);
nn_forward(nn);
uint8_t pixel = MAT_AT(NN_OUTPUT(nn), 0, 0)*255.f;
ImageDrawPixel(&preview_image, x, y, CLITERAL(Color) { pixel, pixel, pixel, 255 });
}
}
UpdateTexture(preview_texture, preview_image.data);
DrawTextureEx(preview_texture, CLITERAL(Vector2) { rx, ry }, 0, scale, WHITE);
DrawTextureEx(original_texture, CLITERAL(Vector2) { rx, ry + img_height*scale }, 0, scale, WHITE);
char buffer[256];
snprintf(buffer, sizeof(buffer), "Epoch: %zu/%zu, Rate: %f, Cost: %f", epoch, max_epoch, rate, nn_cost(nn, ti, to));
DrawText(buffer, 0, 0, h*0.04, WHITE);
}
EndDrawing();
}
for (size_t y = 0; y < (size_t) img_height; ++y) {
for (size_t x = 0; x < (size_t) img_width; ++x) {
uint8_t pixel = img_pixels[y*img_width + x];
if (pixel) printf("%3u ", pixel); else printf(" ");
}
printf("\n");
}
for (size_t y = 0; y < (size_t) img_height; ++y) {
for (size_t x = 0; x < (size_t) img_width; ++x) {
MAT_AT(NN_INPUT(nn), 0, 0) = (float)x/(img_width - 1);
MAT_AT(NN_INPUT(nn), 0, 1) = (float)y/(img_height - 1);
nn_forward(nn);
uint8_t pixel = MAT_AT(NN_OUTPUT(nn), 0, 0)*255.f;
if (pixel) printf("%3u ", pixel); else printf(" ");
}
printf("\n");
}
size_t out_width = 512;
size_t out_height = 512;
uint8_t *out_pixels = malloc(sizeof(*out_pixels)*out_width*out_height);
assert(out_pixels != NULL);
for (size_t y = 0; y < out_height; ++y) {
for (size_t x = 0; x < out_width; ++x) {
MAT_AT(NN_INPUT(nn), 0, 0) = (float)x/(out_width - 1);
MAT_AT(NN_INPUT(nn), 0, 1) = (float)y/(out_height - 1);
nn_forward(nn);
uint8_t pixel = MAT_AT(NN_OUTPUT(nn), 0, 0)*255.f;
out_pixels[y*out_width + x] = pixel;
}
}
const char *out_file_path = "upscaled.png";
if (!stbi_write_png(out_file_path, out_width, out_height, 1, out_pixels, out_width*sizeof(*out_pixels))) {
fprintf(stderr, "ERROR: could not save image %s\n", out_file_path);
return 1;
}
printf("Generated %s from %s\n", out_file_path, img_file_path);
return 0;
}