原文章:https://blog.csdn.net/littleyy666/article/details/130594501
请多多支持原作者,谢谢大家!
今天想复现一下经典的变化检测网络STANet,想要拿STANet当对比实验,只是换了一个数据集,结果总是报错dimension is 4194304 but corresponding boolean dimension is 65536:
Traceback (most recent call last):
File "./train.py", line 169, in
miou_current = val(opt, model)
File "./train.py", line 86, in val
score = model.test(val=True) # run inference
File "F:\wqcdcode\STANet-master\models\CDFA_model.py", line 79, in test
metrics.update(self.L.detach().cpu().numpy(), pred.detach().cpu().numpy())
File "F:\wqcdcode\STANet-master\util\metrics.py", line 121, in update
self.confusion_matrix += self.__fast_hist(lt.flatten(), lp.flatten())
File "F:\wqcdcode\STANet-master\util\metrics.py", line 108, in __fast_hist
hist = np.bincount(self.num_classes * label_gt[mask].astype(int) + label_pred[mask],
IndexError: boolean index did not match indexed array along dimension 0; dimension is 4194304 but corresponding boolean dimension is 65536
从网上查了很久也没有得到解决,心想应该是某个包的问题,因为通常的问题网上一般都有答案。在我又找了许久之后,终于在本文开头的原文章中找到了解决办法!(果然是包版本更新的问题)直接在models的CDFAmodel的85行前向传递函数中添加一句代码:self.dist = self.dist.permute(0, 3, 1, 2)具体解释大家可以去看原博主的解答。
def forward(self):
"""Run forward pass; called by both functions <optimize_parameters> and <test>."""
self.feat_A = self.netF(self.A) # f(A)
self.feat_B = self.netF(self.B) # f(B)
self.feat_A, self.feat_B = self.netA(self.feat_A,self.feat_B)
self.dist = F.pairwise_distance(self.feat_A, self.feat_B, keepdim=True) # 特征距离
self.dist = self.dist.permute(0, 3, 1, 2) #添加这句代码
self.dist = F.interpolate(self.dist, size=self.A.shape[2:], mode='bilinear',align_corners=True)
self.pred_L = (self.dist > 1).float()
# self.pred_L = F.interpolate(self.pred_L, size=self.A.shape[2:], mode='nearest')
self.pred_L_show = self.pred_L.long()
return self.pred_L
成功解决。
如果大家想要修改epoch的话,epoch在STANet代码的options文件夹下的train_options.py中以以下形式显示:
parser.add_argument('--niter', type=int, default=100, help='# of iter at starting learning rate')
parser.add_argument('--niter_decay', type=int, default=100, help='# of iter to linearly decay learning rate to zero')
--niter
指定了在开始时使用固定学习率进行训练的迭代次数,即模型在初始学习率下进行的迭代次数。--niter_decay
指定了学习率线性衰减到零所需的迭代次数。- 总epoch数 = niter + niter_decay
根据需要自行修改即可。batchsize和lr在运行终端的语句中就可以修改哦。