Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 자원측정
- apt-get
- asyncio
- 레파지토리
- python3.9
- Windows
- golang
- docker
- 스트리밍
- Thread
- BATCH
- repo
- Replication
- pip
- Finance
- APT
- FastAPI
- 주식
- QT_QPA_PLATFORM_PLUGIN_PATH
- 가상화 중첩화
- Python
- 영상스트리밍
- 파이썬
- 자식프로세스
- go.mod
- mariadb
- Hyper-V
- psutil
- dockercontainer
- go
Archives
- Today
- Total
검색하기귀찮아서만든블로그
[ML] MNIST 모델 테스트 본문
지난번 포스팅에서 학습한 MNIST 모델은 테스트해 보려고 한다.
테스트는 MNIST 모델을 로드하고 PyQt5 툴을 사용하여 사용자가 입력한 숫자를 모델에 태워서 원하는 결과가 나오는지 확인하였다.
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import numpy as np
import tensorflow as tf
class MyApp(QMainWindow):
def __init__(self):
super().__init__()
self.image = QImage(QSize(400, 400), QImage.Format_RGB32)
self.image.fill(Qt.white)
self.drawing = False
self.brush_size = 30
self.brush_color = Qt.black
self.last_point = QPoint()
self.loaded_model = None
self.initUI()
def initUI(self):
menubar = self.menuBar()
menubar.setNativeMenuBar(False)
filemenu = menubar.addMenu('File')
load_model_action = QAction('Load model', self)
load_model_action.setShortcut('Ctrl+L')
load_model_action.triggered.connect(self.load_model)
save_action = QAction('Save', self)
save_action.setShortcut('Ctrl+S')
save_action.triggered.connect(self.save)
clear_action = QAction('Clear', self)
clear_action.setShortcut('Ctrl+C')
clear_action.triggered.connect(self.clear)
filemenu.addAction(load_model_action)
filemenu.addAction(save_action)
filemenu.addAction(clear_action)
self.statusbar = self.statusBar()
self.setWindowTitle('MNIST Classifier')
self.setGeometry(300, 300, 400, 400)
self.show()
def paintEvent(self, e):
canvas = QPainter(self)
canvas.drawImage(self.rect(), self.image, self.image.rect())
def mousePressEvent(self, e):
if e.button() == Qt.LeftButton:
self.drawing = True
self.last_point = e.pos()
def mouseMoveEvent(self, e):
if (e.buttons() & Qt.LeftButton) & self.drawing:
painter = QPainter(self.image)
painter.setPen(QPen(self.brush_color, self.brush_size, Qt.SolidLine, Qt.RoundCap))
painter.drawLine(self.last_point, e.pos())
self.last_point = e.pos()
self.update()
def mouseReleaseEvent(self, e):
if e.button() == Qt.LeftButton:
self.drawing = False
arr = np.zeros((28, 28))
for i in range(28):
for j in range(28):
arr[j, i] = 1 - self.image.scaled(28, 28).pixelColor(i, j).getRgb()[0] / 255.0
arr = arr.reshape(-1, 28, 28)
if self.loaded_model:
pred = self.loaded_model.predict(arr)[0]
pred_num = str(np.argmax(pred))
self.statusbar.showMessage('숫자 ' + pred_num + '입니다.')
def load_model(self):
fname, _ = QFileDialog.getOpenFileName(self, 'Load Model', '')
if fname:
self.loaded_model = tf.keras.models.load_model(fname)
self.statusbar.showMessage('Model loaded.')
def save(self):
fpath, _ = QFileDialog.getSaveFileName(self, 'Save Image', '', "PNG(*.png);;JPEG(*.jpg *.jpeg);;All Files(*.*) ")
if fpath:
self.image.scaled(28, 28).save(fpath)
def clear(self):
self.image.fill(Qt.white)
self.update()
self.statusbar.clearMessage()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())

로드된 MNIST 모델이 그려진 이미지를 인식하여 숫자로 결과를 출력하는 것을 볼 수 있다.
코드 참조 : https://codetorial.net/pyqt5/examples/mnist_classifier.html