{"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 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# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"from PIL import Image\nfrom zipfile import ZipFile\nfrom tqdm import tqdm\nfrom cv2 import cv2\nfrom keras.layers import Conv2D,Dense,Flatten\nfrom keras.models import Sequential\nimport keras\nimport tensorflow","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_width = 150\nimg_height = 150\nTRAIN_DIR = '/kaggle/input/dogs-vs-cats-redux-kernels-edition/train'\nTEST_DIR = '/kaggle/input/dogs-vs-cats-redux-kernels-edition/test'\ntrain_images_dogs_cats = []\ntest_images_dogs_cats= []","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels = []\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for img in tqdm(os.listdir(TRAIN_DIR)):\n    \n    try:\n        \n        imgr = cv2.imread(os.path.join(TRAIN_DIR,img))\n        train_images_dogs_cats.append(cv2.resize(imgr,(150,150),interpolation=cv2.INTER_CUBIC))\n        \n        if 'dog' in img:\n                labels.append(1)\n        else:\n                labels.append(0)\n    except Exception as e:\n        \n        print(\"bad image\")\n        \n       ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nfrom sklearn.model_selection import train_test_split","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(len(train_images_dogs_cats))\nprint(len(labels))\nprint(train_images_dogs_cats[1].shape)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train,y_train,x_test,y_test = train_test_split(train_images_dogs_cats,labels, test_size = 0.2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(len(x_train))\nprint(len(y_train))\nprint(len(x_test))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mobilenet_path = '../input/mobilenet/mobilenet_1_0_224_tf_no_top.h5'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = Sequential()\nmodel.add(keras.applications.mobilenet.MobileNet(input_shape=(150,150,3), alpha=1.0, depth_multiplier=1, dropout=1e-3, include_top=False, weights=mobilenet_path, input_tensor=None, pooling=None, classes=1000))\nmodel.add(Flatten())\nmodel.add(Dense(128,activation='relu'))\nmodel.add(Dense(32,activation = 'relu'))\nmodel.add(Dense(2,activation='softmax'))\nmodel.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit(np.array(x_train),np.array(x_test),epochs=3,validation_data=(np.array(y_train),np.array(y_test)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_data_labels = []\ntest_data_image = []","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for img in tqdm(os.listdir('/kaggle/input/dogs-vs-cats-redux-kernels-edition/test')):\n    try:\n        imgr = cv2.imread(os.path.join(TEST_DIR,img))\n        test_data_image.append(cv2.resize(imgr,(150,150),interpolation=cv2.INTER_CUBIC))\n    except Exception as e:\n        print(',')\n        \n        \n    \n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(test_data_image[12499].shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\ncount = 0\nfor img in tqdm(os.listdir('/kaggle/input/dogs-vs-cats-redux-kernels-edition/test')):\n    count = count+1\n    if count>10:\n        break;\n    try:\n        imgr = cv2.imread(os.path.join(TEST_DIR,img))\n        #test_data_image.append(cv2.resize(imgr,(150,150),interpolation=cv2.INTER_CUBIC))\n        imgplot = plt.imshow(imgr)\n        plt.show()\n    except Exception as e:\n        print(',')\n        \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_data_labels = model.predict(np.array(test_data_image))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(test_data_labels[1])\nif np.argmax(test_data_labels[9]) == 1: \n        str_predicted='Dog'\nelse: \n        str_predicted='Cat'\nprint(str_predicted)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"solution = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"solution.label = test_data_labels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"solution.to_csv(\"dogsVScats.csv\", index = False)","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}