{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport tensorflow as tf\nimport keras\nfrom tensorflow.keras.optimizers import Adam, SGD\nfrom keras.utils.np_utils import to_categorical\nfrom keras.models import Sequential\nfrom keras.layers import (Conv2D, MaxPooling2D, Flatten, Dense, \n                          Dropout, Rescaling, RandomFlip, RandomRotation, BatchNormalization)\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.metrics import accuracy_score, classification_report\nfrom tensorflow import keras\nfrom tensorflow.keras.preprocessing import image_dataset_from_directory\n\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline \n\nimport cv2\nimport zipfile\nimport os\nimport glob\nimport shutil","metadata":{"execution":{"iopub.status.busy":"2023-02-07T11:43:17.547326Z","iopub.execute_input":"2023-02-07T11:43:17.548638Z","iopub.status.idle":"2023-02-07T11:43:17.560828Z","shell.execute_reply.started":"2023-02-07T11:43:17.548591Z","shell.execute_reply":"2023-02-07T11:43:17.559822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Unzip photos","metadata":{}},{"cell_type":"code","source":"train_dir = 'train'\n\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip', 'r') as train_zip:\n    train_zip.extractall('')\n\ntrain_list = glob.glob(os.path.join(train_dir,'*.jpg'))\nprint(f\"Train Data: {len(train_list)}\")\n\nbase_dir = '/kaggle/working'\ntrain_dir = os.path.join(base_dir, 'train')","metadata":{"execution":{"iopub.status.busy":"2023-02-07T11:43:17.606935Z","iopub.execute_input":"2023-02-07T11:43:17.607241Z","iopub.status.idle":"2023-02-07T11:43:29.988478Z","shell.execute_reply.started":"2023-02-07T11:43:17.607214Z","shell.execute_reply":"2023-02-07T11:43:29.987430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Let’s visualize few data from training","metadata":{}},{"cell_type":"code","source":"fig = plt.figure(figsize=(16, 8))\nfor i, path in enumerate(train_list[:10], 1):\n    subplot = fig.add_subplot(2, 5, i)\n    subplot.set_title('%s' %path.split('/')[1].split('.')[0])\n    img = cv2.imread(path)[...,::-1]\n    img = cv2.resize(img, (224,224))\n    plt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T11:43:29.990901Z","iopub.execute_input":"2023-02-07T11:43:29.991251Z","iopub.status.idle":"2023-02-07T11:43:31.236383Z","shell.execute_reply.started":"2023-02-07T11:43:29.991216Z","shell.execute_reply":"2023-02-07T11:43:31.235321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_label(filename):\n  return filename.split('.jpg')[0].split('.')[0]","metadata":{"execution":{"iopub.status.busy":"2023-02-07T11:43:31.237308Z","iopub.execute_input":"2023-02-07T11:43:31.237637Z","iopub.status.idle":"2023-02-07T11:43:31.243249Z","shell.execute_reply.started":"2023-02-07T11:43:31.237607Z","shell.execute_reply":"2023-02-07T11:43:31.242389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Creating label and train directories","metadata":{}},{"cell_type":"code","source":"# Create labels directory\npath = '/kaggle/working'\nfor image in os.listdir(train_dir):\n  if not os.path.exists(os.path.join(path, get_label(image))):\n    os.makedirs(os.path.join(path, get_label(image)))\n  shutil.move(os.path.join(path+'/train', image),os.path.join(path, get_label(image), image))","metadata":{"execution":{"iopub.status.busy":"2023-02-07T11:43:31.244783Z","iopub.execute_input":"2023-02-07T11:43:31.245465Z","iopub.status.idle":"2023-02-07T11:43:32.153486Z","shell.execute_reply.started":"2023-02-07T11:43:31.245420Z","shell.execute_reply":"2023-02-07T11:43:32.152511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(os.listdir('/kaggle/working/dog')) + len(os.listdir('/kaggle/working/cat')))\nshutil.rmtree('/kaggle/working/train/')\nos.makedirs('/kaggle/working/images')\nshutil.move('/kaggle/working/cat', '/kaggle/working/images/')\nshutil.move('/kaggle/working/dog', '/kaggle/working/images/')\nprint(len(os.listdir('/kaggle/working/images/dog')) + len(os.listdir('/kaggle/working/images/cat')))","metadata":{"execution":{"iopub.status.busy":"2023-02-07T11:43:32.157042Z","iopub.execute_input":"2023-02-07T11:43:32.158297Z","iopub.status.idle":"2023-02-07T11:43:32.190611Z","shell.execute_reply.started":"2023-02-07T11:43:32.158247Z","shell.execute_reply":"2023-02-07T11:43:32.189432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data preprocesing","metadata":{}},{"cell_type":"code","source":"img_height,img_width = 224, 224\nbatch_size = 32\n\ntrain_ds = tf.keras.preprocessing.image_dataset_from_directory(\n  '/kaggle/working/images/',\n  validation_split=0.2,\n  subset=\"training\",\n  seed=42,\n  image_size=(img_height, img_width),\n  batch_size=batch_size,\n  shuffle=True,\n  label_mode='binary')\n     \n    \nval_ds = tf.keras.preprocessing.image_dataset_from_directory(\n  '/kaggle/working/images/',\n  validation_split=0.2,\n  subset=\"validation\",\n  seed=42,\n  image_size=(img_height, img_width),\n  batch_size=batch_size,\n  shuffle=True,\n  label_mode='binary')","metadata":{"execution":{"iopub.status.busy":"2023-02-07T11:43:32.192056Z","iopub.execute_input":"2023-02-07T11:43:32.192602Z","iopub.status.idle":"2023-02-07T11:43:36.824270Z","shell.execute_reply.started":"2023-02-07T11:43:32.192565Z","shell.execute_reply":"2023-02-07T11:43:36.823346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds","metadata":{"execution":{"iopub.status.busy":"2023-02-07T11:43:36.825753Z","iopub.execute_input":"2023-02-07T11:43:36.826095Z","iopub.status.idle":"2023-02-07T11:43:36.835475Z","shell.execute_reply.started":"2023-02-07T11:43:36.826061Z","shell.execute_reply":"2023-02-07T11:43:36.834275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds.class_names","metadata":{"execution":{"iopub.status.busy":"2023-02-07T11:43:36.836929Z","iopub.execute_input":"2023-02-07T11:43:36.839212Z","iopub.status.idle":"2023-02-07T11:43:36.847551Z","shell.execute_reply.started":"2023-02-07T11:43:36.839185Z","shell.execute_reply":"2023-02-07T11:43:36.846424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rescale = Sequential([\n  Rescaling(1./255)\n])","metadata":{"execution":{"iopub.status.busy":"2023-02-07T11:43:36.848872Z","iopub.execute_input":"2023-02-07T11:43:36.849281Z","iopub.status.idle":"2023-02-07T11:43:36.877194Z","shell.execute_reply.started":"2023-02-07T11:43:36.849246Z","shell.execute_reply":"2023-02-07T11:43:36.876375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_augmentation = Sequential([\n  RandomFlip(\"horizontal_and_vertical\"),\n  RandomRotation(0.2),\n])","metadata":{"execution":{"iopub.status.busy":"2023-02-07T11:43:36.878609Z","iopub.execute_input":"2023-02-07T11:43:36.879134Z","iopub.status.idle":"2023-02-07T11:43:36.894334Z","shell.execute_reply.started":"2023-02-07T11:43:36.879099Z","shell.execute_reply":"2023-02-07T11:43:36.893411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Plt some images after transform","metadata":{}},{"cell_type":"code","source":"image = tf.expand_dims(img, 0)\nplt.figure(figsize=(10, 10))\nfor i in range(9):\n  augmented_image = data_augmentation(image)\n  ax = plt.subplot(3, 3, i + 1)\n  plt.imshow(augmented_image[0])\n  plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2023-02-07T11:43:36.895828Z","iopub.execute_input":"2023-02-07T11:43:36.896126Z","iopub.status.idle":"2023-02-07T11:43:37.934929Z","shell.execute_reply.started":"2023-02-07T11:43:36.896102Z","shell.execute_reply":"2023-02-07T11:43:37.934046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Resnet50 - base model for transfer learning","metadata":{}},{"cell_type":"code","source":"base_model= tf.keras.applications.ResNet50(include_top=False,\n                   input_shape=(224,224,3),\n                   pooling='avg',\n                   weights='imagenet', classes=2)\nfor layer in base_model.layers:\n        layer.trainable=False","metadata":{"execution":{"iopub.status.busy":"2023-02-07T11:43:37.936318Z","iopub.execute_input":"2023-02-07T11:43:37.936887Z","iopub.status.idle":"2023-02-07T11:43:39.741332Z","shell.execute_reply.started":"2023-02-07T11:43:37.936852Z","shell.execute_reply":"2023-02-07T11:43:39.740250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-02-07T11:43:39.742891Z","iopub.execute_input":"2023-02-07T11:43:39.743290Z","iopub.status.idle":"2023-02-07T11:43:39.770590Z","shell.execute_reply.started":"2023-02-07T11:43:39.743252Z","shell.execute_reply":"2023-02-07T11:43:39.769432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Add 2 dense layers for correct classification","metadata":{}},{"cell_type":"code","source":"model = Sequential()\nmodel.add(base_model)\nmodel.add(Flatten())\nmodel.add(Dense(256, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dense(1, activation='sigmoid', kernel_regularizer=tf.keras.regularizers.l1(1e-4)))","metadata":{"execution":{"iopub.status.busy":"2023-02-07T12:04:09.767895Z","iopub.execute_input":"2023-02-07T12:04:09.768277Z","iopub.status.idle":"2023-02-07T12:04:10.478352Z","shell.execute_reply.started":"2023-02-07T12:04:09.768246Z","shell.execute_reply":"2023-02-07T12:04:10.477409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-02-07T12:04:10.480292Z","iopub.execute_input":"2023-02-07T12:04:10.481126Z","iopub.status.idle":"2023-02-07T12:04:10.498013Z","shell.execute_reply.started":"2023-02-07T12:04:10.481090Z","shell.execute_reply":"2023-02-07T12:04:10.497056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stop = EarlyStopping(monitor=\"val_loss\",min_delta=0, patience=5,\n                           verbose=0, mode=\"min\", baseline=None, restore_best_weights=True)\ncheck = ModelCheckpoint(filepath='convnet.keras', save_best_only=True, monitor='val_loss')\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_loss', \n                                            patience=3, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.00001)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T12:04:10.531994Z","iopub.execute_input":"2023-02-07T12:04:10.532268Z","iopub.status.idle":"2023-02-07T12:04:10.539460Z","shell.execute_reply.started":"2023-02-07T12:04:10.532242Z","shell.execute_reply":"2023-02-07T12:04:10.538391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=tf.keras.losses.categorical_crossentropy,\n              optimizer=SGD(learning_rate = 0.001, momentum = 0.1, decay = 0.0),\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-02-07T12:04:11.797715Z","iopub.execute_input":"2023-02-07T12:04:11.798080Z","iopub.status.idle":"2023-02-07T12:04:11.811479Z","shell.execute_reply.started":"2023-02-07T12:04:11.798047Z","shell.execute_reply":"2023-02-07T12:04:11.810551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_epoch = 10\nmodel_log = model.fit(train_ds,\n                      epochs=num_epoch,\n                      verbose=1,\n                      validation_data=val_ds,\n                      callbacks = [early_stop, check, learning_rate_reduction])\nmodel.save_weights('weights.h5')","metadata":{"execution":{"iopub.status.busy":"2023-02-07T12:04:12.698585Z","iopub.execute_input":"2023-02-07T12:04:12.699263Z","iopub.status.idle":"2023-02-07T12:09:15.333952Z","shell.execute_reply.started":"2023-02-07T12:04:12.699220Z","shell.execute_reply":"2023-02-07T12:09:15.332518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create test directory","metadata":{}},{"cell_type":"code","source":"test_dir = 'test'\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip', 'r') as test_zip:\n    test_zip.extractall('test')\n\ntest_list = glob.glob(os.path.join(test_dir, '*.jpg'))\ntest_dir = os.path.join(base_dir, 'test')","metadata":{"execution":{"iopub.status.busy":"2023-02-07T12:10:56.225268Z","iopub.execute_input":"2023-02-07T12:10:56.225645Z","iopub.status.idle":"2023-02-07T12:11:00.354509Z","shell.execute_reply.started":"2023-02-07T12:10:56.225615Z","shell.execute_reply":"2023-02-07T12:11:00.353510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create image generator for test data","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications.resnet50 import preprocess_input\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\ntest_data = ImageDataGenerator(preprocessing_function=preprocess_input).flow_from_directory(\n    directory = '/kaggle/working/test/',\n    target_size = (img_height,img_width),\n    batch_size = 32,\n    class_mode = None,\n    shuffle = False,\n    seed = 42\n)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T12:11:00.356775Z","iopub.execute_input":"2023-02-07T12:11:00.357490Z","iopub.status.idle":"2023-02-07T12:11:00.589949Z","shell.execute_reply.started":"2023-02-07T12:11:00.357434Z","shell.execute_reply":"2023-02-07T12:11:00.588949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.reset()\n\npred = model.predict(test_data, steps = len(test_data), verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T12:11:00.591240Z","iopub.execute_input":"2023-02-07T12:11:00.592158Z","iopub.status.idle":"2023-02-07T12:11:48.623238Z","shell.execute_reply.started":"2023-02-07T12:11:00.592121Z","shell.execute_reply":"2023-02-07T12:11:48.622315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Plt result","metadata":{}},{"cell_type":"code","source":"f, ax = plt.subplots(5, 5, figsize = (15, 15))\n\nfor i in range(0,25):\n    imgBGR = cv2.imread('/kaggle/working/test/' + test_data.filenames[i])\n    imgRGB = cv2.cvtColor(imgBGR, cv2.COLOR_BGR2RGB)\n    \n    # a if condition else b\n    predicted_class = \"Dog \" if pred[i] > 0.5 else \"Cat\"\n\n    ax[i//5, i%5].imshow(imgRGB)\n    ax[i//5, i%5].axis('off')\n    ax[i//5, i%5].set_title(\"Predicted:{}\".format(predicted_class))    \n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-07T12:11:48.625597Z","iopub.execute_input":"2023-02-07T12:11:48.626007Z","iopub.status.idle":"2023-02-07T12:11:50.670944Z","shell.execute_reply.started":"2023-02-07T12:11:48.625966Z","shell.execute_reply":"2023-02-07T12:11:50.670007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred.clip(min=0.05, max=0.95)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T12:14:55.782699Z","iopub.execute_input":"2023-02-07T12:14:55.783060Z","iopub.status.idle":"2023-02-07T12:14:55.791170Z","shell.execute_reply.started":"2023-02-07T12:14:55.783031Z","shell.execute_reply":"2023-02-07T12:14:55.790101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')\nsub.label = pred.clip(min=0.05, max=0.95)\nsub","metadata":{"execution":{"iopub.status.busy":"2023-02-07T12:15:02.084305Z","iopub.execute_input":"2023-02-07T12:15:02.084684Z","iopub.status.idle":"2023-02-07T12:15:02.105251Z","shell.execute_reply.started":"2023-02-07T12:15:02.084654Z","shell.execute_reply":"2023-02-07T12:15:02.104396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv('submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T12:15:05.810160Z","iopub.execute_input":"2023-02-07T12:15:05.810662Z","iopub.status.idle":"2023-02-07T12:15:05.840498Z","shell.execute_reply.started":"2023-02-07T12:15:05.810624Z","shell.execute_reply":"2023-02-07T12:15:05.839451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}