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

Fixed all memory leaks in segmentation view #261

Merged
merged 4 commits into from
Mar 28, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
17 changes: 11 additions & 6 deletions apps/segment/segment.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,7 @@ function camicStopDraw(e) {
} else {
segmentModel(args.status);
}
var memory = tf.memory();
console.log(memory);
console.log(tf.memory());
$UI.segmentPanel.setPosition(box.rect.x,box.rect.y,box.rect.width,box.rect.height);
$UI.segmentPanel.open(args);

Expand Down Expand Up @@ -686,7 +685,11 @@ async function segmentModel(key) {

model = await tf.loadLayersModel(IDB_URL + key);
console.log('Model Loaded');

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

tf.tidy(()=>{
// Warmup the model before using real data.
const warmupResult = model.predict(tf.zeros([1, image_size, image_size, input_channels]));
Expand Down Expand Up @@ -735,6 +738,7 @@ async function segmentModel(key) {
} else {
img2 = tf.image.resizeBilinear(img, [image_size, image_size]);
}
img.dispose()
let scaleMethod = $UI.filter? $UI.filter.status: 'norm';
console.log(scaleMethod);

Expand All @@ -761,7 +765,7 @@ async function segmentModel(key) {
let std = (img2.squaredDifference(mean).sum()).div(img2.flatten().shape).sqrt();
normalized = img2.sub(mean).div(std);
}

img2.dispose()

let batched = normalized.reshape([1, image_size, image_size, input_channels]);
let values = model.predict(batched).dataSync();
Expand All @@ -771,9 +775,10 @@ async function segmentModel(key) {
val = new Array();
while (values.length > 0) val.push(values.splice(0, image_size));
});
tf.engine().startScope()
await tf.browser.toPixels(val, temp);
finalRes.getContext('2d').drawImage(temp, dx, dy);

finalRes.getContext('2d').drawImage(temp, dx, dy);
tf.engine().endScope()
dx += step;
}
dy += step;
Expand Down
6 changes: 3 additions & 3 deletions apps/segment/tf.min.js

Large diffs are not rendered by default.