Created
May 4, 2018 18:10
-
-
Save marko-asplund/377a247ac77454288c8d213cb613e963 to your computer and use it in GitHub Desktop.
This file contains 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
import numpy as np | |
import tensorflow as tf | |
from tensorflow import keras | |
from keras.models import Sequential | |
import h5py | |
from keras.utils.io_utils import HDF5Matrix | |
data_dir = '...' | |
train_fn = data_dir + '/dataset_train.h5' | |
test_fn = data_dir + '/dataset_test.h5' | |
image_dims = (64, 64, 3) | |
train_epochs = 2500 | |
X_train = HDF5Matrix(train_fn, 'train_set_x', normalizer = lambda x: x / 255.0) | |
y_train = HDF5Matrix(train_fn, 'train_set_y') | |
X_test = HDF5Matrix(test_fn, 'test_set_x', normalizer = lambda x: x / 255.0) | |
y_test = HDF5Matrix(test_fn, 'test_set_y') | |
m_train = X_train.shape[0] | |
m_test = X_test.shape[0] | |
#X_train.shape: (209, 64, 64, 3) | |
#y_train.shape: (209,) | |
model = tf.keras.Sequential([ | |
tf.keras.layers.Flatten(input_shape=image_dims), | |
tf.keras.layers.Dense(20, activation="relu"), | |
tf.keras.layers.Dense(7, activation="relu"), | |
tf.keras.layers.Dense(5, activation="relu"), | |
tf.keras.layers.Dense(1, activation="sigmoid") | |
]) | |
from keras import optimizers | |
model.compile(loss=keras.losses.binary_crossentropy, | |
optimizer=keras.optimizers.SGD(lr=0.01), | |
metrics=['accuracy']) | |
model.fit(X_train, y_train, epochs=train_epochs, shuffle="batch", batch_size=m_train) # batch GD | |
loss_and_metrics = model.evaluate(X_test, y_test, batch_size=m_test) | |
print("loss and metrics: %s" % loss_and_metrics) | |
classes = model.predict(X_test) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment