mobielnet提取特征图片比对

平时pytorch用得多,tf的代码还是弄了半天,网上的code不靠谱太多。当然得先down模型,clone tensorflow models,然后执行下代码里的export。

# encoding: utf-8

import os
import sys
import cv2
import glob
import numpy as np
import tensorflow as tf
import tensorflow.contrib.slim as slim
from nets import mobilenet_v1
import skimage
import skimage.io
import skimage.transform
from sklearn.preprocessing import Normalizer

# export PYTHONPATH="$PYTHONPATH:/ai/tensorflow/models/research/slim"

def load_image(path):
    # load image
    img = skimage.io.imread(path)
    img = img / 255.0
    assert (0 <= img).all() and (img <= 1.0).all()
    # print "Original Image Shape: ", img.shape
    # we crop image from center
    short_edge = min(img.shape[:2])
    yy = int((img.shape[0] - short_edge) / 2)
    xx = int((img.shape[1] - short_edge) / 2)
    crop_img = img[yy: yy + short_edge, xx: xx + short_edge]
    # resize to 224, 224
    resized_img = skimage.transform.resize(crop_img, (224, 224))
    return resized_img

if __name__ == "__main__":
    os.environ["CUDA_VISIBLE_DEVICES"] = "1"

    gpu_options = tf.GPUOptions(allow_growth=True)
    sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))

    norm2 = Normalizer(norm='l2')

    ckpt_path = './mobilenet_v1/mobilenet_v1_1.0_224.ckpt'

    img1 = load_image(sys.argv[1])
    img2 = load_image(sys.argv[2])

    batch1 = img1.reshape((1, 224, 224, 3))
    batch2 = img2.reshape((1, 224, 224, 3))
    batch = np.concatenate((batch1, batch2), 0)

    images = tf.placeholder("float", [2, 224, 224, 3])

    with tf.contrib.slim.arg_scope(mobilenet_v1.mobilenet_v1_arg_scope()):
        logits, endpoints = mobilenet_v1.mobilenet_v1(images, num_classes=1001)

        with tf.Session() as sess:
            saver = tf.train.Saver()
            saver.restore(sess, ckpt_path)

            fc_map = endpoints['AvgPool_1a']
            fc_feat = tf.squeeze(fc_map, [1, 2])

            feed_dict = { images: batch }
            fc_feature = sess.run(fc_feat, feed_dict=feed_dict)

            norm_feas = norm2.fit_transform(fc_feature)

            print(np.matmul(norm_feas[0], norm_feas[1].T))

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中可以使用OpenCV库进行图片特征比对。 首先,我们需要导入OpenCV库和所需的辅助库。然后,读取待比对的两张图片: ```python import cv2 import numpy as np # 读取图片 image1 = cv2.imread("image1.jpg") image2 = cv2.imread("image2.jpg") ``` 接下来,我们可以使用SIFT算法提取图片特征点和特征描述符。SIFT算法可以在图片中找到关键点,而特征描述符描述了关键点的局部特征。这样做是为了提取图像中的特征,将其用于比对。 ```python # 创建SIFT对象 sift = cv2.xfeatures2d.SIFT_create() # 检测关键点和计算描述符 keypoints1, descriptors1 = sift.detectAndCompute(image1, None) keypoints2, descriptors2 = sift.detectAndCompute(image2, None) ``` 然后,我们可以使用FLANN算法进行特征匹配。FLANN是快速最近邻搜索库,用于在两组特征向量之间进行快速匹配。 ```python # 设置FLANN算法参数 index_params = dict(algorithm=1, trees=5) search_params = dict(checks=50) # 创建FLANN匹配器 flann = cv2.FlannBasedMatcher(index_params, search_params) # 进行特征匹配 matches = flann.knnMatch(descriptors1, descriptors2, k=2) ``` 最后,我们可以根据匹配的特征点计算相似性度量值,例如比对的距离。一般情况下,如果两幅图像的特征点之间的距离小于一个阈值,则可以认为两幅图像是相似的。 ```python # 进行特征点筛选 good_matches = [] for m, n in matches: if m.distance < 0.7 * n.distance: good_matches.append(m) # 计算比对的距离 similarity = len(good_matches) / min(len(keypoints1), len(keypoints2)) ``` 以上就是使用Python进行图片特征比对的步骤。通过提取特征点和特征描述符,并使用FLANN算法进行匹配,我们可以得到两张图片之间的相似度量值,用于比较它们的相似程度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值