{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"import os\nimport random\nimport sys\nimport datetime\n## pip3 install opencv-python\nimport cv2\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"418c21c766879e7f515762cb9855c64e6a89ce79"},"cell_type":"code","source":"\n\n## constants\nTRAIN_DIR = \"../input/train/\"\nTEST_DIR = \"../input/test/\"\nTRAIN_SIZE = 25000\nTEST_SIZE = 12500\nDEV_RATIO = 0.1\nIMAGE_HEIGHT = IMAGE_WIDTH = 128\n\nLEARNING_RATE = 0.0001\nMINIBATCH_SIZE = 32\nINPUT_SIZE = IMAGE_HEIGHT * IMAGE_WIDTH * 3\nOUTPUT_SIZE = 2\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b0ab41c379352be52459b63902b3c64ded81ab81"},"cell_type":"code","source":"\n\n## data utility functions\ndef split_data(two_dims_datas, split_ratio=DEV_RATIO):\n    left_count = int(two_dims_datas.shape[1] * split_ratio)\n    left_datas = two_dims_datas[:, :left_count]\n    right_datas = two_dims_datas[:, left_count:]\n    print(\"input datas shape: {}, left datas shape:{}, \\\n    right datas shape: {}\".format(two_dims_datas.shape, left_datas.shape, right_datas.shape))\n    return left_datas, right_datas\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f8ed01d5360460bb171a4591927b518148879a34"},"cell_type":"code","source":"def load_data(dirname=TRAIN_DIR, file_count=1000, shuffle=True):\n    all_filenames = os.listdir(dirname)\n    random.shuffle(all_filenames)\n    filenames = all_filenames[:file_count]\n    \n    ## images\n    images = np.zeros((file_count, IMAGE_HEIGHT*IMAGE_WIDTH*3))\n    for i in range(file_count):\n        imgnd_origin = cv2.imread(dirname+filenames[i])\n        imgnd_resized = cv2.resize(imgnd_origin, (IMAGE_HEIGHT, IMAGE_WIDTH), interpolation=cv2.INTER_CUBIC)\n        imgnd_flatten = imgnd_resized.reshape(1,-1)\n        images[i] = imgnd_flatten\n    \n    ## labels from filenames\n    labels_list = [\"dog\" in filename for filename in filenames]\n    labels = np.array(labels_list, dtype='int8').reshape(file_count, 1)\n    \n    ## shuffle\n    if shuffle:\n        permutation = list(np.random.permutation(labels.shape[0]))\n        labels = labels[permutation, :]\n        images = images[permutation, :]\n\n    ## normalization\n    images = images/255.0\n    \n    return images.T, labels.T","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"16abc2f558bc142e08b4b8ce5e7dbd42b5648085"},"cell_type":"code","source":"\n\nimages, labels = load_data(file_count=200)\ndev_images, train_images = split_data(images)\ndev_labels, train_labels = split_data(labels)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"abfc49adf5c86e3ea9060974ab0990ef2cd213b9"},"cell_type":"code","source":"\n\nn_x = train_images.shape[0] # size of input layer\nn_h = 4          # hard code the hidden layer size to be 4\nn_y = train_labels.shape[0] # size of output layer\n\nlayer_sizes = (n_x, n_h, n_y)\nprint(layer_sizes)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"175c567f16a6770c1a315658fa8eba0835c17de2"},"cell_type":"code","source":"def initialize_parameters(n_x, n_h, n_y):\n    \"\"\"\n    Argument:\n    n_x -- size of the input layer\n    n_h -- size of the hidden layer\n    n_y -- size of the output layer\n    \n    Returns:\n    params -- python dictionary containing your parameters:\n                    W1 -- weight matrix of shape (n_h, n_x)\n                    b1 -- bias vector of shape (n_h, 1)\n                    W2 -- weight matrix of shape (n_y, n_h)\n                    b2 -- bias vector of shape (n_y, 1)\n    \"\"\"\n    \n    W1 = np.random.randn(n_h, n_x) * 0.01\n    b1 = np.zeros((n_h, 1))\n    W2 = np.random.randn(n_y, n_h) * 0.01\n    b2 = np.zeros((n_y, 1))\n    \n    parameters = {\"W1\": W1,\n                  \"b1\": b1,\n                  \"W2\": W2,\n                  \"b2\": b2}\n    \n    return parameters","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fed462a8a730983829f02e644a915974c41cb5d7"},"cell_type":"code","source":"## SIGMOID\n\ndef sigmoid(z):\n    \"\"\"\n    Compute the sigmoid of z\n\n    Arguments:\n    z -- A scalar or numpy array of any size.\n\n    Return:\n    s -- sigmoid(z)\n    \"\"\"\n    \n    s = 1.0/(1.0 + np.exp(-1.0 * z))\n    \n    return s","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5d2f5b0f7d84d14eebec0317a3cc95a1a90e756f"},"cell_type":"code","source":"# forward_propagation\n\ndef forward_propagation(X, parameters):\n    \"\"\"\n    Argument:\n    X -- input data of size (n_x, m)\n    parameters -- python dictionary containing your parameters (output of initialization function)\n            W1 -- weight matrix of shape (n_h, n_x)\n            b1 -- bias vector of shape (n_h, 1)\n            W2 -- weight matrix of shape (n_y, n_h)\n            b2 -- bias vector of shape (n_y, 1)\n    \n    Returns:\n    A2 -- The sigmoid output of the second activation\n    cache -- a dictionary containing \"Z1\", \"A1\", \"Z2\" and \"A2\"\n    \"\"\"\n    # Retrieve each parameter from the dictionary \"parameters\"\n    W1 = parameters[\"W1\"]\n    b1 = parameters[\"b1\"]\n    W2 = parameters[\"W2\"]\n    b2 = parameters[\"b2\"]\n    \n    # Implement Forward Propagation to calculate A2 (probabilities)\n    #print(W1.shape, X.shape)\n    #print(np.matmul(W1, X).shape, b1.shape)\n    Z1 = np.add(np.matmul(W1, X), b1)\n    A1 = np.tanh(Z1)\n    Z2 = np.add(np.matmul(W2, A1), b2)\n    A2 = sigmoid(Z2)\n    \n    assert(A2.shape == (1, X.shape[1]))\n    \n    cache = {\"Z1\": Z1,\n             \"A1\": A1,\n             \"Z2\": Z2,\n             \"A2\": A2}\n    \n    return A2, cache","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c714a12ac0d15386f7f6115e66bbcfe5c583dc87"},"cell_type":"code","source":"# compute_cost\n\ndef compute_cost(A2, Y, parameters):\n    \"\"\"\n    Computes the cross-entropy cost given in equation (13)\n    \n    Arguments:\n    A2 -- The sigmoid output of the second activation, of shape (1, number of examples)\n    Y -- \"true\" labels vector of shape (1, number of examples)\n    parameters -- python dictionary containing your parameters W1, b1, W2 and b2\n    \n    Returns:\n    cost -- cross-entropy cost given equation (13)\n    \"\"\"\n    \n    m = Y.shape[1] # number of example\n\n    # Compute the cross-entropy cost\n    logprobs = np.multiply(Y, np.log(A2)) + np.multiply((1 - Y), np.log(1 - A2))\n    #print(logprobs.shape)\n    cost = (-1.0/m) * np.sum(logprobs)\n    \n    cost = np.squeeze(cost)     # makes sure cost is the dimension we expect. \n                                # E.g., turns [[17]] into 17 \n    assert(isinstance(cost, float))\n    \n    return cost","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"906d1fac91dd6918362bb03feaa81f0603b04945"},"cell_type":"code","source":"# backward_propagation\n\ndef backward_propagation(parameters, cache, X, Y):\n    \"\"\"\n    Implement the backward propagation using the instructions above.\n    \n    Arguments:\n    parameters -- python dictionary containing our parameters \n    cache -- a dictionary containing \"Z1\", \"A1\", \"Z2\" and \"A2\".\n    X -- input data of shape (2, number of examples)\n    Y -- \"true\" labels vector of shape (1, number of examples)\n    \n    Returns:\n    grads -- python dictionary containing your gradients with respect to different parameters\n    \"\"\"\n    m = X.shape[1]\n    \n    # First, retrieve W1 and W2 from the dictionary \"parameters\".\n    W1 = parameters[\"W1\"]\n    W2 = parameters[\"W2\"]\n        \n    # Retrieve also A1 and A2 from dictionary \"cache\".\n    A1 = cache[\"A1\"]\n    A2 = cache[\"A2\"]\n    \n    # Backward propagation: calculate dW1, db1, dW2, db2. \n    dZ2 = A2 - Y\n    dW2 = (1.0/m) * np.matmul(dZ2, np.transpose(A1))\n    db2 = (1.0/m) * np.sum(dZ2, axis=1, keepdims=True)\n    dZ1 = np.matmul(np.transpose(W2), dZ2) * (1 - np.power(A1, 2))\n    dW1 = (1.0/m) * np.matmul(dZ1, np.transpose(X))\n    db1 = (1.0/m) * np.sum(dZ1, axis=1, keepdims=True)\n    \n    grads = {\"dW1\": dW1,\n             \"db1\": db1,\n             \"dW2\": dW2,\n             \"db2\": db2}\n    \n    return grads","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cce0b3a16ff3390ac081acbac7611a2f3757bc0a"},"cell_type":"code","source":"# update_parameters\n\ndef update_parameters(parameters, grads, learning_rate = 1.2):\n    \"\"\"\n    Updates parameters using the gradient descent update rule given above\n    \n    Arguments:\n    parameters -- python dictionary containing your parameters \n    grads -- python dictionary containing your gradients \n    \n    Returns:\n    parameters -- python dictionary containing your updated parameters \n    \"\"\"\n    # Retrieve each parameter from the dictionary \"parameters\"\n    W1 = parameters[\"W1\"]\n    b1 = parameters[\"b1\"]\n    W2 = parameters[\"W2\"]\n    b2 = parameters[\"b2\"]\n    \n    # Retrieve each gradient from the dictionary \"grads\"\n    dW1 = grads[\"dW1\"]\n    db1 = grads[\"db1\"]\n    dW2 = grads[\"dW2\"]\n    db2 = grads[\"db2\"]\n    \n    # Update rule for each parameter\n    W1 = W1 - learning_rate * dW1\n    b1 = b1 - learning_rate * db1\n    W2 = W2 - learning_rate * dW2\n    b2 = b2 - learning_rate * db2\n    \n    parameters = {\"W1\": W1,\n                  \"b1\": b1,\n                  \"W2\": W2,\n                  \"b2\": b2}\n    \n    return parameters","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"26caac95ebf197b617804ef6538f79d4da3a7553"},"cell_type":"code","source":"# nn_model\n\ndef nn_model(X, Y, n_h, num_iterations = 10000, learning_rate=1.2, print_cost=False):\n    \"\"\"\n    Arguments:\n    X -- dataset of shape (n_x, number of examples)\n    Y -- labels of shape (n_y, number of examples)\n    n_h -- size of the hidden layer\n    num_iterations -- Number of iterations in gradient descent loop\n    print_cost -- if True, print the cost every 1000 iterations\n    \n    Returns:\n    parameters -- parameters learnt by the model. They can then be used to predict.\n    \"\"\"\n    \n    n_x = X.shape[0]\n    n_y = Y.shape[0]\n    \n    # Initialize parameters, then retrieve W1, b1, W2, b2. Inputs: \"n_x, n_h, n_y\". Outputs = \"W1, b1, W2, b2, parameters\".\n    parameters = initialize_parameters(n_x, n_h, n_y)\n    W1 = parameters[\"W1\"]\n    b1 = parameters[\"b1\"]\n    W2 = parameters[\"W2\"]\n    b2 = parameters[\"b2\"]\n    \n    # Loop (gradient descent)\n    costs = []\n    for i in range(0, num_iterations):\n         \n        # Forward propagation. Inputs: \"X, parameters\". Outputs: \"A2, cache\".\n        A2, cache = forward_propagation(X, parameters)\n        \n        # Cost function. Inputs: \"A2, Y, parameters\". Outputs: \"cost\".\n        cost = compute_cost(A2, Y, parameters)\n        costs.append(cost)\n \n        # Backpropagation. Inputs: \"parameters, cache, X, Y\". Outputs: \"grads\".\n        grads = backward_propagation(parameters, cache, X, Y)\n \n        # Gradient descent parameter update. Inputs: \"parameters, grads\". Outputs: \"parameters\".\n        parameters = update_parameters(parameters, grads, learning_rate=learning_rate)\n        \n        # cache all about model\n        trained_model = {\n            \"layer_sizes\": (n_x, n_h, n_y),\n            \"learning_rate\": learning_rate,\n            \"costs\": costs,\n            \"parameters\": parameters\n        }\n\n        # Print the cost every 1000 iterations\n        if print_cost and i % 1000 == 0:\n            print (\"Cost after iteration %i: %f\" %(i, cost))\n\n    return trained_model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bc48b4e83c0b5f8e042aabcd772d595216d4e866"},"cell_type":"code","source":"# predict\n\ndef predict(parameters, X):\n    \"\"\"\n    Using the learned parameters, predicts a class for each example in X\n    \n    Arguments:\n    parameters -- python dictionary containing your parameters \n    X -- input data of size (n_x, m)\n    \n    Returns\n    predictions -- vector of predictions of our model (red: 0 / blue: 1)\n    \"\"\"\n    \n    # Computes probabilities using forward propagation, and classifies to 0/1 using 0.5 as the threshold.\n    A2, cache = forward_propagation(X, parameters)\n    predictions = A2 > 0.5\n    \n    return predictions","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d2ce9671627b3bb2d98d59a0a51f163b31496c0c"},"cell_type":"code","source":"def plot_costs(trained_model):\n    # Plot learning curve (with costs)\n    costs = np.squeeze(trained_model[\"costs\"])\n    plt.plot(costs)\n    plt.ylabel('cost')\n    plt.xlabel('iterations (per hundreds)')\n    plt.title(\"Learning rate ={}\\n, layer_sizes={}\\n, accuracy:{}, m:{}\\n\".format(\n        trained_model[\"learning_rate\"], trained_model[\"layer_sizes\"],\n        trained_model.get(\"accuracy\"), trained_model.get(\"m\")\n    ))\n    #plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ef2b7f93a13194c206e27f1bb6e9638a77c099c1"},"cell_type":"code","source":"# Example of a picture that was wrongly classified.\ntrained_model = nn_model(train_images, train_labels, n_h, num_iterations = 50)\n\nindex = 11\nplt.imshow(dev_images[:,index].reshape((IMAGE_HEIGHT, IMAGE_HEIGHT, 3)))\npredictions = predict(trained_model[\"parameters\"], dev_images[:,index:index+1])\nprint (\"y = {}, you predicted that it is a {}\".format(dev_labels[0,index],int(predictions)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ad414f3f37a2e99da5a2b72574de86a5513ca6e1"},"cell_type":"code","source":"print(\"num of training data:{}\".format(train_images.shape[1]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2177308fd0dd871cf98904c62008f2b4c92efa64"},"cell_type":"code","source":"# This may take about 2 minutes to run\nm = 180\nhidden_layer_sizes = [1, 2, 3, 4, 5, 20, 50]\nlearning_rate=1.2\nnum_iterations=50\ntrained_models = []\n\nfor n_h in hidden_layer_sizes:\n    trained_model = nn_model(train_images, train_labels, n_h, num_iterations=num_iterations, learning_rate=learning_rate)\n    predictions = predict(trained_model[\"parameters\"], dev_images)\n    accuracy = float((np.dot(dev_labels,predictions.T) + np.dot(1-dev_labels,1-predictions.T))/float(dev_labels.size)*100)\n    print (\"Accuracy for {} hidden units: {} %\".format(n_h, accuracy))\n    ## cache trained_model\n    trained_model[\"accuracy\"] = accuracy\n    trained_model[\"m\"] = m\n    trained_model[\"num_iterations\"] = num_iterations\n    trained_models.append(trained_model)\n    ## plot costs\n    plt.figure(num=None, figsize=(15, 6), dpi=50, facecolor='w', edgecolor='k')\n    plot_costs(trained_model)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"08c1d7cf48f4c9d2490c1b464970510bfa32a0b5"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"200e32e71049d4b5beb61308f9d2ebcaadfa9af1"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}