Platform
Resources
Pricing
Sign in
Get started
Olga Belitskaya
Workspace
Fork
Public
By
Olga Belitskaya
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.
Try it for free
Learn more
Fork
View
Export
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
img_folder
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
img_file
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
img_size
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
imgSrc
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
image
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
label
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
idx1
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
secondMax
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
idx2
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
imagenet_classes
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
model_url
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
model
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
tf
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML