{"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 cv2\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"310d68f12992433ff688ce9ddb38704d996c02ce"},"cell_type":"code","source":"data=os.listdir(\"../input/train/train\") #path for image data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"99784977b2ae6021ac381057ea2b4f173f5d6ebb"},"cell_type":"code","source":"data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8f4d94cf2046ceca0e0660555de198f981d494a8"},"cell_type":"code","source":"categories=[] #empty list\nfor x in data:\n    category=x.split('.')[0]\n    if(category=='dog'):\n        categories.append(1)\n    else:\n        categories.append(0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c40df96765b939edcd259a1ea9f80b1243281a5e"},"cell_type":"code","source":"df=pd.DataFrame()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b70bf85e7d808889979e6b44859f4b3b161fc14b"},"cell_type":"code","source":"df[\"filename\"]=data\ndf[\"category\"]=categories","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9b9d499d57eccba423ab9236e5543c4875372516"},"cell_type":"code","source":"df.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"026a6c8957f66bf0293e71532ca6e61de9c83120"},"cell_type":"code","source":"df['category'].value_counts().plot.bar()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2757102021fce3bde7dc4fad493f9a4e183e6320"},"cell_type":"code","source":"df.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0e50e4cf71a05d1f343e6727636be44a296400d5"},"cell_type":"code","source":"import random\nsample=random.choice(data) #picking random sample from data list\nimg=cv2.imread(\"../input/train/train/\"+sample)\nplt.imshow(img,cmap=\"gray\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e039ed9f3134d705b21600325abeb8cfb6e0823a"},"cell_type":"code","source":"FAST_RUN = False\nIMAGE_WIDTH=128\nIMAGE_HEIGHT=128\nIMAGE_SIZE=(IMAGE_WIDTH, IMAGE_HEIGHT)\nIMAGE_CHANNELS=3 # RGB color","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fb9961c0f5ddaf325069ba7868029908a7eea2f2"},"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, Activation, BatchNormalization","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4173dbcdbd9b167c42548a372060d3fb5a54ce73"},"cell_type":"code","source":"model=Sequential()\nmodel.add(Conv2D(32,(3,3),activation='relu',input_shape=(IMAGE_WIDTH, IMAGE_HEIGHT,IMAGE_CHANNELS)))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(64,(3, 3), activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(128,(3, 3), activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\nmodel.add(Dense(512,activation=\"relu\"))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.25))\n\nmodel.add(Dense(1,activation=\"sigmoid\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b8752452ac32058d687c392293f966814b16879e"},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1f8ca730d8f875dd9c9f228825fe98572423321b"},"cell_type":"code","source":"model.compile(loss='binary_crossentropy',optimizer='rmsprop',metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"89947e71cbe70b7c8a06a14356e89ed14e0bb077"},"cell_type":"code","source":"from keras.callbacks import EarlyStopping, ReduceLROnPlateau","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"85ce9397ae9e3a959dd18771e75a70fc05984fb8"},"cell_type":"code","source":"earlystop = EarlyStopping(patience=10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a724357f5770f44d33c0b1ba5836efd2847f9a08"},"cell_type":"code","source":"learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',patience=2,verbose=1,factor=0.5,min_lr=0.00001)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e91dae5d993bcbb716fabf53223d6f6fe3d515e3"},"cell_type":"code","source":"callbacks = [earlystop, learning_rate_reduction]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"767337f95fe33172662fc22fc30027e5b384a331"},"cell_type":"code","source":"from sklearn.model_selection import train_test_split","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f09b4bb09660c16e4d83344b03e5d13129ee3bef"},"cell_type":"code","source":"train_df,validate_df=train_test_split(df,test_size=0.2,random_state=42)\ntrain_df = train_df.reset_index(drop='True')\nvalidate_df = validate_df.reset_index(drop='True')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"88b0b01f9d84aa18c6fe4a1d83a1d9949208ab82"},"cell_type":"code","source":"train_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"47974d4e571bec4d9cadded7afca298e8c84396f"},"cell_type":"code","source":"train_df['category'].value_counts().plot.bar()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"974580cca38fa070627a8bba9c5baf02e96956ae"},"cell_type":"code","source":"validate_df['category'].value_counts().plot.bar()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"85cf98c3a5ed278441e59e4b62b039b0660a7830"},"cell_type":"code","source":"train_df.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f90131ed1d6e17a921a2669227db83697b94db52"},"cell_type":"code","source":"validate_df.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8e704e06bb702c931a3f28ed52236348caf7acb1"},"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator, load_img\nfrom keras.utils import to_categorical","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"285fe121090b47d5d338fbd5137e227175a29b37"},"cell_type":"code","source":"train_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9b3658ece305905e108791322460bdb8c6c6f9ce"},"cell_type":"code","source":"train_df.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f1fd1348450e2eb48368c4417ca54a412bcd5395"},"cell_type":"code","source":"train_datagen=ImageDataGenerator(\n                    rotation_range=15,\n                    rescale=1./255,\n                    shear_range=0.1,\n                    zoom_range=0.2, # zoom range (1-0.2 to 1+0.2)\n                    horizontal_flip=True,\n                    width_shift_range=0.1,\n                    height_shift_range=0.1\n                 )\ntrain_generator=train_datagen.flow_from_dataframe(\n                    dataframe=train_df, \n                    directory=\"../input/train/train/\", \n                    x_col=\"filename\",\n                    y_col=\"category\",\n                    target_size=IMAGE_SIZE,\n                    class_mode='other',\n                    batch_size=15\n                )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"99042bcf0b3e67e076a2efc1096c0e36cd77e25f"},"cell_type":"code","source":"validation_datagen = ImageDataGenerator(rescale=1./255)\nvalidation_generator = validation_datagen.flow_from_dataframe(\n        validate_df, \n        \"../input/train/train/\", \n        x_col='filename',\n        y_col='category',\n        target_size=IMAGE_SIZE,\n        class_mode='other',\n        batch_size=15\n    )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"308930974f4a6840ee0a319db81bd235c376fd50"},"cell_type":"code","source":"example_df = train_df.sample(n=1).reset_index(drop=True)\nexample_generator = train_datagen.flow_from_dataframe(\n    example_df, \n    \"../input/train/train/\", \n    x_col='filename',\n    y_col='category',\n    target_size=IMAGE_SIZE,\n    class_mode='other'\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"30872336f7e7f055fb3db2c8da0a0b094cb561a3"},"cell_type":"code","source":"plt.figure(figsize=(12, 12))\nfor i in range(0, 15):\n    plt.subplot(5, 3, i+1)\n    for X_batch, Y_batch in example_generator:\n        image = X_batch[0]\n        plt.imshow(image)\n        #print(Y_batch[0])\n        break","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fe85ce7a6d9d6b829f7d2b28b21356cb3e34b810"},"cell_type":"code","source":"batch_size=15","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ab2eedf9c726520882a1002be194784c98d12b2d"},"cell_type":"code","source":"epochs=3\nhistory = model.fit_generator(\n    train_generator, \n    epochs=epochs,\n    validation_data=validation_generator,\n    validation_steps=train_df.shape[0]//batch_size,\n    steps_per_epoch=train_df.shape[0]//batch_size,\n    callbacks=callbacks\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d4a9a6e4ff99c88c65ebe32fb7a4bf8010c7bcda"},"cell_type":"code","source":"test_file=os.listdir(\"../input/test1/test1\")\ntest_df=pd.DataFrame()\ntest_df[\"filename\"]=test_file","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"edbb8899b03de63e3379187840a77eacb1289a2a"},"cell_type":"code","source":"test_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d332998c2ab0d2c1f1b3708437c08982b1f53eb2"},"cell_type":"code","source":"test_gen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_gen.flow_from_dataframe(\n    test_df, \n    \"../input/test1/test1/\", \n    x_col='filename',\n    y_col=None,\n    class_mode=None,\n    target_size=IMAGE_SIZE,\n    batch_size=batch_size,\n    shuffle=False\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e419406f441f59ef5bebccd7ef537b6587e03123"},"cell_type":"code","source":"predict = model.predict_generator(test_generator, steps=np.ceil(test_df.shape[0]/batch_size))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7e2981210221e98940947c88d59ea3eb4fd01497"},"cell_type":"code","source":"predict","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f5bdfcaeaafdb3a372f1a669f05d31552f7c3381"},"cell_type":"code","source":"threshold=0.5\ntest_df[\"category\"]=predict\ntest_df['category'] = np.where(test_df['category'] > threshold, 1,0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"1fd8db91c26c0d247b5c7dc77d1d9dbbb84e1fcd"},"cell_type":"code","source":"test_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fd980fc116a75a31cc8f127b87d3a41bc16f83f4"},"cell_type":"code","source":"sample=random.choice(test_file)\nprint(sample)\nimg=cv2.imread(\"../input/test1/test1/\"+sample)\nplt.imshow(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ad270e037b34329105708821cd64ff2cfd6f2287"},"cell_type":"code","source":"test_sample = test_df.head(10)\ntest_sample.head()\n\nplt.figure(figsize=(12, 24))\nfor index, row in test_sample.iterrows():\n    filename = row['filename']\n    category = row['category']\n    img = cv2.imread(\"../input/test1/test1/\"+filename)\n    plt.subplot(6, 3, index+1)\n    plt.imshow(img)\n    plt.xlabel(filename + '(' + \"{}\".format(category) + ')')\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f701e425c7fe4ed8b00a1bf59f04c0e46c748119"},"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}