Giter VIP home page Giter VIP logo

Comments (8)

zbp-xxxp avatar zbp-xxxp commented on May 16, 2024

my codes are:

gMLP_PATH = "./final.pdparams"
gMLP = gMLPVision(
    image_size = 32,
    patch_size = 2,
    num_classes = 10,
    dim = 128,
    depth = 30
)
gMLP_state_dict = paddle.load(gMLP_PATH)
gMLP.set_dict(gMLP_state_dict)

gradcam = it.GradCAMInterpreter(gMLP, use_cuda=False)

heatmap = gradcam.interpret(
        "./5.png",
        'layers.29.fn.fn.sgu',
        # labels=None,
        visual=False,
        save_path="./5view.png")

from interpretdl.

zbp-xxxp avatar zbp-xxxp commented on May 16, 2024

my package version is:
paddlepaddle==2.0.0
interpretdl==0.2.2

from interpretdl.

holyseven avatar holyseven commented on May 16, 2024

Hi @zbp-xxxp ,

It seems an error of using ssh and matplotlib. The solution that I found from here may help you out:

sudo pip uninstall matplotlib
sudo apt-get install python-matplotlib

Some comments from that issue may be also helpful.

from interpretdl.

zbp-xxxp avatar zbp-xxxp commented on May 16, 2024

/usr/lib/python3/dist-packages/matplotlib/backend_bases.py:57: DeprecationWarning: PILLOW_VERSION is deprecated and will be removed in Pillow 9 (2022-01-02). Use version instead.
from PIL import PILLOW_VERSION
Killed

from interpretdl.

holyseven avatar holyseven commented on May 16, 2024

Sorry I cannot reproduce this error. Could you try to run the code on a local machine if you did not? or try to create a new Python environment.

from interpretdl.

zbp-xxxp avatar zbp-xxxp commented on May 16, 2024

RD您好,是这样的,我用中文跟您说吧。
我跑基于模型中间特征解析模型的代码(GradCAM,ScoreCAM)的时候,AI Studio会卡死直到内存溢出,我在本地机器跑会直接被kill掉,就像上面提的那个一样。
我的模型是gMLP,基于输入特征解析模型的代码(LIMECVInterpreter)是可以跑通的。
但是我需要可视化的是网络中每一层的权重,而不是训练一个子网络检验模型性能,因此还是需要基于模型中间特征去解析模型。
我分析了一下跑不通的原因,不知道对不对:gMLP有to patch的操作,会不会是这个操作导致了这个问题?如果是,有什么方法可以解决吗?
to patch代码:

class PatchEmbed(nn.Layer):
    """ Image to Patch Embedding
    """
    def __init__(self, img_size=32, patch_size=16, in_chans=3, embed_dim=768):
        super(PatchEmbed, self).__init__()
        img_size = (img_size, img_size)
        patch_size = (patch_size, patch_size)
        num_patches = (img_size[1] // patch_size[1]) * (img_size[0] // patch_size[0])
        self.img_size = img_size
        self.patch_size = patch_size
        self.num_patches = num_patches

        self.project = nn.Conv2D(in_chans, embed_dim, kernel_size=self.patch_size, stride=self.patch_size)

    def forward(self, x):
        x = self.project(x)
        x = x.flatten(2)
        x = paddle.transpose(x, perm=[0, 2, 1])
        return x

from interpretdl.

holyseven avatar holyseven commented on May 16, 2024

不太确定是哪一个步骤导致上述问题,如果有更详细的代码可以更深入分析。

如果是有关gradient的计算的话,可以参考这个issue。我们计算gradient的代码之后也会更新。

import paddle
from interpretdl.data_processor.readers import preprocess_inputs
from datetime import datetime
import numpy as np

img_path = 'assets/catdog.png'
# read the image and scale to [0, 1] with substract mean and divided by std.
data = np.random.rand(1, 3, 224, 224).astype(np.float32)
# imgs, data = preprocess_inputs(img_path, [224, 224, 3])  
print(data.shape, data.dtype)
# print(imgs.shape, data.shape)  # (1, 224, 224, 3) (1, 3, 224, 224)

# from paddle.vision.models import resnet101, resnet50, mobilenet_v2, vgg13, vgg19
# paddle_model = resnet101(pretrained=False)

from assets.vision_transformer import ViT_small_patch16_224
# https://github.com/PaddlePaddle/PaddleClas/blob/release/2.2/ppcls/arch/backbone/model_zoo/vision_transformer.py
paddle_model = ViT_small_patch16_224()
MODEL_PATH = 'assets/ViT_small_patch16_224_pretrained.pdparams'
paddle_model.set_dict(paddle.load(MODEL_PATH))

data = paddle.to_tensor(data)
data.stop_gradient = False
out = paddle_model(data)
preds = paddle.argmax(out, axis=1)
label_onehot = paddle.nn.functional.one_hot(
                    paddle.to_tensor(preds), num_classes=out.shape[1])
target = paddle.sum(out * label_onehot, axis=1)

now = datetime.now()
# g = paddle.grad(outputs=target, inputs=data)[0]
target.backward()
g = data.grad
print(datetime.now() - now)

from interpretdl.

holyseven avatar holyseven commented on May 16, 2024

Free to reopen the issue if there are more questions.

from interpretdl.

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.