File size: 19,360 Bytes
e47b149
 
 
52af76f
 
e47b149
 
52af76f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e47b149
 
 
 
52af76f
 
 
 
 
 
 
 
 
 
e47b149
52af76f
e47b149
52af76f
 
 
 
 
 
e47b149
52af76f
e47b149
52af76f
 
 
 
 
 
 
 
 
e47b149
52af76f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e47b149
 
 
 
52af76f
 
 
e47b149
 
 
52af76f
 
 
 
e47b149
52af76f
 
 
 
 
 
 
 
 
259a0f2
 
 
 
 
 
52af76f
e47b149
52af76f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
259a0f2
 
 
 
52af76f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e47b149
52af76f
e47b149
52af76f
 
 
 
 
e47b149
52af76f
e47b149
52af76f
 
 
 
e47b149
 
 
52af76f
 
 
 
 
 
 
 
 
e47b149
 
52af76f
 
 
 
e47b149
 
52af76f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e47b149
52af76f
 
 
 
e47b149
 
52af76f
 
 
e47b149
52af76f
 
 
 
 
 
 
 
 
 
259a0f2
 
 
 
 
 
 
 
 
 
 
52af76f
 
259a0f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52af76f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e47b149
52af76f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>AlexNet Image Classifier</title>
    <style>
        /* --- existing styles unchanged --- */
        * { margin:0; padding:0; box-sizing:border-box; }
        body { font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,'Helvetica Neue',Arial,sans-serif; background:linear-gradient(135deg,#667eea 0%,#764ba2 100%); min-height:100vh; display:flex; justify-content:center; align-items:center; padding:20px; }
        .container { background:white; border-radius:20px; box-shadow:0 20px 60px rgba(0,0,0,0.3); max-width:900px; width:100%; padding:40px; }
        .header { text-align:center; margin-bottom:40px; }
        .header h1 { color:#333; font-size:2.5em; margin-bottom:10px; background:linear-gradient(135deg,#667eea 0%,#764ba2 100%); -webkit-background-clip:text; -webkit-text-fill-color:transparent; background-clip:text; }
        .header p { color:#666; font-size:1.1em; }

        .preset-section { margin-bottom:30px; }
        .preset-grid { display:grid; grid-template-columns:repeat(4,1fr); gap:16px; }
        .preset-card { border:2px solid #eee; border-radius:12px; overflow:hidden; background:#fafafa; cursor:pointer; transition:all .2s ease; }
        .preset-card:hover { transform:translateY(-2px); box-shadow:0 8px 24px rgba(0,0,0,.08); }
        .preset-card.selected { border-color:#667eea; box-shadow:0 0 0 3px rgba(102,126,234,.25) inset; }
        .preset-thumb { width:100%; height:120px; object-fit:cover; display:block; background:#f0f0f0; }
        .preset-label { text-align:center; font-weight:700; padding:10px; color:#444; }

        .upload-section { margin-bottom:30px; }
        .upload-area { border:3px dashed #ddd; border-radius:15px; padding:24px; text-align:center; transition:all .3s ease; cursor:pointer; background:#fafafa; }
        .upload-area:hover { border-color:#667eea; background:#f5f5ff; }
        .upload-area.drag-over { border-color:#764ba2; background:#f0f0ff; transform:scale(1.02); }
        .upload-icon { font-size:36px; color:#667eea; margin-bottom:8px; }
        .upload-text { color:#333; font-size:1.05em; margin-bottom:6px; }
        .upload-subtext { color:#666; font-size:.9em; }
        .file-input { display:none; }

        .image-preview-section { display:none; margin-bottom:30px; }
        .image-preview-section.active { display:block; }
        .preview-container { display:flex; gap:30px; align-items:start; }
        .preview-image-wrapper { flex:1; max-width:400px; }
        .preview-image { width:100%; height:auto; border-radius:10px; box-shadow:0 4px 20px rgba(0,0,0,0.1); }
        .image-info { flex:1; padding:20px; background:#f8f9fa; border-radius:10px; }
        .info-item { display:flex; justify-content:space-between; margin-bottom:10px; padding-bottom:10px; border-bottom:1px solid #e9ecef; }
        .info-item:last-child { border-bottom:none; margin-bottom:0; }
        .info-label { color:#666; font-weight:500; }
        .info-value { color:#333; font-weight:600; }

        .button-group { display:flex; gap:15px; margin-top:20px; }
        .btn { padding:12px 30px; border:none; border-radius:8px; font-size:1em; font-weight:600; cursor:pointer; transition:all .3s ease; }
        .btn-primary { background:linear-gradient(135deg,#667eea 0%,#764ba2 100%); color:white; flex:1; }
        .btn-primary:hover { transform:translateY(-2px); box-shadow:0 5px 20px rgba(102,126,234,.4); }
        .btn-primary:disabled { background:#ccc; cursor:not-allowed; transform:none; }
        .btn-secondary { background:#e9ecef; color:#495057; }
        .btn-secondary:hover { background:#dee2e6; }

        .results-section { display:none; }
        .results-section.active { display:block; animation:slideIn .3s ease; }
        @keyframes slideIn { from { opacity:0; transform:translateY(20px);} to { opacity:1; transform:translateY(0);} }
        .results-header { background:linear-gradient(135deg,#28a745 0%,#20c997 100%); color:white; padding:20px; border-radius:10px; margin-bottom:20px; }
        .predicted-class { font-size:1.8em; font-weight:700; margin-bottom:5px; }
        .confidence-score { font-size:1.2em; opacity:.95; }

        .probabilities-container { background:#f8f9fa; border-radius:10px; padding:20px; }
        .probabilities-title { font-size:1.2em; color:#333; margin-bottom:15px; font-weight:600; }
        .probability-item { margin-bottom:15px; }
        .probability-label { display:flex; justify-content:space-between; margin-bottom:5px; }
        .class-name { color:#495057; font-weight:500; }
        .class-prob { color:#333; font-weight:600; }
        .probability-bar-bg { height:8px; background:#e9ecef; border-radius:4px; overflow:hidden; }
        .probability-bar { height:100%; background:linear-gradient(90deg,#667eea 0%,#764ba2 100%); border-radius:4px; transition:width .5s ease; }

        .error-message { display:none; background:#f8d7da; color:#721c24; padding:15px; border-radius:8px; margin-top:20px; }
        .error-message.active { display:block; }

        .loading-spinner { display:none; text-align:center; padding:20px; }
        .loading-spinner.active { display:block; }
        .spinner { display:inline-block; width:40px; height:40px; border:4px solid #f3f3f3; border-top:4px solid #667eea; border-radius:50%; animation:spin 1s linear infinite; }
        @keyframes spin { 0% { transform:rotate(0deg);} 100% { transform:rotate(360deg);} }
        .loading-text { color:#666; margin-top:10px; }

        @media (max-width:768px) {
            .container { padding:20px; }
            .header h1 { font-size:2em; }
            .preview-container { flex-direction:column; }
            .preview-image-wrapper { max-width:100%; }
            .preset-grid { grid-template-columns:repeat(2,1fr); }
        }
    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <h1>🧠 AlexNet Classifier</h1>
        <p>Select a preset image or upload your own</p>
    </div>

    <!-- NEW: Preset selector -->
    <div class="preset-section">
        <div class="preset-grid" id="presetGrid">
            <!-- Cards are populated by JS using /preset_image/<label> -->
        </div>
    </div>

    <div class="upload-section">
        <div class="upload-area" id="uploadArea">
            <input type="file" id="fileInput" class="file-input" accept="image/*">
            <div class="upload-icon">📸</div>
            <div class="upload-text">Click to upload or drag and drop</div>
            <div class="upload-subtext">Supports JPG, PNG, GIF, BMP</div>
        </div>
    </div>

    <div class="image-preview-section" id="previewSection">
        <div class="preview-container">
            <div class="preview-image-wrapper">
                <img id="previewImage" class="preview-image" alt="Preview">
            </div>
            <div class="image-info">
                <div class="info-item">
                    <span class="info-label">Source:</span>
                    <span class="info-value" id="sourceLabel">-</span>
                </div>
                <div class="info-item">
                    <span class="info-label">File Name:</span>
                    <span class="info-value" id="fileName">-</span>
                </div>
                <div class="info-item">
                    <span class="info-label">File Size:</span>
                    <span class="info-value" id="fileSize">-</span>
                </div>
                <div class="info-item">
                    <span class="info-label">Image Type:</span>
                    <span class="info-value" id="fileType">-</span>
                </div>
                <div class="info-item">
                    <span class="info-label">Dimensions:</span>
                    <span class="info-value" id="imageDimensions">-</span>
                </div>
            </div>
        </div>

        <div class="button-group">
            <button class="btn btn-secondary" id="clearBtn">Clear</button>
            <button class="btn btn-primary" id="classifyBtn">Classify Image</button>
        </div>
    </div>

    <div class="loading-spinner" id="loadingSpinner">
        <div class="spinner"></div>
        <div class="loading-text">Analyzing image...</div>
    </div>

    <div class="results-section" id="resultsSection">
        <div class="results-header">
            <div class="predicted-class" id="predictedClass">-</div>
            <div class="confidence-score" id="confidenceScore">-</div>
        </div>
        <div class="probabilities-container">
            <div class="probabilities-title">All Class Probabilities</div>
            <div id="probabilitiesList"></div>
        </div>

        <div class="gradcam-container" id="gradcamContainer" style="display:none; margin:16px 0 20px;">
            <div class="probabilities-title" style="margin-bottom:10px;">Grad-CAM (Predicted Class)</div>
            <img id="gradcamImage" class="preview-image" alt="Grad-CAM visualization" style="max-width:480px; width:100%; border-radius:10px; box-shadow:0 4px 20px rgba(0,0,0,0.08);" />
        </div>

    </div>

    <div class="error-message" id="errorMessage"></div>
</div>

<script>
    const uploadArea = document.getElementById('uploadArea');
    const fileInput = document.getElementById('fileInput');
    const previewSection = document.getElementById('previewSection');
    const previewImage = document.getElementById('previewImage');
    const fileName = document.getElementById('fileName');
    const fileSize = document.getElementById('fileSize');
    const fileType = document.getElementById('fileType');
    const imageDimensions = document.getElementById('imageDimensions');
    const sourceLabel = document.getElementById('sourceLabel');
    const clearBtn = document.getElementById('clearBtn');
    const classifyBtn = document.getElementById('classifyBtn');
    const resultsSection = document.getElementById('resultsSection');
    const predictedClass = document.getElementById('predictedClass');
    const confidenceScore = document.getElementById('confidenceScore');
    const probabilitiesList = document.getElementById('probabilitiesList');
    const errorMessage = document.getElementById('errorMessage');
    const loadingSpinner = document.getElementById('loadingSpinner');
    const presetGrid = document.getElementById('presetGrid');

    const gradcamContainer = document.getElementById('gradcamContainer');
    const gradcamImage = document.getElementById('gradcamImage');


    let currentFile = null;
    let currentPreset = null; // 'TP' | 'TN' | 'FN' | 'FP' | null

    const PRESETS = [
        { key: 'TN', label: 'True Negative', name: 'Image A' },
        { key: 'FP', label: 'False Positive', name: 'Image B' },
        { key: 'TP', label: 'True Positive', name: 'Image C' },
        { key: 'FN', label: 'False Negative', name: 'Image D' },
    ];

    // Build preset cards
    function buildPresetGrid() {
        presetGrid.innerHTML = '';
        PRESETS.forEach(p => {
            const card = document.createElement('div');
            card.className = 'preset-card';
            card.dataset.key = p.key;
            card.innerHTML = `
                <img class="preset-thumb" alt="${p.key}" src="/preset_image/${p.key}">
                <div class="preset-label">${p.name}</div>
            `;
            card.addEventListener('click', () => selectPreset(p.key,p.name));
            presetGrid.appendChild(card);
        });
    }

    function markSelectedPreset() {
        const cards = presetGrid.querySelectorAll('.preset-card');
        cards.forEach(c => {
            if (c.dataset.key === currentPreset) c.classList.add('selected');
            else c.classList.remove('selected');
        });
    }

    async function selectPreset(key, name) {
        try {
            currentPreset = key;
            currentFile = null; // uploading not used when preset chosen
            hideError();
            hideResults();

            const imgURL = `/preset_image/${key}`;
            // Fetch blob to get size + type metadata
            const resp = await fetch(imgURL);
            if (!resp.ok) throw new Error('Failed to load preset image');
            const blob = await resp.blob();

            previewImage.src = imgURL;
            previewImage.onload = () => {
                imageDimensions.textContent = `${previewImage.naturalWidth} × ${previewImage.naturalHeight}`;
            };

            fileName.textContent = `${name}.jpg`;
            fileSize.textContent = formatFileSize(blob.size);
            fileType.textContent = blob.type || 'image/jpeg';
            sourceLabel.textContent = `Preset (${name.split(' ')[1]})`;

            previewSection.classList.add('active');
            markSelectedPreset();
        } catch (e) {
            showError(e.message || 'Could not select preset image.');
        }
    }

    // Handle upload area
    uploadArea.addEventListener('click', () => fileInput.click());

    fileInput.addEventListener('change', (e) => {
        const file = e.target.files[0];
        if (file) handleFile(file);
    });

    uploadArea.addEventListener('dragover', (e) => {
        e.preventDefault(); uploadArea.classList.add('drag-over');
    });
    uploadArea.addEventListener('dragleave', () => uploadArea.classList.remove('drag-over'));
    uploadArea.addEventListener('drop', (e) => {
        e.preventDefault(); uploadArea.classList.remove('drag-over');
        const files = e.dataTransfer.files;
        if (files.length > 0) handleFile(files[0]);
    });

    function handleFile(file) {
        if (!file.type.startsWith('image/')) {
            showError('Please upload an image file');
            return;
        }
        currentFile = file;
        currentPreset = null;
        displayPreview(file);
        hideError();
        hideResults();
        markSelectedPreset();
        sourceLabel.textContent = 'Uploaded file';
    }

    function displayPreview(file) {
        const reader = new FileReader();
        reader.onload = (e) => {
            previewImage.src = e.target.result;
            previewImage.onload = () => {
                imageDimensions.textContent = `${previewImage.naturalWidth} × ${previewImage.naturalHeight}`;
            };
        };
        reader.readAsDataURL(file);

        fileName.textContent = file.name;
        fileSize.textContent = formatFileSize(file.size);
        fileType.textContent = file.type || 'Unknown';
        previewSection.classList.add('active');
    }

    function formatFileSize(bytes) {
        if (bytes === 0) return '0 Bytes';
        const k = 1024, sizes = ['Bytes','KB','MB','GB'];
        const i = Math.floor(Math.log(bytes)/Math.log(k));
        return Math.round(bytes/Math.pow(k,i)*100)/100 + ' ' + sizes[i];
    }

    clearBtn.addEventListener('click', () => {
        currentFile = null;
        currentPreset = null;
        fileInput.value = '';
        previewSection.classList.remove('active');
        hideResults();
        hideError();
        markSelectedPreset();
    });

    classifyBtn.addEventListener('click', async () => {
        if (!currentFile && !currentPreset) {
            showError('No image selected (choose a preset or upload one).');
            return;
        }
        try {
            showLoading(); hideError(); hideResults();
            let response;

            if (currentPreset) {
                response = await fetch('/predict_preset', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ preset: currentPreset })
                });
            } else {
                const formData = new FormData();
                formData.append('image', currentFile);
                response = await fetch('/predict_AlexNet', { method: 'POST', body: formData });
            }

            if (!response.ok) {
                const errorData = await response.json().catch(() => ({}));
                throw new Error(errorData.error || 'Failed to classify image');
            }
            const result = await response.json();
            displayResults(result);
        } catch (error) {
            showError(error.message || 'Failed to classify image. Please try again.');
            console.error('Classification error:', error);
        } finally {
            hideLoading();
        }
    });

function displayResults(result) {
    predictedClass.textContent = result.class;
    confidenceScore.textContent = `${(result.confidence * 100).toFixed(2)}% Confidence`;

    // --- NEW: Grad-CAM rendering ---
    if (result.gradcam) {
        gradcamImage.src = result.gradcam;
        gradcamContainer.style.display = 'block';
    } else {
        gradcamContainer.style.display = 'none';
        gradcamImage.removeAttribute('src');
    }

    const sortedProbs = Object.entries(result.probabilities)
        .sort(([, a], [, b]) => b - a)
        .slice(0, 10);

    probabilitiesList.innerHTML = '';
    sortedProbs.forEach(([className, prob], index) => {
        const probPercent = (prob * 100).toFixed(2);
        const isTop = index === 0;
        const div = document.createElement('div');
        div.className = 'probability-item';
        div.innerHTML = `
            <div class="probability-label">
                <span class="class-name" style="${isTop ? 'font-weight:700;color:#667eea;' : ''}">${className}</span>
                <span class="class-prob" style="${isTop ? 'font-weight:700;color:#667eea;' : ''}">${probPercent}%</span>
            </div>
            <div class="probability-bar-bg">
                <div class="probability-bar" style="width:0%;" data-width="${probPercent}"></div>
            </div>
        `;
        probabilitiesList.appendChild(div);
    });

    resultsSection.classList.add('active');
    setTimeout(() => {
        probabilitiesList.querySelectorAll('.probability-bar').forEach(bar => {
            bar.style.width = bar.getAttribute('data-width') + '%';
        });
    }, 100);
}

    function showLoading() { loadingSpinner.classList.add('active'); classifyBtn.disabled = true; }
    function hideLoading() { loadingSpinner.classList.remove('active'); classifyBtn.disabled = false; }
    function hideResults() { resultsSection.classList.remove('active'); }
    function showError(message) { errorMessage.textContent = message; errorMessage.classList.add('active'); }
    function hideError() { errorMessage.classList.remove('active'); }

    document.addEventListener('keydown', (e) => {
        if (e.key === 'Enter' && (currentFile || currentPreset) && !classifyBtn.disabled) classifyBtn.click();
        if (e.key === 'Escape' && previewSection.classList.contains('active')) clearBtn.click();
    });

    window.addEventListener('load', async () => {
        buildPresetGrid();
        try {
            const response = await fetch('/health');
            if (!response.ok) showError('Backend server is not responding. Ensure it is running on port 7860.');
        } catch {
            showError('Cannot connect to backend server. Ensure it is running on http://0.0.0.0:7860');
        }
    });
</script>
</body>
</html>