0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SVMによるmnistの学習・推論

Posted at

背景

SVMでmnistのデータを学習する研修はよく見るが、学習させたものに対して自分で手書きした文字の食べさせ方を説明しているものは見たことがなかった。
まぁ、自分がこの研修を以前受けたときは、仕組みは分かったから別にいいかとなったのですが、その場で試してみた方が定着が良いかと思うので、自分の備忘録もかねて投稿します。

学習

google colaboratory上のドライブ準備

手順1 google driveにMy Drive\Colab Notebooks\dataのフォルダを作成。

手順2 フォルダのマウント
from google.colab import drive
drive.mount('/content/drive')
%cd /content/drive/My Drive/Colab Notebooks/data
手順3 mnistデータ取得と正規化
from sklearn.datasets import fetch_openml
from sklearn.model_selection import train_test_split

%time X, y = fetch_openml('mnist_784', version=1, return_X_y=True)

X_train, X_test, y_train, y_test = train_test_split(X / 255,     # 正規化
                                   y.astype('int64'),            # 正解データを数値にする
                                   stratify = y,
                                   random_state=0)
手順4 SVMによる学習と学習済みモデルの塩漬け
import pickle
from sklearn.svm import SVC

clf = SVC(kernel='rbf', gamma='auto', random_state=0, C=2)
clf.fit(X_train, y_train)

pickle.dump(clf, open("trained_model.pkl", 'wb'))         #trained model pickle

推論

上記の学習でtrained_model.pklが生成されているので、再学習は不要です。

手順1 学習済みモデルの読込
import pickle
clf = pickle.load(open('trained_model.pkl', 'rb'))        #trained model load
print(clf)
手順2 予測
import matplotlib.pyplot as plt
import numpy as np

fname = "./tegaki1_28-28.jpg"

tegaki_0_255 = plt.imread(fname,0)   # read image.
tegaki_0_1   = tegaki_0_255/255      # Normalization.

tmp = tegaki_0_1.reshape(1,784)      # convert matrix to 1 line.
res = clf.predict(tmp)

print("prediction result: ",res)
plt.imshow(tegaki_0_255, cmap = "gray")

[推論の結果]
何の文字だったかと、その画像が出力される。

番外編 画像生成リサイズ

手順1 google driveのMy Drive/Colab Notebooks/dataにtegaki0.jpgを配置。
    画像はカラー・モノクロ問いません。サイズも適当で良いです。

手順2 画像のリサイズ
%cd /content/drive/My Drive/Colab Notebooks/data

import cv2
from google.colab.patches import cv2_imshow

in_fname = "./tegaki0.jpg"
out_fname = "./tegaki0_28-28.jpg"

src = cv2.imread(in_fname,cv2.IMREAD_GRAYSCALE)
tgt = cv2.resize(src,dsize=(28,28))

cv2.imwrite(out_fname,tgt)

[番外編の結果]
google driveのMy Drive/Colab Notebooks/dataにtegaki0_28-28.jpgという名前で
28ピクセル×28ピクセルのグレースケール画像が作成される。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?