深度學(xué)習(xí)實(shí)踐1--FashionMNIST分類-創(chuàng)新互聯(lián)

目錄

網(wǎng)站的建設(shè)創(chuàng)新互聯(lián)建站專注網(wǎng)站定制,經(jīng)驗(yàn)豐富,不做模板,主營網(wǎng)站定制開發(fā).小程序定制開發(fā),H5頁面制作!給你煥然一新的設(shè)計(jì)體驗(yàn)!已為成都塑料袋等企業(yè)提供專業(yè)服務(wù)。

前言

一、深度學(xué)習(xí)是什么?

二、FashionMNIST分類

1.介紹數(shù)據(jù)集

2.獲取數(shù)據(jù)集

3.分批迭代數(shù)據(jù)?

4.圖片可視化

5.構(gòu)建網(wǎng)絡(luò)模型

6.選擇損失函數(shù)和優(yōu)化器

7.訓(xùn)練

8.驗(yàn)證

9.訓(xùn)練并驗(yàn)證

10.寫成csv

11.完整代碼?

總結(jié)


前言

隨著人工智能的發(fā)展,深度學(xué)習(xí)也越發(fā)重要,目前深度學(xué)習(xí)可以應(yīng)用到多方面,如圖像處理領(lǐng)域、語音識(shí)別領(lǐng)域、自然語言處理鄰域等。本篇是深度學(xué)習(xí)的入門篇,應(yīng)用于圖像分類。


一、深度學(xué)習(xí)是什么?

一般是指通過訓(xùn)練多層網(wǎng)絡(luò)結(jié)構(gòu)對(duì)未知數(shù)據(jù)進(jìn)行分類或回歸。

二、FashionMNIST分類 1.介紹數(shù)據(jù)集

Fashion-MNIST是Zalando的研究論文中提出的一個(gè)數(shù)據(jù)集,由包含60000個(gè)實(shí)例的訓(xùn)練集和包含10000個(gè)實(shí)例的測(cè)試集組成。每個(gè)實(shí)例包含一張28x28的灰度服飾圖像和對(duì)應(yīng)的類別標(biāo)記(共有10類服飾,分別是:t-shirt(T恤),trouser(牛仔褲),pullover(套衫),dress(裙子),coat(外套),sandal(涼鞋),shirt(襯衫),sneaker(運(yùn)動(dòng)鞋),bag(包),ankle boot(短靴))。

2.獲取數(shù)據(jù)集

使用torchvision.datasets.FashionMNIST獲取內(nèi)置數(shù)據(jù)集

import torchvision
from torchvision import transforms

# 將內(nèi)置數(shù)據(jù)集的圖片大小轉(zhuǎn)為1*28*28后轉(zhuǎn)化為tensor
# 可以對(duì)圖片進(jìn)行增廣以增加精確度
train_transform = transforms.Compose([
    transforms.Resize(28),
    transforms.ToTensor()
])
# test_transform = transforms.Compose([])

train_data = torchvision.datasets.FashionMNIST(root='/data/FashionMNIST', train=True, download=True,
                                               transform=train_transform)
test_data = torchvision.datasets.FashionMNIST(root='/data/FashionMNIST', train=False, download=True,
                                              transform=train_transform)

print(len(train_data))  # 長度為60000
print(train_data[0])
from torch.utils.data import Dataset
import pandas as pd


# 如果是自己的數(shù)據(jù)集需要自己構(gòu)建dataset
class FashionMNISTDataset(Dataset):
    def __init__(self, df, transform=None):
        self.df = df
        self.transform = transform
        self.images = df.iloc[:, 1:].values.astype(np.uint8)
        self.labels = df.iloc[:, 0].values

    def __len__(self):
        return len(self.images)

    def __getitem__(self, idx):
        image = self.images[idx].reshape(28, 28, 1)
        label = int(self.labels[idx])
        if self.transform is not None:
            image = self.transform(image)
        else:
            image = torch.tensor(image / 255., dtype=torch.float)
        label = torch.tensor(label, dtype=torch.long)
        return image, label


train = pd.read_csv("../FashionMNIST/fashion-mnist_train.csv")
# train.head(10)
test = pd.read_csv("../FashionMNIST/fashion-mnist_test.csv")
# test.head(10)
print(len(test))
train_iter = FashionMNISTDataset(train, data_transform)
print(train_iter)
test_iter = FashionMNISTDataset(test, data_transform)
3.分批迭代數(shù)據(jù)?

調(diào)用DataLoader包迭代數(shù)據(jù),shuffle用于打亂數(shù)據(jù)集,訓(xùn)練集需要打亂,測(cè)試集不用打亂

from torch.utils.data import DataLoader


batch_size = 256  # 迭代一批的大?。梢栽O(shè)置為其它的,一般用2**n)
num_workers = 4  # windows設(shè)置為0
train_iter = DataLoader(train_data, batch_size=batch_size, shuffle=True, num_workers=num_workers)
test_iter = DataLoader(test_data, batch_size=batch_size, shuffle=False, num_workers=num_workers)

# print(len(train_iter))  # 253
4.圖片可視化
import matplotlib as plt

def show_images(imgs, num_rows, num_cols, targets, scale=1.5):
    figsize = (num_cols * scale, num_rows * scale)
    _, axes = plt.subplots(num_rows, num_cols, figsize=figsize)
    axes = axes.flatten()
    for ax, img, target in zip(axes, imgs, targets):
        if torch.is_tensor(img):
            # 圖片張量
            ax.imshow(img.numpy())
        else:
            # PIL
            ax.imshow(img)
        # 設(shè)置坐標(biāo)軸不可見
        ax.axes.get_xaxis().set_visible(False)
        ax.axes.get_yaxis().set_visible(False)
        plt.subplots_adjust(hspace=0.35)
        ax.set_title('{}'.format(target))
    return axes


# 將dataloader轉(zhuǎn)換成迭代器才可以使用next方法
X, y = next(iter(train_iter))
show_images(X.squeeze(), 3, 8, targets=y)
plt.show()

5.構(gòu)建網(wǎng)絡(luò)模型

卷積->池化->激活->連接

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 64, 1, padding=1)
        self.conv2 = nn.Conv2d(64, 64, 3, padding=1)
        self.pool1 = nn.MaxPool2d(2, 2)
        self.bn1 = nn.BatchNorm2d(64)
        self.relu1 = nn.ReLU()

        self.conv3 = nn.Conv2d(64, 128, 3, padding=1)
        self.conv4 = nn.Conv2d(128, 128, 3, padding=1)
        self.pool2 = nn.MaxPool2d(2, 2, padding=1)
        self.bn2 = nn.BatchNorm2d(128)
        self.relu2 = nn.ReLU()

        self.fc5 = nn.Linear(128 * 8 * 8, 512)
        self.drop1 = nn.Dropout()  # Dropout可以比較有效的緩解過擬合的發(fā)生,在一定程度上達(dá)到正則化的效果。
        self.fc6 = nn.Linear(512, 10)

    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        x = self.pool1(x)
        x = self.bn1(x)
        x = self.relu1(x)

        x = self.conv3(x)
        x = self.conv4(x)
        x = self.pool2(x)
        x = self.bn2(x)
        x = self.relu2(x)

        # print(" x shape ",x.size())
        x = x.view(-1, 128 * 8 * 8)
        x = F.relu(self.fc5(x))
        x = self.drop1(x)
        x = self.fc6(x)

        return x


device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = Net()
model = model.to(device)
print(model)

6.選擇損失函數(shù)和優(yōu)化器
import torch.optim as optim


criterion = nn.CrossEntropyLoss()  # 交叉熵?fù)p失
optimizer = optim.Adam(model.parameters(), lr=0.001)  # lr->learning rate學(xué)習(xí)率
7.訓(xùn)練
def train(epoch):
    model.train()
    train_loss = 0
    for data,label in train_iter:
        data,label = data.cuda() ,label.cuda()
        optimizer.zero_grad()
        output = model(data)
        loss = criterion(output,label)
        loss.backward()  # 反向計(jì)算
        optimizer.step()  # 優(yōu)化
        train_loss += loss.item()*data.size(0)
    train_loss = train_loss/len(train_iter.dataset)
    print('Epoch:{}\tTraining Loss:{:.6f}'.format(epoch+1, train_loss))


# train(1)
8.驗(yàn)證
def val():
    model.eval()
    val_loss = 0
    gt_labels = []
    pred_labels = []
    with torch.no_grad():
        for data, label in test_iter:
            data, label = data.cuda(), label.cuda()
            output = model(data)
            preds = torch.argmax(output, 1)
            gt_labels.append(label.cpu().data.numpy())
            pred_labels.append(preds.cpu().data.numpy())
    gt_labels, pred_labels = np.concatenate(gt_labels), np.concatenate(pred_labels)
    acc = np.sum(gt_labels == pred_labels) / len(pred_labels)
    print(gt_labels,pred_labels)
    print('Accuracy: {:6f}'.format(acc))
9.訓(xùn)練并驗(yàn)證
epochs = 20
for epoch in range(epochs):
    train(epoch)
    val()  

torch.save(model,"mymmodel.pth")  # 保存模型
10.寫成csv

用于提交比賽

# 寫成csv
model = torch.load("mymodel.pth")
model = model.to(device)
id = 0
preds_list = []
with torch.no_grad():
    for x, y in test_iter:
        batch_pred = list(model(x.to(device)).argmax(dim=1).cpu().numpy())
        for y_pred in batch_pred:
            preds_list.append((id, y_pred))
            id += 1
# print(batch_pred)

with open('result.csv', 'w') as f:
    f.write('Id,Category\n')
    for id, pred in preds_list:
        f.write('{},{}\n'.format(id, pred))
11.完整代碼?
import torch
import torchvision
from torchvision import transforms
from torch.utils.data import Dataset, DataLoader
import matplotlib.pyplot as plt
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import numpy as np
import pandas as pd

train_transform = transforms.Compose([
    # transforms.ToPILImage(),   # 這一步取決于后續(xù)的數(shù)據(jù)讀取方式,如果使用內(nèi)置數(shù)據(jù)集則不需要
    transforms.Resize(28),
    transforms.ToTensor()
])
# test_transform = transforms.Compose([
#     transforms.ToTensor()
# ])

train_data = torchvision.datasets.FashionMNIST(root='/data/FashionMNIST', train=True, download=True,
                                               transform=train_transform)
test_data = torchvision.datasets.FashionMNIST(root='/data/FashionMNIST', train=False, download=True,
                                              transform=train_transform)
# print(test_data[0])

batch_size = 256
num_workers = 0
train_iter = DataLoader(train_data, batch_size=batch_size, shuffle=True, num_workers=num_workers)
test_iter = DataLoader(test_data, batch_size=batch_size, shuffle=False, num_workers=num_workers)


# # 如果是自己的數(shù)據(jù)集需要自己構(gòu)建dataset
# class FashionMNISTDataset(Dataset):
#     def __init__(self, df, transform=None):
#         self.df = df
#         self.transform = transform
#         self.images = df.iloc[:, 1:].values.astype(np.uint8)
#         self.labels = df.iloc[:, 0].values
#
#     def __len__(self):
#         return len(self.images)
#
#     def __getitem__(self, idx):
#         image = self.images[idx].reshape(28, 28, 1)
#         label = int(self.labels[idx])
#         if self.transform is not None:
#             image = self.transform(image)
#         else:
#             image = torch.tensor(image / 255., dtype=torch.float)
#         label = torch.tensor(label, dtype=torch.long)
#         return image, label
#
#
# train = pd.read_csv("../FashionMNIST/fashion-mnist_train.csv")
# # train.head(10)
# test = pd.read_csv("../FashionMNIST/fashion-mnist_test.csv")
# # test.head(10)
# print(len(test))
# train_iter = FashionMNISTDataset(train, data_transform)
# print(train_iter)
# test_iter = FashionMNISTDataset(test, data_transform)


# print(train_iter)


def show_images(imgs, num_rows, num_cols, targets, scale=1.5):
    """Plot a list of images."""
    figsize = (num_cols * scale, num_rows * scale)
    _, axes = plt.subplots(num_rows, num_cols, figsize=figsize)
    axes = axes.flatten()
    for ax, img, target in zip(axes, imgs, targets):
        if torch.is_tensor(img):
            # 圖片張量
            ax.imshow(img.numpy())
        else:
            # PIL
            ax.imshow(img)
        # 設(shè)置坐標(biāo)軸不可見
        ax.axes.get_xaxis().set_visible(False)
        ax.axes.get_yaxis().set_visible(False)
        plt.subplots_adjust(hspace=0.35)
        ax.set_title('{}'.format(target))
    return axes


# 將dataloader轉(zhuǎn)換成迭代器才可以使用next方法
# X, y = next(iter(train_iter))
# show_images(X.squeeze(), 3, 8, targets=y)
# plt.show()

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 64, 1, padding=1)
        self.conv2 = nn.Conv2d(64, 64, 3, padding=1)
        self.pool1 = nn.MaxPool2d(2, 2)
        self.bn1 = nn.BatchNorm2d(64)
        self.relu1 = nn.ReLU()

        self.conv3 = nn.Conv2d(64, 128, 3, padding=1)
        self.conv4 = nn.Conv2d(128, 128, 3, padding=1)
        self.pool2 = nn.MaxPool2d(2, 2, padding=1)
        self.bn2 = nn.BatchNorm2d(128)
        self.relu2 = nn.ReLU()

        self.fc5 = nn.Linear(128 * 8 * 8, 512)
        self.drop1 = nn.Dropout()
        self.fc6 = nn.Linear(512, 10)

    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        x = self.pool1(x)
        x = self.bn1(x)
        x = self.relu1(x)

        x = self.conv3(x)
        x = self.conv4(x)
        x = self.pool2(x)
        x = self.bn2(x)
        x = self.relu2(x)

        # print(" x shape ",x.size())
        x = x.view(-1, 128 * 8 * 8)
        x = F.relu(self.fc5(x))
        x = self.drop1(x)
        x = self.fc6(x)

        return x


device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = Net()
model = model.to(device)
# print(model)

criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)


def train(epoch):
    model.train()
    train_loss = 0
    train_loss_list = []
    for data, label in train_iter:
        data, label = data.cuda(), label.cuda()
        optimizer.zero_grad()
        output = model(data)
        loss = criterion(output, label)
        loss.backward()
        optimizer.step()
        train_loss += loss.item() * data.size(0)
    train_loss = train_loss / len(train_iter.dataset)
    train_loss_list.append(train_loss)
    print('Epoch:{}\tTraining Loss:{:.6f}'.format(epoch + 1, train_loss))


def val():
    model.eval()
    gt_labels = []
    pred_labels = []
    acc_list = []
    with torch.no_grad():
        for data, label in test_iter:
            data, label = data.cuda(), label.cuda()
            output = model(data)
            preds = torch.argmax(output, 1)
            gt_labels.append(label.cpu().data.numpy())
            pred_labels.append(preds.cpu().data.numpy())
    gt_labels, pred_labels = np.concatenate(gt_labels), np.concatenate(pred_labels)
    acc = np.sum(gt_labels == pred_labels) / len(pred_labels)
    acc_list.append(acc)
    print(gt_labels, pred_labels)
    print('Accuracy: {:6f}'.format(acc))


epochs = 2
for epoch in range(epochs):
    train(epoch)
    val()

torch.save(model, "mymodel.pth")

# 寫成csv
model = torch.load("mymodel.pth")
model = model.to(device)
id = 0
preds_list = []
with torch.no_grad():
    for x, y in test_iter:
        batch_pred = list(model(x.to(device)).argmax(dim=1).cpu().numpy())
        for y_pred in batch_pred:
            preds_list.append((id, y_pred))
            id += 1
# print(preds_list)

with open('result_ljh.csv', 'w') as f:
    f.write('Id,Category\n')
    for id, pred in preds_list:
        f.write('{},{}\n'.format(id, pred))


總結(jié)

里面還有許多可以改進(jìn)的地方,如數(shù)據(jù)增廣,調(diào)整超參數(shù),卷積模型,調(diào)用多個(gè)GPU加快,同時(shí)還可以可視化損失及準(zhǔn)確率。

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧

新聞名稱:深度學(xué)習(xí)實(shí)踐1--FashionMNIST分類-創(chuàng)新互聯(lián)
分享路徑:http://www.muchs.cn/article26/dgiecg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、App設(shè)計(jì)品牌網(wǎng)站建設(shè)、企業(yè)建站網(wǎng)站收錄、網(wǎng)頁設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管