-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatasets.py
More file actions
72 lines (54 loc) · 2.28 KB
/
Copy pathdatasets.py
File metadata and controls
72 lines (54 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os, requests
from os.path import exists
import logging
logging.basicConfig(format="%(asctime)s - %(message)s")
logger = logging.getLogger()
logger.setLevel("INFO")
info = lambda message: logger.info(message)
import pandas as pd
import numpy as np
from tqdm import tqdm
def mnist():
"""Fetch, parse and return mnist data."""
if not exists("mnist"):
os.mkdir("mnist/")
if not exists("mnist/train.csv"):
info("downloading mnist training set")
r = requests.get("https://pjreddie.com/media/files/mnist_train.csv", stream=True)
contentlength = int(r.headers["Content-Length"])
nchunks = 420
chunk_size = contentlength // nchunks
total = nchunks if contentlength % nchunks == 0 else nchunks + 1
chunks = []
for i, chunk in tqdm(enumerate(r.iter_content(chunk_size=chunk_size)), total=total):
chunks.append(chunk)
text = b"".join(chunks).decode()
with open("mnist/train.csv", "w") as f:
f.write(text)
if not exists("mnist/test.csv"):
info("downloading mnist test set")
r = requests.get("https://pjreddie.com/media/files/mnist_test.csv", stream=True)
contentlength = int(r.headers["Content-Length"])
nchunks = 420
chunk_size = contentlength // nchunks
total = nchunks if contentlength % nchunks == 0 else nchunks + 1
chunks = []
for i, chunk in tqdm(enumerate(r.iter_content(chunk_size=chunk_size)), total=total):
chunks.append(chunk)
text = b"".join(chunks).decode()
with open("mnist/test.csv", "w") as f:
f.write(text)
names = ["label"] + [f"pixel{i}" for i in range(784)]
df = pd.read_csv("mnist/train.csv", names=names, dtype=np.float32)
label, pixels = df["label"], df.drop("label", axis=1)
X_train = pixels.values.reshape(-1, 28, 28) / 255
y_train = label.values
df = pd.read_csv("mnist/test.csv", names=names, dtype=np.float32)
label, pixels = df["label"], df.drop("label", axis=1)
X_test = pixels.values.reshape(-1, 28, 28) / 255
y_test = label.values
X_train.flags.writeable = False
y_train.flags.writeable = False
X_test.flags.writeable = False
y_test.flags.writeable = False
return X_train, y_train, X_test, y_test