Visual-Behavior / detr-tensorflow

Tensorflow implementation of DETR : Object Detection with Transformers
MIT License
167 stars 53 forks source link

Saved models? #4

Closed Sequential-circuits closed 3 years ago

Sequential-circuits commented 3 years ago

I am training a very simple try (just 1 picture) coco model just to try the system and using the command python train_coco.py --datadir /root/detr-tensorflow/coco --batch_size 8 --target_batch 32

It seems to work, but it is unclear if it actually saves the model? I'd like to later use it in the webcam demo

Any help appreciated, thanks

thibo73800 commented 3 years ago

Hi, Thanks for asking,

In the train_coco file exmaple, the following code is used to train and evaluate the model

# Run the training for 100 epochs
for epoch_nb in range(100):
    training.eval(detr, valid_dt, config, CLASS_NAME, evaluation_step=200)
    training.fit(detr, train_dt, optimzers, config, epoch_nb, CLASS_NAME)

Therefore, to save the model, you must manually save the model in the for loop

# Run the training for 100 epochs
for epoch_nb in range(100):
    training.eval(detr, valid_dt, config, CLASS_NAME, evaluation_step=200)
    training.fit(detr, train_dt, optimzers, config, epoch_nb, CLASS_NAME)
    # Save the model here
    detr.save("detr-model.ckpt")

Then at inference you can simply load the same model without the pre trained weights and load you're weights afterwards

detr = get_detr_model(config, include_top=True, weights=None)
detr.load_weights("detr-model.ckpt")

Hope it helps. Additionally, you can use --log and setup a wandb env to quickly visualize your model performances at training.

Sequential-circuits commented 3 years ago

Thanks, but getting this: File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saved_model/save_impl.py", line 570, in call_and_return_conditional_losses call_output = layer_call(inputs, *args, **kwargs) File "/root/detr-tensorflow/networks/transformer.py", line 219, in call key_mem = memory + pos_encoding TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'

thibo73800 commented 3 years ago
detr.save_weights("detr-model.ckpt")

Instead of

detr.save("detr-model.ckpt")
Sequential-circuits commented 3 years ago

Ok, I got it to work and imported it into the webcam, but it seems to pull regular COCO labels instead of the single one that was declared in the original JSON train file

thibo73800 commented 3 years ago

Hi @Sequential-circuits , I'm working today on a tutorial that should definetely help you out with thoses issues.

Regarding your last question, it might just be a display issue. You must pass the argument class_name to the function numpy_bbox_to_image where class_name is the list you used to train your model.

Sequential-circuits commented 3 years ago

Thanks. My model has only one class, so I really don't understand where it got those other classes it shows up

thibo73800 commented 3 years ago

Hi the README you'll find links to some tutorial that should help you to train on your data.

Best, Thibault