Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The accuracy is much less than the site #796

Open
morteza102030 opened this issue Nov 13, 2023 · 1 comment
Open

The accuracy is much less than the site #796

morteza102030 opened this issue Nov 13, 2023 · 1 comment

Comments

@morteza102030
Copy link

The accuracy is much less than the site
I have found hundreds of examples of contradictions and this is just one

ww
image

my result

[
{ className: 'Porn', probability: 0.960785984992981 },
{ className: 'Neutral', probability: 0.027830250561237335 },
{ className: 'Sexy', probability: 0.007882381789386272 },
{ className: 'Hentai', probability: 0.003485230728983879 },
{ className: 'Drawing', probability: 0.000016127258277265355 }
]

@caycewilliams
Copy link

caycewilliams commented Dec 1, 2023

@morteza102030
I had the same problem with accuracy, on a node nestjs api I was able to do the following setup and get a more accurate classification as shown on the website.

For it to work well I was missing two key parts:

  1. Convert images to jpeg using jpeg-js and return as tf.Tensor3D for classifcation
  2. Use the MobileNet V2 (90% accurate) model on https://nsfwjs.com/. This is what nsfwjs.load() uses

model.json download: https://nsfwjs.com/quant_nsfw_mobilenet/model.json
group1-shard1of1 download: https://nsfwjs.com/quant_nsfw_mobilenet/group1-shard1of1

If using them locally, make sure to put them in a static directory (for nestjs, you need @nestjs/serve-static, and place them in dist/public/model)

Like so:

public/
  model/
    model.json
    group1-shard1of1
import * as nsfwjs from 'nsfwjs'
import * as tf from '@tensorflow/tfjs'
import * as jpeg from 'jpeg-js'
const sharp = require('sharp')

//I used @UploadedFile from '@nestjs/common' to handle image uploads.
interface MulterFile {
  fieldname: string
  originalname: string
  encoding: string
  mimetype: string
  buffer: Buffer
  size: number
}
async isCensorableImage(file: MulterFile) {

        const imageToTensor = async (rawImageData: ArrayBuffer): Promise<tf.Tensor3D | ImageData> => {
            rawImageData = await sharp(rawImageData).raw().jpeg().toBuffer()
            const decoded = jpeg.decode(rawImageData); //This is key for the prediction to work well
            const { width, height, data } = decoded
            const buffer = new Uint8Array(width * height * 3);
            let offset = 0;
            for (let i = 0; i < buffer.length; i += 3) {
                buffer[i] = data[offset];
                buffer[i + 1] = data[offset + 1];
                buffer[i + 2] = data[offset + 2];

                offset += 4;
            }

            return tf.tensor3d(buffer, [height, width, 3]);
        }
        await tf.ready();
        tf.env().set('PROD', true) //Disable logs
        const nsfwModel = await nsfwjs.load('http://localhost:8000/model/', { size: 224 }); // Or nsfwjs.load()
        const imageTensor = await imageToTensor(file.buffer);
        const predictionsArray = await nsfwModel.classify(imageTensor)
        console.log(predictionsArray)
        
        return true; //Your logic here checking the predictionsArray
    }

Using node v20.5.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants