首页 > 其他分享 >代码实现-小样本-RN

代码实现-小样本-RN

时间:2023-07-23 17:46:59浏览次数:37  
标签:样本 omniglot 代码 py CLASS transforms File RN line

此篇为《Learning to Compare Relation Network for Few-Shot Learning》

只实现了基于Omniglot数据集的小样本代码
datas为数据集
models为训练好的模型
venv为配置文件
下面的py文件是具体实现代码

1.结构

image

2.问题: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'

由于linux和window路径的转换,需要把把'/'改成'\'即可。
image
image

修改一:
def get_class(self, sample):
return os.path.join(*sample.split('\\')[:-1])
修改二:

3.问题:IndexError: invalid index of a 0-dim tensor.

报错信息:
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

按要求改成
image

if (episode + 1) % 100 == 0:
print("episode:", episode + 1, "loss", loss.item())

4.问题:RuntimeError: output with shape [1, 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数据集时的报错,主要原因在于使用 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])
改成
image

normalize = transforms.Normalize(mean=[0.92206], std=[0.08426])
dataset = Omniglot(task,split=split,transform=transforms.Compose([Rotate(rotation)

5.问题:AttributeError: module 'torch. nn’has no attribute

报错信息:
Traceback (most recent call last):
File"D:/Omnight/omniglot/omniglot_train few shot.py",line 263, in <modulemainO
File"D:/Omnight/omniglot/omniglot train few shot.py",line 140,in main
if mn.path. exists(str("./models omniglot_feature_encoder_"+ str(CLASS _AM)way_"+ str(SANPLE_ANM_PER_CLASS) +'shot. phk1')):AttributeError: module 'torch. nn’has no attribute 'path'

torch.nn模块是PyTorch中用于神经网络构建和操作的核心模块,它包含了各种层、损失函数和激活函数等。并不包含文件或目录的处理函数,所以没有path函数。因此,更改为使用os.path.exists是一个正确的解决方法,以检查文件或目录是否存在。所以将nn改成os就行了
image

6.问题:IndexErrorscatter_(: Expected dtype int64 for index.

报错信息
"" vpTraceback (most recent call last) :
File "D;:/Omnight/omniglot/omniglot_train_few shot.py",line 264, in <module>
main()
File"D:/Omnight/omniglot/omniglot_train_few_shot.py",line 188, in main
one_hot_labels = Variable(torch. zeros (BATCA_NMM_PER_CLASS*CLASS_NM,CLASS _NUM .scatter_(l, batch_labels.vier-1,1),..ua(lFb)IndexErrorscatter_(: Expected dtype int64 for index.

scatter_()函数内的索引错误,此函数内部的参数必须是64位整数,改为
image

one_hot_labels = Variable(
            torch.zeros(BATCH_NUM_PER_CLASS * CLASS_NUM, CLASS_NUM).scatter_(1, batch_labels.view(-1, 1).long(), 1)).cuda(GPU)
loss = mse(relations, one_hot_labels)

7.问题:NotADirectoryBrror:[WinError 267]目录名称无效。:Alphabet_of_the_Magi\ .DS_Store

报错信息:
NotADirectoryBrror:[WinError 267]目录名称无效。:'.\ |datas\ omniglot_resized Alphabet_of_the_Magi\ .DS_Store

只要删除这个文件就行。
解释:
.DS_Store是Mac OS系统在文件夹中生成的隐藏文件,是特定于 Mac OS 系统的文件,是用于存储文件夹的元数据和自定义显示属性。如果出现与 .DS_Store 相关的报错,可能是因为程序在处理文件夹时意外地尝试读取或操作了 .DS_Store 文件,这可能是由于编程代码中没有正确处理隐藏文件的情况,或者没有明确地指定忽略这些文件。在数据集文件夹中删除这个文件就行。

标签:样本,omniglot,代码,py,CLASS,transforms,File,RN,line
From: https://www.cnblogs.com/ZarkY/p/17575277.html

相关文章

  • 7/23·afternoon
    问题A:魔法鲜花  http://www.jzoj.cn/problem.php?cid=5707&pid=0#include<bits/stdc++.h>usingnamespacestd;ints,t;intvis[100007];structnode{intnum;stringpath;};stringABC="ABC";intmain(){memset(vis,-1,sizeof(vis)......
  • 7/23·morning
    1248:DungeonMaster  http://ybt.ssoier.cn:8088/problem_show.php?pid=1248#include<bits/stdc++.h>usingnamespacestd;chara[103][103][103];intvis[103][103][103];intsx,sy,sz,ex,ey,ez;structqwert{intx,y,z;};intxx[6]={1,-1,0,0,0,0};in......
  • WEB漏洞—RCE代码及命令漏洞执行
    RCE:远程命令/代码执行(remotecommand/codeexecute)在 Web 应用中有时候程序员为了考虑灵活性、简洁性,会在代码调用代码或命令执行函数去处理。比如当应用在调用一些能将字符串转化成代码的函数时,没有考虑用户是否能控制这个字符串,将造成代码执行漏洞。同样调用系统命令处理......
  • 练习10.7 代码改错
    下面代码是否有错,怎么改1vector<int>vec;list<int>lst;inti;while(cin>>i) lst.push_back(i);copy(lst.cbegin(),lst.cend(),vec.begin());算法总是通过迭代器操作容器,不能直接向容器添加删除元素,无法改变元素的大小。对于copy算法,目标序列至少要和源序列有一样......
  • SpringBoot项目集成Mybatis Generator代码生成器
    添加依赖在项目的pom.xml文件中添加以下依赖<!--mybatisgenerator自动生成代码插件--><plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId>......
  • C++ extern关键字
    首先,一个文件中的变量或者函数,它的可视范围只在这个文件中,其他文件是不会知晓定义在另一个文件中的变量和函数的。extern关键字的作用就是,告知编译器,这里有一个变量或者函数的声明,它的定义你得去其他合作者那里去找。这就是所有了。C++或者C是一个组合多文件进行合作编程的语言......
  • Python代码玩遍童年的小游戏
    ​贪吃蛇,吃豆人,迷宫,井字游戏......这些小单击游戏我相信大家小的时候肯定玩过,或许在某个时段还沉迷过.随着年龄的增长,这些小游戏离我们越来越远,但是我相信大家的童心还是一直在的今天就分享一个真正可以玩耍的GitHub项目---free-python-games安装与使用python大家都......
  • C语言实现飞机大战手机游戏完整代码
    ​大一课设做的飞机大战手机游戏,可以进行登入和注册,这个是利用单链表做的,源代码已经给出,这个是最基本的飞机大战模式,我设置了几个功能,比如排行榜之类的。排行榜是用结构体数组做的,已及冒泡排序,并且在文件里保存信息。比较简单。#include"stdio.h"#include"windows.h"  //用......
  • 《代码整洁之道》第 3 章 函数
    第3章函数3.1短小函数的第一规则是短小。函数不该有100行那么长,20行封顶最佳。……每个函数都只有两行、三行或四行长。每个函数都一目了然。每个函数都只说一件事。而且,每个函数都依序把你带到下一个函数。这就是函数应该达到的短小程度!if语句、else语句、while......
  • PHP实现注册登录的详细代码,可供参考
    一、先看演示~  就输出个成功算了吧~ 二、代码代码里面注释写很详细了哦~废话不多说直接上代码~index.php<!DOCTYPEhtml><html><head><metacharset="utf-8"><metaname="viewport"content="width=device-width,initial-scale=1"><......