{"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\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\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\nprint('Session Started')\n\n# You can write up to 5GB 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","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import cv2\nimport zipfile\nimport gc\nimport matplotlib.pyplot as plt\n%matplotlib inline\nfiledir = '../input/dogs-vs-cats-redux-kernels-edition/train.zip'\ntestdir = '../input/dogs-vs-cats-redux-kernels-edition/test.zip'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"archive = zipfile.ZipFile(filedir,'r')\narchive.extractall()\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def convert_zip_2_np(myfile):\n    imglist = []\n    label = []\n    c = -1\n    for dirname, _, filenames in os.walk(myfile):\n        for filename in filenames:\n            c+=1\n            if c<2600:\n                img_path = os.path.join(dirname, filename)\n                img = cv2.imread(img_path,cv2.IMREAD_COLOR)\n                img_class = filename[:3]\n                img_resize = cv2.resize(img, (400,300))\n    #             img = Image.open(img_path)\n    #             img=img.resize((400,300), Image.ANTIALIAS)\n                imglist.append(img_resize)\n                label.append(img_class)\n    return label,np.array(imglist)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels,x = convert_zip_2_np('train/')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.DataFrame(labels)\ndel labels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"gc.collect()\ntrain.columns = ['label']\ny = train['label'].str.get_dummies()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y = y['cat']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom tensorflow.python import keras\nfrom tensorflow.python.keras.models import Sequential\nfrom tensorflow.python.keras.layers import Dense, Flatten, Conv2D, Dropout, MaxPooling2D, Activation\n\n\nimg_rows, img_cols = 300, 400\nnum_classes = 1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(20, kernel_size=(3, 3),\n                 activation='relu',\n                 input_shape=(img_rows, img_cols, 3)))\n# model.add(MaxPooling2D(pool_size=(2, 2)))\n# model.add(Conv2D(20, kernel_size=(5, 5), activation='relu'))\n# model.add(MaxPooling2D(pool_size=(2, 2)))\n# model.add(Flatten())\n# model.add(Dense(128, activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel.add(Conv2D(32, (3, 3)))\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel.add(Conv2D(64, (3, 3)))\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel.add(Flatten())\nmodel.add(Dense(128))\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(16))\nmodel.add(Dense(num_classes, activation='sigmoid'))\n\nmodel.compile(loss=keras.losses.binary_crossentropy,\n              optimizer='rmsprop',\n              metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit(x, y,\n          batch_size=20,\n          epochs=10,\n          validation_split = 0.3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.evaluate(x,y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"archive_test = zipfile.ZipFile(testdir,'r')\narchive_test.extractall()\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def convert_zip_2_np_test(myfile):\n    imglist = []\n    ids = []\n    for dirname, _, filenames in os.walk(myfile):\n        for filename in filenames:\n            ids.append(filename[:-4])\n            img_path = os.path.join(dirname, filename)\n            img = cv2.imread(img_path,cv2.IMREAD_COLOR)\n            img_resize = cv2.resize(img, (400,300))\n            imglist.append(img_resize)\n    return np.array(imglist),ids","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_x,test_id = convert_zip_2_np_test('test/')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"prediction = model.predict(test_x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"gc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"del x","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"prediction[:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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":4}