IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 人工智能 -> 【吴恩达深度学习】Residual Networks(PyTorch) -> 正文阅读

[人工智能]【吴恩达深度学习】Residual Networks(PyTorch)

keras版本链接

导包

import torch
from torch import nn
from torch import optim
import torch.nn.functional as F
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
from resnets_utils import *

Dataset类

class MyDataset(Dataset):
    def __init__(self, x, y):
        super(MyDataset, self).__init__()
        assert x.shape[0] == y.shape[0]
        self.x = x
        self.y = y

    def __len__(self):
        return self.x.shape[0]

    def __getitem__(self, item):
        return self.x[item], self.y[item]

Flatten类

class Flatten(nn.Module):
    def __init__(self, start_dim=1, end_dim=-1):
        super(Flatten, self).__init__()
        self.start_dim = start_dim
        self.end_dim = end_dim

    def forward(self, input):
        return input.flatten(self.start_dim, self.end_dim)

The identity block

在这里插入图片描述

class IdentityBlock(nn.Module):
    def __init__(self, channels, f):
        super(IdentityBlock, self).__init__()
        channel1, channel2, channel3, channel4 = channels
        self.conv = nn.Sequential(
            # nn.Conv2d(in_channels=channel1, out_channels=channel2, kernel_size=1, stride=1, padding='valid'),
            nn.Conv2d(in_channels=channel1, out_channels=channel2, kernel_size=1, stride=1, padding=0),
            nn.BatchNorm2d(num_features=channel2),
            nn.ReLU(),

            # nn.Conv2d(in_channels=channel2, out_channels=channel3, kernel_size=f, stride=1, padding='same'),
            nn.Conv2d(in_channels=channel2, out_channels=channel3, kernel_size=f, stride=1, padding=(f - 1) // 2),
            nn.BatchNorm2d(num_features=channel3),
            nn.ReLU(),

            # nn.Conv2d(in_channels=channel3, out_channels=channel4, kernel_size=1, stride=1, padding='valid'),
            nn.Conv2d(in_channels=channel3, out_channels=channel4, kernel_size=1, stride=1, padding=0),
            nn.BatchNorm2d(num_features=channel4),
        )

    def forward(self, input):
        x_shortcut = input
        x = self.conv(input)
        x = x_shortcut + x
        x = F.relu(x)
        return x

The convolutional block

在这里插入图片描述

class ConvolutionalBlock(nn.Module):
    def __init__(self, channels, f, s):
        super(ConvolutionalBlock, self).__init__()
        channel1, channel2, channel3, channel4 = channels
        self.conv1 = nn.Sequential(
            # nn.Conv2d(in_channels=channel1, out_channels=channel2, kernel_size=1, stride=s, padding='valid'),
            nn.Conv2d(in_channels=channel1, out_channels=channel2, kernel_size=1, stride=s, padding=0),
            nn.BatchNorm2d(num_features=channel2),
            nn.ReLU(),

            # nn.Conv2d(in_channels=channel2, out_channels=channel3, kernel_size=f, stride=1, padding='same'),
            nn.Conv2d(in_channels=channel2, out_channels=channel3, kernel_size=f, stride=1, padding=(f - 1) // 2),
            nn.BatchNorm2d(num_features=channel3),
            nn.ReLU(),

            # nn.Conv2d(in_channels=channel3, out_channels=channel4, kernel_size=1, stride=1, padding='valid'),
            nn.Conv2d(in_channels=channel3, out_channels=channel4, kernel_size=1, stride=1, padding=0),
            nn.BatchNorm2d(num_features=channel4)
        )

        self.conv2 = nn.Sequential(
            # nn.Conv2d(in_channels=channel1, out_channels=channel4, kernel_size=1, stride=s, padding='valid'),
            nn.Conv2d(in_channels=channel1, out_channels=channel4, kernel_size=1, stride=s, padding=0),
            nn.BatchNorm2d(num_features=channel4)
        )

    def forward(self, input):
        x = self.conv1(input)
        x_shortcut = self.conv2(input)
        x = x + x_shortcut
        x = F.relu(x)
        return x

ResNet50

在这里插入图片描述

class ResNet50(nn.Module):
    def __init__(self, classes=6):
        super(ResNet50, self).__init__()
        self.net = nn.Sequential(
            nn.ZeroPad2d(padding=(3, 3, 3, 3)),
            nn.Conv2d(in_channels=3, out_channels=64, kernel_size=7, stride=2, padding=0),
            nn.BatchNorm2d(num_features=64),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=3, stride=2),

            ConvolutionalBlock(channels=[64, 64, 64, 256], f=3, s=1),
            IdentityBlock(channels=[256, 64, 64, 256], f=3),
            IdentityBlock(channels=[256, 64, 64, 256], f=3),

            ConvolutionalBlock(channels=[256, 128, 128, 512], f=3, s=2),
            IdentityBlock(channels=[512, 128, 128, 512], f=3),
            IdentityBlock(channels=[512, 128, 128, 512], f=3),
            IdentityBlock(channels=[512, 128, 128, 512], f=3),

            ConvolutionalBlock(channels=[512, 256, 256, 1024], f=3, s=2),
            IdentityBlock(channels=[1024, 256, 256, 1024], f=3),
            IdentityBlock(channels=[1024, 256, 256, 1024], f=3),
            IdentityBlock(channels=[1024, 256, 256, 1024], f=3),
            IdentityBlock(channels=[1024, 256, 256, 1024], f=3),
            IdentityBlock(channels=[1024, 256, 256, 1024], f=3),

            ConvolutionalBlock(channels=[1024, 512, 512, 2048], f=3, s=2),
            IdentityBlock(channels=[2048, 512, 512, 2048], f=3),
            IdentityBlock(channels=[2048, 512, 512, 2048], f=3),

            nn.AvgPool2d(kernel_size=2),
            Flatten(),
            nn.Linear(2048, classes),
        )

    def forward(self, input):
        x = self.net(input)
        return x

加载数据集和预处理

X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset()

# Normalize image vectors
X_train = X_train_orig / 255.
X_test = X_test_orig / 255.
X_train = np.transpose(X_train, [0, 3, 1, 2])
X_test = np.transpose(X_test, [0, 3, 1, 2])

Y_train = Y_train_orig.T
Y_test = Y_test_orig.T

print("number of training examples = " + str(X_train.shape[0]))
print("number of test examples = " + str(X_test.shape[0]))
print("X_train shape: " + str(X_train.shape))
print("Y_train shape: " + str(Y_train.shape))
print("X_test shape: " + str(X_test.shape))
print("Y_test shape: " + str(Y_test.shape))

构建网络、优化器、损失函数

model = ResNet50()
optimizer = optim.Adam(model.parameters())
criterion = nn.CrossEntropyLoss()
epochs = 2
batch_size = 32
train_dataset = MyDataset(X_train, Y_train)
train_data = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)

训练

model.train()
for epoch in range(epochs):
    for i, (x, y) in enumerate(train_data):
        x = x.float()
        y = y.long().squeeze()

        optimizer.zero_grad()
        y_hat = model(x)
        loss = criterion(y_hat, y)
        loss.backward()
        optimizer.step()

测试

model.eval()
with torch.no_grad():
    x = torch.tensor(X_test).float()
    y = torch.tensor(Y_test).long().squeeze()

    y_hat = model(x)
    loss = criterion(y_hat, y)
    print("Loss = ", loss.item())

    y_hat = torch.argmax(y_hat, dim=-1)
    correct_prediction = y_hat == y
    test_accuracy = torch.sum(correct_prediction).float() / y.shape[0]
    print("Test Accuracy = ", test_accuracy.item())

  人工智能 最新文章
2022吴恩达机器学习课程——第二课(神经网
第十五章 规则学习
FixMatch: Simplifying Semi-Supervised Le
数据挖掘Java——Kmeans算法的实现
大脑皮层的分割方法
【翻译】GPT-3是如何工作的
论文笔记:TEACHTEXT: CrossModal Generaliz
python从零学(六)
详解Python 3.x 导入(import)
【答读者问27】backtrader不支持最新版本的
上一篇文章      下一篇文章      查看所有文章
加:2022-03-16 22:21:38  更:2022-03-16 22:25:08 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 14:56:04-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码