Skip to content

Instantly share code, notes, and snippets.

@PaulaRudy
Last active March 30, 2016 18:35
Show Gist options
  • Select an option

  • Save PaulaRudy/85a3a61f999f4296b25b to your computer and use it in GitHub Desktop.

Select an option

Save PaulaRudy/85a3a61f999f4296b25b to your computer and use it in GitHub Desktop.
Bowler wAIlnut Noise Invariance Test
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.InvalidRemoteException;
import org.eclipse.jgit.api.errors.TransportException;
import com.neuronrobotics.bowlerstudio.scripting.ScriptingEngine;
import com.neuronrobotics.imageprovider.AbstractImageProvider;
import com.neuronrobotics.sdk.common.BowlerAbstractDevice;
import model.MARK_II.connectTypes.AbstractSensorCellsToRegionConnect;
import model.MARK_II.connectTypes.SensorCellsToRegionRectangleConnect;
import model.MARK_II.generalAlgorithm.ColumnPosition;
import model.MARK_II.generalAlgorithm.SpatialPooler;
import model.MARK_II.region.Region;
import model.MARK_II.sensory.Retina;
/**
* -------------------------------Purpose---------------------------------------
* To show the spatial pooling learning algorithm is good at producing the same
* output of neural activity even when the input is very noisy.
*
* ------------------------------Experiment-------------------------------------
* Run the spatial pooling algorithm on 3 different bitmap images. The 3 images
* are both of the same thing but 1 of the images has no noise, 1 image has some
* noise, and 1 image has a lot of noise.
*
* ------------------------------Conclusion-------------------------------------
* The spatial pooling algorithm does simple local computations on it's input to
* remove noise very efficiently up to a specific threshold that can vary
* between locations in the input.
*
* @author Quinn Liu ([email protected]), edited by Paula Rudy (Github: PaulaRudy)
* to work with BowlerStudio (http://neuronrobotics.com)
*/
File two = null,twoSomeNoise = null,twoLotsOfNoise = null;
try {
ScriptingEngine.setAutoupdate(true);
two = ScriptingEngine
.fileFromGit(
"https://github.com/PaulaRudy/MQPResources.git",//git repo URL
"master",//branch
"2.bmp"// File from within the Git repo
);
twoSomeNoise = ScriptingEngine
.fileFromGit(
"https://github.com/PaulaRudy/MQPResources.git",//git repo URL
"master",//branch
"2_with_some_noise.bmp"// File from within the Git repo
);
twoLotsOfNoise = ScriptingEngine
.fileFromGit(
"https://github.com/PaulaRudy/MQPResources.git",//git repo URL
"master",//branch
"2_with_a_lot_of_noise.bmp"// File from within the Git repo
);
} catch (InvalidRemoteException e1) {
e1.printStackTrace();
} catch (TransportException e1) {
e1.printStackTrace();
} catch (GitAPIException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
SpatialPooler spatialPooler;
//Creates a retina (input layer) of size 66 by 66 pixels.
Retina retina = new Retina(66, 66);
/* public Region(String biologicalName, int numberOfColumnsAlongRowsDimension,
int numberOfColumnsAlongColumnsDimension, int cellsPerColumn,
double percentMinimumOverlapScore, int desiredLocalActivity)
Where:
Region size (in simulated neurons) = (columns * rows * depth)
->columns = "numberOfColumnsAlongColumnsDimension"
->rows = "numberOfColumnsAlongRowsDimension"
->depth = "cellsPerColumn"
"percentMinimumOverlapScore" =
threshold (in percentage) over which (>=) the number of connected neurons that are active
will result in the connected neuron of the next layer being activated.
IE if neuron A is connected to neurons 1-10, and percentMinimumOverlapScore = 0.5,
then neuron A is activated if 5 or more of neurons 1-10 are activated.
"desiredLocalActivity" = ????stride???
*/
Region region = new Region("Region name", 8, 8, 1, 65, 2);
//Dimension (width = height, depth = max depth of region layer?) of neurons in one layer connected to a single neuron in the next layer
region.setInhibitionRadius(3);
AbstractSensorCellsToRegionConnect retinaToRegion = new SensorCellsToRegionRectangleConnect();
retinaToRegion.connect(retina.getVisionCells(), region.getColumns(), 0, 0);
spatialPooler = new SpatialPooler(region);
spatialPooler.setLearningState(true);
// create columns that should be in the sets
ColumnPosition cp1 = new ColumnPosition(6, 5);
ColumnPosition cp2 = new ColumnPosition(6, 2);
ColumnPosition cp3 = new ColumnPosition(2, 5);
ColumnPosition cp4 = new ColumnPosition(1, 5);
// create a set to test against the first two images
Set<ColumnPosition> set1 = new HashSet<ColumnPosition>();
set1.add(cp1);
set1.add(cp2);
set1.add(cp3);
set1.add(cp4);
// set to use for the final image
Set<ColumnPosition> set2 = new HashSet<ColumnPosition>();
set2.add(cp3);
set2.add(cp4);
// View all three images of digit 2 @ https://github.com/WalnutiQ/WalnutiQ#noise-invariance-experiment
try {
retina.seeBMPImage(two);
} catch (IOException e) {
e.printStackTrace();
}
spatialPooler.performPooling();
// set1 = ((6, 5), (6, 2), (2, 5), (1, 5))
System.out.println(set1.equals(spatialPooler.getActiveColumnPositions()));
try {
retina.seeBMPImage(twoSomeNoise);
} catch (IOException e) {
e.printStackTrace();
}
spatialPooler.performPooling();
// set1 = ((6, 5), (6, 2), (2, 5), (1, 5))
System.out.println(set1.equals(spatialPooler.getActiveColumnPositions()));
try {
retina.seeBMPImage(twoLotsOfNoise);
} catch (IOException e) {
e.printStackTrace();
}
spatialPooler.performPooling();
// when there is a lot of noise notice how the active columns are no longer the same?
// set2 = ((2, 5), (1, 5))
System.out.println(set2.equals(spatialPooler.getActiveColumnPositions()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment