如何使用TensorFlow在TensorFlow中使用DeepLab进行对象分割

by Beeren Sahu

通过比伦·萨胡(Beeren Sahu)

如何使用TensorFlow在TensorFlow中使用DeepLab进行对象分割 (How to use DeepLab in TensorFlow for object segmentation using Deep Learning)

修改DeepLab代码以在您自己的数据集中训练图像中的对象分割 (Modifying the DeepLab code to train on your own dataset for object segmentation in images)

I work as a Research Scientist at FlixStock, focusing on Deep Learning solutions to generate and/or edit images. We identify coherent regions belonging to various objects in an image using Semantic Segmentation.

我是FlixStock的研究科学家,致力于深度学习解决方案来生成和/或编辑图像。 我们使用语义分割识别属于图像中各个对象的相干区域。

DeepLab is an ideal solution for Semantic Segmentation. The code is available in TensorFlow.

DeepLab是语义分割的理想解决方案。 该代码在TensorFlow中可用。

In this article, I will be sharing how we can train a DeepLab semantic segmentation model for our own data-set in TensorFlow. But before we begin…

在本文中,我将分享我们如何在TensorFlow中为我们自己的数据集训练DeepLab语义分段模型。 但是在我们开始之前...

什么是DeepLab? (What is DeepLab?)

DeepLab is one of the most promising techniques for semantic image segmentation with Deep Learning. Semantic segmentation is understanding an image at the pixel level, then assigning a label to every pixel in an image such that pixels with the same label share certain characteristics.

DeepLab是深度学习中最有前途的语义图像分割技术之一。 语义分割是在像素级别理解图像,然后为图像中的每个像素分配一个标签,以使具有相同标签的像素共享某些特征。

安装 (Installation)

DeepLab implementation in TensorFlow is available on GitHub here.

在TensorFlow DeepLab实现可以在GitHub上这里

准备数据集 (Preparing Dataset)

Before you create your own dataset and train DeepLab, you should be very clear about what you want to want to do with it. Here are the two scenarios:

在创建自己的数据集并训练DeepLab之前,您应该非常清楚要使用它做什么。 这是两种情况:

  • Training the model from scratch: you are free to have any number of classes of objects (number of labels) for segmentation. This needs a very long time for training.

    从头开始训练模型:您可以自由使用任意数量的对象类别(标签数量)进行细分。 这需要很长时间进行培训。
  • Use the pre-trained model: you are free to have any number of classes of objects for segmentation. Use the pre-trained model and only update your classifier weights with transfer learning. This will take far less time for training compared to the prior scenario.

    使用预先训练的模型:您可以自由使用任意数量的对象类别进行细分。 使用预先训练的模型,仅通过转移学习来更新分类器权重。 与以前的方案相比,这将花费更少的时间进行培训。

Let us name your new dataset as “PQR”. Create a new folder “PQR” as: tensorflow/models/research/deeplab/datasets/PQR.

让我们将您的新数据集命名为“ PQR”。 创建一个新文件夹“ PQR”: tensorflow/models/research/deeplab/datasets/PQR

To start, all you need is input images and their pre-segmented images as ground-truth for training. Input images need to be color images and the segmented images need to be color indexed images. Refer to the PASCAL dataset.

首先,您需要输入的图像及其预先分段的图像作为训练的真实内容。 输入图像必须是彩色图像,而分段图像需要是彩色索引图像。 请参考PASCAL数据集。

Create a folder named “dataset” inside “PQR”. It should have the following directory structure:

在“ PQR”中创建一个名为“ dataset”的文件夹。 它应具有以下目录结构:

+ dataset    -JPEGImages    -SegmentationClass    -ImageSets+ tfrecord
JPEG图像 (JPEGImages)

It contains all the input color images in *.jpg format.

它包含所有以*.jpg格式输入的彩色图像。

SegmentationClass (SegmentationClass)

This folder contains all the semantic segmentation annotations images for each of the color input images, which is the ground truth for the semantic segmentation.

该文件夹包含每个颜色输入图像的所有语义分割注释图像,这是语义分割的基础。

These images should be color indexed. Each color index represents a unique class (with unique color) known as a color map.

这些图像应进行颜色索引。 每个颜色索引代表一个唯一的类(具有唯一的颜色),称为色图。

Note: Files in the “SegmentationClass” folder should have the same name as in the “JPEGImage” folder for corresponding image-segmentation file pair.

注意: “ SegmentationClass”文件夹中的文件应与对应的图像分段文件对的“ JPEGImage”文件夹中的文件名相同。

影像集 (ImageSets)

This folder contains:

该文件夹包含:

  • train.txt: list of image names for the training set

    train.txt:训练集的图像名称列表
  • val.txt: list of image names for the validation set

    val.txt:验证集的图像名称列表
  • trainval.txt: list of image names for training + validation set

    trainval.txt:用于训练和验证集的图像名称列表

Sample *.txt file looks something like this:

示例*.txt文件如下所示:

pqr_000032pqr_000039pqr_000063pqr_000068pqr_000121
删除地面实况注释中的颜色图 (Remove the color-map in the ground truth annotations)

If your segmentation annotation images are RGB images instead of color indexed images. Here is a Python script that will be of help.

如果您的分段注释图像是RGB图像而不是彩色索引图像。 这是一个会有帮助的Python脚本。

Here, the palette defines the “RGB:LABEL” pair. In this sample code (0,0,0):0 is background and (255,0,0):1 is the foreground class. Note, the new_label_dir is the location where the raw segmentation data is stored.

在这里,调色板定义了“ RGB:LABEL”对。 在此示例代码中,(0,0,0):0是背景,(255,0,0):1是前景类。 请注意,new_label_dir是原始细分数据的存储位置。

Next, the task is to convert the image dataset to a TensorFlow record. Make a new copy of the script file./dataset/download_and_convert_voc2012.sh as ./dataset/convert_pqr.sh. Below is the modified script.

接下来,任务是将图像数据集转换为TensorFlow记录。 新建一个脚本文件./dataset/download_and_convert_voc2012.sh副本,作为./dataset/convert_pqr.sh 。 下面是修改后的脚本。

The converted dataset will be saved at ./deeplab/datasets/PQR/tfrecord

转换后的数据集将保存在./deeplab/datasets/PQR/tfrecord

定义数据集描述 (Defining the dataset description)

Open the file segmentation_dataset.py present in the research/deeplab/datasets/ folder. Add the following code segment defining the description for your PQR dataset.

打开research / deeplab / datasets /文件夹中存在的segmentation_dataset.py文件。 添加以下代码段,为您的PQR数据集定义说明。

_PQR_SEG_INFORMATION = DatasetDescriptor(    splits_to_sizes={        'train': 11111, # number of file in the train folder        'trainval': 22222,        'val': 11111,    },    num_classes=2, # number of classes in your dataset    ignore_label=255, # white edges that will be ignored to be class)

Make the following changes as shown bellow:

如下所示进行以下更改:

_DATASETS_INFORMATION = {    'cityscapes': _CITYSCAPES_INFORMATION,    'pascal_voc_seg': _PASCAL_VOC_SEG_INFORMATION,    'ade20k': _ADE20K_INFORMATION,    'pqr': _PQR_SEG_INFORMATION}

训练 (Training)

In order to train the model on your dataset, you need to run the train.py file in the research/deeplab/ folder. So, we have written a script file train-pqr.sh to do the task for you.

为了在您的数据集上训练模型,您需要运行research / deeplab /文件夹中的train.py文件。 因此,我们编写了一个脚本文件train-pqr.sh来为您完成任务。

Here, we have used xception_65 for your local training. You can specify the number of training iterations to the variable NUM_ITERATIONS. and set “ — tf_initial_checkpoint” to the location where you have downloaded or pre-trained the model *.ckpt. After training, the final trained model can be found in the TRAIN_LOGDIR directory.

在这里,我们将xception_65用于您的本地培训。 您可以为变量NUM_ITERATIONS指定训练迭代次数。 并将“ — tf_initial_checkpoint”设置为您下载或预训练模型* .ckpt的位置。 训练后,可以在TRAIN_LOGDIR目录中找到最终的训练模型。

Finally, run the above script from the …/research/deeplab directory.

最后,从…/ research / deeplab目录运行上述脚本。

# sh ./train-pqr.sh

Voilà! You have successfully trained DeepLab on your dataset.

瞧! 您已经在数据集中成功训练了DeepLab。

In the coming months, I will be sharing more of my experiences with Images & Deep Learning. Stay tuned and don’t forget to spare some claps if you like this article. It will encourage me immensely.

在接下来的几个月中,我将分享更多有关图像和深度学习的经验。 请继续关注,如果您喜欢这篇文章,别忘了鼓掌。 它会极大地鼓励我。

翻译自: https://www.freecodecamp.org/news/how-to-use-deeplab-in-tensorflow-for-object-segmentation-using-deep-learning-a5777290ab6b/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值