Closed 587687525 closed 1 year ago
hello @587687525 , is the Conv2d
in your model not a pytorch built-in torch.nn.Conv2d
?
@J-shang hi,My Conv2d has been wrapped once.It looks like this:
class Conv2d(nn.Module): def __init__(self, in_channels, out_channels, kernel_size, stride=1, dilation=1, groups=1, padding='same', bias=False, bn=True, relu=False): super(Conv2d, self).__init__() if '__iter__' not in dir(kernel_size): kernel_size = (kernel_size, kernel_size) if '__iter__' not in dir(stride): stride = (stride, stride) if '__iter__' not in dir(dilation): dilation = (dilation, dilation)
if padding == 'same':
width_pad_size = kernel_size[0] + (kernel_size[0] - 1) * (dilation[0] - 1)
height_pad_size = kernel_size[1] + (kernel_size[1] - 1) * (dilation[1] - 1)
elif padding == 'valid':
width_pad_size = 0
height_pad_size = 0
else:
if '__iter__' in dir(padding):
width_pad_size = padding[0] * 2
height_pad_size = padding[1] * 2
else:
width_pad_size = padding * 2
height_pad_size = padding * 2
width_pad_size = width_pad_size // 2 + (width_pad_size % 2 - 1)
height_pad_size = height_pad_size // 2 + (height_pad_size % 2 - 1)
pad_size = (width_pad_size, height_pad_size)
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride, pad_size, dilation, groups, bias=bias)
self.reset_parameters()
if bn is True:
self.bn = nn.BatchNorm2d(out_channels)
else:
self.bn = None
if relu is True:
self.relu = nn.ReLU(inplace=True)
else:
self.relu = None
def forward(self, x):
x = self.conv(x)
if self.bn is not None:
x = self.bn(x)
if self.relu is not None:
x = self.relu(x)
return x
def reset_parameters(self):
nn.init.kaiming_normal_(self.conv.weight)
@587687525 Do you still have any questions?
I still don't know how to solve my error.
hello @587687525 , the fastest workaround is renaming your customized Conv2d
, i.e. Conv2d -> FusionConv2d
Had you tried Ning's suggestions? Expect your reply! @587687525
您好@587687525,最快的解决方法是重命名您的自定义,即
Conv2d``Conv2d -> FusionConv2d
I have solved the problem with this method, thank you very much. But my code is still giving errors elsewhere and I want to know how to fix them.#5436
Describe the bug: I'm new to using nni, and I can't guarantee if there are other problems with my code, but I can't fix the current problem, so I'm here to ask. I'm using the nni AutoCompressPruner to compression of the model on https://github.com/plemeri/InSPyReNet. If it is convenient, please help me complete the compressed code of InSPyReNet model, thanks. The following error appeared :
Environment:
Reproduce the problem
import nni import numpy as np import torch from PIL import Image from nni.algorithms.compression.pytorch.pruning import AutoCompressPruner from torch.nn import ParameterList from torch.nn import functional as F from torch.optim import Adam from torch.utils.data.distributed import DistributedSampler from torchvision import transforms
from lib.InSPyReNet import RhNet_SwinB from lib.optim import PolyLr from utils.dataloader import RGB_Dataset from utils.eval import SegmentationMetric from utils.misc import load_config, to_cuda
filepath = osp.split(osp.abspath(file))[0] repopath = osp.split(filepath)[0] sys.path.append(repopath)
torch.backends.cuda.matmul.allow_tf32 = False torch.backends.cudnn.allow_tf32 = False
def trainer(model, optimizer, criterion=None): global opt, args
def finetuner(model): backbone_params = ParameterList() decoder_params = ParameterList()
def evaluator(model): model.eval()
def main(opt, args): model = InSPyReNet_SwinB(**opt.Model) model.load_state_dict(torch.load(args.weights, map_location=torch.device('cpu')), strict=True)
if name == 'main': parser = argparse.ArgumentParser() parser.add_argument('--weights', type=str, default=r"./weights/InSPyReNet.pth", help="weights path") parser.add_argument('--gpu', '-g', action='store_true', default=True) parser.add_argument('--config', '-c', type=str, default='configs/RhineSOD.yaml') parser.add_argument('--imgsize', type=int, default=320, help='input image size') parser.add_argument('--thres', type=int, default=50) parser.add_argument('--original_path', type=str, default=r"G:\ML-Dataset\DUTS-TR\images", help="input image path") parser.add_argument('--label_path', type=str, default=r"G:\ML-Dataset\DUTS-TR\masks", help="input image path") parser.add_argument('--mask_path', type=str, default="./outputs/mask", help="output masked path") args = parser.parse_args()