pytorch imagenet测试代码
image_test.py
import argparse
import numpy as np
import sys
import os
import csv
from imagenet_test_base import TestKit
import torch class TestTorch(TestKit): def __init__(self):
super(TestTorch, self).__init__() self.truth['tensorflow']['inception_v3'] = [(22, 9.6691055), (24, 4.3524747), (25, 3.5957973), (132, 3.5657473), (23, 3.346283)]
self.truth['keras']['inception_v3'] = [(21, 0.93430489), (23, 0.002883445), (131, 0.0014781791), (24, 0.0014518998), (22, 0.0014435351)] self.model = self.MainModel.KitModel(self.args.w)
self.model.eval() def preprocess(self, image_path):
x = super(TestTorch, self).preprocess(image_path)
x = np.transpose(x, (2, 0, 1))
x = np.expand_dims(x, 0).copy()
self.data = torch.from_numpy(x)
self.data = torch.autograd.Variable(self.data, requires_grad = False) def print_result(self, image_name, top1, top5):
predict = self.model(self.data)
predict = predict.data.numpy()
return super(TestTorch, self).print_result(predict, image_name, top1, top5) def print_intermediate_result(self, layer_name, if_transpose=False):
intermediate_output = self.model.test.data.numpy()
super(TestTorch, self).print_intermediate_result(intermediate_output, if_transpose) def inference(self, images): with open(images) as fp_images:
images_file = csv.reader(fp_images, delimiter='\n')
top1 = 0.0
top5 = 0.0
image_count = 0
for image_name in images_file:
image_count += 1
image_path = "../data/imagenet/small_imagenet/"+image_name[0]
self.preprocess(image_path)
temp1, temp5 = self.print_result(image_name[0], top1, top5)
top1 = temp1
top5 = temp5
print("top1's accuracy : %f"%(top1/image_count))
print("top5's accuracy : %f"%(top5/image_count))
# self.print_intermediate_result(None, False)
# self.test_truth() def dump(self, path=None):
if path is None: path = self.args.dump
torch.save(self.model, path)
print('PyTorch model file is saved as [{}], generated by [{}.py] and [{}].'.format(
path, self.args.n, self.args.w)) if __name__=='__main__':
tester = TestTorch()
if tester.args.dump:
tester.dump()
else:
tester.inference(tester.args.image)
image_test_base.py:
请见上传的代码。 下载地址:https://files.cnblogs.com/files/jzcbest1016/imagenet_test_base.py.tar.gz
执行py文件时,需要终端输入参数:
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--preprocess', type=_text_type, help='Model Preprocess Type') # pytorch的测试程序, 这里为image_test.py
parser.add_argument('-n', type=_text_type, default='kit_imagenet',
help='Network structure file name.') # 模型结构测试文件
parser.add_argument('-s', type=_text_type, help='Source Framework Type',
choices=self.truth.keys()) # 框架类型:pytorch,tensorflow...
parser.add_argument('-w', type=_text_type, required=True,
help='Network weights file name') #模型结构文件
parser.add_argument('--image', '-i',
type=_text_type, help='Test image path.',
default="../data/file_list.txt" #图像路径
)
parser.add_argument('-l', '--label',
type=_text_type,
default='../data/val.txt',
help='Path of label.') #测试集类别
parser.add_argument('--dump',
type=_text_type,
default=None,
help='Target model path.') # 转化的目标模型文件的保存路径
parser.add_argument('--detect',
type=_text_type,
default=None,
help='Model detection result path.')
# tensorflow dump tag
parser.add_argument('--dump_tag',
type=_text_type,
default=None,
help='Tensorflow model dump type',
choices=['SERVING', 'TRAINING'])
pytorch imagenet测试代码的更多相关文章
- .NET单元测试的艺术-3.测试代码
开篇:上一篇我们学习单元测试和核心技术:存根.模拟对象和隔离框架,它们是我们进行高质量单元测试的技术基础.本篇会集中在管理和组织单元测试的技术,以及如何确保在真实项目中进行高质量的单元测试. 系列目录 ...
- mysql锁 实战测试代码
存储引擎 支持的锁定 MyISAM 表级锁 MEMORY 表级锁 InnoDB 行级锁 BDB 页面锁 表级锁:开销小,加锁快:不会出现死锁:锁定粒度大,发生锁冲突的概率最高,并发度最低.行级锁:开销 ...
- 使用Microsoft Fakes隔离测试代码
在单元测试(Unit Test)中我们遇到的问题之一是:假如被测试组件(类或项目)为A,组件A依赖于组件B,那么在组件A的单元测试ATest中测试A时,也需要依赖于B,在B发生改动后,就可能影响到A的 ...
- iOS开发:XCTest单元测试(附上一个单例的测试代码)
测试驱动开发并不是一个很新鲜的概念了.在我最开始学习程序编写时,最喜欢干的事情就是编写一段代码,然后运行观察结果是否正确.我所学习第一门语言是c语言,用的最多的是在算法设计上,那时候最常做的事情就是编 ...
- 在内核中异步请求设备固件firmware的测试代码
在内核中异步请求设备固件firmware的测试代码 static void ghost_load_firmware_callback(const struct firmware *fw, void * ...
- x264测试代码
建立一个工程,将头文件,库文件加载到工程,测试代码如下:#include <iostream>#include <string>#include "stdint.h& ...
- Android网络传输中必用的两个加密算法:MD5 和 RSA (附java完成测试代码)
MD5和RSA是网络传输中最常用的两个算法,了解这两个算法原理后就能大致知道加密是怎么一回事了.但这两种算法使用环境有差异,刚好互补. 一.MD5算法 首先MD5是不可逆的,只能加密而不能解密.比如明 ...
- Git合并开发代码分支到测试代码分支
——转载请注明出自天外归云的博客园 用TortoiseGit下载代码到本地 首先需要在本机安装好TortoiseGit.然后在随便哪个路径下比如D盘,右键“Git Clone”: 然后URL处选择项目 ...
- mvn编写主代码与测试代码
maven编写主代码与测试代码 3.2 编写主代码 项目主代码和测试代码不同,项目的主代码会被打包到最终的构件中(比如jar),而测试代码只在运行测试时用到,不会被打包.默认情况下,Maven假设项目 ...
随机推荐
- [LOJ2537] [PKUWC2018] Minimax
题目链接 LOJ:https://loj.ac/problem/2537 洛谷:https://www.luogu.org/problemnew/show/P5298 Solution 不定期诈尸 好 ...
- Sql Server 根据条件查找多条数据中最大值的详细记录
--(正常效果) select l.* from loadCurveSampling l left join Meter m on l.meter_id=m.Meter_ID --聚合当天最大值数据记 ...
- spring boot 使用GraphQL
在此之前需要简单了解GraphQL的基本知识,可通过以下来源进行学习 GraphQL官方中文网站 :https://graphql.cn GraphQL-java 官网:https://www.gra ...
- Spring Boot 集成 Swagger 生成 RESTful API 文档
原文链接: Spring Boot 集成 Swagger 生成 RESTful API 文档 简介 Swagger 官网是这么描述它的:The Best APIs are Built with Swa ...
- 阿里云OSS上传文件本地调试跨域问题解决
问题描述: 最近后台说为了提高上传效率,要前端直接上传文件到阿里云,而不经过后台.因为在阿里云服务器设置的允许源(region)为某个固定的域名下的源(例如*.cheche.com),直接在本地访问会 ...
- Oracle 检索数据(查询数据、select语句)
用户对表或视图最常进行的操作就是检索数据,检索数据可以通过 select 语句来实现,该语句由多个子句组成,通过这些子句完成筛选.投影和连接等各种数据操作,最终得到想要的结果. 语法: select ...
- 【cookie的使用】&【Session】
明确一点: cookie由服务器创建Cookie cookie=new Cookie("haha","xixi") 通过HtttpServletR ...
- 如何让django模型中的字段和model名显示为中文
如何让django模型中的字段和model名显示为中文:在模型中加入class Meta即可 class People(models.Model): name = models.CharField(n ...
- 用PL/SQL工具连接Oracle数据库的时报错:ORA-12638: 身份证明检索失败的解决方法
本地客户端用PLSQL Developer连接远程数据库时提示: ORA-12638: 身份证明检索失败!!! 解决方法一: 此目录下F:\myorcl\product\11.2.0\client_1 ...
- 前后端分离架构:Web实现前后端分离,前后端解耦
一.前言 ”前后端分离“已经成为互联网项目开发的业界标杆,通过Tomcat+Ngnix(也可以中间有个Node.js),有效地进行解耦.并且前后端分离会为以后的大型分布式架构.弹性计算架构.微服务架构 ...