功能性API Keras预测方案的替代解决方案() [英] Functional API Keras alternate solution for predict_classes()

查看:123
本文介绍了功能性API Keras预测方案的替代解决方案()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请为我先前的问题提供背景信息,请参见此处.根据答案 >纳西姆·本.我使用功能性API训练了两路径体系结构模型.现在,由于需要预测每个像素的类别,我感到很困惑.这是相同的代码:

Please refer here for my previous question for background information. As per answer suggested by Nassim Ben. I trained model of two-path architecture using functional API. Now I feel stuck as I need to predict the class of each pixel. here is the code for the same:

    imgs = io.imread(test_img).astype('float').reshape(5,240,240)
    plist = []

 # create patches from an entire slice
            for img in imgs[:-1]:
                if np.max(img) != 0:
                    img /= np.max(img)
                p = extract_patches_2d(img, (33,33))
                plist.append(p)
            patches = np.array(zip(np.array(plist[0]), np.array(plist[1]), np.array(plist[2]), np.array(plist[3])))

    # predict classes of each pixel based on model
            full_pred = self.model_comp.predict_classes(patches)
            fp1 = full_pred.reshape(208,208)

但是根据 github链接,predict_classes()不可用.所以我的问题还有其他可以尝试的替代方法吗?

But according to the github-link predict_classes() is unavailable. So my question is there any other alternative that I can try?

推荐答案

实际上,predict_classes不适用于功能模型,因为在某些情况下使用它可能没有意义. 但是,存在一种单一衬里"解决方案:

Indeed, predict_classes is not available for functionnal models as it might not make sense to use it in some cases. However, a "one liner" solution exists to this :

y_classes = keras.utils.np_utils.probas_to_classes(self.model_comp.predict(patches))

这在keras 1.2.2中有效,不确定keras 2.0,我在源代码中找不到该功能.但是,实际上没有什么可遮挡的,您的模型输出一个属于每个类的概率向量.该函数所做的只是获取argmax并输出对应于最高概率的类core.

This works in keras 1.2.2, not sure about keras 2.0, I couldn't find the function in the source code. But there is really nothing shady about this, your model outputs a vector of probabilities to belonging to each class. What the function does is just take the argmax and outputs the class coresponding to the highest probability.

我希望这会有所帮助.

这篇关于功能性API Keras预测方案的替代解决方案()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆