models icon indicating copy to clipboard operation
models copied to clipboard

Does object detection support focal loss?

Open kaishijeng opened this issue 9 years ago • 10 comments

If yes, how to enable it in the object detection configuration?

Thanks,

kaishijeng avatar Nov 01 '17 02:11 kaishijeng

@kaishijeng Its experimental. You could change the classification loss in the config to something like classification_loss { weighted_sigmoid_focal { anchorwise_output: true gamma: 2.0 alpha: 0.75 } } and removing the hard example mining section from the config.

tombstone avatar Nov 01 '17 19:11 tombstone

I follow your instruction and got the following error:

File "object_detection/train.py", line 198, in tf.app.run() File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/app.py", line 48, in run _sys.exit(main(_sys.argv[:1] + flags_passthrough)) File "object_detection/train.py", line 143, in main model_config, train_config, input_config = get_configs_from_pipeline_file() File "object_detection/train.py", line 103, in get_configs_from_pipeline_file text_format.Merge(f.read(), pipeline_config) File "/usr/local/lib/python2.7/dist-packages/google/protobuf/text_format.py", line 525, in Merge descriptor_pool=descriptor_pool) File "/usr/local/lib/python2.7/dist-packages/google/protobuf/text_format.py", line 579, in MergeLines return parser.MergeLines(lines, message) File "/usr/local/lib/python2.7/dist-packages/google/protobuf/text_format.py", line 612, in MergeLines self._ParseOrMerge(lines, message) File "/usr/local/lib/python2.7/dist-packages/google/protobuf/text_format.py", line 627, in _ParseOrMerge self._MergeField(tokenizer, message) File "/usr/local/lib/python2.7/dist-packages/google/protobuf/text_format.py", line 727, in _MergeField merger(tokenizer, message, field) File "/usr/local/lib/python2.7/dist-packages/google/protobuf/text_format.py", line 815, in _MergeMessageField self._MergeField(tokenizer, sub_message) File "/usr/local/lib/python2.7/dist-packages/google/protobuf/text_format.py", line 727, in _MergeField merger(tokenizer, message, field) File "/usr/local/lib/python2.7/dist-packages/google/protobuf/text_format.py", line 815, in _MergeMessageField self._MergeField(tokenizer, sub_message) File "/usr/local/lib/python2.7/dist-packages/google/protobuf/text_format.py", line 727, in _MergeField merger(tokenizer, message, field) File "/usr/local/lib/python2.7/dist-packages/google/protobuf/text_format.py", line 815, in _MergeMessageField self._MergeField(tokenizer, sub_message) File "/usr/local/lib/python2.7/dist-packages/google/protobuf/text_format.py", line 727, in _MergeField merger(tokenizer, message, field) File "/usr/local/lib/python2.7/dist-packages/google/protobuf/text_format.py", line 815, in _MergeMessageField self._MergeField(tokenizer, sub_message) File "/usr/local/lib/python2.7/dist-packages/google/protobuf/text_format.py", line 695, in _MergeField (message_descriptor.full_name, name)) google.protobuf.text_format.ParseError: 110:12 : Message type "object_detection.protos.ClassificationLoss" has no field named "weighted_sigmoid_focal".

kaishijeng avatar Nov 02 '17 02:11 kaishijeng

I check out the latest models and now is OK to run it. However, I encounter an error below:

2017-11-01 21:26:04.115929: W tensorflow/core/framework/op_kernel.cc:1192] Invalid argument: exceptions.TypeError: 'numpy.float64' object cannot be interpreted as an index INFO:tensorflow:Error reported to Coordinator: <class 'tensorflow.python.framework.errors_impl.UnknownError'>, exceptions.RuntimeError: main thread is not in main loop [[Node: Loss/PyFunc = PyFuncTin=[DT_FLOAT], Tout=[DT_UINT8], token="pyfunc_0", _device="/job:localhost/replica:0/task:0/device:CPU:0"]]

Caused by op u'Loss/PyFunc', defined at: File "object_detection/train.py", line 163, in tf.app.run()

Here is my configuration: classification_loss { weighted_sigmoid_focal { anchorwise_output: true gamma: 2.0 alpha: 0.75 } } localization_loss { weighted_smooth_l1 { anchorwise_output: true } }

If I use a regular loss as below, training is running OK classification_loss { weighted_sigmoid { anchorwise_output: true } } localization_loss { weighted_smooth_l1 { anchorwise_output: true } } hard_example_miner { num_hard_examples: 3000 iou_threshold: 0.99 loss_type: CLASSIFICATION max_negatives_per_positive: 3 min_negatives_per_image: 0 }

kaishijeng avatar Nov 02 '17 04:11 kaishijeng

@tombstone

Can you reopen this issue until it resolves?

Thanks,

kaishijeng avatar Nov 02 '17 17:11 kaishijeng

I just commented the line 482 and 483 in the object_detection/meta_architectures/ssd_meta_arch.py. Then the training is working again.

I think this error is caused by some new features added recently. The error log shows in the object_detection/utils/visualization_utils.py, cdf_plot tries to compute float64 values and return an uint8 image, but the tf.py_func somehow cannot pass Tin=[DT_FLOAT] to Tout=[DT_UINT8].

@tombstone can you help check this py_func?

izzrak avatar Nov 06 '17 06:11 izzrak

@izzrak

It works. Will this get fixed in the github? By the way, what are good values to use for Gamma/Alpha?

Thanks,

kaishijeng avatar Nov 06 '17 17:11 kaishijeng

@kaishijeng I tried my first training result on some test images and it looks very bad. Since the new evaluator is broken as well, I need do some test to make sure the focal loss is working as intended. The paper claims that they got the best result when gamma = 2 and alpha = 0.25, you can definitely start with this pair.

izzrak avatar Nov 07 '17 02:11 izzrak

@izzrak You may want to check Lilian's implementation of local loss in Tensorflow below. https://github.com/lijiancheng0614/tensorflow_object_detection

Not sure correctness of his focal loss implementation

kaishijeng avatar Nov 07 '17 03:11 kaishijeng

I just updated to the latest commit, looks like they fixed the training issue. I tried the focal loss they provided and it worked great. The converge speed of the precision is faster and the loss is much smaller.

izzrak avatar Nov 14 '17 03:11 izzrak

@izzrak

I just updated to the latest commit Should I just check out the latest models to get your latest commit? Thanks,

kaishijeng avatar Nov 15 '17 04:11 kaishijeng