-
Notifications
You must be signed in to change notification settings - Fork 548
Update k-means example to show graphical result #2521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update k-means example to show graphical result #2521
Conversation
929e63d
to
c10cdbf
Compare
c10cdbf
to
9c9fae5
Compare
Thanks for reviving this good example 👍 Perhaps, we should use a smaller image size so that the example runs relatively fast on lower level NVIDIA/AMD GPUs. I have tried few images from our examples-images-assets, diff --git a/examples/machine_learning/kmeans.cpp b/examples/machine_learning/kmeans.cpp
index 81f1503a..e3a80018 100644
--- a/examples/machine_learning/kmeans.cpp
+++ b/examples/machine_learning/kmeans.cpp
@@ -113,7 +113,7 @@ int kmeans_demo(int k, bool console) {
printf("** ArrayFire K-Means Demo (k = %d) **\n\n", k);
array img =
- loadImage(ASSETS_DIR "/examples/images/vegetable-woman.jpg", true) /
+ loadImage(ASSETS_DIR "/examples/images/spider.jpg", false) /
255; // [0-255]
int w = img.dims(0), h = img.dims(1), c = img.dims(2);
@@ -133,16 +133,16 @@ int kmeans_demo(int k, bool console) {
array out_half = moddims(means_half(span, clusters_half, span), img.dims());
array out_dbl = moddims(means_dbl (span, clusters_dbl , span), img.dims());
- af::Window wnd("ArrayFire K-Means Demo");
- wnd.grid(1, 4);
+ af::Window wnd(1024, 1024, "ArrayFire K-Means Demo");
+ wnd.grid(2, 2);
std::string out_full_caption = "k = " + std::to_string(k);
std::string out_half_caption = "k = " + std::to_string(k / 2);
std::string out_dbl_caption = "k = " + std::to_string(k * 2);
while (!wnd.close()) {
wnd(0, 0).image(img, "Input Image");
wnd(0, 1).image(out_full, out_full_caption.c_str());
- wnd(0, 2).image(out_half, out_half_caption.c_str());
- wnd(0, 3).image(out_dbl, out_dbl_caption.c_str());
+ wnd(1, 0).image(out_half, out_half_caption.c_str());
+ wnd(1, 1).image(out_dbl, out_dbl_caption.c_str());
wnd.show();
}
`` |
@9prady9 Thanks. I tried the spider example, and it looks like it's being processed faster indeed on my laptop, I assume largely because it is monochrome. I also think changing the grid layout to 2x2 is good, but I think it's better to have the window |
ca48a0f
to
cfb7c55
Compare
Yes, it is three times less data in a monochrome image. Sure, you can reduce it to 800x800 and cluster parameter to 8. The reason I suggested smaller image is since CUDA/OpenCL takes so long, on CPU backend the example will seem like it is running forever. |
The k-means example has a non-working code stub that looks like it was supposed to be updated once arrayfire had the functionality to show windows. This change simply converts that code stub into a working implementation.
Here is a screenshot of the result (for k = 16, which is the example's default setting):
