Giter VIP home page Giter VIP logo

tf-retinanet's Introduction

TF RetinaNet

Tensorflow Keras implementation of RetinaNet object detection as described in Focal Loss for Dense Object Detection by Tsung-Yi Lin, Priya Goyal, Ross Girshick, Kaiming He and Piotr Dollár.

Disclaimer

The repository is still work in progress. Same results as keras-retinanet are not yet achieved in this repository. Any help will be welcomed.

TODO's

  • Train properly in order to achieve the same results as keras-retinanet.
  • Update jupyter notebook.
  • Benchmark network speed.

Components

The tf-retinanet project has been designed to be modular. The following components are part of the project:

Installation

  1. Clone this repository.
  2. Ensure numpy is installed using pip install numpy --user
  3. In the repository, execute pip install . --user. Note that due to inconsistencies with how tensorflow should be installed, this package does not define a dependency on tensorflow as it will try to install that (which at least on Arch Linux results in an incorrect installation). Please make sure tensorflow is installed as per your systems requirements.
  4. Alternatively, you can run the code directly from the cloned repository, however you need to run python setup.py build_ext --inplace to compile Cython code first.
  5. Optionally, install pycocotools if you want to train / test on the MS COCO dataset by running pip install --user git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI.

Testing

In general, inference of the network works as follows:

boxes, scores, labels = model.predict_on_batch(inputs)

Where boxes are shaped (None, None, 4) (for (x1, y1, x2, y2)), scores is shaped (None, None) (classification score) and labels is shaped (None, None) (label corresponding to the score). In all three outputs, the first dimension represents the shape and the second dimension indexes the list of detections.

Loading models can be done in the following manner:

from tf_retinanet.models import load_model
model = load_model('/path/to/model.h5', backbone=backbone)

Converting a training model to inference model

The training procedure of tf-retinanet works with training models. These are stripped down versions compared to the inference model and only contains the layers necessary for training (regression and classification values). If you wish to do inference on a model (perform object detection on an image), you need to convert the trained model to an inference model. This is done as follows:

# Running directly from the repository:
tf_retinanet/bin/convert_model.py /path/to/training/model.h5 /path/to/save/inference/model.h5 --config /path/to/config_file.yaml

# Using the installed script:
retinanet-convert-model /path/to/training/model.h5 /path/to/save/inference/model.h5 --config /path/to/config_file.yaml

Most scripts (like retinanet-evaluate) also support converting on the fly, using the --convert-model argument.

Training

tf-retinanet can be trained using this script. Note that the train script uses relative imports since it is inside the tf_retinanet package. If you want to adjust the script for your own use outside of this repository, you will need to switch it to use absolute imports.

If you installed tf-retinanet correctly, the train script will be installed as retinanet-train. However, if you make local modifications to the tf-retinanet repository, you should run the script directly from the repository. That will ensure that your local changes will be used by the train script.

Usage

Anchor optimization

In some cases, the default anchor configuration is not suitable for detecting objects in your dataset, for example, if your objects are smaller than the 32x32px (size of the smallest anchors). In this case, it might be suitable to modify the anchor configuration, this can be done automatically by following the steps in the anchor-optimization repository. To use the generated configuration check the sample train.yaml file.

Debugging

Creating your own dataset does not always work out of the box. There is a debug.py tool to help find the most common mistakes.

Particularly helpful is the --annotations flag which displays your annotations on the images from your dataset. Annotations are colored in green when there are anchors available and colored in red when there are no anchors available. If an annotation doesn't have anchors available, it means it won't contribute to training. It is normal for a small amount of annotations to show up in red, but if most or all annotations are red there is cause for concern. The most common issues are that the annotations are too small or too oddly shaped (stretched out).

Results

MS COCO

Status

Example output images using tf-retinanet are shown below.

Example result of RetinaNet on MS COCO Example result of RetinaNet on MS COCO Example result of RetinaNet on MS COCO

Projects using keras-retinanet, the ancestor of tf-retinanet.

If you have a project based on tf-retinanet or keras-retinanet and would like to have it published here, shoot me a message on Slack.

Notes

  • This repository requires Tensorflow 2.0 or higher.

Contributions to this project are welcome.

Discussions

Feel free to join the #keras-retinanet Keras Slack channel for discussions and questions.

FAQ

  • I get the warning UserWarning: No training configuration found in save file: the model was not compiled. Compile it manually., should I be worried? This warning can safely be ignored during inference.
  • I get the error ValueError: not enough values to unpack (expected 3, got 2) during inference, what to do?. This is because you are using a train model to do inference. See https://github.com/fizyr/keras-retinanet#converting-a-training-model-to-inference-model for more information.
  • How do I do transfer learning? The easiest solution is to use the --weights argument when training. Keras will load models, even if the number of classes don't match (it will simply skip loading of weights when there is a mismatch). Run for example retinanet-train --weights snapshots/some_coco_model.h5 pascal /path/to/pascal to transfer weights from a COCO model to a PascalVOC training session. If your dataset is small, you can also use the --freeze-backbone argument to freeze the backbone layers.
  • How do I change the number / shape of the anchors? The train tool allows to pass a configuration file, where the anchor parameters can be adjusted. Check here for an example config file.
  • I get a loss of 0, what is going on? This mostly happens when none of the anchors "fit" on your objects, because they are most likely too small or elongated. You can verify this using the debug tool.
  • I have an older model, can I use it after an update of tf-retinanet? This depends on what has changed. If it is a change that doesn't affect the weights then you can "update" models by creating a new retinanet model, loading your old weights using model.load_weights(weights_path, by_name=True) and saving this model. If the change has been too significant, you should retrain your model (you can try to load in the weights from your old model when starting training, this might be a better starting position than ImageNet).
  • I get the error ModuleNotFoundError: No module named 'tf_retinanet.utils.compute_overlap', how do I fix this? Most likely you are running the code from the cloned repository. This is fine, but you need to compile some extensions for this to work (python setup.py build_ext --inplace).
  • How do I train on my own dataset? The steps to train on your dataset are roughly as follows:
    1. Prepare your dataset in the CSV format (a training and validation split is advised).
    1. Check that your dataset is correct using retinanet-debug.
    1. Train retinanet, preferably using the pretrained COCO weights (this gives a far better starting point, making training much quicker and accurate). You can optionally perform evaluation of your validation set during training to keep track of how well it performs (advised).
    1. Convert your training model to an inference model.
    1. Evaluate your inference model on your test or validation set.
    1. Profit!

tf-retinanet's People

Contributors

enricoliscio avatar hgaiser avatar vcarpani avatar vsuryamurthy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tf-retinanet's Issues

how to train on my own dataset?

Please make sure that you follow the steps below when creating an issue.
Only use GitHub issues for issues with the implementation, not for issues with specific datasets or general questions about functionality.
If your issue is an implementation question, please ask your question on the #keras-retinanet Slack channel instead of filing a GitHub issue.
You can find directions for the Slack channel here: https://github.com/fizyr/keras-retinanet#discussions

Thank you!

  1. Check that you are up-to-date with the master branch of tf-retinanet.
  2. Check that you are up-to-date with the latest version of TensorFlow.
    The installation instructions can be found here: https://www.tensorflow.org/get_started/os_setup.
  3. Check that you have read the entire README.md: https://github.com/fizyr/tf-retinanet/README.md.
    Most noticably the FAQ section shows common issues: https://github.com/fizyr/tf-retinanet#faq.
  4. Clearly describe the issues you're having including the expected behaviour, the actual behaviour
    and the steps required to trigger the issue.
  5. Include relevant output from the commands you're executing, including full stack traces where relevant.
  6. Remove this entire message and replace it with your issue.

Error in importing backbone package

Hi,

I did a clean install of tf-retinanet together with its backbone and generator while following the written instructions.
After running the train.py from tf-retinanet/bin using Pycharm CE

i get the following error

/Users/jsoncunanan/opt/anaconda3/envs/tf-retinanet/bin/python /Users/jsoncunanan/Documents/GitHub/tf-retinanet/tf_retinanet/bin/train.py Traceback (most recent call last): File "/Users/jsoncunanan/Documents/GitHub/tf-retinanet/tf_retinanet/bin/train.py", line 172, in <module> main() File "/Users/jsoncunanan/Documents/GitHub/tf-retinanet/tf_retinanet/bin/train.py", line 94, in main backbone = get_backbone(config['backbone']) File "/Users/jsoncunanan/Documents/GitHub/tf-retinanet/tf_retinanet/bin/../../tf_retinanet/backbones/__init__.py", line 71, in get_backbone backbone_pkg = import_package(config['name'], 'tf_retinanet_backbones') KeyError: 'name'

probably a simple fix but I am really new to this.
Thank you in advance.

Resnet50 implementation Problem

Hi,
My own dataset for used Resnet50 backbone model. But run train.py by implementation error:
File "/Users/servercalap/tf-retinanet-master/tf_retinanet/bin/../../tf_retinanet/utils/init.py", line 20, in import_package
raise Exception(name + ' is not present in ' + package_type) from e
Exception: tf_retinanet_backbones is not present in tf_retinanet_backbones

Thank you , helping me.

Loading/running model trained & saved with `keras_retinanet` (TF 1.14) using `tf_retinanet` (TF 2.0)

Environment:
Ubuntu: 18.04
Tensorflow 2.0

I am loading the model trained/saved using keras_retinanet into tf_retinanet with the following code:

from tf_retinanet import models
from tf_retinanet.utils.config import set_defaults
from tf_retinanet.backbones import get_backbone
from my_defaults import default_evaluation_config  # changed a bit default evaluation configs

from _batch_normalization import BatchNormalization # copied from keras_resnet package  

model_path = 'my_old_retinanet_model_trained_with_keras_retinanet.h5'

retinanet_config = set_defaults({}, submodels_manager = models.submodels.SubmodelsManager(retinanet_config['submodels'])
submodels_manager.create(num_classes=80)

backbone = get_backbone(retinanet_config['backbone'])
self.model = models.load_model(model_path,
                                       backbone=backbone,
                                       submodels=submodels_manager.get_submodels(),
                                       custom_objects={
                                           'BatchNormalization': BatchNormalization
                                       })

Model loading goes fine, so I try to make inference using the model loaded:

image = cv2.imread('image.png')
data = np.expand_dims(image, axis=0)
result = model.predict(tf.constant(data)) # output is the same with or without tf.constant

model.predict fails with the following error:

2020-03-19 17:57:13.300204: I tensorflow/core/platform/cpu_feature_guard.cc:145] This TensorFlow binary is optimized with Intel(R) MKL-DNN to use the following CPU instructions in performance critical operations:  SSE4.1 SSE4.2 AVX AVX2 FMA
To enable them in non-MKL-DNN operations, rebuild TensorFlow with the appropriate compiler flags.
2020-03-19 17:57:13.324100: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2304000000 Hz
2020-03-19 17:57:13.325293: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x560dbf0e55c0 executing computations on platform Host. Devices:
2020-03-19 17:57:13.325337: I tensorflow/compiler/xla/service/service.cc:175]   StreamExecutor device (0): Host, Default Version
2020-03-19 17:57:13.325871: I tensorflow/core/common_runtime/process_util.cc:115] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance.
WARNING:tensorflow:No training configuration found in save file: the model was *not* compiled. Compile it manually.
2020-03-19 17:57:25.807059: W tensorflow/core/framework/op_kernel.cc:1622] OP_REQUIRES failed at strided_slice_op.cc:108 : Invalid argument: slice index 1 of dimension 0 out of bounds.
2020-03-19 17:57:25.807120: W tensorflow/core/common_runtime/base_collective_executor.cc:216] BaseCollectiveExecutor::StartAbort Invalid argument: slice index 1 of dimension 0 out of bounds.
         [[{{node anchors_3/strided_slice}}]]
2020-03-19 17:57:25.820325: W tensorflow/core/framework/op_kernel.cc:1622] OP_REQUIRES failed at strided_slice_op.cc:108 : Invalid argument: slice index 1 of dimension 0 out of bounds.
2020-03-19 17:57:25.855884: W tensorflow/core/framework/op_kernel.cc:1622] OP_REQUIRES failed at strided_slice_op.cc:108 : Invalid argument: slice index 1 of dimension 0 out of bounds.
2020-03-19 17:57:26.122898: W tensorflow/core/framework/op_kernel.cc:1622] OP_REQUIRES failed at strided_slice_op.cc:108 : Invalid argument: slice index 1 of dimension 0 out of bounds.
2020-03-19 17:57:26.124582: W tensorflow/core/framework/op_kernel.cc:1622] OP_REQUIRES failed at strided_slice_op.cc:108 : Invalid argument: slice index 1 of dimension 0 out of bounds.
Traceback (most recent call last):
  File "model_loading_test.py", line 33, in <module>
    result = model.predict(tf.constant(data))
  File "/home/andrii/miniconda3/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py", line 909, in predict
    use_multiprocessing=use_multiprocessing)
  File "/home/andrii/miniconda3/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_arrays.py", line 722, in predict
    callbacks=callbacks)
  File "/home/andrii/miniconda3/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_arrays.py", line 393, in model_iteration
    batch_outs = f(ins_batch)
  File "/home/andrii/miniconda3/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/keras/backend.py", line 3740, in __call__
    outputs = self._graph_fn(*converted_inputs)
  File "/home/andrii/miniconda3/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 1081, in __call__
    return self._call_impl(args, kwargs)
  File "/home/andrii/miniconda3/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 1121, in _call_impl
    return self._call_flat(args, self.captured_inputs, cancellation_manager)
  File "/home/andrii/miniconda3/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 1224, in _call_flat
    ctx, args, cancellation_manager=cancellation_manager)
  File "/home/andrii/miniconda3/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 511, in call
    ctx=ctx)
  File "/home/andrii/miniconda3/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/eager/execute.py", line 67, in quick_execute
    six.raise_from(core._status_to_exception(e.code, message), None)
  File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.InvalidArgumentError:  slice index 1 of dimension 0 out of bounds.
         [[node anchors_3/strided_slice (defined at /home/andrii/miniconda3/envs/tf2/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py:1751) ]] [Op:__inference_keras_scratch_graph_14306]

Function call stack:
keras_scratch_graph

Input shape seems to be fine. What could be the reason of this error?

  • This code was ran on CPU-only machine, but I also tried to execute it on GPU machine with similar CUDA-based environment. On that machine I got the same error.

[TF2] Utility function for submodels search in other packages

Make a utility function to do the following operation:

try:
    submodel_pkg = __import__('tf_retinanet_submodels', fromlist=[submodel['category']])
    submodel_pkg = getattr(submodel_pkg, submodel['category'])
except ImportError:
    raise ValueError(submodel['category'] + 'is not a valid submodel')
submodel['class'] = submodel_pkg.parse_submodel(submodel['details'])

[TF2] Add missing unit tests

In the keras-retinanet implementation there are some unit tests which have not been ported due to the restructuring that has been made, especially under utils.

Doubt on calculation of smooth_l1 loss

In this line, according to the coment the code is supposed to filter out the samples marked as "ignore" state. What it does, instead, is extracting only the positive samples.

Shouldn't the line be: indices = tf.where(tf.keras.backend.not_equal(anchor_state, -1)) (or an equivalent version using >= 0)?

Unless I'm missing something, the network seems to be training only on positive samples for the regression branch.

tflite

I am trying to convert a model to tflite. Is the model capable of this?

tflite_convert --keras_model_file=path --output_file=output_file

Error ValueError: Unknown layer: UpsampleLike

Question: Implementing ResnetV2 backbones

Just a quick question so if I'm on the right track. I'm implementing some additions to your library (usage of csv generators, additional callbacks, continuing training from a saved model etc.). Currently I'm working on adding a support for ResnetV2 backbones and I noticed that the library doesn't exactly work as it is with resnetv2 architecture.
I know that you intended the library to be modular but since you're supposed to use the resnet output layers (activation layers) with the classification, regression and bbox models it doesn't exactly work out of the box with resnetv2 IMO.
For f.ex. with resnet50v2 architecture I've specified the output layers for resnet50v2["conv3_block4_2_relu", "conv4_block6_2_relu", "conv5_block2_2_relu"] vs resnet50 ["conv3_block4_out", "conv4_block6_out", "conv5_block3_out"].
Seems that the submodels are outputting 2d data with the resnet50v2 architecture so the losses don't work.
I'm kind of new with this subject and tried looking for answers in the keras-retinanet package, but didn't quite figure out if the problem is with the output layers, the fpn.py or with the losses.py itself so I'm asking if you have already perhaps implemented these backends or could point me to the right direction.
Thank you!

Train on costume dataset

Hi,
I have read here :

  • Prepare your dataset in the CSV format (a training and validation split is advised).
    And considering it seems there is only a genereator for COCO, how would I use my own dataset?

Thanks,
Bruno

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.