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()
help="Image index") args = parser.parse_args() return args args = parse_command_line_arguments() datadir = args.datadir k = args.k idx = args.index if idx == None: train = MNISTReader("%s/train-images-idx3-ubyte" % datadir, "%s/train-labels-idx1-ubyte" % datadir, preload=True) test = MNISTReader("%s/t10k-images-idx3-ubyte" % datadir, "%s/t10k-labels-idx1-ubyte" % datadir, preload=True) else: train = MNISTReader("%s/train-images-idx3-ubyte" % datadir, "%s/train-labels-idx1-ubyte" % datadir) test = MNISTReader("%s/t10k-images-idx3-ubyte" % datadir, "%s/t10k-labels-idx1-ubyte" % datadir) 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)
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()
help='Number of neighbors to use') parser.add_argument('-i', '--index', dest='index', action='store', type=int, help="Image index") args = parser.parse_args() return args args = parse_command_line_arguments() datadir = args.datadir k = args.k idx = args.index if idx == None: train = MNISTReader("%s/train-images-idx3-ubyte" % datadir, "%s/train-labels-idx1-ubyte" % datadir, preload=True) test = MNISTReader("%s/t10k-images-idx3-ubyte" % datadir, "%s/t10k-labels-idx1-ubyte" % datadir, preload=True) else: train = MNISTReader("%s/train-images-idx3-ubyte" % datadir, "%s/train-labels-idx1-ubyte" % datadir) test = MNISTReader("%s/t10k-images-idx3-ubyte" % datadir, "%s/t10k-labels-idx1-ubyte" % datadir) 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"
parser.add_argument('-o', '--outfile', metavar='FILE', dest='outfile', action='store', help='The file to save k-mean images to. If none is specified, the image is displayed interactively.') parser.add_argument('-t', '--threshold', dest='cutoff', action='store', type=float, default=100.0, help="Converge once the centroids move less than this threshold.") args = parser.parse_args() return args args = parse_command_line_arguments() k = args.k cutoff = args.cutoff print "Loading data..." test = MNISTReader("%s/t10k-images-idx3-ubyte" % args.datadir, "%s/t10k-labels-idx1-ubyte" % args.datadir) points = None for img in test.images(): if points is None: points = numpy.array([img.imgdata]) else: points = numpy.append(points, [img.imgdata], axis=0) km = KMeans(points, k) centroids = km.select_random_centroids() iteration=1 while True: print "Iteration #%i" % iteration