{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing\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\n\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import tensorflow.keras\nimport matplotlib.pyplot as plt\nimport random\nfrom keras .preprocessing.image import load_img,ImageDataGenerator\nprint(os.listdir(\"../input/\"))\nfrom sklearn.model_selection import train_test_split","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import zipfile","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nwith zipfile.ZipFile(\"../input/dogs-vs-cats/train.zip\",\"r\") as z:\n    z.extractall(\".\")\n    \n    \nwith zipfile.ZipFile(\"../input/dogs-vs-cats/test1.zip\",\"r\") as z:\n    z.extractall(\".\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!ls  train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"filenames=os.listdir(\"./train\")\ncategories=[]\nfor filename in filenames:\n    category=filename.split(\".\")[0]\n    if category=='dog':\n        categories.append(1)\n    else:\n        categories.append(0)\n    \ndf=pd.DataFrame({'filename':filenames,'category':categories})\n\n#Cat is 0, Dog is 1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df['category'].value_counts().plot.bar(color='g')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#!pip install Pillow\nimport PIL\n\nimage=PIL.Image.open(\"./train/dog.9374.jpg\")\n\nwidth,height=image.size\n\nprint(\"Width and Height are: \",width,\"and \",height)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"random_file=random.choice(filenames)\nimg=load_img(\"./train/\"+random_file)\n\nplt.imshow(img)\nx=df.loc[df['filename'] == random_file]\n#print(df[df['filename']==random_file])\nprint(x['category'])\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":" # Creating Model"},{"metadata":{"trusted":true},"cell_type":"code","source":"FAST_RUN = False\nimage_width=128\nimage_height=128\nimage_size=(image_width, image_height)\nimage_channels=3","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Conv2D,MaxPooling2D,Dropout,Flatten,Dense, Activation,BatchNormalization\n\n\nmymodel=Sequential()\nmymodel.add(Conv2D(32,(3,3),activation='relu',input_shape=(image_width,image_height,image_channels)))\nmymodel.add(BatchNormalization())\nmymodel.add(MaxPooling2D(pool_size=(2,2)))\nmymodel.add(Dropout(0.25))\n\n\n#Layer 2\nmymodel.add(Conv2D(64,(3,3),activation='relu'))\nmymodel.add(BatchNormalization())\nmymodel.add(MaxPooling2D(pool_size=(2,2)))\nmymodel.add(Dropout(0.25))\n\n#Layer 3\nmymodel.add(Conv2D(128,(3,3),activation='relu'))\nmymodel.add(BatchNormalization())\nmymodel.add(MaxPooling2D(pool_size=(2,2)))\nmymodel.add(Dropout(0.25))\n\n\n#Flat Layer\nmymodel.add(Flatten())\nmymodel.add(Dense(512,activation='relu'))\nmymodel.add(BatchNormalization())\nmymodel.add(Dropout(0.5))\n\nmymodel.add(Dense(2, activation='softmax'))\n\nmymodel.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])\n\nmymodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.callbacks import EarlyStopping, ReduceLROnPlateau","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Defining EarlyStopping to stop the model from learning, if it does not progress enough after 10 epochs"},{"metadata":{"trusted":true},"cell_type":"code","source":"earlystop=EarlyStopping(patience=10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learning_rate_reduction=ReduceLROnPlateau(monitor='val_loss',patience=2,verbose=1,factor=0.5,min_lr=0.00001)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"callbacks=[earlystop,learning_rate_reduction]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Prepare Data For Training\n## 1. Create One Hot Encoding for Labels"},{"metadata":{"trusted":true},"cell_type":"code","source":"df['category']=df['category'].replace({0:'cat',1:'dog'})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(df.shape[0])\ntrain_df,validate_df=train_test_split(df,test_size=0.20,random_state=42)\ntrain_df=train_df.reset_index(drop=True)\nvalidate_df=validate_df.reset_index(drop=True)\nprint(train_df.shape[0])\nprint(validate_df.shape[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df['category'].value_counts().plot.bar()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df_number=train_df.shape[0]\nvalidate_df_number=validate_df.shape[0]\nbatch_size=15\nprint(validate_df.shape[0])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":""},{"metadata":{"trusted":true},"cell_type":"code","source":"train_datagen=ImageDataGenerator(rotation_range=15,rescale=1./255,shear_range=0.1,\n                                zoom_range=0.2,horizontal_flip=True,width_shift_range=0.1,height_shift_range=0.1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_generator=train_datagen.flow_from_dataframe(train_df,'./train',x_col='filename',y_col='category',target_size=image_size,\n                                                 class_mode='categorical',batch_size=batch_size)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"validation_datagen = ImageDataGenerator(rescale=1./255)\nvalidation_generator = validation_datagen.flow_from_dataframe(\n    validate_df, \n    \"./train/\", \n    x_col='filename',\n    y_col='category',\n    target_size=image_size,\n    class_mode='categorical',\n    batch_size=batch_size\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"epochs=10\nhistory=mymodel.fit_generator(train_generator,epochs=epochs,validation_data=validation_generator,\n                             validation_steps=validate_df_number//batch_size,\n                              steps_per_epoch=train_df_number//batch_size,callbacks=callbacks)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mymodel.save_weights(\"mymodel.h5\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(history.history)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"_,(ax1, ax2)=plt.subplots(2,1,figsize=(12,12))\nax1.plot(history.history['loss'],color='b',label='Training Loss')\nax1.plot(history.history['val_loss'],color='r', label='Validation Loss')\nax1.set_xticks(np.arange(1,epochs,1))\nax1.set_yticks(np.arange(0,1,0.1))\n\nax2.plot(history.history['accuracy'],color='g',label='Training Accuracy')\nax2.plot(history.history['val_accuracy'],color='r',label='Validation Accuracy')\nax2.set_xticks(np.arange(1,epochs,1))\n\nlegend=plt.legend(loc='best',shadow=True)\nplt.tight_layout()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_filenames=os.listdir('./test1/')\n\ntest_df=pd.DataFrame({\n    'filename':test_filenames\n})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df.head()\nnb_samples=test_df.shape[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_gen=ImageDataGenerator(rescale=1./255)\n\ntest_generator=test_gen.flow_from_dataframe(test_df,\"./test1\",x_col='filename',y_col=None, class_mode=None,\n                                           target_size=image_size,batch_size=batch_size,shuffle=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predict=mymodel.predict_generator(test_generator,steps=np.ceil(nb_samples/batch_size))\n\nprint(predict)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df['category']=np.argmax(predict,axis=-1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"label_map = dict((v,k) for k,v in train_generator.class_indices.items())\ntest_df['category'] = test_df['category'].replace(label_map)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df['category']=test_df['category'].replace({'cat':0, \n                                                 'dog':1\n})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df['category'].value_counts().plot(kind='bar')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df=test_df.copy()\nsubmission_df['id']=submission_df['filename'].str.split('.').str[0]\nsubmission_df['label'] = submission_df['category']\nsubmission_df.drop(['filename', 'category'], axis=1, inplace=True)\nsubmission_df.to_csv('submission.csv', index=False)","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}