{"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 # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom keras import layers\nfrom keras import models\nfrom keras import optimizers\nfrom keras.preprocessing.image import load_img, img_to_array, ImageDataGenerator\nfrom keras import applications\n\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn import metrics\n\nimport glob\nimport os\nprint(os.listdir(\"/kaggle/input/dogs-vs-cats-redux-kernels-edition\"))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-11T05:06:05.012043Z","iopub.execute_input":"2023-01-11T05:06:05.012537Z","iopub.status.idle":"2023-01-11T05:06:12.899276Z","shell.execute_reply.started":"2023-01-11T05:06:05.012432Z","shell.execute_reply":"2023-01-11T05:06:12.897868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n\n\nzip_files = ['test', 'train']\n# Will unzip the files so that you can see them..\nfor zip_file in zip_files:\n    with zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats-redux-kernels-edition/{}.zip\".format(zip_file),\"r\") as z:\n        z.extractall(\".\")\n        print(\"{} unzipped\".format(zip_file))","metadata":{"execution":{"iopub.status.busy":"2023-01-11T05:08:20.143823Z","iopub.execute_input":"2023-01-11T05:08:20.144651Z","iopub.status.idle":"2023-01-11T05:08:43.485290Z","shell.execute_reply.started":"2023-01-11T05:08:20.144613Z","shell.execute_reply":"2023-01-11T05:08:43.483859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir('/kaggle/input/dogs-vs-cats-redux-kernels-edition'))","metadata":{"execution":{"iopub.status.busy":"2023-01-11T05:08:43.488090Z","iopub.execute_input":"2023-01-11T05:08:43.489029Z","iopub.status.idle":"2023-01-11T05:08:43.497971Z","shell.execute_reply.started":"2023-01-11T05:08:43.488967Z","shell.execute_reply":"2023-01-11T05:08:43.496470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_FOLDER_PATH=\"/kaggle/working/train\"\nFILE_NAMES=os.listdir(IMAGE_FOLDER_PATH)\nWIDTH=150\nHEIGHT=150","metadata":{"execution":{"iopub.status.busy":"2023-01-11T05:08:46.815161Z","iopub.execute_input":"2023-01-11T05:08:46.815701Z","iopub.status.idle":"2023-01-11T05:08:46.841789Z","shell.execute_reply.started":"2023-01-11T05:08:46.815636Z","shell.execute_reply":"2023-01-11T05:08:46.840403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets=list()\nfull_paths=list()\nfor file_name in FILE_NAMES:\n    target=file_name.split(\".\")[0]\n    full_path=os.path.join(IMAGE_FOLDER_PATH, file_name)\n    full_paths.append(full_path)\n    targets.append(target)\n\ndataset=pd.DataFrame()\ndataset['image_path']=full_paths\ndataset['target']=targets","metadata":{"execution":{"iopub.status.busy":"2023-01-11T05:08:47.137125Z","iopub.execute_input":"2023-01-11T05:08:47.138214Z","iopub.status.idle":"2023-01-11T05:08:47.245399Z","shell.execute_reply.started":"2023-01-11T05:08:47.138144Z","shell.execute_reply":"2023-01-11T05:08:47.243885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.head(10)\n","metadata":{"execution":{"iopub.status.busy":"2023-01-11T05:08:47.780674Z","iopub.execute_input":"2023-01-11T05:08:47.781902Z","iopub.status.idle":"2023-01-11T05:08:47.801053Z","shell.execute_reply.started":"2023-01-11T05:08:47.781848Z","shell.execute_reply":"2023-01-11T05:08:47.800083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_counts=dataset['target'].value_counts()\nprint(\"Number of dogs in the dataset:{}\".format(target_counts['dog']))\nprint(\"Number of cats in the dataset:{}\".format(target_counts['cat']))","metadata":{"execution":{"iopub.status.busy":"2023-01-11T05:08:48.534517Z","iopub.execute_input":"2023-01-11T05:08:48.537511Z","iopub.status.idle":"2023-01-11T05:08:48.550994Z","shell.execute_reply.started":"2023-01-11T05:08:48.537467Z","shell.execute_reply":"2023-01-11T05:08:48.549471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_side(img, side_type, side_size=5):\n    height, width, channel=img.shape\n    if side_type==\"horizontal\":\n        return np.ones((height,side_size,  channel), dtype=np.float32)*255\n        \n    return np.ones((side_size, width,  channel), dtype=np.float32)*255\n\ndef show_gallery(show=\"both\"):\n    n=100\n    counter=0\n    images=list()\n    vertical_images=[]\n    rng_state = np.random.get_state()\n    np.random.shuffle(full_paths)\n    np.random.set_state(rng_state)\n    np.random.shuffle(targets)\n    for path, target in zip(full_paths, targets):\n        if target!=show and show!=\"both\":\n            continue\n        counter=counter+1\n        if counter%100==0:\n            break\n        #Image loading from disk as JpegImageFile file format\n        img=load_img(path, target_size=(WIDTH,HEIGHT))\n        #Converting JpegImageFile to numpy array\n        img=img_to_array(img)\n        \n        hside=get_side(img, side_type=\"horizontal\")\n        images.append(img)\n        images.append(hside)\n\n        if counter%10==0:\n            himage=np.hstack((images))\n            vside=get_side(himage, side_type=\"vertical\")\n            vertical_images.append(himage)\n            vertical_images.append(vside)\n            \n            images=list()\n\n    gallery=np.vstack((vertical_images)) \n    plt.figure(figsize=(12,12))\n    plt.xticks([])\n    plt.yticks([])\n    title={\"both\":\"Dogs and Cats\",\n          \"cat\": \"Cats\",\n          \"dog\": \"Dogs\"}\n    plt.title(\"100 samples of {} of the dataset\".format(title[show]))\n    plt.imshow(gallery.astype(np.uint8))","metadata":{"execution":{"iopub.status.busy":"2023-01-11T05:08:48.924520Z","iopub.execute_input":"2023-01-11T05:08:48.924984Z","iopub.status.idle":"2023-01-11T05:08:48.938940Z","shell.execute_reply.started":"2023-01-11T05:08:48.924885Z","shell.execute_reply":"2023-01-11T05:08:48.937497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_gallery(show=\"cat\")\nshow_gallery(show=\"dog\")\nshow_gallery(show=\"both\")","metadata":{"execution":{"iopub.status.busy":"2023-01-11T05:08:49.085490Z","iopub.execute_input":"2023-01-11T05:08:49.086621Z","iopub.status.idle":"2023-01-11T05:08:52.595293Z","shell.execute_reply.started":"2023-01-11T05:08:49.086580Z","shell.execute_reply":"2023-01-11T05:08:52.593495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_model_history(modelHistory, model_name):\n    history=pd.DataFrame()\n    history[\"Train Loss\"]=modelHistory.history['loss']\n    history[\"Validatin Loss\"]=modelHistory.history['val_loss']\n    history[\"Train Accuracy\"]=modelHistory.history['accuracy']\n    history[\"Validatin Accuracy\"]=modelHistory.history['val_accuracy']\n  \n    history.plot(figsize=(12,8))\n    plt.title(\" Convulutional Model {} Train and Validation Loss and Accuracy History\".format(model_name))\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-11T05:08:52.597930Z","iopub.execute_input":"2023-01-11T05:08:52.598387Z","iopub.status.idle":"2023-01-11T05:08:52.607111Z","shell.execute_reply.started":"2023-01-11T05:08:52.598347Z","shell.execute_reply":"2023-01-11T05:08:52.605819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras import layers\nfrom keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, Activation,GlobalMaxPooling2D\nfrom keras import applications\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras import optimizers\nfrom keras.applications.vgg16 import VGG16\nfrom keras.models import Model\n\nimage_size = 224\ninput_shape = (image_size, image_size, 3)\n\nepochs = 5\nbatch_size = 16\n\npre_trained_model = VGG16(weights = '/kaggle/input/vgg16-weights/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5', include_top = False, input_shape = input_shape)\n    \nfor layer in pre_trained_model.layers[:15]:\n    layer.trainable = False\n\nfor layer in pre_trained_model.layers[15:]:\n    layer.trainable = True\n    \nlast_layer = pre_trained_model.get_layer('block5_pool')\nlast_output = last_layer.output\n    \n# Flatten the output layer to 1 dimension\nx = GlobalMaxPooling2D()(last_output)\n# Add a fully connected layer with 512 hidden units and ReLU activation\nx = Dense(512, activation='relu')(x)\n# Add a dropout rate of 0.5\nx = Dropout(0.5)(x)\n# Add a final sigmoid layer for classification\nx = layers.Dense(1, activation='sigmoid')(x)\n\nmodel = Model(pre_trained_model.input, x)\n\nmodel.compile(loss='binary_crossentropy',\n              optimizer=tf.optimizers.SGD(learning_rate=1e-4, momentum=0.9),\n              metrics=['accuracy'])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-01-11T05:08:56.567537Z","iopub.execute_input":"2023-01-11T05:08:56.568038Z","iopub.status.idle":"2023-01-11T05:08:58.028087Z","shell.execute_reply.started":"2023-01-11T05:08:56.567999Z","shell.execute_reply":"2023-01-11T05:08:58.026859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=\"binary_crossentropy\", \n             optimizer= tf.optimizers.Adam(learning_rate=1e-4),\n             metrics=[\"accuracy\"])\n","metadata":{"execution":{"iopub.status.busy":"2023-01-11T05:09:08.217227Z","iopub.execute_input":"2023-01-11T05:09:08.217663Z","iopub.status.idle":"2023-01-11T05:09:08.233587Z","shell.execute_reply.started":"2023-01-11T05:09:08.217610Z","shell.execute_reply":"2023-01-11T05:09:08.232245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_train, dataset_test=train_test_split(dataset,\n                                                 test_size=0.2,\n                                                 random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-01-11T05:09:08.908035Z","iopub.execute_input":"2023-01-11T05:09:08.908457Z","iopub.status.idle":"2023-01-11T05:09:08.922065Z","shell.execute_reply.started":"2023-01-11T05:09:08.908421Z","shell.execute_reply":"2023-01-11T05:09:08.920882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen=ImageDataGenerator(\nrotation_range=15,\nrescale=1./255,\nshear_range=0.1,\nzoom_range=0.2,\nhorizontal_flip=True,\nwidth_shift_range=0.1,\nheight_shift_range=0.1)\n\ntrain_datagenerator=train_datagen.flow_from_dataframe(dataframe=dataset_train,\n                                                     x_col=\"image_path\",\n                                                     y_col=\"target\",\n                                                     target_size=(WIDTH, HEIGHT),\n                                                     class_mode=\"binary\",\n                                                     batch_size=150)","metadata":{"execution":{"iopub.status.busy":"2023-01-11T05:09:11.468167Z","iopub.execute_input":"2023-01-11T05:09:11.468884Z","iopub.status.idle":"2023-01-11T05:09:11.701763Z","shell.execute_reply.started":"2023-01-11T05:09:11.468848Z","shell.execute_reply":"2023-01-11T05:09:11.700405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen=ImageDataGenerator(rescale=1./255)\ntest_datagenerator=test_datagen.flow_from_dataframe(dataframe=dataset_test,\n                                                   x_col=\"image_path\",\n                                                   y_col=\"target\",\n                                                   target_size=(WIDTH, HEIGHT),\n                                                   class_mode=\"binary\",\n                                                   batch_size=150)","metadata":{"execution":{"iopub.status.busy":"2023-01-11T05:09:14.976294Z","iopub.execute_input":"2023-01-11T05:09:14.976735Z","iopub.status.idle":"2023-01-11T05:09:15.046084Z","shell.execute_reply.started":"2023-01-11T05:09:14.976696Z","shell.execute_reply":"2023-01-11T05:09:15.044686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelHistory=model.fit(train_datagenerator,\n                                epochs=1,\n                                validation_data=test_datagenerator,\n                                validation_steps=dataset_test.shape[0]//150,\n                                steps_per_epoch=dataset_train.shape[0]//150\n                                )","metadata":{"execution":{"iopub.status.busy":"2023-01-11T05:09:17.093075Z","iopub.execute_input":"2023-01-11T05:09:17.094012Z","iopub.status.idle":"2023-01-11T06:01:46.143525Z","shell.execute_reply.started":"2023-01-11T05:09:17.093974Z","shell.execute_reply":"2023-01-11T06:01:46.141680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights(\"model.h5\")\n","metadata":{"execution":{"iopub.status.busy":"2023-01-11T06:08:14.459648Z","iopub.execute_input":"2023-01-11T06:08:14.460079Z","iopub.status.idle":"2023-01-11T06:08:14.576169Z","shell.execute_reply.started":"2023-01-11T06:08:14.460047Z","shell.execute_reply":"2023-01-11T06:08:14.574789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 12))\nax1.plot(modelHistory.history['loss'], color='b', label=\"Training loss\")\nax1.plot(modelHistory.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(modelHistory.history['accuracy'], color='b', label=\"Training accuracy\")\nax2.plot(modelHistory.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()","metadata":{"execution":{"iopub.status.busy":"2023-01-11T06:20:41.175860Z","iopub.execute_input":"2023-01-11T06:20:41.176296Z","iopub.status.idle":"2023-01-11T06:20:41.555402Z","shell.execute_reply.started":"2023-01-11T06:20:41.176261Z","shell.execute_reply":"2023-01-11T06:20:41.554160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}