构建图像金字塔_我们如何通过转移学习构建易于使用的图像分割工具

构建图像金字塔

Authors: Jenny Huang, Ian Hunt-Isaak, William Palmer

作者: 黄珍妮 , 伊恩·亨特·伊萨克 , 威廉·帕尔默

GitHub Repo

GitHub回购

介绍 (Introduction)

Training an image segmentation model on new images can be daunting, especially when you need to label your own data. To make this task easier and faster, we built a user-friendly tool that lets you build this entire process in a single Jupyter notebook. In the sections below, we will show you how our tool lets you:

在新图像上训练图像分割模型可能会令人生畏,尤其是当您需要标记自己的数据时。 为了使此任务更容易,更快捷,我们构建了一个用户友好的工具,可让您在单个Jupyter笔记本中构建整个过程。 在以下各节中,我们将向您展示我们的工具如何使您:

  1. Manually label your own images

    手动标记自己的图像
  2. Build an effective segmentation model through transfer learning

    通过转移学习建立有效的细分模型
  3. Visualize the model and its results

    可视化模型及其结果
  4. Share your project as a Docker image

    以Docker映像形式共享您的项目

The main benefits of this tool are that it is easy-to-use, all in one platform, and well-integrated with existing data science workflows. Through interactive widgets and command prompts, we built a user-friendly way to label images and train the model. On top of that, everything can run in a single Jupyter notebook, making it quick and easy to spin up a model, without much overhead. Lastly, by working in a Python environment and using standard libraries like Tensorflow and Matplotlib, this tool can be well-integrated into existing data science workflows, making it ideal for uses like scientific research.

该工具的主要优点是易于使用在一个平台上全部 使用 ,并且与现有的数据科学工作流程很好地集成在一起 。 通过交互式小部件和命令提示符,我们构建了一种用户友好的方式来标记图像和训练模型。 最重要的是,所有内容都可以在单个Jupyter笔记本中运行,从而快速,轻松地建立模型,而没有太多开销。 最后,通过在Python环境中工作并使用Tensorflow和Matplotlib等标准库,可以将该工具很好地集成到现有的数据科学工作流程中,使其非常适合科学研究等用途。

For instance, in microbiology, it can be very useful to segment microscopy images of cells. However, tracking cells over time can easily result in the need to segment hundreds of images, which can be very difficult to do manually. In this article, we will use microscopy images of yeast cells as our dataset and show how we built our tool to differentiate between the background, mother cells, and daughter cells.

例如,在微生物学中,分割细胞的显微镜图像可能非常有用。 但是,随着时间的推移跟踪单元很容易导致需要分割成百上千个图像,这可能很难手动完成。 在本文中,我们将使用酵母细胞的显微图像作为数据集,并展示如何构建用于区分背景,母细胞和子细胞的工具。

1.标签 (1. Labelling)

There are many existing tools to create labelled masks for images, including Labelme, ImageJ, and even the graphics editor GIMP. While these are all great tools, they can’t be integrated within a Jupyter notebook, making them harder to use with many existing workflows. Fortunately, Jupyter Widgets make it easy for us to make interactive components and connect them with the rest of our Python code.

现有许多工具可以为图像创建带标签的蒙版,包括Labelme , ImageJ甚至是图形编辑器GIMP 。 这些都是很棒的工具,但是它们无法集成到Jupyter笔记本中,这使得它们很难与许多现有工作流程一起使用。 幸运的是, Jupyter Widgets使我们能够轻松制作交互式组件并将其与我们的其余Python代码连接。

To create training masks in the notebook, we have two problems to solve:

要在笔记本中创建训练口罩,我们要解决两个问题:

  1. Select parts of an image with a mouse

    用鼠标选择图像的一部分
  2. Easily switch between images and select the class to label

    轻松在图像之间切换并选择要标记的类别

To solve the first problem, we used the Matplotlib widget backend and the built-in LassoSelector. The LassoSelector handles drawing a line to show what you are selecting, but we need a little bit of custom code to draw the masks as an overlay:

为了解决第一个问题,我们使用了Matplotlib小部件后端和内置的LassoSelector 。 LassoSelector会处理一条线以显示您所选择的内容,但是我们需要一些自定义代码来将蒙版绘制为覆盖层:

Class to manage a Lasso Selector for Matplotlib in a Jupyter notebook
在Jupyter笔记本中管理Matplotlib的套索选择器的类

For the second problem, we added nice looking buttons and other controls using ipywidgets:

对于第二个问题,我们使用ipywidgets添加了漂亮的按钮和其他控件:

Image for post

We combined these elements (along with improvements like scroll to zoom) to make a single labelling controller object. Now we can take microscopy images of yeast and segment the mother cells and daughter cells:

我们结合了这些元素(以及滚动缩放等改进功能)来制作了一个标签控制器对象。 现在,我们可以对酵母进行显微镜检查,并对母细胞和子细胞进行分段:

Demo of lasso selection image labeler
套索选择图像标签演示

You can check out the full object, which lets you scroll to zoom, right click to pan, and select multiple classes here.

您可以签出完整的对象,使您可以滚动以缩放,右键单击以平移并在此处选择多个类。

Now we can label a small number of images in the notebook, save them into the correct folder structure, and start to train CNN!

现在,我们可以在笔记本中标记少量图像,将它们保存到正确的文件夹结构中,然后开始训练CNN!

2.模型训练 (2. Model Training)

该模型 (The Model)

U-net is a convolutional neural network that was initially designed to segment biomedical images but has been successful for many other types of images. It builds upon existing convolutional networks to work better with very few training images and make more precise segmentations. It is a state-of-the-art model that is also easy to implement using the segmentation_models library.

U-net是一个卷积神经网络,最初设计用于分割生物医学图像,但已成功用于许多其他类型的图像。 它以现有的卷积网络为基础,可以在很少的训练图像的情况下更好地工作,并进行更精确的分割。 这是一个最新的模型,也可以使用segmentation_models库轻松实现。

Image for post
https://arxiv.org/pdf/1505.04597.pdfhttps://arxiv.org/pdf/1505.04597.pdf

U-net is unique because it combines an encoder and a decoder using cross-connections (the gray arrows in the figure above). These skip connections cross from the same sized part in the downsampling path to the upsampling path. This creates awareness of the original pixels inputted into the model when you upsample, which has been shown to improve performance on segmentation tasks.

U-net的独特之处在于它通过交叉连接将编码器和解码器结合在一起(上图中的灰色箭头)。 这些跳过连接从下采样路径中的相同大小的部分跨到上采样路径。 这样可以提高您对上采样时输入到模型中的原始像素的了解,这已经显示出可以提高分割任务的性能。

As great as U-net is, it won’t work well if we don’t give it enough training examples. And given how tedious it is to manually segment images, we only manually labelled 13 images. With so few training examples, it seems impossible to train a neural network with millions of parameters. To overcome this, we need both Data Augmentation and Transfer Learning.

尽管U-net很棒,但是如果我们没有给它足够的培训示例,它将无法正常工作。 考虑到手动分割图像的繁琐工作,我们仅手动标记了13张图像。 仅用很少的训练示例,就不可能训练具有数百万个参数的神经网络。 为了克服这个问题,我们既需要数据扩充又需要转移学习

数据扩充 (Data Augmentation)

Naturally, if your model has a lot of parameters, you would need a proportional amount of training examples to get good performance. Using our small dataset of images and masks, we can create new images that will be as insightful and useful to our model as our original images.

自然,如果您的模型具有很多参数,则需要成比例的训练示例才能获得良好的性能。 使用我们小的图像和蒙版数据集,我们可以创建新图像,这些图像对于模型和原始图像一样具有洞察力和实用性。

How do we do that? We can flip the image, rotate it at an angle, scale it inward or outward, crop it, translate it, or even blur the image by adding noise, but most importantly, we can do a combination of those operations to create many new training examples.

我们该怎么做? 我们可以翻转图像,旋转角度,向内或向外缩放,裁剪,平移图像,甚至可以通过添加噪点来模糊图像,但最重要的是,我们可以将这些操作结合起来以创建许多新的训练例子。

Image for post
Examples of augmented images
增强图像的例子

Image data augmentation has one more complication in segmentation compared to classification. For classification, you just need to augment the image as the label will remain the same (0 or 1 or 2…). However, for segmentation, the label (which is a mask) needs to also be transformed in sync with the image. To do this, we used the albumentations library with a custom data generator since, to our knowledge, the Keras ImageDataGenerator does not currently support the combination “Image + mask”.

与分类相比,图像数据增强在分割方面具有更多的复杂性。 对于分类,您只需要放大图像,因为标签将保持不变(0或1或2…)。 但是,对于分割,还需要与图像同步转换标签(作为蒙版)。 为此,我们使用了带有自定义数据生成器的albumentations库,因为据我们所知, Keras ImageDataGenerator目前不支持“ Image + Mask”组合。

Custom data generator for image segmentation using albumentations
自定义数据生成器,用于使用影印法进行图像分割

转移学习 (Transfer Learning)

Even though we have now created 100 or more images, this still isn’t enough as the U-net model has more than 6 million parameters. This is where transfer learning comes into play.

即使我们现在已经创建了100个或更多的图像,但这仍然不够,因为U-net模型具有超过600万个参数。 这是转移学习发挥作用的地方。

Transfer Learning lets you take a model trained on one task and reuse it for another similar task. It reduces your training time drastically and more importantly, it can lead to effective models even with a small training set like ours. For example, neural networks like MobileNet, Inception, and DeepNet, learn a feature space, shapes, colors, texture, and more, by training on a great number of images. We can then transfer what was learned by taking these model weights and modifying them slightly to activate for patterns in our own training images.

转移学习使您可以采用在一项任务上受过训练的模型,并将其重用于另一项类似任务。 它极大地减少了您的培训时间,更重要的是,即使像我们这样的小型培训,它也可以产生有效的模型。 例如,诸如MobileNet,Inception和DeepNet之类的神经网络通过训练大量图像来学习特征空间,形状,颜色,纹理等。 然后,我们可以通过获取这些模型权重并对其进行稍微修改以激活我们自己的训练图像中的模式来转移所学的内容。

Now how do we use transfer learning with U-net? We used the segmentation_models library to do this. We use the layers of a deep neural network of your choosing (MobileNet, Inception, ResNet) and the parameters found training on image classification (ImageNet) and use them as the first half (encoder) of your U-net. Then, you train the decoder layers with your own augmented dataset.

现在我们如何在U-net中使用转移学习? 我们使用segmentation_models库执行此操作。 我们使用您选择的深度神经网络(MobileNet,Inception,ResNet)的各层以及针对图像分类训练的找到的参数(ImageNet),并将它们用作U-net的前半部分(编码器)。 然后,您可以使用自己的扩充数据集训练解码器层。

Putting it Together

把它放在一起

We put this all together in a Segmentation model class that you can find here. When creating your model object, you get an interactive command prompt where you can customize aspects of your U-net like the loss function, backbone, and more:

我们将所有这些放到了Segmentation模型类中,您可以在此处找到。 创建模型对象时,您会得到一个交互式命令提示符,您可以在其中自定义U-net的各个方面,例如损失函数,主干等。

Segmentation model customization demo
细分模型定制演示

After 30 epochs of training, we achieved 95% accuracy. Note that it is important to choose a good loss function. We first tried cross-entropy loss, but the model was unable to distinguish between the similar looking mother and daughter cells and had poor performance due to the class imbalance of seeing many more non-yeast pixels than yeast pixels. We found that using dice loss gave us much better results. The dice loss is linked to the Intersection over Union Score (IOU) and is usually better adapted to segmentation tasks as it gives incentive to maximize the overlap between the predicted and ground truth masks.

经过30个星期的培训,我们达到了95%的准确性。 请注意,选择良好的损耗函数很重要。 我们首先尝试了交叉熵损失,但是该模型无法区分相貌相似的母细胞和子细胞,并且由于看到的非酵母像素多于酵母像素的类不平衡,因此该模型的性能不佳。 我们发现使用骰子损失可以获得更好的结果。 骰子损失与联盟分数交叉点(IOU)相关联,通常会更好地适应分段任务,因为它可以最大程度地促进预测的和真实面罩之间的重叠。

Image for post
Example predictions by our model compared to true masks
我们的模型与真实蒙版相比的示例预测

3.可视化 (3. Visualization)

Now that our model is trained, let’s use some visualization techniques to see how it works. We follow Ankit Paliwal’s tutorial to do so. You can find the implementation in his corresponding GitHub repository. In this section, we will visualize two of his techniques, Intermediate Layer Activations and Heatmaps of Class Activations, on our yeast cell segmentation model.

现在我们的模型已经训练完毕,让我们使用一些可视化技术来查看其工作原理。 我们按照Ankit Paliwal的教程进行操作。 您可以在他相应的GitHub存储库中找到实现。 在本节中,我们将在酵母细胞细分模型上可视化他的两种技术,即中间层激活和类激活的热图。

中间层激活 (Intermediate Layer Activations)

This first technique shows the output of intermediate layers in a forward pass of the network on a test image. This lets us see what features of the input image are highlighted at each layer. After inputting a test image, we visualized the first few outputs for some convolutional layers in our network:

第一项技术在测试图像上显示了网络的前向传递中中间层的输出。 这使我们可以看到输入图像的哪些功能在每一层都突出显示。 输入测试图像后,我们将网络中一些卷积层的前几个输出可视化:

Image for post
Outputs for some encoder layers
某些编码器层的输出
Image for post
Outputs for some decoder layers
某些解码器层的输出

In the encoder layers, filters close to the input detect more detail and those close to the output of the model detect more general features, which is to be expected. In the decoder layers, we see the opposite pattern, of going from abstract to more specific details, which is also to be expected.

在编码器层中,靠近输入的过滤器可检测更多细节,而靠近模型输出的过滤器可检测更一般的特征,这是可以预期的。 在解码器层中,我们看到了相反的模式,即从抽象到更具体的细节,这也是可以预期的。

类激活的热图 (Heatmaps of Class Activations)

Next, we look at class activation maps. These heat maps let you see how important each location of the image is for predicting an output class. Here, we visualize the final layer of our yeast cell model, since the class prediction label will largely depend on it.

接下来,我们看一下类激活图。 这些热图让您了解图像的每个位置对于预测输出类别的重要性。 在这里,我们可视化酵母细胞模型的最后一层,因为类别预测标签将在很大程度上取决于它。

Image for post
Heatmaps of class activations on a few sample images
一些示例图像上的类激活的热图

We see from the heat maps that the cell locations are correctly activated, along with parts of the image border, which is somewhat surprising.

从热图可以看出,单元位置以及部分图像边界已被正确激活,这有些令人惊讶。

We also looked at the last technique in the tutorial, which shows what images each convolutional filter maximally responds to, but the visualizations were not very informative for our specific yeast cell model.

我们还研究了本教程中的最后一项技术,该技术显示了每个卷积滤波器最大程度地响应哪些图像,但是可视化对于我们的特定酵母细胞模型不是很有帮助。

4.制作和共享Docker映像 (4. Making and Sharing a Docker Image)

Finding an awesome model and trying to run it, only to find that it doesn’t work in your environment due to mysterious dependency issues, is very frustrating. We addressed this by creating a Docker image for our tool. This allows us to completely define the environment that the code is run in, all the way down to the operating system. For this project, we based our Docker image off of the jupyter/tensorflow-notebook image from Jupyter Docker Stacks. Then we just added a few lines to install the libraries we needed and to copy the contents of our GitHub repository into the Docker image. If you’re curious, you can see our final Dockerfile here. Finally, we pushed this image to Docker Hub for easy distribution. You can try it out by running:

找到令人敬畏的模型并尝试运行它,却发现它由于神秘的依赖关系问题而在您的环境中不起作用,这非常令人沮丧。 我们通过为我们的工具创建一个Docker镜像来解决这个问题。 这使我们可以完全定义运行代码的环境,一直到操作系统。 对于此项目,我们基于Jupyter Docker Stacks的jupyter/tensorflow-notebook映像构建Docker映像。 然后,我们仅添加了几行内容来安装所需的库,并将GitHub存储库的内容复制到Docker映像中。 如果您好奇,可以在此处查看我们的最终Dockerfile。 最后,我们将此映像推送到Docker Hub以便于分发。 您可以通过运行以下命令进行尝试:

sudo docker run -p 8888:8888 ianhuntisaak/ac295-final-project:v3 \
-e JUPYTER_LAB_ENABLE=yes

结论与未来工作 (Conclusion and Future Work)

This tool lets you easily train a segmentation model on new images in a user-friendly way. While it works, there is still room for improvement in usability, customization, and model performance. In the future, we hope to:

使用此工具,您可以以用户友好的方式轻松地在新图像上训练分割模型。 虽然有效,但在可用性,自定义和模型性能方面仍有改进的余地。 将来,我们希望:

  1. Improve the lasso tool by building a custom Jupyter Widget using the html5 canvas to reduce lag when manually segmenting

    通过使用html5 canvas构建自定义的Jupyter小部件来改善套索工具,以减少手动分段时的滞后
  2. Explore new loss functions and models (like this U-net pre-trained on broad nucleus dataset) as a basis for transfer learning

    探索新的损失函数和模型(例如在宽核数据集上预先训练的U-net )作为转移学习的基础

  3. Make it easier to interpret visualizations and suggest methods of improving the results to the user

    使解释可视化更加容易,并向用户建议改善结果的方法

致谢 (Acknowledgements)

We would like to thank our professor Pavlos Protopapas and the Harvard Applied Computation 295 course teaching staff for their guidance and support.

我们要感谢我们的教授Pavlos Protopapas和哈佛应用计算295课程的教学人员的指导和支持。

翻译自: https://towardsdatascience.com/how-we-built-an-easy-to-use-image-segmentation-tool-with-transfer-learning-546efb6ae98

构建图像金字塔

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/388701.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

PHP mongodb运用,MongoDB在PHP下的应用学习笔记

1、连接mongodb默认端口是:27017,因此我们连接mongodb:$mongodb new Mongo(localhost) 或者指定IP与端口 $mongodb new Mongo(192.168.127.1:27017) 端口可改变若mongodb开启认证,即--auth,则连接为: $mongodb new …

SpringBoot项目打war包部署Tomcat教程

一、简介 正常来说SpringBoot项目就直接用jar包来启动&#xff0c;使用它内部的tomcat实现微服务&#xff0c;但有些时候可能有部署到外部tomcat的需求&#xff0c;本教程就讲解一下如何操作 二、修改pom.xml 将要部署的module的pom.xml文件<packaging>节点设置为war <…

关于如何使用xposed来hook微信软件

安卓端 难点有两个 收款码的生成和到帐监听需要源码加 2442982910转载于:https://www.cnblogs.com/ganchuanpu/p/10220705.html

GitHub动作简介

GitHub Actions can be a little confusing if you’re new to DevOps and the CI/CD world, so in this article, we’re going to explore some features and see what we can do using the tool.如果您是DevOps和CI / CD领域的新手&#xff0c;那么GitHub Actions可能会使您…

java returnaddress,JVM之数据类型

《Java虚拟机规范》阅读笔记-数据类型1.概述Java虚拟机的数据类型可分为两大类&#xff1a;原始类型(Primitive Types&#xff0c;也称为基本类型)和引用类型(Reference Types)。Java虚拟机用不同的字节码指令来操作不同的数据类型[1] 。2.原始类型原始类型是最基本的元素&…

C# matlab

编译环境&#xff1a;Microsoft Visual Studio 2008版本 9.0.21022.8 RTMMicrosoft .NET Framework版本 3.5已安装的版本: ProfessionalMicrosoft Visual Basic 2008 91986-031-5000002-60050Microsoft Visual Basic 2008Microsoft Visual C# 2008 91986-031-5000002-60050…

基于容器制作镜像

一。镜像基础 一。基于容器制作镜像 1. 查看并关联运行的容器 [ghlocalhost ~]$ docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4da438fc9a8e busybox …

认识数据分析_认识您的最佳探索数据分析新朋友

认识数据分析Visualization often plays a minimal role in the data science and model-building process, yet Tukey, the creator of Exploratory Data Analysis, specifically advocated for the heavy use of visualization to address the limitations of numerical indi…

架构探险笔记10-框架优化之文件上传

确定文件上传使用场景 通常情况下&#xff0c;我们可以通过一个form&#xff08;表单&#xff09;来上传文件&#xff0c;就以下面的“创建客户”为例来说明&#xff08;对应的文件名是customer_create.jsp&#xff09;&#xff0c;需要提供一个form&#xff0c;并将其enctype属…

Windows Server 2003 DNS服务安装篇

导读-- DNS(Domain Name System&#xff0c;域名系统)是一种组织成层次结构的分布式数据库&#xff0c;里面包含有从DNS域名到各种数据类型(如IP地址)的映射“贵有恒&#xff0c;何必三更起五更勤;最无益&#xff0c;只怕一日曝十日寒。”前一段时间巴哥因为一些生活琐事而中止…

arima模型怎么拟合_7个统计测试,用于验证和帮助拟合ARIMA模型

arima模型怎么拟合什么是ARIMA&#xff1f; (What is ARIMA?) ARIMA models are one of the most classic and most widely used statistical forecasting techniques when dealing with univariate time series. It basically uses the lag values and lagged forecast error…

[WPF]ListView点击列头排序功能实现

[WPF]ListView点击列头排序功能实现 这是一个非常常见的功能&#xff0c;要求也很简单&#xff0c;在Column Header上显示一个小三角表示表示现在是在哪个Header上的正序还是倒序就可以了。微软的MSDN也已经提供了实现方式。微软的方法中&#xff0c;是通过ColumnHeader Templ…

天池幸福感的数据处理_了解幸福感与数据(第1部分)

天池幸福感的数据处理In these exceptional times, the lockdown left many of us with a lot of time to think. Think about the past and the future. Think about our way of life and our achievements. But most importantly, think about what has been and would be ou…

红草绿叶

从小到大喜欢阴天&#xff0c;喜欢下雨&#xff0c;喜欢那种潮湿的感觉。却又丝毫容不得脚上有一丝的水汽&#xff0c;也极其讨厌穿凉鞋。小时候特别喜欢去山上玩&#xff0c;偷桃子柿子&#xff0c;一切一切都成了美好的回忆&#xff0c;长大了&#xff0c;那些事情就都不复存…

詹森不等式_注意詹森差距

詹森不等式背景 (Background) In Kaggle’s M5 Forecasting — Accuracy competition, the square root transformation ruined many of my team’s forecasts and led to a selective patching effort in the eleventh hour. Although it turned out well, we were reminded t…

数据分析师 需求分析师_是什么让分析师出色?

数据分析师 需求分析师重点 (Top highlight)Before we dissect the nature of analytical excellence, let’s start with a quick summary of three common misconceptions about analytics from Part 1:在剖析卓越分析的本质之前&#xff0c;让我们从第1部分中对分析的三种常…

JQuery发起ajax请求,并在页面动态的添加元素

页面html代码&#xff1a; <li><div class"coll-tit"><span class"coll-icon"><iclass"sysfont coll-default"></i>全域旅游目的地</span></div><div class"coll-panel"><div c…

MAYA插件入门

我们知道&#xff0c; MAYA 是一个基于结点的插件式软件架构&#xff0c;这种开放式的软件架构是非常优秀的&#xff0c;它可以让用户非常方便地在其基础上开发一些自已想要的插件&#xff0c;从而实现一些特殊的功能或效果。 在MAYA上开发自已的插件&#xff0c;你有3种选择&a…

(原創) 如何使用C++/CLI读/写jpg檔? (.NET) (C++/CLI) (GDI+) (C/C++) (Image Processing)

Abstract因为Computer Vision的作业&#xff0c;之前都是用C# GDI写&#xff0c;但这次的作业要做Grayscale Dilation&#xff0c;想用STL的Generic Algorithm写&#xff0c;但C Standard Library并无法读取jpg档&#xff0c;用其它Library又比较麻烦&#xff0c;所以又回头想…

猫眼电影评论_电影的人群意见和评论家的意见一样好吗?

猫眼电影评论Ryan Bellgardt’s 2018 movie, The Jurassic Games, tells the story of ten death row inmates who must compete for survival in a virtual reality game where they not only fight each other but must also fight dinosaurs which can kill them both in th…