{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Best articl for this problem\n\nhttps://gsurma.medium.com/image-classifier-cats-vs-dogs-with-convolutional-neural-networks-cnns-and-google-colabs-4e9af21ae7a8"},{"metadata":{},"cell_type":"markdown","source":"# Import Libraries"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator, load_img\nimport numpy as np\nimport pandas as pd\nimport cv2\nfrom matplotlib import pyplot as plt\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, Dropout, Dense, MaxPooling2D, BatchNormalization, Flatten\nimport random\nimport os\nfrom zipfile import ZipFile\nfrom keras.optimizers import Adam\nfrom sklearn.model_selection import train_test_split\nfrom PIL import Image\nfrom keras.callbacks import EarlyStopping, ReduceLROnPlateau","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Get Data from Zipfile"},{"metadata":{"trusted":true},"cell_type":"code","source":"ZipFile(\"../input/dogs-vs-cats/train.zip\",\"r\").extractall()\nZipFile(\"../input/dogs-vs-cats/test1.zip\", \"r\").extractall()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Define Constants"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_dir = \"./train/\"\ntest_dir = \"./test1/\"\nIMG_HEIGHT = 128\nIMG_WIDTH = 128\nIMG_CHANNEL = 3\nIMG_SIZE = (IMG_WIDTH, IMG_HEIGHT)\nbatch_size = 64\nepochs = 50","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Prepare Data"},{"metadata":{"trusted":true},"cell_type":"code","source":"Dtrain = os.listdir(train_dir)\ncategories = []\nfor filename in Dtrain:\n    category = filename.split(\".\")[0]\n    if category == \"dog\":\n        categories.append(\"dog\")\n    else:\n        categories.append(\"cat\")\ndf = pd.DataFrame({\n    \"filename\" : Dtrain,\n    \"category\" : categories\n})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(df.head())\nprint(df.tail())","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Plot Data"},{"metadata":{"trusted":true},"cell_type":"code","source":"df.category.value_counts().plot.bar()\nplt.title(str(len(df[df['category'] == 'cat']))+\" cats\" + \" | \" +str(len(df[df['category'] == 'dog']))+\" dogs\", fontsize=18)\nplt.ylabel(len(df), fontsize = 14)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Build Model"},{"metadata":{"trusted":true},"cell_type":"code","source":"model=Sequential()\n\nmodel.add(Conv2D(32, (3,3), input_shape=(IMG_WIDTH, IMG_HEIGHT, IMG_CHANNEL), activation=\"relu\"))\nmodel.add((MaxPooling2D(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(64, (3,3), activation=\"relu\"))\nmodel.add((MaxPooling2D(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(128, (3,3), activation=\"relu\"))\nmodel.add((MaxPooling2D(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\nmodel.add(Dense(1024, activation=\"relu\"))\nmodel.add(Dropout(0.25))\n\nmodel.add(Dense(2, activation=\"sigmoid\"))\n\nmodel.compile(loss='binary_crossentropy',optimizer=Adam(lr=0.0001, decay=1e-6),metrics=['accuracy'])\n\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Show sample"},{"metadata":{"trusted":true},"cell_type":"code","source":"sampel = random.choice(df.filename)\nimg = load_img(train_dir+sampel)\nplt.imshow(img)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Split Data to Train and Validation set"},{"metadata":{"trusted":true},"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":{},"cell_type":"markdown","source":"# Plot Train and Validation"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.category.value_counts().plot.bar()\nplt.ylabel(len(train_df), fontsize = 14)\nplt.title(str(len(train_df[train_df['category'] == 'dog']))+\" Dogs  |  \"+str((len(train_df[train_df['category'] == 'cat'])))+\" Cats\", fontsize=18)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"validate_df.category.value_counts().plot.bar()\nplt.ylabel(len(train_df), fontsize = 14)\nplt.title(str(len(validate_df[validate_df['category'] == 'cat']))+\" Cats  |  \"+str((len(validate_df[validate_df['category'] == 'dog'])))+\" Dogs\", fontsize=18)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Callbacks"},{"metadata":{"trusted":true},"cell_type":"code","source":"earlystop = EarlyStopping(patience=10)\n\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            patience=2, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.00001)\n\ncallbacks = [earlystop, learning_rate_reduction]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Traning Generator"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    rotation_range=20,\n    rescale=1./255,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    width_shift_range=0.1,\n    height_shift_range=0.1\n)\n\ntrain_generator = train_datagen.flow_from_dataframe(\n    train_df, \n    \"./train/\", \n    x_col='filename',\n    y_col='category',\n    target_size=IMG_SIZE,\n    class_mode='categorical',\n    batch_size=batch_size,\n    color_mode=\"rgb\",\n    shuffle=True\n)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Validation Generator"},{"metadata":{"trusted":true},"cell_type":"code","source":"validation_datagen = ImageDataGenerator(rescale=1./255)\n\nvalidation_generator = validation_datagen.flow_from_dataframe(\n    validate_df, \n    \"./train/\", \n    x_col='filename',\n    y_col='category',\n    target_size=IMG_SIZE,\n    class_mode='categorical',\n    batch_size=batch_size,\n    color_mode=\"rgb\",\n    shuffle=True\n)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Example Generator"},{"metadata":{"trusted":true},"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    \"./train/\", \n    x_col='filename',\n    y_col='category',\n    target_size=IMG_SIZE,\n    class_mode='categorical',\n    color_mode=\"rgb\",\n    shuffle=True\n)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Show how Generator works"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(12,12))\nfor i in range(0,8):\n    plt.subplot(2, 4, i+1)\n    for x_batch, y_batch in example_generator:\n        img = x_batch[0]\n        plt.imshow(img)\n        break\nplt.tight_layout()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Fit Model"},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit_generator(\n    train_generator,\n    steps_per_epoch=len(train_df)//64,\n    epochs=epochs,\n    validation_data=validation_generator,\n    validation_steps=len(validate_df)//64,\n    callbacks=callbacks\n)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Save model's weights"},{"metadata":{"trusted":true},"cell_type":"code","source":"model.save_weights(\"CatsVsDogs.h5\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Virtualize Training"},{"metadata":{"trusted":true},"cell_type":"code","source":"fig,(ax1, ax2) = plt.subplots(2, 1, figsize = (12,12))\nax1.plot(history.history[\"loss\"], color= \"red\", label = \"Training loss\")\nax1.plot(history.history[\"val_loss\"], color= \"b\", label = \"Validation loss\")\n\nax1.set_xticks(np.arange(1, epochs, 1))\nax1.set_yticks(np.arange(0, 1, 0.1))\n\nax2.plot(history.history[\"accuracy\"], color = \"red\", label = \"Training accuracy\")\nax2.plot(history.history[\"val_accuracy\"], color = \"b\", label = \"Validation accuracy\")\n\nax2.set_xticks(np.arange(1,epochs, 1))\nax2.set_yticks(np.arange(0, 1, 0.1))\n\nlegend = plt.legend(loc = \"best\", shadow = True)\nplt.tight_layout()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Prepare Test set"},{"metadata":{"trusted":true},"cell_type":"code","source":"Dtest = os.listdir(test_dir)\ndf_test = pd.DataFrame({\n    \"filename\" : Dtest\n})","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Test Generator"},{"metadata":{"trusted":true},"cell_type":"code","source":"test_dataGen = ImageDataGenerator(1./255)\n\ntest_gen = test_dataGen.flow_from_dataframe(\n    df_test,\n    test_dir,\n    target_size = IMG_SIZE,\n    x_col = \"filename\",\n    y_col = None,\n    batch_size = batch_size,\n    class_mode = None,\n    shuffle = True,\n    color_mode=\"rgb\",\n\n)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Predict Model"},{"metadata":{"trusted":true},"cell_type":"code","source":"predicted = model.predict(test_gen)\ndf_test[\"category\"] = np.argmax(predicted, axis=-1)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Show model accuracy"},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"Train Accuracy:{:.3f}\".format(history.history['accuracy'][-1]))\nprint(\"Test Accuracy:{:.3f}\".format(history.history['val_accuracy'][-1]))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Plot Test Data"},{"metadata":{"trusted":true},"cell_type":"code","source":"df_test[\"category\"] = df_test[\"category\"].replace({1 : \"cat\", 0 : \"dog\"})\ndf_test['category'].value_counts().plot.bar()\nplt.ylabel(len(df_test), fontsize = 14)\nplt.title(str(len(df_test[df_test['category'] == 'dog']))+\" Dogs  |  \"+str((len(df_test[df_test['category'] == 'cat'])))+\" Cats\", fontsize=18)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Show the result"},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_test = df_test.head(18)\nsample_test.head()\nplt.figure(figsize=(12, 24))\nfor index, row in sample_test.iterrows():\n    filename = row['filename']\n    category = row['category']\n    img = load_img(test_dir+filename, target_size=IMG_SIZE)\n    plt.subplot(6, 3, index+1)\n    plt.imshow(img)\n    plt.xlabel(\"{}\".format(category))\nplt.tight_layout()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Submission"},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df = df_test.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":{"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}