filipradenovic / cnnimageretrieval-pytorch

CNN Image Retrieval in PyTorch: Training and evaluating CNNs for Image Retrieval in PyTorch
http://cmp.felk.cvut.cz/cnnimageretrieval
MIT License
1.43k stars 322 forks source link

TypeError: integer argument expected, got float #2

Closed qcl1994 closed 6 years ago

qcl1994 commented 6 years ago

Hi, I used python3 to train the network, but there seems to be something wrong. Could you give me some advice? Thank u!

qiuchenli@qiuchenli-GS43VR-7RE:~/deeplearn_loop/pytorch-cnnimageretrieval/cnnimageretrieval-pytorch$ python3 -m cirtorch.examples.train YOUR_EXPORT_DIR --gpu-id '0' --training-dataset 'retrieval-SfM-120k' --test-datasets 'roxford5k,rparis6k' --arch 'resnet101' --pool 'gem' --loss 'contrastive' --loss-margin 0.85 --optimizer 'adam' --lr 1e-6 --neg-num 5 --query-size=2000 --pool-size=20000 --batch-size 5 --image-size 362

Creating directory if it does not exist: 'YOUR_EXPORT_DIR/retrieval-SfM-120k_resnet101_gem_contrastive_m0.85_adam_lr1.0e-06_wd1.0e-04_nnum5_qsize2000_psize20000_bsize5_imsize362' Using pre-trained model 'resnet101' imageretrievalnet.py: for 'resnet101' custom pretrained features 'imagenet-caffe-resnet101-features-10a101d.pth' are used Evaluating network on test datasets... roxford5k: Extracting... roxford5k: database images...

4993/4993 done... roxford5k: query images... Traceback (most recent call last): File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main "main", mod_spec) File "/usr/lib/python3.5/runpy.py", line 85, in _run_code exec(code, run_globals) File "/home/qiuchenli/deeplearn_loop/pytorch-cnnimageretrieval/cnnimageretrieval-pytorch/cirtorch/examples/train.py", line 507, in main() File "/home/qiuchenli/deeplearn_loop/pytorch-cnnimageretrieval/cnnimageretrieval-pytorch/cirtorch/examples/train.py", line 240, in main test(args.test_datasets, model) File "/home/qiuchenli/deeplearn_loop/pytorch-cnnimageretrieval/cnnimageretrieval-pytorch/cirtorch/examples/train.py", line 444, in test qvecs = extract_vectors(net, qimages, image_size, transform, bbxs) File "/home/qiuchenli/deeplearn_loop/pytorch-cnnimageretrieval/cnnimageretrieval-pytorch/cirtorch/networks/imageretrievalnet.py", line 174, in extract_vectors for i, input in enumerate(loader): File "/usr/local/lib/python3.5/dist-packages/torch/utils/data/dataloader.py", line 210, in next return self._process_next_batch(batch) File "/usr/local/lib/python3.5/dist-packages/torch/utils/data/dataloader.py", line 230, in _process_next_batch raise batch.exc_type(batch.exc_msg) TypeError: Traceback (most recent call last): File "/usr/local/lib/python3.5/dist-packages/torch/utils/data/dataloader.py", line 42, in _worker_loop samples = collate_fn([dataset[i] for i in batch_indices]) File "/usr/local/lib/python3.5/dist-packages/torch/utils/data/dataloader.py", line 42, in samples = collate_fn([dataset[i] for i in batch_indices]) File "/home/qiuchenli/deeplearn_loop/pytorch-cnnimageretrieval/cnnimageretrieval-pytorch/cirtorch/datasets/genericdataset.py", line 56, in getitem img = self.transform(img) File "/home/qiuchenli/.local/lib/python3.5/site-packages/torchvision/transforms/transforms.py", line 49, in call img = t(img) File "/home/qiuchenli/.local/lib/python3.5/site-packages/torchvision/transforms/transforms.py", line 76, in call return F.to_tensor(pic) File "/home/qiuchenli/.local/lib/python3.5/site-packages/torchvision/transforms/functional.py", line 70, in to_tensor img = torch.ByteTensor(torch.ByteStorage.from_buffer(pic.tobytes())) File "/usr/lib/python3/dist-packages/PIL/Image.py", line 674, in tobytes self.load() File "/usr/lib/python3/dist-packages/PIL/Image.py", line 1961, in load self.im = self.im.crop(self.__crop) TypeError: integer argument expected, got float

filipradenovic commented 6 years ago

Hello, thank you for pointing this out.

This seems to be an issue with the PIL library and cropping, line #L52 in genericdataset.py. The bbx can be a float in our case, and PIL version 4.3.0 that we use is ok with the float input. However, for you this seems to be a problem. Can you please tell me which PIL version are you using because I cannot reproduce this error?

One solution would be to try and update PIL to a more recent version.

Second solution would be to replace #L52 with these two lines (I hope this will work):

bbx = tuple([int(round(b)) for b in self.bbx[index])
img = img.crop(bbx)

I currently have a limited computer access, so I cannot test the code and the update by myself, but I will try to do it as soon as possible and make a revision. I hope that my suggestions will help you so that you can continue working immediately. Let me know if it helped.

qcl1994 commented 6 years ago

Thank u for you quick response! My PIL library version is 3.1.2. I will try these two silutions!

filipradenovic commented 6 years ago

It seems that this is a known issue with PIL 3.1. and earlier, see PIL source code issue #1744 and #1745. This was fixed, so updating to a newer version, ideally 4. and later, is definitely going to fix this particual cropping issue.