recognizing but training must be improved

This commit is contained in:
shaaqu 2020-05-20 08:24:33 +02:00
parent ddb652119b
commit 4b8c560be9
8 changed files with 129 additions and 64 deletions

View File

@ -19,11 +19,14 @@
<select />
</component>
<component name="ChangeListManager">
<list default="true" id="828778c9-9d97-422f-a727-18ddbd059b85" name="Default Changelist" comment="train.py">
<change afterPath="$PROJECT_DIR$/coder/gr_test.png" afterDir="false" />
<change afterPath="$PROJECT_DIR$/coder/image.py" afterDir="false" />
<list default="true" id="828778c9-9d97-422f-a727-18ddbd059b85" name="Default Changelist" comment="fun created">
<change afterPath="$PROJECT_DIR$/coder/rocognizer.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/coder/test1.jpg" afterDir="false" />
<change afterPath="$PROJECT_DIR$/coder/test3.png" afterDir="false" />
<change afterPath="$PROJECT_DIR$/coder/test9.png" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/coder/test.jpg" beforeDir="false" afterPath="$PROJECT_DIR$/coder/test.jpg" afterDir="false" />
<change beforePath="$PROJECT_DIR$/coder/image.py" beforeDir="false" afterPath="$PROJECT_DIR$/coder/image.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/coder/train.py" beforeDir="false" afterPath="$PROJECT_DIR$/coder/train_nn.py" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -81,7 +84,7 @@
<recent name="C:\Users\Pawel Lukaszewicz\PycharmProjects\AL-2020\coder" />
</key>
</component>
<component name="RunManager" selected="Python.train">
<component name="RunManager" selected="Python.load_image">
<configuration default="true" type="PythonConfigurationType" factoryName="Python">
<module name="wozek" />
<option name="INTERPRETER_OPTIONS" value="" />
@ -158,7 +161,7 @@
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/coder/image.py" />
<option name="SCRIPT_NAME" value="C:\Users\Pawel Lukaszewicz\PycharmProjects\AL-2020\coder\image.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="true" />
<option name="EMULATE_TERMINAL" value="false" />
@ -189,7 +192,7 @@
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<configuration name="train" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<configuration name="train_nn" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="wozek" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
@ -199,7 +202,7 @@
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="SCRIPT_NAME" value="C:\Users\Pawel Lukaszewicz\PycharmProjects\AL-2020\coder\train.py" />
<option name="SCRIPT_NAME" value="C:\Users\Pawel Lukaszewicz\PycharmProjects\AL-2020\coder\train_nn.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="true" />
<option name="EMULATE_TERMINAL" value="false" />
@ -212,13 +215,13 @@
<item itemvalue="Python.board" />
<item itemvalue="Python.main" />
<item itemvalue="Python.feature_hashing" />
<item itemvalue="Python.train" />
<item itemvalue="Python.image" />
<item itemvalue="Python.train_nn" />
<item itemvalue="Python.load_image" />
</list>
<recent_temporary>
<list>
<item itemvalue="Python.train" />
<item itemvalue="Python.image" />
<item itemvalue="Python.load_image" />
<item itemvalue="Python.train_nn" />
<item itemvalue="Python.main" />
<item itemvalue="Python.feature_hashing" />
<item itemvalue="Python.board" />
@ -247,7 +250,7 @@
<workItem from="1589814601057" duration="4208000" />
<workItem from="1589844260514" duration="777000" />
<workItem from="1589845066174" duration="177000" />
<workItem from="1589888090669" duration="16639000" />
<workItem from="1589888090669" duration="19744000" />
</task>
<task id="LOCAL-00001" summary="create Shelf">
<created>1589815443652</created>
@ -277,7 +280,21 @@
<option name="project" value="LOCAL" />
<updated>1589918562733</updated>
</task>
<option name="localTasksCounter" value="5" />
<task id="LOCAL-00005" summary="img to 1d array">
<created>1589952733192</created>
<option name="number" value="00005" />
<option name="presentableId" value="LOCAL-00005" />
<option name="project" value="LOCAL" />
<updated>1589952733192</updated>
</task>
<task id="LOCAL-00006" summary="fun created">
<created>1589952979012</created>
<option name="number" value="00006" />
<option name="presentableId" value="LOCAL-00006" />
<option name="project" value="LOCAL" />
<updated>1589952979012</updated>
</task>
<option name="localTasksCounter" value="7" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -301,7 +318,9 @@
<MESSAGE value="create addShelf method in Field class" />
<MESSAGE value="changed CreateDataSweets to lowercase" />
<MESSAGE value="train.py" />
<option name="LAST_COMMIT_MESSAGE" value="train.py" />
<MESSAGE value="img to 1d array" />
<MESSAGE value="fun created" />
<option name="LAST_COMMIT_MESSAGE" value="fun created" />
</component>
<component name="WindowStateProjectService">
<state x="115" y="162" key="#com.intellij.refactoring.safeDelete.UnsafeUsagesDialog" timestamp="1589923610328">

View File

@ -1,31 +1,63 @@
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 cv2
def image():
img = cv2.cvtColor(cv2.imread('test.jpg'), cv2.COLOR_BGR2GRAY)
img = cv2.GaussianBlur(img, (15, 15), 0) # poprawia jakosc
img = cv2.resize(img, (8, 8), interpolation=cv2.INTER_AREA)
# training
# recznie napisane cyfry
digits = datasets.load_digits()
print(type(img))
print(img.shape)
print(img)
plt.imshow(img, cmap='binary')
plt.show()
y = digits.target
x = digits.images.reshape((len(digits.images), -1))
data = []
x_train = x[:1000000]
y_train = y[:1000000]
x_test = x[1000:]
y_test = y[1000:]
rows, cols = img.shape
for i in range(rows):
for j in range(cols):
k = img[i, j]
if k > 200:
k = 0 # brak czarnego
else:
k = 1
mlp = MLPClassifier(hidden_layer_sizes=(15,), activation='logistic', alpha=1e-4,
solver='sgd', tol=1e-4, random_state=1,
learning_rate_init=.1, verbose=True)
data.append(k)
mlp.fit(x_train, y_train)
predictions = mlp.predict(x_test)
print(accuracy_score(y_test, predictions))
# image
img = cv2.cvtColor(cv2.imread('test3.png'), cv2.COLOR_BGR2GRAY)
img = cv2.GaussianBlur(img, (5, 5), 0) # poprawia jakosc
img = cv2.resize(img, (8, 8), interpolation=cv2.INTER_AREA)
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 > 100:
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])
print(data)

13
coder/rocognizer.py Normal file
View File

@ -0,0 +1,13 @@
import matplotlib.pyplot as plt
import numpy as np
from numpy import asarray
from sklearn import datasets
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
from PIL import Image
import pygame
import functions
import sys
import time

BIN
coder/test1.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

BIN
coder/test3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB

BIN
coder/test9.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.7 KiB

View File

@ -1,29 +0,0 @@
import matplotlib.pyplot as plt
import numpy as np
from numpy import asarray
from sklearn import datasets
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
from PIL import Image
def train():
# recznie napisane cyfry
digits = datasets.load_digits()
y = digits.target
x = digits.images.reshape((len(digits.images), -1))
x_train = x[:1000000]
y_train = y[:1000000]
x_test = x[1000:]
y_test = y[1000:]
mlp = MLPClassifier(hidden_layer_sizes=(15,), activation='logistic', alpha=1e-4,
solver='sgd', tol=1e-4, random_state=1,
learning_rate_init=.1, verbose=True)
mlp.fit(x_train, y_train)
predictions = mlp.predict(x_test)
print(accuracy_score(y_test, predictions))

30
coder/train_nn.py Normal file
View File

@ -0,0 +1,30 @@
import matplotlib.pyplot as plt
import numpy as np
from numpy import asarray
import pygame
from sklearn import datasets
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
from PIL import Image
# recznie napisane cyfry
digits = datasets.load_digits()
y = digits.target
x = digits.images.reshape((len(digits.images), -1))
x_train = x[:1000000]
y_train = y[:1000000]
x_test = x[1000:]
y_test = y[1000:]
mlp = MLPClassifier(hidden_layer_sizes=(15,), activation='logistic', alpha=1e-4,
solver='sgd', tol=1e-4, random_state=1,
learning_rate_init=.1, verbose=True)
mlp.fit(x_train, y_train)
predictions = mlp.predict(x_test)
print(accuracy_score(y_test, predictions))
print(x_test[1])