Self-Organizing Feature Map

Java implementation of self-organizing feature map (Kohonen map)

License

License

MIT
Categories

Categories

Java Languages
GroupId

GroupId

com.github.chen0040
ArtifactId

ArtifactId

java-som
Last Version

Last Version

1.0.2
Release Date

Release Date

Type

Type

jar
Description

Description

Self-Organizing Feature Map
Java implementation of self-organizing feature map (Kohonen map)
Project URL

Project URL

https://github.com/chen0040/java-som
Source Code Management

Source Code Management

https://github.com/chen0040/java-som

Download java-som

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.chen0040/java-som/ -->
<dependency>
    <groupId>com.github.chen0040</groupId>
    <artifactId>java-som</artifactId>
    <version>1.0.2</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.chen0040/java-som/
implementation 'com.github.chen0040:java-som:1.0.2'
// https://jarcasting.com/artifacts/com.github.chen0040/java-som/
implementation ("com.github.chen0040:java-som:1.0.2")
'com.github.chen0040:java-som:jar:1.0.2'
<dependency org="com.github.chen0040" name="java-som" rev="1.0.2">
  <artifact name="java-som" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.chen0040', module='java-som', version='1.0.2')
)
libraryDependencies += "com.github.chen0040" % "java-som" % "1.0.2"
[com.github.chen0040/java-som "1.0.2"]

Dependencies

compile (4)

Group / Artifact Type Version
org.slf4j : slf4j-api jar 1.7.20
org.slf4j : slf4j-log4j12 jar 1.7.20
com.github.chen0040 : java-data-frame jar 1.0.9
com.github.chen0040 : java-data-image jar 1.0.1

provided (1)

Group / Artifact Type Version
org.projectlombok : lombok jar 1.16.6

test (10)

Group / Artifact Type Version
org.testng : testng jar 6.9.10
org.hamcrest : hamcrest-core jar 1.3
org.hamcrest : hamcrest-library jar 1.3
org.assertj : assertj-core jar 3.5.2
org.powermock : powermock-core jar 1.6.5
org.powermock : powermock-api-mockito jar 1.6.5
org.powermock : powermock-module-junit4 jar 1.6.5
org.powermock : powermock-module-testng jar 1.6.5
org.mockito : mockito-core jar 2.0.2-beta
org.mockito : mockito-all jar 2.0.2-beta

Project Modules

There are no modules declared in this project.

java-som

Package provides java implementation of self-organizing feature map (Kohonen map)

Build Status Coverage Status

Install

Add the following dependency to your POM file:

<dependency>
  <groupId>com.github.chen0040</groupId>
  <artifactId>java-som</artifactId>
  <version>1.0.2</version>
</dependency>

Usage

Spatial clustering using SOFM

The sample code below shows how to cluster a set of 2-D points (c1, c2) in space using SOFM:

DataQuery.DataFrameQueryBuilder schema = DataQuery.blank()
      .newInput("c1")
      .newInput("c2")
      .newOutput("designed")
      .end();

Sampler.DataSampleBuilder negativeSampler = new Sampler()
      .forColumn("c1").generate((name, index) -> randn() * 0.3 + (index % 2 == 0 ? 2 : 4))
      .forColumn("c2").generate((name, index) -> randn() * 0.3 + (index % 2 == 0 ? 2 : 4))
      .forColumn("designed").generate((name, index) -> 0.0)
      .end();

Sampler.DataSampleBuilder positiveSampler = new Sampler()
      .forColumn("c1").generate((name, index) -> rand(-4, -2))
      .forColumn("c2").generate((name, index) -> rand(-2, -4))
      .forColumn("designed").generate((name, index) -> 1.0)
      .end();

DataFrame data = schema.build();

data = negativeSampler.sample(data, 50);
data = positiveSampler.sample(data, 50);

System.out.println(data.head(10));

SOFM algorithm = new SOFM();
// create a 1x2 SOM grid for 2-clusters
algorithm.setColumnCount(2);
algorithm.setRowCount(1);

DataFrame learnedData = algorithm.fitAndTransform(data);

for(int i = 0; i < learnedData.rowCount(); ++i){
 DataRow tuple = learnedData.row(i);
 String clusterId = tuple.getCategoricalTargetCell("cluster");
 System.out.println("learned: " + clusterId +"\tknown: "+tuple.target());
}

Image Segmentation (Clustering) using SOFM

The following sample code shows how to use SOFM to perform image segmentation:

BufferedImage img= ImageIO.read(FileUtils.getResource("1.jpg"));

DataFrame dataFrame = ImageDataFrameFactory.dataFrame(img);

SOFM cluster = new SOFM();
cluster.fit(dataFrame);

List<Integer> classColors = new ArrayList<Integer>();
for(int i=0; i < 5; ++i){
 for(int j=0; j < 5; ++j){
    classColors.add(ImageDataFrameFactory.get_rgb(255, rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)));
 }
}

BufferedImage segmented_image = new BufferedImage(img.getWidth(), img.getHeight(), img.getType());
for(int x=0; x < img.getWidth(); x++)
{
 for(int y=0; y < img.getHeight(); y++)
 {
    int rgb = img.getRGB(x, y);

    DataRow tuple = ImageDataFrameFactory.getPixelTuple(dataFrame, rgb);

    int clusterIndex = cluster.transform(tuple);

    rgb = classColors.get(clusterIndex % classColors.size());

    segmented_image.setRGB(x, y, rgb);
 }
}

Versions

Version
1.0.2
1.0.1