Skip to content

Instantly share code, notes, and snippets.

@kotobuki
Last active August 20, 2018 03:42
Show Gist options
  • Save kotobuki/259bf67f6d921e149d04bba48db39a7d to your computer and use it in GitHub Desktop.
Save kotobuki/259bf67f6d921e149d04bba48db39a7d to your computer and use it in GitHub Desktop.
Grad-CAM [WIP]
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<h1></h1>
<img id="bird" src="https://ml5js.org/docs/assets/img/bird.jpg" crossorigin="anonymous" />
</body>
</html>
console.clear();
let sourceImg;
function setup() {
sourceImg = select("#bird");
sourceImg.class("img").size(224, 224);
loadMobilenet();
}
async function loadMobilenet() {
const model = await tf.loadModel(
"https://storage.googleapis.com/tfjs-models/tfjs/mobilenet_v1_0.25_224/model.json"
);
model.summary();
// Reference:
// https://github.com/Ankush96/grad-cam.tensorflow/blob/master/main.py#L29-L62
const nbClasses = 1000;
model.predict(imgToTensor(sourceImg.elt, [224, 224])).print();
const predictedClass = model
.predict(imgToTensor(sourceImg.elt, [224, 224]))
.as1D()
.argMax();
let classId = (await predictedClass.data())[0];
const convLayer = model.getLayer("conv_pw_13");
const oneHot = tf.oneHot(tf.tensor1d([classId], "int32"), nbClasses);
const signal = tf.mul(
model.getLayer("reshape_2").output,
oneHot.asType("float32")
);
// TODO: Implement following lines...
}
// From: https://github.com/ml5js/ml5-library/blob/5756876f3647cab6ff657dd0db4ec92ed668796e/src/utils/imageUtilities.js#L64-L75
function imgToTensor(input, size = null) {
return tf.tidy(() => {
let img = tf.fromPixels(input);
if (size) {
img = tf.image.resizeBilinear(img, size);
}
// const croppedImage = cropImage(img);
const batchedImage = img.expandDims(0);
return batchedImage
.toFloat()
.div(tf.scalar(127))
.sub(tf.scalar(1));
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/tensorflow/0.12.5/tf.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.1/p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.1/addons/p5.dom.min.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment