Pytorch-yolov3: TypeError: narrow(): argument 'start' (position 2) must be int, not numpy.int64

Created on 15 May 2019  路  6Comments  路  Source: eriklindernoren/PyTorch-YOLOv3

Traceback (most recent call last):
File "train.py", line 101, in
for batch_i, (_, imgs, targets) in enumerate(dataloader):
File "/opt/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 336, in __next__
return self._process_next_batch(batch)
File "/opt/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 357, in _process_next_batch
raise batch.exc_type(batch.exc_msg)
TypeError: Traceback (most recent call last):
File "/opt/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 106, in _worker_loop
samples = collate_fn([dataset[i] for i in batch_indices])
File "/opt/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 106, in
samples = collate_fn([dataset[i] for i in batch_indices])
File "/home/jasonkurohara/cs231nproject/PyTorch-YOLOv3/utils/datasets.py", line 96, in __getitem__
img, pad = pad_to_square(img, 0)
File "/home/jasonkurohara/cs231nproject/PyTorch-YOLOv3/utils/datasets.py", line 23, in pad_to_square
img = F.pad(img, pad, "constant", value=pad_value)
File "/opt/anaconda3/lib/python3.7/site-packages/torch/nn/functional.py", line 2159, in pad
return ConstantPadNd.apply(input, pad, value)
File "/opt/anaconda3/lib/python3.7/site-packages/torch/nn/_functions/padding.py", line 40, in forward
c_output = c_output.narrow(i, p[0], c_output.size(i) - p[0])
TypeError: narrow(): argument 'start' (position 2) must be int, not numpy.int64

Anybody know how to fix this error? I am trying to train on a custom dataset.

Most helpful comment

it worked for me when I modified the below in datasets.py by adding the line hope this fixes your issue

def pad_to_square(img, pad_value):
c, h, w = img.shape
dim_diff = np.abs(h - w)
# (upper / left) padding and (lower / right) padding
pad1, pad2 = dim_diff // 2, dim_diff - dim_diff // 2
# Determine padding
pad = (0, 0, pad1, pad2) if h <= w else (pad1, pad2, 0, 0)
###### modification line added ######
pad = torch.from_numpy(np.array(pad))
###############################
# Add padding
img = F.pad(img, pad, "constant", value=pad_value)
return img, pad

All 6 comments

have u solved this promble锛焛 got the same errer...

first i use cpu to train my dataset and it can be trainedand but it's so slow that i can鈥榯 bear.then i try to use cuda mode to train and met the same promble as you opened.

same problem with me. It occurs on CUDA too.

it worked for me when I modified the below in datasets.py by adding the line hope this fixes your issue

def pad_to_square(img, pad_value):
c, h, w = img.shape
dim_diff = np.abs(h - w)
# (upper / left) padding and (lower / right) padding
pad1, pad2 = dim_diff // 2, dim_diff - dim_diff // 2
# Determine padding
pad = (0, 0, pad1, pad2) if h <= w else (pad1, pad2, 0, 0)
###### modification line added ######
pad = torch.from_numpy(np.array(pad))
###############################
# Add padding
img = F.pad(img, pad, "constant", value=pad_value)
return img, pad

no work for me ,I modified:
pad = (0, 0, int(pad1), int(pad2)) if h <= w else (pad1, pad2, 0, 0)
this works.

The pad code is outdated.

Was this page helpful?
0 / 5 - 0 ratings