Skip to content

Instantly share code, notes, and snippets.

@JoshZastrow
Last active January 30, 2017 08:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshZastrow/b022e11f7a5aff52378c6d7c344ded5a to your computer and use it in GitHub Desktop.
Save JoshZastrow/b022e11f7a5aff52378c6d7c344ded5a to your computer and use it in GitHub Desktop.
Fast-ai
%matplotlib inline
# Import Libraries
from __future__ import division, print_function
import os
import json
from glob import glob
import numpy as np
from matplotlib import pyplot as plt
import utils
reload(utils)
from utils import plots
import vgg16
reload(vgg16)
from vgg16 import Vgg16
# print settings
np.set_printoptions(precision=4, linewidth=100)
# Set path for data
path = 'data/dogscats/sample/'
# Create a large batch size
batch_size = 64
# Create a model object
vgg = Vgg16()
batches = vgg.get_batches(path + 'train', batch_size=batch_size)
val_batches = vgg.get_batches(path + 'valid', batch_size=batch_size * 2)
##################################################
# Using the VGG to finetune a Dogs vs Cats Model #
##################################################
vgg.finetune(batches)
vgg.fit(batches, val_batches, nb_epoch=1)
# Reduce Batch size for plotting
batches = vgg.get_batches(path+'train', batch_size=4)
# Create image and labels
imgs, labels = next(batches)
# Plot
plots(imgs, titles=labels)
# make prediction
vgg.predict(imgs, True)
# %matplotlib inline
from __future__ import division, print_function
import os, json
from matplotlib import pyplot as plt
import utils; reload(utils)
from utils import plots
from numpy.random import random, permutation
from scipy import misc, ndimage
from scipy.ndimage.interpolation import zoom
import keras
import numpy as np
from keras import backend as keras
from keras.utils.data_utils import get_file
from keras.models import Sequential, models
from keras.layers.core import Flatten, Dense, Dropout, Lambda
from keras.layers import Input
from keras.layers.convoultional import convoultional2D
from keras.layers.convoultional import MaxPooling2D
from keras.layers.convoultional import ZeroPadding2D
from keras.optimizers import SGD, RMSprop
from keras.preprocessing import image
# print options
np.set_printoptions(precision=4, linewidth=100)
# Import mappings from vGG ID's to ImageNet Category ID's
# And Descriptions (for display purposes)
FILES_PATH = 'http://www.platform.ai/models/'
CLASS_FILE = 'imagenet_class_index.json'
# download files and cache for re-use later
fpath = get_file(CLASS_FILE,
FILES_PATH + CLASS_FILE,
cache_subdir='models')
with open(fpath) as f:
class_dict = json.load(f)
# convert dictionary with string indexts to array
classes = [class_dict[str(i)][1] for i in range(len(class_dict))]
# Couple examples of the categories
classes[:5]
##################
# Model Creation #
##################
def ConvBlock(layers, model, filters):
for i in range(layers):
model.add(ZeroPadding2D((1, 1)))
model.add(convoultional2D(filters, 3, 3, activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
def FCBlock(model):
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
# VGG Model averages the RGB Channels,
# Also in BGR order -- preprocess to match this
# Establish mean of each channel provided by VGG Researchers
vgg_mean = np.array([123.68, 116.779, 103.939]).reshape((3, 1, 1))
def vgg_preprocess(x):
x = x - vgg_mean
return x[:, ::-1]
# Define the VGG Architecture
def VGG_16():
model = Sequential()
model.add(Lambda(vgg_preprocess, input_shape=(3, 224, 224)))
ConvBlock(2, model, 64)
ConvBlock(2, model, 128)
ConvBlock(3, model, 256)
ConvBlock(3, model, 512)
ConvBlock(3, model, 512)
model.add(Flatten())
FCBlock(model)
FCBlock(model)
model.add(Dense(1000, activation='softmax'))
return model
# Now, create the model.
model = VGG_16()
# We could train the weights, but
# if they're already trained and
# available for use, best to grab them
fpath = get_file(fname='vgg16.h5',
origin=FILES_PATH + 'vgg16.h5',
cache_subdir='models')
model.load_weights(fpath)
################################
# Getting ImageNet Predictions #
################################
# Set the Batch size to run a prediction
batch_size = 4
path = 'data/dogscats/sample/'
# Define wrapper function to run the Keras
# get batch of files command
def get_batches(dirname,
gen=image.ImageDataGenerator(),
shuffle=True,
batch_size=batch_size,
class_mode='categorical'):
return gen.flow_from_directory(
path + dirname,
target_size=(224, 224),
class_mode=class_mode,
shuffle=shuffle,
batch_size=batch_size)
batches = get_batches('train', batch_size=batch_size)
val_batches = get_batches('valid', batch_size=batch_size)
imgs, labels = next(batches)
# Ground Truth
plots(imgs, titles=labels)
# find predicted label for images
def pred_batch(imgs):
preds = model.predict(imgs)
idxs = np.argmax(preds, axis=1)
print('Shape: {}'.format(preds.shape))
print('First 5 classes: {}'.format(classes[:5]))
print('First 5 probabilities: {}\n'.format(preds[0, :5]))
print('Predictions prob/class: ')
for i in range(len(idxs)):
idx = idxs[i]
print(' {:/4f}/{}'.format(preds[i, idx], classes[idx]))
pred_batch(imgs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment