532 lines
18 KiB
HTML
532 lines
18 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>自定义图像分类器 - TensorFlow.js</title>
|
|
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"></script>
|
|
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/mobilenet@latest"></script>
|
|
<style>
|
|
* {
|
|
margin: 0;
|
|
padding: 0;
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
body {
|
|
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
min-height: 100vh;
|
|
padding: 20px;
|
|
}
|
|
|
|
.main-container {
|
|
max-width: 1400px;
|
|
margin: 0 auto;
|
|
}
|
|
|
|
h1 {
|
|
color: white;
|
|
text-align: center;
|
|
margin-bottom: 30px;
|
|
font-size: 2.5em;
|
|
text-shadow: 2px 2px 4px rgba(0,0,0,0.2);
|
|
}
|
|
|
|
.grid-container {
|
|
display: grid;
|
|
grid-template-columns: 1fr 1fr;
|
|
gap: 20px;
|
|
margin-bottom: 20px;
|
|
}
|
|
|
|
@media (max-width: 768px) {
|
|
.grid-container {
|
|
grid-template-columns: 1fr;
|
|
}
|
|
}
|
|
|
|
.card {
|
|
background: white;
|
|
border-radius: 15px;
|
|
padding: 25px;
|
|
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
|
|
}
|
|
|
|
.card h2 {
|
|
color: #333;
|
|
margin-bottom: 20px;
|
|
border-bottom: 2px solid #667eea;
|
|
padding-bottom: 10px;
|
|
}
|
|
|
|
.class-input {
|
|
margin-bottom: 20px;
|
|
padding: 15px;
|
|
background: #f8f9fa;
|
|
border-radius: 10px;
|
|
}
|
|
|
|
.class-input h3 {
|
|
color: #555;
|
|
margin-bottom: 10px;
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 10px;
|
|
}
|
|
|
|
.class-number {
|
|
background: #667eea;
|
|
color: white;
|
|
width: 25px;
|
|
height: 25px;
|
|
border-radius: 50%;
|
|
display: inline-flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
font-size: 14px;
|
|
}
|
|
|
|
input[type="text"] {
|
|
width: 100%;
|
|
padding: 10px;
|
|
border: 2px solid #e0e0e0;
|
|
border-radius: 5px;
|
|
margin-bottom: 10px;
|
|
font-size: 16px;
|
|
transition: border-color 0.3s;
|
|
}
|
|
|
|
input[type="text"]:focus {
|
|
outline: none;
|
|
border-color: #667eea;
|
|
}
|
|
|
|
input[type="file"] {
|
|
display: none;
|
|
}
|
|
|
|
.file-label {
|
|
display: inline-block;
|
|
padding: 10px 20px;
|
|
background: #667eea;
|
|
color: white;
|
|
border-radius: 5px;
|
|
cursor: pointer;
|
|
transition: background 0.3s;
|
|
margin-right: 10px;
|
|
}
|
|
|
|
.file-label:hover {
|
|
background: #5a67d8;
|
|
}
|
|
|
|
.image-preview {
|
|
display: flex;
|
|
flex-wrap: wrap;
|
|
gap: 10px;
|
|
margin-top: 10px;
|
|
max-height: 150px;
|
|
overflow-y: auto;
|
|
}
|
|
|
|
.preview-img {
|
|
width: 60px;
|
|
height: 60px;
|
|
object-fit: cover;
|
|
border-radius: 5px;
|
|
border: 2px solid #e0e0e0;
|
|
}
|
|
|
|
.btn {
|
|
padding: 12px 30px;
|
|
border: none;
|
|
border-radius: 5px;
|
|
font-size: 16px;
|
|
cursor: pointer;
|
|
transition: all 0.3s;
|
|
margin: 5px;
|
|
}
|
|
|
|
.btn-primary {
|
|
background: #667eea;
|
|
color: white;
|
|
}
|
|
|
|
.btn-primary:hover {
|
|
background: #5a67d8;
|
|
transform: translateY(-2px);
|
|
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
|
|
}
|
|
|
|
.btn-success {
|
|
background: #48bb78;
|
|
color: white;
|
|
}
|
|
|
|
.btn-success:hover {
|
|
background: #38a169;
|
|
}
|
|
|
|
.btn-danger {
|
|
background: #f56565;
|
|
color: white;
|
|
}
|
|
|
|
.btn-danger:hover {
|
|
background: #e53e3e;
|
|
}
|
|
|
|
.btn:disabled {
|
|
background: #cbd5e0;
|
|
cursor: not-allowed;
|
|
transform: none;
|
|
}
|
|
|
|
#webcam-container {
|
|
position: relative;
|
|
width: 100%;
|
|
max-width: 640px;
|
|
margin: 20px auto;
|
|
}
|
|
|
|
#webcam {
|
|
width: 100%;
|
|
border-radius: 10px;
|
|
background: #000;
|
|
}
|
|
|
|
.confidence-bars {
|
|
margin-top: 20px;
|
|
}
|
|
|
|
.confidence-item {
|
|
margin-bottom: 15px;
|
|
}
|
|
|
|
.confidence-label {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
margin-bottom: 5px;
|
|
font-weight: 500;
|
|
}
|
|
|
|
.confidence-bar {
|
|
height: 30px;
|
|
background: #e0e0e0;
|
|
border-radius: 15px;
|
|
overflow: hidden;
|
|
position: relative;
|
|
}
|
|
|
|
.confidence-fill {
|
|
height: 100%;
|
|
background: linear-gradient(90deg, #667eea, #764ba2);
|
|
border-radius: 15px;
|
|
transition: width 0.3s ease;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
color: white;
|
|
font-weight: bold;
|
|
}
|
|
|
|
.status-message {
|
|
padding: 15px;
|
|
border-radius: 5px;
|
|
margin: 10px 0;
|
|
text-align: center;
|
|
font-weight: 500;
|
|
}
|
|
|
|
.status-success {
|
|
background: #c6f6d5;
|
|
color: #22543d;
|
|
border: 1px solid #9ae6b4;
|
|
}
|
|
|
|
.status-error {
|
|
background: #fed7d7;
|
|
color: #742a2a;
|
|
border: 1px solid #fc8181;
|
|
}
|
|
|
|
.status-info {
|
|
background: #bee3f8;
|
|
color: #2c5282;
|
|
border: 1px solid #90cdf4;
|
|
}
|
|
|
|
.status-warning {
|
|
background: #fef3c7;
|
|
color: #92400e;
|
|
border: 1px solid #fcd34d;
|
|
}
|
|
|
|
.training-progress {
|
|
margin: 20px 0;
|
|
}
|
|
|
|
.progress-bar {
|
|
height: 25px;
|
|
background: #e0e0e0;
|
|
border-radius: 12px;
|
|
overflow: hidden;
|
|
}
|
|
|
|
.progress-fill {
|
|
height: 100%;
|
|
background: linear-gradient(90deg, #48bb78, #38a169);
|
|
transition: width 0.3s;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
color: white;
|
|
font-weight: bold;
|
|
}
|
|
|
|
.metrics {
|
|
display: grid;
|
|
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
|
|
gap: 15px;
|
|
margin: 20px 0;
|
|
}
|
|
|
|
.metric-card {
|
|
background: #f7fafc;
|
|
padding: 15px;
|
|
border-radius: 8px;
|
|
text-align: center;
|
|
border: 1px solid #e2e8f0;
|
|
}
|
|
|
|
.metric-label {
|
|
color: #718096;
|
|
font-size: 12px;
|
|
text-transform: uppercase;
|
|
margin-bottom: 5px;
|
|
}
|
|
|
|
.metric-value {
|
|
color: #2d3748;
|
|
font-size: 24px;
|
|
font-weight: bold;
|
|
}
|
|
|
|
.samples-count {
|
|
display: inline-block;
|
|
background: #edf2f7;
|
|
padding: 2px 8px;
|
|
border-radius: 10px;
|
|
font-size: 12px;
|
|
color: #4a5568;
|
|
margin-left: 5px;
|
|
}
|
|
|
|
.full-width {
|
|
grid-column: 1 / -1;
|
|
}
|
|
|
|
.button-group {
|
|
display: flex;
|
|
gap: 10px;
|
|
margin: 20px 0;
|
|
flex-wrap: wrap;
|
|
}
|
|
|
|
.hidden {
|
|
display: none;
|
|
}
|
|
|
|
#lossChart {
|
|
width: 100%;
|
|
height: 300px;
|
|
margin-top: 20px;
|
|
border: 1px solid #e0e0e0;
|
|
border-radius: 8px;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="main-container">
|
|
<h1>🤖 自定义图像分类器</h1>
|
|
|
|
<div class="grid-container">
|
|
<!-- 数据采集卡片 -->
|
|
<div class="card">
|
|
<h2>📸 数据采集</h2>
|
|
|
|
<div class="class-input">
|
|
<h3><span class="class-number">1</span> 第一类</h3>
|
|
<input type="text" id="class1Name" placeholder="输入类别名称(如:人)" value="类别1">
|
|
<label class="file-label" for="class1Images">
|
|
选择图片
|
|
</label>
|
|
<input type="file" id="class1Images" multiple accept="image/*">
|
|
<span class="samples-count" id="class1Count">0 张图片</span>
|
|
<div class="image-preview" id="class1Preview"></div>
|
|
</div>
|
|
|
|
<div class="class-input">
|
|
<h3><span class="class-number">2</span> 第二类</h3>
|
|
<input type="text" id="class2Name" placeholder="输入类别名称(如:狗)" value="类别2">
|
|
<label class="file-label" for="class2Images">
|
|
选择图片
|
|
</label>
|
|
<input type="file" id="class2Images" multiple accept="image/*">
|
|
<span class="samples-count" id="class2Count">0 张图片</span>
|
|
<div class="image-preview" id="class2Preview"></div>
|
|
</div>
|
|
|
|
<div class="class-input">
|
|
<h3><span class="class-number">3</span> 第三类(可选)</h3>
|
|
<input type="text" id="class3Name" placeholder="输入类别名称(可选)" value="">
|
|
<label class="file-label" for="class3Images">
|
|
选择图片
|
|
</label>
|
|
<input type="file" id="class3Images" multiple accept="image/*">
|
|
<span class="samples-count" id="class3Count">0 张图片</span>
|
|
<div class="image-preview" id="class3Preview"></div>
|
|
</div>
|
|
|
|
<div class="button-group">
|
|
<button class="btn btn-primary" id="addDataBtn">添加到数据集</button>
|
|
<button class="btn btn-danger" id="clearDataBtn">清空数据集</button>
|
|
</div>
|
|
|
|
<div id="dataStatus"></div>
|
|
</div>
|
|
|
|
<!-- 训练控制卡片 -->
|
|
<div class="card">
|
|
<h2>🎯 模型训练</h2>
|
|
|
|
<!-- 超参数调节 -->
|
|
<div class="hyperparameters" style="background: #f8f9fa; padding: 15px; border-radius: 8px; margin-bottom: 20px;">
|
|
<h3 style="margin-bottom: 15px; color: #555;">⚙️ 超参数设置</h3>
|
|
|
|
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 15px;">
|
|
<div>
|
|
<label style="display: block; margin-bottom: 5px; font-size: 14px; color: #666;">
|
|
学习率: <span id="learningRateValue">0.001</span>
|
|
</label>
|
|
<input type="range" id="learningRate" min="-5" max="-1" step="0.1" value="-3"
|
|
style="width: 100%;" oninput="document.getElementById('learningRateValue').textContent = Math.pow(10, this.value).toFixed(5)">
|
|
</div>
|
|
|
|
<div>
|
|
<label style="display: block; margin-bottom: 5px; font-size: 14px; color: #666;">
|
|
训练轮数: <span id="epochsValue">100</span>
|
|
</label>
|
|
<input type="range" id="epochs" min="10" max="200" step="10" value="100"
|
|
style="width: 100%;" oninput="document.getElementById('epochsValue').textContent = this.value">
|
|
</div>
|
|
|
|
<div>
|
|
<label style="display: block; margin-bottom: 5px; font-size: 14px; color: #666;">
|
|
Dropout率: <span id="dropoutValue">0.3</span>
|
|
</label>
|
|
<input type="range" id="dropoutRate" min="0" max="0.7" step="0.05" value="0.3"
|
|
style="width: 100%;" oninput="document.getElementById('dropoutValue').textContent = this.value">
|
|
</div>
|
|
|
|
<div>
|
|
<label style="display: block; margin-bottom: 5px; font-size: 14px; color: #666;">
|
|
L2正则化: <span id="l2Value">0.01</span>
|
|
</label>
|
|
<input type="range" id="l2Regularization" min="-4" max="-1" step="0.1" value="-2"
|
|
style="width: 100%;" oninput="document.getElementById('l2Value').textContent = Math.pow(10, this.value).toFixed(4)">
|
|
</div>
|
|
|
|
<div>
|
|
<label style="display: block; margin-bottom: 5px; font-size: 14px; color: #666;">
|
|
批次大小: <span id="batchSizeValue">32</span>
|
|
</label>
|
|
<input type="range" id="batchSize" min="8" max="128" step="8" value="32"
|
|
style="width: 100%;" oninput="document.getElementById('batchSizeValue').textContent = this.value">
|
|
</div>
|
|
|
|
<div>
|
|
<label style="display: block; margin-bottom: 5px; font-size: 14px; color: #666;">
|
|
温度缩放: <span id="temperatureValue">1.0</span>
|
|
</label>
|
|
<input type="range" id="temperature" min="0.5" max="5" step="0.1" value="1.0"
|
|
style="width: 100%;" oninput="document.getElementById('temperatureValue').textContent = this.value">
|
|
</div>
|
|
</div>
|
|
|
|
<div style="margin-top: 15px;">
|
|
<label style="display: flex; align-items: center; gap: 10px; cursor: pointer;">
|
|
<input type="checkbox" id="earlyStoppingCheck" checked>
|
|
<span style="font-size: 14px; color: #666;">启用早停 (防止过拟合)</span>
|
|
</label>
|
|
|
|
<div id="earlyStoppingOptions" style="margin-top: 10px; padding-left: 25px;">
|
|
<label style="display: block; margin-bottom: 5px; font-size: 14px; color: #666;">
|
|
耐心值: <span id="patienceValue">10</span> epochs
|
|
</label>
|
|
<input type="range" id="patience" min="5" max="30" step="5" value="10"
|
|
style="width: 200px;" oninput="document.getElementById('patienceValue').textContent = this.value">
|
|
</div>
|
|
</div>
|
|
|
|
<div style="margin-top: 15px;">
|
|
<label style="display: flex; align-items: center; gap: 10px; cursor: pointer;">
|
|
<input type="checkbox" id="dataAugmentationCheck">
|
|
<span style="font-size: 14px; color: #666;">启用数据增强 (提高泛化能力)</span>
|
|
</label>
|
|
</div>
|
|
|
|
<div style="margin-top: 15px; display: flex; gap: 10px;">
|
|
<button class="btn btn-primary" onclick="classifier.resetHyperparameters()" style="padding: 8px 15px; font-size: 14px;">
|
|
重置为默认值
|
|
</button>
|
|
<button class="btn btn-primary" onclick="classifier.showRecommendations()" style="padding: 8px 15px; font-size: 14px;">
|
|
查看建议设置
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="button-group">
|
|
<button class="btn btn-success" id="trainBtn">开始训练</button>
|
|
<button class="btn btn-danger" id="stopBtn" disabled>停止训练</button>
|
|
</div>
|
|
|
|
<div id="trainingStatus"></div>
|
|
|
|
<div class="training-progress hidden" id="trainingProgress">
|
|
<div class="progress-bar">
|
|
<div class="progress-fill" id="progressFill">0%</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="metrics" id="metricsContainer"></div>
|
|
|
|
<canvas id="lossChart"></canvas>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- 预测卡片 -->
|
|
<div class="card full-width">
|
|
<h2>📹 实时预测</h2>
|
|
|
|
<div class="button-group">
|
|
<button class="btn btn-primary" id="startWebcamBtn">启动摄像头</button>
|
|
<button class="btn btn-danger" id="stopWebcamBtn" disabled>停止摄像头</button>
|
|
<button class="btn btn-success" id="saveModelBtn">保存模型</button>
|
|
<button class="btn btn-primary" id="loadModelBtn">加载模型</button>
|
|
</div>
|
|
|
|
<div id="webcam-container">
|
|
<video id="webcam" autoplay playsinline muted></video>
|
|
</div>
|
|
|
|
<div class="confidence-bars" id="confidenceBars"></div>
|
|
|
|
<div id="predictionStatus"></div>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="custom-classifier.js"></script>
|
|
</body>
|
|
</html> |