forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
489 lines (383 loc) · 19.1 KB
/
Copy pathgui.py
File metadata and controls
489 lines (383 loc) · 19.1 KB
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import sys
import os
def main():
try:
from PyQt6.QtWidgets import QApplication
except ImportError:
print("Error: PyQt6 not installed")
print("Install with: pip install neuralforge[gui]")
print("Or: pip install PyQt6")
sys.exit(1)
current_dir = os.path.dirname(os.path.abspath(__file__))
root_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(current_dir))))
sys.path.insert(0, root_dir)
from PyQt6.QtWidgets import (QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
QPushButton, QLabel, QLineEdit, QFileDialog,
QProgressBar, QTextEdit, QGroupBox)
from PyQt6.QtCore import Qt, QThread, pyqtSignal
from PyQt6.QtGui import QPixmap, QFont
import torch
import torch.nn.functional as F
from torchvision import transforms
from PIL import Image
from neuralforge.data.datasets import get_dataset, get_num_classes
from neuralforge.models.resnet import ResNet18
class PredictionThread(QThread):
finished = pyqtSignal(list, list, str)
error = pyqtSignal(str)
def __init__(self, model, image_path, classes, device):
super().__init__()
self.model = model
self.image_path = image_path
self.classes = classes
self.device = device
def run(self):
try:
image = Image.open(self.image_path).convert('RGB')
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])
])
image_tensor = transform(image).unsqueeze(0).to(self.device)
with torch.no_grad():
outputs = self.model(image_tensor)
probabilities = F.softmax(outputs, dim=1)
top5_prob, top5_idx = torch.topk(probabilities, min(5, len(self.classes)), dim=1)
predictions = []
confidences = []
for idx, prob in zip(top5_idx[0].cpu().numpy(), top5_prob[0].cpu().numpy()):
predictions.append(self.classes[idx])
confidences.append(float(prob) * 100)
main_prediction = predictions[0]
self.finished.emit(predictions, confidences, main_prediction)
except Exception as e:
self.error.emit(str(e))
class NeuralForgeGUI(QMainWindow):
def __init__(self):
super().__init__()
self.model = None
self.device = 'cuda' if torch.cuda.is_available() else 'cpu'
self.classes = []
self.dataset_name = 'cifar10'
self.init_ui()
self.apply_stylesheet()
def init_ui(self):
self.setWindowTitle('NeuralForge - Model Tester')
self.setGeometry(100, 100, 1200, 800)
central_widget = QWidget()
self.setCentralWidget(central_widget)
main_layout = QHBoxLayout()
central_widget.setLayout(main_layout)
left_panel = self.create_left_panel()
right_panel = self.create_right_panel()
main_layout.addWidget(left_panel, 1)
main_layout.addWidget(right_panel, 1)
def create_left_panel(self):
panel = QWidget()
layout = QVBoxLayout()
panel.setLayout(layout)
title = QLabel('🚀 NeuralForge Model Tester')
title.setFont(QFont('Arial', 20, QFont.Weight.Bold))
title.setAlignment(Qt.AlignmentFlag.AlignCenter)
layout.addWidget(title)
model_group = QGroupBox('Model Selection')
model_layout = QVBoxLayout()
model_path_layout = QHBoxLayout()
self.model_path_input = QLineEdit()
self.model_path_input.setPlaceholderText('Path to model file (.pt)')
model_path_layout.addWidget(self.model_path_input)
browse_btn = QPushButton('Browse')
browse_btn.clicked.connect(self.browse_model)
model_path_layout.addWidget(browse_btn)
default_btn = QPushButton('Use Default')
default_btn.clicked.connect(self.use_default_model)
model_path_layout.addWidget(default_btn)
model_layout.addLayout(model_path_layout)
dataset_layout = QHBoxLayout()
dataset_label = QLabel('Dataset:')
self.dataset_input = QLineEdit('cifar10')
self.dataset_input.setPlaceholderText('cifar10, mnist, stl10, tiny_imagenet, etc.')
self.dataset_input.setToolTip('Supported: cifar10, cifar100, mnist, fashion_mnist, stl10,\ntiny_imagenet, imagenet, food101, caltech256, oxford_pets')
dataset_layout.addWidget(dataset_label)
dataset_layout.addWidget(self.dataset_input)
model_layout.addLayout(dataset_layout)
self.load_model_btn = QPushButton('Load Model')
self.load_model_btn.clicked.connect(self.load_model)
model_layout.addWidget(self.load_model_btn)
self.model_status = QLabel('No model loaded')
self.model_status.setAlignment(Qt.AlignmentFlag.AlignCenter)
model_layout.addWidget(self.model_status)
model_group.setLayout(model_layout)
layout.addWidget(model_group)
image_group = QGroupBox('Image Selection')
image_layout = QVBoxLayout()
image_path_layout = QHBoxLayout()
self.image_path_input = QLineEdit()
self.image_path_input.setPlaceholderText('Path to image file')
image_path_layout.addWidget(self.image_path_input)
browse_image_btn = QPushButton('Browse')
browse_image_btn.clicked.connect(self.browse_image)
image_path_layout.addWidget(browse_image_btn)
image_layout.addLayout(image_path_layout)
self.image_preview = QLabel()
self.image_preview.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.image_preview.setMinimumHeight(300)
self.image_preview.setStyleSheet('border: 2px dashed #666; border-radius: 10px;')
self.image_preview.setText('No image selected')
image_layout.addWidget(self.image_preview)
self.predict_btn = QPushButton('🔍 Predict')
self.predict_btn.clicked.connect(self.predict_image)
self.predict_btn.setEnabled(False)
image_layout.addWidget(self.predict_btn)
image_group.setLayout(image_layout)
layout.addWidget(image_group)
layout.addStretch()
return panel
def create_right_panel(self):
panel = QWidget()
layout = QVBoxLayout()
panel.setLayout(layout)
results_group = QGroupBox('Prediction Results')
results_layout = QVBoxLayout()
self.main_prediction = QLabel('No prediction yet')
self.main_prediction.setFont(QFont('Arial', 24, QFont.Weight.Bold))
self.main_prediction.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.main_prediction.setStyleSheet('color: #4CAF50; padding: 20px;')
results_layout.addWidget(self.main_prediction)
self.confidence_label = QLabel('')
self.confidence_label.setFont(QFont('Arial', 16))
self.confidence_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
results_layout.addWidget(self.confidence_label)
self.progress_bar = QProgressBar()
self.progress_bar.setVisible(False)
results_layout.addWidget(self.progress_bar)
results_group.setLayout(results_layout)
layout.addWidget(results_group)
top5_group = QGroupBox('Top-5 Predictions')
top5_layout = QVBoxLayout()
self.top5_display = QTextEdit()
self.top5_display.setReadOnly(True)
self.top5_display.setMinimumHeight(200)
top5_layout.addWidget(self.top5_display)
top5_group.setLayout(top5_layout)
layout.addWidget(top5_group)
info_group = QGroupBox('Model Information')
info_layout = QVBoxLayout()
self.model_info = QTextEdit()
self.model_info.setReadOnly(True)
self.model_info.setMaximumHeight(150)
info_layout.addWidget(self.model_info)
info_group.setLayout(info_layout)
layout.addWidget(info_group)
layout.addStretch()
return panel
def apply_stylesheet(self):
qss = """
QMainWindow {
background-color: #1e1e1e;
}
QWidget {
background-color: #1e1e1e;
color: #e0e0e0;
font-family: 'Segoe UI', Arial;
font-size: 12px;
}
QGroupBox {
border: 2px solid #3d3d3d;
border-radius: 8px;
margin-top: 10px;
padding-top: 15px;
font-weight: bold;
color: #4CAF50;
}
QGroupBox::title {
subcontrol-origin: margin;
left: 10px;
padding: 0 5px;
}
QPushButton {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
border-radius: 5px;
font-weight: bold;
font-size: 13px;
}
QPushButton:hover {
background-color: #45a049;
}
QPushButton:pressed {
background-color: #3d8b40;
}
QPushButton:disabled {
background-color: #555555;
color: #888888;
}
QLineEdit {
background-color: #2d2d2d;
border: 2px solid #3d3d3d;
border-radius: 5px;
padding: 8px;
color: #e0e0e0;
}
QLineEdit:focus {
border: 2px solid #4CAF50;
}
QTextEdit {
background-color: #2d2d2d;
border: 2px solid #3d3d3d;
border-radius: 5px;
padding: 10px;
color: #e0e0e0;
}
QLabel {
color: #e0e0e0;
}
QProgressBar {
border: 2px solid #3d3d3d;
border-radius: 5px;
text-align: center;
background-color: #2d2d2d;
}
QProgressBar::chunk {
background-color: #4CAF50;
border-radius: 3px;
}
"""
self.setStyleSheet(qss)
def browse_model(self):
file_path, _ = QFileDialog.getOpenFileName(
self,
'Select Model File',
'./models',
'Model Files (*.pt *.pth);;All Files (*.*)'
)
if file_path:
self.model_path_input.setText(file_path)
def use_default_model(self):
default_path = './models/final_model.pt'
if not os.path.exists(default_path):
default_path = './models/best_model.pt'
self.model_path_input.setText(os.path.abspath(default_path))
def browse_image(self):
file_path, _ = QFileDialog.getOpenFileName(
self,
'Select Image File',
'',
'Image Files (*.png *.jpg *.jpeg *.bmp *.gif);;All Files (*.*)'
)
if file_path:
self.image_path_input.setText(file_path)
self.display_image(file_path)
def display_image(self, image_path):
try:
pixmap = QPixmap(image_path)
scaled_pixmap = pixmap.scaled(400, 300, Qt.AspectRatioMode.KeepAspectRatio,
Qt.TransformationMode.SmoothTransformation)
self.image_preview.setPixmap(scaled_pixmap)
except Exception as e:
self.image_preview.setText(f'Error loading image: {e}')
def load_model(self):
model_path = self.model_path_input.text()
dataset_input = self.dataset_input.text().lower().strip()
dataset_aliases = {
'cifar10': 'cifar10', 'cifar-10': 'cifar10', 'cifar_10': 'cifar10',
'cifar100': 'cifar100', 'cifar-100': 'cifar100', 'cifar_100': 'cifar100',
'mnist': 'mnist',
'fashionmnist': 'fashion_mnist', 'fashion-mnist': 'fashion_mnist', 'fashion_mnist': 'fashion_mnist',
'stl10': 'stl10', 'stl-10': 'stl10', 'stl_10': 'stl10',
'tinyimagenet': 'tiny_imagenet', 'tiny-imagenet': 'tiny_imagenet', 'tiny_imagenet': 'tiny_imagenet',
'imagenet': 'imagenet',
'food101': 'food101', 'food-101': 'food101', 'food_101': 'food101',
'caltech256': 'caltech256', 'caltech-256': 'caltech256', 'caltech_256': 'caltech256',
'oxfordpets': 'oxford_pets', 'oxford-pets': 'oxford_pets', 'oxford_pets': 'oxford_pets',
}
self.dataset_name = dataset_aliases.get(dataset_input, dataset_input)
if not model_path:
self.model_status.setText('Please select a model file')
self.model_status.setStyleSheet('color: #f44336;')
return
if not os.path.exists(model_path):
self.model_status.setText('Model file not found')
self.model_status.setStyleSheet('color: #f44336;')
return
try:
self.model_status.setText('Loading model...')
self.model_status.setStyleSheet('color: #FFC107;')
QApplication.processEvents()
num_classes = get_num_classes(self.dataset_name)
self.model = ResNet18(num_classes=num_classes)
self.model = self.model.to(self.device)
checkpoint = torch.load(model_path, map_location=self.device, weights_only=False)
self.model.load_state_dict(checkpoint['model_state_dict'])
self.model.eval()
try:
dataset = get_dataset(self.dataset_name, train=False, download=False)
self.classes = getattr(dataset, 'classes', [str(i) for i in range(num_classes)])
except:
from neuralforge.data.datasets import get_class_names
self.classes = get_class_names(self.dataset_name)
self.model_status.setText(f'✓ Model loaded successfully')
self.model_status.setStyleSheet('color: #4CAF50;')
self.predict_btn.setEnabled(True)
total_params = sum(p.numel() for p in self.model.parameters())
epoch = checkpoint.get('epoch', 'Unknown')
val_loss = checkpoint.get('best_val_loss', 'Unknown')
val_loss_str = f"{val_loss:.4f}" if isinstance(val_loss, float) else str(val_loss)
info_text = f"""
Model: ResNet18
Dataset: {self.dataset_name.upper()}
Classes: {num_classes}
Parameters: {total_params:,}
Epoch: {epoch}
Best Val Loss: {val_loss_str}
Device: {self.device.upper()}
"""
self.model_info.setText(info_text.strip())
except Exception as e:
self.model_status.setText(f'Error: {str(e)}')
self.model_status.setStyleSheet('color: #f44336;')
def predict_image(self):
image_path = self.image_path_input.text()
if not image_path or not os.path.exists(image_path):
self.main_prediction.setText('Please select a valid image')
self.main_prediction.setStyleSheet('color: #f44336;')
return
if self.model is None:
self.main_prediction.setText('Please load a model first')
self.main_prediction.setStyleSheet('color: #f44336;')
return
self.predict_btn.setEnabled(False)
self.progress_bar.setVisible(True)
self.progress_bar.setRange(0, 0)
self.prediction_thread = PredictionThread(self.model, image_path, self.classes, self.device)
self.prediction_thread.finished.connect(self.display_results)
self.prediction_thread.error.connect(self.display_error)
self.prediction_thread.start()
def display_results(self, predictions, confidences, main_prediction):
self.progress_bar.setVisible(False)
self.predict_btn.setEnabled(True)
self.main_prediction.setText(f'🎯 {main_prediction}')
self.main_prediction.setStyleSheet('color: #4CAF50; padding: 20px; font-size: 28px;')
self.confidence_label.setText(f'Confidence: {confidences[0]:.2f}%')
top5_text = '<h3>Top-5 Predictions:</h3><hr>'
for i, (pred, conf) in enumerate(zip(predictions, confidences), 1):
bar_width = int(conf * 3)
bar = '█' * bar_width
top5_text += f'<p style="margin: 10px 0;"><b>{i}. {pred}</b><br>'
top5_text += f'<span style="color: #4CAF50;">{bar}</span> {conf:.2f}%</p>'
self.top5_display.setHtml(top5_text)
def display_error(self, error_msg):
self.progress_bar.setVisible(False)
self.predict_btn.setEnabled(True)
self.main_prediction.setText(f'Error: {error_msg}')
self.main_prediction.setStyleSheet('color: #f44336;')
app = QApplication(sys.argv)
window = NeuralForgeGUI()
window.show()
sys.exit(app.exec())
if __name__ == '__main__':
main()