Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async init() {
this.useFloatTextures = !GLOBALS.browserUtils.isMobile && !GLOBALS.browserUtils.isSafari;
tf.ENV.set('WEBGL_DOWNLOAD_FLOAT_ENABLED', false);
this.classifier = knnClassifier.create();
// Load mobilenet.
this.mobilenetModule = await mobilenet.load();
}
if (this.state.featureIndices !== undefined && this.state.featureIndices.length > 0) {
return emb.gather(this.state.featureIndices).arraySync();
}
return emb.arraySync();
});
embeddings.dispose();
return this.normalize(embedding).then(normalizedEmbedding => {
return normalizedEmbedding.map(v => this.web3.utils.toHex(v));
});
};
this.transformInput = this.transformInput.bind(this);
});
} else if (this.state.contractInfo.encoder === 'MobileNetv2') {
this.setState({ inputType: INPUT_TYPE_IMAGE });
// https://github.com/tensorflow/tfjs-models/tree/master/mobilenet
mobilenet.load({
version: 2,
alpha: 1,
}).then(model => {
this.transformInput = async (imgElement) => {
if (Array.isArray(imgElement)) {
// Assume this is from given data and this method is being called from data already in the database.
return imgElement;
}
const imgEmbedding = await model.infer(imgElement, { embedding: true });
let embedding = tf.tidy(_ => {
const embedding = imgEmbedding.gather(0);
if (this.state.featureIndices !== undefined && this.state.featureIndices.length > 0) {
return embedding.gather(this.state.featureIndices).arraySync();
}
return embedding.arraySync();
});
async componentDidMount() {
// Load mobilenet
const model = await mobilenet.load();
//warmup mobilenet
await model.classify(tf.zeros([1, 224, 224, 3]));
// Read the image into a tensor
const imageAssetPath = Image.resolveAssetSource(this.props.image);
const response = await fetch(imageAssetPath.uri, {}, { isBinary: true });
const rawImageData = await response.arrayBuffer();
const imageTensor = this.imageToTensor(rawImageData);
// Compute a checksum for the image. Useful for debugging.
const imageTensorSum = imageTensor.sum();
const imageChecksum = (await imageTensorSum.data())[0];
// Classify the image.
const start = Date.now();
tf.loadModel('http://localhost:5000/model.json').then(model => {
mobileNet
.load()
.then((mn: any) => {
mobilenet = mobilenetInfer(mn);
document.getElementById('playground').style.display = 'table';
document.getElementById('loading-page').style.display = 'none';
console.log('MobileNet created');
})
.then(() => {
setInterval(() => {
canvas.getContext('2d').drawImage(video, 0, 0);
crop.getContext('2d').drawImage(canvas, 0, 0, ImageSize.Width, ImageSize.Height);
crop
.getContext('2d')
.drawImage(
canvas,
async componentDidMount() {
await tf.ready()
this.setState({
isTfReady: true
})
this.model = await mobilenet.load()
this.setState({ isModelReady: true })
this.getPermissionAsync()
}
async loadMobileNetModal() {
const modal = await mobilenet.load({
version: 1,
alpha: 0.25 | .50 | .75 | 1.0,
})
return modal;
}
async bindPage() {
this.knn = knnClassifier.create();
this.mobilenet = await mobilenetModule.load();
this.start();
}
async function main() {
global.encoder = await mobilenet.load(
{
version: 2,
alpha: 1,
}
);
let model;
switch (CLASSIFIER_TYPE) {
case 'ncc':
model = await getNearestCentroidModel();
break;
case 'perceptron':
model = await getPerceptronModel();
break;
default:
throw new Error(`Unrecognized classifierType: "${CLASSIFIER_TYPE}"`);