小白思路理顺U-Net模型(keras)--预测部分

第一、写法1:这里对最后预测出来的结果图直接保存成tif,在arcgis中打开了。


from keras.layers import *
from keras.models import *
from PIL import Image


image_input = Input(shape=(512, 512, 3))

# <editor-fold desc="左半边的编码结构,进行不断的下采样">
# block1 , (512,512,3)-->(512,512,64)
x = Conv2D(64, (3, 3), padding='same', activation='relu', name='block1_conv1')(image_input)
x = Conv2D(64, (3, 3), padding='same', activation='relu', name='block1_conv2')(x)
feat1 = x
# 最大池化,(512,512,64)-->(256,256,64)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)

# block2 , (256,256,64)-->(256,256,128)
x = Conv2D(128, (3, 3), padding='same', activation='relu', name='block2_conv1')(x)
x = Conv2D(128, (3, 3), padding='same', activation='relu', name='block2_conv2')(x)
feat2 = x
# 最大池化,(256,256,128)-->(128,128,128)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)

# block3 , (128,128,128)-->(128,128,256)
x = Conv2D(256, (3, 3), padding='same', activation='relu', name='block3_conv1')(x)
x = Conv2D(256, (3, 3), padding='same', activation='relu', name='block3_conv2')(x)
x = Conv2D(256, (3, 3), padding='same', activation='relu', name='block3_conv3')(x)
feat3 = x
# 最大池化,(128,128,256)-->(64,64,256)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)

# block4 , (64,64,256)-->(64,64,512)
x = Conv2D(512, (3, 3), padding='same', activation='relu', name='block4_conv1')(x)
x = Conv2D(512, (3, 3), padding='same', activation='relu', name='block4_conv2')(x)
x = Conv2D(512, (3, 3), padding='same', activation='relu', name='block4_conv3')(x)
feat4 = x
# 最大池化,(64,64,512)-->(32,32,512)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)

# block5 , (32,32,512)-->(32,32,512)
x = Conv2D(512, (3, 3), padding='same', activation='relu', name='block5_conv1')(x)
x = Conv2D(512, (3, 3), padding='same', activation='relu', name='block5_conv2')(x)
x = Conv2D(512, (3, 3), padding='same', activation='relu', name='block5_conv3')(x)
feat5 = x
# </editor-fold>

#-------------------------------#
    #   获得五个有效特征层
    #   feat1   512,512,64
    #   feat2   256,256,128
    #   feat3   128,128,256
    #   feat4   64,64,512
    #   feat5   32,32,512
#-------------------------------#

# <editor-fold desc="解码部分,上采样">
# 开始解码,上采样
# (32,32,512)-->(64,64,512)
P5_up = UpSampling2D(size=(2, 2))(feat5)
# (64,64,512)-->(64,64,1024)
P4 = Concatenate(axis=3)([feat4, P5_up])
# (64,64,1024)-->(64,64,512)
P4 = Conv2D(512, (3, 3), padding='same', activation='relu', kernel_initializer='he_normal')(P4)
P4 = Conv2D(512, (3, 3), padding='same', activation='relu', kernel_initializer='he_normal')(P4)
# (64,64,512)-->(128,128,512)
P4_up = UpSampling2D(size=(2, 2))(P4)
# (128,128,512) + (128,128,256)-->(128,128,768)
P3 = Concatenate(axis=3)([feat3, P4_up])
# (128,128,768)-->(128,128,256)
P3 = Conv2D(256, (3, 3), padding='same', activation='relu', kernel_initializer='he_normal')(P3)
P3 = Conv2D(256, (3, 3), padding='same', activation='relu', kernel_initializer='he_normal')(P3)
# (128,128,256)-->(256,256,256)
P3_up = UpSampling2D(size=(2, 2))(P3)
# (256,256,256) + (256,256,128)-->(256,256,384)
P2 = Concatenate(axis=3)([feat2, P3_up])
# (256,256,384)-->(256,256,128)
P2 = Conv2D(128, (3, 3), padding='same', activation='relu', kernel_initializer='he_normal')(P2)
P2 = Conv2D(128, (3, 3), padding='same', activation='relu', kernel_initializer='he_normal')(P2)
# (256,256,128)-->(512,512,128)
P2_up = UpSampling2D(size=(2, 2))(P2)
# (512,512,128) + (512,512,64)-->(512,512,192)
P1 = Concatenate(axis=3)([feat1, P2_up])
# (512,512,192)-->(512,512,64)
P1 = Conv2D(64, (3, 3), padding='same', activation='relu', kernel_initializer='he_normal')(P1)
P1 = Conv2D(64, (3, 3), padding='same', activation='relu', kernel_initializer='he_normal')(P1)

# (512,512,64)-->(512,512,num_classes)
P1 = Conv2D(21, (1, 1), activation='softmax')(P1)
# </editor-fold>

# 构建模型
model = Model(inputs=image_input, outputs=P1)
# 加载权重
model.load_weights('D:/unet-keras-master/model_data/unet_voc.h5')
# 加载数据
image = Image.open('D:/unet-keras-master/img/street.jpg')
image = image.resize((512,512), Image.BICUBIC)
# 注意下面的方括号,不加是(512, 512, 3),加上是(1, 512, 512, 3)
img = np.asarray([np.array(image) / 255]) # img.shape = (1, 512, 512, 3)

# 进行预测
pr = model.predict(img)[0] # pr.shape = (512, 512, 21)
print(pr.shape)

# 这里的argmax(axis=-1)就是找出最大值在位深上的序号,
# 比如某个位置21个值,第15个位置数值最大,返回的就是15,对应第15个类别
# 其实这里就已经生成了一张类别图,用gdal把这个二维图形保存成tif,在arcgis打开就可以看到预测效果
pr = pr.argmax(axis=-1)
print(pr.shape) # pr.shape = (512, 512)

import gdal

# 创建tif文件
driver = gdal.GetDriverByName("GTiff")
New_YG_dataset = driver.Create(r'F:\YG\result\结果图.tif', 512, 512, 1, gdal.GDT_Float32)
band = New_YG_dataset.GetRasterBand(1)
band.WriteArray(pr)

 

第二、写法2:对预测出来的结果图,进行代码赋彩色。


from keras.layers import *
from keras.models import *
from PIL import Image
import numpy as np


image_input = Input(shape=(512, 512, 3))

# <editor-fold desc="左半边的编码结构,进行不断的下采样">
# block1 , (512,512,3)-->(512,512,64)
x = Conv2D(64, (3, 3), padding='same', activation='relu', name='block1_conv1')(image_input)
x = Conv2D(64, (3, 3), padding='same', activation='relu', name='block1_conv2')(x)
feat1 = x
# 最大池化,(512,512,64)-->(256,256,64)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)

# block2 , (256,256,64)-->(256,256,128)
x = Conv2D(128, (3, 3), padding='same', activation='relu', name='block2_conv1')(x)
x = Conv2D(128, (3, 3), padding='same', activation='relu', name='block2_conv2')(x)
feat2 = x
# 最大池化,(256,256,128)-->(128,128,128)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)

# block3 , (128,128,128)-->(128,128,256)
x = Conv2D(256, (3, 3), padding='same', activation='relu', name='block3_conv1')(x)
x = Conv2D(256, (3, 3), padding='same', activation='relu', name='block3_conv2')(x)
x = Conv2D(256, (3, 3), padding='same', activation='relu', name='block3_conv3')(x)
feat3 = x
# 最大池化,(128,128,256)-->(64,64,256)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)

# block4 , (64,64,256)-->(64,64,512)
x = Conv2D(512, (3, 3), padding='same', activation='relu', name='block4_conv1')(x)
x = Conv2D(512, (3, 3), padding='same', activation='relu', name='block4_conv2')(x)
x = Conv2D(512, (3, 3), padding='same', activation='relu', name='block4_conv3')(x)
feat4 = x
# 最大池化,(64,64,512)-->(32,32,512)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)

# block5 , (32,32,512)-->(32,32,512)
x = Conv2D(512, (3, 3), padding='same', activation='relu', name='block5_conv1')(x)
x = Conv2D(512, (3, 3), padding='same', activation='relu', name='block5_conv2')(x)
x = Conv2D(512, (3, 3), padding='same', activation='relu', name='block5_conv3')(x)
feat5 = x
# </editor-fold>

#-------------------------------#
    #   获得五个有效特征层
    #   feat1   512,512,64
    #   feat2   256,256,128
    #   feat3   128,128,256
    #   feat4   64,64,512
    #   feat5   32,32,512
#-------------------------------#

# <editor-fold desc="解码部分,上采样">
# 开始解码,上采样
# (32,32,512)-->(64,64,512)
P5_up = UpSampling2D(size=(2, 2))(feat5)
# (64,64,512)-->(64,64,1024)
P4 = Concatenate(axis=3)([feat4, P5_up])
# (64,64,1024)-->(64,64,512)
P4 = Conv2D(512, (3, 3), padding='same', activation='relu', kernel_initializer='he_normal')(P4)
P4 = Conv2D(512, (3, 3), padding='same', activation='relu', kernel_initializer='he_normal')(P4)
# (64,64,512)-->(128,128,512)
P4_up = UpSampling2D(size=(2, 2))(P4)
# (128,128,512) + (128,128,256)-->(128,128,768)
P3 = Concatenate(axis=3)([feat3, P4_up])
# (128,128,768)-->(128,128,256)
P3 = Conv2D(256, (3, 3), padding='same', activation='relu', kernel_initializer='he_normal')(P3)
P3 = Conv2D(256, (3, 3), padding='same', activation='relu', kernel_initializer='he_normal')(P3)
# (128,128,256)-->(256,256,256)
P3_up = UpSampling2D(size=(2, 2))(P3)
# (256,256,256) + (256,256,128)-->(256,256,384)
P2 = Concatenate(axis=3)([feat2, P3_up])
# (256,256,384)-->(256,256,128)
P2 = Conv2D(128, (3, 3), padding='same', activation='relu', kernel_initializer='he_normal')(P2)
P2 = Conv2D(128, (3, 3), padding='same', activation='relu', kernel_initializer='he_normal')(P2)
# (256,256,128)-->(512,512,128)
P2_up = UpSampling2D(size=(2, 2))(P2)
# (512,512,128) + (512,512,64)-->(512,512,192)
P1 = Concatenate(axis=3)([feat1, P2_up])
# (512,512,192)-->(512,512,64)
P1 = Conv2D(64, (3, 3), padding='same', activation='relu', kernel_initializer='he_normal')(P1)
P1 = Conv2D(64, (3, 3), padding='same', activation='relu', kernel_initializer='he_normal')(P1)

# (512,512,64)-->(512,512,num_classes)
P1 = Conv2D(21, (1, 1), activation='softmax')(P1)
# </editor-fold>

# 构建模型
model = Model(inputs=image_input, outputs=P1)
# 加载权重
model.load_weights('D:/unet-keras-master/model_data/unet_voc.h5')
# 加载数据
image = Image.open('D:/unet-keras-master/img/street.jpg')
image = image.resize((512,512), Image.BICUBIC)
old_img = copy.deepcopy(image)
# 注意下面的方括号,不加是(512, 512, 3),加上是(1, 512, 512, 3)
img = np.asarray([np.array(image) / 255]) # img.shape = (1, 512, 512, 3)

# 进行预测
pr = model.predict(img)[0] # pr.shape = (512, 512, 21)
print(pr.shape)

# 这里的argmax(axis=-1)就是找出最大值在位深上的序号,
# 比如某个位置21个值,第15个位置数值最大,返回的就是15,对应第15个类别
# 其实这里就已经生成了一张类别图,用gdal把这个二维图形保存成tif,在arcgis打开就可以看到预测效果
pr = pr.argmax(axis=-1)
print(pr.shape)

colors = [(0, 0, 0), (128, 0, 0), (0, 128, 0), (128, 128, 0), (0, 0, 128), (128, 0, 128), (0, 128, 128),
                    (128, 128, 128), (64, 0, 0), (192, 0, 0), (64, 128, 0), (192, 128, 0), (64, 0, 128), (192, 0, 128),
                    (64, 128, 128), (192, 128, 128), (0, 64, 0), (128, 64, 0), (0, 192, 0), (128, 192, 0), (0, 64, 128), (128, 64, 12)]
# 这里先创建一个三通道的空数组
seg_img = np.zeros((512,512,3))
# 对结果图的二维数组进行遍历,如果某个位置的像元值等于c,这个位置对应空数组第一个波段的像元值赋值为colors[c][0]
for c in range(21):
    seg_img[:, :, 0] = seg_img[:, :, 0] + ((pr[:, :] == c) * (colors[c][0])).astype('uint8')
    seg_img[:, :, 1] = seg_img[:, :, 1] + ((pr[:, :] == c) * (colors[c][1])).astype('uint8')
    seg_img[:, :, 2] = seg_img[:, :, 2] + ((pr[:, :] == c) * (colors[c][2])).astype('uint8')
# 将新图片转换为Image格式
image = Image.fromarray(np.uint8(seg_img))
# 这个就是将结果图和原图进行合并
image = Image.blend(old_img,image,0.7)
image.show()

 

 

 

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值