【问题标题】:Image segmentation with keras and pretrained pspnet50使用 keras 和预训练的 pspnet50 进行图像分割
【发布时间】:2020-02-04 03:08:10
【问题描述】:

我有一个预训练的 keras 模型 pspnet50_ad20k 并希望从中获取分段图像。输入是一个形状为 (1, 473, 473, 3) 的 numpy 数组中的图像,它返回一个形状为 (1, 473, 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 个类着色,但我不知道如何。谁能给我解释一下?

【问题讨论】:

    标签: numpy tensorflow keras deep-learning image-segmentation


    【解决方案1】:

    Ade20k 数据集有 150 个类,这就是您的网络最终输出 (1, 473, 473, 150) 的原因。 150 表示每个像素位置有 150 个类别的概率。所以需要改最后一层,一般是conv2d(256,classes=150,kernel_size=1)。请将 150 替换为 50,这是您要预测的类数,并在您自己的数据集上微调您的模型。

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

    from PIL import Image
    import numpy as np
    prediction = np.argmax(prediction, axis=3) # get the max prob label
    palette = np.loadtxt('path/to/ade20k_colors.txt').astype('uint8')
    # The format of `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')
    color.putpalette(palette)
    # color is colorized prediction. 
    

    【讨论】:

      【解决方案2】:

      json_file = open('/data/pspnet50_ade20k.json', 'r') 您使用的模型以预测 50 个类别的方式编码,重新编码更改模型或 构建您自己的模型来预测您需要多少类。

      最好使用 keras 来构建您自己的模型,加载权重为 :loaded_model.load_weights("/data/pspnet50_ade20k.h5")

      【讨论】:

        猜你喜欢
        • 2015-05-22
        • 1970-01-01
        • 2018-12-15
        • 2017-07-18
        • 2018-09-25
        • 2019-05-17
        • 2018-09-17
        • 2018-05-29
        • 2019-01-31
        相关资源
        最近更新 更多