Samjith888 / Keras-retinanet-Training-on-custom-datasets-for-Object-Detection-

28 stars 22 forks source link

Layer conv1 was called with an input that isn't a symbolic tenso #9

Open mansi-aggarwal-2504 opened 3 years ago

mansi-aggarwal-2504 commented 3 years ago

Hi! I am getting this error when I am running the pretrained model on my dataset:

ValueError: Layer conv1 was called with an input that isn't a symbolic tensor. Received type: <class 'tensorflow.python.framework.ops.Tensor'>. Full input: [<tf.Tensor 'input_1:0' shape=(None, None, None, 3) dtype=float32>]. All inputs to the layer should be tensors.

What's the reason for this?

Samjith888 commented 3 years ago

Have you installed the keras-retinanet by using pip install keras-retinanet ? Please attach the full log?

mansi-aggarwal-2504 commented 3 years ago

Yes, I did installed keras-retinanet using pip. However, I am actually now getting this error:

(object-detection-venv) (base) Ms-MacBook-Pro:keras-retinanet-git M$ retinanet-train --weights resnet50_coco_best_v2.1.0.h5 --steps 400 --epochs 20 --snapshot-path snapshots --tensorboard-dir tensorboard csv dataset/train.csv dataset/classes.csv

Using Theano backend.
You are using tensorflow version 1.15.0. The minimum required version is 2.3.0 (blacklisted: []).
Samjith888 commented 3 years ago

Yes, I did installed keras-retinanet using pip. However, I am actually now getting this error:

(object-detection-venv) (base) Ms-MacBook-Pro:keras-retinanet-git M$ retinanet-train --weights resnet50_coco_best_v2.1.0.h5 --steps 400 --epochs 20 --snapshot-path snapshots --tensorboard-dir tensorboard csv dataset/train.csv dataset/classes.csv

Using Theano backend.
You are using tensorflow version 1.15.0. The minimum required version is 2.3.0 (blacklisted: []).

try removing the keras-retinanet installed by pip , then install it by using this repo (have updated the installation steps in the readme). Issue is that if u install keras-retinanet by using pip, then its installing the latest version where they have made lots of changes. recently they have depreacted the repo.

Alternatively you can try by upgrading tensorflow version into 2.3.0.

mansi-aggarwal-2504 commented 3 years ago

I installed keras-retinanet using the repo but I am still getting this error:

Using Theano backend.
You are using tensorflow version 1.15.0. The minimum required version is 2.3.0 (blacklisted: []).

Also tried upgrading Tensorflow to 2.3.0 but then I got this error:

`Using Theano backend.
Creating model, this may take a second...
Traceback (most recent call last):
  File "/Users/invlab/Desktop/Projects/Forest_Management/DL/Object_Detection/object-detection-venv/lib/python3.7/site-packages/keras/engine/base_layer.py", line 310, in assert_input_compatibility
    K.is_keras_tensor(x)
  File "/Users/invlab/Desktop/Projects/Forest_Management/DL/Object_Detection/object-detection-venv/lib/python3.7/site-packages/keras/backend/theano_backend.py", line 225, in is_keras_tensor
    str(type(x)) + '`. '
ValueError: Unexpectedly found an instance of type `<class 'tensorflow.python.framework.ops.Tensor'>`. Expected a symbolic tensor instance.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "xxx/object-detection-venv/bin/retinanet-train", line 8, in <module>
    sys.exit(main())
  File "xxx/lib/python3.7/site-packages/keras_retinanet/bin/train.py", line 513, in main
    config=args.config
  File "xxx/object-detection-venv/lib/python3.7/site-packages/keras_retinanet/bin/train.py", line 115, in create_models
    model          = model_with_weights(backbone_retinanet(num_classes, num_anchors=num_anchors, modifier=modifier, pyramid_levels=pyramid_levels), weights=weights, skip_mismatch=True)
  File "xxx/object-detection-venv/lib/python3.7/site-packages/keras_retinanet/models/resnet.py", line 37, in retinanet
    return resnet_retinanet(*args, backbone=self.backbone, **kwargs)
  File "xxx/object-detection-venv/lib/python3.7/site-packages/keras_retinanet/models/resnet.py", line 98, in resnet_retinanet
    resnet = keras_resnet.models.ResNet50(inputs, include_top=False, freeze_bn=True)
  File "xxx/object-detection-venv/lib/python3.7/site-packages/keras_resnet/models/_2d.py", line 242, in __init__
    **kwargs
  File "xxx/object-detection-venv/lib/python3.7/site-packages/keras_resnet/models/_2d.py", line 76, in __init__
    x = keras.layers.Conv2D(64, (7, 7), strides=(2, 2), use_bias=False, name="conv1", padding="same")(inputs)
  File "xxx/object-detection-venv/lib/python3.7/site-packages/keras/engine/base_layer.py", line 446, in __call__
    self.assert_input_compatibility(inputs)
  File "xxx/object-detection-venv/lib/python3.7/site-packages/keras/engine/base_layer.py", line 316, in assert_input_compatibility
    str(inputs) + '. All inputs to the layer '
ValueError: Layer conv1 was called with an input that isn't a symbolic tensor. Received type: <class 'tensorflow.python.framework.ops.Tensor'>. Full input: [<tf.Tensor 'input_1:0' shape=(None, None, None, 3) dtype=float32>]. All inputs to the layer should be tensors.
`
Samjith888 commented 3 years ago

Able to train by using following versions

python version : 3.6.1
tensorflow : 1.15.0
keras: 2.3.1
keras-retinanet: 0.5.1

Please paste the above packages versions that you have used

mansi-aggarwal-2504 commented 3 years ago

I guess I have to downgrade python (missed it in your readme) and keras-retinanet. Should I pip install keras-retinanet?

python version : 3.7.6
tensorflow : 1.15.0
keras: 2.3.1
keras-retinanet: 1.0.0
Samjith888 commented 3 years ago

I guess I have to downgrade python (missed it in your readme) and keras-retinanet. Should I pip install keras-retinanet?

python version : 3.7.6
tensorflow : 1.15.0
keras: 2.3.1
keras-retinanet: 1.0.0

Downgrade the python version. and install the keras-retinanet older version by using following command

pip install keras-retinanet==0.5.1

mansi-aggarwal-2504 commented 3 years ago

Did all that and getting a new error:

ImportError: cannot import name 'meshgrid'

Complete log:

$ retinanet-train --weights resnet50_coco_best_v2.1.0.h5 --steps 400 --epochs 20 --snapshot-path snapshots --tensorboard-dir tensorboard csv dataset/train.csv dataset/classes.csv --val-annotations dataset/test.csv
Using Theano backend.
Traceback (most recent call last):
  File "XXX/Object_Detection/retinanet-venv/bin/retinanet-train", line 6, in <module>
    from keras_retinanet.bin.train import main
  File "XXX/Object_Detection/retinanet-venv/lib/python3.6/site-packages/keras_retinanet/bin/train.py", line 35, in <module>
    from .. import layers  # noqa: F401
  File "XXX/Object_Detection/retinanet-venv/lib/python3.6/site-packages/keras_retinanet/layers/__init__.py", line 1, in <module>
    from ._misc import RegressBoxes, UpsampleLike, Anchors, ClipBoxes  # noqa: F401
  File "XXX/Object_Detection/retinanet-venv/lib/python3.6/site-packages/keras_retinanet/layers/_misc.py", line 18, in <module>
    from .. import backend
  File "XXX/Object_Detection/retinanet-venv/lib/python3.6/site-packages/keras_retinanet/backend/__init__.py", line 2, in <module>
    from .common import *   # noqa: F401,F403
  File "XXX/Object_Detection/retinanet-venv/lib/python3.6/site-packages/keras_retinanet/backend/common.py", line 18, in <module>
    from .dynamic import meshgrid
ImportError: cannot import name 'meshgrid'
Samjith888 commented 3 years ago

Seems version mismatch. Try the following steps

#create new conda environment
conda create -n retina python=3.6 anaconda
conda activate retina

#Install cuda and cudnn  if you are using gpu
conda install -c anaconda cudatoolkit
conda install -c anaconda cudnn

#install following project dependencies 
pip install tensorflow-gpu==1.15.0
pip install Keras==2.3.1
pip install keras-retinanet==0.5.1
keras-resnet==0.2.0
pip install git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI
pip install opencv-python
pip install h5py

If you still getting any errors then feel free to mail me.

mansi-aggarwal-2504 commented 3 years ago

Thank you so much. It's working now. 👍

Screenshot 2020-10-29 at 10 23 14 AM
mansi-aggarwal-2504 commented 3 years ago

I am using the same environment and package versions to test an image, using people_detection_image.py But I am getting the previous error: ValueError: Layer bn_conv1 was called with an input that isn't a symbolic tensor. Received type: <class 'tensorflow.python.framework.ops.Tensor'>. Full input: [<tf.Tensor 'conv1/Conv2D:0' shape=(?, ?, ?, 64) dtype=float32>]. All inputs to the layer should be tensors.

Samjith888 commented 3 years ago

Have you converted the trained model for inference ?

Model convertion

retinanet-convert-model <path/to/desired/snapshot.h5> <path/to/output/model.h5>

mansi-aggarwal-2504 commented 3 years ago

Sorry about the late reply. Yes I had converted the trained model (saved in snapshots) for inference