Public
Edited
Nov 29, 2023
Insert cell
md`# 📑 Tensorflow Hub Practice`
Insert cell
img_folder='animals/'
Insert cell
img_file='00_00_001.png'
Insert cell
img_size=224
Insert cell
viewof imgSrc={
const animalSrc='https://raw.githubusercontent.com/'
.concat('OlgaBelitskaya/data/main/')
.concat(img_folder).concat(img_file);
const input=html`
<input type=text
style='background:lightgray; border:double;
width:600px; height:20px; font-size:16px'>`;
input.value=animalSrc;
return input;
}
Insert cell
image={
const img=new Image();
img.setAttribute('crossorigin','anonymous');
img.width=img_size; img.height=img_size; img.src=imgSrc;
return img;
}
Insert cell
html`<p style='font-size:32px'>${imagenet_classes[idx1]}</p>`
Insert cell
html`<p style='font-size:32px'>${imagenet_classes[idx2]}</p>`
Insert cell
label={
while(true) {
yield tf.tidy(()=>{
let offset=tf.scalar(127.5)
let img=tf.browser.fromPixels(image)
let inputs=img.cast('float32').sub(offset)
.div(offset).reshape([1,img_size,img_size,3])
return model.predict(inputs).dataSync();
})
}
}
Insert cell
idx1=label.indexOf(Math.max(...label))
Insert cell
secondMax=function (arr){
var max=Math.max.apply(null,arr),maxidx=arr.indexOf(max);
arr[maxidx]=-Infinity;
var secondMax=Math.max.apply(null,arr);
arr[maxidx]=max;
return secondMax;
};
Insert cell
idx2=label.indexOf(secondMax(label))
Insert cell
Insert cell
model_url='https://tfhub.dev/google/tfjs-model/'
.concat('imagenet/mobilenet_v2_100_').concat(img_size)
.concat('/classification/3/default/1')
Insert cell
model=tf.loadGraphModel(model_url,{fromTFHub:true})
Insert cell
tf = require('@tensorflow/tfjs')
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