Custom dataset 0 mAP
Has anyone successfully trained on a custom dataset?
I tried training on a custom dataset with one class and 1700 images in colab:
# train!
!python main.py --training_file_pattern=tfrecord/train* --model_dir=models --hparams="use_bfloat16=false,num_classes=1,learning_rate=0.001" --use_tpu=False \
--model_name='efficientdet-d0' --train_batch_size 16 \
--validation_file_pattern=tfrecord/val* --val_json_file='/content/unzipped_dset/bin_COCO/data/COCO/annotations/instances_val2017.json' \
--mode='train_and_eval'
The loss looked normal after a few epochs:

However, when I did eval, I got 0 mAP: Command:
# eval
!python main.py --hparams="use_bfloat16=false,num_classes=1" --use_tpu=False \
--model_name='efficientdet-d0' --model_dir=models \
--validation_file_pattern=tfrecord/val* --val_json_file='/content/unzipped_dset/bin_COCO/data/COCO/annotations/instances_val2017.json' \
--mode='eval' --logtostderr
Output:
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.000
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.000
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.000
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = -1.000
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = -1.000
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.000
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.000
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.000
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.000
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = -1.000
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = -1.000
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.000
Has anyone else had this issue?
@CraigWang1 , Have you tried to train it for longer time? The loss looks still large. I also tried to train my custom dataset but the loss didn't converge to 0. #25
Besides, I raised another issue on the image normalization. I'm not sure if this relates to that. #69
@ancorasir Thanks for your reply! After letting it train for a few hours and several more epochs, the mAP is still 0
@CraigWang1 How about the loss? Does it keep decreasing or saturate to some value?
It is decreasing, but only by a tiny bit

Did you change only the parameter values so that only one class is learned?
@WonTaeYeon Yes, I added this flag:
--hparams="use_bfloat16=false,num_classes=1"
Were you able to train a model on a custom dataset?
@CraigWang1 I am also contemplating the same problem. Models with one class change do not work in inference. Have you ever changed TFrecord?
@WonTaeYeon Yeah I have! I've also tried inferencing, but it doesn't work for me either with
Invalid argument: Input to reshape is a tensor with 36864 values, but the requested shape requires a multiple of 90
[[{{node Reshape}}]]
[[strided_slice_8/_1427]]
Hi @CraigWang1, I am facing the same issue with the coco dataset. Have you trained on coco ? How is your box loss ?
I have just set the batch size to 8 and the learning rate to 0.008.

Hi @ALagre, no I have not trained on coco.
This is my box loss on my custom dataset, it's also all 0s:
I think it's very strange how the mAP is 0 even on the COCO dataset because the author already trained on it.
@CraigWang1 @ALagre I have similar problem training on my custom dataset of 4 categories and 10k+ images. My training looks ok as below except the loss is not decreasing after 5 epochs. The batch size is 8 and the initial learning rate is about 0.0001 and decrease after 10 epochs. So I guess it's not because my learning rate is too small.
I inferred a single image from the training data itself and it's totally wrong which means the model has not learned the training data yet.

How about start from coco-pretrained model and finetune on customer dataset? Does anyone have small example datasets to share?
I can do the test, but how to use pretrained model? Could anyone give me an example?
How about start from coco-pretrained model and finetune on customer dataset? Does anyone have small example datasets to share?
Here is my dataset of about 1900 512x380 images that I'm training on with one 'bin' class (586 MB).
By starting from a coco-pretrained model, do you mean to download it, then start training with the checkpoint except on the custom dataset?
Hello. could you tell me how to put train dir into training_file_pattern? I got Failed precondition: new_bird/train; Is a directory when I try to put dir into it. Thank you!
To initialize models with coco-pretrained weights, you can do something like this:
python main.py --training_file_pattern=/coco_tfrecord/train-00000-of-00256.tfrecord \
--val_json_file=/coco_tfrecord//annotations/instances_val2017.json \
--model_name=efficientdet-d0 \
--model_dir=/tmp/test/ \
--ckpt=/ckpt/efficientdet/efficientdet-d0 \
--hparams="use_bfloat16=false" --use_tpu=False
Similar to issue https://github.com/google/automl/issues/40
@CraigWang1 Can you try this way. If you still have issues, could you help convert your dataset into tfrecord by following the tutorial and I would try your dataset. Thanks!
Problem Solved! Thank you!
Also submitted a change that allows you to exclude some variables. For example, I have verified this command line works:
python main.py --training_file_pattern=/coco_tfrecord/train-00000-of-00256.tfrecord \
--val_json_file=/coco_tfrecord//annotations/instances_val2017.json \
--model_name=efficientdet-d0 \
--model_dir=/tmp/test/ \
--ckpt=/ckpt/efficientdet/efficientdet-d0 \
--hparams="use_bfloat16=false,num_classes=10,var_exclude_expr=r'.*/class-predict/.*'" \
--use_tpu=False
@mingxingtan Thank you for your suggestions! Using a pretrained backbone, the loss looked much better, but the eval mAP was still 0 and I had trouble using the model to predict.
Train command:
!python main.py \
--training_file_pattern=tfrecord/train* \
--val_json_file=/coco_tfrecord//annotations/instances_val2017.json \
--model_name=efficientdet-d0 \
--model_dir=models \
--ckpt=efficientdet-d0 \
--hparams="use_bfloat16=false,num_classes=1,var_exclude_expr=r'.*/class-predict/.*'" \
--train_batch_size 4 \
--use_tpu=False
Loss:
Eval command:
# eval
!python main.py \
--validation_file_pattern=tfrecord/val* \
--val_json_file='/content/unzipped_dset/bin_COCO/data/COCO/annotations/instances_val2017.json' \
--hparams="use_bfloat16=false,num_classes=1,var_exclude_expr=r'.*/class-predict/.*'" \
--use_tpu=False \
--model_name='efficientdet-d0' \
--model_dir=models \
--mode='eval'
Eval results:
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.000
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.000
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.000
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = -1.000
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = -1.000
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.000
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.000
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.000
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.000
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = -1.000
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = -1.000
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.000
INFO:tensorflow:Inference Time : 21.57176s
I0329 20:35:12.562011 140277525276544 evaluation.py:273] Inference Time : 21.57176s
Inference code:
# Inference
import tensorflow.compat.v1 as tf
img_path = '/content/729.png'
img_out_dir = '/content'
image_size = 512
MODEL = 'efficientdet-d0'
ckpt_path = '/content/automl/efficientdet/models'
min_score_thresh = 0.2 #@param
max_boxes_to_draw = 100 #@param
line_thickness = 4 #@param
# call InferenceDriver
import inference
tf.reset_default_graph()
driver = inference.InferenceDriver(MODEL, ckpt_path, image_size=image_size, label_id_mapping={0:'bin'})
driver.inference(img_path,
img_out_dir,
min_score_thresh=min_score_thresh,
max_boxes_to_draw=max_boxes_to_draw,
line_thickness=line_thickness)
Inference error:
Input to reshape is a tensor with 36864 values, but the requested shape requires a multiple of 90
I tried changing num_classes in hparams_config.py to 1 for infencing, but it still gave me this error.
This is very puzzling.
Here is my tfrecord dataset.