Permalink
Browse files

Initial commit

  • Loading branch information...
Ridam Batra
Ridam Batra committed Oct 1, 2017
0 parents commit 72d040e4452e07a792bcce0440ee7959fb9f7e5c
Showing with 3,526 additions and 0 deletions.
  1. +23 −0 README.md
  2. 0 WORKSPACE
  3. BIN data/fashion/t10k-images-idx3-ubyte.gz
  4. BIN data/fashion/t10k-labels-idx1-ubyte.gz
  5. BIN data/fashion/train-images-idx3-ubyte.gz
  6. BIN data/fashion/train-labels-idx1-ubyte.gz
  7. BIN data/mnist_sequence1_sample_5distortions5x5.npz
  8. +53 −0 data_loader.py
  9. +37 −0 learning_to_remember_rare_events/README.md
  10. BIN learning_to_remember_rare_events/__pycache__/data_utils.cpython-36.pyc
  11. BIN learning_to_remember_rare_events/__pycache__/memory.cpython-36.pyc
  12. BIN learning_to_remember_rare_events/__pycache__/model.cpython-36.pyc
  13. BIN learning_to_remember_rare_events/__pycache__/sort_pool2d.cpython-36.pyc
  14. +241 −0 learning_to_remember_rare_events/data_utils.py
  15. +386 −0 learning_to_remember_rare_events/memory.py
  16. +306 −0 learning_to_remember_rare_events/model.py
  17. +66 −0 learning_to_remember_rare_events/sort_pool2d.py
  18. +247 −0 learning_to_remember_rare_events/train.py
  19. +2 −0 learning_to_remember_rare_events/train_metrics.csv
  20. +21 −0 matching_networks/LICENSE
  21. +16 −0 matching_networks/README.md
  22. BIN matching_networks/data.npy
  23. +125 −0 matching_networks/data.py
  24. +157 −0 matching_networks/experiment_builder.py
  25. +305 −0 matching_networks/one_shot_learning_network.py
  26. +65 −0 matching_networks/sort_pool2d.py
  27. +26 −0 matching_networks/storage.py
  28. +86 −0 matching_networks/train_one_shot_learning_matching_network.py
  29. +186 −0 model.py
  30. +40 −0 resnet/BUILD
  31. +77 −0 resnet/README.md
  32. +122 −0 resnet/cifar_input.py
  33. +196 −0 resnet/resnet_main.py
  34. +373 −0 resnet/resnet_model.py
  35. +91 −0 sortpool2d.py
  36. +100 −0 sortpool2d_test.py
  37. +179 −0 train.py
@@ -0,0 +1,23 @@
## loss-exps
Experiments with a different pooling layer for image classification
### Run cluttered mnist experiments
```bash
python train.py --result cluttered-mnist/pool_1 --pool-range 1 # val_cross_entropy 0.259
python train.py --result cluttered-mnist/pool_2 --pool-range 2 # val_cross_entropy 0.202
python train.py --result cluttered-mnist/pool_3 --pool-range 3 # val_cross_entropy 0.196
python train.py --result cluttered-mnist/pool_4 --pool-range 4 # val_cross_entropy 0.196
```
### Run fashion mnist experiments
```bash
python train.py --result fashion-mnist/pool_1 --pool-range 1 --dataset fashion-mnist # val_cross_entropy 0.291
python train.py --result fashion-mnist/pool_2 --pool-range 2 --dataset fashion-mnist # val_cross_entropy 0.282
python train.py --result fashion-mnist/pool_3 --pool-range 3 --dataset fashion-mnist # val_cross_entropy 0.268
python train.py --result fashion-mnist/pool_4 --pool-range 4 --dataset fashion-mnist # val_cross_entropy 0.276
```
### To access all command line arguments
```bash
python train.py -h
```
No changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,53 @@
import tensorflow as tf
import os, gzip, numpy as np
def load_mnist(path, kind='train'):
labels_path = os.path.join(path, '%s-labels-idx1-ubyte.gz' % kind)
images_path = os.path.join(path, '%s-images-idx3-ubyte.gz' % kind)
with gzip.open(labels_path, 'rb') as lbpath:
labels = np.expand_dims(np.frombuffer(lbpath.read(),
dtype=np.uint8, offset=8), axis=1)
with gzip.open(images_path, 'rb') as imgpath:
images = np.frombuffer(imgpath.read(), dtype=np.uint8,
offset=16).reshape(len(labels), 28, 28, 1)
return images, labels
def load_data(dataset):
if(dataset=='fashion-mnist'):
x_train, y_train = load_mnist('data/fashion', kind='train')
x_test, y_test = load_mnist('data/fashion', kind='t10k')
y_train, y_test = dense_to_one_hot(y_train, n_classes=10), dense_to_one_hot(y_test, n_classes=10)
elif(dataset=='cluttered-mnist'):
mnist_cluttered = np.load('data/mnist_sequence1_sample_5distortions5x5.npz')
x_train, y_train = mnist_cluttered['X_train'], mnist_cluttered['y_train']
x_valid, y_valid = mnist_cluttered['X_valid'], mnist_cluttered['y_valid']
x_test, y_test = mnist_cluttered['X_test'], mnist_cluttered['y_test']
x_train = np.concatenate((x_train, x_valid))
y_train = np.concatenate((y_train, y_valid))
x_train = x_train.reshape(-1, 40, 40, 1)
x_test = x_test.reshape(-1, 40, 40, 1)
y_train, y_test = dense_to_one_hot(y_train, n_classes=10), dense_to_one_hot(y_test, n_classes=10)
elif(dataset=='cifar10'):
(x_train, y_train), (x_test, y_test) = tf.contrib.keras.datasets.cifar10.load_data()
y_train, y_test = dense_to_one_hot(y_train, n_classes=10), dense_to_one_hot(y_test, n_classes=10)
elif(dataset=='cifar100'):
(x_train, y_train), (x_test, y_test) = tf.contrib.keras.datasets.cifar100.load_data()
y_train, y_test = dense_to_one_hot(y_train, n_classes=100), dense_to_one_hot(y_test, n_classes=100)
else:
raise ValueError('dataset not found')
return (x_train, y_train), (x_test, y_test)
def dense_to_one_hot(labels, n_classes=2):
"""Convert class labels from scalars to one-hot vectors."""
labels = np.array(labels)
n_labels = labels.shape[0]
index_offset = np.arange(n_labels) * n_classes
labels_one_hot = np.zeros((n_labels, n_classes), dtype=np.float32)
labels_one_hot.flat[index_offset + labels.ravel()] = 1
return labels_one_hot
@@ -0,0 +1,37 @@
Code for the Memory Module as described
in "Learning to Remember Rare Events" by
Lukasz Kaiser, Ofir Nachum, Aurko Roy, and Samy Bengio
published as a conference paper at ICLR 2017.
Requirements:
* TensorFlow (see tensorflow.org for how to install)
* Some basic command-line utilities (git, unzip).
Description:
The general memory module is located in memory.py.
Some code is provided to see the memory module in
action on the standard Omniglot dataset.
Download and setup the dataset using data_utils.py
and then run the training script train.py
(see example commands below).
Note that the structure and parameters of the model
are optimized for the data preparation as provided.
Quick Start:
First download and set-up Omniglot data by running
```
python data_utils.py
```
Then run the training script:
```
python train.py --memory_size=8192 --pool_range=1 --save_dir=5-way/pool_1/
python train.py --memory_size=8192 --pool_range=2 --save_dir=5-way/pool_2/
python train.py --memory_size=8192 --pool_range=3 --save_dir=5-way/pool_3/
python train.py --memory_size=8192 --pool_range=4 --save_dir=5-way/pool_4/
```
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,241 @@
# Copyright 2017 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ==============================================================================
"""Data loading and other utilities.
Use this file to first copy over and pre-process the Omniglot dataset.
Simply call
python data_utils.py
"""
import _pickle as pickle
import logging
import os
import subprocess
import numpy as np
from scipy.misc import imresize
from scipy.misc import imrotate
from scipy.ndimage import imread
import tensorflow as tf
MAIN_DIR = ''
REPO_LOCATION = 'https://github.com/brendenlake/omniglot.git'
REPO_DIR = os.path.join(MAIN_DIR, 'omniglot')
DATA_DIR = os.path.join(REPO_DIR, 'python')
TRAIN_DIR = os.path.join(DATA_DIR, 'images_background')
TEST_DIR = os.path.join(DATA_DIR, 'images_evaluation')
DATA_FILE_FORMAT = os.path.join(MAIN_DIR, '%s_omni.pkl')
TRAIN_ROTATIONS = True # augment training data with rotations
TEST_ROTATIONS = False # augment testing data with rotations
IMAGE_ORIGINAL_SIZE = 105
IMAGE_NEW_SIZE = 28
def get_data():
"""Get data in form suitable for episodic training.
Returns:
Train and test data as dictionaries mapping
label to list of examples.
"""
with tf.gfile.GFile(DATA_FILE_FORMAT % 'train', 'rb') as f:
processed_train_data = pickle.load(f)
with tf.gfile.GFile(DATA_FILE_FORMAT % 'test', 'rb') as f:
processed_test_data = pickle.load(f)
train_data = {}
test_data = {}
for data, processed_data in zip([train_data, test_data],
[processed_train_data, processed_test_data]):
for image, label in zip(processed_data['images'],
processed_data['labels']):
if label not in data:
data[label] = []
data[label].append(image.reshape([-1]).astype('float32'))
intersection = set(train_data.keys()) & set(test_data.keys())
assert not intersection, 'Train and test data intersect.'
ok_num_examples = [len(ll) == 20 for _, ll in train_data.items()]
assert all(ok_num_examples), 'Bad number of examples in train data.'
ok_num_examples = [len(ll) == 20 for _, ll in test_data.items()]
assert all(ok_num_examples), 'Bad number of examples in test data.'
logging.info('Number of labels in train data: %d.', len(train_data))
logging.info('Number of labels in test data: %d.', len(test_data))
return train_data, test_data
def crawl_directory(directory, augment_with_rotations=False,
first_label=0):
"""Crawls data directory and returns stuff."""
label_idx = first_label
images = []
labels = []
info = []
# traverse root directory
for root, _, files in os.walk(directory):
logging.info('Reading files from %s', root)
fileflag = 0
for file_name in files:
full_file_name = os.path.join(root, file_name)
img = imread(full_file_name, flatten=True)
for i, angle in enumerate([0, 90, 180, 270]):
if not augment_with_rotations and i > 0:
break
images.append(imrotate(img, angle))
labels.append(label_idx + i)
info.append(full_file_name)
fileflag = 1
if fileflag:
label_idx += 4 if augment_with_rotations else 1
return images, labels, info
def resize_images(images, new_width, new_height):
"""Resize images to new dimensions."""
resized_images = np.zeros([images.shape[0], new_width, new_height],
dtype=np.float32)
for i in range(images.shape[0]):
resized_images[i, :, :] = imresize(images[i, :, :],
[new_width, new_height],
interp='bilinear',
mode=None)
return resized_images
def write_datafiles(directory, write_file,
resize=True, rotate=False,
new_width=IMAGE_NEW_SIZE, new_height=IMAGE_NEW_SIZE,
first_label=0):
"""Load and preprocess images from a directory and write them to a file.
Args:
directory: Directory of alphabet sub-directories.
write_file: Filename to write to.
resize: Whether to resize the images.
rotate: Whether to augment the dataset with rotations.
new_width: New resize width.
new_height: New resize height.
first_label: Label to start with.
Returns:
Number of new labels created.
"""
# these are the default sizes for Omniglot:
imgwidth = IMAGE_ORIGINAL_SIZE
imgheight = IMAGE_ORIGINAL_SIZE
logging.info('Reading the data.')
images, labels, info = crawl_directory(directory,
augment_with_rotations=rotate,
first_label=first_label)
images_np = np.zeros([len(images), imgwidth, imgheight], dtype=np.bool)
labels_np = np.zeros([len(labels)], dtype=np.uint32)
for i in range(len(images)):
images_np[i, :, :] = images[i]
labels_np[i] = labels[i]
if resize:
logging.info('Resizing images.')
resized_images = resize_images(images_np, new_width, new_height)
logging.info('Writing resized data in float32 format.')
data = {'images': resized_images,
'labels': labels_np,
'info': info}
with tf.gfile.GFile(write_file, 'w') as f:
pickle.dump(data, f)
else:
logging.info('Writing original sized data in boolean format.')
data = {'images': images_np,
'labels': labels_np,
'info': info}
with tf.gfile.GFile(write_file, 'w') as f:
pickle.dump(data, f)
return len(np.unique(labels_np))
def maybe_download_data():
"""Download Omniglot repo if it does not exist."""
if os.path.exists(REPO_DIR):
logging.info('It appears that Git repo already exists.')
else:
logging.info('It appears that Git repo does not exist.')
logging.info('Cloning now.')
subprocess.check_output('git clone %s' % REPO_LOCATION, shell=True)
if os.path.exists(TRAIN_DIR):
logging.info('It appears that train data has already been unzipped.')
else:
logging.info('It appears that train data has not been unzipped.')
logging.info('Unzipping now.')
subprocess.check_output('unzip %s.zip -d %s' % (TRAIN_DIR, DATA_DIR),
shell=True)
if os.path.exists(TEST_DIR):
logging.info('It appears that test data has already been unzipped.')
else:
logging.info('It appears that test data has not been unzipped.')
logging.info('Unzipping now.')
subprocess.check_output('unzip %s.zip -d %s' % (TEST_DIR, DATA_DIR),
shell=True)
def preprocess_omniglot():
"""Download and prepare raw Omniglot data.
Downloads the data from GitHub if it does not exist.
Then load the images, augment with rotations if desired.
Resize the images and write them to a pickle file.
"""
maybe_download_data()
directory = TRAIN_DIR
write_file = DATA_FILE_FORMAT % 'train'
num_labels = write_datafiles(
directory, write_file, resize=True, rotate=TRAIN_ROTATIONS,
new_width=IMAGE_NEW_SIZE, new_height=IMAGE_NEW_SIZE)
directory = TEST_DIR
write_file = DATA_FILE_FORMAT % 'test'
write_datafiles(directory, write_file, resize=True, rotate=TEST_ROTATIONS,
new_width=IMAGE_NEW_SIZE, new_height=IMAGE_NEW_SIZE,
first_label=num_labels)
def main(unused_argv):
logging.basicConfig(level=logging.INFO)
preprocess_omniglot()
if __name__ == '__main__':
tf.app.run()
Oops, something went wrong.

0 comments on commit 72d040e

Please sign in to comment.