Git Product home page Git Product logo

Comments (5)

frgfm avatar frgfm commented on May 17, 2024

Hello again @FredrikM97 👋

Thanks for helping to catch some bugs! Could you produce a minimal snippet to produce this error please?
I don't have a 3DResNet available so I'm not able to reproduce the error!

Also, where is the RuntimeError raised please?

from torch-cam.

FredrikM97 avatar FredrikM97 commented on May 17, 2024

No worries! Thank you for quick replies!

The error occurs at line 122 in cams.py:
batch_cams = (weights.view(*weights.shape, 1, 1) * self.hook_a.squeeze(0)).sum(0) # type: ignore[union-attr]
It looks like

Here is a sample of a 3D nifti image and the resnet: 3D_network.zip. Note that the resnet is configured for grayscale images. and three output classes.

To open the file:

import nibabel as nib
img = nib.load(example_filename).get_fdata()

Here is a sample of the code I use:

from resnet import resnet50
device = 'cuda'
model = resnet50().to(device)
model.eval()

input_tensor = torch.from_numpy(img).unsqueeze(0).unsqueeze(0).float()

cam = SmoothGradCAMpp(
    model, 
    'layer4',
    batch_size = 1,
    num_samples = 1,
    std = 2.0,
    input_shape = (1,1, 79, 224, 224),
)
with torch.no_grad(): 
    scores = model(input_tensor)
    activation_map = cam(scores.squeeze(0).argmax().item(), scores).cpu()

Update:
Before the avgpool layer in ResNet the shape of the data is torch.Size([1, 2048, 3, 6, 5]). So the hook seem to be working. However after: alpha = grad_2 / (2 * grad_2 + (grad_3 * init_fmap).sum(dim=(2, 3), keepdim=True)) the following shapes are observed:

alpha.shape: torch.Size([1, 2048, 3, 7, 7]) 
self.hook_g.squeeze(0).shape: torch.Size([2048, 3, 7, 7])
torch.relu(self.hook_g.squeeze(0)).shape:  torch.Size([2048, 3, 7, 7])
alpha.squeeze_(0).mul_(torch.relu(self.hook_g.squeeze(0))).shape:  torch.Size([2048, 3, 7, 7])

The last step is to perform .sum(dim=(1, 2)) which I assume is the cause of the new shape torch.Size([2048, 5, 1, 1]).

from torch-cam.

zsddd avatar zsddd commented on May 17, 2024

不用担心!感谢您的快速回复!

错误发生在 cams.py 中的第 122 行:如下所示batch_cams = (weights.view(*weights.shape, 1, 1) * self.hook_a.squeeze(0)).sum(0) # type: ignore[union-attr]

以下是3D nifti图像和resnet的示例:3D_network.zip。请注意,resnet 是为灰度图像配置的。和三个输出类。

要打开文件:

import nibabel as nib
img = nib.load(example_filename).get_fdata()

以下是我使用的代码示例:

from resnet import resnet50
device = 'cuda'
model = resnet50().to(device)
model.eval()

input_tensor = torch.from_numpy(img).unsqueeze(0).unsqueeze(0).float()

cam = SmoothGradCAMpp(
    model, 
    'layer4',
    batch_size = 1,
    num_samples = 1,
    std = 2.0,
    input_shape = (1,1, 79, 224, 224),
)
with torch.no_grad(): 
    scores = model(input_tensor)
    activation_map = cam(scores.squeeze(0).argmax().item(), scores).cpu()

**更新:**在 ResNet 中的平均池层之前,数据的形状是火炬。大小([1, 2048, 3, 6, 5])。所以钩子似乎正在起作用。但是,在之后:观察到以下形状:alpha = grad_2 / (2 * grad_2 + (grad_3 * init_fmap).sum(dim=(2, 3), keepdim=True))

alpha.shape: torch.Size([1, 2048, 3, 7, 7]) 
self.hook_g.squeeze(0).shape: torch.Size([2048, 3, 7, 7])
torch.relu(self.hook_g.squeeze(0)).shape:  torch.Size([2048, 3, 7, 7])
alpha.squeeze_(0).mul_(torch.relu(self.hook_g.squeeze(0))).shape:  torch.Size([2048, 3, 7, 7])

最后一步是执行,我认为这是新形状的原因.sum(dim=(1, 2)) ``torch.Size([2048, 5, 1, 1]).

Hello, have you finished your 3D visualization? Can you share it? Thank you very much.

from torch-cam.

frgfm avatar frgfm commented on May 17, 2024

Hi there 👋

The issue raised by @FredrikM97 was solved last year. I'm not sure what you're referring to regarding the 3D visualization?
CAMs are working on 3D data if that's your question :)

from torch-cam.

zsddd avatar zsddd commented on May 17, 2024

Thank you for quick replies!

Hi there 👋

The issue raised by @FredrikM97 was solved last year. I'm not sure what you're referring to regarding the 3D visualization? CAMs are working on 3D data if that's your question :)
Thank you for quick replies!
My program has the following problems:cannot register a hook on a tensor that doesn't require gradient
This is my program:
file=r'E:\pythonProject\data\train\128\label404347_4 1.npy'
con_arr1 = np.load(file,allow_pickle=True) # 读取npy文件
con_arr=con_arr1[0][0]
device = 'cuda'
model = resnet18(num_classes=1).to(device)
model.eval()

input_tensor = np.array(con_arr)
input_tensor=torch.from_numpy(input_tensor)
input_tensor =input_tensor.unsqueeze(0).unsqueeze(0).float().to(device)

print(input_tensor.shape)

cam = SmoothGradCAMpp( model,'layer1',num_samples = 1,std = 2.0,input_shape = (1,128,128,128))
with torch.no_grad():
scores = model(input_tensor)
activation_map = cam(scores.squeeze(0).argmax().item(), scores).cpu()

from torch-cam.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.