-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.py
68 lines (53 loc) · 1.93 KB
/
model.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
56
57
58
59
60
61
62
63
64
65
66
67
68
import pandas as pd
import tensorflow as tf
from tensorflow import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.models import load_model
import matplotlib.pyplot as plt
from sklearn.preprocessing import OneHotEncoder
import os
import numpy as np
df = pd.read_csv('faces.csv')
dataset = df.values
X = dataset[:,0:136]
Y = dataset[:,-1]
labels = [0 for x in range(9)]
new_Y = []
for index, cat in enumerate(Y):
new_labels = labels.copy()
new_labels[int(cat)] = 1
new_Y.append(np.array(new_labels))
# ohe = OneHotEncoder(categories='auto', categorical_features = 'all')
# Y = ohe.fit_transform(Y).toarray()
new_Y = np.array(new_Y)
print(new_Y)
from sklearn import preprocessing
min_max_scaler = preprocessing.MinMaxScaler()
X_scale = min_max_scaler.fit_transform(X)
from sklearn.model_selection import train_test_split
# X_train, X_val_and_test, Y_train, Y_val_and_test = train_test_split(X_scale, new_Y, test_size=0.2)
# X_val, X_test, Y_val, Y_test = train_test_split(X_val_and_test, Y_val_and_test, test_size=0.2)
X_train, X_test, Y_train, Y_test = train_test_split(X_scale, new_Y, test_size=0.2)
# X_val, X_test, Y_val, Y_test = train_test_split(X_val_and_test, Y_val_and_test, test_size=0.2)
model = None
if not os.path.isfile('./model.h5'):
model = Sequential([
Dense(32, activation='relu', input_shape=(136,)),
Dense(128, activation='relu'),
Dense(9, activation='softmax'),
])
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
hist = model.fit(X_train, Y_train,
batch_size=4, epochs=1000,
validation_data=(X_test, Y_test))
model.save("model.h5")
else:
new_model = load_model('model.h5')
prediction=new_model.predict(X_test)
print(prediction[0])
#Accuracy
test_loss, test_acc = new_model.evaluate(X_test, Y_test)
print('Test accuracy:', test_acc)