GantMan / nsfw_model

Keras model of NSFW detector
Other
1.75k stars 273 forks source link

expected shape=(None, 299, 299, 3), found shape=(None, 224, 224, 3) but image is really (1, 299, 299, 3) #138

Open wanghaifengnihaoa opened 1 year ago

wanghaifengnihaoa commented 1 year ago

use:nsfw-predict --saved_model_path ./nsfw.299x299.h5 --image_source images/色情1-2.jpeg

but:

Metal device set to: Apple M1

systemMemory: 16.00 GB
maxCacheSize: 5.33 GB

images/色情1-2.jpeg size: (224, 224)
Traceback (most recent call last):
  File "/Users/wanghaifeng/miniconda3/envs/tensorflow/bin/nsfw-predict", line 8, in <module>
    sys.exit(main())
  File "/Users/wanghaifeng/miniconda3/envs/tensorflow/lib/python3.10/site-packages/nsfw_detector/predict.py", line 109, in main
    image_preds = classify(model, config['image_source'], config['image_dim'])
  File "/Users/wanghaifeng/miniconda3/envs/tensorflow/lib/python3.10/site-packages/nsfw_detector/predict.py", line 64, in classify
    probs = classify_nd(model, images)
  File "/Users/wanghaifeng/miniconda3/envs/tensorflow/lib/python3.10/site-packages/nsfw_detector/predict.py", line 71, in classify_nd
    model_preds = model.predict(nd_images)
  File "/Users/wanghaifeng/miniconda3/envs/tensorflow/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/var/folders/wn/r9w735dn2f958w3hhv5_2th00000gn/T/__autograph_generated_file_3gw2_vs.py", line 15, in tf__predict_function
    retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
ValueError: in user code:

    File "/Users/wanghaifeng/miniconda3/envs/tensorflow/lib/python3.10/site-packages/keras/src/engine/training.py", line 2341, in predict_function  *
        return step_function(self, iterator)
    File "/Users/wanghaifeng/miniconda3/envs/tensorflow/lib/python3.10/site-packages/keras/src/engine/training.py", line 2327, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/Users/wanghaifeng/miniconda3/envs/tensorflow/lib/python3.10/site-packages/keras/src/engine/training.py", line 2315, in run_step  **
        outputs = model.predict_step(data)
    File "/Users/wanghaifeng/miniconda3/envs/tensorflow/lib/python3.10/site-packages/keras/src/engine/training.py", line 2283, in predict_step
        return self(x, training=False)
    File "/Users/wanghaifeng/miniconda3/envs/tensorflow/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py", line 70, in error_handler
        raise e.with_traceback(filtered_tb) from None
    File "/Users/wanghaifeng/miniconda3/envs/tensorflow/lib/python3.10/site-packages/keras/src/engine/input_spec.py", line 298, in assert_input_compatibility
        raise ValueError(

    ValueError: Input 0 of layer "model_1" is incompatible with the layer: expected shape=(None, 299, 299, 3), found shape=(None, 224, 224, 3)

get shape:

import numpy as np
from PIL import Image

image = Image.open("images/色情1-2.jpeg")
print("image_size:", image.size)
image_array = np.array(image)
image_array = np.expand_dims(image_array, axis=0)
print("image_shape:", image_array.shape)

is:

image_size: (299, 299)
image_shape: (1, 299, 299, 3)
FurkanGozukara commented 1 year ago

i have the same error lol

made a new venv python 3.10.9

installed requirements

then installed

Successfully installed protobuf-3.20.2

(venv) F:\768 1024 realistic man class dataset\nsfw_model>python fix.py
F:\768 1024 realistic man class dataset\raw\00000-102817736.png size: (224, 224)
Traceback (most recent call last):
  File "F:\768 1024 realistic man class dataset\nsfw_model\fix.py", line 31, in <module>
    main(source_folder, dest_folder, model_path)
  File "F:\768 1024 realistic man class dataset\nsfw_model\fix.py", line 24, in main
    if filename.lower().endswith(('.png', '.jpg', '.jpeg')) and is_nsfw(file_path, model):
  File "F:\768 1024 realistic man class dataset\nsfw_model\fix.py", line 7, in is_nsfw
    pred = predict.classify(model, img_path)
  File "F:\768 1024 realistic man class dataset\nsfw_model\nsfw_detector\predict.py", line 67, in classify
    probs = classify_nd(model, images, predict_args)
  File "F:\768 1024 realistic man class dataset\nsfw_model\nsfw_detector\predict.py", line 77, in classify_nd
    model_preds = model.predict(nd_images, **predict_args)
  File "F:\768 1024 realistic man class dataset\nsfw_model\venv\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "C:\Users\King\AppData\Local\Temp\__autograph_generated_filej3ztbmn8.py", line 15, in tf__predict_function
    retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
ValueError: in user code:

    File "F:\768 1024 realistic man class dataset\nsfw_model\venv\lib\site-packages\keras\engine\training.py", line 2169, in predict_function  *
        return step_function(self, iterator)
    File "F:\768 1024 realistic man class dataset\nsfw_model\venv\lib\site-packages\keras\engine\training.py", line 2155, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "F:\768 1024 realistic man class dataset\nsfw_model\venv\lib\site-packages\keras\engine\training.py", line 2143, in run_step  **
        outputs = model.predict_step(data)
    File "F:\768 1024 realistic man class dataset\nsfw_model\venv\lib\site-packages\keras\engine\training.py", line 2111, in predict_step
        return self(x, training=False)
    File "F:\768 1024 realistic man class dataset\nsfw_model\venv\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
        raise e.with_traceback(filtered_tb) from None
    File "F:\768 1024 realistic man class dataset\nsfw_model\venv\lib\site-packages\keras\engine\input_spec.py", line 298, in assert_input_compatibility
        raise ValueError(

    ValueError: Input 0 of layer "model_1" is incompatible with the layer: expected shape=(None, 299, 299, 3), found shape=(None, 224, 224, 3)
szeitlin commented 1 year ago

ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 224, 224, 3), found shape=(32, 224, 3)

I doublechecked that image.shape is (224, 224, 3)

I'm running predict.classify_nd(model, image)

Puzzled. Did either of you ever figure this out? It seems to work fine for me if I run it on a file from the local filesystem, but I need to run from bytearrays, so I'm converting via PIL to numpy and I'm having trouble figuring out what it's trying to do that's mangling the shape.

hua1995116 commented 1 year ago

usage example:

# Predict single image with nsfw.299x299.h5
model = predict.load_model('./nsfw.299x299.h5')
predict.classify(model, '2.jpg', 299)
# {'2.jpg': {'sexy': 4.3454795e-05, 'neutral': 0.00026579312, 'porn': 0.0007733498, 'hentai': 0.14751942, 'drawings': 0.8513979}, '6.jpg': {'drawings': 0.004214506, 'hentai': 0.013342537, 'neutral': 0.01834045, 'porn': 0.4431829, 'sexy': 0.5209196}}

I've added a usage example in the readme with pr https://github.com/GantMan/nsfw_model/pull/141

Priyamwa commented 7 months ago

ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 224, 224, 3), found shape=(32, 224, 3)

I doublechecked that image.shape is (224, 224, 3)

I'm running predict.classify_nd(model, image)

Puzzled. Did either of you ever figure this out? It seems to work fine for me if I run it on a file from the local filesystem, but I need to run from bytearrays, so I'm converting via PIL to numpy and I'm having trouble figuring out what it's trying to do that's mangling the shape.

have you found the solution for, i am having same issue

Priyamwa commented 7 months ago

ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 224, 224, 3), found shape=(32, 224, 3) I doublechecked that image.shape is (224, 224, 3) I'm running predict.classify_nd(model, image) Puzzled. Did either of you ever figure this out? It seems to work fine for me if I run it on a file from the local filesystem, but I need to run from bytearrays, so I'm converting via PIL to numpy and I'm having trouble figuring out what it's trying to do that's mangling the shape.

have you found the solution for, i am having same issue

got it, changed the img to be predicted from tuple to list