In [2]:
import os
import json
DIR_CS231n = '/Users/cthorey/Documents/MLearning/CS231/assignment2/'
import sys
from sklearn.externals import joblib
sys.path.append(DIR_CS231n)
import numpy as np
import matplotlib.pyplot as plt
from cs231n.classifiers.cnn import ThreeLayerConvNet
from cs231n.data_utils import get_CIFAR10_data
from cs231n.gradient_check import eval_numerical_gradient_array, eval_numerical_gradient
from cs231n.layers import *
from cs231n.fast_layers import *
from cs231n.solver import Solver
%load_ext autoreload
%autoreload 2
In [10]:
root = os.getcwd()
test1 = os.path.join(root,[f for f in os.listdir(root) if f[:2] == 'lr'][0],'checkpoints')
In [14]:
clf = joblib.load(os.path.join(test1,os.listdir(test1)[2],'check_2.pkl'))
In [18]:
model = clf['model']
In [19]:
data = get_CIFAR10_data(DIR_CS231n)
for k, v in data.iteritems():
print '%s: ' % k, v.shape
In [20]:
def check_accuracy(model, X, y, num_samples=None, batch_size=100):
"""
Check accuracy of the model on the provided data.
Inputs:
- X: Array of data, of shape (N, d_1, ..., d_k)
- y: Array of labels, of shape (N,)
- num_samples: If not None, subsample the data and only test the model
on num_samples datapoints.
- batch_size: Split X and y into batches of this size to avoid using too
much memory.
Returns:
- acc: Scalar giving the fraction of instances that were correctly
classified by the model.
"""
# Maybe subsample the data
N = X.shape[0]
if num_samples is not None and N > num_samples:
mask = np.random.choice(N, num_samples)
N = num_samples
X = X[mask]
y = y[mask]
# Compute predictions in batches
num_batches = N / batch_size
if N % batch_size != 0:
num_batches += 1
y_pred = []
for i in xrange(num_batches):
start = i * batch_size
end = (i + 1) * batch_size
scores = model.loss(X[start:end])
y_pred.append(np.argmax(scores, axis=1))
y_pred = np.hstack(y_pred)
acc = np.mean(y_pred == y)
return acc
In [21]:
check_accuracy(model,data['X_val'],data['y_val'])
Out[21]:
In [ ]: