最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Pytorch實現(xiàn)Fashion-mnist分類任務(wù)全過程

 更新時間:2022年12月14日 11:37:54   作者:LGDDDDDD  
這篇文章主要介紹了Pytorch實現(xiàn)Fashion-mnist分類任務(wù)全過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

數(shù)據(jù)概況

Fashion-mnist

經(jīng)典的MNIST數(shù)據(jù)集包含了大量的手寫數(shù)字。十幾年來,來自機器學(xué)習(xí)、機器視覺、人工智能、深度學(xué)習(xí)領(lǐng)域的研究員們把這個數(shù)據(jù)集作為衡量算法的基準(zhǔn)之一。

你會在很多的會議,期刊的論文中發(fā)現(xiàn)這個數(shù)據(jù)集的身影。實際上,MNIST數(shù)據(jù)集已經(jīng)成為算法作者的必測的數(shù)據(jù)集之一。

類別標(biāo)注

在Fashion-mnist數(shù)據(jù)集中,每個訓(xùn)練樣本都按照以下類別進行了標(biāo)注:

數(shù)據(jù)處理

對輸入進行歸一化

歸一化時需要統(tǒng)一進行 x = (x - mean) / std

train_trans = transforms.Compose([
        transforms.RandomCrop(28, padding=2),#數(shù)據(jù)增強
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        normalize
    ])
test_trans = transforms.Compose([
        transforms.ToTensor(),
        normalize
    ])
mnist_train = torchvision.datasets.FashionMNIST(root='../data',train=True,download=True,transform=train_trans)
mnist_test = torchvision.datasets.FashionMNIST(root='../data',train=False,download=True,transform=test_trans)
train_iter = torch.utils.data.DataLoader(mnist_train, batch_size=batch_size, shuffle=True)
test_iter = torch.utils.data.DataLoader(mnist_test, batch_size=batch_size, shuffle=False)
# 求整個數(shù)據(jù)集的均值
temp_sum = 0
cnt = 0
for X, y in train_iter:
    if y.shape[0] != batch_size:
        break   # 最后一個batch不足batch_size,這里就忽略了
    channel_mean = torch.mean(X, dim=(0,2,3))  # 按channel求均值(不過這里只有1個channel)
    cnt += 1   # cnt記錄的是batch的個數(shù),不是圖像
    temp_sum += channel_mean[0].item()
dataset_global_mean = temp_sum / cnt
print('整個數(shù)據(jù)集的像素均值:{}'.format(dataset_global_mean))
# 求整個數(shù)據(jù)集的標(biāo)準(zhǔn)差
cnt = 0
temp_sum = 0
for X, y in train_iter:
    if y.shape[0] != batch_size:
        break   # 最后一個batch不足batch_size,這里就忽略了
    residual = (X - dataset_global_mean) ** 2
    channel_var_mean = torch.mean(residual, dim=(0,2,3))
    cnt += 1   # cnt記錄的是batch的個數(shù),不是圖像
    temp_sum += math.sqrt(channel_var_mean[0].item())
dataset_global_std = temp_sum / cnt
print('整個數(shù)據(jù)集的像素標(biāo)準(zhǔn)差:{}'.format(dataset_global_std))

整個數(shù)據(jù)集的像素均值:0.2860366729433025

整個數(shù)據(jù)集的像素標(biāo)準(zhǔn)差:0.35288708155778725

數(shù)據(jù)增強

加入隨機裁剪和翻轉(zhuǎn)

 ============================ step 1/6 數(shù)據(jù) ============================
batch_size = 64
normalize = transforms.Normalize(mean=[0.286], std=[0.352])#對像素值歸一化
train_trans = transforms.Compose([
        transforms.RandomCrop(28, padding=2),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        normalize
    ])
test_trans = transforms.Compose([
        transforms.ToTensor(),
        normalize
    ])
mnist_train = torchvision.datasets.FashionMNIST(root='../data',train=True,download=True,transform=train_trans)
mnist_test = torchvision.datasets.FashionMNIST(root='../data',train=False,download=True,transform=test_trans)
train_iter = torch.utils.data.DataLoader(mnist_train, batch_size=batch_size, shuffle=True)
test_iter = torch.utils.data.DataLoader(mnist_test, batch_size=batch_size, shuffle=False)

定義Resnet網(wǎng)絡(luò)

class GlobalAvgPool2d(nn.Module):
    """
    全局平均池化層
    可通過將普通的平均池化的窗口形狀設(shè)置成輸入的高和寬實現(xiàn)
    """

    def __init__(self):
        super(GlobalAvgPool2d, self).__init__()

    def forward(self, x):
        return F.avg_pool2d(x, kernel_size=x.size()[2:])


class FlattenLayer(torch.nn.Module):
    def __init__(self):
        super(FlattenLayer, self).__init__()

    def forward(self, x):  # x shape: (batch, *, *, ...)
        return x.view(x.shape[0], -1)


class Residual(nn.Module):
    def __init__(self, in_channels, out_channels, use_1x1conv=False, stride=1):
        """
            use_1×1conv: 是否使用額外的1x1卷積層來修改通道數(shù)
            stride: 卷積層的步幅, resnet使用步長為2的卷積來替代pooling的作用,是個很贊的idea
        """
        super(Residual, self).__init__()
        self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1, stride=stride)
        self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1)
        if use_1x1conv:
            self.conv3 = nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=stride)
        else:
            self.conv3 = None
        self.bn1 = nn.BatchNorm2d(out_channels)
        self.bn2 = nn.BatchNorm2d(out_channels)

    def forward(self, X):
        Y = F.relu(self.bn1(self.conv1(X)))
        Y = self.bn2(self.conv2(Y))
        if self.conv3:
            X = self.conv3(X)
        return F.relu(Y + X)


def resnet_block(in_channels, out_channels, num_residuals, first_block=False):
    '''
    resnet block
    num_residuals: 當(dāng)前block包含多少個殘差塊
    first_block: 是否為第一個block
    一個resnet block由num_residuals個殘差塊組成
    其中第一個殘差塊起到了通道數(shù)的轉(zhuǎn)換和pooling的作用
    后面的若干殘差塊就是完成正常的特征提取
    '''
    if first_block:
        assert in_channels == out_channels  # 第一個模塊的輸出通道數(shù)同輸入通道數(shù)一致
    blk = []
    for i in range(num_residuals):
        if i == 0 and not first_block:
            blk.append(Residual(in_channels, out_channels, use_1x1conv=True, stride=2))
        else:
            blk.append(Residual(out_channels, out_channels))
    return nn.Sequential(*blk)


# 定義resnet模型結(jié)構(gòu)
net = nn.Sequential(
    nn.Conv2d(1, 32, kernel_size=3, stride=1, padding=1),  # TODO: 縮小感受野, 縮channel
    nn.BatchNorm2d(32),
    nn.ReLU())
# nn.ReLU(),
# nn.MaxPool2d(kernel_size=2, stride=2))   # TODO:去掉maxpool縮小感受野

# 然后是連續(xù)4個block
net.add_module("resnet_block1", resnet_block(32, 32, 2, first_block=True))  # TODO: channel統(tǒng)一減半
net.add_module("resnet_block2", resnet_block(32, 64, 2))
net.add_module("resnet_block3", resnet_block(64, 128, 2))
net.add_module("resnet_block4", resnet_block(128, 256, 2))
# global average pooling
net.add_module("global_avg_pool", GlobalAvgPool2d())
# fc layer
net.add_module("fc", nn.Sequential(FlattenLayer(), nn.Linear(256, 10)))

訓(xùn)練與測試

def evaluate_accuracy(data_iter, net, device=None):
	#評估模型在測試集的準(zhǔn)確率
    if device is None and isinstance(net, torch.nn.Module):
        # 如果沒指定device就使用net的device
        device = list(net.parameters())[0].device
    net.eval()
    acc_sum, n = 0.0, 0
    with torch.no_grad():
        for X, y in data_iter:
            acc_sum += (net(X.to(device)).argmax(dim=1) == y.to(device)).float().sum().cpu().item()
            n += y.shape[0]
    net.train()  # 改回訓(xùn)練模式
    return acc_sum / n


def train_model(net, train_iter, test_iter, batch_size, optimizer, device, num_epochs):
    net = net.to(device)
    print("training on ", device)
    loss = torch.nn.CrossEntropyLoss()
    best_test_acc = 0
    for epoch in range(num_epochs):
        train_l_sum, train_acc_sum, n, batch_count, start = 0.0, 0.0, 0, 0, time.time()
        for X, y in train_iter:
            X = X.to(device)
            y = y.to(device)
            y_hat = net(X)
            l = loss(y_hat, y)
            optimizer.zero_grad()
            l.backward()
            optimizer.step()
            train_l_sum += l.cpu().item()
            train_acc_sum += (y_hat.argmax(dim=1) == y).sum().cpu().item()
            n += y.shape[0]
            batch_count += 1
        test_acc = evaluate_accuracy(test_iter, net)
        print('epoch %d, loss %.4f, train acc %.3f, test acc %.3f, time %.1f sec'
              % (epoch + 1, train_l_sum / batch_count, train_acc_sum / n, test_acc, time.time() - start))
        if test_acc > best_test_acc:
            print('find best! save at model/best.pth')
            best_test_acc = test_acc
            torch.save(net.state_dict(), 'model/best.pth')


lr, num_epochs = 0.01, 10
optimizer = torch.optim.Adam(net.parameters(), lr=lr)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
train_model(net, train_iter, test_iter, batch_size, optimizer, device, num_epochs)

完整代碼

import os
import sys
import time
import torch
from torch import nn, optim
import torch.nn.functional as F
import torchvision
from torchvision import transforms



class GlobalAvgPool2d(nn.Module):
    """
    全局平均池化層
    可通過將普通的平均池化的窗口形狀設(shè)置成輸入的高和寬實現(xiàn)
    """

    def __init__(self):
        super(GlobalAvgPool2d, self).__init__()

    def forward(self, x):
        return F.avg_pool2d(x, kernel_size=x.size()[2:])


class FlattenLayer(torch.nn.Module):
    def __init__(self):
        super(FlattenLayer, self).__init__()

    def forward(self, x):  # x shape: (batch, *, *, ...)
        return x.view(x.shape[0], -1)


class Residual(nn.Module):
    def __init__(self, in_channels, out_channels, use_1x1conv=False, stride=1):
        """
            use_1×1conv: 是否使用額外的1x1卷積層來修改通道數(shù)
            stride: 卷積層的步幅, resnet使用步長為2的卷積來替代pooling的作用,是個很贊的idea
        """
        super(Residual, self).__init__()
        self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1, stride=stride)
        self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1)
        if use_1x1conv:
            self.conv3 = nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=stride)
        else:
            self.conv3 = None
        self.bn1 = nn.BatchNorm2d(out_channels)
        self.bn2 = nn.BatchNorm2d(out_channels)

    def forward(self, X):
        Y = F.relu(self.bn1(self.conv1(X)))
        Y = self.bn2(self.conv2(Y))
        if self.conv3:
            X = self.conv3(X)
        return F.relu(Y + X)


def resnet_block(in_channels, out_channels, num_residuals, first_block=False):
    '''
    resnet block
    num_residuals: 當(dāng)前block包含多少個殘差塊
    first_block: 是否為第一個block
    一個resnet block由num_residuals個殘差塊組成
    其中第一個殘差塊起到了通道數(shù)的轉(zhuǎn)換和pooling的作用
    后面的若干殘差塊就是完成正常的特征提取
    '''
    if first_block:
        assert in_channels == out_channels  # 第一個模塊的輸出通道數(shù)同輸入通道數(shù)一致
    blk = []
    for i in range(num_residuals):
        if i == 0 and not first_block:
            blk.append(Residual(in_channels, out_channels, use_1x1conv=True, stride=2))
        else:
            blk.append(Residual(out_channels, out_channels))
    return nn.Sequential(*blk)


# 定義resnet模型結(jié)構(gòu)
net = nn.Sequential(
    nn.Conv2d(1, 32, kernel_size=3, stride=1, padding=1),  # TODO: 縮小感受野, 縮channel
    nn.BatchNorm2d(32),
    nn.ReLU())
# nn.ReLU(),
# nn.MaxPool2d(kernel_size=2, stride=2))   # TODO:去掉maxpool縮小感受野

# 然后是連續(xù)4個block
net.add_module("resnet_block1", resnet_block(32, 32, 2, first_block=True))  # TODO: channel統(tǒng)一減半
net.add_module("resnet_block2", resnet_block(32, 64, 2))
net.add_module("resnet_block3", resnet_block(64, 128, 2))
net.add_module("resnet_block4", resnet_block(128, 256, 2))
# global average pooling
net.add_module("global_avg_pool", GlobalAvgPool2d())
# fc layer
net.add_module("fc", nn.Sequential(FlattenLayer(), nn.Linear(256, 10)))

def load_data_fashion_mnist(batch_size, root='../data'):
    """Download the fashion mnist dataset and then load into memory."""

    normalize = transforms.Normalize(mean=[0.28], std=[0.35])
    train_augs = transforms.Compose([
        transforms.RandomCrop(28, padding=2),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        normalize
    ])

    test_augs = transforms.Compose([
        transforms.ToTensor(),
        normalize
    ])

    mnist_train = torchvision.datasets.FashionMNIST(root=root, train=True, download=True, transform=train_augs)
    mnist_test = torchvision.datasets.FashionMNIST(root=root, train=False, download=True, transform=test_augs)
    if sys.platform.startswith('win'):
        num_workers = 0  # 0表示不用額外的進程來加速讀取數(shù)據(jù)
    else:
        num_workers = 4
    train_iter = torch.utils.data.DataLoader(mnist_train, batch_size=batch_size, shuffle=True, num_workers=num_workers)
    test_iter = torch.utils.data.DataLoader(mnist_test, batch_size=batch_size, shuffle=False, num_workers=num_workers)

    return train_iter, test_iter


print('訓(xùn)練...')
batch_size = 64
train_iter, test_iter = load_data_fashion_mnist(batch_size, root='../data')


def evaluate_accuracy(data_iter, net, device=None):
    if device is None and isinstance(net, torch.nn.Module):
        # 如果沒指定device就使用net的device
        device = list(net.parameters())[0].device
    net.eval()
    acc_sum, n = 0.0, 0
    with torch.no_grad():
        for X, y in data_iter:
            acc_sum += (net(X.to(device)).argmax(dim=1) == y.to(device)).float().sum().cpu().item()
            n += y.shape[0]
    net.train()  # 改回訓(xùn)練模式
    return acc_sum / n


def train_model(net, train_iter, test_iter, batch_size, optimizer, device, num_epochs, lr, lr_period, lr_decay):
    net = net.to(device)
    print("training on ", device)
    loss = torch.nn.CrossEntropyLoss()
    best_test_acc = 0
    for epoch in range(num_epochs):
        train_l_sum, train_acc_sum, n, batch_count, start = 0.0, 0.0, 0, 0, time.time()

        if epoch > 0 and epoch % lr_period == 0:  # 每lr_period個epoch,學(xué)習(xí)率衰減一次
            lr = lr * lr_decay
            for param_group in optimizer.param_groups:
                param_group['lr'] = lr

        for X, y in train_iter:
            X = X.to(device)
            y = y.to(device)
            y_hat = net(X)
            l = loss(y_hat, y)
            optimizer.zero_grad()
            l.backward()
            optimizer.step()
            train_l_sum += l.cpu().item()
            train_acc_sum += (y_hat.argmax(dim=1) == y).sum().cpu().item()
            n += y.shape[0]
            batch_count += 1
        test_acc = evaluate_accuracy(test_iter, net)
        print('epoch %d, loss %.4f, train acc %.3f, test acc %.3f, time %.1f sec'
              % (epoch + 1, train_l_sum / batch_count, train_acc_sum / n, test_acc, time.time() - start))
        if test_acc > best_test_acc:
            print('find best! save at model/best.pth')
            best_test_acc = test_acc
            torch.save(net.state_dict(), 'model/best.pth')
            # utils.save_model({
            #    'arch': args.model,
            #    'state_dict': net.state_dict()
            # }, 'saved-models/{}-run-{}.pth.tar'.format(args.model, run))


lr, num_epochs, lr_period, lr_decay = 0.01, 50, 5, 0.1
#optimizer = optim.Adam(net.parameters(), lr=lr)
optimizer = optim.SGD(net.parameters(), lr=lr, momentum=0.9, weight_decay=5e-4)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
train_model(net, train_iter, test_iter, batch_size, optimizer, device, num_epochs, lr, lr_period, lr_decay)

print('加載最優(yōu)模型')
net.load_state_dict(torch.load('model/best.pth'))
net = net.to(device)

print('inference測試集')
net.eval()
id = 0
preds_list = []
with torch.no_grad():
    for X, y in test_iter:
        batch_pred = list(net(X.to(device)).argmax(dim=1).cpu().numpy())
        for y_pred in batch_pred:
            preds_list.append((id, y_pred))
            id += 1

print('生成測試集評估文件')
with open('result.csv', 'w') as f:
    f.write('ID,Prediction\n')
    for id, pred in preds_list:
        f.write('{},{}\n'.format(id, pred))

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python 利用pandas和mysql-connector獲取Excel數(shù)據(jù)寫入到MySQL數(shù)據(jù)庫

    Python 利用pandas和mysql-connector獲取Excel數(shù)據(jù)寫入到MySQL數(shù)據(jù)庫

    在實際應(yīng)用中,我們可能需要將Excel表格中的數(shù)據(jù)導(dǎo)入到MySQL數(shù)據(jù)庫中,以便于進行進一步的數(shù)據(jù)分析和處理,本文將介紹如何使用Python將Excel表格中的數(shù)據(jù)插入到MySQL數(shù)據(jù)庫中,需要的朋友可以參考下
    2023-10-10
  • Python?OLS?雙向逐步回歸方式

    Python?OLS?雙向逐步回歸方式

    這篇文章主要介紹了Python?OLS?雙向逐步回歸方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • python中賦值語句的特點和形式

    python中賦值語句的特點和形式

    這篇文章主要介紹了python中賦值語句的特點和形式,文中介紹了多目標(biāo)賦值的共享引用問題,多目標(biāo)賦值其實是多個目標(biāo)對同一個內(nèi)存空間的引用,這里要分兩種情況,當(dāng)被引用對象是不可變對象時則不存在問題,感興趣的朋友跟隨小編一起看看吧
    2023-12-12
  • Python如何批量提取pdf文本內(nèi)容

    Python如何批量提取pdf文本內(nèi)容

    PyMuPDF功能強大,并且支持文本提取、圖片提取、頁面操作等,本文將為大家介紹一下Python如何使用PyMuPDF批量提取PDF文本內(nèi)容,感興趣的可以了解下
    2025-04-04
  • Python快速實現(xiàn)一鍵摳圖功能的全過程

    Python快速實現(xiàn)一鍵摳圖功能的全過程

    你有沒想過,Python也能成為這樣的一種工具:在只有一張圖片,需要細致地摳出人物的情況下,能幫你減少摳圖步驟,這篇文章主要給大家介紹了關(guān)于Python快速實現(xiàn)一鍵摳圖功能的相關(guān)資料,需要的朋友可以參考下
    2021-06-06
  • python根據(jù)開頭和結(jié)尾字符串獲取中間字符串的方法

    python根據(jù)開頭和結(jié)尾字符串獲取中間字符串的方法

    這篇文章主要介紹了python根據(jù)開頭和結(jié)尾字符串獲取中間字符串的方法,涉及Python操作字符串截取的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03
  • Python+PyQt5實現(xiàn)滅霸響指功能

    Python+PyQt5實現(xiàn)滅霸響指功能

    這篇文章主要介紹了Python+PyQt5實現(xiàn)滅霸響指功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-05-05
  • python根據(jù)完整路徑獲得盤名/路徑名/文件名/文件擴展名的方法

    python根據(jù)完整路徑獲得盤名/路徑名/文件名/文件擴展名的方法

    這篇文章主要介紹了python根據(jù)完整路徑獲得盤名,路徑名,文件名,文件擴展名的代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-04-04
  • python?中的?super詳解

    python?中的?super詳解

    這篇文章主要介紹了python?中的?super,提到 super,最直接的想法就是它代表了父類,替父類執(zhí)行某些方法,但是理解也僅止步于此,下面對 super 做進一步理解,需要的朋友可以參考下
    2022-08-08
  • 利用OpenCV給彩色圖像添加椒鹽噪聲的方法

    利用OpenCV給彩色圖像添加椒鹽噪聲的方法

    椒鹽噪聲是數(shù)字圖像中的常見噪聲,一般是圖像傳感器、傳輸信道及解碼處理等產(chǎn)生的黑白相間的亮暗點噪聲,椒鹽噪聲常由圖像切割產(chǎn)生,這篇文章主要給大家介紹了關(guān)于利用OpenCV給彩色圖像添加椒鹽噪聲的相關(guān)資料,需要的朋友可以參考下
    2021-10-10

最新評論

庐江县| 赤城县| 贵德县| 万安县| 高要市| 平塘县| 榆中县| 石泉县| 东乡县| 绥中县| 璧山县| 神农架林区| 汉源县| 密云县| 天门市| 桐乡市| 瑞丽市| 舒兰市| 鹿邑县| 额尔古纳市| 阳城县| 寿宁县| 平乐县| 南郑县| 庆元县| 长垣县| 子长县| 同江市| 清河县| 竹山县| 佳木斯市| 郑州市| 黔南| 上虞市| 托克托县| 泽州县| 吴忠市| 哈巴河县| 汾西县| 武鸣县| 申扎县|