Row 4396
Content Data
This page contains data entry 4396 from the Axioma AXP content repository. The structured data below represents the complete record for this entry.
I am new to pytorch. I want to use imagenet images to understand how much each pixel contributes to the gradient. For this, I am trying to construct attention maps for my images. However, while doing so, I am encountering the following error:
`<ipython-input-89-08560ac86bab>:2: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor). images_tensor = torch.tensor(images, requires_grad=True) <ipython-input-89-08560ac86bab>:3: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor). labels_tensor = torch.tensor(labels) --------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) <ipython-input-90-49bfbb2b28f0> in <cell line: 20>() 18 plt.show() 19 ---> 20 show_attention_maps(X, y) 9 frames/usr/local/lib/python3.10/dist-packages/torch/nn/functional.py in batch_norm(input, running_mean, running_var, weight, bias, training, momentum, eps) 2480 _verify_batch_size(input.size()) 2481 -> 2482 return torch.batch_norm( 2483 input, weight, bias, running_mean, running_var, training, momentum, eps, torch.backends.cudnn.enabled 2484 )`
`RuntimeError: running_mean should contain 1 elements not 64`
I have tried changing the image size in preprocessing and changing the model to resnet152 instead of resnet18. My understanding from the research I have done is that the batchnorm in the first layer expects input size 1, but I have 64. I am not sure how that can be changed.
My code is here:
`model = torch.hub.load('pytorch/vision:v0.10.0', 'resnet18', pretrained=True)`
`import torch.nn as nn new_conv1 = nn.Conv2d(15, 1, kernel_size=1, stride=1, padding=112) nn.init.constant_(new_conv1.weight, 1)`
`model.conv1 = new_conv1 model.eval()`
`for param in model.parameters():`
`param.requires_grad = False`
`def compute_attention_maps(images, labels, model):`
`images_tensor = torch.tensor(images, requires_grad=True)`
`labels_tensor = torch.tensor(labels)`
`predictions = model(images_tensor.unsqueeze(0))`
`criterion = torch.nn.CrossEntropyLoss()`
`loss = criterion(predictions, labels_tensor)`
`model.zero_grad() loss.backward()`
`gradients = images_tensor.grad`
`attention_maps = torch.mean(gradients.abs(), dim=1)`
`return attention_maps`
​
`def show_attention_maps(X, y): X_tensor = torch.cat([preprocess(Image.fromarray(x)) for x in X], dim=0)`
`y_tensor = torch.LongTensor(y) attention = compute_attention_maps(X_tensor, y_tensor, model) attention = attention.numpy()`
`N = X.shape[0]`
`for i in range(N):`
`plt.subplot(2, N, i + 1)`
`plt.imshow(X[i]) plt.axis('off')`
`plt.title(class_names[y[i]])`
`plt.subplot(2, N, N + i + 1)`
`plt.imshow(attention[i], cmap=plt.cm.gray)`
`plt.axis('off')`
`plt.gcf().set_size_inches(12, 5)`
`plt.suptitle('Attention maps')`
`plt.show() show_attention_maps(X, y)`
Thank you very much in advance. Your help would help me learn and understand pytorch programming better!
​
​
| Field | Value |
|---|---|
| text | I am new to pytorch. I want to use imagenet images to understand how much each pixel contributes to the gradient. For this, I am trying to construct attention maps for my images. However, while doing so, I am encountering the following error: `<ipython-input-89-08560ac86bab>:2: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor). images_tensor = torch.… |
| label | r/pytorch |
| dataType | post |
| communityName | r/pytorch |
| datetime | 2024-04-10 |
| username_encoded | Z0FBQUFBQm5LakwxMXdrMDlkVUlhRFFKSEJZYThtcXl3S3Niei1nTEI1SGRUZmNvOUtuWGtyWjZjbjdmMEszSVdfZjdxRVprMGtoMzFpU1hFdUI4NS1LeWI1ZXcyakNSNUE9PQ== |
| url_encoded | Z0FBQUFBQm5Lak9GemNYX3dRRk1CY1BWcDdKcl9pNk1NXzFoOTlCTFc2NkNfZ2hNMlhBN0hQWjlEU1hMaWYyR2lUaUZBSjh1VGZHTFhZWkFZeVRIajlDS2lzcFYwalB2bWdidGVMRnhyaHpyV3FsTng3WFJwX1NlMFdtY0ZNRFRHLVRKY1d4QnlpTFZ2WVRPU3VRcVBVTnZBM1BNS09nM1M3ODVncGk5U0E4ZUhvRkc3S0lwZnZXQ1ZBNk1kMEdFWUdHOEk5U0VhTFJpOHRRX2pDUGNva1N3NVBpdnF6WE5EZz09 |
Raw Record
{
"text": "I am new to pytorch. I want to use imagenet images to understand how much each pixel contributes to the gradient. For this, I am trying to construct attention maps for my images. However, while doing so, I am encountering the following error:\n\n`<ipython-input-89-08560ac86bab>:2: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor). images_tensor = torch.tensor(images, requires_grad=True) <ipython-input-89-08560ac86bab>:3: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor). labels_tensor = torch.tensor(labels) --------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) <ipython-input-90-49bfbb2b28f0> in <cell line: 20>() 18 plt.show() 19 ---> 20 show_attention_maps(X, y) 9 frames/usr/local/lib/python3.10/dist-packages/torch/nn/functional.py in batch_norm(input, running_mean, running_var, weight, bias, training, momentum, eps) 2480 _verify_batch_size(input.size()) 2481 -> 2482 return torch.batch_norm( 2483 input, weight, bias, running_mean, running_var, training, momentum, eps, torch.backends.cudnn.enabled 2484 )` \n\n`RuntimeError: running_mean should contain 1 elements not 64`\n\nI have tried changing the image size in preprocessing and changing the model to resnet152 instead of resnet18. My understanding from the research I have done is that the batchnorm in the first layer expects input size 1, but I have 64. I am not sure how that can be changed.\n\nMy code is here:\n\n`model = torch.hub.load('pytorch/vision:v0.10.0', 'resnet18', pretrained=True)` \n\n`import torch.nn as nn new_conv1 = nn.Conv2d(15, 1, kernel_size=1, stride=1, padding=112) nn.init.constant_(new_conv1.weight, 1)`\n\n `model.conv1 = new_conv1 model.eval()` \n\n`for param in model.parameters():` \n\n`param.requires_grad = False` \n\n`def compute_attention_maps(images, labels, model):` \n\n`images_tensor = torch.tensor(images, requires_grad=True)` \n\n`labels_tensor = torch.tensor(labels)` \n\n`predictions = model(images_tensor.unsqueeze(0))` \n\n`criterion = torch.nn.CrossEntropyLoss()` \n\n`loss = criterion(predictions, labels_tensor)` \n\n`model.zero_grad() loss.backward()` \n\n`gradients = images_tensor.grad` \n\n`attention_maps = torch.mean(gradients.abs(), dim=1)` \n\n`return attention_maps`\n\n​\n\n`def show_attention_maps(X, y): X_tensor = torch.cat([preprocess(Image.fromarray(x)) for x in X], dim=0)` \n\n`y_tensor = torch.LongTensor(y) attention = compute_attention_maps(X_tensor, y_tensor, model) attention = attention.numpy()` \n\n`N = X.shape[0]` \n\n`for i in range(N):` \n\n`plt.subplot(2, N, i + 1)` \n\n`plt.imshow(X[i]) plt.axis('off')` \n\n`plt.title(class_names[y[i]])` \n\n`plt.subplot(2, N, N + i + 1)` \n\n`plt.imshow(attention[i], cmap=plt.cm.gray)` \n\n`plt.axis('off')` \n\n`plt.gcf().set_size_inches(12, 5)` \n\n`plt.suptitle('Attention maps')` \n\n`plt.show() show_attention_maps(X, y)`\n\nThank you very much in advance. Your help would help me learn and understand pytorch programming better!\n\n​\n\n​\n\n \n",
"label": "r/pytorch",
"dataType": "post",
"communityName": "r/pytorch",
"datetime": "2024-04-10",
"username_encoded": "Z0FBQUFBQm5LakwxMXdrMDlkVUlhRFFKSEJZYThtcXl3S3Niei1nTEI1SGRUZmNvOUtuWGtyWjZjbjdmMEszSVdfZjdxRVprMGtoMzFpU1hFdUI4NS1LeWI1ZXcyakNSNUE9PQ==",
"url_encoded": "Z0FBQUFBQm5Lak9GemNYX3dRRk1CY1BWcDdKcl9pNk1NXzFoOTlCTFc2NkNfZ2hNMlhBN0hQWjlEU1hMaWYyR2lUaUZBSjh1VGZHTFhZWkFZeVRIajlDS2lzcFYwalB2bWdidGVMRnhyaHpyV3FsTng3WFJwX1NlMFdtY0ZNRFRHLVRKY1d4QnlpTFZ2WVRPU3VRcVBVTnZBM1BNS09nM1M3ODVncGk5U0E4ZUhvRkc3S0lwZnZXQ1ZBNk1kMEdFWUdHOEk5U0VhTFJpOHRRX2pDUGNva1N3NVBpdnF6WE5EZz09"
}
Entry Information
- Entry ID: 4396
- Repository: Axioma AXP
- Dataset: arrmlet/reddit_dataset_36
- Total Entries: 100,000