import numpy as np from scipy import misc import pandas as pd import matplotlib.pyplot as plt from PIL import Image # Create first network with Keras from keras.models import Sequential from keras.layers import Dense import numpy from keras.models import load_model # fix random seed for reproducibility seed = 7 numpy.random.seed(seed) #loading the model from h5 file model = load_model('my_model.h5') # load the data which can be tested #Converting the mnist test set into an image test_images = pd.read_csv('test.csv').values dir = "images/" aList = ['images/test0.png','images/test1.png','images/test2.png','images/test4.png','images/test5.png','images/test6.png'] for i in range(6): #img_var = np.reshape(test_images[i], (-1, 28)) #print np.shape(img_var) #img_var = img_var.astype(np.uint8) #img = Image.fromarray(img_var) #img.save(dir + 'test'+str(i)+'.png') #read an image from the folder imgOld = misc.imread(aList[i],flatten=True, mode='L') #imgOld = misc.imread(dir + 'test'+str(i)+'.png',flatten=True, mode='L') img = misc.imresize(imgOld, (28, 28)) test_img = np.array([img]) #print test_img.shape X_test = test_img.reshape(test_img.shape[0], 1, 28, 28).astype('float32') X = X_test / 255 print ('------------------------------------------------------------------------\n') #print X # calculate predictions using the KERAS MODEL!!!!! predictions = model.predict(X) #print('predicted_lables({0})'.format(predictions)) print ('Predicted_Digit({0})'.format(np.argmax(predictions))) """ #read an image from the folder imgOld = misc.imread(dir + 'test'+str(i)+'.png',flatten=True) img = misc.imresize(imgOld, (28, 28)) test_img = np.array(img) test_img = test_img.astype(np.float) # convert from [0:255] => [0.0:1.0] test_images = np.multiply(test_img, 1.0 / 255.0) print('test_images({0[0]},{0[1]})'.format(test_images.shape)) test_images_f= np.ravel(test_images) test_images_n = np.reshape(test_images_f, (1,784)) print ('-d ------------------------------------------------------------------------\n') #print (test_images_n) # predict test set predicted_lables = predict.eval(feed_dict={x: test_images_n, keep_prob: 1.0}) print('predicted_lables({0})'.format(predicted_lables)) """ # round predictions #rounded = [round(x) for x in predictions] #print(rounded) #print('predicted_lables({0})'.format(predictions))