Added files. /JL
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
|
||||
import numpy as np
|
||||
import time
|
||||
import matplotlib.pyplot as plt
|
||||
import keras.models as km
|
||||
from keras.datasets import mnist
|
||||
from keras.models import Sequential
|
||||
from keras.layers.core import Dense, Flatten, Dropout, Activation
|
||||
from keras.utils import np_utils
|
||||
|
||||
predictions = ['T-shirt/top', 'trouser', 'Pullover', 'Dress', 'Coat',
|
||||
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
|
||||
|
||||
(X_train, y_train), (X_test, y_test) = mnist.load_data()
|
||||
num_pixels = X_train.shape[1] * X_train.shape[2]
|
||||
num_classes = 10
|
||||
|
||||
t1 = time.time()
|
||||
X_train = X_train.reshape(60000, 784) / 255
|
||||
X_test = X_test.reshape(10000, 784) / 255
|
||||
X_train = X_train.astype('float32')
|
||||
X_test = X_test.astype('float32')
|
||||
|
||||
# let's print the shape before we reshape and normalize
|
||||
print("X_train shape", X_train.shape)
|
||||
print("y_train shape", y_train.shape)
|
||||
print("X_test shape", X_test.shape)
|
||||
print("y_test shape", y_test.shape)
|
||||
|
||||
Y_train = np_utils.to_categorical(y_train, num_classes)
|
||||
Y_test = np_utils.to_categorical(y_test, num_classes)
|
||||
t2 = time.time()
|
||||
print("Preprocessing took %.2f sec." % (t2 - t1))
|
||||
|
||||
print('Loading model and weights.')
|
||||
json_file = open('mnist.json', 'r')
|
||||
loaded_nnet = json_file.read()
|
||||
json_file.close()
|
||||
|
||||
model = km.model_from_json(loaded_nnet)
|
||||
model.load_weights('mnist.h5')
|
||||
|
||||
print("Training network ...")
|
||||
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
|
||||
|
||||
print("Making predictions...")
|
||||
t1 = time.time()
|
||||
for i in range(9):
|
||||
img = np.array(X_test[i][np.newaxis,:])
|
||||
preds = model.predict_classes(img)
|
||||
print("Image[", i, "] - Me thinks me saw a : ", predictions[int(preds[0])] )
|
||||
t2 = time.time()
|
||||
print("Predictions took %.2f sec." % (t2 - t1))
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
{"class_name": "Sequential", "config": {"name": "sequential_1", "layers": [{"class_name": "Dense", "config": {"name": "dense_1", "trainable": true, "batch_input_shape": [null, 784], "dtype": "float32", "units": 512, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "VarianceScaling", "config": {"scale": 1.0, "mode": "fan_avg", "distribution": "uniform", "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "Activation", "config": {"name": "activation_1", "trainable": true, "activation": "relu"}}, {"class_name": "Dropout", "config": {"name": "dropout_1", "trainable": true, "rate": 0.2, "noise_shape": null, "seed": null}}, {"class_name": "Dense", "config": {"name": "dense_2", "trainable": true, "units": 512, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "VarianceScaling", "config": {"scale": 1.0, "mode": "fan_avg", "distribution": "uniform", "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "Activation", "config": {"name": "activation_2", "trainable": true, "activation": "relu"}}, {"class_name": "Dropout", "config": {"name": "dropout_2", "trainable": true, "rate": 0.2, "noise_shape": null, "seed": null}}, {"class_name": "Dense", "config": {"name": "dense_3", "trainable": true, "units": 10, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "VarianceScaling", "config": {"scale": 1.0, "mode": "fan_avg", "distribution": "uniform", "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "Activation", "config": {"name": "activation_3", "trainable": true, "activation": "softmax"}}]}, "keras_version": "2.2.4", "backend": "tensorflow"}
|
||||
Executable
+73
@@ -0,0 +1,73 @@
|
||||
|
||||
import numpy as np
|
||||
import time
|
||||
import matplotlib.pyplot as plt
|
||||
from keras.datasets import mnist
|
||||
from keras.models import Sequential
|
||||
from keras.layers.core import Dense, Flatten, Dropout, Activation
|
||||
from keras.utils import np_utils
|
||||
|
||||
(X_train, y_train), (X_test, y_test) = mnist.load_data()
|
||||
num_pixels = X_train.shape[1] * X_train.shape[2]
|
||||
num_classes = 10
|
||||
|
||||
fig = plt.figure()
|
||||
for i in range(9):
|
||||
plt.subplot(3,3,i+1)
|
||||
plt.tight_layout()
|
||||
plt.imshow(X_test[i], cmap='gray', interpolation='none')
|
||||
plt.title("Digit: %d" % (y_test[i]))
|
||||
plt.xticks([])
|
||||
plt.yticks([])
|
||||
fig.show()
|
||||
t1 = time.time()
|
||||
|
||||
X_train = X_train.reshape(60000, 784) / 255
|
||||
X_test = X_test.reshape(10000, 784) / 255
|
||||
X_train = X_train.astype('float32')
|
||||
X_test = X_test.astype('float32')
|
||||
|
||||
# let's print the shape before we reshape and normalize
|
||||
print("X_train shape", X_train.shape)
|
||||
print("y_train shape", y_train.shape)
|
||||
print("X_test shape", X_test.shape)
|
||||
print("y_test shape", y_test.shape)
|
||||
|
||||
Y_train = np_utils.to_categorical(y_train, num_classes)
|
||||
Y_test = np_utils.to_categorical(y_test, num_classes)
|
||||
t2 = time.time()
|
||||
print("Preprocessing took %.2f sec." % (t2 - t1))
|
||||
|
||||
t1 = time.time()
|
||||
# building a linear stack of layers with the sequential model
|
||||
model = Sequential()
|
||||
model.add(Dense(512, input_shape = (784,)))
|
||||
model.add(Activation('relu'))
|
||||
model.add(Dropout(0.2))
|
||||
model.add(Dense(512))
|
||||
model.add(Activation('relu'))
|
||||
model.add(Dropout(0.2))
|
||||
model.add(Dense(10))
|
||||
model.add(Activation('softmax'))
|
||||
|
||||
|
||||
print("Training network ...")
|
||||
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
|
||||
|
||||
print("Fitting data")
|
||||
model.fit(X_train, Y_train, batch_size=128, epochs=15,
|
||||
verbose=1, validation_data=(X_test, Y_test))
|
||||
t2 = time.time()
|
||||
print("Training took %.2f sec." % (t2 - t1))
|
||||
|
||||
print("Making predictions...")
|
||||
t1 = time.time()
|
||||
for i in range(9):
|
||||
img = np.array(X_test[i][np.newaxis,:])
|
||||
preds = model.predict_classes(img)
|
||||
print("Image[", i, "] - Me thinks me saw a : ", int(preds[0]) )
|
||||
t2 = time.time()
|
||||
print("Predictions took %.2f sec." % (t2 - t1))
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
|
||||
import numpy as np
|
||||
import time
|
||||
import matplotlib.pyplot as plt
|
||||
from keras.datasets import mnist
|
||||
from keras.models import Sequential
|
||||
from keras.layers.core import Dense, Flatten, Dropout, Activation
|
||||
from keras.utils import np_utils
|
||||
|
||||
predictions = ['T-shirt/top', 'trouser', 'Pullover', 'Dress', 'Coat',
|
||||
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
|
||||
|
||||
(X_train, y_train), (X_test, y_test) = mnist.load_data()
|
||||
num_pixels = X_train.shape[1] * X_train.shape[2]
|
||||
num_classes = 10
|
||||
|
||||
fig = plt.figure()
|
||||
for i in range(9):
|
||||
plt.subplot(3,3,i+1)
|
||||
plt.tight_layout()
|
||||
plt.imshow(X_test[i], cmap='gray', interpolation='none')
|
||||
plt.title("Digit: %d" % (y_test[i]))
|
||||
plt.xticks([])
|
||||
plt.yticks([])
|
||||
fig.show()
|
||||
t1 = time.time()
|
||||
|
||||
X_train = X_train.reshape(60000, 784) / 255
|
||||
X_test = X_test.reshape(10000, 784) / 255
|
||||
X_train = X_train.astype('float32')
|
||||
X_test = X_test.astype('float32')
|
||||
|
||||
# let's print the shape before we reshape and normalize
|
||||
print("X_train shape", X_train.shape)
|
||||
print("y_train shape", y_train.shape)
|
||||
print("X_test shape", X_test.shape)
|
||||
print("y_test shape", y_test.shape)
|
||||
|
||||
Y_train = np_utils.to_categorical(y_train, num_classes)
|
||||
Y_test = np_utils.to_categorical(y_test, num_classes)
|
||||
t2 = time.time()
|
||||
print("Preprocessing took %.2f sec." % (t2 - t1))
|
||||
|
||||
t1 = time.time()
|
||||
# building a linear stack of layers with the sequential model
|
||||
model = Sequential()
|
||||
model.add(Dense(512, input_shape = (784,)))
|
||||
model.add(Activation('relu'))
|
||||
model.add(Dropout(0.2))
|
||||
model.add(Dense(512))
|
||||
model.add(Activation('relu'))
|
||||
model.add(Dropout(0.2))
|
||||
model.add(Dense(10))
|
||||
model.add(Activation('softmax'))
|
||||
|
||||
|
||||
print("Training network ...")
|
||||
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
|
||||
|
||||
print("Fitting data")
|
||||
model.fit(X_train, Y_train, batch_size=128, epochs=15,
|
||||
verbose=1, validation_data=(X_test, Y_test))
|
||||
t2 = time.time()
|
||||
print("Training took %.2f sec." % (t2 - t1))
|
||||
|
||||
print('Saving model and weights.')
|
||||
model_json = model.to_json()
|
||||
with open("mnist.json", "w") as json_file:
|
||||
json_file.write(model_json)
|
||||
model.save_weights('mnist.h5')
|
||||
|
||||
print("Making predictions...")
|
||||
t1 = time.time()
|
||||
for i in range(9):
|
||||
img = np.array(X_test[i][np.newaxis,:])
|
||||
preds = model.predict_classes(img)
|
||||
print("Image[", i, "] - Me thinks me saw a : ", predictions[int(preds[0])] )
|
||||
t2 = time.time()
|
||||
print("Predictions took %.2f sec." % (t2 - t1))
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user