refactor: merge multiple project into one and create new project
This commit is contained in:
2
dl-exp/exp2/datasets/.gitignore
vendored
Normal file
2
dl-exp/exp2/datasets/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
# Ignore datasets file
|
||||
mnist.npz
|
||||
3
dl-exp/exp2/models/.gitignore
vendored
Normal file
3
dl-exp/exp2/models/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
# Ignore every saved model files
|
||||
*.pth
|
||||
*.ckpt
|
||||
80
dl-exp/exp2/modified/dataset.py
Normal file
80
dl-exp/exp2/modified/dataset.py
Normal file
@@ -0,0 +1,80 @@
|
||||
from pathlib import Path
|
||||
import numpy
|
||||
import torch
|
||||
from torch.utils.data import DataLoader, Dataset
|
||||
from torchvision.transforms import v2 as tvtrans
|
||||
import settings
|
||||
|
||||
class MnistDataset(Dataset):
|
||||
"""适配PyTorch的自定义Dataset,用于加载MNIST数据。"""
|
||||
|
||||
shape: int
|
||||
transform: tvtrans.Transform
|
||||
images_data: numpy.ndarray
|
||||
labels_data: torch.Tensor
|
||||
|
||||
def __init__(self, images: numpy.ndarray, labels: numpy.ndarray, transform: tvtrans.Transform):
|
||||
images_len: int = images.shape[0]
|
||||
labels_len: int = labels.shape[0]
|
||||
assert (images_len == labels_len)
|
||||
self.shape = images_len
|
||||
|
||||
self.images_data = images
|
||||
self.labels_data = torch.from_numpy(labels)
|
||||
self.transform = transform
|
||||
|
||||
def __getitem__(self, index):
|
||||
return self.transform(self.images_data[index]), self.labels_data[index]
|
||||
|
||||
def __len__(self):
|
||||
return self.shape
|
||||
|
||||
|
||||
class MnistDataLoaders:
|
||||
"""包含适配PyTorch的训练数据Loader和测试数据Loader的类。"""
|
||||
|
||||
train_loader: DataLoader
|
||||
test_loader: DataLoader
|
||||
|
||||
def __init__(self, batch_size: int):
|
||||
dataset = numpy.load(settings.MNIST_DATASET_PATH)
|
||||
|
||||
# 所有图片均为黑底白字
|
||||
# 6万张训练图片:60000x28x28。标签只有第一维。
|
||||
train_images: numpy.ndarray = dataset['x_train']
|
||||
train_labels: numpy.ndarray = dataset['y_train']
|
||||
# 1万张测试图片:10000x28x28。标签只有第一维。
|
||||
test_images: numpy.ndarray = dataset['x_test']
|
||||
test_labels: numpy.ndarray = dataset['y_test']
|
||||
|
||||
# 定义数据转换器
|
||||
trans = tvtrans.Compose([
|
||||
# 从uint8转换为float32并自动归一化到0-1区间
|
||||
# YYC MARK: 下面这个被标outdated了,换下面两个替代。
|
||||
# tvtrans.ToTensor(),
|
||||
tvtrans.ToImage(),
|
||||
tvtrans.ToDtype(torch.float32, scale=True),
|
||||
|
||||
# 为了符合后面图像的输入颜色通道条件,要在最后挤出一个新的维度
|
||||
# YYC MARK: 上面这两步已经帮我们自动挤出那个灰度通道了。
|
||||
# tvtrans.Lambda(lambda x: x.unsqueeze(-1))
|
||||
|
||||
# 这个特定的标准化参数 (0.1307, 0.3081) 是 MNIST 数据集的标准化参数,这些数值是MNIST训练集的全局均值和标准差。
|
||||
# 这种标准化有助于模型训练时的数值稳定性和收敛速度。
|
||||
# YYC MARK: 但我不想用,反正最后训练的也收敛。
|
||||
# tvtrans.Normalize((0.1307,), (0.3081,)),
|
||||
])
|
||||
|
||||
# 创建数据集
|
||||
train_dataset = MnistDataset(train_images, train_labels,
|
||||
transform=trans)
|
||||
test_dataset = MnistDataset(test_images, test_labels,
|
||||
transform=trans)
|
||||
|
||||
# 赋值到自身
|
||||
self.train_loader = DataLoader(dataset=train_dataset,
|
||||
batch_size=batch_size,
|
||||
shuffle=False)
|
||||
self.test_loader = DataLoader(dataset=test_dataset,
|
||||
batch_size=batch_size,
|
||||
shuffle=False)
|
||||
40
dl-exp/exp2/modified/model.py
Normal file
40
dl-exp/exp2/modified/model.py
Normal file
@@ -0,0 +1,40 @@
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
|
||||
class Cnn(torch.nn.Module):
|
||||
"""卷积神经网络模型"""
|
||||
|
||||
def __init__(self):
|
||||
super(Cnn, self).__init__()
|
||||
|
||||
self.conv1 = torch.nn.Conv2d(1, 32, kernel_size=(3, 3))
|
||||
self.pool1 = torch.nn.MaxPool2d(kernel_size=(2, 2))
|
||||
self.conv2 = torch.nn.Conv2d(32, 64, kernel_size=(3, 3))
|
||||
self.pool2 = torch.nn.MaxPool2d(kernel_size=(2, 2))
|
||||
self.conv3 = torch.nn.Conv2d(64, 64, kernel_size=(3, 3))
|
||||
self.flatten = torch.nn.Flatten()
|
||||
# 28x28过第一轮卷积后变为26x26,过第一轮池化后变为13x13
|
||||
# 过第二轮卷积后变为11x11,过第二轮池化后变为5x5
|
||||
# 过第三轮卷积后变为3x3。
|
||||
# 最后一轮卷积核个数为64。
|
||||
self.fc1 = torch.nn.Linear(64 * 3 * 3, 64)
|
||||
self.fc2 = torch.nn.Linear(64, 10)
|
||||
|
||||
def forward(self, x):
|
||||
x = F.relu(self.conv1(x))
|
||||
x = self.pool1(x)
|
||||
x = F.relu(self.conv2(x))
|
||||
x = self.pool2(x)
|
||||
x = F.relu(self.conv3(x))
|
||||
x = self.flatten(x)
|
||||
x = F.relu(self.fc1(x))
|
||||
x = self.fc2(x)
|
||||
# YYC MARK:
|
||||
# 绝对不要在这里用F.softmax(x, dim=1)输出!
|
||||
# 由于这些代码是从tensorflow里转换过来的,
|
||||
# tensorflow的loss function是接受possibility作为交叉熵计算的,
|
||||
# 而pytorch要求接受logits,即模型softmax之前的参数作为交叉熵计算。
|
||||
# 所以这里直接输出模型结果。
|
||||
return x
|
||||
|
||||
|
||||
135
dl-exp/exp2/modified/predict.py
Normal file
135
dl-exp/exp2/modified/predict.py
Normal file
@@ -0,0 +1,135 @@
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import numpy
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
from PIL import Image, ImageFile
|
||||
import matplotlib.pyplot as plt
|
||||
from model import Cnn
|
||||
import settings
|
||||
|
||||
sys.path.append(str(Path(__file__).resolve().parent.parent.parent))
|
||||
import gpu_utils
|
||||
|
||||
|
||||
class PredictResult:
|
||||
"""预测的结果"""
|
||||
|
||||
possibilities: torch.Tensor
|
||||
"""每个数字不同的概率"""
|
||||
|
||||
def __init__(self, possibilities: torch.Tensor):
|
||||
"""
|
||||
创建预测结果。
|
||||
|
||||
:param possibilities: 传入的tensor表示每个数字不同的概率,是经过softmax后的数值。
|
||||
其shape为二维。dim 0为batch,应当只有一维;dim 1为每个数字对应的概率。
|
||||
"""
|
||||
self.possibilities = possibilities
|
||||
|
||||
def chosen_number(self) -> int:
|
||||
"""
|
||||
获取最终选定的数字
|
||||
|
||||
:return: 以当前概率分布,推测的最终数字。
|
||||
"""
|
||||
# 输出出来是10个数字各自的可能性,所以要选取最高可能性的那个对比
|
||||
# 在dim=1上找最大的那个,就选那个。dim=0是批次所以不管他。
|
||||
return self.possibilities.argmax(1).item()
|
||||
|
||||
def number_possibilities(self) -> list[float]:
|
||||
"""
|
||||
获取每个数字出现的概率
|
||||
|
||||
:return: 返回一个具有10个元素的列表,列表的每一项表示当前index所代表数字的概率。
|
||||
"""
|
||||
return list(self.possibilities[0][i].item() for i in range(10))
|
||||
|
||||
class Predictor:
|
||||
device: torch.device
|
||||
model: Cnn
|
||||
|
||||
def __init__(self):
|
||||
self.device = gpu_utils.get_gpu_device()
|
||||
self.model = Cnn().to(self.device)
|
||||
|
||||
# 加载保存好的模型参数
|
||||
self.model.load_state_dict(torch.load(settings.SAVED_MODEL_PATH))
|
||||
|
||||
def __predict_tensor(self, in_data: torch.Tensor) -> PredictResult:
|
||||
"""
|
||||
其它预测函数都要使用的预测后端。其它预测函数将数据处理成Tensor,然后传递给此函数进行实际预测。
|
||||
|
||||
:param in_data: 传入的tensor,该tensor的shape必须是28x28,dtype为float32。
|
||||
:return: 预测结果。
|
||||
"""
|
||||
# 上传tensor到GPU
|
||||
in_data = in_data.to(self.device)
|
||||
# 为了满足要求,要在第一维度挤出2下
|
||||
# 一次是灰度通道,一次是批次。
|
||||
# 相当于batch size = 1的计算
|
||||
in_data = in_data.unsqueeze(0).unsqueeze(0)
|
||||
# 开始预测,由于模型输出的是没有softmax的数值,因此最后还需要softmax一下,
|
||||
with torch.no_grad():
|
||||
out_data = self.model(in_data)
|
||||
out_data = F.softmax(out_data, dim=-1)
|
||||
return PredictResult(out_data)
|
||||
|
||||
|
||||
def predict_sketchpad(self, image: list[list[bool]]) -> PredictResult:
|
||||
"""
|
||||
以sketchpad的数据进行预测。
|
||||
|
||||
:param image: 该列表的shape必须为28x28。
|
||||
:return: 预测结果。
|
||||
"""
|
||||
input = torch.tensor(image, dtype=torch.float32)
|
||||
assert(input.dim() == 2)
|
||||
assert(input.size(0) == 28)
|
||||
assert(input.size(1) == 28)
|
||||
|
||||
return self.__predict_tensor(input)
|
||||
|
||||
def predict_image(self, image: ImageFile.ImageFile) -> PredictResult:
|
||||
"""
|
||||
以Pillow图像的数据进行预测。
|
||||
|
||||
:param image: Pillow图像数据。该图像必须为28x28大小。
|
||||
:return: 预测结果。
|
||||
"""
|
||||
# 确保图像为灰度图像,以及宽高合适
|
||||
grayscale_image = image.convert('L')
|
||||
assert(grayscale_image.width == 28)
|
||||
assert(grayscale_image.height == 28)
|
||||
# 转换为numpy数组。注意这里的numpy数组是只读的,所以要先拷贝一份
|
||||
numpy_data = numpy.reshape(grayscale_image, (28, 28), copy=True)
|
||||
# 转换到Tensor,设置dtype
|
||||
data = torch.from_numpy(numpy_data).float()
|
||||
# 归一化到255,又因为图像输入是白底黑字,需要做转换。
|
||||
data.div_(255.0).sub_(1).mul_(-1)
|
||||
|
||||
return self.__predict_tensor(data)
|
||||
|
||||
def main():
|
||||
predictor = Predictor()
|
||||
|
||||
# 遍历测试目录中的所有图片,并处理。
|
||||
test_dir = Path(__file__).resolve().parent.parent / 'test_images'
|
||||
for image_path in test_dir.glob('*.png'):
|
||||
if image_path.is_file():
|
||||
print(f'Predicting {image_path} ...')
|
||||
image = Image.open(image_path)
|
||||
rv = predictor.predict_image(image)
|
||||
|
||||
print(f'Predict digit: {rv.chosen_number()}')
|
||||
plt.figure(f'Image - {image_path}')
|
||||
plt.imshow(image)
|
||||
plt.axis('on')
|
||||
plt.title(f'Predict digit: {rv.chosen_number()}')
|
||||
plt.show()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
gpu_utils.print_gpu_availability()
|
||||
main()
|
||||
|
||||
12
dl-exp/exp2/modified/settings.py
Normal file
12
dl-exp/exp2/modified/settings.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from pathlib import Path
|
||||
|
||||
MNIST_DATASET_PATH: Path = Path(__file__).resolve().parent.parent / 'datasets' / 'mnist.npz'
|
||||
"""MNIST数据集文件的路径"""
|
||||
|
||||
SAVED_MODEL_PATH: Path = Path(__file__).resolve().parent.parent / 'models' / 'cnn.pth'
|
||||
"""训练好的模型保存的位置"""
|
||||
|
||||
N_EPOCH: int = 5
|
||||
"""训练时的epoch次数"""
|
||||
N_BATCH_SIZE: int = 1000
|
||||
"""训练时的batch size"""
|
||||
190
dl-exp/exp2/modified/sketchpad.py
Normal file
190
dl-exp/exp2/modified/sketchpad.py
Normal file
@@ -0,0 +1,190 @@
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import typing
|
||||
import tkinter as tk
|
||||
from predict import PredictResult, Predictor
|
||||
|
||||
sys.path.append(str(Path(__file__).resolve().parent.parent.parent))
|
||||
import gpu_utils
|
||||
|
||||
|
||||
class SketchpadApp:
|
||||
IMAGE_HW: typing.ClassVar[int] = 28
|
||||
PIXEL_HW: typing.ClassVar[int] = 15
|
||||
|
||||
def __init__(self, root: tk.Tk, predictor: Predictor):
|
||||
self.root = root
|
||||
self.root.title("看图说数")
|
||||
|
||||
# 创建画板框架
|
||||
canvas_frame = tk.Frame(root)
|
||||
canvas_frame.pack(pady=10)
|
||||
# 创建图像大小的画板
|
||||
self.canvas_pixel_count = SketchpadApp.IMAGE_HW
|
||||
self.canvas_pixel_size = SketchpadApp.PIXEL_HW # 每个像素的大小
|
||||
canvas_hw = self.canvas_pixel_count * self.canvas_pixel_size
|
||||
self.canvas_width = canvas_hw
|
||||
self.canvas_height = canvas_hw
|
||||
self.canvas = tk.Canvas(
|
||||
canvas_frame,
|
||||
width=self.canvas_width,
|
||||
height=self.canvas_height,
|
||||
bg='black'
|
||||
)
|
||||
self.canvas.pack()
|
||||
# 存储画板状态。False表示没有画(黑色),True表示画了(白色)。
|
||||
self.canvas_data = [[False for _ in range(self.canvas_pixel_count)] for _ in range(self.canvas_pixel_count)]
|
||||
# 绑定鼠标事件
|
||||
self.canvas.bind("<B1-Motion>", self.paint)
|
||||
self.canvas.bind("<Button-1>", self.paint)
|
||||
# 绘制初始网格
|
||||
self.draw_grid()
|
||||
|
||||
# 创建表格框架
|
||||
table_frame = tk.Frame(root)
|
||||
table_frame.pack(pady=10)
|
||||
# 表头数据
|
||||
header_words = ("猜测的数字", ) + tuple(f'为{i}的概率' for i in range(10))
|
||||
# 创建表头
|
||||
for col, header in enumerate(header_words):
|
||||
header_label = tk.Label(
|
||||
table_frame,
|
||||
text=header,
|
||||
relief="solid",
|
||||
borderwidth=1,
|
||||
width=12,
|
||||
height=2,
|
||||
bg="lightblue"
|
||||
)
|
||||
header_label.grid(row=0, column=col, sticky="nsew")
|
||||
# 创建第二行(显示数值的行)
|
||||
self.value_labels = []
|
||||
for col in range(len(header_words)):
|
||||
value_label = tk.Label(
|
||||
table_frame,
|
||||
text="0.00", # 默认显示0.00
|
||||
relief="solid",
|
||||
borderwidth=1,
|
||||
width=12,
|
||||
height=2,
|
||||
bg="white"
|
||||
)
|
||||
value_label.grid(row=1, column=col, sticky="nsew")
|
||||
self.value_labels.append(value_label)
|
||||
# 设置第一列的特殊样式(猜测的数字)
|
||||
self.value_labels[0].config(text="N/A", bg="lightyellow")
|
||||
# 清空样式
|
||||
self.clear_table()
|
||||
|
||||
# 创建按钮框架
|
||||
button_frame = tk.Frame(root)
|
||||
button_frame.pack(pady=10)
|
||||
# 执行按钮
|
||||
execute_button = tk.Button(
|
||||
button_frame,
|
||||
text="执行",
|
||||
command=self.execute,
|
||||
bg='lightgreen',
|
||||
width=10
|
||||
)
|
||||
execute_button.pack(side=tk.LEFT, padx=5)
|
||||
# 重置按钮
|
||||
reset_button = tk.Button(
|
||||
button_frame,
|
||||
text="重置",
|
||||
command=self.reset,
|
||||
bg='lightcoral',
|
||||
width=10
|
||||
)
|
||||
reset_button.pack(side=tk.LEFT, padx=5)
|
||||
# 设置用于执行的predictor
|
||||
self.predictor = predictor
|
||||
|
||||
# region: 画板部分
|
||||
|
||||
canvas: tk.Canvas
|
||||
canvas_data: list[list[bool]]
|
||||
canvas_width: int
|
||||
canvas_height: int
|
||||
|
||||
def draw_grid(self):
|
||||
"""绘制网格线"""
|
||||
for i in range(self.canvas_pixel_count + 1):
|
||||
# 垂直线
|
||||
self.canvas.create_line(
|
||||
i * self.canvas_pixel_size, 0,
|
||||
i * self.canvas_pixel_size, self.canvas_height,
|
||||
fill='lightgray'
|
||||
)
|
||||
# 水平线
|
||||
self.canvas.create_line(
|
||||
0, i * self.canvas_pixel_size,
|
||||
self.canvas_width, i * self.canvas_pixel_size,
|
||||
fill='lightgray'
|
||||
)
|
||||
|
||||
def paint(self, event):
|
||||
"""处理鼠标绘制事件"""
|
||||
# 计算点击的网格坐标
|
||||
col = event.x // self.canvas_pixel_size
|
||||
row = event.y // self.canvas_pixel_size
|
||||
|
||||
# 确保坐标在有效范围内
|
||||
if 0 <= col < self.canvas_pixel_count and 0 <= row < self.canvas_pixel_count:
|
||||
# 更新网格状态
|
||||
if self.canvas_data[row][col] != True:
|
||||
self.canvas_data[row][col] = True
|
||||
|
||||
# 绘制黑色矩形
|
||||
x1 = col * self.canvas_pixel_size
|
||||
y1 = row * self.canvas_pixel_size
|
||||
x2 = x1 + self.canvas_pixel_size
|
||||
y2 = y1 + self.canvas_pixel_size
|
||||
|
||||
self.canvas.create_rectangle(x1, y1, x2, y2, fill='white', outline='')
|
||||
|
||||
# endregion
|
||||
|
||||
# region: 表格部分
|
||||
|
||||
value_labels: list[tk.Label]
|
||||
|
||||
def show_in_table(self, result: PredictResult):
|
||||
self.value_labels[0].config(text=str(result.chosen_number()))
|
||||
|
||||
number_possibilities = result.number_possibilities()
|
||||
for index, label in enumerate(self.value_labels[1:]):
|
||||
label.config(text=f'{number_possibilities[index]:.4f}')
|
||||
|
||||
def clear_table(self):
|
||||
for label in self.value_labels:
|
||||
label.config(text='N/A')
|
||||
|
||||
# endregion
|
||||
|
||||
# region: 按钮部分
|
||||
|
||||
predictor: Predictor
|
||||
|
||||
def execute(self):
|
||||
"""执行按钮功能 - 将画板数据传递给后端"""
|
||||
prediction = self.predictor.predict_sketchpad(self.canvas_data)
|
||||
self.show_in_table(prediction)
|
||||
|
||||
def reset(self):
|
||||
"""重置按钮功能 - 清空画板"""
|
||||
self.canvas.delete("all")
|
||||
self.canvas_data = [[0 for _ in range(self.canvas_pixel_count)] for _ in range(self.canvas_pixel_count)]
|
||||
self.draw_grid()
|
||||
self.clear_table()
|
||||
|
||||
# endregion
|
||||
|
||||
if __name__ == "__main__":
|
||||
gpu_utils.print_gpu_availability()
|
||||
|
||||
predictor = Predictor()
|
||||
|
||||
root = tk.Tk()
|
||||
app = SketchpadApp(root, predictor)
|
||||
root.mainloop()
|
||||
85
dl-exp/exp2/modified/train.py
Normal file
85
dl-exp/exp2/modified/train.py
Normal file
@@ -0,0 +1,85 @@
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import typing
|
||||
import torch
|
||||
import torchinfo
|
||||
import ignite.engine
|
||||
import ignite.metrics
|
||||
from ignite.engine import Engine, Events
|
||||
from ignite.handlers.tqdm_logger import ProgressBar
|
||||
from dataset import MnistDataLoaders
|
||||
from model import Cnn
|
||||
import settings
|
||||
|
||||
sys.path.append(str(Path(__file__).resolve().parent.parent.parent))
|
||||
import gpu_utils
|
||||
|
||||
|
||||
class Trainer:
|
||||
"""核心训练器"""
|
||||
|
||||
device: torch.device
|
||||
data_source: MnistDataLoaders
|
||||
model: Cnn
|
||||
|
||||
trainer: Engine
|
||||
evaluator: Engine
|
||||
pbar: ProgressBar
|
||||
|
||||
def __init__(self):
|
||||
# 创建训练设备,模型和数据加载器。
|
||||
self.device = gpu_utils.get_gpu_device()
|
||||
self.model = Cnn().to(self.device)
|
||||
self.data_source = MnistDataLoaders(batch_size=settings.N_BATCH_SIZE)
|
||||
# 展示模型结构。批次为指定批次数量,通道只有一个灰度通道,大小28x28。
|
||||
torchinfo.summary(self.model, (settings.N_BATCH_SIZE, 1, 28, 28))
|
||||
|
||||
# 优化器和损失函数
|
||||
optimizer = torch.optim.Adam(self.model.parameters(), eps=1e-7)
|
||||
criterion = torch.nn.CrossEntropyLoss()
|
||||
# 创建训练器
|
||||
self.trainer = ignite.engine.create_supervised_trainer(
|
||||
self.model, optimizer, criterion, self.device)
|
||||
# 将训练器关联到进度条
|
||||
self.pbar = ProgressBar(persist=True)
|
||||
self.pbar.attach(self.trainer, output_transform=lambda loss: {"loss": loss})
|
||||
|
||||
# 创建测试的评估器的评估量
|
||||
evaluator_metrics = {
|
||||
# 这个Accuracy要的是logits,而不是possibilities,
|
||||
# 所以依然是不需要softmax处理后的结果。
|
||||
"accuracy": ignite.metrics.Accuracy(device=self.device),
|
||||
"loss": ignite.metrics.Loss(criterion, device=self.device)
|
||||
}
|
||||
# 创建测试评估器
|
||||
self.evaluator = ignite.engine.create_supervised_evaluator(
|
||||
self.model, metrics=evaluator_metrics, device=self.device)
|
||||
|
||||
def train_model(self):
|
||||
# 训练模型
|
||||
self.trainer.run(self.data_source.train_loader, max_epochs=settings.N_EPOCH)
|
||||
|
||||
def save_model(self):
|
||||
# 确保保存模型的文件夹存在。
|
||||
settings.SAVED_MODEL_PATH.parent.mkdir(parents=True, exist_ok=True)
|
||||
# 仅保存模型参数
|
||||
torch.save(self.model.state_dict(), settings.SAVED_MODEL_PATH)
|
||||
print(f'Model was saved into: {settings.SAVED_MODEL_PATH}')
|
||||
|
||||
def test_model(self):
|
||||
# 测试模型并输出结果
|
||||
self.evaluator.run(self.data_source.test_loader)
|
||||
metrics = self.evaluator.state.metrics
|
||||
print(f"Accuracy: {metrics['accuracy']:.4f} Loss: {metrics['loss']:.4f}")
|
||||
|
||||
|
||||
def main():
|
||||
trainer = Trainer()
|
||||
trainer.train_model()
|
||||
trainer.save_model()
|
||||
trainer.test_model()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
gpu_utils.print_gpu_availability()
|
||||
main()
|
||||
40
dl-exp/exp2/source/predict.py
Normal file
40
dl-exp/exp2/source/predict.py
Normal file
@@ -0,0 +1,40 @@
|
||||
from pathlib import Path
|
||||
import tensorflow as tf
|
||||
from PIL import Image
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
from train import CNN
|
||||
|
||||
class Predict(object):
|
||||
def __init__(self):
|
||||
latest = tf.train.latest_checkpoint('./ckpt')
|
||||
self.cnn = CNN()
|
||||
# 恢复网络权重
|
||||
self.cnn.model.load_weights(latest)
|
||||
|
||||
def predict(self, image_path):
|
||||
# 以黑白方式读取图片
|
||||
img = Image.open(image_path).convert('L')
|
||||
img = np.reshape(img, (28, 28, 1)) / 255.
|
||||
x = np.array([1 - img])
|
||||
y = self.cnn.model.predict(x)
|
||||
|
||||
# 因为x只传入了一张图片,取y[0]即可
|
||||
# np.argmax()取得最大值的下标,即代表的数字
|
||||
print(image_path)
|
||||
# print(y[0])
|
||||
print(' -> Predict digit', np.argmax(y[0]))
|
||||
plt.figure("Image") # 图像窗口名称
|
||||
plt.imshow(img)
|
||||
plt.axis('on') # 关掉坐标轴为 off
|
||||
plt.title(np.argmax(y[0])) # 图像题目 # 必须有这个,要不然无法显示
|
||||
plt.show()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = Predict()
|
||||
images_dir = Path(__file__).resolve().parent.parent / 'test_images'
|
||||
app.predict(images_dir / '0.png')
|
||||
app.predict(images_dir / '1.png')
|
||||
app.predict(images_dir / '4.png')
|
||||
56
dl-exp/exp2/source/train.py
Normal file
56
dl-exp/exp2/source/train.py
Normal file
@@ -0,0 +1,56 @@
|
||||
from pathlib import Path
|
||||
import tensorflow as tf
|
||||
from tensor.keras import datasets, layers, models
|
||||
|
||||
class CNN(object):
|
||||
def __init__(self):
|
||||
model = models.Sequential()
|
||||
# 第1层卷积,卷积核大小为3*3,32个,28*28为待训练图片的大小
|
||||
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
|
||||
model.add(layers.MaxPooling2D((2, 2)))
|
||||
# 第2层卷积,卷积核大小为3*3,64个
|
||||
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
|
||||
model.add(layers.MaxPooling2D((2, 2)))
|
||||
# 第三层卷积,卷积核大小为3*3,64个
|
||||
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
|
||||
model.add(layers.Flatten())
|
||||
model.add(layers.Dense(64, activation='relu'))
|
||||
model.add(layers.Dense(10, activation='softmax'))
|
||||
model.summary()
|
||||
self.model = model
|
||||
|
||||
class DataSource(object):
|
||||
def __init__(self):
|
||||
# mnist数据集存储的位置,如何不存在将自动下载
|
||||
data_path = Path(__file__).resolve().parent.parent / 'datasets' / 'mnist.npz'
|
||||
(train_images, train_labels), (test_images,
|
||||
test_labels) = datasets.mnist.load_data(path=data_path)
|
||||
# 6万张训练图片,1万张测试图片
|
||||
train_images = train_images.reshape((60000, 28, 28, 1))
|
||||
test_images = test_images.reshape((10000, 28, 28, 1))
|
||||
# 像素值映射到 0 - 1 之间
|
||||
train_images, test_images = train_images / 255.0, test_images / 255.0
|
||||
self.train_images, self.train_labels = train_images, train_labels
|
||||
self.test_images, self.test_labels = test_images, test_labels
|
||||
|
||||
class Train:
|
||||
def __init__(self):
|
||||
self.cnn = CNN()
|
||||
self.data = DataSource()
|
||||
def train(self):
|
||||
check_path = Path(__file__).resolve().parent.parent / 'models' / 'cnn.ckpt'
|
||||
# period 每隔5epoch保存一次
|
||||
save_model_cb = tf.keras.callbacks.ModelCheckpoint(
|
||||
str(check_path), save_weights_only=True, verbose=1, period=5)
|
||||
self.cnn.model.compile(optimizer='adam',
|
||||
loss='sparse_categorical_crossentropy',
|
||||
metrics=['accuracy'])
|
||||
self.cnn.model.fit(self.data.train_images, self.data.train_labels,
|
||||
epochs=5, batch_size=1000, callbacks=[save_model_cb])
|
||||
test_loss, test_acc = self.cnn.model.evaluate(
|
||||
self.data.test_images, self.data.test_labels)
|
||||
print("准确率: %.4f, 共测试了%d张图片 " % (test_acc, len(self.data.test_labels)))
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = Train()
|
||||
app.train()
|
||||
2
dl-exp/exp2/test_images/.gitignore
vendored
Normal file
2
dl-exp/exp2/test_images/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
# Ignore all test images
|
||||
*.png
|
||||
Reference in New Issue
Block a user