forked from s444420/AL-2020
86 lines
2.2 KiB
Python
86 lines
2.2 KiB
Python
import numpy as np
|
|
from PIL import Image
|
|
import matplotlib.pyplot as plt
|
|
from sklearn import datasets
|
|
from sklearn.metrics import accuracy_score
|
|
from sklearn.neural_network import MLPClassifier
|
|
import pandas as pd
|
|
import cv2
|
|
import keras
|
|
|
|
# 28x28
|
|
train_data = np.genfromtxt('dataset/train.csv', delimiter=',', skip_header=1, max_rows=20000, encoding='utf-8')
|
|
test_data = np.genfromtxt('dataset/test.csv', delimiter=',', skip_header=1, max_rows=20000, encoding='utf-8')
|
|
|
|
# train_data = pd.read_csv('dataset/train.csv')
|
|
# test_data = pd.read_csv('dataset/test.csv')
|
|
|
|
# training
|
|
# recznie napisane cyfry
|
|
|
|
digits = datasets.load_digits()
|
|
y = digits.target
|
|
x = digits.images.reshape((len(digits.images), -1))
|
|
|
|
# print(type(y[0]), type(x[0]))
|
|
# ogarnac zbior, zwiekszyc warstwy
|
|
|
|
# x_train = train_data.iloc[:, 1:].values.astype('float32')
|
|
# y_train = train_data.iloc[:, 0].values.astype('int32')
|
|
# x_test = test_data.values.astype('float32')
|
|
|
|
x_train = train_data[0:10000, 1:]
|
|
y_train = train_data[0:10000, 0]
|
|
x_test = train_data[10001:20000, 1:]
|
|
y_test = train_data[10001:20000, 0].astype('int')
|
|
|
|
print(type(y_test[0]), type(x_test[0]))
|
|
|
|
# x_train = x[:900]
|
|
# y_train = y[:900]
|
|
# x_test = x[900:]
|
|
# y_test = y[900:]
|
|
|
|
# 500, 500, 500, 500, 500
|
|
mlp = MLPClassifier(hidden_layer_sizes=(150, 100, 100, 100), activation='logistic', alpha=1e-4,
|
|
solver='sgd', tol=0.000000000001, random_state=1,
|
|
learning_rate_init=.1, verbose=True, max_iter=10000)
|
|
|
|
mlp.fit(x_train, y_train)
|
|
predictions = mlp.predict(x_test)
|
|
|
|
print("Accuracy: ", accuracy_score(y_test, predictions))
|
|
|
|
# image
|
|
|
|
img = cv2.cvtColor(cv2.imread('test5.jpg'), cv2.COLOR_BGR2GRAY)
|
|
img = cv2.blur(img, (9, 9)) # poprawia jakosc
|
|
img = cv2.resize(img, (28, 28), interpolation=cv2.INTER_AREA)
|
|
img = img.reshape((len(img), -1))
|
|
|
|
# print(type(img))
|
|
# print(img.shape)
|
|
# plt.imshow(img ,cmap='binary')
|
|
# plt.show()
|
|
|
|
data = []
|
|
|
|
rows, cols = img.shape
|
|
for i in range(rows):
|
|
for j in range(cols):
|
|
k = img[i, j]
|
|
if k > 225:
|
|
k = 0 # brak czarnego
|
|
else:
|
|
k = 255
|
|
|
|
data.append(k)
|
|
|
|
data = np.asarray(data, dtype=np.float64)
|
|
# print(data)
|
|
print(type(data))
|
|
|
|
predictions = mlp.predict([data])
|
|
|
|
print("Liczba to:", predictions[0].astype('int'))
|