基于Aidlux的图片相似度对比

利用深度神经网络,提取印章深度特征,同时学习印章之间的相似度,自己与自己相似,自己与其它不相似。

1.Siamese网络

Siamese网络是一种常用的深度学习相似性度量方法,它包含两个共享权重的CNN网络(说白了这两个网络其实就是一个网络,在代码中就构建一个网络就行了),将两个输入映射到同一特征空间,然后计算它们的距离或相似度一一使用共享的卷积层和全连接层,输出特征向量表示,然后计算相似度。

2.Triplet Loss网络

TripletLoss网络是一种通过比较三个样本之间的相似度来训练网络的方法。它包含三个共享权重的CNN网络,分别处理anchor、 positive和negative样本,其中positive样本与anchor相似与negative样本则不相似。通过三元组训练方法学习将同类别样本映射到相邻区域,不同类别样本映射到较远的区域。

3.本文方法

本文利用李生网络,把真章、假章同时输入进行学习,真与真相似度为1;真与假相似度为0,设计损失函数(结合BCELoss和Contrastive Loss) 进行模型训练。

训练步骤:

1.按上述格式放置数据集,放在dataset文件夹下。

2.将train.py当中的train_own_data设置成True。

3.运行train.py开始训练,可以观察对应step训练集和验证集的准确率。

将训练得到的.pth文件转换为onnx模型,再通过AIMO将onnx转换为tflite与dlc模型。

Aidlux平台部署

1.tfilte部署

import aidlite_gpu

import cv2

from cvs import *

import numpy as np

import os

import time

from PIL import Image

from contrast_utils.utils import letterbox_image, preprocess_input, cvtColor


def sigmoid(x):

    return 1 / (1+np.exp(-x))


if __name__ == "__main__":


    # 1.初始化aidlite类并创建aidlite对象

    aidlite = aidlite_gpu.aidlite()

    print("ok")


    # 2.加载模型

    w = h = 112

    input_shape = [w, h]

    in_shape = [ 1 * w * h * 3 * 4, 1 * w * h * 3 * 4]

    out_shape = [ 1 * 1 * 1 * 4]


    model_path = "/home/aidlux/model/tflite/vgg16_fixed_fp32.tflite"

    value = aidlite.ANNModel(model_path, in_shape, out_shape, 4, 0)

    print("gpu:", value)


    img1_pth = "/home/aidlux/test_imgs/test/false/beijing_2019-11-21_10406_200_200_seal.jpg"

    img2_pth = "/home/aidlux/test_imgs/test/true/beijing_0905_61269575.jpg"

    out = "result"

    os.makedirs(out, exist_ok=True)


    img10 = cv2.imread(img1_pth)

    img20 = cv2.imread(img2_pth)


    img1 = Image.fromarray(cv2.cvtColor(img10, cv2.COLOR_BGR2RGB))

    img2 = Image.fromarray(cv2.cvtColor(img20, cv2.COLOR_BGR2RGB))


    image_1 = letterbox_image(img1, [input_shape[1], input_shape[0]], False)

    image_2 = letterbox_image(img2, [input_shape[1], input_shape[0]], False)


    photo_1  = preprocess_input(np.array(image_1, np.float32))

    photo_2  = preprocess_input(np.array(image_2, np.float32))


    photo_1 = np.expand_dims(np.transpose(photo_1, (2, 0, 1)), 0)

    photo_2 = np.expand_dims(np.transpose(photo_2, (2, 0, 1)), 0)


    # 3.传入模型输入数据

    # input_data = np.array([photo_1, photo_2])

    aidlite.setInput_Float32(photo_1, index=0)

    aidlite.setInput_Float32(photo_2, index=1)


    # 4.执行推理

    start = time.time()

    aidlite.invoke()

    end = time.time()

    timerValue = (end - start) * 1000

    print("infer time(ms):{}".format(timerValue))


    # 5.获取输出

    pred = aidlite.getOutput_Float32(0)[0]

    print(pred)

    outs = round(sigmoid(pred), 9)

    print(outs)


    img_pair = np.hstack((cv2.resize(img10, (112,112)), cv2.resize(img20, (112,112))))

    h, w = img_pair.shape[:2]

    print('--+++', img_pair.shape)

    h, w = img_pair.shape[:2]

    cv2.putText(img_pair, 'sim:{}'.format(str(outs)), (0, h), cv2.FONT_ITALIC, 1, (255,255,0), 2)


    # from cvs import *

    cvs.imshow(img_pair)

    cv2.imwrite("/home/aidlux/res/adilux_tflite_img_pair.jpg", img_pair)

2.dlc部署

import aidlite_gpu

import cv2

from cvs import *

import numpy as np

import os

import time

from PIL import Image

from contrast_utils.utils import letterbox_image, preprocess_input, cvtColor


def sigmoid(x):

    return 1 / (1+np.exp(-x))


if __name__ == "__main__":


    # 1.初始化aidlite类并创建aidlite对象

    aidlite = aidlite_gpu.aidlite()

    print("ok")


    # 2.加载模型

    w = h = 112

    input_shape = [w, h]

    #rgb3通道 1个float是32位也就是4字节,每个数据4个字节, 4代表4个字节

    in_shape = [ 1 * w * h * 3 * 4,  1 * w * h * 3 * 4]

    out_shape = [1 * 1 * 1 * 4]


    model_path = "/home/aidlux/model/dlc/vgg16_fixed.dlc"

    # value = aidlite.ANNModel(model_path, in_shape, out_shape, numberOfThreads, enableNNAPI)

    #numberOfThreads- int类型。加载数据和模型所需要的核数,可选的数值为1,2,3,4

    # enableNNAPI -  int类型。选择模型的推理的方式,默认可选值为-1:在cpu上推理,0:在GPU上推理,1:混合模式推理,2:dsp推理模式


    value = aidlite.ANNModel(model_path, in_shape, out_shape, 4, 0) #不支持多输入

    # value = aidlite.FAST_ANNModel(model_path, in_shape, out_shape, 4, 0)

    print("gpu:", value)


    img1_pth = "/home/aidlux/test_imgs/test/false/beijing_2019-11-21_10406_200_200_seal.jpg"

    img2_pth = "/home/aidlux/test_imgs/test/true/beijing_0905_61269575.jpg"

    out = "result"

    os.makedirs(out, exist_ok=True)


    img10 = cv2.imread(img1_pth)

    img20 = cv2.imread(img2_pth)


    img1 = Image.fromarray(cv2.cvtColor(img10, cv2.COLOR_BGR2RGB))

    img2 = Image.fromarray(cv2.cvtColor(img20, cv2.COLOR_BGR2RGB))


    image_1 = letterbox_image(img1, [input_shape[1], input_shape[0]], False)

    image_2 = letterbox_image(img2, [input_shape[1], input_shape[0]], False)


    photo_1  = preprocess_input(np.array(image_1, np.float32))

    photo_2  = preprocess_input(np.array(image_2, np.float32))


    photo_1 = np.expand_dims(np.transpose(photo_1, (2, 0, 1)), 0)

    photo_2 = np.expand_dims(np.transpose(photo_2, (2, 0, 1)), 0)


    # 3.传入模型输入数据

    aidlite.setInput_Float32(photo_1, index=0)

    aidlite.setInput_Float32(photo_2, index=1)


    # 4.执行推理

    start = time.time()

    aidlite.invoke()

    end = time.time()

    timerValue = (end - start) * 1000

    print("infer time(ms):{}".format(timerValue))


    # 5.获取输出

    pred = aidlite.getOutput_Float32(0)[0]

    print(pred)

    outs = round(sigmoid(float(pred)), 9)

    print(outs)


    img_pair = np.hstack((cv2.resize(img10, (112,112)), cv2.resize(img20, (112,112))))

    h, w = img_pair.shape[:2]

    print('--+++', img_pair.shape)

    h, w = img_pair.shape[:2]

    cv2.putText(img_pair, 'sim:{}'.format(str(outs)), (0, h), cv2.FONT_ITALIC, 1, (0,0,255), 2)


    # from cvs import *

    cvs.imshow(img_pair)

    cv2.imwrite("/home/aidlux/res/adilux_dlc_img_pair.jpg", img_pair)

效果视频:

pth转onnx、onnx推理、tflite推理、转tflite以及转dlc过程:

模型转换推理过程_哔哩哔哩_bilibili

tflite部署:https://www.bilibili.com/video/BV1ZQ4y1p7iL/

dcl部署:https://www.bilibili.com/video/BV1oC4y137t1/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,695评论 6 515
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,569评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 168,130评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,648评论 1 297
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,655评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,268评论 1 309
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,835评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,740评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,286评论 1 318
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,375评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,505评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,185评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,873评论 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,357评论 0 24
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,466评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,921评论 3 376
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,515评论 2 359

推荐阅读更多精彩内容