Skip to content

talis-fb/KMeans-Image-Segmentation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Concurrent K-Means Image Segmentation in Rust and Java

Technologies

This repository is part of a comparative study on concurrent programming strategies for K-Means clustering applied to image segmentation. The study explores and contrasts different concurrency models and techniques in Rust and Java, analyzing their performance, synchronization mechanisms, and runtime characteristics.

Overview

This project implements various K-Means clustering algorithms for image segmentation in both Rust and Java, focusing on different parallelization strategies. The goal is to evaluate the efficiency and scalability of each approach when applied to image processing and study purposes.

It's used the Image to CSV CLI project to convert an image to CSV, process it with kmeans for segmentation, and convert the result back to an image.

Image Segmentations with Different K Values

To get a concrete example of result of this program...

Base Image
KImage
2
5
10
25
60

Strategies and Tools Used

Java Implementation

In the Java implementation, multiple strategies were employed to handle concurrency, including:

  • Thread Synchronization Techniques: Various approaches such as locks, synchronized blocks, and atomic operations were explored.

  • Virtual Threads vs. Native Threads: Performance was compared between traditional Java threads and virtual threads introduced in newer Java versions.

  • Performance Profiling and Benchmarking:

    • JMH (Java Microbenchmark Harness): Used to benchmark different concurrency approaches.

    • JMC (Java Mission Control): Employed for detailed profiling and runtime analysis.

    • Java Profiling Tools: Used to inspect thread behavior, contention, and memory usage.

Rust Implementation

In the Rust implementation, different concurrency models were tested:

  • Tokio Runtime: An asynchronous runtime for handling concurrent tasks.

  • Rayon: A data parallelism library optimized for multi-threading.

  • Rust Standard Library (std::thread): A more traditional multi-threading approach.

Each runtime was analyzed based on execution speed, CPU utilization, and memory consumption.

Example Workflow

As the tool uses STDIN and STDOUT for communication, you can use pipes and redirection to integrate it with img-to-csv.

This example processes an input_image.jpg image file and creates another image file called output_final_image.png, applying KMeans image segmentation with K equals 5:

img-to-csv to-csv input_image.jpg | kmeans -K 5 -m parallel | img-to-csv to-image -o output_final_image.png

Workflow step-by-step

  1. Convert image to CSV:
img-to-csv to-csv input_image.jpg > image.csv
  1. Apply KMeans clustering:
kmeans -K 5 -m parallel < image.csv > segmented_image.csv
  1. Convert CSV back to image:
img-to-csv to-image -o output_image.jpg < segmented_image.csv

How It Works

The tool processes CSV files where each line represents a pixel's coordinates (X and Y) and RGB values:

X:Y R G B
  • Input: CSV format from STDIN.
  • Output: CSV format to STDOUT with modified RGB values representing cluster centers.

Benchmarks

You can check the complete study HERE.

Language Implementation Threading Mode Milliseconds
Technologies Adder Platform 46
Technologies Rayon (parallel iterators) Platform 48
Technologies Adder Confinement Platform 51
Technologies Join (STD) Native OS 215.15
Technologies ParallelStream - 243
Technologies Join (Tokio) Green Threads 333.45
Technologies Volatile Virtual 384.998
Technologies Lock And
Semaphore
Virtual 392
Technologies Lock Virtual 395
Technologies Volatile Platform 396
Technologies Join Platform 397
Technologies Lock Platform 399
Technologies Lock And
Semaphore
Platform 405
Technologies Semaphore Virtual 406
Technologies Semaphore Platform 408
Technologies Join Virtual 419
Technologies Mutex Native OS + 900

About

A comparative study on concurrent programming for the K-Means algorithm applied to image segmentation in Rust and Java

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors