Skip to content

Commit

Permalink
selection issue fix (#260)
Browse files Browse the repository at this point in the history
* selection issue fix

* model.js updated

* closing box if selection region is small

* closing box if selection region is small

Co-authored-by: Ryan Birmingham <[email protected]>
Co-authored-by: leoarc <[email protected]>
  • Loading branch information
3 people authored Mar 29, 2020
1 parent 259e225 commit 910ea1f
Show file tree
Hide file tree
Showing 2 changed files with 247 additions and 236 deletions.
250 changes: 128 additions & 122 deletions apps/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,10 @@ function camicStopDraw(e) {
if (args) {
runPredict(args.status);
}
var memory = tf.memory();
console.log(memory);
$UI.modelPanel.setPosition(box.rect.x,box.rect.y,box.rect.width,box.rect.height);
$UI.modelPanel.open(args);
if($UI.modelPanel.__spImgWidth != 0){
$UI.modelPanel.open(args);
}

canvasDraw.clear();
csvContent = "";
Expand Down Expand Up @@ -469,140 +469,146 @@ function runPredict(key) {
Y = self.__spImgY,
totalSize = self.__spImgWidth,
step = parseInt(key.split('_')[1].split('-')[0]);

self.showResults(" --Result-- ")
if(totalSize > 0){
const prefix_url = ImgloaderMode == 'iip'?`../../img/IIP/raw/?IIIF=${$D.params.data.location}`:$CAMIC.slideId;
self.showProgress("Predicting...");

const prefix_url = ImgloaderMode == 'iip'?`../../img/IIP/raw/?IIIF=${$D.params.data.location}`:$CAMIC.slideId;
self.showProgress("Predicting...");
let fullResCvs = self.__fullsrc;

let fullResCvs = self.__fullsrc;
// Starting the transaction and opening the model store
let tx = db.transaction("models_store", "readonly");
let store = tx.objectStore("models_store");
store.get(key).onsuccess = async function (e) {
// Keras sorts the labels by alphabetical order.
let classes = e.target.result.classes.sort();

// Starting the transaction and opening the model store
let tx = db.transaction("models_store", "readonly");
let store = tx.objectStore("models_store");
store.get(key).onsuccess = async function (e) {
// Keras sorts the labels by alphabetical order.
let classes = e.target.result.classes.sort();
let input_shape = e.target.result.input_shape
// let input_channels = parseInt(input_shape[3]);
let input_channels = 3;
let image_size = input_shape[1];

let input_shape = e.target.result.input_shape
// let input_channels = parseInt(input_shape[3]);
let input_channels = 3;
let image_size = input_shape[1];
model = await tf.loadLayersModel(IDB_URL + key);
self.showProgress("Model loaded...");
tfvis.show.modelSummary({name: 'Model Summary', tab: 'Model Inspection'}, model);

model = await tf.loadLayersModel(IDB_URL + key);
self.showProgress("Model loaded...");
tfvis.show.modelSummary({name: 'Model Summary', tab: 'Model Inspection'}, model);
// Warmup the model before using real data.
tf.tidy(()=>{
model.predict(tf.zeros([1, image_size, image_size, input_channels]));
console.log("Model ready");
});

// Warmup the model before using real data.
tf.tidy(()=>{
model.predict(tf.zeros([1, image_size, image_size, input_channels]));
console.log("Model ready");
});
const memory = tf.memory()
console.log("Model Memory Usage")
console.log("GPU : " + memory.numBytesInGPU + " bytes")
console.log("Total : " + memory.numBytes + " bytes")

let temp = document.querySelector('#dummy');
temp.height = step;
temp.width = step;

function addImageProcess(src){
return new Promise((resolve, reject) => {
let img = new Image()
img.onload = () => resolve(img)
img.onerror = reject
img.src = src
})
}

const memory = tf.memory()
console.log("Model Memory Usage")
console.log("GPU : " + memory.numBytesInGPU + " bytes")
console.log("Total : " + memory.numBytes + " bytes")

let temp = document.querySelector('#dummy');
temp.height = step;
temp.width = step;

function addImageProcess(src){
return new Promise((resolve, reject) => {
let img = new Image()
img.onload = () => resolve(img)
img.onerror = reject
img.src = src
})
}
let results = [];
csvContent = "data:text/csv;charset=utf-8,";
classes.forEach((e) => {
csvContent += e + ",";
});
csvContent += "x,y\n\r";
self.showProgress("Predicting...");

for (let y = Y, dy = 0; y < (Y + totalSize); y+=(step)) {
let dx = 0
for (let x = X; x < (X + totalSize); x+=(step)) {

let src = prefix_url+'\/'+x+','+y+','+step+','+step+'\/'+step+',/0/default.jpg';

let l_img = await addImageProcess(src);
fullResCvs.height = l_img.height;
fullResCvs.width = l_img.width;
fullResCvs.getContext('2d').drawImage(l_img, 0, 0);

let imgData = fullResCvs.getContext('2d').getImageData(0,0,fullResCvs.width,fullResCvs.height);
tf.tidy(()=>{
const img = tf.browser.fromPixels(imgData).toFloat();
let img2;
if (input_channels == 1) {
img2 = tf.image.resizeBilinear(img, [image_size, image_size]).mean(2);
} else {
img2 = tf.image.resizeBilinear(img, [image_size, image_size]);
}
let scaleMethod = $UI.filter? $UI.filter.status: 'norm';
console.log(scaleMethod);

let results = [];
csvContent = "data:text/csv;charset=utf-8,";
classes.forEach((e) => {
csvContent += e + ",";
});
csvContent += "x,y\n\r";
self.showProgress("Predicting...");
let normalized;
if (scaleMethod == 'norm') {
// Pixel Normalization: scale pixel values to the range 0-1.

for (let y = Y, dy = 0; y < (Y + totalSize); y+=(step)) {
let dx = 0
for (let x = X; x < (X + totalSize); x+=(step)) {

let src = prefix_url+'\/'+x+','+y+','+step+','+step+'\/'+step+',/0/default.jpg';

let l_img = await addImageProcess(src);
fullResCvs.height = l_img.height;
fullResCvs.width = l_img.width;
fullResCvs.getContext('2d').drawImage(l_img, 0, 0);

let imgData = fullResCvs.getContext('2d').getImageData(0,0,fullResCvs.width,fullResCvs.height);
tf.tidy(()=>{
const img = tf.browser.fromPixels(imgData).toFloat();
let img2;
if (input_channels == 1) {
img2 = tf.image.resizeBilinear(img, [image_size, image_size]).mean(2);
} else {
img2 = tf.image.resizeBilinear(img, [image_size, image_size]);
}
let scaleMethod = $UI.filter? $UI.filter.status: 'norm';
console.log(scaleMethod);

let normalized;
if (scaleMethod == 'norm') {
// Pixel Normalization: scale pixel values to the range 0-1.

let scale = tf.scalar(255);
normalized = img2.div(scale);

} else if (scaleMethod == 'center') {
// Pixel Centering: scale pixel values to have a zero mean.

let mean = img2.mean();
normalized = img2.sub(mean);
// normalized.mean().print(true); // Uncomment to check mean value.
// let min = img2.min();
// let max = img2.max();
// let normalized = img2.sub(min).div(max.sub(min));
} else {
// Pixel Standardization: scale pixel values to have a zero mean and unit variance.

let mean = img2.mean();
let std = (img2.squaredDifference(mean).sum()).div(img2.flatten().shape).sqrt();
normalized = img2.sub(mean).div(std);
}
let batched = normalized.reshape([1, image_size, image_size, input_channels]);
let values =model.predict(batched).dataSync();

values.forEach((e) => {
csvContent += e.toString() + ",";
})
csvContent += '' + dx + "," + dy + "\n\r";
let scale = tf.scalar(255);
normalized = img2.div(scale);

results.push(values);
// Retrieving the top class
} else if (scaleMethod == 'center') {
// Pixel Centering: scale pixel values to have a zero mean.

dx += step;
});
let mean = img2.mean();
normalized = img2.sub(mean);
// normalized.mean().print(true); // Uncomment to check mean value.
// let min = img2.min();
// let max = img2.max();
// let normalized = img2.sub(min).div(max.sub(min));
} else {
// Pixel Standardization: scale pixel values to have a zero mean and unit variance.

let mean = img2.mean();
let std = (img2.squaredDifference(mean).sum()).div(img2.flatten().shape).sqrt();
normalized = img2.sub(mean).div(std);
}
let batched = normalized.reshape([1, image_size, image_size, input_channels]);
let values =model.predict(batched).dataSync();

values.forEach((e) => {
csvContent += e.toString() + ",";
})
csvContent += '' + dx + "," + dy + "\n\r";

results.push(values);
// Retrieving the top class

dx += step;
});
}
dy += step;
}
dy += step;
}

let len = results.length;
let final = new Array(results[0].length).fill(0);
for (let i = 0; i < results.length; i++) {
for (let j = 0; j < results[0].length; j++) {
final[j] += results[i][j]
let len = results.length;
let final = new Array(results[0].length).fill(0);
for (let i = 0; i < results.length; i++) {
for (let j = 0; j < results[0].length; j++) {
final[j] += results[i][j]
}
}
for (let i = 0; i < final.length; i++) {
final[i] /= len;
}
}
for (let i = 0; i < final.length; i++) {
final[i] /= len;
}

i_max = Object.keys(final).reduce((a, b) => final[a] > final[b] ? a : b);
let i = parseInt(i_max) + 1;
self.showResults('' + i + ': ' + classes[i_max] + ' - ' + final[i_max].toFixed(3));
self.hideProgress()
model.dispose()
};
i_max = Object.keys(final).reduce((a, b) => final[a] > final[b] ? a : b);
let i = parseInt(i_max) + 1;
self.showResults('' + i + ': ' + classes[i_max] + ' - ' + final[i_max].toFixed(3));
self.hideProgress()
model.dispose()
};
}
else{
alert("Selected section too small. Please select a larger section.");
}
}


Expand Down
Loading

0 comments on commit 910ea1f

Please sign in to comment.