def query_digit(digit): host, port = "localhost", 4567 con = httplib.HTTPConnection(host, port) params = json.dumps({"data": digit}) con.request("POST", "/digit", params) response = con.getresponse() print "For digit:%s\nReceived prediction response [%s]\n" % (MNIST.display(digit), response.read())
def mnistDataRandomTest(dataset, path): mndata = MNIST(path) images, labels = loadMNISTData(dataset, path, mndata) index = random.randrange(0, len(images)) print(mndata.display(images[index]))
def test(self, n_tests): print("Starting...") mndata = MNIST("MNIST_db") images, labels = mndata.load_testing() good_results = 0 for i in range(n_tests): print("************ Test {} ************".format(i)) rand = int(random() * 10000) ans = [0] * 9 ans.insert(labels[rand], 1) out = self.process(images[rand]) print(mndata.display(images[rand])) for o, a in zip(out, ans): print("{} : {}".format(o, a)) out = map(lambda y: y.x, out) choice = out.index(max(out)) print("Choice : {}".format(choice)) print("\n") if choice == labels[rand]: good_results += 1 print("OK!\n") print("####################################################") print("############### Results ###################") print("####################################################") print("{}/{} ({} %)".format(good_results, n_tests, float(good_results) / n_tests * 100))
def query_digit(digit): host, port = "localhost", 4567 con = httplib.HTTPConnection(host, port) params = json.dumps({"data": digit}) con.request("POST", "/digit", params) response = con.getresponse() print "For digit:%s\nReceived prediction response [%s]\n" % ( MNIST.display(digit), response.read())
def get_MNIST(self, type, printfirst=False): mndata = MNIST('C:/Users/Diana/Documents/Models/Database') if type == 'train': images, labels = mndata.load_training() if type == 'test': images, labels = mndata.load_testing() if printfirst: print(mndata.display(images[0])) return np.asarray(images), np.asarray(labels), mndata
def draw_random_misclassification(truth_array, prediction, test_label, test_data): """ Prints the prediction, label and digit for a random misclassified sample """ incorrect_idx = [ idx for idx, is_true in enumerate(truth_array) if not is_true ] n = incorrect_idx[np.random.randint(0, len(incorrect_idx))] print "predicted [%s]\nlabeled [%s]\nraw data:\n%s" % ( prediction[n].argmax(), test_label[n], MNIST.display(test_data[n]))
def query_digit(digit, host=None, port=None): """ Issues HTTP POST to host, port with digit array Expects a digit in the response """ if not host or not port: host, port = "localhost", 4567 con = httplib.HTTPConnection(host, port) params = json.dumps({"data": digit}) con.request("POST", "/digit", params) response = con.getresponse() print "For digit:%s\nReceived prediction response [%s]\n" % (MNIST.display(digit), response.read())
def query_digit(digit, host=None, port=None): """ Issues HTTP POST to host, port with digit array Expects a digit in the response """ if not host or not port: host, port = "localhost", 4567 con = httplib.HTTPConnection(host, port) params = json.dumps({"data": digit}) con.request("POST", "/digit", params) response = con.getresponse() print "For digit:%s\nReceived prediction response [%s]\n" % ( MNIST.display(digit), response.read())
def test(outputsEnabled=False): pred = predict(testImgs) testPred = softmax(pred) # returns the "guesses" of the network errorsAt = np.array(np.where(np.not_equal( testLabels, testPred))).T #the samples the network was mistaken on if (outputsEnabled): for i in np.nditer(errorsAt): print(MNIST.display(testImgs[i])) print("network Output: " + str(testPred[i])) print("correct Output: " + str(testLabels[i])) print("networks whole Output: ") print(pred[i]) return (1 - (len(errorsAt) / len(testPred))) * 100
def mnist_client(): num_img = randint(0, 9) TF_MODEL_SERVER_HOST = os.getenv("TF_MODEL_SERVER_HOST", "127.0.0.1") TF_MODEL_SERVER_PORT = int(os.getenv("TF_MODEL_SERVER_PORT", 9000)) TF_DATA_DIR = os.getenv("TF_DATA_DIR", "/tmp/data/") TF_MNIST_IMAGE_PATH = os.getenv("TF_MNIST_IMAGE_PATH", "data/" + str(num_img) + ".png") TF_MNIST_TEST_IMAGE_NUMBER = int( os.getenv("TF_MNIST_TEST_IMAGE_NUMBER", -1)) if TF_MNIST_IMAGE_PATH != None: raw_image = Image.open(TF_MNIST_IMAGE_PATH) int_image = numpy.array(raw_image) image = numpy.reshape(int_image, 784).astype(numpy.float32) elif TF_MNIST_TEST_IMAGE_NUMBER > -1: test_data_set = input_data.read_data_sets(TF_DATA_DIR, one_hot=True).test image = test_data_set.images[TF_MNIST_TEST_IMAGE_NUMBER] else: test_data_set = input_data.read_data_sets(TF_DATA_DIR, one_hot=True).test image = random.choice(test_data_set.images) channel = implementations.insecure_channel(TF_MODEL_SERVER_HOST, TF_MODEL_SERVER_PORT) stub = prediction_service_pb2.beta_create_PredictionService_stub(channel) request = predict_pb2.PredictRequest() request.model_spec.name = "mnist" request.model_spec.signature_name = "serving_default" request.inputs['x'].CopyFrom( tf.contrib.util.make_tensor_proto(image, shape=[1, 28, 28])) result = stub.Predict(request, 10.0) # 10 secs timeout logging.info(MNIST.display(image, threshold=0)) logging.info("Your model says the above number is... %d!" % result.outputs["classes"].int_val[0]) if (num_img == result.outputs["classes"].int_val[0]): #logging.info(colored("TEST PASSED!!!", 'green')) logging.info("TEST PASSED!!!")
class Image(IPreprocessor): def __init__(self, image, imageID): self.__mnist_data = MNIST() self._image = image self.__id = imageID self._description = "Image {0}".format(self.__id) def print_image(self): print(self.__mnist_data.display(self._image)) def get_image_arr(self): return self._image[0] def get_image_id(self): return self.__id def get_shape(self): h = int(math.sqrt(len(self.get_image_arr()))) return (h,h) def apply(self): return self._description
import random import numpy as np import pickle RAW_DATA_DIR = 'data/ubyte' PARSED_IMAGE_DIR = 'data/' DATA_PICKLE_NAME = 'data/pickles/test_data.pickle' LABELS_PICKLE_NAME = 'data/pickles/test_labels.pickle' mndata = MNIST(RAW_DATA_DIR) images, labels = mndata.load_testing() data = [] for i in range(0, len(images)): ubyte_img = mndata.display(images[i]).split('\n')[1:] image_as_list = [] for j in range(0, len(ubyte_img)): img_line_list = [] ubyte_img_line = ubyte_img[j] for k in range(0, len(ubyte_img_line)): if (ubyte_img_line[k] == '.'): # img_line_list.append(0) image_as_list.append(0) else: # img_line_list.append(255) image_as_list.append(255) # image_as_list.append(img_line_list) image_as_np = np.reshape(np.array(image_as_list), (28, 28, 1)) data.append(image_as_list) data_np = np.array(data)
from mnist import MNIST import random import numpy as np import normalizer mndata = MNIST('../../training_data/MNISTData') images, labels = mndata.load_testing() index = random.randrange(0, 50) print(mndata.display(images[45])) print(images[45]) ## Turn flat list into 2D array arr = np.array(images[45]).reshape(-1, 28) normalized_input = normalizer.normalizeInput_training(arr) print(arr)
init = tf.global_variables_initializer() sess.run(init) saver = tf.train.Saver() for i in range(1000): batch_xs, batch_ys = mnist.train.next_batch(500) sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys}) # save_path = saver.save(sess, "./mnistsoftmax/my_model_final.ckpt") import cv2 images, labels = mndata.load_testing() num = 8000 image = images[num] label = labels[num] # 打印图片 print(mndata.display(image)) print('这张图片的实际数字是: ' + str(label)) def inverse_color(image): height,width = image.shape img2 = image.copy() for i in range(height): for j in range(width): img2[i,j] = (255-image[i,j]) return img2 # 测试新图片,并输出预测值 # mp = mpimg.imread('./1.png') im = Image.open('./1.png').convert('L')
print("Target accuracy not achieved (" + str(correct/(correct+wrong)) + " < " + str(TARGET_ACCURACY) + ")") #testing correct = 0 wrong = 0 for i in range(testImages.shape[0]): #convert the current image to a column vector curImage = numpy.transpose(testImages[i]) #compute output output = numpy.add(numpy.matmul(layerweights[0], curImage), layerbiases[0]) for k in range(1, len(LAYER_ARRAY)): output = numpy.add(numpy.matmul(layerweights[k], ReLU(output)), layerbiases[k]) guess = ReLU(output).argmax() #display number and guess print(mndata.display(testImagesInput[i])) print("Guess: ") print(guess) #add to stats if (guess == testLabels[i]): correct += 1 else: wrong += 1 print("Accuracy: ") print(correct / (correct + wrong))
from mnist import MNIST #you can find python-mnist source code on https://github.com/sorki/python-mnist datahandler = MNIST() #change for data path train_data = datahandler.load_training() for i in range(59989, 60000): print('IMAGE: {}'.format(i+1)) img = train_data[0][i] print(datahandler.display(img)) print('LABEL: {}\n\n\n'.format(train_data[1][i]))
# TODO: We are implementing deep learning mnist database. The MNIST database includes digits and alphabets. The final output should be the indicatoin of what the given character. Anyhow as on july 24 i am trying to display the character. ## libraries... importing mnist. MNIST is minimal package that doesn't contain any details when tried with help. ##importing os library to migrate the directory from one place to another place ##the code is copied and referred from https://stackoverflow.com/questions/40427435/extract-images-from-idx3-ubyte-file-or-gzip-via-python from mnist import MNIST import os #os.chdir('.') this is intended to change the directory from a different directory to the current directory which is obselete since the same directory doesn't require because already the path is given. However with the following commented code we can see all the files stored ''' for file in os.listdir(os.curdir): print file''' mndata = MNIST( os.getcwd() ) # here '.' and os.getcwd gives the same output which is the current working directory. images, labels = mndata.load_training() #images,labels=mndata.load_testing() print(mndata.display(images[5])) # this displays the 5th image in the array. ##Conclusion we can do it, still we are not able to load the dataset and experiment.
from mnist import MNIST import random mndata = MNIST('samples') images, labels = mndata.load_training(r'..\train\train-images-idx3-ubyte') index = random.randrange(0, len(images)) print(mndata.display(images[index]))
vote[2] += 1 elif (trainLabels[nearestNeighbors[j][1]] == 3): vote[3] += 1 elif (trainLabels[nearestNeighbors[j][1]] == 4): vote[4] += 1 elif (trainLabels[nearestNeighbors[j][1]] == 5): vote[5] += 1 elif (trainLabels[nearestNeighbors[j][1]] == 6): vote[6] += 1 elif (trainLabels[nearestNeighbors[j][1]] == 7): vote[7] += 1 elif (trainLabels[nearestNeighbors[j][1]] == 8): vote[8] += 1 elif (trainLabels[nearestNeighbors[j][1]] == 9): vote[9] += 1 #the result of the vote guess = vote.index(max(vote)) #display number and guess print(mndata.display(testImages[i])) print("Guess: ") print(guess) #add to stats if (guess == testLabels[i]): correct += 1 else: wrong += 1 print("Accuracy: ") print(correct / (correct + wrong))
if TF_MNIST_IMAGE_PATH != None: raw_image = Image.open(TF_MNIST_IMAGE_PATH) int_image = numpy.array(raw_image) image = numpy.reshape(int_image, 784).astype(numpy.float32) elif TF_MNIST_TEST_IMAGE_NUMBER > -1: test_data_set = input_data.read_data_sets(TF_DATA_DIR, one_hot=True).test image = test_data_set.images[TF_MNIST_TEST_IMAGE_NUMBER] else: test_data_set = input_data.read_data_sets(TF_DATA_DIR, one_hot=True).test image = random.choice(test_data_set.images) channel = implementations.insecure_channel( TF_MODEL_SERVER_HOST, TF_MODEL_SERVER_PORT) stub = prediction_service_pb2.beta_create_PredictionService_stub(channel) request = predict_pb2.PredictRequest() request.model_spec.name = "mnist" request.model_spec.signature_name = "predict_images" request.inputs['images'].CopyFrom( tf.contrib.util.make_tensor_proto(image, shape=[1, 784])) result = stub.Predict(request, 10.0) # 10 secs timeout # print(result) print(MNIST.display(image, threshold=0)) response = numpy.array( result.outputs['scores'].float_val) prediction = numpy.argmax(response) # print(prediction) print("Your model says the above number is... %d!" % prediction)
image_training, label_training = mndata.load_training() image_testing, label_testing = mndata.load_testing() #Lorsque l'on souhaite train/guess une image, on la convertit d'abord en nombre entre 0 et 1 #Boucle qui prépare et envoi les donnés au training for i in range(0, 3000): image_a_transformer = image_training[i] image_transformer = [[pixel / 255] for pixel in image_a_transformer] label_a_transformer = label_training[i] array_label = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] array_label[label_a_transformer] = 1 array_label = [[element] for element in array_label] label_transformer = numpy.asmatrix(array_label) digit_network.train(image_transformer, label_transformer) #Boucle qui prépare l'Envoi du test set #Display égalment des statistiques for i in range(0, 10): image_a_transformer = image_testing[i] image_transformer = [[pixel / 255] for pixel in image_a_transformer] label = label_testing[i] guess = numpy.round(digit_network.guess(image_transformer), 2) print(mndata.display(image_a_transformer)) print("Guess:", guess) time.sleep(0.5)
# nn = NeuralNet(images, labels, images.shape[1], 100, 100, labels.shape[1]) # nn.train_neural_network(images, labels) np_testing_X = np.array([images[i] for i in range(len(images))]) np_testing_Y = np.zeros((len(image_label), 10)) for i in range(np_testing_Y.shape[0]): index = image_label[i] np_testing_Y[i, index] = 1 nn = NeuralNet.NeuralNetwork(np_testing_X, np_testing_Y, np_testing_X.shape[1], 800, np_testing_Y.shape[1]) # nn.train_neural_network(X_test, y_test) # Just gonna save the theta in a file. # nn.save_theta() nn.load_theta("theta0.csv", "theta1.csv") # Pick a random index. index = random.randrange(0, len(np_testing_X)) print(mndata.display(np_testing_X[index])) # Make a prediction print("Prediction:", nn.predict(np_testing_X[index].reshape((-1, 1))))
from mnist import MNIST mndata = MNIST('samples') images, labels = mndata.load_testing() print(mndata.display(images[0])) hexdata = [format(x, 'x') for x in images[0]] file = open("imagedata_7.txt", "w+") for item in hexdata: file.write("%s\n" % item) file.close()
# for default always use training set if anyone is especified if not args.training and not args.testing: args.training = True if args.training: img, label = mn.load_training() else: img, label = mn.load_testing() if args.id: which = args.id else: which = random.randrange(0, len(label)) print('Mostrarndo num: {}'.format(label[which])) print(mn.display(img[which])) mn.plot_mnist_digit(img[which], label=label[which]) if args.training: print(type(mn.train_images)) print(type(mn.train_labels)) print(mn.train_images.shape) print(mn.train_labels.shape) else: print(type(mn.test_images)) print(type(mn.test_labels)) print(mn.test_images.shape) print(mn.test_labels.shape) import numpy
"""If you want to some examples. You need to download datasets from http://yann.lecun.com/exdb/mnist/ and put them into samples folder""" from mnist import MNIST import sys, os from contextlib import ExitStack mndata = MNIST('samples') images, labels = mndata.load_training() # or # images, labels = mndata.load_testing() COUNT_OF_SAMPLES = 20 with ExitStack() as stack: files = [stack.enter_context(open(fname, "w")) for fname in "0123456789"] indexes = {i: 0 for i in range(10)} for i in range( len(images) ): # index = random.randrange(0, len(images)) # choose an index ;-) digit = labels[i] if indexes[digit] < COUNT_OF_SAMPLES: files[digit].write(mndata.display(images[i])) files[digit].write("\n") indexes[digit] += 1
p[j] = multivariate_normal(means[j], smoothed_matrix[j]) err_rate = multivariate_gaussian(validations, validx, validy, p) err_rates[i] = err_rate min_index = np.argmin(err_rates) print(err_rates) return min_index #best_c = find_best_cofactor(c) #print(best_c) results = [None] * len(testY) smoothed_matrix = np.array([None] * 10) smooth_matrix(covs, c[9], smoothed_matrix) p = np.array([None] * 10) for j in range(len(p)): p[j] = multivariate_normal(means[j], smoothed_matrix[j]) misclassified = [] err_rate = multivariate_gaussian(results, testX, testY, p, misclassified) print(err_rate) top5misclassfied = misclassified[0:5] for i in range(len(top5misclassfied)): print(mndata.display(testX[top5misclassfied[i]])) print(results[top5misclassfied[i]]) densities = np.array([None] * 10) probX = 0 for j in range(len(densities)): densities[j] = p[j].logpdf(testX[top5misclassfied[i]]) probX += densities[j] for k in range(len(densities)): print(float(densities[k]) / float(probX))
def printImages(images, labels): for i in range(len(images)): print(MNIST.display(images[i], threshold=0.5))
TF_MNIST_IMAGE_PATH = os.getenv("TF_MNIST_IMAGE_PATH", None) TF_MNIST_TEST_IMAGE_NUMBER = int(os.getenv("TF_MNIST_TEST_IMAGE_NUMBER", -1)) if TF_MNIST_IMAGE_PATH != None: raw_image = Image.open(TF_MNIST_IMAGE_PATH) int_image = numpy.array(raw_image) image = numpy.reshape(int_image, 784).astype(numpy.float32) elif TF_MNIST_TEST_IMAGE_NUMBER > -1: test_data_set = input_data.read_data_sets(TF_DATA_DIR, one_hot=True).test image = test_data_set.images[TF_MNIST_TEST_IMAGE_NUMBER] else: test_data_set = input_data.read_data_sets(TF_DATA_DIR, one_hot=True).test image = random.choice(test_data_set.images) channel = implementations.insecure_channel(TF_MODEL_SERVER_HOST, TF_MODEL_SERVER_PORT) stub = prediction_service_pb2.beta_create_PredictionService_stub(channel) request = predict_pb2.PredictRequest() request.model_spec.name = "mnist" request.model_spec.signature_name = "serving_default" request.inputs['x'].CopyFrom( tf.contrib.util.make_tensor_proto(image, shape=[1, 28, 28])) result = stub.Predict(request, 10.0) # 10 secs timeout print(result) print(MNIST.display(image, threshold=0)) print("Your model says the above number is... %d!" % result.outputs["classes"].int_val[0])
#PART1 #test du model sur une image MNIST quelconque du set original num = randint(0, mnist.test.images.shape[0]) #Pour trouver un nombre random img = mnist.test.images[num] #Pour prendre une image Random classification = sess.run(tf.argmax(y, 1), feed_dict={x: [img]}) print("Prediction: ", classification[0]) print("Realite: ", np.argmax(mnist.test.labels[num])) mndata = MNIST('MNIST_data') images, labels = mndata.load_testing() print(mndata.display(images[num])) #PART2 #2: Using our model to classify MNIST digit from a custom image: # create an an array where we can store 1 picture images = np.zeros((1, 784)) # and the correct values correct_vals = np.zeros((1, 10)) # read the image gray = cv2.imread("my_digit.png", 0) #0=cv2.CV_LOAD_IMAGE_GRAYSCALE #must be .png! # rescale it
def draw_random_misclassification(truth_array, prediction, test_label, test_data): """ Prints the prediction, label and digit for a random misclassified sample """ incorrect_idx = [idx for idx, is_true in enumerate(truth_array) if not is_true] n = incorrect_idx[np.random.randint(0, len(incorrect_idx))] print "predicted [%s]\nlabeled [%s]\nraw data:\n%s" % (prediction[n].argmax(), test_label[n], MNIST.display(test_data[n]))
from mnist import MNIST from sklearn.neural_network import MLPClassifier from sklearn.metrics import classification_report, confusion_matrix import random # 1. 载入MNIST print("1. 载入MNIST...") mndata = MNIST('dataset') train_images, train_labels = mndata.load_training() test_images, test_labels = mndata.load_testing() # 随机显示一个手写图 print("载入完成:%d训练样本,%d测试样本。" % (len(train_images), len(test_images))) print("随机显示一条训练集数据:") print(mndata.display(train_images[random.randrange(0, len(train_images))])) # 2. 创建神经网络模型 # TODO 测试特征规范化的影响 # print("进行特征规范化...") # scaler = StandardScaler() # scaler.fit(X_train) # X_train = scaler.transform(X_train) # X_test = scaler.transform(X_test) # print("特征规范化完成。") print("2. 创建神经网络(类型为Multi-layer Classifier,中间层维度:(5, 5, 5),最大迭代数1000)") mlp = MLPClassifier(hidden_layer_sizes=(50, 50, 50), max_iter=1000, verbose=2) print('神经网络训练中...') mlp.fit(train_images, train_labels) print('神经网络已训练完成。') print('开始预测测试集...') predictions = mlp.predict(test_images) print('已预测测试集。')
X_train, y_train, X_test, y_test = dataProvisioning(dataset_path) print("X_train.shape = ", X_train.numpy().shape) print("y_train.shape = ", y_train.numpy().shape) print("X_test.shape = ", X_test.numpy().shape) print("y_test.shape = ", y_test.numpy().shape) train_maxcount = y_train.numpy().shape[0] select_input = int( input( f"Select the number of image you want to see: (Range from 0 to {train_maxcount - 1}) " )) select_imgmap = X_train.numpy()[select_input] select_value = y_train.numpy()[select_input] print( f"The number of image you selected is: {select_value}({select_input})") print(MNIST.display(select_imgmap)) imageRendering(select_imgmap, select_value) sys.exit(0) # %% import tensorflow as tf y_true = tf.constant([0, 0], dtype=tf.float64) y_pred = tf.constant([[1, 0], [0, 1]], dtype=tf.float64) print(tf.keras.metrics.sparse_categorical_crossentropy(y_true, y_pred)) print(tf.keras.metrics.sparse_categorical_accuracy(y_true, y_pred)) # %%
from mnist import MNIST from Library.p307_001_mnistData import imageRendering if __name__ == "__main__": imgID = random.randint(0, 10001) testdata_path = "./Data/mnist_dataset" model_path = "./Model/handwriting_models.yyy" testdata = MNIST(testdata_path, return_type="numpy") test_imgs, test_labs = testdata.load_testing() modelS = tf.keras.models.load_model(model_path) test_img = tf.constant([test_imgs[imgID].tolist()], dtype=tf.float64) testResult = modelS.predict(x=test_img) print(testResult) print(MNIST.display(test_imgs[imgID].tolist())) preditValue = tf.argmax(testResult[0]) print( f"PreditValue is: {preditValue}, with probability: {testResult[0][preditValue]}" ) imageRendering(test_imgs[imgID], test_labs[imgID]) sys.exit(0) # %%