help='Directory containing the MNIST files') parser.add_argument('-d', '--dataset', dest='dataset', action='store', required=True, choices = ("TEST","TRAIN"), help='Dataset to use (testing or training)') parser.add_argument('-i', '--index', dest='index', action='store', type=int, required=True, help="Image index") args = parser.parse_args() return args args = parse_command_line_arguments() datadir = args.datadir dataset = args.dataset idx = args.index if dataset == "TRAIN": data = MNISTReader("%s/train-images-idx3-ubyte" % datadir, "%s/train-labels-idx1-ubyte" % datadir) elif dataset == "TEST": data = MNISTReader("%s/t10k-images-idx3-ubyte" % datadir, "%s/t10k-labels-idx1-ubyte" % datadir) img = data.get_image(idx) print img.as_asciiart() print "Labelled as %d" % img.label img.as_image().show()
if idx == None: i = 0 for p, known_label in test.images(as_array=True): label, nearest = knn(p, train.images(as_array=True), k) if label == known_label: x="" else: x="XXX" print "%i %i %i %s" % (i, known_label, label, x) sys.stdout.flush() i+=1 else: img = test.get_image(idx) label, nearest = knn(img.as_array(), train.images(as_array=True), k) print "K-NEAREST NEIGHBORS" print "============================================================" print for dist, nearpoint, nearlabel in nearest: print MNISTImage(nearpoint, nearlabel, 28, 28).as_asciiart() print print "CLASSIFIED IMAGE" print "============================================================" print print img.as_asciiart() print
if idx == None: i = 0 for p, known_label in test.images(as_array=True): label, nearest = knn(p, train.images(as_array=True), k) if label == known_label: x = "" else: x = "XXX" print "%i %i %i %s" % (i, known_label, label, x) sys.stdout.flush() i += 1 else: img = test.get_image(idx) label, nearest = knn(img.as_array(), train.images(as_array=True), k) print "K-NEAREST NEIGHBORS" print "============================================================" print for dist, nearpoint, nearlabel in nearest: print MNISTImage(nearpoint, nearlabel, 28, 28).as_asciiart() print print "CLASSIFIED IMAGE" print "============================================================" print print img.as_asciiart() print
dest='index', action='store', type=int, required=True, help="Image index") args = parser.parse_args() return args args = parse_command_line_arguments() datadir = args.datadir dataset = args.dataset idx = args.index if dataset == "TRAIN": data = MNISTReader("%s/train-images-idx3-ubyte" % datadir, "%s/train-labels-idx1-ubyte" % datadir) elif dataset == "TEST": data = MNISTReader("%s/t10k-images-idx3-ubyte" % datadir, "%s/t10k-labels-idx1-ubyte" % datadir) img = data.get_image(idx) print img.as_asciiart() print "Labelled as %d" % img.label img.as_image().show()