Created
March 18, 2020 01:02
-
-
Save MinaGabriel/b682885268cdd6401a03e6e3055a84c9 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 keras | |
from keras.datasets import mnist | |
from keras.layers import Input, Dense | |
from keras.models import load_model | |
from skimage.util import invert | |
import numpy as np | |
import matplotlib.pyplot as plt | |
(X_train, y_train), (X_test, y_test) = mnist.load_data() | |
X_train = invert(X_train.astype('float32') / 255) | |
X_test = invert(X_test.astype('float32') / 255) | |
print('Size of test images 1 : ', X_test.shape) | |
X_train = X_train.reshape(len(X_train), np.prod(X_train.shape[1:])) | |
X_test = X_test.reshape(len(X_test), np.prod(X_test.shape[1:])) | |
print('Size of test images 2 : ', X_test.shape) | |
# convert class vectors to binary class matrices | |
# y_train = keras.utils.to_categorical(y_train, 10) | |
# y_test = keras.utils.to_categorical(y_test, 10) | |
inputs = Input(shape=(784,)) | |
x = Dense(units=64, activation='relu')(inputs) | |
x = Dense(units=64, activation='relu')(x) | |
outputs = Dense(units=10, activation='softmax')(x) | |
model = keras.Model(inputs=inputs, outputs=outputs) | |
model.compile(optimizer=keras.optimizers.Adam(), # Optimizer | |
# Loss function to minimize | |
loss=keras.losses.sparse_categorical_crossentropy, | |
# List of metrics to monitor | |
metrics=['accuracy']) | |
history = model.fit(X_train, y_train, batch_size=64, epochs=1, validation_data=(X_test, y_test)) | |
print('\nhistory dict:', history.history) | |
# Evaluate the model on the test data using `evaluate` | |
print('\n# Evaluate on test data') | |
results = model.evaluate(X_test, y_test, batch_size=64) | |
print('test loss, test acc:', results) | |
print('\n Save and Load Model **************---------------****************** ') | |
model.save('MNIST.h5') | |
test_model = load_model('MNIST.h5') | |
test_model.summary() | |
print('Size of test images 3 : ', X_test.shape) | |
k = X_test[2533] | |
#k = invert(k) | |
test_img = k.reshape(28, 28) | |
plt.imshow(test_img, cmap='gray') | |
plt.show() | |
print(test_img.shape) | |
k = np.array(k) | |
print(k.shape) | |
k = k.reshape(1, 784) | |
#print(k) | |
prediction = test_model.predict(k) | |
print(np.argmax(prediction)) | |
from keras.datasets import mnist | |
from keras.models import load_model | |
import numpy as np | |
import sys | |
from skimage.io import imread, imsave | |
from skimage.util import invert | |
import matplotlib.image as mpimg | |
import matplotlib.pyplot as plt | |
np.set_printoptions(threshold=sys.maxsize) | |
img_width, img_height = 28, 28 | |
(X_train, y_train), (X_test, y_test) = mnist.load_data() | |
img = X_train[5589] | |
testing_img = imsave('img.png', invert(img)) | |
im = mpimg.imread('img.png') | |
plt.imshow(im, cmap='gray') | |
plt.show() | |
model = load_model('MNIST.h5') | |
im = np.array(im).reshape(1, 784) | |
predict = model.predict(im) | |
print(predict) | |
print(np.argmax(predict)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment