Numpy 基于keras和预训练pspnet50的图像分割

Numpy 基于keras和预训练pspnet50的图像分割,numpy,tensorflow,keras,deep-learning,image-segmentation,Numpy,Tensorflow,Keras,Deep Learning,Image Segmentation,我有一个预训练的keras模型pspnet50_ad20k,想从中得到一个分割图像。输入是形状为(1473,473,3)的numpy数组中的图像,它返回形状为(1473,473,150)的数组,因为该模型预测了150个不同的类 import os os.environ['KERAS_BACKEND'] = "tensorflow" from keras.models import model_from_json json_file = open('/data/pspnet50_ade20k.j

我有一个预训练的keras模型pspnet50_ad20k,想从中得到一个分割图像。输入是形状为(1473,473,3)的numpy数组中的图像,它返回形状为(1473,473,150)的数组,因为该模型预测了150个不同的类

import os
os.environ['KERAS_BACKEND'] = "tensorflow"
from keras.models import model_from_json

json_file = open('/data/pspnet50_ade20k.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("/data/pspnet50_ade20k.h5")
print("Loaded model from disk")

# img_array contains img in shape (1, 473, 473, 3)
result = loaded_model.predict(img_array)
# result contains img in shape (1, 473, 473, 150)
我的问题:如何从生成的数组中获得分割图像?我不得不对图像中预测的150个类进行着色,但我不知道如何。有人能给我解释一下吗?

json\u file=open('/data/pspnet50\u ade20k.json','r') 使用的模型u以预测50类的方式编码,重新编码或更改模型 构建自己的模型来预测需要多少类


最好使用keras构建您自己的模型,并加载以下权重:loaded_model.load_weights(“/data/pspnet50_ade20k.h5”)

ade20k数据集有150个类,这就是为什么您的网络最终输出
(1473,473,150)
。150表示每个像素位置上150个类的概率。因此您需要更改最后一层,通常是
conv2d(256,class=150,kernel\u size=1)
。请将150替换为50,这是您想要预测的类数,并在您自己的数据集上微调您的模型

对于着色预测,您可以这样做

从PIL导入图像
将numpy作为np导入
预测=np.argmax(预测,轴=3)#获取最大概率标签
palete=np.loadtxt('path/to/ade20k_colors.txt')。astype('uint8'))
#`ade20k_colors.txt的格式`
# 120 120 120
# 180 120 120
# 6 230 230
# 80 50 50
# 4 200 3
# ...
color=Image.fromarray(prediction.astype(np.uint8)).convert('P')
调色板(调色板)
#颜色是彩色的。