图像语义分割python_用于图像语义分割FC-DenseNet的TensorFlow实现

FC-DenseNet-Tensorflow

This is a re-implementation of the 100 layer tiramisu, technically a fully convolutional DenseNet, in TensorFlow (Tiramisu). The aim of the repository is to break down the working modules of the network, as presented in the paper, for ease of understanding. To facilitate this, the network is defined in a class, with functions for each block in the network. This promotes a modular view, and an understanding of what each component does individually. I tried to make the model code more readable, and this is the main aim of the this repository.

Network Architecture

network.png

Submodules

The "submodules" that build up the Tiramisu are explained here. Note: The graphics are just a redrawing of the ones from the original paper.

The Conv Layer:

The "conv layer" is the most atomic unit of the FC-DenseNet, it is the building block of all other modules. The following image shows the conv layer:

conv-layer.png In code, it is implemented as:

def conv_layer(self, x, training, filters, name):

with tf.name_scope(name):

x = self.batch_norm(x, training, name=name+'_bn')

x = tf.nn.relu(x, name=name+'_relu')

x = tf.layers.conv2d(x,

filters=filters,

kernel_size=[3, 3],

strides=[1, 1],

padding='SAME',

dilation_rate=[1, 1],

activation=None,

kernel_initializer=tf.contrib.layers.xavier_initializer(),

name=name+'_conv3x3')

x = tf.layers.dropout(x, rate=0.2, training=training, name=name+'_dropout')

As can be seen, each "convolutional" layer is actually a 4 step procedure of batch normalization -> Relu -> 2D-Convolution -> Dropout.

The Dense Block

The dense block is a sequence of convolutions followed by concatenations. The output of a conv layer is concated depth wise with its input, this forms the input to the next layer, and is repeated for all layers in a dense block. For the final output i.e., the output of the Dense Block, all the outputs of each conv layer in the block are concated, as shown:

dense-block.png

In code, it is implemented as:

def dense_block(self, x, training, block_nb, name):

dense_out = []

with tf.name_scope(name):

for i in range(self.layers_per_block[block_nb]):

conv = self.conv_layer(x, training, self.growth_k, name=name+'_layer_'+str(i))

x = tf.concat([conv, x], axis=3)

dense_out.append(conv)

x = tf.concat(dense_out, axis=3)

return x

How to Run

To run the network on your own dataset, do the following:

Clone this repository.

Open up your terminal and navigate to the cloned repository

Type in the following:

python main.py --mode=train --train_data=path/to/train/data --val_data=path/to/validation/data \

--ckpt=path/to/save/checkpoint/model.ckpt --layers_per_block=4,5,7,10,12,15 \

--batch_size=8 --epochs=10 --growth_k=16 --num_classes=2 --learning_rate=0.001

The "layers_per_block" argument is only specified for the downsample path, upto the final bottleneck dense block, the upsample path is then automatically built by mirroring the downsample path.

Run with trained checkpoint

To run the code with a trained checkpoint file on images, use the infer mode in in the command line options, like so:

python main.py --mode=infer --infer_data=path/to/infer/data --batch_size=4 \

--ckpt=models/model.ckpt-20 --output_folder=outputs

Tests

The python files ending with "*_test.py" are unit test files, if you make changes or have just cloned the repo, it is a good idea to run them once in your favorite Python IDE, they should let you know if your changes break anything. Currently, the test coverage is not that high, I plan to keep adding more in the future.

TODOs:

Add some more functionality in the code.

Add more detail into this readme.

Save model graph.

Rework command line arguments.

Update with some examples of performance once trained.

Increase test coverage.

Save loss summaries for Tensorboard.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Python中进行图像语义分割,可以使用PixelLib库。PixelLib使用Deeplabv3+框架实现语义分割,并在pascalvoc数据集上训练了Xception模型用于语义分割\[1\]。 首先,需要安装所需的第三方库文件,包括TensorFlow、Pillow、OpenCV-Python、scikit-image和PixelLib\[2\]。可以使用pip命令进行安装。 接下来,导入PixelLib模块,并使用semantic_segmentation类进行语义分割。可以使用以下代码进行导入和语义分割的操作: ``` import pixellib from pixellib.semantic import semantic_segmentation segment_image = semantic_segmentation() segment_image.load_pascalvoc_model("path_to_Xception_model") segment_image.segmentAsPascalvoc("path_to_image", output_image_name="path_to_output_image") ``` 在上述代码中,需要将路径替换为自己的环境路径。首先,使用`load_pascalvoc_model`方法加载预训练的Xception模型。然后,使用`segmentAsPascalvoc`方法对图像进行语义分割,并指定输出图像的路径\[3\]。 这样,你就可以在Python中使用PixelLib库进行图像语义分割了。 #### 引用[.reference_title] - *1* *2* *3* [Python代码实现图像语义分割](https://blog.csdn.net/No1_Lucky_pig/article/details/119571351)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值