Created
October 11, 2018 11:56
-
-
Save skipperkongen/26fe121c795fcb7ac9bd38f0fc3a364c to your computer and use it in GitHub Desktop.
Train neural network on videos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.models import Sequential, load_model | |
from keras.layers import Dense, Activation, Dropout | |
from sklearn.model_selection import train_test_split | |
from sklearn.metrics import f1_score | |
MODEL_PATH='model.h5' | |
EPOCHS = 10 | |
HIDDEN_SIZE = 16 | |
model = Sequential() | |
model.add(Dense(HIDDEN_SIZE, input_shape=(X.shape[1],))) | |
model.add(Dense(HIDDEN_SIZE)) | |
model.add(Dropout(0.2)) | |
model.add(Dense(len(CLASSES), activation='softmax')) | |
model.compile(loss='categorical_crossentropy', | |
optimizer='rmsprop', | |
metrics=['accuracy']) | |
x_train, x_test, y_train, y_test = train_test_split(X, y, random_state=42) | |
model.fit(x_train, y_train, | |
batch_size=10, epochs=EPOCHS, | |
validation_split=0.1) | |
model.save(MODEL_PATH) | |
y_true = [np.argmax(y) for y in y_test] | |
y_pred = [np.argmax(pred) for pred in model.predict(x_test)] | |
score = f1_score(y_true, y_pred) | |
print('F1:', score) | |
# Use this to load the model | |
model = load_model(MODEL_PATH) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment