VGG-Model(pytorch版本)

V G G − M o d e l ( p y t o r c h 版 本 ) VGG-Model(pytorch版本) VGGModel(pytorch)

import torch
import torch.nn as nn
import torchvision
def Conv3x3BNReLU(in_channels,out_channels):
    return nn.Sequential(
        nn.Conv2d(in_channels=in_channels,out_channels=out_channels,kernel_size=3,stride=1,padding=1),
        nn.BatchNorm2d(out_channels),
        nn.ReLU6(inplace=True)
    )

class VGGNet(nn.Module):
    def __init__(self, block_nums,num_classes=5,num_linear=25088):
        super(VGGNet, self).__init__()

        self.stage1 = self._make_layers(in_channels=3, out_channels=64, block_num=block_nums[0])
        self.stage2 = self._make_layers(in_channels=64, out_channels=128, block_num=block_nums[1])
        self.stage3 = self._make_layers(in_channels=128, out_channels=256, block_num=block_nums[2])
        self.stage4 = self._make_layers(in_channels=256, out_channels=512, block_num=block_nums[3])
        self.stage5 = self._make_layers(in_channels=512, out_channels=512, block_num=block_nums[4])

        self.classifier = nn.Sequential(
            nn.Linear(in_features=num_linear,out_features=4096),
            nn.ReLU6(inplace=True),
            nn.Dropout(p=0.2),
            nn.Linear(in_features=4096, out_features=4096),
            nn.ReLU6(inplace=True),
            nn.Dropout(p=0.2),
            nn.Linear(in_features=4096, out_features=num_classes)
        )

    def _make_layers(self, in_channels, out_channels, block_num):
        layers = []
        layers.append(Conv3x3BNReLU(in_channels,out_channels))
        for i in range(1,block_num):
            layers.append(Conv3x3BNReLU(out_channels,out_channels))
        layers.append(nn.MaxPool2d(kernel_size=2,stride=2))
        return nn.Sequential(*layers)

    def forward(self, x):
        x = self.stage1(x)
        x = self.stage2(x)
        x = self.stage3(x)
        x = self.stage4(x)
        x = self.stage5(x)
        x = x.view(x.size(0),-1)
        out = self.classifier(x)
        return out

def VGG16(num_classes,num_linear):
    block_nums = [2, 2, 3, 3, 3]
    model = VGGNet(block_nums,num_classes,num_linear)
    return model

def VGG19(num_classes,num_linear):
    block_nums = [2, 2, 4, 4, 4]
    model = VGGNet(block_nums,num_classes,num_linear)
    return model
# 随机生成输入数据
rgb = torch.randn(1, 3, 224, 224)
# 定义网络
# num_linear的设置是为了,随着输入图片数据大小的改变,使线性层的神经元数量可以匹配成功
# 默认输入图片数据大小为224*224
net = VGG19(num_classes=8,num_linear=25088)
# 前向传播
out = net(rgb)
print('--VGG19---'*5)
# 打印输出大小
print(out.shape)
print('----------'*5)
net = VGG16(num_classes=8,num_linear=25088)
out = net(rgb)
print('--VGG16---'*5)
# 打印输出大小
print(out.shape)

在这里插入图片描述

补充:SE_VGG

SE_VGG

以下是基于 VGG16 回归模型的 Grad-CAM PyTorch 实现代码示例: ```python import torch import torchvision.models as models import torch.nn.functional as F import cv2 import numpy as np # Load pre-trained VGG16 model model = models.vgg16(pretrained=True) # Define the last convolutional layer of VGG16 target_layer = model.features[-1] # Define the input tensor input_tensor = torch.randn(1, 3, 224, 224, requires_grad=True) # Define the class index for which the Grad-CAM will be computed class_index = 10 # Forward pass through the model output = model(input_tensor) # Compute the gradients of the output with respect to the input tensor output[:, class_index].backward() # Get the gradients of the target layer gradients = target_layer.weight.grad # Compute the average gradients across all spatial dimensions pooled_gradients = torch.mean(gradients, dim=[2, 3]) # Get the activations of the target layer activations = model.features(input_tensor) # Compute the weighted sum of the activations and gradients for i in range(512): activations[:, i, :, :] *= pooled_gradients[0, i] # Get the heatmap heatmap = torch.mean(activations, dim=1).squeeze().detach().numpy() # Apply ReLU to the heatmap heatmap = np.maximum(heatmap, 0) # Normalize the heatmap heatmap /= np.max(heatmap) # Load the original image img = cv2.imread('path/to/image.jpg') # Resize the heatmap to the size of the original image heatmap = cv2.resize(heatmap, (img.shape[1], img.shape[0])) # Convert the heatmap to RGB heatmap = np.uint8(255 * heatmap) heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET) # Overlay the heatmap on the original image result = cv2.addWeighted(img, 0.8, heatmap, 0.4, 0) # Display the result cv2.imshow('Grad-CAM', result) cv2.waitKey(0) cv2.destroyAllWindows() ``` 注意:以上代码仅供参考,具体实现可能需要根据自己的数据和模型做一些调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值