-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
62 lines (43 loc) · 1.37 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import matplotlib.pyplot as plt
import pandas as pd
import cv2
import numpy as np
import collections
import tensorflow as tf
from sklearn import datasets
from sklearn.model_selection import train_test_split
import os
# Load the digits dataset
digits = datasets.load_digits()
# Get the input images from the dataset
data = digits.images
# Calculate the moments of the input images
moments = []
for img in data:
# Calculate the moments
M = cv2.moments(img)
moments.append(M)
# Convert the moments to a pandas DataFrame
df_data = pd.DataFrame(moments)
# Convert the DataFrame to float32
df_data = df_data.astype('float32')
# Split the data into training and testing sets
X_train, X_test, y_train, y_test =train_test_split(df_data, digits.target,
test_size=0.2, shuffle=False)
# Define the neural network model
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(df_data.shape[1],)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10)
])
# Compile the model
model.compile(optimizer='adam',
loss= tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics =['accuracy'])
# Train the model
model.fit(X_train, y_train, epochs=100)
# Evaluate the model on the testing data
test_loss, test_acc = model.evaluate(X_test, y_test, verbose=2)
# Print the test accuracy
print('\nTest accuracy:', test_acc)
model.save("handwritten.model")