Ubuntu系统下用U-Net模型进行细胞分割的学习成果(附代码)

最近在做有关细胞分割的毕设内容,用到了U-Net模型,于是本小白花了十几天时间摸爬滚打从配置环境到修改代码终于把这个模型搞懂了!下面简单总结一下自己修改出来的代码。

原文出处

U-Net: Convolutional Networks for Biomedical Image Segmentation

也可以直接从我的网盘链接看论文原文和原代码
链接:https://pan.baidu.com/s/1LDVjZNTJWXt8Y-z5j9X3-w
提取码:l1vy

环境配置

(因为我跑代码时总是遇到因为版本不匹配无法运行的情况,所以我会尽量把版本信息写的细点)
系统:Ubuntu 16.04.4
Anaconda版本:4.5.0
Python版本:3.6.0
Keras版本:2.2.4
Keras后端:TensorFlow-GPU 1.3.0
Keras后端:TensorFlow-GPU 1.2.0(更新一下)

大致思路

U-Net模型是目前比较常用的用于细胞分割的深度学习模型。输入一些细胞图像以及每张图对应的标记出轮廓的标记图像作为训练集(由于人工进行标记是个很繁琐的过程,一般也不容易获取到大量细胞图并对其标记,所以本文采取弹性形变的方法,将原本的30对训练集的图像进行扩充),通过U型网络的训练产生一个训练模型,之后用新的细胞图像作为测试集来对训练模型进行测试。

原始训练集的原始图像

从0开始按顺序标号,保存为png格式
原始训练集的原始图像

原始训练集的标记图像

从0开始以‘_mask’为后缀按顺序标号,保存为png格式
原始训练集的标记图像
一定要注意保存的时候每组图的对应关系是否正确!!!
我就因为把顺序存乱了导致最后训练效果极差找不到原因而苦恼了好久。。。

文件夹的安排

我总共分了以下几个文件夹:
文件夹的安排
其中code用于存放代码及之后的训练模型,data是原始的数据(将原始数据处理完后这个文件夹可以删掉),deform存放通过弹性形变进行扩充后的训练集(在这里建两个文件夹train和label,分别存放),npydata存放训练中产生的npy文件,results存放测试出来的结果,test为测试集

代码介绍

原代码中有一部分我没用上,就给删了,顺便根据我的需求对其进行了修改,并加入了一些我自己的文件,最后分为了以下几个文件:elastic_deform.py、data.py、unet.py、see.py

原文所提供的数据是三个图片集,train,label,test各为30张叠在一起的图片,这里我直接用wps把每张图片都提取了出来。原代码中好像也有提取图片相关的代码,正好我用不到了也没太看懂,就连着数据增强部分一起都删了,之后用其他的代码用弹性形变对数据进行了增强处理。

elastic_deform.py

这部分代码是从网上找的,有一些改动。用于将原有的30张进行扩充。每运行一遍这个文件可以产生30张形变的用于训练的数据。加上原始的训练集数据,我一共用了150组数据作为最后的训练集,从测试结果看来150组够用了。

import numpy as np
import cv2
from skimage import exposure
import random
from scipy.ndimage.interpolation import map_coordinates
from scipy.ndimage.filters import gaussian_filter
from skimage.transform import rotate
from PIL import Image


def elastic_transform(image, alpha, sigma, alpha_affine, random_state=None):
    """
    Elastic deformation of images as described in [Simard2003]_ (with modifications).
    .. [Simard2003] Simard, Steinkraus and Platt, "Best Practices for
         Convolutional Neural Networks applied to Visual Document Analysis", in
         Proc. of the International Conference on Document Analysis and
         Recognition, 2003.

     Based on https://gist.github.com/erniejunior/601cdf56d2b424757de5
    """
    if random_state is None:
        random_state = np.random.RandomState(None)

    shape = image.shape
    shape_size = shape[:2]

    # Random affine
    center_square = np.float32(shape_size) // 2
    square_size = min(shape_size) // 3
    pts1 = np.float32([center_square + square_size, [center_square[0] + square_size, center_square[1] - square_size],
                       center_square - square_size])
    pts2 = pts1 + random_state.uniform(-alpha_affine, alpha_affine, size=pts1.shape).astype(np.float32)
    M = cv2.getAffineTransform(pts1, pts2)
    image = cv2.warpAffine(image, M, shape_size[::-1], borderMode=cv2.BORDER_REFLECT_101)

    dx = gaussian_filter((random_state.rand(*shape) * 2 - 1), sigma) * alpha
    dy = gaussian_filter((random_state.rand(*shape) * 2 - 1), sigma) * alpha

    x, y, z = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]), np.arange(shape[2]))
    indices = np.reshape(y + dy, (-1, 1)), np.reshape(x + dx, (-1, 1)), np.reshape(z, (-1, 1))

    return map_coordinates(image, indices, order=1, mode='reflect').reshape(shape)


# Define function to draw a grid
def draw_grid(img, grid_size):
    # Draw grid lines
    for x in range(0, img.shape[1], grid_size):
        cv2.line(img, (x, 0), (x, im.shape[0]), color=(255,))
    for y in range(0, img.shape[0], grid_size):
        cv2.line(img, (0, y), (im.shape[1], y), color=(255,))


def augmentation(image, imageB, org_width=256, org_height=256, width=270, height=270):

    max_angle = 360
    image = cv2.resize(image, (height, width))
    imageB = cv2.resize(imageB, (height, width))

    angle = np.random.randint(max_angle)
    if np.random.randint(2):
        angle = -angle
    image = rotate(image, angle, resize=True)
    imageB = rotate(imageB, angle, resize=True)

    xstart = np.random.randint(width - org_width)
    ystart = np.random.randint(height - org_height)
    image = image[xstart:xstart + org_width, ystart:ystart + org_height]
    imageB = imageB[xstart:xstart + org_width, ystart:ystart + org_height]

    if np.random.randint(2):
        image = cv2.flip(image, 1)
        imageB = cv2
  • 6
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值