hjc-abc 1 week ago
parent 89bcf750a1
commit bd36728c06

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 94 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 868 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 109 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 69 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 101 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 128 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 176 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 86 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.3 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.3 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

@ -0,0 +1 @@
<table><tr><th>잚깎</th><th>零斤똑</th></tr><tr><td>瑨叉</td><td>95.4335</td></tr><tr><td>鬼뒈일빪</td><td>1.9126</td></tr><tr><td>圖쵠측</td><td>1.2005</td></tr><tr><td>겜懃빻쏜밴</td><td>1.0906</td></tr><tr><td>댕뒈일빪</td><td>0.2073</td></tr><tr><td>뼝뒈일빪</td><td>0.0621</td></tr><tr><td>譏녔</td><td>0.0485</td></tr><tr><td>쏜濾녔</td><td>0.0193</td></tr><tr><td>庚療측</td><td>0.0087</td></tr><tr><td>迭直</td><td>0.0066</td></tr></table>

Binary file not shown.

After

Width:  |  Height:  |  Size: 897 KiB

File diff suppressed because one or more lines are too long

@ -0,0 +1,160 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "4a36abc9-47b6-4e9a-8d2c-330e64012db2",
"metadata": {},
"source": [
"# 准备图像分类数据集\n",
"\n",
"同济子豪兄https://space.bilibili.com/1900783\n",
"\n",
"[代码运行云GPU环境](https://featurize.cn/?s=d7ce99f842414bfcaea5662a97581bd1)GPU RTX 3060、CUDA v11.2\n",
"\n",
"## 构建自己的图像分类数据集\n",
"\n",
"https://www.bilibili.com/video/BV1Jd4y1T7rw"
]
},
{
"cell_type": "markdown",
"id": "e0f48f3b-5137-4b11-a529-4ae41c8806e4",
"metadata": {},
"source": [
"## 下载样例数据集"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "70a97135-b7a8-4817-a43d-742171ac5978",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"--2023-03-26 17:13:25-- https://zihao-openmmlab.obs.cn-east-3.myhuaweicloud.com/20220716-mmclassification/dataset/fruit30/fruit30_split.zip\n",
"正在连接 172.16.0.13:5848... 已连接。\n",
"已发出 Proxy 请求,正在等待回应... 200 OK\n",
"长度: 226278151 (216M) [application/zip]\n",
"正在保存至: “fruit30_split.zip”\n",
"\n",
"fruit30_split.zip 100%[===================>] 215.79M 46.1MB/s 用时 4.9s \n",
"\n",
"2023-03-26 17:13:30 (43.9 MB/s) - 已保存 “fruit30_split.zip” [226278151/226278151])\n",
"\n"
]
}
],
"source": [
"# 下载数据集压缩包\n",
"!wget https://zihao-openmmlab.obs.cn-east-3.myhuaweicloud.com/20220716-mmclassification/dataset/fruit30/fruit30_split.zip"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "21711cbb-685d-40ff-9ec9-2348d8d1a1a1",
"metadata": {},
"outputs": [],
"source": [
"# 解压\n",
"!unzip fruit30_split.zip >> /dev/null"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b9324762-352f-4b9c-b9e8-2b299c0ef2fc",
"metadata": {},
"outputs": [],
"source": [
"# 删除压缩包\n",
"!rm fruit30_split.zip"
]
},
{
"cell_type": "markdown",
"id": "55155fe0-2d99-458e-bdb2-ac894d7a6046",
"metadata": {},
"source": [
"## 查看数据集目录结构"
]
},
{
"cell_type": "code",
"id": "f06d00df-aa4f-41fe-a2be-50a677bb5a3f",
"metadata": {
"ExecuteTime": {
"end_time": "2024-10-12T01:01:43.528374Z",
"start_time": "2024-10-12T01:01:43.488297Z"
}
},
"source": [
"!sudo snap install tree"
],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"'sudo' 不是内部或外部命令,也不是可运行的程序\n",
"或批处理文件。\n"
]
}
],
"execution_count": 1
},
{
"cell_type": "code",
"id": "8f9f463f-ea2e-4d7b-b7b2-9f528afe987c",
"metadata": {
"ExecuteTime": {
"end_time": "2024-10-12T01:01:45.086148Z",
"start_time": "2024-10-12T01:01:45.035422Z"
}
},
"source": "!tree dataset -L 2",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"参数太多 - -L\n"
]
}
],
"execution_count": 2
},
{
"cell_type": "code",
"execution_count": null,
"id": "139d2504-a78a-4145-8520-5206fb51b829",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -0,0 +1,93 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>图像分类工具 - 结果页面</title>
<style>
body {
font-family: "SimHei", sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
background-color: #f6f6f6;
}
h1 {
font-size: 50px; /* 标题放大 */
}
button {
margin: 25px;
padding: 25px 45px;
font-size: 20px; /* 按钮文字放大 */
}
#result {
margin-top: 30px;
font-size: 24px; /* 结果文字放大 */
}
/* 样式用于显示后端返回的 HTML 结果 */
#safe-result {
margin-top: 30px;
font-size: 24px; /* 结果文字放大 */
white-space: pre-line; /* 保持换行 */
}
</style>
</head>
<body>
<h1>图像分类工具</h1>
<input type="file" id="imageInput" accept="image/*" />
<button onclick="analyzeImage()">获取分析结果</button>
<div id="result"></div>
<!-- 结果页面中用于显示后端返回的 HTML 内容 -->
{% if result %}
<div id="safe-result">
<!-- 使用 |safe 过滤器来确保 HTML 标签不被转义 -->
{{ result|safe }}
</div>
{% endif %}
<script>
async function analyzeImage() {
const input = document.getElementById('imageInput');
if (!input.files[0]) {
alert("请先插入一张图片。");
return;
}
// 读取图像
const file = input.files[0];
const img = new Image();
img.src = URL.createObjectURL(file);
await img.decode();
// 此处应调用后端接口进行预测
// 示例:假设有一个 API '/api/predict' 返回预测结果
const formData = new FormData();
formData.append("image", file);
try {
const response = await fetch('/api/predict', {
method: 'POST',
body: formData
});
if (!response.ok) {
throw new Error("网络错误");
}
const result = await response.json();
document.getElementById('result').innerText = `具有最大置信度的类别名称:${result.label}`;
} catch (error) {
alert("分析失败:" + error.message);
}
}
</script>
</body>
</html>

@ -0,0 +1,109 @@
import torch
import torch.nn.functional as F
import torchvision.transforms as transforms
from PIL import Image
import tkinter as tk
from tkinter import filedialog, messagebox
import numpy as np
# 有 GPU 就用 GPU没有就用 CPU
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
# 载入模型
model_path = 'checkpoint/dataset_pytorch_C1.pth' # 确保路径正确
try:
model = torch.load(model_path)
model = model.eval().to(device)
except FileNotFoundError:
print(f"错误:无法找到模型文件 '{model_path}'")
exit()
# 测试集图像预处理-RCTN缩放、裁剪、转 Tensor、归一化
test_transform = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
# 类别映射
idx_to_labels = np.load('idx_to_labels.npy', allow_pickle=True).item()
# 全局变量保存图片和分析结果
img_path = None
pred_result = None
def insert_image():
global img_path
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename(
title="选择一张图片",
filetypes=[("Image files", "*.jpg *.jpeg *.png *.bmp *.gif")]
)
if file_path:
img_path = file_path
messagebox.showinfo("成功", "图片插入成功!")
else:
messagebox.showwarning("警告", "未选择任何图片。")
def analyze_image():
global img_path, pred_result
if img_path is None:
messagebox.showwarning("警告", "请先插入一张图片。")
return
# 打开并预处理图像
img_pil = Image.open(img_path)
input_img = test_transform(img_pil).unsqueeze(0).to(device)
# 前向预测
with torch.no_grad():
pred_logits = model(input_img)
# 计算 softmax
pred_softmax = F.softmax(pred_logits, dim=1)
# 找到置信度最大的前 n 个结果
n = 10
top_n = torch.topk(pred_softmax, n)
pred_ids = top_n[1].cpu().numpy().squeeze()
confs = top_n[0].cpu().numpy().squeeze()
# 找到最大置信度的类别名称
max_conf_index = np.argmax(confs)
max_conf_class_name = idx_to_labels[pred_ids[max_conf_index]]
# 保存结果
pred_result = f"具有最大置信度的类别名称:{max_conf_class_name}"
# 打开分析结果窗口
analysis_window = tk.Toplevel()
analysis_window.title("分析结果")
analysis_window.geometry("400x300")
result_label = tk.Label(analysis_window, text=pred_result, font=("SimHei", 12), wraplength=350, justify="left")
result_label.grid(row=0, column=0, padx=20, pady=20)
# 使用 grid() 将按钮放在右下角
ok_button = tk.Button(analysis_window, text="确定", command=analysis_window.destroy)
ok_button.grid(row=1, column=0, sticky="ew", padx=20, pady=10) # sticky="ew" 使按钮占据整行
# 创建主窗口
main_window = tk.Tk()
main_window.title("图像分类工具")
main_window.geometry("300x200")
# 创建按钮
insert_button = tk.Button(main_window, text="插入图片", command=insert_image)
insert_button.pack(pady=20)
analyze_button = tk.Button(main_window, text="获取分析结果", command=analyze_image)
analyze_button.pack(pady=20)
# 运行主循环
main_window.mainloop()

@ -0,0 +1,31 @@
epoch,test_loss,test_accuracy,test_precision,test_recall,test_f1-score
1,2.0930123,0.38472418670438474,0.1691631623197644,0.21857142857142858,0.18289168026241523
2,1.9176342,0.4045261669024045,0.3099095071675483,0.2430026455026455,0.22423302592957986
3,1.8613107,0.4144271570014144,0.27610813015358454,0.25837301587301587,0.24874843872971764
4,1.8651094,0.4214992927864215,0.33039935968860973,0.2672883597883598,0.24876040306538025
5,1.8502045,0.4314002828854314,0.31955499557865813,0.2849338624338625,0.26468437342725254
6,1.8187857,0.4328147100424328,0.3252951988957093,0.28615079365079366,0.28332304309531886
7,1.7824266,0.43705799151343705,0.3353262740983161,0.28779100529100526,0.27932666603867806
8,1.8227451,0.4314002828854314,0.29637810366383366,0.2860846560846561,0.26648188873060297
9,1.7772177,0.44271570014144274,0.3099393993772887,0.29665343915343917,0.2819555068621611
10,1.7979189,0.4229137199434229,0.2975161990832252,0.27507936507936515,0.2707682859710133
11,1.7620848,0.43988684582743987,0.30952029558829136,0.29451058201058206,0.29220656348971
12,1.7911305,0.43705799151343705,0.2909793367263787,0.2820767195767196,0.2727105710265571
13,1.7775599,0.44413012729844414,0.318483538956524,0.3021560846560846,0.2972421185421302
14,1.7817583,0.44130127298444133,0.3157370165572269,0.2934391534391535,0.2909034392666732
15,1.7718116,0.44271570014144274,0.29943831676843574,0.2972222222222222,0.2797717733879937
16,1.7626699,0.44271570014144274,0.29926954558959684,0.2923677248677249,0.2895974527762101
17,1.7618271,0.4328147100424328,0.2856043777861611,0.287010582010582,0.27674578999814975
18,1.7683886,0.4314002828854314,0.2833496937467535,0.2780820105820106,0.276456813860324
19,1.7561138,0.44695898161244696,0.3167865597171075,0.3057275132275132,0.3033790826352277
20,1.7712598,0.43705799151343705,0.29400557788923193,0.2843650793650794,0.28214464901662517
21,1.767193,0.43422913719943423,0.28206519175059364,0.28165343915343916,0.276932009517325
22,1.7524676,0.44554455445544555,0.3062301152163019,0.30251322751322757,0.2975329765478318
23,1.7697207,0.42998585572843,0.28522484656004016,0.2762962962962963,0.27404965397289627
24,1.7572181,0.42998585572843,0.2846096769765771,0.282010582010582,0.2758498613083168
25,1.7555789,0.4328147100424328,0.2902450766924691,0.2847222222222222,0.27955444217403624
26,1.7539029,0.42998585572843,0.28390766067150264,0.2768650793650793,0.27517516228842065
27,1.7535378,0.44130127298444133,0.29291407487928234,0.29400793650793655,0.287903699063399
28,1.7552626,0.43847241867043846,0.29508124173100647,0.29215608465608467,0.2885014907300926
29,1.7594873,0.42998585572843,0.2830196009563221,0.27772486772486765,0.27459399295593495
30,1.7565756,0.42998585572843,0.28064225400986786,0.2828703703703704,0.27678367056270065
1 epoch test_loss test_accuracy test_precision test_recall test_f1-score
2 1 2.0930123 0.38472418670438474 0.1691631623197644 0.21857142857142858 0.18289168026241523
3 2 1.9176342 0.4045261669024045 0.3099095071675483 0.2430026455026455 0.22423302592957986
4 3 1.8613107 0.4144271570014144 0.27610813015358454 0.25837301587301587 0.24874843872971764
5 4 1.8651094 0.4214992927864215 0.33039935968860973 0.2672883597883598 0.24876040306538025
6 5 1.8502045 0.4314002828854314 0.31955499557865813 0.2849338624338625 0.26468437342725254
7 6 1.8187857 0.4328147100424328 0.3252951988957093 0.28615079365079366 0.28332304309531886
8 7 1.7824266 0.43705799151343705 0.3353262740983161 0.28779100529100526 0.27932666603867806
9 8 1.8227451 0.4314002828854314 0.29637810366383366 0.2860846560846561 0.26648188873060297
10 9 1.7772177 0.44271570014144274 0.3099393993772887 0.29665343915343917 0.2819555068621611
11 10 1.7979189 0.4229137199434229 0.2975161990832252 0.27507936507936515 0.2707682859710133
12 11 1.7620848 0.43988684582743987 0.30952029558829136 0.29451058201058206 0.29220656348971
13 12 1.7911305 0.43705799151343705 0.2909793367263787 0.2820767195767196 0.2727105710265571
14 13 1.7775599 0.44413012729844414 0.318483538956524 0.3021560846560846 0.2972421185421302
15 14 1.7817583 0.44130127298444133 0.3157370165572269 0.2934391534391535 0.2909034392666732
16 15 1.7718116 0.44271570014144274 0.29943831676843574 0.2972222222222222 0.2797717733879937
17 16 1.7626699 0.44271570014144274 0.29926954558959684 0.2923677248677249 0.2895974527762101
18 17 1.7618271 0.4328147100424328 0.2856043777861611 0.287010582010582 0.27674578999814975
19 18 1.7683886 0.4314002828854314 0.2833496937467535 0.2780820105820106 0.276456813860324
20 19 1.7561138 0.44695898161244696 0.3167865597171075 0.3057275132275132 0.3033790826352277
21 20 1.7712598 0.43705799151343705 0.29400557788923193 0.2843650793650794 0.28214464901662517
22 21 1.767193 0.43422913719943423 0.28206519175059364 0.28165343915343916 0.276932009517325
23 22 1.7524676 0.44554455445544555 0.3062301152163019 0.30251322751322757 0.2975329765478318
24 23 1.7697207 0.42998585572843 0.28522484656004016 0.2762962962962963 0.27404965397289627
25 24 1.7572181 0.42998585572843 0.2846096769765771 0.282010582010582 0.2758498613083168
26 25 1.7555789 0.4328147100424328 0.2902450766924691 0.2847222222222222 0.27955444217403624
27 26 1.7539029 0.42998585572843 0.28390766067150264 0.2768650793650793 0.27517516228842065
28 27 1.7535378 0.44130127298444133 0.29291407487928234 0.29400793650793655 0.287903699063399
29 28 1.7552626 0.43847241867043846 0.29508124173100647 0.29215608465608467 0.2885014907300926
30 29 1.7594873 0.42998585572843 0.2830196009563221 0.27772486772486765 0.27459399295593495
31 30 1.7565756 0.42998585572843 0.28064225400986786 0.2828703703703704 0.27678367056270065

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save