Models: DEEPLABV3+ ISSUE: Model getting corrupted after converting to pb file

Created on 23 Mar 2018  路  6Comments  路  Source: tensorflow/models

I am using Tensorflow's Deeplabv3 model https://github.com/tensorflow/models/tree/master/research/deeplab
.Where i took the pretrained model and trained on my own dataset. However when i am exporting the saved model(checkpoint) using export_model.py to convert it into .pb file and then looking at the predictions , i am getting random output.

Same checkpoint is giving desired outputs when used in vis.py(https://github.com/tensorflow/models/blob/master/research/deeplab/vis.py) and eval.py(https://github.com/tensorflow/models/blob/master/research/deeplab/eval.py) .
Even the iou's are upto the mark when evaluated.

Is it possible the weights are getting corrupted when converted to .pb file?

`class DeepLabModel(object):

INPUT_TENSOR_NAME = 'ImageTensor:0'
OUTPUT_TENSOR_NAME = 'SemanticPredictions:0'
INPUT_SIZE = 513

def __init__(self, checkpoint_path):

    self.graph = tf.Graph()

    graph_def = None
    # Extract frozen graph from tar archive.
    tar_file = tarfile.open(checkpoint_path)
    for tar_info in tar_file.getmembers():
        if _FROZEN_GRAPH_NAME in os.path.basename(tar_info.name):
            file_handle = tar_file.extractfile(tar_info)
            graph_def = tf.GraphDef.FromString(file_handle.read())
            break

    tar_file.close()

    if graph_def is None:
        raise RuntimeError('Cannot find inference graph in tar archive.')
    for op in self.graph.get_operations():
            print(op.name)
    with self.graph.as_default():
        tf.import_graph_def(graph_def, name='')
    print(tf.all_variables())
    self.sess = tf.Session(graph=self.graph)        
def run(self, image):
    width, height = image.size 
    resize_ratio = 1.0 * self.INPUT_SIZE / max(width, height)
    target_size = (int(resize_ratio * width), int(resize_ratio * height))
    resized_image = image.convert('RGB').resize(target_size, Image.ANTIALIAS)
    batch_seg_map = self.sess.run(
        self.OUTPUT_TENSOR_NAME,
        feed_dict={self.INPUT_TENSOR_NAME: [np.asarray(resized_image)]})
    seg_map = batch_seg_map[0]
    return resized_image, seg_map
    model = DeepLabModel("/output/raj/exported.tar.gz")
   orignal_im = Image.open('/output/pascal_voc_seg/VOCdevkit/VOC2012/JPEGImages/00004.jpg')
    resized_im, seg_map = model.run(orignal_im)`

Have I written custom code : No
OS Platform and Distribution : Ubuntu 16.04
TensorFlow installed from : pip
TensorFlow version : 1.5
Bazel version : N/A
CUDA/cuDNN version : 8.0/5.0
GPU model and memory : TeslaK80 , 12 GB
Exact command to reproduce : N/A

Most helpful comment

python deeplab/export_model.py \ --logtostderr \ --vis_split="val" \ --model_variant="xception_65" \ --atrous_rates=6 \ --atrous_rates=12 \ --atrous_rates=18 \ --output_stride=16 \ --decoder_output_stride=4 \ --vis_crop_size=1025 \ --vis_crop_size=1025 \ --dataset="pascal_voc_seg" \ --checkpoint_path=${PATH_TO_CHECKPOINT} \ --export_path=${PATH_TO_DATASET}

@szekany I solved this , try using this command from your research directory!

All 6 comments

Can anyone please look into this?

I have the same problem: everything is fine up until I run export_model.py, and running that script appears to corrupt the model. I'm retraining the Cityscapes Xception model with my own training and validation data. Training goes fine with train.py and eval.py, and I can view images with vis.py using the training directory. Then I export the model with export_model.py and run inference on the same images with deeplab_demo.ipynb (changing only which model is loaded) and while the output isn't quite gibberish, it is a really terrible prediction.

I've tried changing all the command line parameters of export_model.py and nothing makes a difference. I know deeplab_demo.ipynb isn't broken because I can run inference with the Cityscapes model from the model zoo and that looks fine.

Have I written custom code : No
OS Platform and Distribution : Ubuntu 16.04
TensorFlow installed from : source
TensorFlow version : 1.6.0
Bazel version : 0.11.1
CUDA/cuDNN version : 8.0/6.0
GPU model and memory : Titan Xp, 12 GB
Exact command to reproduce :

python /opt/tf/models/research/deeplab/export_model.py \
    --input_type image_tensor \
    --output_stride=16 \
    --atrous_rates=6 \
    --atrous_rates=12 \
    --atrous_rates=18 \
    --crop_size=1025 \
    --crop_size=2049 \
    --num_classes=19 \
    --checkpoint_path=/opt/semantic_segmentation_output/traindir/model.ckpt-4458 \
    --export_path=/opt/semantic_segmentation_output/deeplab_inference_graph.pb

python deeplab/export_model.py \ --logtostderr \ --vis_split="val" \ --model_variant="xception_65" \ --atrous_rates=6 \ --atrous_rates=12 \ --atrous_rates=18 \ --output_stride=16 \ --decoder_output_stride=4 \ --vis_crop_size=1025 \ --vis_crop_size=1025 \ --dataset="pascal_voc_seg" \ --checkpoint_path=${PATH_TO_CHECKPOINT} \ --export_path=${PATH_TO_DATASET}

@szekany I solved this , try using this command from your research directory!

Hi @Raj-08, thanks for the suggestion, but unfortunately running export_model from /models/research or /models/research/deeplab made no difference for me.

I'm guessing this is something I'm doing wrong, but I've tried everything I can think of. crop_size, atrous_rates, and output_stride are all the same as for training. It doesn't make sense to change num_classes (and when I do I get an error anyway). That leaves only input_type, but I don't have any ideas what that would change to.

python deeplab/export_model.py    
 --input_type image_tensor    \
 --output_stride=16    \
 --atrous_rates=6    \
 --atrous_rates=12    \
 --atrous_rates=18   \
 --crop_size=1025    \
 --crop_size=2049   \
 --num_classes=19    \
 --checkpoint_path=/opt/semantic_segmentation_output/traindir/model.ckpt-4458    \
 --export_path=/opt/semantic_segmentation_output/deeplab_inference_graph.pb

@szekany pass the exact command i have mentioned!

You're right, looks like I was just missing a few flags. Thanks!

Was this page helpful?
0 / 5 - 0 ratings