图像特征——中篇

简介: 图像特征——中篇

图像特征

tt.png


和文本特征类似,图像特征也是梯度提升树模型非常难以挖掘的一类数据,目前图像相关的问题,例如图像分类,图像分割等等几乎都是以神经网络为主的模型,但是在一些多模态的问题中,例如商品搜索推荐的问题中,里面既包含图像信息又含有文本信息等,这个时候基于梯度提升树模型的建模方案还是至关重要的,这个时候为了更好地使用所有的数据信息,我们需要对图像特征进行多方位的提取。

tt.png

本节我们接着上一节10大特征之后再补充另外的一些最为经典的特征。



1.图像预训练特征

目前预训练的图像特征非常的多,典型的有:

  • VGG-16
  • resnet50
  • xception
  • inception_v3
  • EfficientNet
  • NFNet
  • 其它的,参见链接

预训练好的模型可以拿过来使用,我们仅仅只需要将图片转化为对应模型需要的输入形式,然后输入模型,一般我们会将模型预测结果亦或者是最后几层的特征拿出来作为图像的特征,该特征在多模态的数据竞赛中基本也是获奖选手使用最多的特征。

注:因为预训练的网络模型最终的输出结果往往是非常大的,这个时候可以考虑对其进行降维操作。

  • 抽取ResNet50的特征作为我们的图像特征。
1. from tensorflow.keras.applications import ResNet50
2. from tensorflow.keras.applications.resnet50 import preprocess_input
3. from PIL import Image 
4. from tensorflow.keras.preprocessing.image import load_img 
5. from tensorflow.keras.preprocessing.image import img_to_array
6. 
7. model = ResNet50(weights="imagenet", include_top=False) 
8. img   = load_img('./imgs/chapter7/img_example.jpeg', target_size=(224, 224))
9. img   = img_to_array(img)
10. img = np.expand_dims(img, axis=0)
11. img   = preprocess_input(img)
12. res50_features = model.predict(img)
13. res50_features.shape
1. WARNING:tensorflow:6 out of the last 9 calls to <function Model.make_predict_function.<locals>.predict_function at 0x7f9109f04b80> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for  more details.
2. 
3. (1, 7, 7, 2048)
  • inception_v3的预测结果的TopN作为图像特征
1. import os 
2. import numpy as np
3. import pandas as pd
4. from tensorflow.keras.preprocessing import image
5. import tensorflow.keras.applications.resnet50 as resnet50
6. import tensorflow.keras.applications.xception as xception
7. import tensorflow.keras.applications.inception_v3 as inception_v3
8. inception_model = inception_v3.InceptionV3(weights='imagenet') 
9. 
10. def image_classify(model, pak, img, top_n=3):
11.     """Classify image and return top matches."""
12.     target_size = (299, 299)
13.     if img.size != target_size:
14.         img = img.resize(target_size)
15.     
16.     x = image.img_to_array(img) 
17.     x = np.expand_dims(x, axis=0)
18.     x = pak.preprocess_input(x)
19.     preds = model.predict(x)
20.     return pak.decode_predictions(preds, top=top_n)[0]
21. 
22. def classify_and_plot(image_path):
23.     """Classify an image with different models.
24.     Plot it and its predicitons.
25.     """
26.     img = Image.open(image_path)
27.     resnet_preds = image_classify(resnet_model, resnet50, img)
28.     xception_preds = image_classify(xception_model, xception, img)
29.     inception_preds = image_classify(inception_model, inception_v3, img)
30.     cv_img = cv2.imread(image_path)
31.     preds_arr = [('Resnet50', resnet_preds), ('xception', xception_preds), ('Inception', inception_preds)]
32.     return (img, cv_img, preds_arr)
1. img   = load_img('./imgs/chapter7/img_example.jpeg', target_size=(224, 224))
2. inception_preds = image_classify(inception_model, inception_v3, img)
3. inception_preds
1. WARNING:tensorflow:8 out of the last 11 calls to <function Model.make_predict_function.<locals>.predict_function at 0x7f9111cb1ee0> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for  more details.
2. 
3. [('n03933933', 'pier', 0.9737361),
4.  ('n03216828', 'dock', 0.0070415554),
5.  ('n09332890', 'lakeside', 0.0041139866)]



2.SIFT特征

SIFT特征是一种用于检测和描述数字图像中的局部特征的算法,SIFT特征是可以对抗不同变换(即同一个特征在不同变换下可能看起来不同)保持不变,也是在Deep Learning之前最为流行的算法。SIFT特征点提取较为方便,提取速度较快,对于图像的缩放等变换比较鲁棒。

tt.png

1. sift = cv2.SIFT_create() 
2. img = cv2.imread('./imgs/chapter7/img_example.jpeg')
3. kp, des = sift.detectAndCompute(img, None) 
4. img_kp = cv2.drawKeypoints(img, kp, img)
1. plt.figure(figsize=(10, 10))
2. plt.imshow(img_kp);



3.SURF特征

SIFT是非常好的特征,但是它的计算是比较缓慢的,为了提升SIFT的计算速度,Bay, H., Tuytelaars, T.以及Van Gool, L三人提出了一种新的算法叫做SURF(Speeded-Up Robust Features)。顾名思义,它是SIFT的加速版。

分析表明,SURF它比SIFT快3倍,性能与SIFT相当。SURF擅长处理模糊和旋转的图像,但不擅长处理视点变化和光照变化。

1. ### 因为版权问题,有的版本里面没有SURF了,如果要使用需要对版本进行调整,
2. ### SURF implementations are no longer included in the OpenCV 3 library by default.
3. 
4. surf = cv2.xfeatures2d.SURF_create(400)
5. # Find keypoints and descriptors directly
6. kp, des = surf.detectAndCompute(img,None)



4.ORB特征

ORB特征在计算成本、匹配性能等方面都是SIFT和SURF的一个很好的替代选择。因为SIFT和SURF都是有专利的,使用的话需要付费。但是ORB特征则不需要。ORB融合了快速关键点检测器和BRIEF描述符,并进行了许多改进以提高性能。首先利用FAST算法寻找关键点,然后利用Harris角点测度寻找关键点中的前N个点,此外它还使用金字塔生成多尺度特征。

tt.png

1. orb = cv2.ORB_create()  # OpenCV 3 backward incompatibility: Do not create a detector with `cv2.ORB()`.
2. key_points, description = orb.detectAndCompute(img, None)
3. img_building_keypoints = cv2.drawKeypoints(img, 
4.                                            key_points, 
5.                                            img, 
6.                                            flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) # Draw circles.
7. plt.figure(figsize=(10, 10))
8. plt.title('ORB Interest Points')
9. plt.imshow(img_building_keypoints);



5.FAST特征

算法如SIFT、SURF提取到的特征非常优秀(有较强的不变性),但时间消耗较大大,如果实践使用的话可能无法满足我们的需求。Edward Rosten和Tom Drummond在2006年《Machine learning for high-speed corner detection》一文中提出了FAST特征点,并在2010年稍作修改后发表了《Features From Accelerated Segment Test》,简称FAST。

tt.png

1. path  = './imgs/chapter7/img_example.jpeg'
2. img    = cv2.imread(path)
3. fast = cv2.FastFeatureDetector_create(40)
4. # find and draw the keypoints
5. kp = fast.detect(img,None)
1. img2 = cv2.drawKeypoints(img, kp, None, color=(255,0,0))
2. # Print all default params
3. print( "Threshold: {}".format(fast.getThreshold()) )
4. print( "nonmaxSuppression:{}".format(fast.getNonmaxSuppression()) )
5. print( "neighborhood: {}".format(fast.getType()) )
6. print( "Total Keypoints with nonmaxSuppression: {}".format(len(kp)) )
7.
1. Threshold: 40
2. nonmaxSuppression:True
3. neighborhood: 2
4. Total Keypoints with nonmaxSuppression: 1483
1. plt.figure(figsize=(10, 10))
2. plt.title('FAST Interest Points')
3. plt.imshow(img2);

 



6.BEBLID特征

OpenCV 4.5.1中最令人兴奋的特性之一是BEBLID,它是一种新的描述符,它可以在减少执行时间的同时提高图像匹配精度。BEBLID是2020年引入的一种新的描述符,它已经被证明可以在多个任务中改善ORB。由于BEBLID适用于多种检测方法,因此必须将ORB关键点的比例设置为0.75~1。

在《Improving your image matching results by 14% with one line of code》作者的对比实验中,发现使用BEBLID描述符可以获得77.57%的inliers。如果我们在description单元格中注释BEBLID并使用ORB descriptor,结果将下降到63.20%:

1. import cv2
2. # Comment or uncomment to use ORB or BEBLID 
3. path  = './imgs/chapter7/img_example.jpeg'
4. img    = cv2.imread(path)
5. detector = cv2.ORB_create(10000)
6. kpts1 = detector.detect(img, None)
7. descriptor = cv2.xfeatures2d.BEBLID_create(0.75) 
8. kpts1, desc1 = descriptor.compute(img, kpts1)



7.图像聚集特征

扫描所有图片进行匹配,按照自定义的规则寻找出匹配上最多的图像的ID作为新的特征,用于寻找最近邻的图像。

tt.png

下面以SIFT特征为例,上面的SURF,ORB,FAST,BEBLID等也都可以用来寻找最近邻信息。

1. import numpy as np
2. import cv2
3. from matplotlib import pyplot as plt
4. '''
5.    https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_feature2d/py_matcher/py_matcher.html#matcher
6. '''
7. img1 = cv2.imread('./imgs/chapter7/img_example.jpeg',0)          # queryImage
8. img2 = cv2.imread('./imgs/chapter7/Apple.png',0) # trainImage
9. 
10. # Initiate SIFT detector
11. sift = cv2.SIFT_create() 
12. 
13. # find the keypoints and descriptors with SIFT
14. kp1, des1 = sift.detectAndCompute(img1,None)
15. kp2, des2 = sift.detectAndCompute(img2,None)
1. # FLANN parameters
2. FLANN_INDEX_KDTREE = 0
3. index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
4. search_params = dict(checks=50)   # or pass empty dictionary
5. lann = cv2.FlannBasedMatcher(index_params,search_params)
6. matches = flann.knnMatch(des1,des2,k=2)
7. 
8. # Need to draw only good matches, so create a mask
9. matchesMask = [[0,0] for i in range(len(matches))]
10. 
11. # ratio test as per Lowe's paper
12. for i,(m,n) in enumerate(matches):
13.     if m.distance < 0.7*n.distance:
14.         matchesMask[i]=[1,0]
15. 
16. draw_params = dict(matchColor = (0,255,0),
17.                    singlePointColor = (255,0,0),
18.                    matchesMask = matchesMask,
19.                    flags = 0)
20. 
21. img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,matches,None,**draw_params)
22. plt.figure(figsize=(10, 10))
23. plt.imshow(img3,)
<matplotlib.image.AxesImage at 0x7f9163d8a520>


目录
相关文章
|
编解码 自然语言处理 算法
开源版图生视频I2VGen-XL:单张图片生成高质量视频
VGen是由阿里巴巴通义实验室开发的开源视频生成模型和代码系列,具备非常先进和完善的视频生成系列能力
|
机器学习/深度学习 算法 PyTorch
论文阅读笔记 | 目标检测算法——DETR
论文阅读笔记 | 目标检测算法——DETR
2640 0
论文阅读笔记 | 目标检测算法——DETR
|
12月前
|
存储 人工智能 运维
AI重构知识管理:如何破解技术团队的6大效率困局
通过AI全链路赋能,实现技术文档智能生成、语义检索、隐性知识沉淀与企业级安全管控,破解研发中API文档低效、故障排查慢、知识复用难等痛点,提升文档效率300%、故障修复提速80%,助力团队从“被动管理”迈向“智能协同”,重构高效能研发新范式。
505 12
|
12月前
|
机器学习/深度学习 人工智能 自然语言处理
构建AI智能体:六十七、超参数如何影响大模型?通俗讲解原理、作用与实战示例
超参数是机器学习模型训练前需要人工设定的参数,它们控制着模型的学习过程而非直接通过学习获得。文章通过生动的类比(如自行车调整、烹饪配方)解释了超参数的概念,并详细介绍了其调优流程、常见类型(学习率、批量大小等)及对模型的影响。通过实际代码示例,展示了不同超参数设置如何影响模型训练效果,强调合理调优对提升模型性能、防止过拟合和优化资源使用的重要性。文章指出,超参数调优是模型成功的关键,初学者可从默认值开始逐步实验,借助网格搜索等工具实现高效调参。
952 105
|
12月前
|
存储 人工智能 缓存
硬核解析:AI 原生知识库系统如何击穿企业知识管理技术瓶颈
AI原生开源知识库系统,以RAG+NLP双引擎重构知识管理全链路。通过语义解析、智能检索、自动文档生成等技术,实现非结构化数据的高效结构化与精准召回,支持多源内容导入、跨系统集成及企业级安全管控,兼具高性能、低门槛与强扩展性,助力企业打破信息孤岛,释放知识资产价值。
|
机器学习/深度学习 并行计算 Android开发
Int8量化算子在移动端CPU的性能优化
Int8量化算子在移动端CPU的性能优化
918 0
|
机器学习/深度学习 前端开发 测试技术
数据集相关知识
数据集相关知识
1189 0
|
机器学习/深度学习 编解码 算法
什么是超分辨率?浅谈一下基于深度学习的图像超分辨率技术
超分辨率技术旨在提升图像或视频的清晰度,通过增加单位长度内的采样点数量来提高空间分辨率。基于深度学习的方法,如SRCNN、VDSR、SRResNet等,通过卷积神经网络和残差学习等技术,显著提升了图像重建的质量。此外,基于参考图像的超分辨率技术通过利用高分辨率参考图像,进一步提高了重建图像的真实感和细节。
|
存储 人工智能 文字识别
AI与OCR:数字档案馆图像扫描与文字识别技术实现与项目案例
本文介绍了纸质档案数字化的技术流程,包括高精度扫描、图像预处理、自动边界检测与切割、文字与图片分离抽取、档案识别与文本提取,以及识别结果的自动保存。通过去噪、增强对比度、校正倾斜等预处理技术,提高图像质量,确保OCR识别的准确性。平台还支持多字体识别、批量处理和结构化存储,实现了高效、准确的档案数字化。具体应用案例显示,该技术在江西省某地质资料档案馆中显著提升了档案管理的效率和质量。
2483 1
|
存储 DataWorks 数据处理
dataworks里面的stg层、ods层、dwd层、dws层、是怎么分层的呢?
【8月更文挑战第21天】dataworks里面的stg层、ods层、dwd层、dws层、是怎么分层的呢?
2017 7