# Author:Richard
# 分类食物 Bread, Dairy product, Dessert, Egg, Fried food, Meat, Noodles/Pasta, Rice, Seafood, Soup, Vegetable/Fruit
import numpy as np
import pandas as pd
import os
import cv2
import torch
import torch.nn as nn
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import time
from torch.utils.data import DataLoader, Dataset
# read files function
def readfile(path, label):
'''
:param path: file path
:param label: boolean variable, if true, it means we don't return y
:return: x (x, y)
'''
image_dir = sorted(os.listdir(path)) # os.listdir() 方法用于返回指定的文件夹包含的文件或文件夹的名字的列表
x = np.zeros((len(image_dir), 128, 128, 3), dtype=np.uint8) # OpenCV显示图片—dtype=np.uint8
y = np.zeros((len(image_dir)), dtype=np.uint8)
for i, file in enumerate(image_dir):
img = cv2.imread(os.path.join(path, file)) # 必须是全英文路径
x[i, :, :] = cv2.resize(img, (128, 128))
if label:
y[i] = int(file.split("_")[0])
if label:
return x, y
else:
return x
workspace_dir = r"G:\proton_Dicom\PythonTest\food-11"
print("Reading data")
train_x, train_y = readfile(os.path.join(workspace_dir, "training"), True)
print("Size of training data = {}".format(len(train_x)))
val_x, val_y = readfile(os.path.join(workspace_dir, "validation"), True)
print("Size of validation data = {}".format(len(val_x)))
test_x = readfile(os.path.join(workspace_dir, "testing"), False)
print("Size of Testing data = {}".format(len(test_x)))
# train data augmentation
train_transform = transforms.Compose([
transforms.ToPILImage(), # transforms.ToPILImage()是转换数据格式,把数据转换为tensfroms格式。只有转换为tensfroms格式才能进行后面的处理。
transforms.RandomHorizontalFlip(), # RandomHorizontalFlip:以0.5的概率水平翻转给定的PIL图像
transforms.RandomRotation(15), # 功能:随机旋转图片 degree: 旋转角度 当为 a 时,在 (-a, a) 之间随机选择旋转角度 当为 (a, b) 时,在 (a, b) 之间随机选择旋转角度
transforms.ToTensor(), # transforms.ToTensor() 转换为tensor格式,这个格式可以直接输入进神经网络了 将图片向量化,并 normalize 到 [0,1] (data normalization)
])
# test data
test_transform = transforms.Compose([
transforms.ToPILImage(),
transforms.ToTensor(),
])
class ImgDataset(Dataset):
def __init__(self, x, y=None, transform=None):
self.x = x
# label is required to be a LongTensor
self.y = y
if y is not None:
self.y = torch.LongTensor(y)
self.transform = transform
def __len__(self):
return len(self.x)
def __getitem__(self, index):
X = self.x[index]
if self.transform is not None:
X = self.transform(X)
if self.y is not None:
Y = self.y[index]
return X, Y
else:
return X
batch_size = 128
train_set = ImgDataset(train_x, train_y, train_transform)
val_set = ImgDataset(val_x, val_y, test_transform)
train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=True)
val_loader = DataLoader(val_set, batch_size=batch_size, shuffle=False)
class Classifier(nn.Module):
def __init__(self):
super(Classifier, self).__init__()
# torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding)
# torch.nn.MaxPool2d(kernel_size, stride, padding)
# input 維度 [3, 128, 128]
self.cnn = nn.Sequential(
nn.Conv2d(3, 64, 3, 1, 1), # [64, 128, 128]
nn.BatchNorm2d(64),
nn.ReLU(),
nn.MaxPool2d(2, 2, 0), # [64, 64, 64]
# in_channels 这个很好理解,就是输入的四维张量[N, C, H, W]中的C了,即输入张量的channels数。这个形参是确定权重等可学习参数的shape所必需的。
# out_channels 也很好理解,即期望的四维输出张量的channels数,不再多说。
# kernal_size 卷积核的大小 3*3
# stride 卷积核在图像窗口上每次平移的间隔,即所谓的步长
# 以padding = 1为例,若原始图像大小为32x32,那么padding后的图像大小就变成了34x34,而不是33x33。
# 根据统计的mean 和var来对数据进行标准化
# ReLU(x)=max(0,x)
#
nn.Conv2d(64, 128, 3, 1, 1), # [128, 64, 64]
nn.BatchNorm2d(128),
nn.ReLU(),
nn.MaxPool2d(2, 2, 0), # [128, 32, 32]
nn.Conv2d(128, 256, 3, 1, 1), # [256, 32, 32]
nn.BatchNorm2d(256),
nn.ReLU(),
nn.MaxPool2d(2, 2, 0), # [256, 16, 16]
nn.Conv2d(256, 512, 3, 1, 1), # [512, 16, 16]
nn.BatchNorm2d(512),
nn.ReLU(),
nn.MaxPool2d(2, 2, 0), # [512, 8, 8]
nn.Conv2d(512, 512, 3, 1, 1), # [512, 8, 8]
nn.BatchNorm2d(512),
nn.ReLU(),
nn.MaxPool2d(2, 2, 0) # [512, 4, 4]
)
self.fc = nn.Sequential(
nn.Linear(512 * 4 * 4, 1024),
nn.ReLU(),
nn.Linear(1024, 512),
nn.ReLU(),
nn.Linear(512, 11)
)
def forward(self, x):
out = self.cnn(x)
out = out.view(out.size()[0], -1)
return self.fc(out)
model = Classifier().cuda()
loss = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
num_epoch = 30
# traing is going
for epoch in range(num_epoch):
epoch_star_time = time.time()
train_acc = 0
train_loss = 0
val_acc = 0
val_loss = 0
model.train()
for i, data in enumerate(train_loader):
optimizer.zero_grad()
train_pred = model(data[0].cuda())
batch_loss = loss(train_pred, data[1].cuda())
batch_loss.backward()
optimizer.step()
train_acc += np.sum(np.argmax(train_pred.cpu().data.numpy(), axis=1) == data[1].numpy())
train_loss += batch_loss.item()
model.eval()
with torch.no_grad():
for i, data in enumerate(val_loader):
val_pred = model(data[0].cuda())
batch_loss = loss(val_pred, data[1].cuda())
val_acc += np.sum(np.argmax(val_pred.cpu().data.numpy(), axis=1) == data[1].numpy())
val_loss += batch_loss.item()
print("[%03d/%03d %2.2f sec(s) Train Acc: %3.6f Loss: %3.6f | Val Acc: %3.6f Loss: %3.6f"%\
(epoch+1,num_epoch,time.time()-epoch_star_time,\
train_acc/train_set.__len__(),train_loss/train_set.__len__(),\
val_acc/val_set.__len__(),val_loss/val_set.__len__()))
机器学习_李宏毅_作业三
最新推荐文章于 2024-09-03 17:37:40 发布