SW-ramon-dyzman/fcnn/main.py

44 lines
1.3 KiB
Python
Raw Normal View History

2021-11-28 17:33:52 +01:00
import os
import cv2
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
def preprocess(img):
2021-11-28 21:00:52 +01:00
scale_percent = 10
2021-11-28 17:33:52 +01:00
width = int(img.shape[1] * scale_percent / 100)
height = int(img.shape[0] * scale_percent / 100)
dim = (width, height)
resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
resized = resized.flatten()
return resized
def readData(data_links):
x, y = [], []
for link in data_links:
2021-11-28 21:00:52 +01:00
img = cv2.imread(link, cv2.IMREAD_COLOR)
2021-11-28 17:33:52 +01:00
img = preprocess(img)
label = link.split("/")[1].split('_')[1]
x.append(img)
y.append(label)
return x, y
data_links = os.listdir("data/")
data_links = ["data/" + x for x in data_links]
x, y = readData(data_links)
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=42)
2021-11-28 21:00:52 +01:00
clf = MLPClassifier(solver='adam', alpha=1e-5, hidden_layer_sizes=(1000, 700), random_state=1,
activation='relu', batch_size='auto', shuffle=True, verbose=True, learning_rate='adaptive', n_iter_no_change=10)
2021-11-28 17:33:52 +01:00
clf.fit(X_train, y_train)
print("Score:")
print(clf.score(X_test, y_test))
print("Summary:")
Y_pred = clf.predict(X_test)
print(classification_report(y_test, Y_pred))