1 介绍
u-net最初是用来对医学图像的语义分割,后来也有人将其应用于其他领域。但大多还是用来进行二分类,即将原始图像分成两个灰度级或者色度,依次找到图像中感兴趣的目标部分。
本文主要利用u-net网络结构实现了多类的语义分割,并展示了部分测试效果,希望对你有用!
2 源代码
(1)训练模型
from __future__ import print_function
import os
import datetime
import numpy as np
from keras.models import model
from keras.layers import input, concatenate, conv2d, maxpooling2d, conv2dtranspose, averagepooling2d, dropout, \
batchnormalization
from keras.optimizers import adam
from keras.layers.convolutional import upsampling2d, conv2d
from keras.callbacks import modelcheckpoint
from keras import backend as k
from keras.layers.advanced_activations import leakyrelu, relu
import cv2
pixel = 512 #set your image size
batch_size = 5
lr = 0.001
epoch = 100
x_channel = 3 # training images channel
y_channel = 1 # label iamges channel
x_num = 422 # your traning data number
pathx = 'i:\\pascal voc dataset\\train1\\images\\' #change your file path
pathy = 'i:\\pascal voc dataset\\train1\\segmentationobject\\' #change your file path
#data processing
def generator(pathx, pathy,batch_size):
while 1:
x_train_files = os.listdir(pathx)
y_train_files = os.listdir(pathy)
a = (np.arange(1, x_num))
x = []
y = []
for i in range(batch_size):
index = np.random.choice(a)
# print(index)
img = cv2.imread(pathx + x_train_files[index], 1)
img = np.array(img).reshape(pixel, pixel, x_channel)
x.append(img)
img1 = cv2.imread(pathy + y_train_files[index], 1)
img1 = np.array(img1).reshape(pixel, pixel, y_ch