sgrvinod / a-PyTorch-Tutorial-to-Image-Captioning

Show, Attend, and Tell | a PyTorch Tutorial to Image Captioning
MIT License
2.76k stars 713 forks source link

SourceChangeWarning #46

Closed 672401341 closed 5 years ago

672401341 commented 5 years ago

Problem: C:\Anaconda3\lib\site-packages\torch\serialization.py:425: SourceChangeWarning: source code of class 'torchvision.models.resnet.Bottleneck' has changed. yo u can retrieve the original source code by accessing the object's source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to r evert the changes. warnings.warn(msg, SourceChangeWarning) C:\Anaconda3\lib\site-packages\torch\serialization.py:425: SourceChangeWarning: source code of class 'torch.nn.modules.batchnorm.BatchNorm2d' has changed. you can retrieve the original source code by accessing the object's source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to revert the changes. warnings.warn(msg, SourceChangeWarning) C:\Anaconda3\lib\site-packages\torch\serialization.py:425: SourceChangeWarning: source code of class 'torchvision.models.resnet.Bottleneck' has changed. yo u can retrieve the original source code by accessing the object's source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to r evert the changes. warnings.warn(msg, SourceChangeWarning) C:\Anaconda3\lib\site-packages\torch\serialization.py:425: SourceChangeWarning: source code of class 'models.DecoderWithAttention' has changed. you can ret rieve the original source code by accessing the object's source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to revert the changes. warnings.warn(msg, SourceChangeWarning) C:\Anaconda3\lib\site-packages\torch\serialization.py:425: SourceChangeWarning: source code of class 'models.Attention' has changed. you can retrieve the o riginal source code by accessing the object's source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to revert the changes. warnings.warn(msg, SourceChangeWarning) C:\Anaconda3\lib\site-packages\torch\serialization.py:425: SourceChangeWarning: source code of class 'torch.nn.modules.linear.Linear' has changed. you can retrieve the original source code by accessing the object's source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to revert the changes. warnings.warn(msg, SourceChangeWarning) C:\Anaconda3\lib\site-packages\torch\serialization.py:425: SourceChangeWarning: source code of class 'torch.nn.modules.sparse.Embedding' has changed. you c an retrieve the original source code by accessing the object's source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to reve rt the changes. warnings.warn(msg, SourceChangeWarning) C:\Anaconda3\lib\site-packages\torch\serialization.py:425: SourceChangeWarning: source code of class 'torch.nn.modules.rnn.LSTMCell' has changed. you can r etrieve the original source code by accessing the object's source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to revert t he changes. warnings.warn(msg, SourceChangeWarning) Traceback (most recent call last): File "caption.py", line 222, in seq, alphas = caption_image_beam_search(encoder, decoder, args.img, word_map, args.beam_size) File "caption.py", line 45, in caption_image_beamsearch image = transform(img) # (3, 256, 256) File "C:\Anaconda3\lib\site-packages\torchvision\transforms\transforms.py", line 60, in call img = t(img) File "C:\Anaconda3\lib\site-packages\torchvision\transforms\transforms.py", line 163, in call return F.normalize(tensor, self.mean, self.std, self.inplace) File "C:\Anaconda3\lib\site-packages\torchvision\transforms\functional.py", line 208, in normalize tensor.sub(mean[:, None, None]).div_(std[:, None, None]) RuntimeError: Expected object of type torch.cuda.FloatTensor but found type torch.FloatTensor for argument #4 'other'

(empty) E:\Program Files\a-PyTorch-Tutorial-to-Image-Captioning> (empty) E:\Program Files\a-PyTorch-Tutorial-to-Image-Captioning>python caption.py --img=e:/paper.jpg --model=fake/BEST_checkpoint_coco_5_cap_per_img_5min word_freq.pth.tar --word_map=fake/WORDMAP_coco_5_cap_per_img_5_min_word_freq.json --beam_size=5 C:\Anaconda3\lib\site-packages\torch\serialization.py:425: SourceChangeWarning: source code of class 'models.Encoder' has changed. you can retrieve the ori ginal source code by accessing the object's source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to revert the changes. warnings.warn(msg, SourceChangeWarning) C:\Anaconda3\lib\site-packages\torch\serialization.py:425: SourceChangeWarning: source code of class 'torch.nn.modules.conv.Conv2d' has changed. you can re trieve the original source code by accessing the object's source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to revert th e changes. warnings.warn(msg, SourceChangeWarning) C:\Anaconda3\lib\site-packages\torch\serialization.py:425: SourceChangeWarning: source code of class 'torch.nn.modules.batchnorm.BatchNorm2d' has changed. you can retrieve the original source code by accessing the object's source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to revert the changes. warnings.warn(msg, SourceChangeWarning) C:\Anaconda3\lib\site-packages\torch\serialization.py:425: SourceChangeWarning: source code of class 'torchvision.models.resnet.Bottleneck' has changed. yo u can retrieve the original source code by accessing the object's source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to r evert the changes. warnings.warn(msg, SourceChangeWarning) C:\Anaconda3\lib\site-packages\torch\serialization.py:425: SourceChangeWarning: source code of class 'models.DecoderWithAttention' has changed. you can ret rieve the original source code by accessing the object's source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to revert the changes. warnings.warn(msg, SourceChangeWarning) C:\Anaconda3\lib\site-packages\torch\serialization.py:425: SourceChangeWarning: source code of class 'models.Attention' has changed. you can retrieve the o riginal source code by accessing the object's source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to revert the changes. warnings.warn(msg, SourceChangeWarning) C:\Anaconda3\lib\site-packages\torch\serialization.py:425: SourceChangeWarning: source code of class 'torch.nn.modules.linear.Linear' has changed. you can retrieve the original source code by accessing the object's source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to revert the changes. warnings.warn(msg, SourceChangeWarning) C:\Anaconda3\lib\site-packages\torch\serialization.py:425: SourceChangeWarning: source code of class 'torch.nn.modules.sparse.Embedding' has changed. you c an retrieve the original source code by accessing the object's source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to reve rt the changes. warnings.warn(msg, SourceChangeWarning) C:\Anaconda3\lib\site-packages\torch\serialization.py:425: SourceChangeWarning: source code of class 'torch.nn.modules.rnn.LSTMCell' has changed. you can r etrieve the original source code by accessing the object's source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to revert t he changes. warnings.warn(msg, SourceChangeWarning) Traceback (most recent call last): File "caption.py", line 222, in seq,alphas = caption_image_beam_search(encoder, decoder, args.img, word_map, args.beam_size) File "caption.py", line 45, in caption_image_beamsearch image = transform(img) # (3, 256, 256) File "C:\Anaconda3\lib\site-packages\torchvision\transforms\transforms.py", line 60, in call img = t(img) File "C:\Anaconda3\lib\site-packages\torchvision\transforms\transforms.py", line 163, in call return F.normalize(tensor, self.mean, self.std, self.inplace) File "C:\Anaconda3\lib\site-packages\torchvision\transforms\functional.py", line 208, in normalize tensor.sub(mean[:, None, None]).div_(std[:, None, None]) RuntimeError: Expected object of type torch.cuda.FloatTensor but found type torch.FloatTensor for argument #4 'other'

(empty) E:\Program Files\a-PyTorch-Tutorial-to-Image-Captioning>python caption.py --img=e:/paper.jpg --model=fake/BEST_checkpoint_coco_5_cap_per_img_5min word_freq.pth.tar --word_map=fake/WORDMAP_coco_5_cap_per_img_5_min_word_freq.json --beam_size=5 C:\Anaconda3\lib\site-packages\torch\serialization.py:425: SourceChangeWarning: source code of class 'models.Encoder' has changed. you can retrieve the ori ginal source code by accessing the object's source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to revert the changes. warnings.warn(msg, SourceChangeWarning) C:\Anaconda3\lib\site-packages\torch\serialization.py:425: SourceChangeWarning: source code of class 'torch.nn.modules.conv.Conv2d' has changed. you can re trieve the original source code by accessing the object's source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to revert th e changes. warnings.warn(msg, SourceChangeWarning) C:\Anaconda3\lib\site-packages\torch\serialization.py:425: SourceChangeWarning: source code of class 'torch.nn.modules.batchnorm.BatchNorm2d' has changed. you can retrieve the original source code by accessing the object's source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to revert the changes. warnings.warn(msg, SourceChangeWarning) C:\Anaconda3\lib\site-packages\torch\serialization.py:425: SourceChangeWarning: source code of class 'torchvision.models.resnet.Bottleneck' has changed. yo u can retrieve the original source code by accessing the object's source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to r evert the changes. warnings.warn(msg, SourceChangeWarning) C:\Anaconda3\lib\site-packages\torch\serialization.py:425: SourceChangeWarning: source code of class 'models.DecoderWithAttention' has changed. you can ret rieve the original source code by accessing the object's source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to revert the changes. warnings.warn(msg, SourceChangeWarning) C:\Anaconda3\lib\site-packages\torch\serialization.py:425: SourceChangeWarning: source code of class 'models.Attention' has changed. you can retrieve the o riginal source code by accessing the object's source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to revert the changes. warnings.warn(msg, SourceChangeWarning) C:\Anaconda3\lib\site-packages\torch\serialization.py:425: SourceChangeWarning: source code of class 'torch.nn.modules.linear.Linear' has changed. you can retrieve the original source code by accessing the object's source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to revert the changes. warnings.warn(msg, SourceChangeWarning) C:\Anaconda3\lib\site-packages\torch\serialization.py:425: SourceChangeWarning: source code of class 'torch.nn.modules.sparse.Embedding' has changed. you c an retrieve the original source code by accessing the object's source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to reve rt the changes. warnings.warn(msg, SourceChangeWarning) C:\Anaconda3\lib\site-packages\torch\serialization.py:425: SourceChangeWarning: source code of class 'torch.nn.modules.rnn.LSTMCell' has changed. you can r etrieve the original source code by accessing the object's source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to revert t he changes. warnings.warn(msg, SourceChangeWarning) Traceback (most recent call last): File "caption.py", line 222, in seq,alphas = caption_image_beam_search(encoder, decoder, args.img, word_map, args.beam_size) File "caption.py", line 45, in caption_image_beamsearch image = transform(img) # (3, 256, 256) File "C:\Anaconda3\lib\site-packages\torchvision\transforms\transforms.py", line 60, in call img = t(img) File "C:\Anaconda3\lib\site-packages\torchvision\transforms\transforms.py", line 163, in call return F.normalize(tensor, self.mean, self.std, self.inplace) File "C:\Anaconda3\lib\site-packages\torchvision\transforms\functional.py", line 208, in normalize tensor.sub(mean[:, None, None]).div_(std[:, None, None]) RuntimeError: Expected object of type torch.cuda.FloatTensor but found type torch.FloatTensor for argument #4 'other'

Pytorch and python are installed according to the version you provide. Why does this happen? Thank you for your answer.

sgrvinod commented 5 years ago

@672401341 Hi, were you able to fix this?

It looks like the image (which is being normalized) is on the GPU, whereas the mean and standard deviation tensors are on the CPU. We're supplying the mean and standard deviations as lists, so they're converted to tensors by PyTorch internally. It's weird – I don't get the error when I try it on my computer.

You could try putting the image on the GPU after the normalization:

    img = torch.FloatTensor(img)
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])
    transform = transforms.Compose([normalize])
    image = transform(img).to(device)  # (3, 256, 256)

Let me know if this fixes it.

(You can ignore the other warnings, they're only because some layer definitions were changed after the model was trained.)

JunlongFeng commented 5 years ago

I also get this warning.It may be the reason as you said.Some definitions about model are changed after the training

sgrvinod commented 5 years ago

@JunlongFeng Yes, it's because I might have modified the code after I trained my checkpoints.

sgrvinod commented 5 years ago

Reopen if needed.

purveshpatel511 commented 4 years ago

Source-code Change Warning.

i get error after running caption.py file:::

/usr/local/lib/python3.6/dist-packages/torch/serialization.py:657: SourceChangeWarning: source code of class 'models.Encoder' has changed. Saved a reverse patch to Encoder.patch. Run `patch -p0 < Encoder.patch` to revert your changes.
  warnings.warn(msg, SourceChangeWarning)
/usr/local/lib/python3.6/dist-packages/torch/serialization.py:657: SourceChangeWarning: source code of class 'torch.nn.modules.container.Sequential' has changed. Saved a reverse patch to Sequential.patch. Run `patch -p0 < Sequential.patch` to revert your changes.
  warnings.warn(msg, SourceChangeWarning)
/usr/local/lib/python3.6/dist-packages/torch/serialization.py:657: SourceChangeWarning: source code of class 'torch.nn.modules.conv.Conv2d' has changed. Saved a reverse patch to Conv2d.patch. Run `patch -p0 < Conv2d.patch` to revert your changes.
  warnings.warn(msg, SourceChangeWarning)
/usr/local/lib/python3.6/dist-packages/torch/serialization.py:657: SourceChangeWarning: source code of class 'torch.nn.modules.batchnorm.BatchNorm2d' has changed. Saved a reverse patch to BatchNorm2d.patch. Run `patch -p0 < BatchNorm2d.patch` to revert your changes.
  warnings.warn(msg, SourceChangeWarning)
/usr/local/lib/python3.6/dist-packages/torch/serialization.py:657: SourceChangeWarning: source code of class 'torch.nn.modules.activation.ReLU' has changed. Saved a reverse patch to ReLU.patch. Run `patch -p0 < ReLU.patch` to revert your changes.
  warnings.warn(msg, SourceChangeWarning)
/usr/local/lib/python3.6/dist-packages/torch/serialization.py:657: SourceChangeWarning: source code of class 'torch.nn.modules.pooling.MaxPool2d' has changed. Saved a reverse patch to MaxPool2d.patch. Run `patch -p0 < MaxPool2d.patch` to revert your changes.
  warnings.warn(msg, SourceChangeWarning)
/usr/local/lib/python3.6/dist-packages/torch/serialization.py:657: SourceChangeWarning: source code of class 'torchvision.models.resnet.Bottleneck' has changed. Saved a reverse patch to Bottleneck.patch. Run `patch -p0 < Bottleneck.patch` to revert your changes.
  warnings.warn(msg, SourceChangeWarning)
/usr/local/lib/python3.6/dist-packages/torch/serialization.py:657: SourceChangeWarning: source code of class 'torch.nn.modules.pooling.AdaptiveAvgPool2d' has changed. Saved a reverse patch to AdaptiveAvgPool2d.patch. Run `patch -p0 < AdaptiveAvgPool2d.patch` to revert your changes.
  warnings.warn(msg, SourceChangeWarning)
/usr/local/lib/python3.6/dist-packages/torch/serialization.py:657: SourceChangeWarning: source code of class 'models.DecoderWithAttention' has changed. Saved a reverse patch to DecoderWithAttention.patch. Run `patch -p0 < DecoderWithAttention.patch` to revert your changes.
  warnings.warn(msg, SourceChangeWarning)
/usr/local/lib/python3.6/dist-packages/torch/serialization.py:657: SourceChangeWarning: source code of class 'models.Attention' has changed. Saved a reverse patch to Attention.patch. Run `patch -p0 < Attention.patch` to revert your changes.
  warnings.warn(msg, SourceChangeWarning)
/usr/local/lib/python3.6/dist-packages/torch/serialization.py:657: SourceChangeWarning: source code of class 'torch.nn.modules.linear.Linear' has changed. Saved a reverse patch to Linear.patch. Run `patch -p0 < Linear.patch` to revert your changes.
  warnings.warn(msg, SourceChangeWarning)
/usr/local/lib/python3.6/dist-packages/torch/serialization.py:657: SourceChangeWarning: source code of class 'torch.nn.modules.activation.Softmax' has changed. Saved a reverse patch to Softmax.patch. Run `patch -p0 < Softmax.patch` to revert your changes.
  warnings.warn(msg, SourceChangeWarning)
/usr/local/lib/python3.6/dist-packages/torch/serialization.py:657: SourceChangeWarning: source code of class 'torch.nn.modules.sparse.Embedding' has changed. Saved a reverse patch to Embedding.patch. Run `patch -p0 < Embedding.patch` to revert your changes.
  warnings.warn(msg, SourceChangeWarning)
/usr/local/lib/python3.6/dist-packages/torch/serialization.py:657: SourceChangeWarning: source code of class 'torch.nn.modules.dropout.Dropout' has changed. Saved a reverse patch to Dropout.patch. Run `patch -p0 < Dropout.patch` to revert your changes.
  warnings.warn(msg, SourceChangeWarning)
/usr/local/lib/python3.6/dist-packages/torch/serialization.py:657: SourceChangeWarning: source code of class 'torch.nn.modules.rnn.LSTMCell' has changed. Saved a reverse patch to LSTMCell.patch. Run `patch -p0 < LSTMCell.patch` to revert your changes.
  warnings.warn(msg, SourceChangeWarning)
/usr/local/lib/python3.6/dist-packages/torch/serialization.py:657: SourceChangeWarning: source code of class 'torch.nn.modules.activation.Sigmoid' has changed. Saved a reverse patch to Sigmoid.patch. Run `patch -p0 < Sigmoid.patch` to revert your changes.
  warnings.warn(msg, SourceChangeWarning)

here i set torch.nn.Module.dump_patches=True as per first error but both the time i am getting same output for every image.

output is :: a close up of a black and white background

can you help me through this error and please you specifi the version of pytorch, torch, torchvision that is compitable with current source code. i think upgraded version of this package is remove or deprecated some functions.

baitongda commented 4 years ago

看不懂啊。这个怎么操作

ragvri commented 3 years ago

Getting the same error as @purveshpatel511. Any fix?

DrRyanHuang commented 3 years ago

@ragvri @baitongda

I also get this warning.

Ubuntu18.04 PyTorch1.6

/home/xx/lib/python3.7/site-packages/torch/serialization.py:649: SourceChangeWarning: source code of class 'torch.nn.modules.conv.Conv2d' has changed. you can retrieve the original source code by accessing the object's source attribute or set `torch.nn.Module.dump_patches = True` and use the patch tool to revert the changes.
  warnings.warn(msg, SourceChangeWarning)
/home/xx/lib/python3.7/site-packages/torch/serialization.py:649: SourceChangeWarning: source code of class 'torch.nn.modules.batchnorm.BatchNorm2d' has changed. you can retrieve the original source code by accessing the object's source attribute or set `torch.nn.Module.dump_patches = True` and use the patch tool to revert the changes.
  warnings.warn(msg, SourceChangeWarning)
/home/xx/lib/python3.7/site-packages/torch/serialization.py:649: SourceChangeWarning: source code of class 'torch.nn.modules.upsampling.UpsamplingNearest2d' has changed. you can retrieve the original source code by accessing the object's source attribute or set `torch.nn.Module.dump_patches = True` and use the patch tool to revert the changes.
  warnings.warn(msg, SourceChangeWarning)

Thus, run in Python Terminal:

torch.nn.Module.dump_patches = True

There will be BatchNorm2d.patch, Conv2d.patch and UpsamplingNearest2d.patch, 3 patch file, in your current work dir, so you can use tool patch If you dont know patch, run in Terminal:

man patch

Then:

patch -p0 < [your patch file]
# eg: patch -p0 < BatchNorm2d.patch
# eg: patch -p0 < Conv2d.patch
# eg: patch -p0 < UpsamplingNearest2d.patch

However, my model is so old that I give up using it, thus I dont know Follow-up operation. : )

nahyungsun commented 2 years ago

checkpoint = torch.load(checkpoint)
torch.save(checkpoint, 'BEST_checkpoint_coco_5_cap_per_img_5_min_word_freq.pth.tar')

Save and recall model to resolve

code : model dim size = 512 pretraning model dim size = 2048

https://github.com/tbepler/protein-sequence-embedding-iclr2019/issues/17