Python+Pytorch掌纹训练识别

程序示例精选
Python+Pytorch掌纹训练识别
如需安装运行环境或远程调试,见文章底部个人QQ名片,由专业技术人员远程协助!

前言

这篇博客针对《Python+Pytorch掌纹训练识别》编写代码,代码整洁,规则,易读。 学习与应用推荐首选。


文章目录

一、所需工具软件
二、使用步骤
1. 主要代码
2. 运行结果
三、在线协助

一、所需工具软件

1. Python
2. Pycharm

二、使用步骤

代码如下(示例):
cpp 复制代码
import torch 
from torch.utils.data import Dataset, DataLoader
import numpy as np
from models import MyDataset
from models import compnet
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print('\ndevice-> ', device, '\n\n')
test_set = './data/test.txt'
testset =MyDataset(txt=test_set, transforms=None, train=False)
batch_size = 1
data_loader_test = DataLoader(dataset=testset, batch_size=batch_size, shuffle=False)
net = compnet(num_classes=600) # IITD: 460    KTU: 145    Tongji: 600    REST: 358    XJTU: 200
#net.load_state_dict(torch.load('./pretrained models/net_params.pth'))
net.load_state_dict(torch.load('./pretrained models/net_params_best.pth'))
net.to(device)
net.eval()
# feature extraction:
featDB_test = []
iddb_test = []
with torch.no_grad():
    for batch_id, (data, target) in enumerate(data_loader_test):
        data = data.to(device)
        target = target.to(device)
        # feature extraction
        codes = net.getFeatureCode(data) 
        codes = codes.cpu().detach().numpy()
        y = target.cpu().detach().numpy()
        if batch_id == 0:
            featDB_test = codes
            iddb_test =  y
        else:
            featDB_test = np.concatenate((featDB_test, codes), axis=0)
            iddb_test = np.concatenate((iddb_test, y))
print('completed feature extraction for test set.')
print('(number of samples, feature vector dimensionality): ', featDB_test.shape)
print('\n')
# feature matching: feat1 vs feat2
cosdis =np.dot(feat1,feat2)
dis = np.arccos(np.clip(cosdis, -1, 1))/np.pi # 0~1
print('matching distance, label1 vs label2: \t%.2f, %d vs %d'%(dis, iddb_test[0], iddb_test[1]))

# feature matching: feat1 vs feat3 
cosdis =np.dot(feat1,feat3)
dis = np.arccos(np.clip(cosdis, -1, 1))/np.pi

print('matching distance, label1 vs label3: \t%.2f, %d vs %d'%(dis, iddb_test[0], iddb_test[-1]))
# Match label1 with all other samples
# Match label1 with all other samples
feat1 = featDB_test[0]  # Feature vector of label1
label1 = iddb_test[0]   # Label of the first sample
for i in range(1, len(featDB_test)):
    feat_other = featDB_test[i]  # Feature vector of the other sample
    label_other = iddb_test[i]   # Label of the other sample
    
    # Compute cosine similarity and convert to distance
    cosdis = np.dot(feat1, feat_other)
    dis = np.arccos(np.clip(cosdis, -1, 1)) / np.pi  # Distance in [0, 1]
   
运行结果

三、在线协助:

如需安装运行环境或远程调试,见文章底部个人 QQ 名片,由专业技术人员远程协助!

1)远程安装运行环境,代码调试
2)Visual Studio, Qt, C++, Python编程语言入门指导
3)界面美化
4)软件制作
5)云服务器申请
6)网站制作

当前文章连接: https://blog.csdn.net/alicema1111/article/details/132666851
个人博客主页https://blog.csdn.net/alicema1111?type=blog
博主所有文章点这里: https://blog.csdn.net/alicema1111?type=blog

博主推荐:
Python人脸识别考勤打卡系统:
https://blog.csdn.net/alicema1111/article/details/133434445
Python果树水果识别https://blog.csdn.net/alicema1111/article/details/130862842
Python+Yolov8+Deepsort入口人流量统计: https://blog.csdn.net/alicema1111/article/details/130454430
Python+Qt人脸识别门禁管理系统: https://blog.csdn.net/alicema1111/article/details/130353433
Python+Qt指纹录入识别考勤系统: https://blog.csdn.net/alicema1111/article/details/129338432
Python Yolov5火焰烟雾识别源码分享: https://blog.csdn.net/alicema1111/article/details/128420453
Python+Yolov8路面桥梁墙体裂缝识别: https://blog.csdn.net/alicema1111/article/details/133434445
Python+Yolov5道路障碍物识别: https://blog.csdn.net/alicema1111/article/details/129589741
Python+Yolov5跌倒检测 摔倒检测 人物目标行为 人体特征识别: https://blog.csdn.net/alicema1111/article/details/129272048

相关推荐
LuckyLay1 小时前
1.1.2 运算符与表达式——AI教你学Django
python·django
学不会就看1 小时前
Django--01基本请求与响应流程
后端·python·django
AI+程序员在路上1 小时前
Qt6中模态与非模态对话框区别
开发语言·c++·qt
在猴站学算法3 小时前
机器学习(西瓜书) 第二章 模型评估与选择
人工智能·机器学习
科技宅说4 小时前
36氪专访丨乐橙CEO谢运:AI科技下的业务创新与长期主义下的品牌坚守
人工智能·科技
学术小八5 小时前
2025年人工智能、虚拟现实与交互设计国际学术会议
人工智能·交互·vr
nbsaas-boot6 小时前
Java 正则表达式白皮书:语法详解、工程实践与常用表达式库
开发语言·python·mysql
仗剑_走天涯6 小时前
基于pytorch.nn模块实现线性模型
人工智能·pytorch·python·深度学习
chao_7896 小时前
二分查找篇——搜索旋转排序数组【LeetCode】两次二分查找
开发语言·数据结构·python·算法·leetcode
风无雨6 小时前
GO 启动 简单服务
开发语言·后端·golang