Public
Edited
Sep 18, 2021
Insert cell
md`# 📑 Tf Practice 2`
Insert cell
Insert cell
data=d3.range(100).map(function(d) {
return {'y':d3.randomUniform(100)()}; })
Insert cell
function get_values(data) {
var values=[];
for (var i=0; i<100; i++){values.push(Math.floor(data[i]['y']*100)/100)};
return values.toString();}
Insert cell
get_values(data)
Insert cell
width_img=224
Insert cell
height_img=224
Insert cell
topK=5
Insert cell
getImg=function(file) {
const img=new Promise((resolve,reject)=>{
let image=new Image();
image.onload=()=>resolve(image); image.onerror=reject;
image.crossOrigin='anonymous';
image.src='https://raw.githubusercontent.com/'
.concat('OlgaBelitskaya/data/main/birds/').concat(file);
image.width=width_img; image.height=height_img;})
return img; }
Insert cell
img0=getImg('00_00_001.png')
Insert cell
img1=getImg('01_00_001.png')
Insert cell
img2=getImg('00_02_001.png')
Insert cell
Insert cell
tensor0=tf.browser.fromPixels(img0)
Insert cell
tensor1=tf.browser.fromPixels(img1)
Insert cell
tensor2=tf.browser.fromPixels(img2)
Insert cell
Insert cell
createImg=function(tensor) {
const [height,width]=tensor.shape;
const ctx=DOM.context2d(height,width);
const buffer=new Uint8ClampedArray(width*height*4)
const imageData=new ImageData(width,height);
const data=tensor.dataSync();
var i=0;
for(var y=0; y<height; y++) {
for(var x=0; x<width; x++) {
var pos=(y*width+x)*4;
buffer[pos ]=data[i]
buffer[pos+1]=data[i+1]
buffer[pos+2]=data[i+2]
buffer[pos+3]=255;
i+=3 } }
imageData.data.set(buffer)
ctx.putImageData(imageData,0,0);
return ctx.canvas; }
Insert cell
timg0=createImg(tensor0)
Insert cell
timg1=createImg(tensor1)
Insert cell
timg2=createImg(tensor2)
Insert cell
Insert cell
predict0=await mobilenet.classify(tensor0,topK)
Insert cell
predict1=await mobilenet.classify(tensor1,topK)
Insert cell
predict2=await mobilenet.classify(tensor2,topK)
Insert cell
mn_predictions=function(predictions) {
const text=predictions.map(prediction=>
prediction.probability.toFixed(3)+': '+prediction.className+'<br>');
const rows=predictions.map(prediction=> `
<div style='display:table-row; background:silver;'>
<div style='display:table-cell; width:300px; padding:2px'>
${prediction.className}</div>
<div style='display:table-cell; padding:2px; text-align:center;'>
${prediction.probability.toFixed(3)}</div>
<div style='display:table-cell; padding:2px; text-align:center;'>
${imagenetClassIdx[prediction.className]}</div>
</div>`);
return html`
<div style='display:table-row; font-weight:bold;
text-align:center; background:silver;'>
<div style='display:table-cell; border:double black;
padding:5px'>class</div>
<div style='display:table-cell; border:double black;
padding:5px'>probability</div>
<div style='display:table-cell; border:double black;
padding:5px'>imagenet class id</div>
</div>
${rows.join('')}`}
Insert cell
mn_predictions(predict0)
Insert cell
mn_predictions(predict1)
Insert cell
mn_predictions(predict2)
Insert cell
md`## Tools`
Insert cell
mobilenet=await mobilenetModule.load()
Insert cell
Insert cell
imagenetClassIdx={
const result={};
for (let i=0; i<1000; i++) {
const className=imagenetClasses[i+''];
result[className]=i; }
return result;}
Insert cell
mobilenetModule=await require('@tensorflow-models/mobilenet@0.2')
Insert cell
//mobilenetModule=await require('@tensorflow-models/mobilenet@2.0.4')
Insert cell
tf=await require('@tensorflow/tfjs@1.0.0')
Insert cell
d3=require('d3@6')
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more