Keras从不同图像中提取相同特征 [英] Keras Same Feature Extraction from Different Images

查看:264
本文介绍了Keras从不同图像中提取相同特征的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Keras的预训练模型在两幅图像中进行特征提取,但是它们给出了相同的结果(array_equal = True).我尝试了其他模型,例如VGG16和Resnet50,但结果相同.我写的代码错了吗?还是预训练模型的局限性?我有什么办法可以提取不同的功能?谢谢!

I'm using Keras' pre-trained model for feature extraction in two images, however they gave the same outcome (array_equal = True). I've tried other model like VGG16 and Resnet50 but the results are the same. Am I writing the code wrong or is it the limitation of pre-trained model? Is there anything I can do to extract different features? Thanks!

import cv2
from keras.applications.inception_v3 import InceptionV3
from keras.applications.inception_v3 import preprocess_input

model = InceptionV3(weights='imagenet', include_top=False)

def get_img_vector(path):

    im = cv2.imread(path)
    im = cv2.resize(im,(224,224))
    img = preprocess_input(np.expand_dims(im.copy(), axis=0))
    resnet_feature = model.predict(img)

    return np.array(resnet_feature)

arr1 = get_img_vector('image1.png')
arr2 = get_img_vector('image2.png')

np.array_equal(arr1,arr2)

下面是我的两张图片:

推荐答案

我认为png的文件格式会造成图像加载问题.当前,cv2.imread一个png文件,而cv2.imshow则导致黑屏,这使两个图像相同.将文件从png保存到jpg,然后重试.

I think that the file format png create the image loading problem. Currently cv2.imread a png file and cv2.imshow it result in a black screen, which make two images identical. Saving the file from png to jpg and trying it again.

这篇关于Keras从不同图像中提取相同特征的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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