{"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 zipfile \nimport numpy as np \nimport pandas as pd \nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport cv2\nimport os\nfrom tensorflow.keras.models import Sequential, Model\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPool2D\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator,load_img","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-17T02:29:11.940199Z","iopub.execute_input":"2022-01-17T02:29:11.94116Z","iopub.status.idle":"2022-01-17T02:29:17.587624Z","shell.execute_reply.started":"2022-01-17T02:29:11.941003Z","shell.execute_reply":"2022-01-17T02:29:17.586916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_set = \"dogs-vs-cats\"\nwith zipfile.ZipFile(\"/kaggle/input/\"+ data_set +\"/train.zip\",\"r\") as z:\n    z.extractall(\".\")\n    destination = '/kaggle/files/images/train'\n    z.extractall(destination)\n    \nwith zipfile.ZipFile(\"/kaggle/input/\"+ data_set +\"/test1.zip\",\"r\") as z:\n    z.extractall(\".\")\n    destination = '/kaggle/files/images/test'\n    z.extractall(destination)","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:29:17.590949Z","iopub.execute_input":"2022-01-17T02:29:17.59142Z","iopub.status.idle":"2022-01-17T02:29:48.589858Z","shell.execute_reply.started":"2022-01-17T02:29:17.591382Z","shell.execute_reply":"2022-01-17T02:29:48.588787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def list_full_paths(directory):\n    return [os.path.join(directory, file) for file in os.listdir(directory)]","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:29:48.5965Z","iopub.execute_input":"2022-01-17T02:29:48.599495Z","iopub.status.idle":"2022-01-17T02:29:48.607344Z","shell.execute_reply.started":"2022-01-17T02:29:48.599339Z","shell.execute_reply":"2022-01-17T02:29:48.606405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.DataFrame({'filepath': list_full_paths('/kaggle/files/images/train/train')})\ntrain['truth_label'] = np.where(train['filepath'].str.contains('dog'), 'dog', 'cat')\n\ntest = pd.DataFrame({'filepath': list_full_paths('/kaggle/files/images/test/test1')})","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:29:48.610993Z","iopub.execute_input":"2022-01-17T02:29:48.612791Z","iopub.status.idle":"2022-01-17T02:29:49.202569Z","shell.execute_reply.started":"2022-01-17T02:29:48.612754Z","shell.execute_reply":"2022-01-17T02:29:49.201637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:29:49.209979Z","iopub.execute_input":"2022-01-17T02:29:49.212649Z","iopub.status.idle":"2022-01-17T02:29:49.234042Z","shell.execute_reply.started":"2022-01-17T02:29:49.212434Z","shell.execute_reply":"2022-01-17T02:29:49.233353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:29:49.235618Z","iopub.execute_input":"2022-01-17T02:29:49.236015Z","iopub.status.idle":"2022-01-17T02:29:49.308565Z","shell.execute_reply.started":"2022-01-17T02:29:49.235979Z","shell.execute_reply":"2022-01-17T02:29:49.307699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['truth_label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:29:49.312501Z","iopub.execute_input":"2022-01-17T02:29:49.314988Z","iopub.status.idle":"2022-01-17T02:29:49.346691Z","shell.execute_reply.started":"2022-01-17T02:29:49.314951Z","shell.execute_reply":"2022-01-17T02:29:49.345829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Splitting train data into train and test\nX_train, X_test = train_test_split(train, test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:29:49.34968Z","iopub.execute_input":"2022-01-17T02:29:49.350262Z","iopub.status.idle":"2022-01-17T02:29:49.378667Z","shell.execute_reply.started":"2022-01-17T02:29:49.350226Z","shell.execute_reply":"2022-01-17T02:29:49.377956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape, X_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:29:49.382217Z","iopub.execute_input":"2022-01-17T02:29:49.384395Z","iopub.status.idle":"2022-01-17T02:29:49.405272Z","shell.execute_reply.started":"2022-01-17T02:29:49.384356Z","shell.execute_reply":"2022-01-17T02:29:49.404491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_SIZE = [224, 224]","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:29:49.406694Z","iopub.execute_input":"2022-01-17T02:29:49.40703Z","iopub.status.idle":"2022-01-17T02:29:49.41226Z","shell.execute_reply.started":"2022-01-17T02:29:49.406992Z","shell.execute_reply":"2022-01-17T02:29:49.411373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale = 1./255)\ntest_datagen = ImageDataGenerator(rescale = 1./255)","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:29:49.413939Z","iopub.execute_input":"2022-01-17T02:29:49.414204Z","iopub.status.idle":"2022-01-17T02:29:49.421411Z","shell.execute_reply.started":"2022-01-17T02:29:49.414169Z","shell.execute_reply":"2022-01-17T02:29:49.420668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_set = train_datagen.flow_from_dataframe(dataframe=X_train, \n                                                 x_col='filepath', \n                                                 y_col='truth_label', \n                                                 class_mode='categorical', \n                                                 target_size = (224, 224), \n                                                 batch_size = 128)\n\ntest_set = test_datagen.flow_from_dataframe(dataframe=X_test, \n                                            x_col='filepath', \n                                            y_col='truth_label', \n                                            class_mode='categorical', \n                                            target_size = (224, 224), \n                                            batch_size = 128)","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:29:49.422852Z","iopub.execute_input":"2022-01-17T02:29:49.423177Z","iopub.status.idle":"2022-01-17T02:29:49.680894Z","shell.execute_reply.started":"2022-01-17T02:29:49.423142Z","shell.execute_reply":"2022-01-17T02:29:49.680145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[train['truth_label'] == \"cat\"].head(1)","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:29:49.682164Z","iopub.execute_input":"2022-01-17T02:29:49.682564Z","iopub.status.idle":"2022-01-17T02:29:49.696142Z","shell.execute_reply.started":"2022-01-17T02:29:49.682525Z","shell.execute_reply":"2022-01-17T02:29:49.695318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[train['truth_label'] == \"dog\"].head(1)","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:29:49.700653Z","iopub.execute_input":"2022-01-17T02:29:49.70091Z","iopub.status.idle":"2022-01-17T02:29:49.714847Z","shell.execute_reply.started":"2022-01-17T02:29:49.700859Z","shell.execute_reply":"2022-01-17T02:29:49.713972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Displaying the Image\nplt.subplot(1, 2, 1)\n# Dog Image\nimage_dog=cv2.imread(\"/kaggle/files/images/train/train/dog.890.jpg\")\nplt.imshow(image_dog)\nplt.subplot(1, 2, 2)\n# Cat Image\nimage_cat=cv2.imread(\"/kaggle/files/images/train/train/cat.3660.jpg\")\nplt.imshow(image_cat)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:29:49.716135Z","iopub.execute_input":"2022-01-17T02:29:49.716469Z","iopub.status.idle":"2022-01-17T02:29:50.066037Z","shell.execute_reply.started":"2022-01-17T02:29:49.716428Z","shell.execute_reply":"2022-01-17T02:29:50.065351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Creating VGG16 Architecture From Scratch","metadata":{}},{"cell_type":"code","source":"model = Sequential()\n# Convolution Layer 1\nmodel.add(Conv2D(input_shape=(224,224,3),filters=64,kernel_size=(3,3),padding=\"same\", activation=\"relu\"))\nmodel.add(Conv2D(filters=64,kernel_size=(3,3),padding=\"same\", activation=\"relu\"))\n# Max Pooling Layer 1\nmodel.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))\n# Convolution Layer 2\nmodel.add(Conv2D(filters=128, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\nmodel.add(Conv2D(filters=128, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\n# Max Pooling Layer 2\nmodel.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))\n# Convolution Layer 3\nmodel.add(Conv2D(filters=256, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\nmodel.add(Conv2D(filters=256, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\nmodel.add(Conv2D(filters=256, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\n# Max Pooling Layer 3\nmodel.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))\n# Convolution Layer 4\nmodel.add(Conv2D(filters=512, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\nmodel.add(Conv2D(filters=512, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\nmodel.add(Conv2D(filters=512, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\n# Max Pooling Layer 4\nmodel.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))\n# Convolution Layer 5\nmodel.add(Conv2D(filters=512, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\nmodel.add(Conv2D(filters=512, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\nmodel.add(Conv2D(filters=512, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\n# Max Pooling Layer 5\nmodel.add(MaxPool2D(pool_size=(2,2),strides=(2,2),name='vgg16'))\n# Flatten Layer\nmodel.add(Flatten(name='flatten'))\n# 3 Dense Layers\nmodel.add(Dense(4096, activation='relu', name='fc1'))\nmodel.add(Dense(4096, activation='relu', name='fc2'))\nmodel.add(Dense(2, activation='sigmoid', name='output'))","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:29:50.067036Z","iopub.execute_input":"2022-01-17T02:29:50.067285Z","iopub.status.idle":"2022-01-17T02:29:52.45943Z","shell.execute_reply.started":"2022-01-17T02:29:50.067246Z","shell.execute_reply":"2022-01-17T02:29:52.458711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:29:52.460527Z","iopub.execute_input":"2022-01-17T02:29:52.461318Z","iopub.status.idle":"2022-01-17T02:29:52.477865Z","shell.execute_reply.started":"2022-01-17T02:29:52.461277Z","shell.execute_reply":"2022-01-17T02:29:52.477204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Vgg16 = Model(inputs=model.input, outputs=model.get_layer('vgg16').output)","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:29:52.479153Z","iopub.execute_input":"2022-01-17T02:29:52.479395Z","iopub.status.idle":"2022-01-17T02:29:52.48597Z","shell.execute_reply.started":"2022-01-17T02:29:52.479363Z","shell.execute_reply":"2022-01-17T02:29:52.485281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Vgg16.load_weights(\"/kaggle/input/keras-pretrained-models/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5\")","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:29:52.487197Z","iopub.execute_input":"2022-01-17T02:29:52.487613Z","iopub.status.idle":"2022-01-17T02:29:54.41433Z","shell.execute_reply.started":"2022-01-17T02:29:52.487577Z","shell.execute_reply":"2022-01-17T02:29:54.41357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in Vgg16.layers:\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:29:54.41685Z","iopub.execute_input":"2022-01-17T02:29:54.417297Z","iopub.status.idle":"2022-01-17T02:29:54.42185Z","shell.execute_reply.started":"2022-01-17T02:29:54.417256Z","shell.execute_reply":"2022-01-17T02:29:54.421149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in model.layers:\n    print(layer, layer.trainable)","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:29:54.423126Z","iopub.execute_input":"2022-01-17T02:29:54.423544Z","iopub.status.idle":"2022-01-17T02:29:54.44021Z","shell.execute_reply.started":"2022-01-17T02:29:54.423507Z","shell.execute_reply":"2022-01-17T02:29:54.439549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=\"binary_crossentropy\", \n              optimizer=\"adam\",\n              metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:29:54.441678Z","iopub.execute_input":"2022-01-17T02:29:54.441936Z","iopub.status.idle":"2022-01-17T02:29:54.624725Z","shell.execute_reply.started":"2022-01-17T02:29:54.441902Z","shell.execute_reply":"2022-01-17T02:29:54.623941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(training_set, validation_data=test_set, epochs=10)","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:29:54.625862Z","iopub.execute_input":"2022-01-17T02:29:54.626551Z","iopub.status.idle":"2022-01-17T02:48:31.711886Z","shell.execute_reply.started":"2022-01-17T02:29:54.626509Z","shell.execute_reply":"2022-01-17T02:48:31.710368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training loss and validation loss\nloss = history.history['loss']\nval_loss = history.history['val_loss']\nepochs = range(1, 11)\n\nplt.figure(figsize=(10, 5))\nplt.plot(epochs, loss, 'b',label='training_loss')\nplt.plot(epochs, val_loss, 'r', label='valid_loss')\nplt.xlabel('epochs')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:48:31.715155Z","iopub.execute_input":"2022-01-17T02:48:31.715705Z","iopub.status.idle":"2022-01-17T02:48:31.90882Z","shell.execute_reply.started":"2022-01-17T02:48:31.71566Z","shell.execute_reply":"2022-01-17T02:48:31.90814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training accuracy and validation accuracy\naccuracy = history.history['accuracy']\nval_accuracy = history.history['val_accuracy']\n\nplt.figure(figsize=(10, 6))\nplt.plot(epochs, accuracy, 'b',label='train_accuracy')\nplt.plot(epochs, val_accuracy, 'r', label='val_accuracy')\nplt.xlabel('epochs')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:48:31.910241Z","iopub.execute_input":"2022-01-17T02:48:31.910643Z","iopub.status.idle":"2022-01-17T02:48:32.101646Z","shell.execute_reply.started":"2022-01-17T02:48:31.910596Z","shell.execute_reply":"2022-01-17T02:48:32.100965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_true = []\ny_pred = []","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:48:32.102923Z","iopub.execute_input":"2022-01-17T02:48:32.103328Z","iopub.status.idle":"2022-01-17T02:48:32.10757Z","shell.execute_reply.started":"2022-01-17T02:48:32.103288Z","shell.execute_reply":"2022-01-17T02:48:32.106771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test_set)","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:48:32.108938Z","iopub.execute_input":"2022-01-17T02:48:32.109222Z","iopub.status.idle":"2022-01-17T02:48:32.11853Z","shell.execute_reply.started":"2022-01-17T02:48:32.109183Z","shell.execute_reply":"2022-01-17T02:48:32.117856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(40):\n    x, y = test_set[i]\n    y_true.extend([np.argmax(i) for i in y])\n    y_pred.extend([np.argmax(i) for i in model.predict(x)])","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:48:32.119847Z","iopub.execute_input":"2022-01-17T02:48:32.12021Z","iopub.status.idle":"2022-01-17T02:49:02.45721Z","shell.execute_reply.started":"2022-01-17T02:48:32.120172Z","shell.execute_reply":"2022-01-17T02:49:02.456452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(y_true), len(y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:49:02.46124Z","iopub.execute_input":"2022-01-17T02:49:02.461454Z","iopub.status.idle":"2022-01-17T02:49:02.466619Z","shell.execute_reply.started":"2022-01-17T02:49:02.461428Z","shell.execute_reply":"2022-01-17T02:49:02.465983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report, confusion_matrix","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:49:02.468041Z","iopub.execute_input":"2022-01-17T02:49:02.468564Z","iopub.status.idle":"2022-01-17T02:49:02.476372Z","shell.execute_reply.started":"2022-01-17T02:49:02.468529Z","shell.execute_reply":"2022-01-17T02:49:02.475721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"confusion_matrix(y_true, y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:49:02.477839Z","iopub.execute_input":"2022-01-17T02:49:02.478121Z","iopub.status.idle":"2022-01-17T02:49:02.500414Z","shell.execute_reply.started":"2022-01-17T02:49:02.478088Z","shell.execute_reply":"2022-01-17T02:49:02.499827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_true, y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:49:02.501428Z","iopub.execute_input":"2022-01-17T02:49:02.50167Z","iopub.status.idle":"2022-01-17T02:49:02.524284Z","shell.execute_reply.started":"2022-01-17T02:49:02.501636Z","shell.execute_reply":"2022-01-17T02:49:02.523489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing import image\nfrom keras.applications.resnet import preprocess_input","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:49:02.525472Z","iopub.execute_input":"2022-01-17T02:49:02.526205Z","iopub.status.idle":"2022-01-17T02:49:02.530701Z","shell.execute_reply.started":"2022-01-17T02:49:02.526167Z","shell.execute_reply":"2022-01-17T02:49:02.529709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict_class(img_path):\n    img=image.load_img(img_path,target_size=(224,224))\n    x=image.img_to_array(img)\n    x=np.expand_dims(x, axis=0)\n    img_data=preprocess_input(x)\n    classes=model.predict(img_data)\n    return classes","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:49:02.532103Z","iopub.execute_input":"2022-01-17T02:49:02.532787Z","iopub.status.idle":"2022-01-17T02:49:02.540282Z","shell.execute_reply.started":"2022-01-17T02:49:02.532746Z","shell.execute_reply":"2022-01-17T02:49:02.539411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_class(\"/kaggle/files/images/train/train/cat.3660.jpg\")","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:49:02.541468Z","iopub.execute_input":"2022-01-17T02:49:02.542112Z","iopub.status.idle":"2022-01-17T02:49:02.838685Z","shell.execute_reply.started":"2022-01-17T02:49:02.542069Z","shell.execute_reply":"2022-01-17T02:49:02.837981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Pre-Trained Model","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications.vgg16 import VGG16\nvgg = VGG16(input_shape=IMAGE_SIZE + [3], weights='imagenet', include_top=False)","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:49:02.841499Z","iopub.execute_input":"2022-01-17T02:49:02.842273Z","iopub.status.idle":"2022-01-17T02:49:03.559911Z","shell.execute_reply.started":"2022-01-17T02:49:02.84223Z","shell.execute_reply":"2022-01-17T02:49:03.55879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in vgg.layers:\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:49:03.561183Z","iopub.execute_input":"2022-01-17T02:49:03.562073Z","iopub.status.idle":"2022-01-17T02:49:03.572849Z","shell.execute_reply.started":"2022-01-17T02:49:03.562027Z","shell.execute_reply":"2022-01-17T02:49:03.571936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = Flatten()(vgg.output)","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:49:03.573868Z","iopub.execute_input":"2022-01-17T02:49:03.574427Z","iopub.status.idle":"2022-01-17T02:49:03.596932Z","shell.execute_reply.started":"2022-01-17T02:49:03.574391Z","shell.execute_reply":"2022-01-17T02:49:03.59596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = Dense(2, activation='softmax')(x)\nmodel = Model(inputs=vgg.input, outputs=prediction)","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:49:03.598206Z","iopub.execute_input":"2022-01-17T02:49:03.598629Z","iopub.status.idle":"2022-01-17T02:49:03.641568Z","shell.execute_reply.started":"2022-01-17T02:49:03.59859Z","shell.execute_reply":"2022-01-17T02:49:03.640862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:49:03.642824Z","iopub.execute_input":"2022-01-17T02:49:03.646064Z","iopub.status.idle":"2022-01-17T02:49:03.672519Z","shell.execute_reply.started":"2022-01-17T02:49:03.646028Z","shell.execute_reply":"2022-01-17T02:49:03.671857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n  loss='categorical_crossentropy',\n  optimizer='adam',\n  metrics=['accuracy']\n)","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:49:03.677717Z","iopub.execute_input":"2022-01-17T02:49:03.678158Z","iopub.status.idle":"2022-01-17T02:49:03.696212Z","shell.execute_reply.started":"2022-01-17T02:49:03.678126Z","shell.execute_reply":"2022-01-17T02:49:03.688337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(training_set, validation_data=test_set, epochs=10)","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:49:03.697349Z","iopub.execute_input":"2022-01-17T02:49:03.697693Z","iopub.status.idle":"2022-01-17T03:07:13.860331Z","shell.execute_reply.started":"2022-01-17T02:49:03.697661Z","shell.execute_reply":"2022-01-17T03:07:13.859371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training loss and validation loss\nloss = history.history['loss']\nval_loss = history.history['val_loss']\nepochs = range(1, 11)\n\nplt.figure(figsize=(10, 5))\nplt.plot(epochs, loss, 'b',label='training_loss')\nplt.plot(epochs, val_loss, 'r', label='valid_loss')\nplt.xlabel('epochs')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-17T03:07:13.8618Z","iopub.execute_input":"2022-01-17T03:07:13.862092Z","iopub.status.idle":"2022-01-17T03:07:14.051342Z","shell.execute_reply.started":"2022-01-17T03:07:13.862056Z","shell.execute_reply":"2022-01-17T03:07:14.050663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training accuracy and validation accuracy\naccuracy = history.history['accuracy']\nval_accuracy = history.history['val_accuracy']\n\nplt.figure(figsize=(10, 6))\nplt.plot(epochs, accuracy, 'b',label='train_accuracy')\nplt.plot(epochs, val_accuracy, 'r', label='val_accuracy')\nplt.xlabel('epochs')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-17T03:07:14.052403Z","iopub.execute_input":"2022-01-17T03:07:14.053959Z","iopub.status.idle":"2022-01-17T03:07:14.246422Z","shell.execute_reply.started":"2022-01-17T03:07:14.053918Z","shell.execute_reply":"2022-01-17T03:07:14.245754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_true = []\ny_pred = []","metadata":{"execution":{"iopub.status.busy":"2022-01-17T03:07:14.247489Z","iopub.execute_input":"2022-01-17T03:07:14.248972Z","iopub.status.idle":"2022-01-17T03:07:14.254283Z","shell.execute_reply.started":"2022-01-17T03:07:14.24893Z","shell.execute_reply":"2022-01-17T03:07:14.253535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(40):\n    x, y = test_set[i]\n    y_true.extend([np.argmax(i) for i in y])\n    y_pred.extend([np.argmax(i) for i in model.predict(x)])","metadata":{"execution":{"iopub.status.busy":"2022-01-17T03:07:14.257066Z","iopub.execute_input":"2022-01-17T03:07:14.257264Z","iopub.status.idle":"2022-01-17T03:07:44.74451Z","shell.execute_reply.started":"2022-01-17T03:07:14.257237Z","shell.execute_reply":"2022-01-17T03:07:44.743757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, classification_report\nconfusion_matrix(y_true, y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-01-17T03:07:44.745951Z","iopub.execute_input":"2022-01-17T03:07:44.746226Z","iopub.status.idle":"2022-01-17T03:07:44.769022Z","shell.execute_reply.started":"2022-01-17T03:07:44.746189Z","shell.execute_reply":"2022-01-17T03:07:44.768361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_true, y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-01-17T03:07:44.770549Z","iopub.execute_input":"2022-01-17T03:07:44.771236Z","iopub.status.idle":"2022-01-17T03:07:44.792993Z","shell.execute_reply.started":"2022-01-17T03:07:44.771197Z","shell.execute_reply":"2022-01-17T03:07:44.792298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_class(\"/kaggle/files/images/train/train/cat.3660.jpg\")","metadata":{"execution":{"iopub.status.busy":"2022-01-17T03:07:44.794145Z","iopub.execute_input":"2022-01-17T03:07:44.794467Z","iopub.status.idle":"2022-01-17T03:07:44.849193Z","shell.execute_reply.started":"2022-01-17T03:07:44.794427Z","shell.execute_reply":"2022-01-17T03:07:44.848417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}