xxxxxxxxxx
153
let brain = [];
let width = 400;
let height = 400;
let t = 0;
let ab = [];
//Creating a training set consisting of the first five letters of the latin alphabet
let training = [];
let input = [];
//let b = 0.1; //Bias
let n = 25; //Number of pixels
//let N = 0; //Training iterations
let η = 0.05; //Learning rate
let px = 50;
let py = 50;
let r = [0.3, 0.59, 0.11];
let c;
let N=14
function preload() {
for (let q = 0; q < N;q++) {
ab.push(loadImage("3a/" + q + ".png"));
}
}
function setup() {
createCanvas(width, height);
for (let i = 0; i < N; i++) {
training.push(pixelate(ab[i]));
}
//print(training)
for (let i = 0; i < N; i++) {
let temp = [];
for (let j = 0; j < px * py; j++) {
temp.push(1 / px / py);
}
brain.push(temp);
}
seeB = createButton("See");
seeB.mousePressed(see);
learnB = createButton("Learn")
learnB.mousePressed(learn)
studyB = createButton("Study")
studyB.mousePressed(study)
focusB=createButton("Focus")
focusB.mousePressed(focus)
resizeCanvas(2 * width, height + 100);
}
function draw() {}
//This trains the machine and adjusts the brain on a random training image
function learn() {
//Here I create a new random training image
//t = random([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]);
//print(output)
let output = think(training[t]);
//Here I adjust the brain according to the output and the expected output(training)
for (let i = 0; i < brain.length; i++) {
for (let j = 0; j < brain[i].length; j++) {
brain[i][j] +=
(int(t == i) - output[i]) *
output[i] *
(1 - output[i]) *
training[t][j] *
η;
}
}
}
function study(){
for(let i=0;i<1000;i++){
t = random([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]);
learn()
}
}
function focus(){
for(let i=0;i<1000;i++){
learn()
}
}
function see(a) {
background(0);
t = random([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]);
//This creates the drawn image
for (let i = 0; i < px * py; i++) {
fill(training[t][i] * 255);
rect(
((i % px) * width) / px,
(floor(i / px) * height) / py,
width / px,
height / py
);
}
tint(255, 255);
image(ab[t], width, 0, width, height);
//This draws the machine's guess
for (let i = 0; i < N; i++) {
tint(255, think(training[t])[i]*255);
image(ab[i], (i * 2 * width) / 14, height, (2 * width) / 14, 100);
}
}
function think(a) {
return matMul(brain, a);
}
//This is the scalar product to calculate each pixel of the output picture
function dot(v, w) {
let S = 0;
for (let i = 0; i < v.length; i++) {
S += v[i] * w[i];
}
return S;
}
//This performs all the scalar products, takes the sigmoid on the result and produces the total image
function matMul(A, x) {
let S = [];
for (let i = 0; i < A.length; i++) {
S.push(sigmoid(dot(A[i], x)));
}
return S;
}
//The sigmoid function makes sure our values are between 0 and 1
function sigmoid(x) {
return 1 / (1 + exp(-x));
}
function pixelate(a) {
let ret = [];
image(a, 0, 0, width, height);
loadPixels();
n = pixels.length / 4;
for (let j = 0; j < py; j++) {
for (let i = 0; i < px; i++) {
let tgrb = 0;
for (let k = 0; k < width / px; k++) {
for (let m = 0; m < height / py; m++) {
for (let l = 0; l < 3; l++) {
tgrb +=
r[l] *
pixels[
4 * ((i * width) / px + k + width * ((height / py) * j + m)) + l
];
}
}
}
ret.push(((tgrb / n) * px * py) / 255);
}
}
return ret;
}
//Clicking a pixel on the left flips that pixel to the opposite colour
/*function mousePressed() {
input[floor((10 * mouseX) / width) + 5 * floor((5 * mouseY) / height)] = int(
!input[floor((10 * mouseX) / width) + 5 * floor((5 * mouseY) / height)]
);
print(input[floor(width / mouseX / 10) + 5 * floor(height / mouseY / 5)]);
}*/