Learning to Compare: Relation Network 源码调试
CVPR 2018 的一篇少样本学习论文
Learning to Compare: Relation Network for Few-Shot Learning
源码地址:https://github.com/floodsung/LearningToCompare_FSL
在自己的破笔记本上跑了下这个源码,windows 系统,pycharm + Anaconda3 + pytorch-cpu 1.0.1
报了一堆bug, 总结如下:
procs_images.py里 ‘cp’报错
用procs_images.py处理 miniImangenet 数据集的时候:
报错信息:
/LearningToCompare_FSL-master/datas/miniImagenet/proc_images.py
'cp' �����ڲ����ⲿ���Ҳ���ǿ����еij������������ļ���
具体位置是
/datas/miniImagenet/procs_images.py Line 48:
os.system('cp images/' + image_name + ' ' + cur_dir)
这个‘cp’是linux环境运行的。
用windows系统的话要改成:
os.rename('images/' + image_name, cur_dir + image_name)
除此之外,所有的 os.system('mkdir ' + filename)
也要改成 os.mkdir(filename),虽然不一定会报错。
cpu RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False.
我的torch版本是是cpu, 所以把所有 .cuda(GPU)删了,另外
使用torch.load时添加 ,map_location ='cpu'
以miniImagenet_train_few_shots.py 为例
Line 150:
feature_encoder.load_state_dict(torch.load(str("./models/omniglot_feature_encoder_" + str(CLASS_NUM) +"way_" + str(SAMPLE_NUM_PER_CLASS) +"shot.pkl")))
改成
feature_encoder.load_state_dict(torch.load(str("./models/omniglot_feature_encoder_" + str(CLASS_NUM) +"way_" + str(SAMPLE_NUM_PER_CLASS) +"shot.pkl"),map_location = 'cpu'
))
Line:153:
relation_network.load_state_dict(torch.load(str("./models/miniimagenet_relation_network_"+ str(CLASS_NUM) +"way_" + str(SAMPLE_NUM_PER_CLASS) +"shot.pkl")))
改成
relation_network.load_state_dict(torch.load(str("./models/miniimagenet_relation_network_"+ str(CLASS_NUM) +"way_" + str(SAMPLE_NUM_PER_CLASS) +"shot.pkl"),map_location = 'cpu'))
KeyError: '..\\datas\\omniglot_resized'
报错信息:
File "LearningToCompare_FSL-master/omniglot/omniglot_train_few_shot.py", line 163, in main
task = tg.OmniglotTask(metatrain_character_folders,CLASS_NUM,SAMPLE_NUM_PER_CLASS,BATCH_NUM_PER_CLASS)
File "LearningToCompare_FSL-master\omniglot\task_generator.py", line 72, in <listcomp>
self.train_labels = [labels[self.get_class(x)] for x in self.train_roots]
KeyError: '..\\datas\\omniglot_resized'
关键的地方其实是在:
task_generator.py, line 74:
def get_class(self, sample):
return os.path.join(*sample.split('/')[:-1])
print (os.path.join(*sample.split('/')[:-1])) 结果是
..\datas\omniglot_resized
而labels是
{'../datas/omniglot_resized/Malay_(Jawi_-_Arabic)\\character25': 0, '../datas/omniglot_resized/Japanese_(hiragana)\\character15': 1, '…}
而 print(os.path.join(*sample.split('\\')[:-1])) 结果正是
../datas/omniglot_resized/Malay_(Jawi_-_Arabic)\character25
解决方法:把'/'改成'\\'即可
def get_class(self, sample):
return os.path.join(*sample.split('\\')[:-1])
RuntimeError: Expected object of scalar type Long but got scalar type Int for argument #3 'index'
报错信息:
File "/LearningToCompare_FSL-master/miniimagenet/miniimagenet_train_few_shot.py", line 193, in main
torch.zeros(BATCH_NUM_PER_CLASS * CLASS_NUM, CLASS_NUM).scatter_(1, batch_labels.view(-1, 1), 1))
RuntimeError: Expected object of scalar type Long but got scalar type Int for argument #3 'index'
解决方法:在前面加一句
batch_labels = batch_labels.long()
RuntimeError: Expected object of scalar type Long but got scalar type Int for argument #2 'other'
报错信息:
File "LearningToCompare_FSL-master/miniimagenet/miniimagenet_test_few_shot.py", line 247, in <listcomp>
rewards = [1 if predict_labels[j]==test_labels[j] else 0 for j in range(batch_size)]
RuntimeError: Expected object of scalar type Long but got scalar type Int for argument #2 'other'
解决方法:在前面加上
predict_labels = predict_labels.long()
test_labels = test_labels.long()
这两个好像是使用torch的数据格式问题
IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number
报错信息:
File "LearningToCompare_FSL-master/miniimagenet/miniimagenet_train_few_shot.py", line 212, in main
print("episode:",episode+1,"loss",loss.data[0])
IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number 按要求改成
print("episode:", episode + 1, "loss", loss.item())
就可以了
RuntimeError: output with shape [1, 28, 28] doesn't match the broadcast shape [3, 28, 28]
报错信息:
File "LearningToCompare_FSL-master\omniglot\task_generator.py", line 107, in __getitem__
image = self.transform(image)
File "...\Anaconda3\envs\python36\lib\site-packages\torchvision\transforms\transforms.py", line 60, in __call__
img = t(img)
File "...\Anaconda3\envs\python36\lib\site-packages\torchvision\transforms\transforms.py", line 163, in __call__
return F.normalize(tensor, self.mean, self.std, self.inplace)
File "...\Anaconda3\envs\python36\lib\site-packages\torchvision\transforms\functional.py", line 208, in normalize
tensor.sub_(mean[:, None, None]).div_(std[:, None, None])
RuntimeError: output with shape [1, 28, 28] doesn't match the broadcast shape [3, 28, 28]
这个是使用Omniglot数据集时的报错,主要原因在于
"\omniglot\task_generator.py", line 139: def get_data_loader(task, num_per_class=1, split='train',shuffle=True,rotation=0):
normalize = transforms.Normalize(mean=[0.92206, 0.92206, 0.92206], std=[0.08426, 0.08426, 0.08426])
dataset = Omniglot(task,split=split,transform=transforms.Compose([Rotate(rotation),transforms.ToTensor(),normalize]))
使用 torch.transforms 中 normalize 用了 3 通道,而实际使用的数据集Omniglot 图片大小是 [1, 28, 28]
解决方法:
把
normalize = transforms.Normalize(mean=[0.92206, 0.92206, 0.92206], std=[0.08426, 0.08426, 0.08426])
改成
normalize = transforms.Normalize(mean=[0.92206], std=[0.08426])
UserWarning: nn.functional.sigmoid is deprecated.
类似的warning 还有
UserWarning : torch.nn.utils.clip_grad_norm is now deprecated in favor of torch.nn.utils.clip_grad_norm_.
按要求改就行
torch.nn.utils.clip_grad_norm(feature_encoder.parameters(), 0.5)
改成
torch.nn.utils.clip_grad_norm_(feature_encoder.parameters(), 0.5) def forward里的
out = F.sigmoid(self.fc2(out))
改成
out = F.torch.sigmoid(self.fc2(out))
Learning to Compare: Relation Network 源码调试的更多相关文章
- Learning to Compare: Relation Network for Few-Shot Learning 论文笔记
主要原理: 和Siamese Neural Networks一样,将分类问题转换成两个输入的相似性问题. 和Siamese Neural Networks不同的是: Relation Network中 ...
- 开启Tomcat 源码调试
开启Tomcat 源码调试 因为工作的原因,需要了解Tomcat整个架构是如何设计的,正如要使用Spring MVC进行Web开发,需要了解Spring是如何设计的一样,有哪些主要的类,分别是用于干什 ...
- 在Eclipse中进行HotSpot的源码调试--转
原文地址:http://www.linuxidc.com/Linux/2015-05/117250.htm 在阅读OpenJDK源码的过程中,经常需要运行.调试程序来帮助理解.我们现在已经可以编译出一 ...
- [原创]在Windows和Linux中搭建PostgreSQL源码调试环境
张文升http://ode.cnblogs.comEmail:wensheng.zhang#foxmail.com 配图太多,完整pdf下载请点这里 本文使用Xming.Putty和VMWare几款工 ...
- SpringMVC DispatcherServlet 启动和加载过程(源码调试)
在阅读本文前,最好先阅读以下内容(当然,如果对 Servlet 已经有所了解,则可跳过): http://www.cnblogs.com/cyhbyw/p/8682078.html http://ww ...
- 《k8s-1.13版本源码分析》-源码调试
源码分析系列文章已经开源到github,地址如下: github:https://github.com/farmer-hutao/k8s-source-code-analysis gitbook:ht ...
- SpringBoot自动配置源码调试
之前对SpringBoot的自动配置原理进行了较为详细的介绍(https://www.cnblogs.com/stm32stm32/p/10560933.html),接下来就对自动配置进行源码调试,探 ...
- HashMap源码调试——认识"put"操作
前言:通常大家都知道HashMap的底层数据结构为数组加链表的形式,但其put操作具体是怎样执行的呢,本文通过调试HashMap的源码来阐述这一问题. 注:jdk版本:jdk1.7.0_51 1.pu ...
- .net源码调试 http://referencesource.microsoft.com/
其实关于.net源码调试 网上的资料已经很多了,我以前转载的文章有 VS2010下如何调试Framework源代码(即FCL) 和 如何使你的应用程序调试进.NET Framework 4.5源代码内 ...
随机推荐
- 算法笔记_103:蓝桥杯练习 算法提高 金明的预算方案(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些 ...
- HipHop PHP简介(转)
HipHop PHP是FaceBook的一个开源项目,它优化了FaceBook网站Web服务器的运行速度. HipHop 是一个源代码转换器.它将 PHP 代码转换为高度优化的 C++ 代码,然后再使 ...
- python基础教程_学习笔记12:充电时刻——模块
充电时刻--模块 python的标准安装包含一组模块,称为标准库. 模块 >>> import math >>> math.sin(0) 0.0 模块是程序 不论什 ...
- POJ2406-Power Strings(kmp循环节)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 31111 Accepted: 12982 D ...
- 泛型Where T 说明
约束 说明 where T : struct 对于结构约束,类型 T 必须是值类型 where T : class 类约束指定类型 T 必须是引用类型 where T : IFoo 指定类型 T 必须 ...
- 转发:【PHP】转义和过滤html单、双引号及HTML标签
一.单引号和双引号转义在PHP的数据存储过程中用得比较多,即往数据库里面存储数据时候需要注意转义单.双引号: 先说几个PHP函数: 1.addslashes — 使用反斜线引用(转义)字符串: 返回字 ...
- 15个极好的Linux find命令示例
基于访问/修改/更改时间查找文件 你可以找到基于以下三个文件的时间属性的文件. 访问时间的文件.文件访问时,访问时间得到更新. 的文件的修改时间.文件内容修改时,修改时间得到更新. 更改文件的时间.更 ...
- python-创建一个登录判断的函数
方法一def account_login(): password = input('Password:') if password == '12345': print('Login success!' ...
- ufldl学习笔记与编程作业:Multi-Layer Neural Network(多层神经网络+识别手写体编程)
ufldl学习笔记与编程作业:Multi-Layer Neural Network(多层神经网络+识别手写体编程) ufldl出了新教程,感觉比之前的好,从基础讲起,系统清晰,又有编程实践. 在dee ...
- 深入研究memcache 特性和限制
深入研究memcache 特性和限制在 Memcached中可以保存的item数据量是没有限制的,只要内存足够 .Memcached 单进程最大使用内存为2G,要使用更多内存,可以分多个端口开启多个M ...