notAI-tech / NudeNet

Lightweight nudity detection
https://nudenet.notai.tech/
GNU Affero General Public License v3.0
1.74k stars 339 forks source link

Incompability with newest tensorflow #62

Closed yowoda closed 3 years ago

yowoda commented 3 years ago

Describe the bug and error messages (if any) I keep getting the error, that the tensorflow module, doesn't have an attribute called contrib. I believe this has to do with tensorflow pushing a new version that removes that attribute. Correct me if I'm wrong.

Traceback:

Traceback (most recent call last):
  File "/Users//Desktop/hallo/Code/hikari/hikaribot/src/bot.py", line 290, in handle_errors
    raise error
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/lightbulb/command_handler.py", line 853, in process_commands_for_event
    await self._invoke_command(command, context, positional_args, keyword_arg)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/lightbulb/command_handler.py", line 789, in _invoke_command
    await command.invoke(context)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/lightbulb/commands.py", line 503, in invoke
    return await self._callback(context, *new_args, **kwargs)
  File "/Users//Desktop/hallo/Code/hikari/hikaribot/src/bot.py", line 244, in nsfw
    detector = NudeDetector()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/nudenet/detector.py", line 62, in __init__
    self.detection_model = tf.contrib.predictor.from_saved_model(
AttributeError: module 'tensorflow' has no attribute 'contrib'

**The code snippet which gave this error***

@bot.command()
async def nsfw(ctx):
    attachment = ctx.message.attachments[0]
    async with attachment.stream() as resp:
        data = await resp.read()

    detector = NudeDetector() #stuck here

    print(detector.detect(data))

data is of the type bytes

Specify versions of the following libraries

  1. nudenet - 2.0.6
  2. tensorflow/ tensorflow-gpu - 2.3.1
  3. keras - not installed

Expected behavior No errors, and the coordinates of the boxes being printed

bedapudi6788 commented 3 years ago

# tensorflow/ tensorflow-gpu <= 1.15.4 is required

At the moment I don't support/plan to support newer tensorflow versions.

yowoda commented 3 years ago

So it doesn't support python 3.8? Since pip doesn't find that version

bedapudi6788 commented 3 years ago

Not out of the box. You can always use the docker image and call it as a rest api.

On Wed, 18 Nov 2020, 20:43 Yoda, notifications@github.com wrote:

So it doesn't support python 3.8? Since pip doesn't find that version

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/notAI-tech/NudeNet/issues/62#issuecomment-729744842, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADZJQHQF7QINMQWRLEOVV5DSQPQBZANCNFSM4TXJ7Z4A .

yowoda commented 3 years ago

Hmm, I would rather not do that. Thanks for answering that quickly though!

selrahc commented 3 years ago

I tried to run the model with the following code under TensorFlow 2.3.1, but have got a very inaccurate result -> There is no problem for now! Please see comments below (cf. #62 (comment)).

# !saved_model_cli show --dir ./default/detector_v2_default_checkpoint_tf --tag_set serve --all

import tensorflow as tf
from detector_utils import preprocess_image
import numpy as np

checkpoint_path = './default/detector_v2_default_checkpoint_tf'
image, scale = preprocess_image('./rga6845.jpg')
sample = np.expand_dims(image, axis=0)

with tf.compat.v1.Session() as sess:
    model = tf.compat.v1.saved_model.load(sess, ['serve'], checkpoint_path)
    x_name = model.signature_def['predict'].inputs['images'].name
    x = sess.graph.get_tensor_by_name(x_name)
    y1_name = model.signature_def['predict'].outputs['output1'].name
    y1 = sess.graph.get_tensor_by_name(y1_name)
    y2_name = model.signature_def['predict'].outputs['output2'].name
    y2 = sess.graph.get_tensor_by_name(y2_name)
    y3_name = model.signature_def['predict'].outputs['output3'].name
    y3 = sess.graph.get_tensor_by_name(y3_name)
    outPred = sess.run([y1, y2, y3], feed_dict={x: sample})
    print(outPred)

The highest score in output2 is just 0.28650957 -> 0.79144394 (cf. https://github.com/notAI-tech/NudeNet/issues/62#issuecomment-756725709).

The same inaccurate result happens when rewriting as following (in TensorFlow 2.3.1).

import tensorflow as tf
from detector_utils import preprocess_image

checkpoint_path = './default/detector_v2_default_checkpoint_tf'
classes_path = './default/classes'
image, scale = preprocess_image('./rga6845.jpg')

model = tf.saved_model.load(checkpoint_path)
infer = model.signatures['predict']
x = tf.keras.preprocessing.image.img_to_array(image)
x = x[tf.newaxis,...]
pred = infer(tf.constant(x))
print(pred)

The highest score in output2 is 0.2620039 -> 0.79144394 (cf. https://github.com/notAI-tech/NudeNet/issues/62#issuecomment-756725709).

But the result looks well when it runs in TensorFlow 1.14.0.

!pip install --upgrade nudenet
!pip install scikit-image==0.16.1

from nudenet import NudeDetector
detector = NudeDetector()
print(detector.detect('./rga6845.jpg'))

output2 contains 0.79144406 as the highest score.

That's a bit strange, and I could not understand why the results are so different among 2.3.1 and 1.14.0. Does it mean that the model is not suggested to use in TF 2.3.1, though it can be loaded and run? -> There is no problem for now! Please see comments below (cf. https://github.com/notAI-tech/NudeNet/issues/62#issuecomment-756725709).

selrahc commented 3 years ago

My bad! I forgot that I have amended detector_utils.py for testing. Once I restored detector_utils.py back to your original version, the results all look good now! I'm extremely sorry for being careless. Hope I didn't have wasted your time! And many many thanks for sharing this project. You are a giant, and thus I can stand on your shoulder to look further.

bedapudi6788 commented 3 years ago

Closing this issue, since it is resolved and newer versions of NudeNet doesn't depend on any version of tensorflow.