-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathclassifier_landmarks.js
169 lines (152 loc) · 4.25 KB
/
classifier_landmarks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const path = require('path')
const VERSION = require('../package.json').version
const download = require('download')
const tar = require('tar')
const fsSync = require('fs')
const YAML = require('yaml')
const _ = require('lodash')
const LABELS = {
landmarks_africa: require('./landmarks/africa.json').name,
landmarks_asia: require('./landmarks/asia.json').name,
landmarks_europe: require('./landmarks/europe.json').name,
landmarks_north_america: require('./landmarks/north_america.json').name,
landmarks_south_america: require('./landmarks/south_america.json').name,
landmarks_oceania: require('./landmarks/oceania.json').name,
}
let tf, getPort, StaticServer
let PUREJS = false
if (process.env.RECOGNIZE_PUREJS === 'true') {
tf = require('@tensorflow/tfjs')
require('@tensorflow/tfjs-backend-wasm')
getPort = require('get-port')
StaticServer = require('static-server')
PUREJS = true
} else {
try {
if (false && process.env.RECOGNIZE_GPU === 'true') {
tf = require('@tensorflow/tfjs-node-gpu')
} else {
tf = require('@tensorflow/tfjs-node')
}
} catch (e) {
console.error(e)
console.error('Trying js-only mode')
tf = require('@tensorflow/tfjs')
require('@tensorflow/tfjs-backend-wasm')
PUREJS = true
}
}
const EfficientNet = require('./efficientnet/EfficientnetModel')
const THRESHOLD = 0.82
/**
* @param className
*/
function findRule(className) {
const rule = rules[className]
if (!rule) {
return
}
if (rule.see) {
return findRule(rule.see)
}
return rule
}
if (process.argv.length < 3) throw new Error('Incorrect arguments: node classify.js ...<IMAGE_FILES> | node classify.js -')
/**
* @param modelName
* @param imgSize
* @param minInput
* @param paths
*/
async function main(modelName, imgSize, minInput, paths) {
const modelPath = path.resolve(__dirname, '..', 'models', modelName)
const modelFileName = 'model.json'
let modelUrl
if (PUREJS) {
// See https://github.com/tensorflow/tfjs/issues/4927
const port = await getPort()
const server = new StaticServer({
rootPath: modelPath,
port,
})
await new Promise(resolve => server.start(resolve))
modelUrl = `http://localhost:${port}/${modelFileName}`
} else {
modelUrl = `file://${modelPath}/${modelFileName}`
}
// Download model on first run
if (!fsSync.existsSync(modelPath)) {
await download(
`https://github.com/marcelklehr/recognize/archive/refs/tags/v${VERSION}.tar.gz`,
path.resolve(__dirname, '..')
)
await new Promise(resolve =>
tar.x({
strip: 1,
C: path.resolve(__dirname, '..'),
file: path.resolve(__dirname, '..', `recognize-${VERSION}.tar.gz`),
}, [`recognize-${VERSION}/models/${modelName}`], resolve)
)
}
const model = await EfficientNet.create(modelUrl, imgSize, minInput, LABELS[modelName])
const result = []
for (const path of paths) {
try {
let results = await model.inference(path, {
topK: 7,
softmax: false,
})
results = results
.filter(result => {
if (result.probability < 0.0) {
return false
}
return result.probability >= THRESHOLD
})
result.push(results)
} catch (e) {
console.error(e)
result.push([])
}
}
return result
}
tf.setBackend(process.env.RECOGNIZE_PUREJS === 'true' ? 'wasm' : 'tensorflow')
.then(async () => {
const imgSize = 321
const minInput = 0
const models = ['landmarks_africa', 'landmarks_asia', 'landmarks_europe', 'landmarks_north_america', 'landmarks_south_america', 'landmarks_oceania']
const getStdin = (await import('get-stdin')).default
const paths = process.argv[2] === '-'
? (await getStdin()).split('\n')
: process.argv.slice(2)
let error
const labels = []
for (const modelName of models) {
try {
const results = await main(modelName, imgSize, minInput, paths)
results.forEach((result, i) => (labels[i] = labels[i] || []).push(...result))
} catch (e) {
console.error(e)
error = e
}
}
labels.forEach((labels, i) => {
console.error(paths[i])
labels.sort((a, b) => a.probability - b.probability).reverse()
console.error(labels)
if (labels.length) {
console.log(JSON.stringify([labels[0].className]))
} else {
console.log(JSON.stringify([]))
}
})
if (error) {
throw error
}
})
.then(() => process.exit(0))
.catch(e => {
console.error(e)
process.exit(1)
})