In input_data.py erledigen diese beiden Funktionen den Hauptjob.
1. Download
def maybe_download(filename, work_directory):
"""Download the data from Yann's website, unless it's already here."""
if not os.path.exists(work_directory):
os.mkdir(work_directory)
filepath = os.path.join(work_directory, filename)
if not os.path.exists(filepath):
filepath, _ = urlretrieve(SOURCE_URL + filename, filepath)
statinfo = os.stat(filepath)
print('Succesfully downloaded', filename, statinfo.st_size, 'bytes.')
return filepath
2 Bild nparray
def extract_images(filename):
"""Extract the images into a 4D uint8 numpy array [index, y, x, depth]."""
print('Extracting', filename)
with gzip.open(filename) as bytestream:
magic = _read32(bytestream)
if magic != 2051:
raise ValueError(
'Invalid magic number %d in MNIST image file: %s' %
(magic, filename))
num_images = _read32(bytestream)
rows = _read32(bytestream)
cols = _read32(bytestream)
buf = bytestream.read(rows * cols * num_images)
data = numpy.frombuffer(buf, dtype=numpy.uint8)
data = data.reshape(num_images, rows, cols, 1)
return data
Basierend auf Ihren Daten-Set und Standort, können Sie anrufen:
local_file = maybe_download(TRAIN_IMAGES, train_dir)
train_images = extract_images(local_file)
bei https://github.com/nlintz/TensorFlow-Tutorials/blob/master/input_data.py den vollständigen Quellcode anzeigen .
Haben Sie input_data.py ausgecheckt? Ich denke, Sie werden einige Ideen aus der Datei bekommen. –
Ich überprüfe es. https://github.com/tensorflow/tensorflow/blob/r0.8/tensorflow/examples/tutorials/mnist/input_data.py Aber ich verstehe nicht, wie man die Daten installiert und analysiert. –
Das Skript lädt und importiert das Dataset automatisch. Ich möchte es selbst machen. –