Giter Site home page Giter Site logo

keras-retinanet's Introduction

Keras RetinaNet Build Status DOI

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.

⚠️ Deprecated

This repository is deprecated in favor of the torchvision module. This project should work with keras 2.4 and tensorflow 2.3.0, newer versions might break support. For more information, check here.

Installation

  1. Clone this repository.
  2. 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.
  3. 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.
  4. 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

An example of testing the network can be seen in this Notebook. 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 keras_retinanet.models import load_model
model = load_model('/path/to/model.h5', backbone_name='resnet50')

Execution time on NVIDIA Pascal Titan X is roughly 75msec for an image of shape 1000x800x3.

Converting a training model to inference model

The training procedure of keras-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:
keras_retinanet/bin/convert_model.py /path/to/training/model.h5 /path/to/save/inference/model.h5

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

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

Training

keras-retinanet can be trained using this script. Note that the train script uses relative imports since it is inside the keras_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 keras-retinanet correctly, the train script will be installed as retinanet-train. However, if you make local modifications to the keras-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.

The default backbone is resnet50. You can change this using the --backbone=xxx argument in the running script. xxx can be one of the backbones in resnet models (resnet50, resnet101, resnet152), mobilenet models (mobilenet128_1.0, mobilenet128_0.75, mobilenet160_1.0, etc), densenet models or vgg models. The different options are defined by each model in their corresponding python scripts (resnet.py, mobilenet.py, etc).

Trained models can't be used directly for inference. To convert a trained model to an inference model, check here.

Usage

For training on Pascal VOC, run:

# Running directly from the repository:
keras_retinanet/bin/train.py pascal /path/to/VOCdevkit/VOC2007

# Using the installed script:
retinanet-train pascal /path/to/VOCdevkit/VOC2007

For training on MS COCO, run:

# Running directly from the repository:
keras_retinanet/bin/train.py coco /path/to/MS/COCO

# Using the installed script:
retinanet-train coco /path/to/MS/COCO

For training on Open Images Dataset OID or taking place to the OID challenges, run:

# Running directly from the repository:
keras_retinanet/bin/train.py oid /path/to/OID

# Using the installed script:
retinanet-train oid /path/to/OID

# You can also specify a list of labels if you want to train on a subset
# by adding the argument 'labels_filter':
keras_retinanet/bin/train.py oid /path/to/OID --labels-filter=Helmet,Tree

# You can also specify a parent label if you want to train on a branch
# from the semantic hierarchical tree (i.e a parent and all children)
(https://storage.googleapis.com/openimages/challenge_2018/bbox_labels_500_hierarchy_visualizer/circle.html)
# by adding the argument 'parent-label':
keras_retinanet/bin/train.py oid /path/to/OID --parent-label=Boat

For training on KITTI, run:

# Running directly from the repository:
keras_retinanet/bin/train.py kitti /path/to/KITTI

# Using the installed script:
retinanet-train kitti /path/to/KITTI

If you want to prepare the dataset you can use the following script:
https://github.com/NVIDIA/DIGITS/blob/master/examples/object-detection/prepare_kitti_data.py

For training on a [custom dataset], a CSV file can be used as a way to pass the data. See below for more details on the format of these CSV files. To train using your CSV, run:

# Running directly from the repository:
keras_retinanet/bin/train.py csv /path/to/csv/file/containing/annotations /path/to/csv/file/containing/classes

# Using the installed script:
retinanet-train csv /path/to/csv/file/containing/annotations /path/to/csv/file/containing/classes

In general, the steps to train on your own datasets are:

  1. Create a model by calling for instance keras_retinanet.models.backbone('resnet50').retinanet(num_classes=80) and compile it. Empirically, the following compile arguments have been found to work well:
model.compile(
    loss={
        'regression'    : keras_retinanet.losses.smooth_l1(),
        'classification': keras_retinanet.losses.focal()
    },
    optimizer=keras.optimizers.Adam(lr=1e-5, clipnorm=0.001)
)
  1. Create generators for training and testing data (an example is show in keras_retinanet.preprocessing.pascal_voc.PascalVocGenerator).
  2. Use model.fit_generator to start training.

Pretrained models

All models can be downloaded from the releases page.

MS COCO

Results using the cocoapi are shown below (note: according to the paper, this configuration should achieve a mAP of 0.357).

 Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.350
 Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.537
 Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.374
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.191
 Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.383
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.472
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.306
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.491
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.533
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.345
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.577
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.681

Open Images Dataset

There are 3 RetinaNet models based on ResNet50, ResNet101 and ResNet152 trained on all 500 classes of the Open Images Dataset (thanks to @ZFTurbo).

Backbone Image Size (px) Small validation mAP LB (Public)
ResNet50 768 - 1024 0.4594 0.4223
ResNet101 768 - 1024 0.4986 0.4520
ResNet152 600 - 800 0.4991 0.4651

For more information, check @ZFTurbo's repository.

CSV datasets

The CSVGenerator provides an easy way to define your own datasets. It uses two CSV files: one file containing annotations and one file containing a class name to ID mapping.

Annotations format

The CSV file with annotations should contain one annotation per line. Images with multiple bounding boxes should use one row per bounding box. Note that indexing for pixel values starts at 0. The expected format of each line is:

path/to/image.jpg,x1,y1,x2,y2,class_name

By default the CSV generator will look for images relative to the directory of the annotations file.

Some images may not contain any labeled objects. To add these images to the dataset as negative examples, add an annotation where x1, y1, x2, y2 and class_name are all empty:

path/to/image.jpg,,,,,

A full example:

/data/imgs/img_001.jpg,837,346,981,456,cow
/data/imgs/img_002.jpg,215,312,279,391,cat
/data/imgs/img_002.jpg,22,5,89,84,bird
/data/imgs/img_003.jpg,,,,,

This defines a dataset with 3 images. img_001.jpg contains a cow. img_002.jpg contains a cat and a bird. img_003.jpg contains no interesting objects/animals.

Class mapping format

The class name to ID mapping file should contain one mapping per line. Each line should use the following format:

class_name,id

Indexing for classes starts at 0. Do not include a background class as it is implicit.

For example:

cow,0
cat,1
bird,2

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 here for an example config file and then pass it to train.py using the --config parameter.

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 keras-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

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

Notes

  • This repository requires Tensorflow 2.3.0 or higher.
  • This repository is tested using OpenCV 3.4.
  • This repository is tested using Python 2.7 and 3.6.

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 keras-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 'keras_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!

keras-retinanet's People

Contributors

adreo00 avatar andreapi avatar apacha avatar awilliamson avatar borda avatar bw4sz avatar cgratie avatar codejjang avatar de-vri-es avatar enricoliscio avatar foghegehog avatar ha-nso-li avatar hgaiser avatar jjiunlin avatar jnmaloof avatar jsemric avatar lvaleriu avatar martinzlocha avatar mihaimorariu avatar nikostsagk avatar pedroconceicao avatar raghav-b avatar rodrigo2019 avatar salahaddin avatar svan70 avatar vcarpani avatar vidosits avatar yecharlie avatar yhenon avatar zfturbo 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  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

keras-retinanet's Issues

ValueError: need more than 1 value to unpack in line 74, in RetinaNet

Is this implementation complete and working?

I get this error when I try to run train.py:

Using TensorFlow backend.
Traceback (most recent call last):
  File "train.py", line 21, in <module>
    model = create_model()
  File "train.py", line 17, in create_model
    return ResNet50RetinaNet([image, im_info, gt_boxes])
  File "build/bdist.linux-x86_64/egg/keras_retinanet/models/resnet.py", line 128, in ResNet50RetinaNet
  File "build/bdist.linux-x86_64/egg/keras_retinanet/models/resnet.py", line 74, in RetinaNet
ValueError: need more than 1 value to unpack

I run setup.py install, which installed keras-resnet 0.0.7 from https://pypi.python.org/simple/keras-resnet/

error to train VOC2007

Pulled latest codes, saw following error during training (ubuntu 16.04/keras2.0.8/tensorflow 1.2):
"File "/home/ai/krn/keras_retinanet/preprocessing/pascal_voc.py", line 149, in load_image
regression_targets = np.append(regression_targets, np.expand_dims(labels, axis=1), axis=1)
File "/home/dsu/tf/local/lib/python2.7/site-packages/numpy/lib/function_base.py", line 5152, in append
return concatenate((arr, values), axis=axis)
ValueError: all the input arrays must have same number of dimensions".

Earlier, saw error: "
File "/home/ai/krn/keras_retinanet/preprocessing/pascal_voc.py", line 148, in load_image
labels, regression_targets = anchor_targets(image, boxes_batch[0])
TypeError: anchor_targets() takes at least 3 arguments (2 given)", which was resolved by adding 3rd-parameter len(self.classes).

StopIteration: attempt to get argmax of an empty sequence

Hi again,

Im getting the following error:
(most recent call last):
File "examples/train_pascal.py", line 106, in
keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10, verbose=1, mode='auto', epsilon=0.0001, cooldown=0, min_lr=0),
File "/usr/local/lib/python2.7/dist-packages/keras/legacy/interfaces.py", line 87, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 2046, in fit_generator
generator_output = next(output_generator)
File "/usr/local/lib/python2.7/dist-packages/keras/utils/data_utils.py", line 518, in get
raise StopIteration(e)
StopIteration: attempt to get argmax of an empty sequence

From here it seems as if its a problem of no significant overlap? I checked the image and there should be eleven bboxes in there, covering almost all the space, so Im not sure if that's really the error here.

Any ideas?

I will remove the image for now and continue training, to check if its just the image...

allow training with larger batch size

in the code there are multiple asserts to only allow a batch size of 1. I tried starting the training on the COCO dataset, but with this restriction it would take a very large training time.
What is the reason of this restriction?

StopIteration: points is not a numpy array, neither a scalar

Hi again,

I want to train on VOCpascal and get this error message. From an earlier, similar issue, it had something to do with OpenCV, but this one seems different.

Sharing experiences or ideas will be highly appreciated !!


.
.
Total params: 36,213,592
Trainable params: 36,107,352
Non-trainable params: 106,240


None
Epoch 1/50
Traceback (most recent call last):
File "examples/train_pascal.py", line 105, in
keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10, verbose=1, mode='auto', epsilon=0.0001, cooldown=0, min_lr=0),
File "/usr/local/lib/python2.7/dist-packages/keras/legacy/interfaces.py", line 87, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 2046, in fit_generator
generator_output = next(output_generator)
File "/usr/local/lib/python2.7/dist-packages/keras/utils/data_utils.py", line 518, in get
raise StopIteration(e)
StopIteration: points is not a numpy array, neither a scalar

Expected regression have unexpected shape

Hi all,

I'm trying to train network on my own data and got that error, could anybody help me with this issue?

Epoch 1/50 Traceback (most recent call last): File "examples/train_pascal.py", line 109, in <module> keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10, verbose=1, mode='auto', epsilon=0.0001, cooldown=0, min_lr=0), File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\keras\legacy\interfaces.py", line 87, in wrapper return func(*args, **kwargs) File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\training.py", line 2114, in fit_generator class_weight=class_weight) File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\training.py", line 1826, in train_on_batch check_batch_axis=True) File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\training.py", line 1411, in _standardize_user_data exception_prefix='target') File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\training.py", line 153, in _standardize_input_data str(array.shape)) ValueError: Error when checking target: expected regression to have shape (None, None, 4) but got array with shape (1, 110520, 5)

Thank you for support :)

unexpected keyword argument 'freeze_bn'

Hey guys, I followed the installation instructions. How can I check if I really do have the master version of keras-resnet?

installation worked fine but when I want to train I get an error:
TypeError: init() got an unexpected keyword argument 'freeze_bn'

cant see what I did wrong. Thanks for your help.

versions:
python 2.7.6
keras 2.0.9

ValueError: need more than 1 value to unpack

Hello, when i run 'Run detection on example' section in "ResNet50RetinaNet - COCO 2017.ipynb", i encounter this error, what's wrong whit it?

ValueError Traceback (most recent call last)
in ()
----> 1 image_batch, outputs = next(test_generator)
2 image = image_batch[0, ...].copy()
3 image -= min(image.flatten())
4 image /= max(image.flatten())
5 image *= 255

/home/wh/.local/lib/python2.7/site-packages/keras_retinanet/preprocessing/coco.pyc in next(self)
138 # lock indexing to prevent race conditions
139 with self.lock:
--> 140 selection, _, batch_size = next(self.index_generator)
141
142 assert(batch_size == 1), "Currently only batch_size=1 is allowed."

ValueError: need more than 1 value to unpack

Problems with smooth_l1

Hi! I am trying to train retinanet on Pascal VOC dataset, but getting a ValueError:

Traceback (most recent call last):
  File "examples/train_pascal.py", line 111, in <module>
    keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10, verbose=1, mode='auto', epsilon=0.0001, cooldown=0, min_lr=0),
  File "C:\Users\Abdurasul\Anaconda3\envs\ML\lib\site-packages\keras\legacy\interfaces.py", line 87, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\Abdurasul\Anaconda3\envs\ML\lib\site-packages\keras\engine\training.py", line 2114, in fit_generator
    class_weight=class_weight)
  File "C:\Users\Abdurasul\Anaconda3\envs\ML\lib\site-packages\keras\engine\training.py", line 1826, in train_on_batch
    check_batch_axis=True)
  File "C:\Users\Abdurasul\Anaconda3\envs\ML\lib\site-packages\keras\engine\training.py", line 1411, in _standardize_user_data
    exception_prefix='target')
  File "C:\Users\Abdurasul\Anaconda3\envs\ML\lib\site-packages\keras\engine\training.py", line 153, in _standardize_input_data
    str(array.shape))
ValueError: Error when checking target: expected regression to have shape (None, None, 4) but got array with shape (1, 90360, 5)

Considering

model.compile(
        loss={
            'regression'    : keras_retinanet.losses.smooth_l1(),
            'classification': keras_retinanet.losses.focal()
        },
        optimizer=keras.optimizers.adam(lr=1e-5, clipnorm=0.001)
    )

I think the problem is in the implementation of smooth_l1 function, but I have no idea how to fix it. Can anyone help?

Got TypeError: __init__() got an unexpected keyword argument 'freeze_bn'

Hi
I got this error when running the line #2 of the Jupyter notebook: ResNet50RetinaNet - Pascal VOC:


TypeError Traceback (most recent call last)
in ()
1 image_input = keras.layers.Input((None, None, 3))
----> 2 model = keras_retinanet.models.ResNet50RetinaNet(image_input, num_classes=20, weights='imagenet')
3
4 # compile model
5 model.compile(

/usr/local/lib/python2.7/dist-packages/keras_retinanet/models/resnet.pyc in ResNet50RetinaNet(inputs, weights, *args, **kwargs)
34 weights_path = weights
35
---> 36 resnet = keras_resnet.models.ResNet50(image, include_top=False, freeze_bn=True)
37
38 model = keras_retinanet.models.retinanet_bbox(inputs=inputs, backbone=resnet, *args, **kwargs)

/usr/local/lib/python2.7/dist-packages/keras_resnet/models/_2d.pyc in ResNet50(inputs, blocks, include_top, classes, *args, **kwargs)
176 blocks = [3, 4, 6, 3]
177
--> 178 return ResNet(inputs, blocks, block=keras_resnet.blocks.bottleneck_2d, include_top=include_top, classes=classes, *args, **kwargs)
179
180

/usr/local/lib/python2.7/dist-packages/keras_resnet/models/_2d.pyc in ResNet(inputs, blocks, block, include_top, classes, *args, **kwargs)
80 else:
81 # Else output each stages features
---> 82 return keras.models.Model(inputs=inputs, outputs=outputs, *args, **kwargs)
83
84

/usr/local/lib/python2.7/dist-packages/keras/legacy/interfaces.pyc in wrapper(*args, **kwargs)
85 warnings.warn('Update your ' + object_name + 86 ' call to the Keras 2 API: ' + signature, stacklevel=2)
---> 87 return func(*args, **kwargs)
88 wrapper._original_function = func
89 return wrapper

TypeError: init() got an unexpected keyword argument 'freeze_bn'

ImportError: cannot import name backend

Hi,
Got ImportError when trying to import keras_retinanet:

Code:

%matplotlib inline
%load_ext autoreload
%autoreload 2

#import keras
import keras_retinanet

import matplotlib.pyplot as plt
import cv2
import os
import numpy as np
import time

import tensorflow as tf

def get_session():
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
return tf.Session(config=config)

os.environ["CUDA_VISIBLE_DEVICES"] = "0"

keras.backend.tensorflow_backend.set_session(get_session())

Error:

The autoreload extension is already loaded. To reload it, use:
%reload_ext autoreload

ImportError Traceback (most recent call last)
in ()
5
6 #import keras
----> 7 import keras_retinanet
8
9

build/bdist.linux-x86_64/egg/keras_retinanet/init.py in ()
----> 1 from . import backend
2 from . import initializers
3 from . import layers
4 from . import losses
5 from . import models

ImportError: cannot import name backend

Get an error in ResNet50(... freeze_bn=True)

I find the definition of ResNet50 in keras_resnet/models/_2d.py
def ResNet50(inputs, blocks=None, include_top=True, classes=1000, *args, **kwargs)
There is not the "freeze_bn" parameter.

sparse_categorical_crossentropy error while training on mscoco

Hello,

I tried to run examples/train_coco.py but it fails with the following error:

Traceback (most recent call last):
File "examples/train_coco.py", line 88, in
keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=2, verbose=1, mode='auto', epsilon=0.0001, cooldown=0, min_lr=0),
File "/usr/local/lib/python2.7/dist-packages/Keras-2.0.3-py2.7.egg/keras/legacy/interfaces.py", line 88, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/Keras-2.0.3-py2.7.egg/keras/engine/training.py", line 1885, in fit_generator
class_weight=class_weight)
File "/usr/local/lib/python2.7/dist-packages/Keras-2.0.3-py2.7.egg/keras/engine/training.py", line 1628, in train_on_batch
outputs = self.train_function(ins)
File "/usr/local/lib/python2.7/dist-packages/Keras-2.0.3-py2.7.egg/keras/backend/tensorflow_backend.py", line 2131, in call
feed_dict=feed_dict)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 895, in run
run_metadata_ptr)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1124, in _run
feed_dict_tensor, options, run_metadata)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1321, in _do_run
options, run_metadata)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1340, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: logits and labels must have the same first dimension, got logits shape [1,101956] and labels shape [9277996]
[[Node: SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits = SparseSoftmaxCrossEntropyWithLogits[T=DT_FLOAT, Tlabels=DT_INT64, _device="/job:localhost/replica:0/task:0/gpu:0"](Reshape_1, Cast_1)]]
[[Node: mul_4/_1687 = _Recvclient_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_40184_mul_4", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]]

Caused by op u'SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits', defined at:
File "examples/train_coco.py", line 44, in
model.compile(loss={'predictions': keras_retinanet.losses.focal_loss()}, optimizer=keras.optimizers.adam(lr=1e-5, clipnorm=0.001))
File "/usr/local/lib/python2.7/dist-packages/Keras-2.0.3-py2.7.egg/keras/engine/training.py", line 905, in compile
sample_weight, mask)
File "/usr/local/lib/python2.7/dist-packages/Keras-2.0.3-py2.7.egg/keras/engine/training.py", line 436, in weighted
score_array = fn(y_true, y_pred)
File "/data/cnn/keras/keras-retinanet/keras_retinanet/losses.py", line 55, in _focal_loss
cls_loss = classification_loss(classification, labels)
File "/data/cnn/keras/keras-retinanet/keras_retinanet/losses.py", line 23, in classification_loss
cls_loss = focal_weight * keras.backend.sparse_categorical_crossentropy(labels, classification)
File "/usr/local/lib/python2.7/dist-packages/Keras-2.0.3-py2.7.egg/keras/backend/tensorflow_backend.py", line 2645, in sparse_categorical_crossentropy
logits=logits)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/nn_ops.py", line 1693, in sparse_softmax_cross_entropy_with_logits
precise_logits, labels, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_nn_ops.py", line 2491, in _sparse_softmax_cross_entropy_with_logits
features=features, labels=labels, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2630, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1204, in init
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access

InvalidArgumentError (see above for traceback): logits and labels must have the same first dimension, got logits shape [1,101956] and labels shape [9277996]
[[Node: SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits = SparseSoftmaxCrossEntropyWithLogits[T=DT_FLOAT, Tlabels=DT_INT64, _device="/job:localhost/replica:0/task:0/gpu:0"](Reshape_1, Cast_1)]]
[[Node: mul_4/_1687 = _Recvclient_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_40184_mul_4", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]]

Apparently with logits shape [1,101956] and labels shape [9277996] it is obvious that 101956*class_num = 9277996 where class_num=91 for mscoco, but I am stuck trying to identify the root cause for that. Some help please?

Error in retinanet.py->default_regression_model->keras.layers.Reshape

Hi,

I am trying to test your code on my system using the instructions given under "Generate RetinaNet model" at https://github.com/fizyr/keras-retinanet/blob/master/examples/ResNet50RetinaNet%20-%20COCO%202017.ipynb

But, I am getting following error during the call of keras.layers.Reshape function from retinanet.py->default_regression_model

I am attaching the traceback. Could you please help me in fixing the issue.

Code:

image = keras.layers.Input((None, None, 3))
model = keras_retinanet.models.ResNet50RetinaNet(image, num_classes=90, weights='imagenet')

compile model

model.compile(
loss={
'regression' : keras_retinanet.losses.smooth_l1(),
'classification': keras_retinanet.losses.focal()
},
optimizer=keras.optimizers.adam(lr=1e-5, clipnorm=0.001)
)
#print(model.summary())

Traceback (most recent call last):

File "", line 2, in
model = keras_retinanet.models.ResNet50RetinaNet(image, num_classes=90, weights='imagenet')

File "E:\PythonCode\spyderprojects\keras-retinanet-master\keras-retinanet-master\keras_retinanet\models\resnet.py", line 38, in ResNet50RetinaNet
model = keras_retinanet.models.retinanet_bbox(inputs=inputs, backbone=resnet, *args, **kwargs)

File "E:\PythonCode\spyderprojects\keras-retinanet-master\keras-retinanet-master\keras_retinanet\models\retinanet.py", line 193, in retinanet_bbox
model = retinanet(inputs=inputs, num_classes=num_classes, *args, **kwargs)

File "E:\PythonCode\spyderprojects\keras-retinanet-master\keras-retinanet-master\keras_retinanet\models\retinanet.py", line 175, in retinanet
submodels = default_submodels(num_classes, anchor_parameters)

File "E:\PythonCode\spyderprojects\keras-retinanet-master\keras-retinanet-master\keras_retinanet\models\retinanet.py", line 139, in default_submodels
default_regression_model(anchor_parameters.num_anchors()),

File "E:\PythonCode\spyderprojects\keras-retinanet-master\keras-retinanet-master\keras_retinanet\models\retinanet.py", line 87, in default_regression_model
outputs = keras.layers.Reshape((-1, 4), name='pyramid_regression_reshape')(outputs)

File "C:\Program Files\Anaconda3\envs\py35\lib\site-packages\keras\engine\topology.py", line 596, in call
output = self.call(inputs, **kwargs)

File "C:\Program Files\Anaconda3\envs\py35\lib\site-packages\keras\layers\core.py", line 391, in call
target_shape = self.compute_output_shape(input_shape)[1:]

File "C:\Program Files\Anaconda3\envs\py35\lib\site-packages\keras\layers\core.py", line 376, in compute_output_shape
input_shape[1:], self.target_shape)

File "C:\Program Files\Anaconda3\envs\py35\lib\site-packages\keras\layers\core.py", line 364, in _fix_unknown_dimension
original = np.prod(input_shape, dtype=int)

File "C:\Program Files\Anaconda3\envs\py35\lib\site-packages\numpy\core\fromnumeric.py", line 2492, in prod
out=out, keepdims=keepdims)

File "C:\Program Files\Anaconda3\envs\py35\lib\site-packages\numpy\core_methods.py", line 35, in _prod
return umr_prod(a, axis, dtype, out, keepdims)

TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

How to get sequencial result in my own dataset?

Hi.
I have some trouble in prediction. Could you help me?

I have trained the RetinaNet in my own dataset and want to get the predicted result.
I made a new generator for my dataset based on PascalVocIterator, and it works well in training step.
For the prediction, I tried to get the result based ipython example.
I would like to get the prediction result in the order of my test data path list, but the prediction is not sync to my list order.
How can I get the the result in the order of my list?

Ground truth bounding boxes with IOU < 0.5

This is more of a general question about retinanet and dense detection:
In the case where a ground truth bounding box doesn't have an IOU score of >= 0.5 with any of the anchor proposals (small items), is it not added to the set of ground truth targets? And if this is the case, does that mean that if the net correctly predicts the object, it is penalized?

ValueError on train_pascal.py

I tried to train the retinanet on a Pascal dataset. And I got a ValueError like this:
Epoch 1/50
Traceback (most recent call last):
File "train_pascal.py", line 114, in
keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10, verbose=1, mode='auto', epsilon=0.0001, cooldown=0, min_lr=0),
File "/home/Conda/.local/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 87, in wrapper
return func(*args, **kwargs)
File "/home/Conda/.local/lib/python3.6/site-packages/keras/engine/training.py", line 2114, in fit_generator
class_weight=class_weight)
File "/home/Conda/.local/lib/python3.6/site-packages/keras/engine/training.py", line 1826, in train_on_batch
check_batch_axis=True)
File "/home/Conda/.local/lib/python3.6/site-packages/keras/engine/training.py", line 1411, in _standardize_user_data
exception_prefix='target')
File "/home/Conda/.local/lib/python3.6/site-packages/keras/engine/training.py", line 153, in _standardize_input_data
str(array.shape))
ValueError: Error when checking target: expected regression to have shape (None, None, 4) but got array with shape (1, 67995, 5)

pretrained models

Can you share pretrained models?
I like to check performance between retinanet and SSD.

Thanks,

raise StopIteration(e) StopIteration

The problem occur like that:

None
Epoch 1/50
Traceback (most recent call last):
  File "examples/train_pascal.py", line 105, in <module>
    keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10, verbose=1, mode='auto', epsilon=0.0001, cooldown=0, min_lr=0),
  File "/usr/local/lib/python2.7/dist-packages/keras/legacy/interfaces.py", line 87, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 2046, in fit_generator
    generator_output = next(output_generator)
  File "/usr/local/lib/python2.7/dist-packages/keras/utils/data_utils.py", line 518, in get
    raise StopIteration(e)
StopIteration

It seems that fit_generator has problem. More detailedly, it may be "PascalVocIterator" problem.
I do not know whether it is the version or the code problem?

load the model generated by train_pascal.py dose not work.

@hgaiser , hello.

from the closed issue. i create model, load weights.
#20

i train model with 2 methods, first is train_pascal.py, second is ResNet50RetinaNet - Pascal VOC.ipynb.
i create model, load weights, use the predict code segments in ResNet50RetinaNet - Pascal VOC.ipynb.
the model generated by the first method can not predict the results, nothing recognized, the second can predict results, as common.

i think the two model is same, so i use the same predict code.
is there anything wrong? please help me to figure it out.

load saved model error

model = keras.models.load_model('snapshots_voc/resnet50_voc_best.h5', keras_retinanet.custom_objects)

error as follows:

#####################################
TypeError Traceback (most recent call last)
in ()
1
----> 2 model = keras.models.load_model('snapshots_voc/resnet50_voc_best.h5', keras_retinanet.custom_objects)
3
4 #model = keras_retinanet.models.ResNet50RetinaNet(image_input, num_classes=21, weights='imagenet')
5

/home/q/seven.wang/anaconda2/lib/python2.7/site-packages/keras/models.pyc in load_model(filepath, custom_objects, compile)
237 raise ValueError('No model found in config file.')
238 model_config = json.loads(model_config.decode('utf-8'))
--> 239 model = model_from_config(model_config, custom_objects=custom_objects)
240
241 # set weights

/home/q/seven.wang/anaconda2/lib/python2.7/site-packages/keras/models.pyc in model_from_config(config, custom_objects)
311 'Maybe you meant to use '
312 'Sequential.from_config(config)?')
--> 313 return layer_module.deserialize(config, custom_objects=custom_objects)
314
315

/home/q/seven.wang/anaconda2/lib/python2.7/site-packages/keras/layers/init.pyc in deserialize(config, custom_objects)
52 module_objects=globs,
53 custom_objects=custom_objects,
---> 54 printable_module_name='layer')

/home/q/seven.wang/anaconda2/lib/python2.7/site-packages/keras/utils/generic_utils.pyc in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
137 return cls.from_config(config['config'],
138 custom_objects=dict(list(_GLOBAL_CUSTOM_OBJECTS.items()) +
--> 139 list(custom_objects.items())))
140 with CustomObjectScope(custom_objects):
141 return cls.from_config(config['config'])

/home/q/seven.wang/anaconda2/lib/python2.7/site-packages/keras/engine/topology.pyc in from_config(cls, config, custom_objects)
2495 if layer in unprocessed_nodes:
2496 for node_data in unprocessed_nodes.pop(layer):
-> 2497 process_node(layer, node_data)
2498
2499 name = config.get('name')

/home/q/seven.wang/anaconda2/lib/python2.7/site-packages/keras/engine/topology.pyc in process_node(layer, node_data)
2452 if input_tensors:
2453 if len(input_tensors) == 1:
-> 2454 layer(input_tensors[0], **kwargs)
2455 else:
2456 layer(input_tensors, **kwargs)

/home/q/seven.wang/anaconda2/lib/python2.7/site-packages/keras/engine/topology.pyc in call(self, inputs, **kwargs)
600
601 # Actually call the layer, collecting output(s), mask(s), and shape(s).
--> 602 output = self.call(inputs, **kwargs)
603 output_mask = self.compute_mask(inputs, previous_mask)
604

/home/q/seven.wang/anaconda2/lib/python2.7/site-packages/keras_retinanet-0.0.1-py2.7.egg/keras_retinanet/layers/_misc.pyc in call(self, inputs, **kwargs)
19
20 # generate proposals from bbox deltas and shifted anchors
---> 21 anchors = keras_retinanet.backend.anchors(base_size=self.anchor_size, ratios=self.anchor_ratios, scales=self.anchor_scales)
22 anchors = keras_retinanet.backend.shift(features_shape, self.anchor_stride, anchors)
23 anchors = keras.backend.expand_dims(anchors, axis=0)

/home/q/seven.wang/anaconda2/lib/python2.7/site-packages/keras_retinanet-0.0.1-py2.7.egg/keras_retinanet/backend/common.pyc in anchors(base_size, ratios, scales)
71 base_anchor = keras.backend.expand_dims(base_anchor, 0)
72
---> 73 ratio_anchors = _ratio_enum(base_anchor, ratios)
74 anchors = _scale_enum(ratio_anchors, scales)
75

/home/q/seven.wang/anaconda2/lib/python2.7/site-packages/keras_retinanet-0.0.1-py2.7.egg/keras_retinanet/backend/common.pyc in _ratio_enum(anchor, ratios)
98 w, h, x_ctr, y_ctr = _whctrs(anchor)
99 size = w * h
--> 100 size_ratios = size / ratios
101 ws = keras.backend.round(keras.backend.sqrt(size_ratios))
102 hs = keras.backend.round(ws * ratios)

/home/q/seven.wang/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.pyc in binary_op_wrapper(x, y)
854 if not isinstance(y, sparse_tensor.SparseTensor):
855 try:
--> 856 y = ops.convert_to_tensor(y, dtype=x.dtype.base_dtype, name="y")
857 except TypeError:
858 # If the RHS is not a tensor, it might be a tensor aware object

/home/q/seven.wang/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in convert_to_tensor(value, dtype, name, preferred_dtype)
609 name=name,
610 preferred_dtype=preferred_dtype,
--> 611 as_ref=False)
612
613

/home/q/seven.wang/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype)
674
675 if ret is None:
--> 676 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
677
678 if ret is NotImplemented:

/home/q/seven.wang/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.pyc in _constant_tensor_conversion_function(v, dtype, name, as_ref)
119 as_ref=False):
120 _ = as_ref
--> 121 return constant(v, dtype=dtype, name=name)
122
123

/home/q/seven.wang/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.pyc in constant(value, dtype, shape, name, verify_shape)
100 tensor_value = attr_value_pb2.AttrValue()
101 tensor_value.tensor.CopyFrom(
--> 102 tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape))
103 dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)
104 const_tensor = g.create_op(

/home/q/seven.wang/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.pyc in make_tensor_proto(values, dtype, shape, verify_shape)
374 nparray = np.empty(shape, dtype=np_dt)
375 else:
--> 376 _AssertCompatible(values, dtype)
377 nparray = np.array(values, dtype=np_dt)
378 # check to them.

/home/q/seven.wang/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.pyc in _AssertCompatible(values, dtype)
300 else:
301 raise TypeError("Expected %s, got %s of type '%s' instead." %
--> 302 (dtype.name, repr(mismatch), type(mismatch).name))
303
304

TypeError: Expected float32, got {u'type': u'ndarray', u'value': [0.5, 1.0, 2.0]} of type 'dict' instead.

#####################################

AttributeError: 'CSVGenerator' object has no attribute 'groups'

Who knows what's the problem?

None
Epoch 1/20
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/local/lib/python2.7/dist-packages/keras/utils/data_utils.py", line 579, in data_generator_task
    generator_output = next(self._generator)
  File "/usr/local/lib/python2.7/dist-packages/keras_retinanet/preprocessing/generator.py", line 130, in next
    self.group_index = (self.group_index + 1) % len(self.groups)
AttributeError: 'CSVGenerator' object has no attribute 'groups'

Traceback (most recent call last):
  File "train_csv.py", line 106, in <module>
    keras.callbacks.ReduceLROnPlateau(monitor='loss', factor=0.1, patience=2, verbose=1, mode='auto', epsilon=0.0001, cooldown=0, min_lr=0),
  File "/usr/local/lib/python2.7/dist-packages/keras/legacy/interfaces.py", line 87, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 2046, in fit_generator
    generator_output = next(output_generator)
StopIteration

ResNet50RetinaNet - Pascal VOC.ipynb not in sync with current codebase

The evaluation part in the example isn't correct as of the current codebase. This part:

boxes, classification, reg_loss, cls_loss = model.predict_on_batch(inputs)
predicted_labels = np.argmax(classification[0, ...], axis=1)
scores = np.max(classification[0, ...], axis=1)
for idx, (label, score) in enumerate(zip(predicted_labels, scores)):
b = boxes[0, idx, :].astype(int)
cv2.rectangle(im, (b[0], b[1]), (b[2], b[3]), (0, 255, 0), 3)

should be more like this now:

regression, boxes = model.predict_on_batch(inputs)
for idx in range(0,boxes.shape[1]):
label = np.argmax(boxes[0, idx,4:], axis=0)
score = np.max(boxes[0, idx,4:], axis=0)

resnet, unexpected keyword argument 'freeze_bn'

pip install keras_resnet

keras_retinanet/models/resnet.py
resnet = keras_resnet.models.ResNet50(image, include_top=False, freeze_bn=True)
error
init() got an unexpected keyword argument 'freeze_bn'

mAP on PASCAL VOC

Have you ever tested on PASCAL VOC? If so, could you share the results?

error to train VOC2007

I got an error below when I run python2 examples/train_pascal.py ~/2TB/src/VOC/VOCdevkit/VOC2007

Epoch 1/50
Traceback (most recent call last):
File "examples/train_pascal.py", line 86, in
keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10, verbose=1, mode='auto', epsilon=0.0001, cooldown=0, min_lr=0),
File "/usr/local/lib/python2.7/dist-packages/Keras-2.0.8-py2.7.egg/keras/legacy/interfaces.py", line 87, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/Keras-2.0.8-py2.7.egg/keras/engine/training.py", line 2016, in fit_generator
generator_output = next(output_generator)
File "/usr/local/lib/python2.7/dist-packages/Keras-2.0.8-py2.7.egg/keras/utils/data_utils.py", line 517, in get
raise StopIteration(e)
StopIteration: 'PascalVocIterator' object has no attribute '_get_batches_of_transformed_samples'

Any idea? My Keras is the latest code from github.

Thanks

demo.py

I have trained pascal model and like to see how good of my trained model.
Do you have a demo.py which takes a pretrained model and one jpeg image as an input and output an image with detected bounded box?

Thanks,

failed to create cublas handle: CUBLAS_STATUS_NOT_INITIALIZED

Hello. I update keras to 2.0.9. and i update the latest project.
when i run 'example/train_coco.py' to train on coco dataset. i got this error.
loading annotations into memory...
Done (t=12.14s)
creating index...
index created!
loading annotations into memory...
Done (t=0.38s)
creating index...
index created!
Epoch 1/20
Traceback (most recent call last):
File "examples/train_coco.py", line 106, in
keras.callbacks.ReduceLROnPlateau(monitor='loss', factor=0.1, patience=2, verbose=1, mode='auto', epsilon=0.0001, cooldown=0, min_lr=0),
File "/home/wh/.local/lib/python2.7/site-packages/keras/legacy/interfaces.py", line 87, in wrapper
return func(*args, **kwargs)
File "/home/wh/.local/lib/python2.7/site-packages/keras/engine/training.py", line 2046, in fit_generator
generator_output = next(output_generator)
File "/home/wh/.local/lib/python2.7/site-packages/keras/utils/data_utils.py", line 518, in get
raise StopIteration(e)
StopIteration: 'NoneType' object has no attribute 'shape'

what is wrong with it?

need more than 1 value to unpack

Hey guys, when I run inference with ipyn (VOCpascal) I get this error:

`---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in ()
----> 1 image_batch, outputs = next(test_generator)
2 image = image_batch[0, ...].copy()
3 image -= min(image.flatten())
4 image /= max(image.flatten())
5 image *= 255

/usr/local/lib/python2.7/dist-packages/keras_retinanet/preprocessing/pascal_voc.pyc in next(self)
179 # lock indexing to prevent race conditions
180 with self.lock:
--> 181 selection, _, batch_size = next(self.index_generator)
182
183 result = self._get_batches_of_transformed_samples(selection)

ValueError: need more than 1 value to unpack`

the initialization of data throws no error, actually gives no feedback at all, but I think its fine.
path should be correct, test.txt exists and contains just one line: the name of the image (without .jpg)

I saw here that someone had kind of the same error. downgrading to keras 2.0.8 throws a new error at a prior step, so I would like to avoid that. I think I have all the changes from the PR mentioned in the other issue.

Any ideas or advice or workarounds?

versions:
python 2.7.6
keras 2.0.9

IOError: Unable to create file

I modify file 'pascal_voc.py' and 'train_pascal.py' and trian my model.But I got the following error:

Epoch 1/50
999/1000 [============================>.] - ETA: 0s - loss: 1.4074 - regression_loss: 0.3482 - classification_loss: 1.0593Epoch 00001: val_loss improved from inf to 1.04777, saving model to snapshots/resnet50_voc_best.h5
Traceback (most recent call last):
File "train_pascal.py", line 106, in
keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10, verbose=1, mode='auto', epsilon=0.0001, cooldown=0, min_lr=0),
File "/home/wanghan/miniconda2/envs/tensorflow/lib/python2.7/site-packages/keras/legacy/interfaces.py", line 87, in wrapper
return func(*args, **kwargs)
File "/home/wanghan/miniconda2/envs/tensorflow/lib/python2.7/site-packages/keras/engine/training.py", line 2117, in fit_generator
callbacks.on_epoch_end(epoch, epoch_logs)
File "/home/wanghan/miniconda2/envs/tensorflow/lib/python2.7/site-packages/keras/callbacks.py", line 73, in on_epoch_end
callback.on_epoch_end(epoch, logs)
File "/home/wanghan/miniconda2/envs/tensorflow/lib/python2.7/site-packages/keras/callbacks.py", line 414, in on_epoch_end
self.model.save(filepath, overwrite=True)
File "/home/wanghan/miniconda2/envs/tensorflow/lib/python2.7/site-packages/keras/engine/topology.py", line 2556, in save
save_model(self, filepath, overwrite, include_optimizer)
File "/home/wanghan/miniconda2/envs/tensorflow/lib/python2.7/site-packages/keras/models.py", line 102, in save_model
with h5py.File(filepath, mode='w') as f:
File "/home/wanghan/miniconda2/envs/tensorflow/lib/python2.7/site-packages/h5py/_hl/files.py", line 271, in init
fid = make_fid(name, mode, userblock_size, fapl, swmr=swmr)
File "/home/wanghan/miniconda2/envs/tensorflow/lib/python2.7/site-packages/h5py/_hl/files.py", line 107, in make_fid
fid = h5f.create(name, h5f.ACC_TRUNC, fapl=fapl, fcpl=fcpl)
File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper (/home/ilan/minonda/conda-bld/h5py_1490028130695/work/h5py/_objects.c:2846)
File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper (/home/ilan/minonda/conda-bld/h5py_1490028130695/work/h5py/_objects.c:2804)
File "h5py/h5f.pyx", line 98, in h5py.h5f.create (/home/ilan/minonda/conda-bld/h5py_1490028130695/work/h5py/h5f.c:2290)
IOError: Unable to create file (Unable to open file: name = 'snapshots/resnet50_voc_best.h5', errno = 2, error message = 'no such file or directory', flags = 13, o_flags = 242)

but i don't understand what cause the problem.
can anyone help me?thank you.

Final 3x3 conv missing for P3, P4, P5?

I was looking through the model specification code and also looking back at the RetinaNet paper and the FPN paper and perhaps found a discrepancy. In the FPN paper (first block of text on page 4), the authors mention using a 3x3 conv layer to reduce aliasing effects. It appears that this is not in the current implementation here and I'm wondering if there's a reason or if it was just overlooked. I realize that this would be somewhat redundant with first layers of the classification and regression submodels, but since it wouldn't be shared across levels of the pyramid, it could have utility.

I don't have a great sense of how important this layer is, but I thought it was at least worth bringing up. The RetinaNet paper is a bit lacking in description so it's not even totally clear that they reused this aspect of FPN.

voc 2017 training error

Thanks a lot for a few recent fixes, now training is underway.

During training, it would stop on random itreation (it may continue for hundreds of images, then encounter the error ):
" labels, regression_targets = anchor_targets(image, boxes_batch[0],len(self.classes))
File "/home/ai/krn/keras_retinanet/preprocessing/anchors.py", line 29, in anchor_targets
argmax_overlaps_inds = np.argmax(overlaps, axis=1)
File "/home/tf/local/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 963, in argmax
return _wrapfunc(a, 'argmax', axis=axis, out=out)
File "/home/tf/local/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 57, in _wrapfunc
return getattr(obj, method)(*args, **kwds)
ValueError: attempt to get argmax of an empty sequence".

Seems to be some type of batch data generation issue somewhere.

Your current performance on VOC and COCO

Hi, Can I check with you how does your implementation performs on VOC 07 (trained with 07 or 07+12) as well as on COCO val. Your information would be highly appreciated.

train_pascal.py import error

pulled latest codes and got following simple import error during training (env: keras 2.0.8/tensorflow 1.2)
"Traceback (most recent call last):
File "examples/itrain.py", line 23, in
from keras_retinanet.models import ResNet50RetinaNet
File "/home/dsu/ai/krn/keras_retinanet/init.py", line 5, in
from . import models
File "/home/dsu/ai/krn/keras_retinanet/models/init.py", line 3, in
from .resnet import ResNet50RetinaNet
File "/home/dsu/ai/krn/keras_retinanet/models/resnet.py", line 18, in
import keras_resnet.models
ImportError: No module named keras_resnet.models"

cannot find keras_resnet in keras-retinanet or keras.

Thanks.

'freeze_bn' not recognized

Hi there... thanks for putting together this repo.
I was trying to run the coco training code and ran into the following error:

Traceback (most recent call last):
File "examples/train_coco.py", line 60, in
model = create_model(weights=args.weights)
File "examples/train_coco.py", line 38, in create_model
return ResNet50RetinaNet(image, num_classes=90, weights=weights)
File "build/bdist.linux-x86_64/egg/keras_retinanet/models/resnet.py", line 36, in ResNet50RetinaNet
File "build/bdist.linux-x86_64/egg/keras_resnet/models/_2d.py", line 178, in ResNet50
File "build/bdist.linux-x86_64/egg/keras_resnet/models/_2d.py", line 82, in ResNet
File "build/bdist.linux-x86_64/egg/keras/legacy/interfaces.py", line 87, in wrapper
TypeError: init() got an unexpected keyword argument 'freeze_bn'

Any thoughts on what might be wrong with my setup?

Thanks.

A couple of things with train_csv.py

Hi love the package! I'm trying to work through a few things with train_csv.py.

The first issue is that there seems to be a syntax error on line 114:

validation_steps=test_generator.size() // args.batch_size if test_generator else: 0,

The second thing is that this line from keras_retinanet.models import ResNet50RetinaNet is throwing an error.

I'm kinda new with this but if you give me some pointers I can try and help!. Anywho really look forward to trying it out with the CSV files! Thanks again!!!

Number of anchors per image

I'm trying to understand how the anchor box proposals are being generated and it seems like calling anchors.anchor_targets(...) generates 2196 anchor targets. The paper describes having 100k anchor boxes per input image. Am I missing something?

Error while training in ResNet50RetinaNet - COCO 2017.ipynb

When trying to run this notebook, I get the following error while evaluating the cell that does the training:

Epoch 1/50
Exception in thread Thread-6:
Traceback (most recent call last):
File "/Users/sayandevmukherjee/anaconda/envs/py36-tensorflow/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/Users/sayandevmukherjee/anaconda/envs/py36-tensorflow/lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "/Users/sayandevmukherjee/anaconda/envs/py36-tensorflow/lib/python3.6/site-packages/keras/utils/data_utils.py", line 568, in data_generator_task
generator_output = next(self._generator)
File "/Users/sayandevmukherjee/anaconda/envs/py36-tensorflow/lib/python3.6/site-packages/keras/preprocessing/image.py", line 737, in next
return self.next(*args, **kwargs)
File "/Users/sayandevmukherjee/anaconda/envs/py36-tensorflow/lib/python3.6/site-packages/keras_retinanet-0.0.1-py3.6.egg/keras_retinanet/preprocessing/coco.py", line 131, in next
image_data = self.load_image(selection[0])
File "/Users/sayandevmukherjee/anaconda/envs/py36-tensorflow/lib/python3.6/site-packages/keras_retinanet-0.0.1-py3.6.egg/keras_retinanet/preprocessing/coco.py", line 67, in load_image
image, image_scale = resize_image(image, min_side=self.image_min_side, max_side=self.image_max_side)
File "/Users/sayandevmukherjee/anaconda/envs/py36-tensorflow/lib/python3.6/site-packages/keras_retinanet-0.0.1-py3.6.egg/keras_retinanet/preprocessing/image.py", line 47, in resize_image
(rows, cols, _) = img.shape
AttributeError: 'NoneType' object has no attribute 'shape'


StopIteration Traceback (most recent call last)
in ()
9 callbacks=[
10 keras.callbacks.ModelCheckpoint('resnet50_coco_best.h5', monitor='val_loss', verbose=1, save_best_only=True),
---> 11 keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=2, verbose=1, mode='auto', epsilon=0.0001, cooldown=0, min_lr=0),
12 ],
13 )

~/anaconda/envs/py36-tensorflow/lib/python3.6/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
85 warnings.warn('Update your ' + object_name + 86 ' call to the Keras 2 API: ' + signature, stacklevel=2)
---> 87 return func(*args, **kwargs)
88 wrapper._original_function = func
89 return wrapper

~/anaconda/envs/py36-tensorflow/lib/python3.6/site-packages/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
2009 batch_index = 0
2010 while steps_done < steps_per_epoch:
-> 2011 generator_output = next(output_generator)
2012
2013 if not hasattr(generator_output, 'len'):

StopIteration:

Am I missing something obvious? I did download the COCO train2017 and val2017 images and annotations.

Thanks for any help!

Training on MSCOCO fails

I did a git pull of the latest code today and tried to train on mscoco. The new pyramid layers apparently have some issue though :( Please take a look at the following log:

Total memory: 10.91GiB
Free memory: 9.84GiB
2017-11-07 11:04:29.832092: I tensorflow/core/common_runtime/gpu/gpu_device.cc:976] DMA: 0
2017-11-07 11:04:29.832105: I tensorflow/core/common_runtime/gpu/gpu_device.cc:986] 0: Y
2017-11-07 11:04:29.832119: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1045] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:01:00.0)
Creating model, this may take a second...
Traceback (most recent call last):
File "examples/train_coco.py", line 60, in
model = create_model(weights=args.weights)
File "examples/train_coco.py", line 38, in create_model
return ResNet50RetinaNet(image, num_classes=90, weights=weights)
File "/data/cnn/keras/keras-retinanet/keras_retinanet/models/resnet.py", line 38, in ResNet50RetinaNet
model = keras_retinanet.models.retinanet_bbox(inputs=inputs, backbone=resnet, *args, **kwargs)
File "/data/cnn/keras/keras-retinanet/keras_retinanet/models/retinanet.py", line 193, in retinanet_bbox
model = retinanet(inputs=inputs, num_classes=num_classes, *args, **kwargs)
File "/data/cnn/keras/keras-retinanet/keras_retinanet/models/retinanet.py", line 175, in retinanet
submodels = default_submodels(num_classes, anchor_parameters)
File "/data/cnn/keras/keras-retinanet/keras_retinanet/models/retinanet.py", line 139, in default_submodels
default_regression_model(anchor_parameters.num_anchors()),
File "/data/cnn/keras/keras-retinanet/keras_retinanet/models/retinanet.py", line 87, in default_regression_model
outputs = keras.layers.Reshape((-1, 4), name='pyramid_regression_reshape')(outputs)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 602, in call
output = self.call(inputs, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 391, in call
target_shape = self.compute_output_shape(input_shape)[1:]
File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 376, in compute_output_shape
input_shape[1:], self.target_shape)
File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 364, in _fix_unknown_dimension
original = np.prod(input_shape, dtype=int)
File "/usr/local/lib/python2.7/dist-packages/numpy/core/fromnumeric.py", line 2498, in prod
out=out, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/numpy/core/_methods.py", line 35, in _prod
return umr_prod(a, axis, dtype, out, keepdims)
TypeError: long() argument must be a string or a number, not 'NoneType'

File structure for the coco dataset?

I'm trying to get the coco and dataset to be read by the sample program. I got the pascal one working ok!

When I train the coco one I get an error saying "AttributeError: 'NoneType' object has no attribute 'astype'" which I think is caused by it reading something weird.

I think I'm having trouble with getting the right file structure to work. For coco2017 how should the annotations, test2017, train2017, val2017 folders be laid out?

OverflowError in Windows

Line 89 in /keras_retinanet/preprocessing/pascal_voc.py,

seed = np.uint32(time.time() * 1000)

This line always raise OverflowError in Windows but linux don't.

keras_resnet has no attribute custom_objects

Traceback (most recent call last):
File "examples/train_coco.py", line 27, in
from keras_retinanet.models.resnet import ResNet50RetinaNet
File "/home/akh/.local/lib/python3.5/site-packages/keras_retinanet/models/resnet.py", line 25, in
custom_objects.update(keras_resnet.custom_objects)
AttributeError: module 'keras_resnet' has no attribute 'custom_objects'

I'm using keras version 2.0.9 and keras-resnet version 0.0.8.

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.