forked from lazyprogrammer/machine_learning_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
executable file
·172 lines (130 loc) · 4.88 KB
/
util.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# https://deeplearningcourses.com/c/deep-learning-gans-and-variational-autoencoders
# https://www.udemy.com/deep-learning-gans-and-variational-autoencoders
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future
import os
import requests
import zipfile
import numpy as np
import pandas as pd
try:
# new version doesn't support
from scipy.misc import imread, imsave, imresize
except:
from PIL import Image
def imread(fn):
im = Image.open(fn)
return np.array(im)
def imsave(fn, arr):
im = Image.fromarray(arr)
im.save(fn)
def imresize(arr, sz):
im = Image.fromarray(arr)
im.resize(sz)
return np.array(im)
from glob import glob
from tqdm import tqdm
from sklearn.utils import shuffle
def get_mnist(limit=None):
if not os.path.exists('../large_files'):
print("You must create a folder called large_files adjacent to the class folder first.")
if not os.path.exists('../large_files/train.csv'):
print("Looks like you haven't downloaded the data or it's not in the right spot.")
print("Please get train.csv from https://www.kaggle.com/c/digit-recognizer")
print("and place it in the large_files folder.")
print("Reading in and transforming data...")
df = pd.read_csv('../large_files/train.csv')
data = df.values
# np.random.shuffle(data)
X = data[:, 1:] / 255.0 # data is from 0..255
Y = data[:, 0]
X, Y = shuffle(X, Y)
if limit is not None:
X, Y = X[:limit], Y[:limit]
return X, Y
def get_celeb(limit=None):
if not os.path.exists('../large_files'):
os.mkdir('../large_files')
# eventual place where our final data will reside
if not os.path.exists('../large_files/img_align_celeba-cropped'):
# check for original data
if not os.path.exists('../large_files/img_align_celeba'):
# download the file and place it here
if not os.path.exists('../large_files/img_align_celeba.zip'):
print("Downloading img_align_celeba.zip...")
download_file(
'0B7EVK8r0v71pZjFTYXZWM3FlRnM',
'../large_files/img_align_celeba.zip'
)
# unzip the file
print("Extracting img_align_celeba.zip...")
with zipfile.ZipFile('../large_files/img_align_celeba.zip') as zf:
zip_dir = zf.namelist()[0]
zf.extractall('../large_files')
# load in the original images
filenames = glob("../large_files/img_align_celeba/*.jpg")
N = len(filenames)
print("Found %d files!" % N)
# crop the images to 64x64
os.mkdir('../large_files/img_align_celeba-cropped')
print("Cropping images, please wait...")
for i in range(N):
crop_and_resave(filenames[i], '../large_files/img_align_celeba-cropped')
if i % 1000 == 0:
print("%d/%d" % (i, N))
# make sure to return the cropped version
filenames = glob("../large_files/img_align_celeba-cropped/*.jpg")
return filenames
def crop_and_resave(inputfile, outputdir):
# theoretically, we could try to find the face
# but let's be lazy
# we assume that the middle 108 pixels will contain the face
im = imread(inputfile)
height, width, color = im.shape
edge_h = int( round( (height - 108) / 2.0 ) )
edge_w = int( round( (width - 108) / 2.0 ) )
cropped = im[edge_h:(edge_h + 108), edge_w:(edge_w + 108)]
small = imresize(cropped, (64, 64))
filename = inputfile.split('/')[-1]
imsave("%s/%s" % (outputdir, filename), small)
def scale_image(im):
# scale to (-1, +1)
return (im / 255.0)*2 - 1
def files2images_theano(filenames):
# theano wants images to be of shape (C, D, D)
# tensorflow wants (D, D, C) which is what scipy imread
# uses by default
return [scale_image(imread(fn).transpose((2, 0, 1))) for fn in filenames]
def files2images(filenames):
return [scale_image(imread(fn)) for fn in filenames]
# functions for downloading file from google drive
def save_response_content(r, dest):
# unfortunately content-length is not provided in header
total_iters = 1409659 # in KB
print("Note: units are in KB, e.g. KKB = MB")
# because we are reading 1024 bytes at a time, hence
# 1KB == 1 "unit" for tqdm
with open(dest, 'wb') as f:
for chunk in tqdm(
r.iter_content(1024),
total=total_iters,
unit='KB',
unit_scale=True):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
def get_confirm_token(response):
for key, value in response.cookies.items():
if key.startswith('download_warning'):
return value
return None
def download_file(file_id, dest):
drive_url = "https://docs.google.com/uc?export=download"
session = requests.Session()
response = session.get(drive_url, params={'id': file_id}, stream=True)
token = get_confirm_token(response)
if token:
params = {'id': file_id, 'confirm': token}
response = session.get(drive_url, params=params, stream=True)
save_response_content(response, dest)