NJU-LHRS / offical-SGSLN

The code of SGSLN
72 stars 6 forks source link

使用inference.py时报错 #10

Closed RooKichenn closed 6 months ago

RooKichenn commented 6 months ago

您好,我使用LEVIR-CD中test数据集进行inference时出现了如下错误:

Traceback (most recent call last):
  File "inference.py", line 119, in <module>
    train_net(dataset_name='../data/LEVIR_T/LEVIR', load_checkpoint=False)
  File "inference.py", line 75, in train_net
    cd_preds = net(batch_img1, batch_img2, log=True, img_name=name)
  File "/home/aipub/anaconda3/envs/eded/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1130, in _call_impl
    return forward_call(*input, **kwargs)
  File "/data2/RSCD/SGLSN/models/Models.py", line 141, in forward
    t1_2 = self.en_block2(t1_1, log=log, module_name='t1_1_en_block2', img_name=img_name)
  File "/home/aipub/anaconda3/envs/eded/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1130, in _call_impl
    return forward_call(*input, **kwargs)
  File "/data2/RSCD/SGLSN/models/dpcd_parts.py", line 211, in forward
    output = self.cbam(x, log=log, module_name=module_name + '-x_cbam', img_name=img_name)
  File "/home/aipub/anaconda3/envs/eded/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1130, in _call_impl
    return forward_call(*input, **kwargs)
  File "/data2/RSCD/SGLSN/models/dpcd_parts.py", line 184, in forward
    log_feature(log_list=log_list, module_name=module_name,
  File "/data2/RSCD/SGLSN/models/dpcd_parts.py", line 293, in log_feature
    log = F.interpolate(
  File "/home/aipub/anaconda3/envs/eded/lib/python3.8/site-packages/torch/nn/functional.py", line 3910, in interpolate
    return torch._C._nn.upsample_nearest2d(input, output_size, scale_factors)
RuntimeError: Input and output sizes should be greater than 0, but got input (H: 512, W: 512) output (H: 0, W: 0)

上面显示问题出在dpcd_parts.py中的F.interpolate,我感觉可能是参数scale_factor=ph.patch_size // h的问题,然后把ph.patch_size // h打印出来发现是0,之后我把scale_factor设置为了2或者0.5,也不行,我又把ph.patch_size设置为了1024(LEVIR-CD数据集是1024x1024),还是出现了一些错误。

最后我把下面这句代码中的log=True改为了log=False,inference.py就正常运行了,不过没有得到预测图,只得到了F1等各种指标,但是我想得到预测图,请问如何修改上述错误呢? cd_preds = net(batch_img1, batch_img2, log=True, img_name=name)

这是我的参数设置:

# dataset hyper-parameter
dataset_name = '../data/LEVIR_T/LEVIR'

# training hyper-parameter
epochs: int = 250  # Number of epochs
batch_size: int = 32  # Batch size
inference_ratio = 2  # batch_size in val and test equal to batch_size*inference_ratio
learning_rate: float = 1e-3  # Learning rate
factor = 0.1  # learning rate decreasing factor
patience = 12  # schedular patience
warm_up_step = 500  # warm up step
weight_decay: float = 1e-3  # AdamW optimizer weight decay
amp: bool = True  # if use mixed precision or not
load: str = "/data2/RSCD/data/LEVIR_T/LEVIR_best_f1score_model/best_f1_epoch100.pth"   # Load model and/or optimizer from a .pth file for testing or continuing training

max_norm: float = 20  # gradient clip max norm

# evaluate hyper-parameter
evaluate_epoch: int = 30  # start evaluate after training for evaluate epochs
stage_epoch = [0, 0, 0, 0, 0]  # adjust learning rate after every stage epoch
save_checkpoint: bool = True  # if save checkpoint of model or not
save_interval: int = 10  # save checkpoint every interval epoch
save_best_model: bool = True  # if save best model or not

# log wandb hyper-parameter
log_wandb_project: str = 'dpcd-1'  # wandb project name

# data transform hyper-parameter
noise_p: float = 0.3  # probability of adding noise

# model hyper-parameter
dropout_p: float = 0.1  # probability of dropout
patch_size: int = 256  # size of input image

y = 2  # ECA-net parameter
b = 1  # ECA-net parameter

# inference parameter
log_path = './log_feature/'
walking-shadow commented 6 months ago

scale_factor=ph.patch_size // h 变成了0应该是因为ph.patch_size小于h导致的,测试的时候我的LEVIR-CD图片大小是256×256,你可能用的是1024×1024,所以出现了问题,你可以根据自己的需要对图片进行裁剪,或者对代码进行调整