In [105]:
import os
import numpy as np
import matplotlib.pyplot as plt
import h5py
import scipy
from PIL import Image
from scipy import ndimage
import h5py

%matplotlib inline

In [106]:
random_state = 100
np.random.seed(100)

In [107]:
def load_data():
    trainfile = h5py.File('dataset/train_cat.h5', "r")
    testfile = h5py.File('dataset/test_cat.h5', 'r')
    
    x_train = np.array(trainfile['train_set_x'][:])
    y_train = np.array(trainfile['train_set_y'][:])
    
    x_test = np.array(testfile['test_set_x'][:])
    y_test = np.array(testfile['test_set_y'][:])
    
    return x_train, x_test, y_train.reshape(1,y_train.shape[0]), y_test.reshape(1, y_test.shape[0])

In [108]:
x_train, x_test, y_train, y_test = load_data()
print("Dataset with Xtrain(shape : {0}) and Ytrain(shape : {1})".format(x_train.shape, y_train.shape))


Dataset with Xtrain(shape : (209, 64, 64, 3)) and Ytrain(shape : (1, 209))

In [109]:
example_num = 102
plt.imshow(x_train[example_num])
print("Image is", 'Cat' if y_train[:,example_num] == 1 else 'Not Cat')


Image is Cat

Unfold RGB image


In [110]:
x_train_unfolded, x_test_unfolded = x_train.reshape(x_train.shape[0],64*64*3).T, x_test.reshape(x_test.shape[0],64*64*3).T

In [111]:
x_train.shape


Out[111]:
(209, 64, 64, 3)

In [112]:
x_train.reshape(x_train.shape[0],-1).T.shape


Out[112]:
(12288, 209)

In [113]:
x_train_unfolded.shape


Out[113]:
(12288, 209)

In [114]:
example = 134
plt.imshow(x_train[example].reshape(64,64,3))
print("Image is {0}".format('Cat' if (y_train[:,example] == 1) else 'Not Cat'))


Image is Cat

In [115]:
print("Training dataset with Xtrain(shape : {0}) and Ytrain(shape : {1})".format(x_train_unfolded.shape, y_train.shape))
print("Testing dataset with Xtest(shape : {0}) and Ytest(shape : {1})".format(x_test_unfolded.shape, y_test.shape))


Training dataset with Xtrain(shape : (12288, 209)) and Ytrain(shape : (1, 209))
Testing dataset with Xtest(shape : (12288, 50)) and Ytest(shape : (1, 50))

Noramalize data


In [116]:
x_train_norm, x_test_norm = x_train_unfolded/255, x_test_unfolded/255

In [117]:
print("Training dataset with Xtrain(shape : {0}) and Ytrain(shape : {1})".format(x_train_unfolded.shape, y_train.shape))
print("Testing dataset with Xtest(shape : {0}) and Ytest(shape : {1})".format(x_test_unfolded.shape, y_test.shape))


Training dataset with Xtrain(shape : (12288, 209)) and Ytrain(shape : (1, 209))
Testing dataset with Xtest(shape : (12288, 50)) and Ytest(shape : (1, 50))

In [118]:
def sigmoid(x):
    return 1/(1+ np.exp(-x))

In [119]:
def forward(W,b,X,Y):
    """
    Forward prop.
    """
    z = np.dot(W.T,X) + b
    yhat = sigmoid(z)
    #print("yhat shape:", yhat.shape)
    return yhat

In [120]:
def backward(W,b,X,Y, yhat):
    """ Backward prop """
    #print("shapes: w,x,y,yhat", W.shape, X.shape,Y.shape,yhat.shape)
    m = X.shape[1]
    cost = - ( (1/m) * np.sum( (Y*np.log(yhat)) + ((1 - Y)*np.log((1 - yhat))) ) )   # cost over all examples
    #print("shapes: w,x,y,yhat", W.shape, X.shape,Y.shape,yhat.shape)
    dW = (1/m) * np.dot(X,(yhat - Y).T)
    db = (1/m) * np.sum((yhat - Y))
    return dW, db, np.squeeze(cost)

In [121]:
def learn(W, b, X, Y, learning_rate=0.01, num_iterations=1000, print_error = False):
    """ learn by iterating over training set """

    costs = []

    for i in range(num_iterations):
        yhat = forward(W,b,X,Y)
        dW,db, cost = backward(W,b,X,Y, yhat)

        #update weights
        W = W - (learning_rate * dW)
        b = b - (learning_rate * db)
        assert(dW.shape == W.shape)
        if( i%100 == 0):
            costs.append(cost)

        if print_error and i%100 == 0:
            print("error after iteration {0} is {1}".format(i, cost))


    parameters = {
        "W":W,
        "b":b
    }

    gradients = {
        "dW":dW,
        "db":db
    }

    return parameters, gradients, costs

In [122]:
def predict(W,b,X):
    """ Predicts output """

    m = X.shape[1]

    predictions = np.zeros((1,m))
    W = W.reshape(X.shape[0], 1)
    yHat = sigmoid(np.dot(W.T, X) + b)
    
    for i in range(yHat.shape[1]):
        if yHat[0,i] <= 0.5 :
            predictions[0,i] = 0
        else:
            predictions[0,i] = 1
    
    return predictions

In [123]:
def model(x_train, y_train, x_test, y_test, learning_rate=0.001, num_iterations = 10000, print_error=False):
    
    W = np.zeros((x_train.shape[0],1))
    b = 0
    params, grads, costs = learn(W,b,x_train,y_train, learning_rate=0.01, num_iterations=num_iterations, print_error = print_error)
     
    train_pred = predict(params["W"], params["b"], x_train)
    test_pred = predict(params["W"], params["b"], x_test)
    
    print("training accuracy: {0} %".format(100-np.mean(np.abs(train_pred - y_train)) * 100))
    print("testing accuracy: {0} %".format(100-np.mean(np.abs(test_pred - y_test)) * 100))
    
    return costs,params

In [148]:
costs, params = model(x_train_norm, y_train, x_test_norm, y_test, learning_rate=0.01, num_iterations = 2000, print_error=True)


error after iteration 0 is 0.6931471805599453
error after iteration 100 is 0.8239208681601369
error after iteration 200 is 0.41894372058616236
error after iteration 300 is 0.6173497063912884
error after iteration 400 is 0.522115767197924
error after iteration 500 is 0.3877087459646933
error after iteration 600 is 0.23625445652290003
error after iteration 700 is 0.15422213305621676
error after iteration 800 is 0.1353278283265459
error after iteration 900 is 0.12497148001124604
error after iteration 1000 is 0.11647833126181911
error after iteration 1100 is 0.10919251128427583
error after iteration 1200 is 0.10280446418273667
error after iteration 1300 is 0.09712981007882818
error after iteration 1400 is 0.09204326923447784
error after iteration 1500 is 0.08745251991513217
error after iteration 1600 is 0.08328603053341713
error after iteration 1700 is 0.07948657037494224
error after iteration 1800 is 0.07600734571751612
error after iteration 1900 is 0.07280949458176567
training accuracy: 99.52153110047847 %
testing accuracy: 70.0 %

In [149]:
costs = np.squeeze(costs)
plt.plot(costs)
plt.ylabel('cost')
plt.xlabel('iterations (per hundreds)')
plt.title("Learning rate =" + str(0.001))
plt.show()



In [127]:
model_dir = "models"
if not os.path.exists(model_dir):
    os.makedirs(model_dir)

import pickle
    
def saveModel(params, name):
    f = open( model_dir + '/' + name + ".model", "wb" )
    pickle.dump( params,f )
    f.close()

In [128]:
def loadModel(name):
    f = open(model_dir + '/' + name + ".model", "rb")
    model = pickle.load(f)
    f.close()
    return model

Save your Model


In [129]:
saveModel(params, 'cat__lr0_01__iter_1000')

In [137]:
saved_model = loadModel('cat__lr0_01__iter_1000')

In [141]:
num_px=64
num_channel = 0
fname = "test_images/im1.jpg"
image = np.array(ndimage.imread(fname, flatten=False))
img = np.array(scipy.misc.imresize(image, size=(num_px,num_px)))
num_channel = img.shape[2]

In [142]:
unfold = img.reshape(64*64*3,1)
plt.imshow(img)


Out[142]:
<matplotlib.image.AxesImage at 0x19848f383c8>

In [143]:
yhat = np.squeeze(predict(saved_model["W"], saved_model["b"],unfold))
print("Given image is {0}".format('Cat' if yhat == 1 else 'Not Cat'))


Given image is Cat

In [ ]: