{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"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\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 read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-11T04:41:30.822719Z","iopub.execute_input":"2022-01-11T04:41:30.823350Z","iopub.status.idle":"2022-01-11T04:41:30.859773Z","shell.execute_reply.started":"2022-01-11T04:41:30.823196Z","shell.execute_reply":"2022-01-11T04:41:30.859013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extrac zip files\nimport zipfile\ndef extract_images(filePath):\n    with zipfile.ZipFile(filePath,\"r\") as z:\n        z.extractall(\".\")\n# Test\nextract_images('/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip')\nextract_images('/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip')","metadata":{"execution":{"iopub.status.busy":"2022-01-11T04:41:30.861524Z","iopub.execute_input":"2022-01-11T04:41:30.861794Z","iopub.status.idle":"2022-01-11T04:41:57.011557Z","shell.execute_reply.started":"2022-01-11T04:41:30.861758Z","shell.execute_reply":"2022-01-11T04:41:57.010704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot to see how it looks\nfrom matplotlib import pyplot as plt\n%matplotlib inline\nclasses = ['cat','dog']\ndef plot_sample(x, y, index):\n    plt.figure(figsize=(15,2))\n    plt.imshow(train_x[index])\n    # plt.ylabel(x[index])\n    plt.xlabel(classes[y[index]])","metadata":{"execution":{"iopub.status.busy":"2022-01-11T04:41:57.014007Z","iopub.execute_input":"2022-01-11T04:41:57.014291Z","iopub.status.idle":"2022-01-11T04:41:57.021529Z","shell.execute_reply.started":"2022-01-11T04:41:57.014259Z","shell.execute_reply":"2022-01-11T04:41:57.020518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extract the first 3 letters from the image names, to generate one hot encoding labels\ndef label_pet_image(img):\n    pet = img.split('.')[-3]\n    if pet == 'cat': return 0\n    elif pet == 'dog': return 1","metadata":{"execution":{"iopub.status.busy":"2022-01-11T04:41:57.023202Z","iopub.execute_input":"2022-01-11T04:41:57.023979Z","iopub.status.idle":"2022-01-11T04:41:57.035820Z","shell.execute_reply.started":"2022-01-11T04:41:57.023938Z","shell.execute_reply":"2022-01-11T04:41:57.035169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Process the data (both train and test set)\nfrom tqdm import tqdm\nimport cv2\nfrom random import shuffle\nIMG_SIZE=64\n# set sample size\nSAMPLE_SIZE=20000\ndef process_data(DATA_FOLDER, isTrain=True):\n    # Read images from extracted directory\n    image_list = os.listdir(DATA_FOLDER)\n    filtered_image_list = image_list[0:SAMPLE_SIZE if isTrain else len(image_list)]\n    data_df = [] # data frame\n    for img in tqdm(filtered_image_list):\n        path = os.path.join(DATA_FOLDER,img)\n        if(isTrain):\n            label = label_pet_image(img)\n        else:\n            label = img.split('.')[0]\n        img = cv2.imread(path,cv2.IMREAD_COLOR)\n        try:\n            img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))\n            data_df.append([np.array(img),label])\n        except Exception as e:\n            print(\"Image with issue name, path, isTrain, label\", img, path, isTrain, label)\n            print(str(e))            \n    shuffle(data_df)\n    return data_df","metadata":{"execution":{"iopub.status.busy":"2022-01-11T04:41:57.038572Z","iopub.execute_input":"2022-01-11T04:41:57.038895Z","iopub.status.idle":"2022-01-11T04:41:57.196248Z","shell.execute_reply.started":"2022-01-11T04:41:57.038857Z","shell.execute_reply":"2022-01-11T04:41:57.195476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = process_data('./train')","metadata":{"execution":{"iopub.status.busy":"2022-01-11T04:41:57.197788Z","iopub.execute_input":"2022-01-11T04:41:57.198063Z","iopub.status.idle":"2022-01-11T04:42:33.847769Z","shell.execute_reply.started":"2022-01-11T04:41:57.198026Z","shell.execute_reply":"2022-01-11T04:42:33.847009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = process_data('./test', False)","metadata":{"execution":{"iopub.status.busy":"2022-01-11T04:42:33.849200Z","iopub.execute_input":"2022-01-11T04:42:33.849622Z","iopub.status.idle":"2022-01-11T04:42:56.281251Z","shell.execute_reply.started":"2022-01-11T04:42:33.849579Z","shell.execute_reply":"2022-01-11T04:42:56.280506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot to see how it looks\nfrom matplotlib import pyplot as plt\n%matplotlib inline\ndef plot_sample(data, isTrain=True):\n    img = data[0]\n    plt.figure(figsize=(15,2))\n    plt.imshow(img)\n    if isTrain:\n        plt.xlabel(classes[data[1]])","metadata":{"execution":{"iopub.status.busy":"2022-01-11T04:42:56.282560Z","iopub.execute_input":"2022-01-11T04:42:56.283354Z","iopub.status.idle":"2022-01-11T04:42:56.291300Z","shell.execute_reply.started":"2022-01-11T04:42:56.283315Z","shell.execute_reply":"2022-01-11T04:42:56.290611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(train[0])","metadata":{"execution":{"iopub.status.busy":"2022-01-11T04:42:56.292444Z","iopub.execute_input":"2022-01-11T04:42:56.292715Z","iopub.status.idle":"2022-01-11T04:42:56.492772Z","shell.execute_reply.started":"2022-01-11T04:42:56.292677Z","shell.execute_reply":"2022-01-11T04:42:56.491912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(test[4], False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prepare the train data\nX = np.array([i[0] for i in train]).reshape(-1,IMG_SIZE,IMG_SIZE,3)\ny = np.array([i[1] for i in train])\ntest_X = np.array([i[0] for i in test]).reshape(-1,IMG_SIZE,IMG_SIZE,3)","metadata":{"execution":{"iopub.status.busy":"2022-01-11T04:42:56.757744Z","iopub.execute_input":"2022-01-11T04:42:56.758190Z","iopub.status.idle":"2022-01-11T04:42:56.933700Z","shell.execute_reply.started":"2022-01-11T04:42:56.758149Z","shell.execute_reply":"2022-01-11T04:42:56.932690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# normalize our data in 0-1\nX = X/255\ntest_X = test_X/255","metadata":{"execution":{"iopub.status.busy":"2022-01-11T04:42:56.935117Z","iopub.execute_input":"2022-01-11T04:42:56.935410Z","iopub.status.idle":"2022-01-11T04:42:57.925312Z","shell.execute_reply.started":"2022-01-11T04:42:56.935370Z","shell.execute_reply":"2022-01-11T04:42:57.924485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntrain_X, validation_X, train_y, validation_y = train_test_split(X, y, test_size=0.3)","metadata":{"execution":{"iopub.status.busy":"2022-01-11T04:42:57.926696Z","iopub.execute_input":"2022-01-11T04:42:57.926961Z","iopub.status.idle":"2022-01-11T04:42:59.272458Z","shell.execute_reply.started":"2022-01-11T04:42:57.926924Z","shell.execute_reply":"2022-01-11T04:42:59.271609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_y","metadata":{"execution":{"iopub.status.busy":"2022-01-11T04:42:59.275462Z","iopub.execute_input":"2022-01-11T04:42:59.275747Z","iopub.status.idle":"2022-01-11T04:42:59.282850Z","shell.execute_reply.started":"2022-01-11T04:42:59.275711Z","shell.execute_reply":"2022-01-11T04:42:59.282157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\n# First apply Artificial neural network (ANN)\nann = keras.Sequential([\n    keras.layers.Flatten(input_shape=(IMG_SIZE, IMG_SIZE, 3)), # Flaten 3d to 1d\n    keras.layers.Dense(3000, activation='relu'), # more hidden layer gives better perf\n    keras.layers.Dense(1000, activation='relu'), # more hidden layer gives better perf\n    keras.layers.Dense(100, activation='relu'), # more hidden layer gives better perf\n    keras.layers.Dense(1, activation='sigmoid')    \n])\nann.compile(optimizer='SGD', loss= keras.losses.BinaryCrossentropy(), metrics=['accuracy'])\nann.fit(train_X, train_y, epochs=10)","metadata":{"execution":{"iopub.status.busy":"2022-01-11T04:42:59.284318Z","iopub.execute_input":"2022-01-11T04:42:59.284805Z","iopub.status.idle":"2022-01-11T04:43:49.025544Z","shell.execute_reply.started":"2022-01-11T04:42:59.284766Z","shell.execute_reply":"2022-01-11T04:43:49.024808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ann.evaluate(validation_X, validation_y)","metadata":{"execution":{"iopub.status.busy":"2022-01-11T04:43:49.027014Z","iopub.execute_input":"2022-01-11T04:43:49.027377Z","iopub.status.idle":"2022-01-11T04:43:50.351851Z","shell.execute_reply.started":"2022-01-11T04:43:49.027337Z","shell.execute_reply":"2022-01-11T04:43:50.351099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_y = ann.predict(test_X)","metadata":{"execution":{"iopub.status.busy":"2022-01-11T04:43:50.353290Z","iopub.execute_input":"2022-01-11T04:43:50.353557Z","iopub.status.idle":"2022-01-11T04:43:52.291407Z","shell.execute_reply.started":"2022-01-11T04:43:50.353520Z","shell.execute_reply":"2022-01-11T04:43:52.290564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_y[0:10]","metadata":{"execution":{"iopub.status.busy":"2022-01-11T04:43:52.292891Z","iopub.execute_input":"2022-01-11T04:43:52.293154Z","iopub.status.idle":"2022-01-11T04:43:52.300069Z","shell.execute_reply.started":"2022-01-11T04:43:52.293115Z","shell.execute_reply":"2022-01-11T04:43:52.299297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(0,10):\n    plot_sample(test[i], False)","metadata":{"execution":{"iopub.status.busy":"2022-01-11T04:53:31.489964Z","iopub.execute_input":"2022-01-11T04:53:31.490270Z","iopub.status.idle":"2022-01-11T04:53:33.044407Z","shell.execute_reply.started":"2022-01-11T04:53:31.490216Z","shell.execute_reply":"2022-01-11T04:53:33.043428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Apply CNN\ncnn = keras.Sequential([\n    # cnn layer\n    keras.layers.Conv2D(filters=IMG_SIZE, kernel_size=(3,3), activation='relu', input_shape=(IMG_SIZE, IMG_SIZE,3)),\n    keras.layers.MaxPooling2D(2,2),\n    # second layer for cnn\n    keras.layers.Conv2D(filters=IMG_SIZE, kernel_size=(3,3), activation='relu'),\n    keras.layers.MaxPooling2D(2,2),\n    # dense layer\n    keras.layers.Flatten(), # Flaten 3d to 1d\n    keras.layers.Dense(IMG_SIZE, activation='relu'),    # cnn will already reduce layers so need less here\n    keras.layers.Dense(1, activation='softmax') # softmax will normalize probability   \n])\ncnn.compile(optimizer='SGD', loss= keras.losses.BinaryCrossentropy(), metrics=['accuracy'])\ncnn.fit(train_X, train_y, epochs=5)","metadata":{"execution":{"iopub.status.busy":"2022-01-11T05:08:38.171374Z","iopub.execute_input":"2022-01-11T05:08:38.172364Z","iopub.status.idle":"2022-01-11T05:09:01.019987Z","shell.execute_reply.started":"2022-01-11T05:08:38.172320Z","shell.execute_reply":"2022-01-11T05:09:01.019100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.evaluate(validation_X, validation_y)","metadata":{"execution":{"iopub.status.busy":"2022-01-11T04:49:42.561578Z","iopub.execute_input":"2022-01-11T04:49:42.562298Z","iopub.status.idle":"2022-01-11T04:49:44.112280Z","shell.execute_reply.started":"2022-01-11T04:49:42.562248Z","shell.execute_reply":"2022-01-11T04:49:44.111530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_y = ann.predict(test_X)","metadata":{"execution":{"iopub.status.busy":"2022-01-11T04:49:56.291307Z","iopub.execute_input":"2022-01-11T04:49:56.291965Z","iopub.status.idle":"2022-01-11T04:49:59.024827Z","shell.execute_reply.started":"2022-01-11T04:49:56.291919Z","shell.execute_reply":"2022-01-11T04:49:59.024014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_y[0:10]","metadata":{"execution":{"iopub.status.busy":"2022-01-11T04:50:03.791759Z","iopub.execute_input":"2022-01-11T04:50:03.792323Z","iopub.status.idle":"2022-01-11T04:50:03.798066Z","shell.execute_reply.started":"2022-01-11T04:50:03.792281Z","shell.execute_reply":"2022-01-11T04:50:03.797375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(0,10):\n    plot_sample(test[i], False)","metadata":{"execution":{"iopub.status.busy":"2022-01-11T05:05:27.407688Z","iopub.execute_input":"2022-01-11T05:05:27.408024Z","iopub.status.idle":"2022-01-11T05:05:28.629988Z","shell.execute_reply.started":"2022-01-11T05:05:27.407988Z","shell.execute_reply":"2022-01-11T05:05:28.629131Z"},"trusted":true},"execution_count":null,"outputs":[]}]}