models icon indicating copy to clipboard operation
models copied to clipboard

[DeepLab]Training on a new dataset

Open andrewsueg opened this issue 7 years ago • 14 comments

I would like to segment only one kind of object in a given image, but it has many of them. What is the right way to do it?

I'm using labelme to segment them and convert to VOC. In my first attempt I segmented only a few of them and let the rest as background, because there are so many. When using the script to convert, I got background as index 0 and object as index 1. In the segmentation_dataset.py I set num_classes=2 and ignore_label=0. Am I right to do that?

When training I get stuck in the same loss, and after training a get really poor results using vis.py.

Should I create a new class2 and give a few examples of background? Basically there are object and ground, but I can't segment all of the objects manually, some of them will stay in the background.

andrewsueg avatar Jan 18 '19 00:01 andrewsueg

Thank you for your post. We noticed you have not filled out the following field in the issue template. Could you update them if they are relevant in your case, or leave them as N/A? Thanks. What is the top-level directory of the model you are using Have I written custom code OS Platform and Distribution TensorFlow installed from TensorFlow version Bazel version CUDA/cuDNN version GPU model and memory Exact command to reproduce

tensorflowbutler avatar Jan 19 '19 00:01 tensorflowbutler

Hi andrewsueg, everything should be fine if you change the value of ignore_label to "2", while keeping background, object and num_classes as it is.

All pixels which are labeled as ignore_label are excluded in the training process which means if you set ignore_label = background = 0 you only have one valid label left. In your case you want the background class to be predicted too :D.

FranklynJey avatar Jan 23 '19 09:01 FranklynJey

Hey @FranklynJey, I am also training on my custom dataset. however I am recieving this error:

Restoring from checkpoint failed. This is most likely due to a mismatch between the current graph and the graph from the checkpoint. Please ensure that you have not altered the graph expected based on the checkpoint. Original error:

Assign requires shapes of both tensors to match. lhs shape= [2] rhs shape= [19]

I am using the following command:

%cd /content/models/research

# From tensorflow/models/research/
!python deeplab/train.py \
    --logtostderr \
    --training_number_of_steps=30000 \
    --train_split="train" \
    --model_variant="mobilenet_v2" \
    --output_stride=16 \
    --decoder_output_stride=4 \
    --train_crop_size=513 \
    --train_crop_size=513 \
    --train_batch_size=12 \
    --dataset="bdd" \
    --initialize_last_layer = True \
    --fine_tune_batch_norm = False \
    --tf_initial_checkpoint=deeplab/deeplabv3_mnv2_cityscapes_train/model.ckpt\
    --train_logdir=deeplab/graphs/ \
    --dataset_dir=deeplab/bdd_seg/tfrecord

My dataset description looks like this:



_DATASETS_INFORMATION = {
    'cityscapes': _CITYSCAPES_INFORMATION,
    'pascal_voc_seg': _PASCAL_VOC_SEG_INFORMATION,
    'ade20k': _ADE20K_INFORMATION,
    'bdd': _BDD_INFORMATION,
}

I am quite confused. Why is this error happening ?

karanchahal avatar Feb 07 '19 16:02 karanchahal

ALso this

_BDD_INFORMATION = DatasetDescriptor(
    splits_to_sizes={
        'train': 8000,
        'val': 1000,
    },
    num_classes=2,
    ignore_label=255,
)

karanchahal avatar Feb 07 '19 17:02 karanchahal

Hi @karanchahal, Take a look on #3730 (comment)

No you don't have to train it from scratch. If you have your own dataset but want to reuse the pre-trained feature encoder (also called backbone), try adding --initialize_last_layer=False --last_layers_contain_logits_only=False

Hope this helps, cheers ;)

FranklynJey avatar Feb 07 '19 17:02 FranklynJey

Hey @FranklynJey , Thanks for your response ! I am using the following command and have added thr two lines that you mentioned

# From tensorflow/models/research/
!python deeplab/train.py \
    --logtostderr \
    --training_number_of_steps=30000 \
    --train_split="train" \
    --model_variant="xception_65" \
    --atrous_rates=6 \
    --atrous_rates=12 \
    --atrous_rates=18 \
    --output_stride=16 \
    --decoder_output_stride=4 \
    --train_crop_size=513 \
    --train_crop_size=513 \
    --train_batch_size=12 \
    --initialize_last_layer = False \
    --last_layers_contain_logits_only = False \
    --dataset="bdd" \
    --tf_initial_checkpoint=deeplab/deeplabv3_cityscapes_train/model.ckpt\
    --train_logdir=deeplab/graphs/ \
    --dataset_dir=deeplab/bdd_seg/tfrecord

However, still getting this error:

tensorflow.python.framework.errors_impl.InvalidArgumentError: Restoring from checkpoint failed. This is most likely due to a mismatch between the current graph and the graph from the checkpoint. Please ensure that you have not altered the graph expected based on the checkpoint. Original error:

Assign requires shapes of both tensors to match. lhs shape= [1,1,256,2] rhs shape= [1,1,256,19]
	 [[node save/Assign_116 (defined at /content/models/research/deeplab/utils/train_utils.py:119) ]]

I am using the xception network as you can see and am training on the cityscapes pretrained network.

I also made these changes to train_utils.py as mentioned in #3730 by replacing global step with '_LOGITS_SCOPE_NAME'

 # Variables that will not be restored.
  exclude_list = ['_LOGITS_SCOPE_NAME']
  if not initialize_last_layer:
    exclude_list.extend(last_layers)

I think I'm making a basic mistake. Any idea what I'm missing ?

karanchahal avatar Feb 08 '19 05:02 karanchahal

Oh my problem got solved, I guess the arg parse wasn't taking in initialize_last_layer = False so I manually turned it off in the code. Pretty strange. I'll check if it's a problem on my end or something with the code. but anyway, it started training ! Thanks !

karanchahal avatar Feb 08 '19 05:02 karanchahal

I got the same issue,could tell me how to solve the issue?thanke you

hufangjian avatar Mar 01 '19 01:03 hufangjian

You'll have to dig into the code and manually stuff off the args, it doesn't seem to register them through the command line command for some reason.

karanchahal avatar Mar 01 '19 11:03 karanchahal

For me the passing of the arguments works fine! So let's have another look before hard coding the parameters in the python scripts :)

@karanchahal: Have you tried to remove the space characters between [ initialize_last_layer, last_layers_contain_logits_only] and the corresponding values [False, False] in your sh-script? This might lead to the disrespect of your chosen values.

Try "comandLineArgument=value " instead of "comandLineArgument = value "

FranklynJey avatar Mar 01 '19 12:03 FranklynJey

Resolved issue: https://github.com/tensorflow/models/issues/6559#issuecomment-482671438

kayvonkhosrowpour avatar Apr 12 '19 18:04 kayvonkhosrowpour

@franckverrot hi , I'm wondering if you can help me with my issue i tried the training on my own data(class=2) label=gray_scale(0 1) with --initialize_last_layer = False
--last_layers_contain_logits_only = False but what i got as output is a black mask

hakS07 avatar Jun 10 '19 21:06 hakS07

Automatically closing this out since I understand the original issue was resolved, but please let me know if I'm mistaken. @essalahsouad Please open a new issue with your problem and context. Also, fill the template and provide any reproducible code . Thanks!

jvishnuvardhan avatar Jun 12 '19 19:06 jvishnuvardhan

I have the same problem as karanchahal and I have set the initialize_last_layer to False manually, but it still does not work well and the loss stops decreasing after a while (if I use xception 6.1 and if I use mobilenet 1.5). I am running the code on my own dataset which has 5 classes. 0 is background and 255 is the ignore label. Also, I have changed train_utils and added my dataset to the segmentation_dataset.py.

python train.py
--logtostderr
--training_number_of_steps=120000
--model_variant="xception_65"
--initialize_last_layer=False
--last_layers_contain_logits_only=False
--train_split="train"
--atrous_rates=6
--atrous_rates=12
--atrous_rates=18
--output_stride=16
--decoder_output_stride=4
--fine_tune_batch_norm=True
--train_crop_size="513,513"
--train_batch_size=16
--dataset="remotesensing"
--tf_initial_checkpoint="datasets/remotesensing/exp/train_on_train_set/initial/model.ckpt.index"
--train_logdir="datasets/remotesensing/exp/train_on_train_set/train"
--dataset_dir="datasets/remotesensing/tfrecord" \

@karanchahal @FranklynJey what is your final loss around? How is your segmentation performance at the end of the day?

behnamnkp avatar Jan 20 '20 18:01 behnamnkp