forked from s444420/AL-2020
82 lines
1.9 KiB
Python
82 lines
1.9 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
|
|
|
|
#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')
|
|
|
|
|
|
|
|
# training
|
|
# recznie napisane cyfry
|
|
|
|
digits = datasets.load_digits()
|
|
y = digits.target
|
|
x = digits.images.reshape((len(digits.images), -1))
|
|
|
|
|
|
#ogarnac zbior, zwiekszyc warstwy
|
|
|
|
x_train = train_data[0:20000, 1:]
|
|
y_train = train_data[0:20000, 0]
|
|
x_test = test_data[0:20000]
|
|
y_test = test_data[0:20000, 0]
|
|
|
|
# x_train = x[:900]
|
|
# y_train = y[:900]
|
|
# x_test = x[900:]
|
|
# y_test = y[900:]
|
|
|
|
print(x_test[0].shape, y_test[9].shape)
|
|
|
|
mlp = MLPClassifier(hidden_layer_sizes=(100, 100, 100, 100), activation='logistic', alpha=1e-4,
|
|
solver='sgd', tol=0.000000000001, random_state=1,
|
|
learning_rate_init=.1, verbose=True, max_iter=1000)
|
|
|
|
mlp.fit(x_train, y_train)
|
|
print(123456789)
|
|
predictions = mlp.predict(x_test)
|
|
print(123456789)
|
|
|
|
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)
|
|
print(img)
|
|
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 = 1
|
|
|
|
data.append(k)
|
|
|
|
data = np.asarray(data, dtype=np.float32)
|
|
print(data)
|
|
|
|
predictions = mlp.predict([data])
|
|
|
|
print("Liczba to:", predictions[0])
|