Compare commits

9 Commits

Author SHA1 Message Date
b2ec17a2a1 Merge branch 'master' of github.com:charlesreid1/lfw_fuel
* 'master' of github.com:charlesreid1/lfw_fuel:
  Fix import statement and argument name to match latest Keras API.
  Update example LFW CNN to use latest Keras API. → → Pretty sure these are not equivalent, but I had some trouble → understanding the prior conv. neural network, so I did my best → to come up with a one-to-one translation. Since it is just a → lightweight example, no biggie smalls.
  Update map() call to work with Python 2 or Python 3.
2017-05-07 18:07:51 -07:00
15cda53bad Read CSV file as strings, not bytes.
If CSV file is opened/read using bytes flag, it causes this error:

    _csv.Error: iterator should return strings, not bytes (did you open the
    file in text mode?)

Python 3 will not do any decoding when reading the file as bytes ('rb').
Relevant documentation:

    https://docs.python.org/3.6/library/functions.html#open

This change has been tested against both Python 2 and Python 3
and works with both.
2017-05-07 17:57:15 -07:00
2c09f8d00e Expand map into list explicitly for Python 3 compatibility. 2017-05-07 17:12:09 -07:00
3e33585f11 Fix import statement and argument name to match latest Keras API. 2017-05-06 05:52:45 -07:00
443e1976bb Update example LFW CNN to use latest Keras API.
→
→ Pretty sure these are not equivalent, but I had some trouble
→ understanding the prior conv. neural network, so I did my best
→ to come up with a one-to-one translation. Since it is just a
→ lightweight example, no biggie smalls.
2017-05-06 04:49:36 -07:00
0c99fbc020 Update map() call to work with Python 2 or Python 3.
The map() call that applies the crop function to the training data
is automatically applied on the fly in Python 2,
but returns a generator in Python 3.
This creates problems when converting the resulting function map
to a numpy array - the numpy array is empty in Python 3.
This fixes the problem by explicitly converting the generator
to a list. This is about as fast as pre-allocating an empty numpy array
and populating it in a for loop. See https://gist.github.com/charlesreid1/436c51eae1e6a0d011d86f3796dec853
2017-05-06 04:33:13 -07:00
Tom White
31680f6fa0 updated readme info on rebuilding from fuel-{download,convert}
Also rolled version number.
2015-09-13 12:15:26 -07:00
Tom White
c09d8849c9 updated subparsers to latest proposed api in mila-udem/fuel#214 2015-08-16 21:45:45 -07:00
Tom White
82e7f75686 Changes for native fuel download/convert support
Cosmetic changes to fuel hooks that should allow
lfw_fuel dataset to be downloaded and converted
by fuel-download and fuel-convert directly in
an upcoming fuel release.
2015-08-12 00:26:52 -07:00
5 changed files with 38 additions and 27 deletions

View File

@@ -95,21 +95,28 @@ keras project.
python setup.py install python setup.py install
``` ```
You can also rebuild the hdf5 files from scratch by running the You can also rebuild the hdf5 files from scratch by running
`kero-download` and `kero-convert` scripts, which are currently `fuel-download` and `fuel-convert` with updated settings for
part of the kerosene installation. For example: `EXTRA_DOWNLOADERS` and `EXTRA_CONVERTERS`.
```bash ```bash
kero-download lfw_fuel/lfw.py FUEL_EXTRA_DOWNLOADERS="lfw_fuel" fuel-download lfw
kero-convert lfw_fuel/lfw.py FUEL_EXTRA_CONVERTERS="lfw_fuel" fuel-convert lfw
``` ```
This will convert the original version of lfw, but funneled and This will convert the original version of lfw, but funneled and
deepfunneled formats are also supported: deepfunneled formats are also supported:
```bash ```bash
kero-download lfw_fuel/lfw.py --format deepfunneled FUEL_EXTRA_DOWNLOADERS="lfw_fuel" fuel-download lfw --format deepfunneled
kero-convert lfw_fuel/lfw.py --format deepfunneled FUEL_EXTRA_CONVERTERS="lfw_fuel" fuel-convert lfw --format deepfunneled
```
These settings can also be set in the `~/.fuelrc` file:
```
extra_downloaders: ['lfw_fuel']
extra_converters: ['lfw_fuel']
``` ```
## License ## License

View File

@@ -5,7 +5,7 @@ np.random.seed(1337) # for reproducibility
from keras.models import Sequential from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.utils import np_utils from keras.utils import np_utils
from scipy.misc import imresize from scipy.misc import imresize
@@ -37,8 +37,8 @@ def cropImage(im):
# the data, shuffled and split between train and test sets # the data, shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = lfw.load_data("deepfunneled") (X_train, y_train), (X_test, y_test) = lfw.load_data("deepfunneled")
# crop features # crop features
X_train = np.asarray(map(cropImage, X_train)) X_train = np.asarray(list(map(cropImage, X_train)))
X_test = np.asarray(map(cropImage, X_test)) X_test = np.asarray(list(map(cropImage, X_test)))
# print shape of data while model is building # print shape of data while model is building
print("{1} train samples, {2} channel{0}, {3}x{4}".format("" if X_train.shape[1] == 1 else "s", *X_train.shape)) print("{1} train samples, {2} channel{0}, {3}x{4}".format("" if X_train.shape[1] == 1 else "s", *X_train.shape))
@@ -50,24 +50,24 @@ Y_test = np_utils.to_categorical(y_test, nb_classes)
model = Sequential() model = Sequential()
model.add(Convolution2D(32, 6, 3, 3, border_mode='full')) model.add(Conv2D(32, (3,3), input_shape=(6,32,32), padding='valid'))
model.add(Activation('relu')) model.add(Activation('relu'))
model.add(Convolution2D(32, 32, 3, 3)) model.add(Conv2D(32, (3,3), input_shape=(6,32,32), padding='valid'))
model.add(Activation('relu')) model.add(Activation('relu'))
model.add(MaxPooling2D(poolsize=(2, 2))) model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25)) model.add(Dropout(0.25))
model.add(Flatten()) model.add(Flatten())
model.add(Dense(feature_width*feature_height*8, 128)) model.add(Dense(128))
model.add(Activation('relu')) model.add(Activation('relu'))
model.add(Dropout(0.5)) model.add(Dropout(0.5))
model.add(Dense(128, nb_classes)) model.add(Dense(nb_classes))
model.add(Activation('softmax')) model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adadelta') model.compile(loss='categorical_crossentropy', metrics=['binary_accuracy'], optimizer='adadelta')
model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=1, validation_data=(X_test, Y_test)) model.fit(X_train, Y_train, batch_size=batch_size, epochs=nb_epoch, verbose=1, validation_data=(X_test, Y_test))
score = model.evaluate(X_test, Y_test, show_accuracy=True, verbose=0) score = model.evaluate(X_test, Y_test, verbose=0)
print('Test score:', score[0]) print('Test score:', score[0])
print('Test accuracy:', score[1]) print('Test accuracy:', score[1])

View File

@@ -0,0 +1,4 @@
from lfw_fuel import lfw
all_downloaders = (('lfw', lfw.download_subparser),)
all_converters = (('lfw', lfw.convert_subparser),)

View File

@@ -51,7 +51,7 @@ def resolve_filename(format):
def downloader_wrapper(format, directory, **kwargs): def downloader_wrapper(format, directory, **kwargs):
# add the right format file to the download list # add the right format file to the download list
files.insert(0, "{}.tgz".format(resolve_filename(format))) files.insert(0, "{}.tgz".format(resolve_filename(format)))
urls = map(lambda s: 'http://vis-www.cs.umass.edu/lfw/' + s, files) urls = list(map(lambda s: 'http://vis-www.cs.umass.edu/lfw/' + s, files))
default_downloader(directory, urls=urls, filenames=files, **kwargs) default_downloader(directory, urls=urls, filenames=files, **kwargs)
# this subparser hook is used for briq-download # this subparser hook is used for briq-download
@@ -67,9 +67,9 @@ def download_subparser(subparser):
subparser.add_argument( subparser.add_argument(
"--format", help="alternate format", type=str, default=None) "--format", help="alternate format", type=str, default=None)
urls = map(lambda s: 'http://vis-www.cs.umass.edu/lfw/' + s, files) urls = list(map(lambda s: 'http://vis-www.cs.umass.edu/lfw/' + s, files))
subparser.set_defaults(func=downloader_wrapper) return downloader_wrapper
########### Convert section ############## ########### Convert section ##############
@@ -121,9 +121,9 @@ def convert_lfw(directory, basename, output_directory):
print("--> Building test/train lists") print("--> Building test/train lists")
# build lists, throwing away heading # build lists, throwing away heading
with open('pairsDevTrain.txt', 'rb') as csvfile: with open('pairsDevTrain.txt', 'r') as csvfile:
trainrows = list(csv.reader(csvfile, delimiter='\t'))[1:] trainrows = list(csv.reader(csvfile, delimiter='\t'))[1:]
with open('pairsDevTest.txt', 'rb') as csvfile: with open('pairsDevTest.txt', 'r') as csvfile:
testrows = list(csv.reader(csvfile, delimiter='\t'))[1:] testrows = list(csv.reader(csvfile, delimiter='\t'))[1:]
print("--> Converting") print("--> Converting")
@@ -131,8 +131,8 @@ def convert_lfw(directory, basename, output_directory):
train_images = load_images("train", tar, tar_subdir, trainrows) train_images = load_images("train", tar, tar_subdir, trainrows)
test_images = load_images("test", tar, tar_subdir, testrows) test_images = load_images("test", tar, tar_subdir, testrows)
train_labels = np.array(map(lambda r:loadLabelsFromRow(r), trainrows)) train_labels = np.array(list(map(lambda r:loadLabelsFromRow(r), trainrows)))
test_labels = np.array(map(lambda r:loadLabelsFromRow(r), testrows)) test_labels = np.array(list(map(lambda r:loadLabelsFromRow(r), testrows)))
train_features = np.array([[f[0,:,:,0], f[0,:,:,1], f[0,:,:,2], f[1,:,:,0], f[1,:,:,1], f[1,:,:,2]] for f in train_images]) train_features = np.array([[f[0,:,:,0], f[0,:,:,1], f[0,:,:,2], f[1,:,:,0], f[1,:,:,1], f[1,:,:,2]] for f in train_images])
test_features = np.array([[f[0,:,:,0], f[0,:,:,1], f[0,:,:,2], f[1,:,:,0], f[1,:,:,1], f[1,:,:,2]] for f in test_images]) test_features = np.array([[f[0,:,:,0], f[0,:,:,1], f[0,:,:,2], f[1,:,:,0], f[1,:,:,1], f[1,:,:,2]] for f in test_images])
@@ -173,7 +173,7 @@ def convert_subparser(subparser):
# optional format can be funneled, deepfunneled, etc # optional format can be funneled, deepfunneled, etc
subparser.add_argument( subparser.add_argument(
"--format", help="alternate format", type=str, default=None) "--format", help="alternate format", type=str, default=None)
subparser.set_defaults(func=convert_lfw_wrapper) return convert_lfw_wrapper
########### Fuel Dataset section ############## ########### Fuel Dataset section ##############

View File

@@ -2,7 +2,7 @@ from setuptools import setup
from setuptools import find_packages from setuptools import find_packages
setup(name='lfw_fuel', setup(name='lfw_fuel',
version='0.1.0', version='0.1.1',
description='Labeled Faces in the Wild fuel dataset', description='Labeled Faces in the Wild fuel dataset',
author='Tom White', author='Tom White',
author_email='tom@sixdozen.com', author_email='tom@sixdozen.com',