Created
May 29, 2018 23:04
-
-
Save koshian2/27f3bfdbe93287195daba65c6ed5de28 to your computer and use it in GitHub Desktop.
CIFAR-10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
from keras.datasets import cifar10 | |
from sklearn.svm import LinearSVC | |
import time | |
start_time = time.time() | |
# データの読み込み | |
(x_train_origin, y_train), (x_test_origin, y_test) = cifar10.load_data() | |
# 小数化 | |
x_train_origin = x_train_origin / 255 | |
x_test_origin = x_test_origin / 255 | |
# データ数 | |
m_train, m_test = x_train_origin.shape[0], x_test_origin.shape[0] | |
# グレースケール化 | |
def to_grayscale(tensor): | |
# https://ja.wikipedia.org/wiki/%E3%82%B0%E3%83%AC%E3%83%BC%E3%82%B9%E3%82%B1%E3%83%BC%E3%83%AB#%E8%BC%9D%E5%BA%A6%E4%BF%9D%E5%AD%98%E5%A4%89%E6%8F%9B | |
# Y = 0.2126R + 0.7152G + 0.0722B | |
return 0.2126*tensor[:, :, :, 0] + 0.7152*tensor[:, :, :, 1] + 0.0722*tensor[:, :, :, 2] | |
x_train, x_test = to_grayscale(x_train_origin), to_grayscale(x_test_origin) | |
print(x_train.shape) #(50000, 32, 32) | |
#plt.imshow(x_train[0], cmap="gray") | |
#plt.show() | |
# ベクトル化 | |
x_train, x_test = x_train.reshape(m_train, -1), x_test.reshape(m_test, -1) | |
# ノルムで標準化 | |
x_train = x_train / np.linalg.norm(x_train, ord=2, axis=1, keepdims=True) | |
x_test = x_test / np.linalg.norm(x_test, ord=2, axis=1, keepdims=True) | |
# サポートベクトルマシン | |
svc = LinearSVC() | |
svc.fit(x_train, y_train) | |
print("Elapsed[s] : ", time.time() - start_time) | |
print("Train :", svc.score(x_train, y_train)) | |
print("Test :", svc.score(x_test, y_test)) | |
#Elapsed[s] : 91.28059148788452 | |
#Train : 0.3313 | |
#Test : 0.2901 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment