- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 pytorch 中修改了 VGG16 以在特征提取器中插入 BN 和 dropout 等内容。当我将 forward 方法的定义从以下位置更改时,我偶然发现了一些奇怪的东西:
def forward(self, x):
x = self.model(x)
return x
到:
def forward(self, x):
x = self.model.features(x)
x = self.model.avgpool(x)
x = self.model.classifier(x)
return x
在第二种方法中,我现在收到一个错误,即矩阵的大小不匹配
mat1 dim 1 must match mat2 dim 0
)
class Vgg(nn.Module):
def __init__(self, n_classes, bias= None, dropout = 0.3):
super().__init__()
self.model = models.vgg16()
#self.bn64 = nn.BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
#self.bn128 = nn.BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
#self.bn256 = nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
#self.bn512 = nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
# change to allow 4 channels input
self.model.features[0] = nn.Conv2d(4, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3))
# remove/edit some of the first layers to make it more similar to Resnet
del self.model.features[2]
del self.model.features[2]
del self.model.features[-1]
self.model.features[2] = nn.MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)
# add dropout
for m in self.model.modules():
if isinstance(m, nn.Dropout):
m.p = dropout
else:
pass
self.dropout = nn.Dropout(p=dropout)
self.r = nn.ReLU(inplace=True)
modules = nn.Sequential(*[self.model.features[0],
#self.bn64,
self.model.features[1:3],
#self.bn64,
self.model.features[3:5],
#self.dropout,
self.model.features[5],
#self.bn128,
self.model.features[6:8],
#self.bn128,
self.model.features[8:10],
#self.dropout,
self.model.features[10],
#self.bn256,
self.model.features[11:13],
#self.bn256,
self.model.features[13:15],
#self.bn256,
self.model.features[15:17],
#self.dropout,
self.model.features[17],
#self.bn512,
self.model.features[18:20],
#self.bn512,
self.model.features[20:22],
#self.bn512,
self.model.features[22:24],
#self.dropout,
self.model.features[24],
#self.bn512,
self.model.features[25:27],
#self.bn512,
self.model.features[27:29],
#self.bn512,
#self.dropout
nn.MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
])
self.model.features = modules
# change the pooling layer
self.model.avgpool = nn.AdaptiveAvgPool2d(output_size=(1, 1))
# set output to correct num classes
self.model.classifier = nn.Linear(in_features=512, out_features=n_classes, bias=True)
# use predefined bias
if bias is not None:
assert isinstance(bias, torch.Tensor), 'bias must be tensor'
self.model.classifier.bias = nn.Parameter(bias)
def forward(self, x):
x = self.model.features(x)
x = self.model.avgpool(x)
x = self.model.classifier(x)
return x
我知道它看起来非常丑陋和hacky。我试图重新编写它,但重新编写的版本由于某种原因也不起作用,我假设当前的问题也与此有关。我认为输入并没有像我认为的那样通过 forward 方法提供。我的假设是调用
x = self.model(x)
不会在我制作的所有已编辑图层中运行“x”,否则我会在上面的两个版本的 forward 方法中得到相同的行为。但我的问题是,当我调用
self.model(x)
时会发生什么?在前进?它是通过 pytorch 的原始 vgg16 运行输入吗?因为当我打印
self.model
在我的控制台中,它显示了我对
self.model.features
的架构所做的更改以及
self.model.avgpool
和
self.model.classifier
.
t
是我为处理训练步骤而制作的类(class)(因此循环训练和验证模式等)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-7-0b3983ae9702> in <module>
77 print_cl_met = True
78 )
---> 79 model = t.run()
80 t.save_to_json()
81 print(np.max(np.array(t.f1_tracker)))
/home/stevea/treesat/TreeSat/TreeSat/trainers/basetrainer.py in run(self)
342 for phase in ['training', 'testing']:
343 self.phase = phase
--> 344 self.model_mode()
345
346 if self.phase == 'testing':
/home/stevea/treesat/TreeSat/TreeSat/trainers/basetrainer.py in model_mode(self)
154 if self.phase == 'training':
155 print('*********TRAINING PHASE*********')
--> 156 self.trainModel()
157 else:
158 print('*********VALIDATION PHASE*********')
/home/stevea/treesat/TreeSat/TreeSat/trainers/basetrainer.py in trainModel(self)
234 # loop through all batches to perform an epoch
235 for loaded in self.loaders[self.phase]:
--> 236 epoch_loss = self.train_step(loaded, epoch_loss)
237
238 mean_loss = np.mean(np.array(epoch_loss))
/home/stevea/treesat/TreeSat/TreeSat/trainers/basetrainer.py in train_step(self, loaded, epoch_loss)
262
263 # process batch through network
--> 264 self.out = self.model(self.img_batch.float())
265
266 # get loss value
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
887 result = self._slow_forward(*input, **kwargs)
888 else:
--> 889 result = self.forward(*input, **kwargs)
890 for hook in itertools.chain(
891 _global_forward_hooks.values(),
/home/stevea/treesat/TreeSat/TreeSat/models/vgg.py in forward(self, x)
143 x = self.model.features(x)
144 x = self.model.avgpool(x)
--> 145 x = self.model.classifier(x)
146 return x
147
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
887 result = self._slow_forward(*input, **kwargs)
888 else:
--> 889 result = self.forward(*input, **kwargs)
890 for hook in itertools.chain(
891 _global_forward_hooks.values(),
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/linear.py in forward(self, input)
92
93 def forward(self, input: Tensor) -> Tensor:
---> 94 return F.linear(input, self.weight, self.bias)
95
96 def extra_repr(self) -> str:
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in linear(input, weight, bias)
1751 if has_torch_function_variadic(input, weight):
1752 return handle_torch_function(linear, (input, weight), input, weight, bias=bias)
-> 1753 return torch._C._nn.linear(input, weight, bias)
1754
1755
RuntimeError: mat1 dim 1 must match mat2 dim 0
我还尝试在 forward 方法的每一步打印出 x 的形状:
def forward(self, x):
x = self.model.features(x)
print(x.shape)
x = self.model.avgpool(x)
print(x.shape)
x = self.model.classifier(x)
print(x.shape)
return x
它向我展示了形状似乎很好,因为分类器应该采用 512 个特征:
torch.Size([64, 512, 4, 4])
torch.Size([64, 512, 1, 1])
最佳答案
我无法运行您的代码,但我认为问题在于线性层需要 2d 数据输入(因为它实际上是矩阵乘法),而您提供 4d 输入(尺寸为 1 的暗淡 2 和 3)。
请尝试squeeze
def forward(self, x):
x = self.model.features(x)
x = self.model.avgpool(x)
x = torch.squeeze(x)
x = self.model.classifier(x)
return x
关于python - 难以理解修改后的 VGG16 前向方法 (Pytorch) 的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67870887/
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!