Skip to content
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

gitignore convnet output files #94

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/convnet*
64 changes: 64 additions & 0 deletions demo/getting-started/classifier.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<!doctype html>
<html>
<head>
<title>
Hello wurl
</title>
<body>
<script src="../../build/convnet-min.js"></script>
<script>
const layer_defs = []

//input layer of size 1x1x2 (all volumes are 3d)
layer_defs.push({
type: 'input',
out_sx: 1,
out_sy: 1,
out_depth: 2
})
//some fully connected layers
layer_defs.push({
type: 'fc',
num_neurons: 20,
activation: 'relu'
})
layer_defs.push({
type: 'fc',
num_neurons: 20,
activation: 'relu'
})
//a softmax classifier predicting probabilities for two classes: 0,1
layer_defs.push({
type: 'softmax',
num_classes: 2
})

//create a net out of it
const net = new convnetjs.Net()
net.makeLayers(layer_defs)

//the network always works on Vol() elements
//these are essentially simple wrappers around lists
//but also contain gradients and dimensions
//line below will create a 1x1x2 volume and fill it with 0.5 and -1.3
const x = new convnetjs.Vol([0.5, -1.3])
const probability_volume = net.forward(x)

console.log(probability_volume.w)
const trainer =
new convnetjs.Trainer(net, {
learning_rate: 0.01,
l2_decay: 0.001
})
trainer.train(x, 0)
for(var i = 0; i < 10; i++) {
var y = Object.assign({}, x)
trainer.train(y, 0)
}
const probability_volume2
= net.forward(x)
console.log(probability_volume2.w)
</script>
</body>
</head>
</html>
50 changes: 50 additions & 0 deletions demo/getting-started/regression.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!doctype html>
<html>
<head>
<title>
Hello wurl
</title>
<body>
<script src="../../build/convnet-min.js"></script>
<script>
//for regression as opposed to classification
//we can proceed similarly just replace the classifier
//layer from softmax
const layer_defs = []
layer_defs.push({
type: 'input',
out_sx: 1,
out_sy: 1,
out_depth: 2
})
layer_defs.push({
type: 'fc',
num_neurons: 5,
activation: 'sigmoid'
})
layer_defs.push({
type: 'regression',
num_neurons: 1
})

const net = new convnetjs.Net()
net.makeLayers(layer_defs)
const x = new convnetjs.Vol([0.5, -1.3])
const trainer = new convnetjs.SGDTrainer(net, {
learning_rate: 0.01,
momentum: 0.0,
batch_size: 1,
l2_decay: 0.001
})
//train on the datapoint, saying [0.5, -1.3]
//should map to value 0.7
for (var i = 0; i < 200; i++) {
trainer.train(Object.assign({}, x), [0.7])
}

const predicted_values = net.forward(x)
console.log(predicted_values.w)
</script>
</body>
</head>
</html>
84 changes: 46 additions & 38 deletions demo/js/image-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,60 @@ var loadFile = function(event) {
var reader = new FileReader();
reader.onload = function(){
var preview = document.getElementById('preview_img');
preview.src = centerCrop(reader.result);
preview.src = resize(preview.src);
centerCrop(reader.result)
.then(resize)
.then(function (src) {
preview.src = src;
});
};
reader.readAsDataURL(event.target.files[0]);
};

function centerCrop(src){
return new Promise(function (resolve) {
var image = new Image();
image.src = src;

var max_width = Math.min(image.width, image.height);
var max_height = Math.min(image.width, image.height);

var canvas = document.createElement('canvas');
var ctx = canvas.getContext("2d");

ctx.clearRect(0, 0, canvas.width, canvas.height);
canvas.width = max_width;
canvas.height = max_height;
ctx.drawImage(image, (max_width - image.width)/2, (max_height - image.height)/2, image.width, image.height);
return canvas.toDataURL("image/png");
image.onload = function () {
var max_width = Math.min(image.width, image.height);
var max_height = Math.min(image.width, image.height);

var canvas = document.createElement('canvas');
var ctx = canvas.getContext("2d");

ctx.clearRect(0, 0, canvas.width, canvas.height);
canvas.width = max_width;
canvas.height = max_height;
ctx.drawImage(image, (max_width - image.width)/2, (max_height - image.height)/2, image.width, image.height);
resolve(canvas.toDataURL("image/png"));
};
});
}

function resize(src){
var image = new Image();
image.src = src;

var canvas = document.createElement('canvas');
canvas.width = image.width;
canvas.height = image.height;
var ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(image, 0, 0, image.width, image.height);

var dst = document.createElement('canvas');
dst.width = image_dimension;
dst.height = image_dimension;

window.pica.WW = false;
window.pica.resizeCanvas(canvas, dst, {
quality: 2,
unsharpAmount: 500,
unsharpThreshold: 100,
transferable: false
}, function (err) { });
window.pica.WW = true;
return dst.toDataURL("image/png");
var image = new Image();
image.src = src;
return new Promise(function (resolve) {
image.onload = function () {
var canvas = document.createElement('canvas');
canvas.width = image.width;
canvas.height = image.height;
var ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(image, 0, 0, image.width, image.height);

var dst = document.createElement('canvas');
dst.width = image_dimension;
dst.height = image_dimension;

window.pica.WW = false;
window.pica.resizeCanvas(canvas, dst, {
quality: 2,
unsharpAmount: 500,
unsharpThreshold: 100,
transferable: false
}, function (err) { });
window.pica.WW = true;
resolve(dst.toDataURL("image/png"));
};
});
}

Loading