-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcli.cpp
More file actions
617 lines (513 loc) · 21.1 KB
/
cli.cpp
File metadata and controls
617 lines (513 loc) · 21.1 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
#include "util/math.h"
#include "util/string.h"
#include "visp/vision.h"
#include <algorithm>
#include <charconv>
#include <cstdio>
#include <filesystem>
#include <optional>
#include <string_view>
#include <vector>
namespace visp {
using std::filesystem::path;
enum class cli_command { none, sam, birefnet, depth_anything, migan, esrgan };
struct cli_args {
cli_command command = cli_command::none;
std::vector<char const*> inputs; // -i --input
char const* output = "output.png"; // -o --output
char const* model = nullptr; // -m --model
std::vector<char const*> prompt; // -p --prompt
// int threads = -1; // -t --threads
// bool verbose = false; // -v --verbose
std::optional<backend_type> bknd_type; // -b --backend
// std::string_view device = 0; // -d --device
// ggml_type float_type = GGML_TYPE_COUNT; // -f32 -f16
char const* composite = nullptr; // --composite
int tile_size = -1; // --tile
};
void print_usage() {
char const* const usage = R"(
Usage: vision-cli <command> [options]
Commands:
sam - MobileSAM image segmentation
birefnet - BirefNet background removal
depthany - Depth-Anything depth estimation
migan - MI-GAN inpainting
esrgan - ESRGAN/Real-ESRGAN upscaling
Options:
-i, --input <image1> [<image2> ...] Input image(s)
-o, --output <file> Output file (default: output.png)
-m, --model <file> Model file (.gguf)
-p, --prompt <x> [<y> ...] Prompt (eg. pixel coordinates)
-b, --backend <cpu|gpu> Backend type (default: auto)
-h, --help Print usage and exit
--composite <file> Composite input image with mask
--tile <size> Tile size to split large images
Examples:
vision-cli sam -m MobileSAM-F16.gguf -i image.jpg -p 100 200 -o mask.png
vision-cli birefnet -m BiRefNet-F16.gguf -i image.jpg -o mask.png --composite output.png
vision-cli migan -m MIGAN-F16.gguf -i image.jpg mask.png -o output.png
vision-cli esrgan -m ESRGAN-x4-F16.gguf -i image.jpg -o upscaled.png
)";
printf("%s", usage);
}
char const* const short_usage = R"(
Usage: vision-cli <command> [options]
See 'vision-cli --help' for more details.
)";
char const* next_arg(int argc, char** argv, int& i) {
if (++i < argc) {
return argv[i];
} else {
throw except("Missing argument after {}", argv[i - 1]);
}
}
std::vector<char const*> collect_args(int argc, char** argv, int& i, char delim = '-') {
std::vector<char const*> r;
do {
r.push_back(next_arg(argc, argv, i));
} while (i + 1 < argc && argv[i + 1][0] != delim);
if (r.empty()) {
throw except("Missing argument after {}", argv[i - 1]);
}
return r;
}
int parse_int(std::string_view arg) {
int value = 0;
auto [ptr, ec] = std::from_chars(arg.data(), arg.data() + arg.size(), value);
if (ec != std::errc()) {
throw except("Invalid integer argument: {}", arg);
}
return value;
}
char const* validate_path(char const* arg) {
if (!exists(path(arg))) {
throw except("File not found: {}", arg);
}
return arg;
}
void require_inputs(std::span<char const* const> inputs, int n_required, char const* names) {
if (inputs.size() != size_t(n_required)) {
throw except(
"Expected -i to be followed by {} inputs: {} - but found {}.", n_required, names,
inputs.size());
}
}
cli_args cli_parse(int argc, char** argv) {
cli_args r;
if (argc < 2) {
throw except("Missing command.\n{}", short_usage);
}
std::string_view arg1 = argv[1];
if (arg1 == "sam") {
r.command = cli_command::sam;
} else if (arg1 == "birefnet") {
r.command = cli_command::birefnet;
} else if (arg1 == "depthany" || arg1 == "depth-anything") {
r.command = cli_command::depth_anything;
} else if (arg1 == "migan") {
r.command = cli_command::migan;
} else if (arg1 == "esrgan") {
r.command = cli_command::esrgan;
} else if (arg1 == "-h" || arg1 == "--help") {
print_usage();
} else {
throw except("Unknown command: '{}'\n{}", arg1, short_usage);
}
for (int i = 2; i < argc; ++i) {
std::string_view arg = argv[i];
if (arg == "-i" || arg == "--input") {
r.inputs = collect_args(argc, argv, i);
for_each(r.inputs.begin(), r.inputs.end(), validate_path);
} else if (arg == "-o" || arg == "--output") {
r.output = next_arg(argc, argv, i);
} else if (arg == "-m" || arg == "--model") {
r.model = next_arg(argc, argv, i);
} else if (arg == "-p" || arg == "--prompt") {
r.prompt = collect_args(argc, argv, i, '-');
} else if (arg == "-b" || arg == "--backend") {
std::string_view backend_arg = next_arg(argc, argv, i);
if (backend_arg == "cpu") {
r.bknd_type = backend_type::cpu;
} else if (backend_arg == "gpu") {
r.bknd_type = backend_type::gpu;
} else {
throw except("Unknown backend type '{}', must be one of: cpu, gpu", backend_arg);
}
} else if (arg == "--composite") {
r.composite = next_arg(argc, argv, i);
} else if (arg == "--tile") {
r.tile_size = parse_int(next_arg(argc, argv, i));
} else if (arg.starts_with("-")) {
throw except("Unknown argument: {}\n{}", arg, short_usage);
}
}
return r;
}
void run_sam(cli_args const&);
void run_birefnet(cli_args const&);
void run_depth_anything(cli_args const&);
void run_migan(cli_args const&);
void run_esrgan(cli_args const&);
} // namespace visp
//
// main
int main(int argc, char** argv) {
using namespace visp;
try {
ggml_time_init();
cli_args args = cli_parse(argc, argv);
switch (args.command) {
case cli_command::sam: run_sam(args); break;
case cli_command::birefnet: run_birefnet(args); break;
case cli_command::depth_anything: run_depth_anything(args); break;
case cli_command::migan: run_migan(args); break;
case cli_command::esrgan: run_esrgan(args); break;
case cli_command::none: break;
}
} catch (std::exception const& e) {
printf("Error: %s\n", e.what());
return 1;
} catch (...) {
return -1;
}
return 0;
}
namespace visp {
struct timer {
int64_t start;
fixed_string<16> string;
timer() : start(ggml_time_us()) {}
int64_t elapsed() const { return ggml_time_us() - start; }
float elapsed_ms() const { return float(elapsed()) / 1000.0f; }
char const* elapsed_str() {
format(string, "{:.1f} ms", elapsed_ms());
return string.c_str();
}
};
//
// Common helpers
backend_device backend_init(cli_args const& args) {
timer t;
printf("Initializing backend... ");
backend_device b;
if (args.bknd_type) {
b = backend_init(*args.bknd_type);
} else {
b = backend_init();
}
printf("done (%s)\n", t.elapsed_str());
ggml_backend_dev_t dev = ggml_backend_get_device(b);
char const* dev_name = ggml_backend_dev_name(dev);
char const* dev_desc = ggml_backend_dev_description(dev);
printf("- device: %s - %s\n", dev_name, dev_desc);
return b;
}
char const* to_string(tensor_data_layout l) {
switch (l) {
case tensor_data_layout::cwhn: return "cwhn";
case tensor_data_layout::whcn: return "whcn";
default: return "unknown";
}
}
path find_model(char const* model_name_or_path) {
path p = path(model_name_or_path);
if (exists(p) || p.is_absolute()) {
return p;
}
path search_paths[5];
search_paths[0] = path("models");
if (char const* vision_model_dir = getenv("VISION_MODEL_DIR")) {
search_paths[1] = path(vision_model_dir);
}
if (char const* xdg_data_home = getenv("XDG_DATA_HOME")) {
search_paths[2] = path(xdg_data_home) / "visioncpp";
}
if (char const* home = getenv("HOME")) {
search_paths[3] = path(home) / ".local/share/visioncpp";
}
if constexpr (VISP_MODEL_INSTALL_DIR[0] != '\0') {
search_paths[4] = path(VISP_MODEL_INSTALL_DIR);
}
for (auto& sp : search_paths) {
if (!sp.empty()) {
path candidate = sp / p;
if (exists(candidate)) {
return candidate;
}
}
}
printf("Looking for %s\n", p.generic_string().c_str());
for (auto& sp : search_paths) {
if (!sp.empty()) {
printf("Looking for %s\n", (sp / p).generic_string().c_str());
}
}
throw except("Model file not found: {}", model_name_or_path);
}
std::tuple<model_file, model_weights> load_model_weights(
cli_args const& args,
backend_device const& dev,
char const* default_model,
int n_tensors = 0,
tensor_data_layout preferred_layout = tensor_data_layout::unknown) {
timer t;
path model_path = find_model(args.model ? args.model : default_model);
auto model_path_str = model_path.generic_string();
printf("Loading model weights from '%s'... ", model_path_str.c_str());
model_file file = model_load(model_path_str.c_str());
model_weights weights = model_init(file.n_tensors() + n_tensors);
if (preferred_layout == tensor_data_layout::unknown) {
preferred_layout = file.tensor_layout();
}
model_transfer(file, weights, dev, dev.preferred_float_type(), preferred_layout);
printf("done (%s)\n", t.elapsed_str());
ggml_type ftype = file.float_type();
if (ftype == GGML_TYPE_COUNT) {
ftype = weights.float_type();
}
printf("- float type: %s\n", ggml_type_name(ftype));
if (preferred_layout != tensor_data_layout::unknown) {
printf("- tensor layout: %s\n", to_string(preferred_layout));
}
return {std::move(file), std::move(weights)};
}
void print_model_flags(model_ref const& m) {
bool flash_attn = !!(m.flags & model_build_flag::flash_attention);
printf("- flash attention: %s\n", flash_attn ? "on" : "off");
}
void compute_timed(compute_graph const& g, backend_device const& b) {
timer t;
printf("Running inference... ");
compute(g, b);
printf("complete (%s)\n", t.elapsed_str());
}
void composite_image_with_mask(image_view image, image_view mask, char const* output_path) {
if (!output_path) {
return;
}
image_data image_f32_data;
if (!is_float(image.format)) {
image_f32_data = image_u8_to_f32(image, image_format::rgba_f32);
image = image_f32_data;
}
image_data mask_f32_data;
if (!is_float(mask.format)) {
mask_f32_data = image_u8_to_f32(mask, image_format::alpha_f32);
mask = mask_f32_data;
}
image_data foreground = image_estimate_foreground(image, mask);
image_data output = image_f32_to_u8(foreground, image_format::rgba_u8);
image_save(output, output_path);
printf("-> image composited and saved to %s\n", output_path);
}
//
// SAM
struct sam_prompt {
i32x2 point1 = {-1, -1};
i32x2 point2 = {-1, -1};
bool is_point() const { return point2[0] == -1 || point2[1] == -1; }
bool is_box() const { return !is_point(); }
};
sam_prompt sam_parse_prompt(std::span<char const* const> args, i32x2 extent) {
if (args.empty()) {
throw except(
"SAM requires a prompt with coordinates for a point or box"
"eg. '--prompt 100 200' to pick the point at pixel (x=100, y=200)");
}
if (args.size() < 2 || args.size() > 4) {
throw except(
"Invalid number of arguments for SAM prompt. Expected 2 (point) or 4 (box) numbers, "
"got {}",
args.size());
}
i32x2 a{-1, -1};
if (args.size() >= 2) {
a = {parse_int(args[0]), parse_int(args[1])};
if (a[0] < 0 || a[1] < 0 || a[0] >= extent[0] || a[1] >= extent[1]) {
throw except("Invalid image coordinates: ({}, {})", a[0], a[1]);
}
}
i32x2 b{-1, -1};
if (args.size() == 4) {
b = {parse_int(args[2]), parse_int(args[3])};
if (b[0] < 0 || b[1] < 0 || b[0] >= extent[0] || b[1] >= extent[1]) {
throw except("Invalid image coordinates: ({}, {})", b[0], b[1]);
}
if (a[0] >= b[0] || a[1] >= b[1]) {
throw except("Invalid box coordinates: ({}, {}) to ({}, {})", a[0], a[1], b[0], b[1]);
}
}
return sam_prompt{a, b};
};
void run_sam(cli_args const& args) {
backend_device backend = backend_init(args);
auto [file, weights] = load_model_weights(
args, backend, "MobileSAM-F16.gguf", 0, backend.preferred_layout());
sam_params params{};
require_inputs(args.inputs, 1, "<image>");
image_data image = image_load(args.inputs[0]);
image_data image_data_ = sam_process_input(image, params);
sam_prompt prompt = sam_parse_prompt(args.prompt, image.extent);
f32x4 prompt_data = prompt.is_point()
? sam_process_point(prompt.point1, image.extent, params)
: sam_process_box({prompt.point1, prompt.point2}, image.extent, params);
compute_graph graph = compute_graph_init();
model_ref m(weights, graph);
tensor image_tensor = compute_graph_input(m, GGML_TYPE_F32, {3, 1024, 1024, 1}, "image");
tensor point_tensor = compute_graph_input(m, GGML_TYPE_F32, {2, 2, 1, 1}, "points");
tensor image_embed = sam_encode_image(m, image_tensor, params);
tensor prompt_embed = prompt.is_point() ? sam_encode_points(m, point_tensor)
: sam_encode_box(m, point_tensor);
sam_prediction output = sam_predict_mask(m, image_embed, prompt_embed);
compute_graph_allocate(graph, backend);
transfer_to_backend(image_tensor, image_data_);
transfer_to_backend(point_tensor, span(prompt_data.v, 4));
compute_timed(graph, backend);
timer t_post;
printf("Postprocessing output... ");
tensor_data iou = transfer_from_backend(output.iou);
tensor_data mask_data = transfer_from_backend(output.masks);
image_data mask = sam_process_mask(mask_data.as_f32(), 2, image.extent, params);
printf("complete (%s)\n", t_post.elapsed_str());
image_save(mask, args.output);
auto ious = iou.as_f32();
printf("-> estimated accuracy (IoU): %f, %f, %f\n", ious[0], ious[1], ious[2]);
printf("-> mask saved to %s\n", args.output);
composite_image_with_mask(image, mask, args.composite);
}
//
// BirefNet
void run_birefnet(cli_args const& args) {
backend_device backend = backend_init(args);
auto [file, weights] = load_model_weights(
args, backend, "BiRefNet-lite-F16.gguf", 0, backend.preferred_layout());
require_inputs(args.inputs, 1, "<image>");
image_data image = image_load(args.inputs[0]);
birefnet_params params = birefnet_detect_params(file, image.extent, backend.max_alloc());
image_data input_data = birefnet_process_input(image, params);
i32x2 extent = params.image_extent;
char const* image_size_str = params.image_size < 0 ? " (dynamic)" : "";
printf("- model image size: %d%s\n", params.image_size, image_size_str);
printf("- inference image size: %dx%d\n", extent[0], extent[1]);
compute_graph graph = compute_graph_init(6 * 1024);
model_ref m(weights, graph);
print_model_flags(m);
birefnet_buffers buffers = birefnet_precompute(m, params);
tensor input = compute_graph_input(m, GGML_TYPE_F32, {3, extent[0], extent[1], 1});
tensor output = birefnet_predict(m, input, params);
compute_graph_allocate(graph, backend);
transfer_to_backend(input, input_data);
for (tensor_data const& buf : buffers) {
transfer_to_backend(buf);
}
compute_timed(graph, backend);
tensor_data mask_data = transfer_from_backend(output);
image_view mask_output(extent, mask_data.as_f32());
image_data mask_resized = image_scale(mask_output, image.extent);
image_data mask = image_f32_to_u8(mask_resized, image_format::alpha_u8);
image_save(mask, args.output);
printf("-> mask saved to %s\n", args.output);
composite_image_with_mask(image, mask_resized, args.composite);
}
//
// Depth Anything
void run_depth_anything(cli_args const& args) {
backend_device backend = backend_init(args);
auto [file, weights] = load_model_weights(
args, backend, "DepthAnythingV2-Small-F32.gguf", 0, backend.preferred_layout());
require_inputs(args.inputs, 1, "<image>");
image_data image = image_load(args.inputs[0]);
depthany_params params = depthany_detect_params(file, image.extent);
image_data input_data = depthany_process_input(image, params);
i32x2 extent = params.image_extent;
printf("- model image size: %d\n", params.image_size);
printf("- inference image size: %dx%d\n", params.image_extent[0], params.image_extent[1]);
compute_graph graph = compute_graph_init();
model_ref m(weights, graph);
print_model_flags(m);
tensor input = compute_graph_input(m, GGML_TYPE_F32, {3, extent[0], extent[1], 1});
tensor output = depthany_predict(m, input, params);
compute_graph_allocate(graph, backend);
transfer_to_backend(input, input_data);
compute_timed(graph, backend);
tensor_data output_data = transfer_from_backend(output);
image_data depth_raw = depthany_process_output(output_data.as_f32(), image.extent, params);
image_data depth_image = image_f32_to_u8(depth_raw, image_format::alpha_u8);
image_save(depth_image, args.output);
printf("-> depth image saved to %s\n", args.output);
}
//
// MI-GAN
void run_migan(cli_args const& args) {
backend_device backend = backend_init(args);
auto [file, weights] = load_model_weights(
args, backend, "MIGAN-512-places2-F16.gguf", backend.preferred_layout());
migan_params params = migan_detect_params(file);
params.invert_mask = true; // -> inpaint opaque areas
require_inputs(args.inputs, 2, "<image> <mask>");
image_data image = image_load(args.inputs[0]);
image_data mask = image_load(args.inputs[1]);
if (mask.format != image_format::alpha_u8) {
mask = image_to_mask(mask);
}
image_data input_data = migan_process_input(image, mask, params);
compute_graph graph = compute_graph_init();
model_ref m(weights, graph);
i64x4 input_shape = {4, params.resolution, params.resolution, 1};
tensor input = compute_graph_input(m, GGML_TYPE_F32, input_shape);
tensor output = migan_generate(m, input, params);
compute_graph_allocate(graph, backend);
transfer_to_backend(input, input_data);
compute_timed(graph, backend);
tensor_data output_data = transfer_from_backend(output);
image_data output_image = migan_process_output(output_data.as_f32(), image.extent, params);
image_data mask_resized = image_scale(mask, image.extent);
image_data composited = image_alpha_composite(output_image, image, mask_resized);
image_save(composited, args.output);
printf("-> output image saved to %s\n", args.output);
}
//
// ESRGAN
void run_esrgan(cli_args const& args) {
backend_device backend = backend_init(args);
auto [file, weights] = load_model_weights(
args, backend, "RealESRGAN-x4.gguf", 0, backend.preferred_layout());
esrgan_params params = esrgan_detect_params(file);
printf("- scale: %dx\n", params.scale);
printf("- block count: %d\n", params.n_blocks);
require_inputs(args.inputs, 1, "<image>");
image_data image = image_load(args.inputs[0]);
int tile_size = args.tile_size > 0 ? args.tile_size : 224;
tile_layout tiles = tile_layout(image.extent, tile_size, 16);
tile_layout tiles_out = tile_scale(tiles, params.scale);
image_data input_tile = image_alloc(tiles.tile_size, image_format::rgb_f32);
image_data output_tile = image_alloc(tiles_out.tile_size, image_format::rgb_f32);
image_data output_image = image_alloc(image.extent * params.scale, image_format::rgb_f32);
image_clear(output_image);
compute_graph graph = compute_graph_init(esrgan_estimate_graph_size(params));
model_ref m(weights, graph);
i64x4 input_shape = {3, tiles.tile_size[0], tiles.tile_size[1], 1};
tensor input = compute_graph_input(m, GGML_TYPE_F32, input_shape);
tensor output = esrgan_generate(m, input, params);
compute_graph_allocate(graph, backend);
timer total;
printf(
"Using tile size %d with %d overlap -> %dx%d tiles\n", //
tile_size, tiles.overlap[0], tiles.n_tiles[0], tiles.n_tiles[1]);
for (int t = 0; t < tiles.total(); ++t) {
printf("\rRunning inference... tile %d of %d", t + 1, tiles.total());
i32x2 tile_coord = tiles.coord(t);
i32x2 tile_offset = tiles.start(tile_coord);
image_u8_to_f32(image, input_tile, f32x4(0), f32x4(1), tile_offset);
transfer_to_backend(input, input_tile);
compute(graph, backend);
transfer_from_backend(output, output_tile);
tile_merge(output_tile, output_image, tile_coord, tiles_out);
}
printf("\rRunning inference... complete (%s)\n", total.elapsed_str());
image_data output_u8 = image_f32_to_u8(output_image, image_format::rgba_u8);
image_save(output_u8, args.output);
printf("-> output image saved to %s\n", args.output);
}
} // namespace visp