CAM 的SV实现

在这里插入图片描述
在这里插入图片描述

// If an update is performed to the same address as a lookup in the same clock cycle ?
module cam #(
    parameter NUM_ENTRIES = 2,
    parameter KEY_WIDTH   = 32,
    parameter INDEX_WIDTH = $clog2(NUM_ENTRIES)
) (
    input                            clk,
    input                            reset,

    // Lookup interface
    input        [KEY_WIDTH   - 1:0] lookup_key,
    output logic [INDEX_WIDTH - 1:0] lookup_idx,
    output logic                     lookup_hit,

    // Update interface
    input                            update_en,
    input [KEY_WIDTH   - 1:0]        update_key,
    input [INDEX_WIDTH - 1:0]        update_idx,
    input                            update_valid
);

    logic [KEY_WIDTH   - 1:0] lookup_table [NUM_ENTRIES - 1:0];
    logic [NUM_ENTRIES - 1:0] entry_valid;

    logic [NUM_ENTRIES - 1:0] hit_oh;

    // lookup
    for (genvar test_index = 0; test_index < NUM_ENTRIES; test_index++)
    begin : lookup_gen
        assign hit_oh[test_index] = entry_valid[test_index]
            && lookup_table[test_index] == lookup_key;
    end

    assign lookup_hit = |hit_oh;

    oh_to_idx #(.NUM_SIGNALS(NUM_ENTRIES)) oh_to_idx_hit (
        .one_hot (hit_oh),
        .index   (lookup_idx)
    );

    // update
    always_ff @(posedge clk, posedge reset) begin
        if (reset) begin
            entry_valid <= '0;
        end else if (update_en) begin
            assert($onehot0(hit_oh));
            entry_valid[update_idx] <= update_valid;
        end
    end

    always_ff @(posedge clk) begin
        if (update_en)
            lookup_table[update_idx] <= update_key;
    end

`ifdef SIMULATION
    // Check for duplicate entries
    always_ff @(posedge clk, posedge reset) begin
        if (!reset && update_en && update_valid) begin : test
            for (int i = 0; i < NUM_ENTRIES; i++) begin
                if (entry_valid[i] && lookup_table[i] == update_key
                    && INDEX_WIDTH'(i) != update_idx)
                begin
                    $display("%m: added duplicate entry to CAM");
                    $display("  original slot %d new slot %d", i, update_idx);
                    $finish;
                end
            end
        end
    end
`endif

endmodule
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AlexNet是一种经典的卷积神经网络模型,而GradCAM(Gradient-weighted Class Activation Mapping)是一种可视化方法,用于理解卷积神经网络在图像分类任务中的决策过程。下面是AlexNet热力图GradCAM代码实现的步骤: 1. 导入必要的库和模块: ```python import torch import torch.nn as nn import torch.nn.functional as F from torchvision.models import alexnet from torchvision import transforms from PIL import Image import matplotlib.pyplot as plt ``` 2. 加载预训练的AlexNet模型: ```python model = alexnet(pretrained=True) ``` 3. 定义GradCAM类: ```python class GradCAM: def __init__(self, model, target_layer): self.model = model self.target_layer = target_layer self.feature_maps = None self.gradient = None self.model.eval() self.hook_layers() def hook_layers(self): def hook_fn(module, input, output): self.feature_maps = output.detach() hook_layer = self.model._modules[self.target_layer] hook_layer.register_forward_hook(hook_fn) def compute_gradient(self): def hook_fn(module, grad_input, grad_output): self.gradient = grad_output[0].detach() hook_layer = self.model._modules[self.target_layer] hook_layer.register_backward_hook(hook_fn) def generate_heatmap(self, input_image, class_index): input_image.requires_grad_() model_output = self.model(input_image) self.model.zero_grad() one_hot_output = torch.zeros_like(model_output) one_hot_output[0][class_index] = 1 model_output.backward(gradient=one_hot_output, retain_graph=True) weights = F.adaptive_avg_pool2d(self.gradient, 1) heatmap = torch.mul(self.feature_maps, weights).sum(dim=1, keepdim=True) heatmap = F.relu(heatmap) heatmap = F.interpolate(heatmap, size=(input_image.size(2), input_image.size(3)), mode='bilinear', align_corners=False) heatmap = heatmap.squeeze() heatmap = heatmap.cpu().numpy() return heatmap ``` 4. 加载图像并进行预处理: ```python image_path = 'path_to_image.jpg' image = Image.open(image_path).convert('RGB') preprocess = transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) input_image = preprocess(image).unsqueeze(0) ``` 5. 创建GradCAM实例并生成热力图: ```python gradcam = GradCAM(model, target_layer='features') heatmap = gradcam.generate_heatmap(input_image, class_index=0) ``` 6. 可视化热力图: ```python plt.imshow(image) plt.imshow(heatmap, alpha=0.5, cmap='jet') plt.axis('off') plt.show() ``` 以上是AlexNet热力图GradCAM代码实现的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值