Giter VIP home page Giter VIP logo

tf-faster-rcnn's Introduction

Faster-RCNN

This is tensorflow Faster-RCNN implementation from scratch supporting to the batch processing. All methods are tried to be created in the simplest way for easy understanding. Most of the operations performed during the implementation were carried out as described in the paper and tf-rpn repository.

It's implemented and tested with tensorflow 2.0.

MobileNetV2 and VGG16 backbones are supported.

Usage

Project models created in virtual environment using miniconda. You can also create required virtual environment with conda.

To create virtual environment (tensorflow-2 gpu environment):

conda env create -f environment.yml

There are two different backbone, first one the legacy vgg16 backbone and the second and default one is mobilenet_v2. You can easily specify the backbone to be used with the --backbone parameter. Default backbone is mobilenet_v2.

To train and test Faster-RCNN model:

python faster_rcnn_trainer.py --backbone mobilenet_v2
python faster_rcnn_predictor.py --backbone mobilenet_v2

You can also train and test RPN alone:

python rpn_trainer.py --backbone vgg16
python rpn_predictor.py --backbone vgg16

If you have GPU issues you can use -handle-gpu flag with these commands:

python faster_rcnn_trainer.py -handle-gpu

Examples

Trained with VOC 0712 trainval data
Man riding bike
Photo by William Peynichou on Unsplash
Airplanes
Photo by Vishu Gowda on Unsplash

References

tf-faster-rcnn's People

Contributors

furkanom 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tf-faster-rcnn's Issues

No such file or directory

hello,i want to running this code,but have a error,this is

tensorflow_datasets.core.download.extractor.ExtractError: Error while extracting C:\Users\yzk/tensorflow_datasets\downloads\pjreddi.com_media_files_VOCtest_6-Nov-2007-uyfHB4aCy4ZRNbxEOl0zsGlfk4orDpuRRs8mcmQyGk.tar to C:\Users\yzk/tensorflow_datasets\downloads\extracted\TAR.pjreddi.com_media_files_VOCtest_6-Nov-2007-uyfHB4aCy4ZRNbxEOl0zsGlfk4orDpuRRs8mcmQyGk.tar (file: VOCdevkit\VOC2007\Annotations\000001.xml) : Failed to create a directory: C:\Users\yzk/tensorflow_datasets\downloads\extracted\TAR.pjreddi.com_media_files_VOCtest_6-Nov-2007-uyfHB4aCy4ZRNbxEOl0zsGlfk4orDpuRRs8mcmQyGk.tar.incomplete_60a23fd8d1924f2dbe3005c1dcb96f47\VOCdevkit\VOC2007\Annotations; No such file or directory

i don`t know deal with this error.
think you!!!!

ValueError: Dimension 2 in both shapes must be equal, but are 4 and 1. Shapes are [?,1500,4] and [?,1500,1]. for 'roi_deltas/Select' (op: 'Select') with input shapes: [?,1500,1], [?,1500,4], [?,1500,4].

Instructions for updating:
Use tf.where in 2.0, which has the same broadcast rule as np.where
Traceback (most recent call last):
File "C:/Users/famer/PycharmProjects/tf-faster-rcnn/faster_rcnn_trainer.py", line 53, in
frcnn_model = faster_rcnn.get_model(feature_extractor, rpn_model, anchors, hyper_params)
File "C:\Users\famer\PycharmProjects\tf-faster-rcnn\models\faster_rcnn.py", line 254, in get_model
[roi_bboxes, input_gt_boxes, input_gt_labels])
File "C:\Users\famer\anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 854, in call
outputs = call_fn(cast_inputs, *args, **kwargs)
File "C:\Users\famer\anaconda3\lib\site-packages\tensorflow_core\python\autograph\impl\api.py", line 237, in wrapper
raise e.ag_error_metadata.to_exception(e)
ValueError: in converted code:
relative to C:\Users\famer:

PycharmProjects\tf-faster-rcnn\models\faster_rcnn.py:162 call  *
    expanded_gt_boxes = tf.where(tf.expand_dims(pos_mask, axis=-1), gt_boxes_map, tf.zeros_like(gt_boxes_map))
anaconda3\lib\site-packages\tensorflow_core\python\util\deprecation.py:324 new_func
    return func(*args, **kwargs)
anaconda3\lib\site-packages\tensorflow_core\python\util\dispatch.py:180 wrapper
    return target(*args, **kwargs)
anaconda3\lib\site-packages\tensorflow_core\python\ops\array_ops.py:3759 where
    return gen_math_ops.select(condition=condition, x=x, y=y, name=name)
anaconda3\lib\site-packages\tensorflow_core\python\ops\gen_math_ops.py:9439 select
    "Select", condition=condition, t=x, e=y, name=name)
anaconda3\lib\site-packages\tensorflow_core\python\framework\op_def_library.py:794 _apply_op_helper
    op_def=op_def)
anaconda3\lib\site-packages\tensorflow_core\python\util\deprecation.py:507 new_func
    return func(*args, **kwargs)
anaconda3\lib\site-packages\tensorflow_core\python\framework\ops.py:3357 create_op
    attrs, op_def, compute_device)
anaconda3\lib\site-packages\tensorflow_core\python\framework\ops.py:3426 _create_op_internal
    op_def=op_def)
anaconda3\lib\site-packages\tensorflow_core\python\framework\ops.py:1770 __init__
    control_input_ops)
anaconda3\lib\site-packages\tensorflow_core\python\framework\ops.py:1610 _create_c_op
    raise ValueError(str(e))

ValueError: Dimension 2 in both shapes must be equal, but are 4 and 1. Shapes are [?,1500,4] and [?,1500,1]. for 'roi_deltas/Select' (op: 'Select') with input shapes: [?,1500,1], [?,1500,4], [?,1500,4].

tensorflow version : 2.0
python version : 3.7.6

I think this error occur at tf.where because update tensorflow

Question about epochs and learning rate selection

Hi,

Thanks for making this repo available. I've spent quite a bit of time implementing my own version of FasterRCNN in both PyTorch and TF2/Keras and have noticed that the latter is very difficult to get working with the same degree of performance and reliability as PyTorch.

I notice that you train for a large number of epochs (50) with a small learning rate (1e-5). Was this determined empirically? The original paper uses about 16 epochs (12 at lr=1e-3 and 4 at lr=1e-4), but this was with Caffe. My PyTorch model is capable of converging to the same mean average precision as the paper using 10 + 4 epochs.

However, with TF2/Keras, I've had to use aggressive gradient norm clipping to train at these learning rates and must increase the number of epochs drastically (I am still trying to determine the optimal number but it is at least 30).

Just wanted to confirm that you saw the same sort of instability during training that I saw when following the paper's recipe (lr=1e-3)?

Thank you,

Bart

Erorr in reg_loss

1- AssertionError: Could not compute output Tensor("rpn_reg_loss/truediv:0", shape=(), dtype=float32) in faster rcnn
2-Incompatible shapes: [8,9216] vs. [8]
[[node gradient_tape/reg_loss/mul/BroadcastGradientArgs (defined at e:/faster_rcnn/rpn_trainer.py:60) ]] [Op:__inference_train_function_12539] in rpn alone

Bounding box resizing in preprocessing??

Hi,
The code you did for Faster RCNN was really amazing, but in preprocessing function after you resized the image, I did not find where have you handled the bbox for the following resized image, Please know whether its there on the code or not??

Thank you in advance
K Rohan Varma

Training strategy

Hi, I'm now facing a problem when training on your code.
I have trained about 37 epochs on PASCAL VOC 2007 trainval dataset directly, and it turns out no bounding box appeared on test image.
I wonder what is your training procedure, for example, changing learning rate value or training RPN and Regression layers alternatively.
Thanks a lot!!!

数据集下载

dataset, info = tfds.load("voc/2007", split="train+validation",data_dir="D:/faster_voc2007",with_info=True),
一直提示创建不了目录
Failed to create a directory: D:/faster_voc2007\voc2007\1.0.0.incompleteSHSNAR; No such file or directory

Data Request

Hi FurkanOM,
Would you be able to upload your pipeline.config?
I cant make out what feature_extractor you're using and weather it is locked or training.

Anchors and rpn module for varied image sizes (width!=height)

Hi FurkanOM, Thanks for the great work!!
This repository works only for square images. What if i want to keep the aspect ratio of my images and train, like 1280x720. Then the anchor generation and anchor target generations have to be modified. Any thoughts on implementing this soon?

Error occurred when finalizing GeneratorDataset iterator

WARNING:tensorflow:input_shape is undefined or non-square, or rows is not in [96, 128, 160, 192, 224]. Weights for input shape (224, 224) will be loaded as the default.
WARNING:tensorflow:input_shape is undefined or non-square, or rows is not in [96, 128, 160, 192, 224]. Weights for input shape (224, 224) will be loaded as the default.
Epoch 1/2
Traceback (most recent call last):
File "C:/Users/SV60002880/Desktop/FRCNN/rpn_trainer.py", line 66, in
rpn_model.fit(rpn_train_feed,
File "D:\anaconda\envs\CPU\lib\site-packages\tensorflow\python\keras\engine\training.py", line 66, in _method_wrapper
return method(self, *args, **kwargs)
File "D:\anaconda\envs\CPU\lib\site-packages\tensorflow\python\keras\engine\training.py", line 848, in fit
tmp_logs = train_function(iterator)
File "D:\anaconda\envs\CPU\lib\site-packages\tensorflow\python\eager\def_function.py", line 580, in call
result = self._call(*args, **kwds)
File "D:\anaconda\envs\CPU\lib\site-packages\tensorflow\python\eager\def_function.py", line 644, in _call
return self._stateless_fn(*args, **kwds)
File "D:\anaconda\envs\CPU\lib\site-packages\tensorflow\python\eager\function.py", line 2420, in call
return graph_function._filtered_call(args, kwargs) # pylint: disable=protected-access
File "D:\anaconda\envs\CPU\lib\site-packages\tensorflow\python\eager\function.py", line 1661, in _filtered_call
return self._call_flat(
File "D:\anaconda\envs\CPU\lib\site-packages\tensorflow\python\eager\function.py", line 1745, in _call_flat
return self._build_call_outputs(self._inference_function.call(
File "D:\anaconda\envs\CPU\lib\site-packages\tensorflow\python\eager\function.py", line 593, in call
outputs = execute.execute(
File "D:\anaconda\envs\CPU\lib\site-packages\tensorflow\python\eager\execute.py", line 59, in quick_execute
tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [4,9216] vs. [4]
[[node reg_loss/mul (defined at C:\Users\SV60002880\Desktop\FRCNN\utils\train_utils.py:223) ]] [Op:__inference_train_function_14590]

Errors may have originated from an input operation.
Input Source operations connected to node reg_loss/mul:
reg_loss/Cast (defined at C:\Users\SV60002880\Desktop\FRCNN\utils\train_utils.py:221)
reg_loss/Sum (defined at C:\Users\SV60002880\Desktop\FRCNN\utils\train_utils.py:218)

Function call stack:
train_function

how to solve it

Predict Error

My code when I run model to predict causing an error

step_size = train_utils.get_step_size(test_total_item, batch_size)
pred_bboxes, pred_labels, pred_scores = frcnn_test_model.predict(test_data, steps=step_size, verbose=1)

here is the stacktrace:

TypeError                                 Traceback (most recent call last)
[<ipython-input-26-de9c8627623e>](https://localhost:8080/#) in <module>()
      1 step_size = train_utils.get_step_size(test_total_item, batch_size)
----> 2 pred_bboxes, pred_labels, pred_scores = frcnn_test_model.predict(test_data, steps=step_size, verbose=1)

[/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/autograph/impl/api.py](https://localhost:8080/#) in wrapper(*args, **kwargs)
    235       except Exception as e:  # pylint:disable=broad-except
    236         if hasattr(e, 'ag_error_metadata'):
--> 237           raise e.ag_error_metadata.to_exception(e)
    238         else:
    239           raise

TypeError: in converted code:

    /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/engine/training_v2.py:677 map_fn
        batch_size=None)
    /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/engine/training.py:2474 _standardize_tensors
        sample_weight, feed_output_names)
    /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/engine/training_utils.py:639 standardize_sample_weights
        'sample_weight')
    /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/engine/training_utils.py:629 standardize_sample_or_class_weights
        str(x_weight))

    TypeError: The model has multiple outputs, so `sample_weight` should be either a list or a dict. Provided `sample_weight` type not understood: Tensor("args_2:0", shape=(None, None), dtype=int32)

bouding boxes predictions on rpn

Hi, I trained the RPN only with rpn_trainer.py with mobilenet_v2 backbone on voc2007, batch size 1 and 50 epochs.
But when running rpn_predictor.py as it is, no changes, I got tons of bounding boxes like in attached image.
Which code should be run to get similar predictions (i.e. just a few bounding boxes) like you obtained in your example with the lion picture?
Thanks!
image
image

hello, it's too slow to download data by tfds.load

I have a voc2007 datasets, how to use it without tfds.load() ?
and it seems your code is wrong with it:
data_utils.get_dataset("voc/2007", "train+validation")

voc2007 is in the tfds' list, voc/2007 not.

Beginner question about frozen layers and transfer learning

When training the RPN Model for example, wouldn't be more suitable to reuse the weights for 'imagenet' instead of retrain all model from scratch?
Did you noticed better results when training all model instead of only last layers?
Thank you

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.