Skip to content

FAQ: NSFW JS

Kevin VanGelder edited this page May 7, 2020 · 3 revisions

Can I get other versions of this model?

Originally asked in https://github.com/infinitered/nsfwjs/issues/50

Yes! We have several versions of this model, including the source code to make your own! The training code and several versions of the model are available here: https://github.com/GantMan/nsfw_model

Can I use this model in Backend Node with tfjs-node?

Originally asked in https://github.com/infinitered/nsfwjs/issues/25

Yes! Untested, but the following code is apparently functional.

const tf =require('@tensorflow/tfjs-node')
const load=require('./dist/index').load

const fs = require('fs');
const jpeg = require('jpeg-js');


// Fix for JEST
const globalAny = global
globalAny.fetch = require('node-fetch')
const timeoutMS = 10000
const NUMBER_OF_CHANNELS = 3


const readImage = (path) => {
  const buf = fs.readFileSync(path)
  const pixels = jpeg.decode(buf, true)
  return pixels
}


const imageByteArray = (image, numChannels) => {
  const pixels = image.data
  const numPixels = image.width * image.height;
  const values = new Int32Array(numPixels * numChannels);

  for (let i = 0; i < numPixels; i++) {
    for (let channel = 0; channel < numChannels; ++channel) {
      values[i * numChannels + channel] = pixels[i * 4 + channel];
    }
  }

  return values
}


const imageToInput = (image, numChannels) => {
  const values = imageByteArray(image, numChannels)
  const outShape = [image.height, image.width, numChannels] ;
  const input = tf.tensor3d(values, outShape, 'int32');

  return input
}


    (async()=>{
        const model = await load('file://./model/')//moved model at root of folder
  const logo = readImage(`./_art/nsfwjs_logo.jpg`)
  const input = imageToInput(logo, NUMBER_OF_CHANNELS)
  console.time('predict')
  const predictions = await model.classify(input)
  console.timeEnd('predict')
 console.log(predictions)
    })()

Does this model work with React Native?

Yes! A proof of concept can be found here: https://shift.infinite.red/nsfw-js-for-react-native-a37c9ba45fe9

Where can I learn more about AI/Machine Learning?

We have a free AI Demystified mini-course, a Beginning Machine Learning with TensorFlow.js course, and a Kickstarter for a Intro to Facial Machine Learning in Javascript course