Skip to content

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

Merged
merged 4 commits into from
May 31, 2019

Conversation

mark-poscablo
Copy link
Contributor

@mark-poscablo mark-poscablo commented May 23, 2019

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):
image

@mark-poscablo mark-poscablo force-pushed the update-kmeans-example branch from 929e63d to c10cdbf Compare May 23, 2019 23:37
@mark-poscablo mark-poscablo force-pushed the update-kmeans-example branch from c10cdbf to 9c9fae5 Compare May 24, 2019 01:22
@9prady9
Copy link
Member

9prady9 commented May 24, 2019

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, spider.jpg(512x512) does perform faster and the banding that appears in the visual display after clustering is more evident with this image. I have changed the layout to 2x2 grid with 1024x1024 as window size to have better image display. Checkout the spider output with following changes and let me know.

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();
         }
 
 ``

@mark-poscablo
Copy link
Contributor Author

mark-poscablo commented May 24, 2019

@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 auto-sized as 800x800 instead because 1024x1024 fills pretty much the whole vertical space on common full HD screens (1920x1080). Lastly I think reducing k from 16 to 8 is good in terms of processing time and just being able to make the banding more obvious.

image

@mark-poscablo mark-poscablo force-pushed the update-kmeans-example branch from ca48a0f to cfb7c55 Compare May 24, 2019 17:27
@9prady9
Copy link
Member

9prady9 commented May 26, 2019

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.

@9prady9 9prady9 merged commit 974a8a3 into arrayfire:master May 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants