pierluigiferrari / ssd_keras

A Keras port of Single Shot MultiBox Detector
Apache License 2.0
1.86k stars 938 forks source link

Changes make the code work in 2023 #393

Open PTN407 opened 1 year ago

PTN407 commented 1 year ago

Hello everyone, First of all, this is not exactly an issue, but some changes i made to the code so the project work (i tested on kaggle). I haven't test on my computer yet (it's old and can't run those code) (so yet can't make pull requests). I used this in code used in researching so I can't share publicly, sorry. Code:

import glob
filenames = glob.glob('/kaggle/working/ssd_keras/keras_layers/*.py')
for filename in filenames:
    with open(filename, 'r') as file :
        text = file.read()
    text = text.replace('keras.engine.topology', 'tensorflow.keras.layers')
    text = text.replace('K.image_dim_ordering()', 'K.image_data_format()')
    text = text.replace('._keras_shape', '.shape')
    text = text.replace('.trainable_weights', '._trainable_weights')
    text = text.replace('tf', 'channels_last')
    with open(filename, 'w') as file:
        file.write(text)
with open('/kaggle/working/ssd_keras/keras_loss_function/keras_ssd_loss.py', 'r') as file :
    text = file.read()
text = text.replace('from __future__ import division', '')
with open('/kaggle/working/ssd_keras/keras_loss_function/keras_ssd_loss.py', 'w') as file:
    file.write(text)
with open('/kaggle/working/ssd_keras/keras_loss_function/keras_ssd_loss.py', 'r+') as f:
        content = f.read()
        f.seek(0, 0)
        f.write('from __future__ import division\nimport tensorflow as tf\ntf.to_float = lambda x: tf.cast(x, tf.float32)'.rstrip('\r\n') + '\n' + content)
with open('/kaggle/working/ssd_keras/data_generator/object_detection_2d_data_generator.py', 'r') as file :
    text = file.read()
text = text.replace('yield ret', 'yield tuple(ret)')
with open('/kaggle/working/ssd_keras/data_generator/object_detection_2d_data_generator.py', 'w') as file:
    file.write(text)
with open('/kaggle/working/ssd_keras/eval_utils/average_precision_evaluator.py', 'r') as file :
    text = file.read()
text = text.replace('if len(predictions) == 0:', 
                    'if len(predictions) == 0:\n                cumulative_true_positives.append(1)\n                cumulative_false_positives.append(1)')
with open('/kaggle/working/ssd_keras/eval_utils/average_precision_evaluator.py', 'w') as file:
    file.write(text)
with open('/kaggle/working/ssd_keras/keras_loss_function/keras_ssd_loss.py', 'r') as file :
    text = file.read()
text = text.replace('tf.log', 'tf.math.log')
text = text.replace('self.neg_pos_ratio = tf.constant(self.neg_pos_ratio)', '')
text = text.replace('self.n_neg_min = tf.constant(self.n_neg_min)', '')
text = text.replace('self.alpha = tf.constant(self.alpha)', '')
text = text.replace('tf.count_nonzero', 'tf.math.count_nonzero')
text = text.replace('tf.to_int32(n_positive)', 'tf.cast(n_positive, tf.int32)')
with open('/kaggle/working/ssd_keras/keras_loss_function/keras_ssd_loss.py', 'w') as file:
    file.write(text)
import os
os.environ['PYTHONPATH'] += ':/kaggle/working/ssd_keras/:/kaggle/working/ssd_keras/keras_layers/'
brunomendes1 commented 10 months ago

This is great. I am trying to get the code to run too. I will try your changes.

Btw, do we really want to run the line text = text.replace('tf', 'channels_last')??

sphinx-suh commented 9 months ago

For information sharing, i am writing this.

  1. ssd7 case, csv file coordinate must be not corner form but minmax form. Almost all of the problems were due to this issue. In def parse_csv, "The expected strings are 'image_name', 'xmin', 'xmax', 'ymin', 'ymax', 'class_id'."

  2. ssd300 case, In evaluation mode, mode must be 'training' not 'inference'

Then, it will work in tensorflow 2.12.

brunomendes1 commented 3 months ago

For information sharing, i am writing this.

1. ssd7 case, csv file coordinate must be not corner form but minmax form. Almost all of the problems were due to this issue. In def parse_csv, "The expected strings are 'image_name', 'xmin', 'xmax', 'ymin', 'ymax', 'class_id'."

2. ssd300 case, In evaluation mode, mode must be 'training' not 'inference'

Then, it will work in tensorflow 2.12.

I am doing another push with this code and I think I am hiting the problem you mention because when I start the training I get the error "SSDInputEncoder detected degenerate ground truth bounding boxes". I think the code is not using the correct format for the coordinates of the boxes. I know I can supply the argument coords='minmax' to build_model and also to SSDInputEncoder; but I am still getting the same error about degenerate boxes. Can you tell me if I need to change the argument 'coords' anywhere else?