Published
Edited
Apr 29, 2018
5 forks
13 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
collageFn = {
return pixels => {
return tf.tidy(() => {
// Feed the input through the model.
let activation = models[layerId].predict(pixels.expandDims(0)).squeeze();

// Normalize within each channel if the checkbox is checked.
if (channelNormalize) {
// Global pool so we compute normalization params for each channel.
const strides = 1; // Doesn't matter
const pad = 0;
const maxpool = activation.maxPool([activation.shape[0], activation.shape[1]], strides, pad);
const minpool = activation.neg().maxPool(
[activation.shape[0], activation.shape[1]], strides, pad).neg()
// To avoid dividing by zero.
.add(tf.scalar(.0000001));

activation = activation.sub(minpool).div(maxpool.sub(minpool));
}

// Transpose the channels to the outer most dimension and then reshape the activation so we can show
// it in a rectangular collage.
const reshaped = activation.transpose([2, 0, 1]).reshape(
[collageDims[layerId][0], collageDims[layerId][1], activation.shape[0], activation.shape[1]]);

// Split channels between rows and columns. [16, width, height] becomes [4, width, 4, height]
const transposed = reshaped.transpose([0, 2, 1, 3]);

// Combine channels. [4, height, 4, height] becomes [4 * height, 4 * height]
let output = transposed.reshape(
[transposed.shape[0] * transposed.shape[1], transposed.shape[2] * transposed.shape[3]]);

// Normalize across the entire activation if the channelNormalize option isn't checked.
if (!channelNormalize) {
const min = output.min();
const max = output.max();
output = output.sub(min).div(max.sub(min));
}
return output.maximum(tf.scalar(0)).minimum(tf.scalar(1));
});
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more