text_plain_output_1.png
import cv2

import matplotlib.image as mpimg

import numpy as np

import os

import numpy as np
import pandas as pd
import zipfile
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import cv2
import random
import os

IMAGE_WIDTH = 128
IMAGE_HEIGHT = 128
IMAGE_SIZE = (IMAGE_WIDTH, IMAGE_HEIGHT)
IMAGE_CHANNELS = 3
TRAIN_DIRECTORY = '/kaggle/working/train/'
TEST_DIRECTORY = '/kaggle/working/test1'

def get_filenames(directory):
    filenames = os.listdir(directory)
    return filenames

def load_data(filenames, directory):
    i = 50
    i = len(filenames)
    X = []
    y = []
    for name in filenames:
        img = mpimg.imread(os.path.join(directory, name))
        X.append(cv2.resize(img, IMAGE_SIZE))
        cat = name.split('.')[0]
        if cat == 'dog':
            y.append(0)
        else:
            y.append(1)
        i -= 1
        if i <= 0:
            break
    return (X, y)

def refine_data(X, y):
    X = np.array(X)
    X = X.reshape(X.shape[0], -1)
    X = X.T
    y = np.array(y)
    y = y.reshape((1, y.shape[0]))
    return (X, y)

X, y = refine_data(X, y)

layer_dims = [X.shape[0], 20, 7, 5, 1]

def initialize_parameters(layer_dims):
    np.random.seed(3)
    parameters = {}
    L = len(layer_dims)
    for l in range(1, L):
        parameters['W' + str(l)] = np.random.randn(layer_dims[l], layer_dims[l - 1]) * 0.01
        parameters['b' + str(l)] = np.zeros((layer_dims[l], 1))
    return parameters

parameters = initialize_parameters(layer_dims)
parameters

def linear_fwd(A, W, b):
    Z = np.dot(W, A) + b
    cache = (A, W, b)
    return (Z, cache)

Z, cache = linear_fwd(X, parameters['W1'], parameters['b1'])
Z.shape

def sigmoid(Z):
    A = 1 / (1 + np.exp(-Z))
    cache = Z
    return (A, Z)
def relu(Z):
    A = np.maximum(Z, 0)
    cache = Z
    return (A, Z)

sigmoid(np.array([0, 2]))