Last active
May 6, 2017 11:57
-
-
Save charlesreid1/436c51eae1e6a0d011d86f3796dec853 to your computer and use it in GitHub Desktop.
Python 3: Numpy Array from Generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ python3 numpy_array_timing.py | |
Using TensorFlow backend. | |
Approach 1 took 43.884194135665894 s | |
Approach 2 took 43.12157201766968 s | |
$ python3 numpy_array_timing.py | |
Using TensorFlow backend. | |
Approach 1 took 44.887431383132935 s | |
Approach 2 took 49.045594930648804 s | |
$ python3 numpy_array_timing.py | |
Using TensorFlow backend. | |
Approach 1 took 46.91734313964844 s | |
Approach 2 took 52.51308083534241 s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Setup of this script comes from | |
# https://github.com/dribnet/lfw_fuel/blob/master/example/run-lfw.py | |
from lfw_fuel import lfw | |
np.random.seed(1337) # for reproducibility | |
from keras.models import Sequential | |
from keras.layers.core import Dense, Dropout, Activation, Flatten | |
from keras.layers import Conv2D, MaxPooling2D | |
from keras.utils import np_utils | |
from scipy.misc import imresize | |
batch_size = 128 | |
nb_classes = 2 | |
nb_epoch = 12 | |
feature_width = 32 | |
feature_height = 32 | |
def cropImage(im): | |
im2 = np.dstack(im).astype(np.uint8) | |
# return centered 128x128 from original 250x250 (40% of area) | |
newim = im2[61:189, 61:189] | |
sized1 = imresize(newim[:,:,0:3], (feature_width, feature_height), interp="bicubic", mode="RGB") | |
sized2 = imresize(newim[:,:,3:6], (feature_width, feature_height), interp="bicubic", mode="RGB") | |
return np.asarray([sized1[:,:,0], sized1[:,:,1], sized1[:,:,2], sized2[:,:,0], sized2[:,:,1], sized2[:,:,2]]) | |
# the data, shuffled and split between train and test sets | |
(X_train, y_train), (X_test, y_test) = lfw.load_data("deepfunneled") | |
############## Timing Comparison ################ | |
import time | |
# Approach #1: Create an empty array and populate it | |
t0 = time.time() | |
temp = np.empty(np.shape(X_train)) | |
for i, el in enumerate(X_train): | |
temp[i] = el | |
t1 = time.time() | |
print("Approach 1 took {0} s".format(t1-t0)) | |
# Approach #2: Convert the map to a list, then the list to an array | |
t2 = time.time() | |
temp2 = np.asarray(list(map(cropImage,X_train))) | |
t3 = time.time() | |
print("Approach 2 took {0} s".format(t3-t2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment