{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"},{"sourceId":7468458,"sourceType":"datasetVersion","datasetId":4347405}],"dockerImageVersionId":30733,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\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# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2024-06-20T01:42:01.162145Z","iopub.execute_input":"2024-06-20T01:42:01.162398Z","iopub.status.idle":"2024-06-20T01:42:02.150400Z","shell.execute_reply.started":"2024-06-20T01:42:01.162375Z","shell.execute_reply":"2024-06-20T01:42:02.149515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Following DeepLizard Tutorials...","metadata":{}},{"cell_type":"code","source":"from random import randint\nfrom sklearn.utils import shuffle\nfrom sklearn.preprocessing import MinMaxScaler","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:42:02.151877Z","iopub.execute_input":"2024-06-20T01:42:02.152262Z","iopub.status.idle":"2024-06-20T01:42:03.095428Z","shell.execute_reply.started":"2024-06-20T01:42:02.152237Z","shell.execute_reply":"2024-06-20T01:42:03.094529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = []\ntrain_samples = []","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:42:03.096626Z","iopub.execute_input":"2024-06-20T01:42:03.096974Z","iopub.status.idle":"2024-06-20T01:42:03.103096Z","shell.execute_reply.started":"2024-06-20T01:42:03.096942Z","shell.execute_reply":"2024-06-20T01:42:03.102115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#generating data set\nfor i in range(50):\n    #The 5% of younger individuals that experienced side effects\n    random_younger = randint(13,64)\n    train_samples.append(random_younger)\n    train_labels.append(1)\n    \n    #The 5% of older individuals that did not experienced side effects\n    random_older = randint(65,100)\n    train_samples.append(random_older)\n    train_labels.append(0)\n    \nfor i in range(1000):\n    #The 95% of younger individuals that did not experience side effects\n    random_younger = randint(13,64)\n    train_samples.append(random_younger)\n    train_labels.append(0)\n    \n    #The 95% of older individuals that did experience side effects\n    random_older = randint(65,100)\n    train_samples.append(random_older)\n    train_labels.append(1)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:42:03.105882Z","iopub.execute_input":"2024-06-20T01:42:03.106235Z","iopub.status.idle":"2024-06-20T01:42:03.117322Z","shell.execute_reply.started":"2024-06-20T01:42:03.106202Z","shell.execute_reply":"2024-06-20T01:42:03.116458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in train_samples:\n    print(i)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:42:03.118351Z","iopub.execute_input":"2024-06-20T01:42:03.118711Z","iopub.status.idle":"2024-06-20T01:42:03.137742Z","shell.execute_reply.started":"2024-06-20T01:42:03.118678Z","shell.execute_reply":"2024-06-20T01:42:03.136868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = np.array(train_labels)\ntrain_samples = np.array(train_samples)\ntrain_labels, train_samples = shuffle(train_labels, train_samples)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:42:03.138693Z","iopub.execute_input":"2024-06-20T01:42:03.138931Z","iopub.status.idle":"2024-06-20T01:42:03.145060Z","shell.execute_reply.started":"2024-06-20T01:42:03.138910Z","shell.execute_reply":"2024-06-20T01:42:03.144127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler = MinMaxScaler(feature_range=(0,1))\nscaled_train_samples = scaler.fit_transform(train_samples.reshape(-1,1))","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:42:03.146519Z","iopub.execute_input":"2024-06-20T01:42:03.146897Z","iopub.status.idle":"2024-06-20T01:42:03.154489Z","shell.execute_reply.started":"2024-06-20T01:42:03.146863Z","shell.execute_reply":"2024-06-20T01:42:03.153609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in scaled_train_samples:\n    print(i)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2024-06-20T01:42:03.155973Z","iopub.execute_input":"2024-06-20T01:42:03.156306Z","iopub.status.idle":"2024-06-20T01:42:03.454815Z","shell.execute_reply.started":"2024-06-20T01:42:03.156277Z","shell.execute_reply":"2024-06-20T01:42:03.453838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n\n## Create an Artificial Neural Network with TensorFlow's Keras API","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nimport tf_keras as tfk\n\nfrom tf_keras.models import Sequential\nfrom tf_keras.layers import Activation, Dense\nfrom tf_keras.optimizers import Adam\nfrom tf_keras.metrics import categorical_crossentropy","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:42:03.456013Z","iopub.execute_input":"2024-06-20T01:42:03.456312Z","iopub.status.idle":"2024-06-20T01:42:15.455748Z","shell.execute_reply.started":"2024-06-20T01:42:03.456287Z","shell.execute_reply":"2024-06-20T01:42:15.454960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"physical_devices = tf.config.experimental.list_physical_devices('GPU')\nprint(\"Num GPUs Available: \", len(physical_devices))\ntf.config.experimental.set_memory_growth(physical_devices[0], True)\ntf.config.experimental.set_memory_growth(physical_devices[1], True)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:42:15.458969Z","iopub.execute_input":"2024-06-20T01:42:15.459577Z","iopub.status.idle":"2024-06-20T01:42:15.642419Z","shell.execute_reply.started":"2024-06-20T01:42:15.459550Z","shell.execute_reply":"2024-06-20T01:42:15.641334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    Dense(units=16, input_shape=(1,), activation='relu'),\n    Dense(units=32, activation='relu'),\n    Dense(units=2, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:42:15.643800Z","iopub.execute_input":"2024-06-20T01:42:15.645090Z","iopub.status.idle":"2024-06-20T01:42:15.998413Z","shell.execute_reply.started":"2024-06-20T01:42:15.645036Z","shell.execute_reply":"2024-06-20T01:42:15.997640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:42:15.999480Z","iopub.execute_input":"2024-06-20T01:42:15.999769Z","iopub.status.idle":"2024-06-20T01:42:16.016507Z","shell.execute_reply.started":"2024-06-20T01:42:15.999745Z","shell.execute_reply":"2024-06-20T01:42:16.015698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n\n## Train an Artificial Neural Network with TensorFlow's Keras API","metadata":{}},{"cell_type":"code","source":"model.compile(\n    optimizer=Adam(learning_rate=0.0001),\n    loss='sparse_categorical_crossentropy',\n    metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:42:16.017627Z","iopub.execute_input":"2024-06-20T01:42:16.017932Z","iopub.status.idle":"2024-06-20T01:42:16.037630Z","shell.execute_reply.started":"2024-06-20T01:42:16.017904Z","shell.execute_reply":"2024-06-20T01:42:16.036945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(\n    x=scaled_train_samples, \n    y=train_labels, \n    validation_split=0.1,\n    batch_size=10,\n    epochs=30,\n    shuffle=True,\n    verbose=2)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:42:16.038625Z","iopub.execute_input":"2024-06-20T01:42:16.038876Z","iopub.status.idle":"2024-06-20T01:42:49.406451Z","shell.execute_reply.started":"2024-06-20T01:42:16.038855Z","shell.execute_reply":"2024-06-20T01:42:49.405517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n\n## Neural Network Predictions with TensorFlow's Keras API","metadata":{}},{"cell_type":"code","source":"test_labels = []\ntest_samples = []","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:42:49.407880Z","iopub.execute_input":"2024-06-20T01:42:49.408369Z","iopub.status.idle":"2024-06-20T01:42:49.412836Z","shell.execute_reply.started":"2024-06-20T01:42:49.408333Z","shell.execute_reply":"2024-06-20T01:42:49.411908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#generating data set\nfor i in range(10):\n    #The 5% of younger individuals that experienced side effects\n    random_younger = randint(13,64)\n    test_samples.append(random_younger)\n    test_labels.append(1)\n    \n    #The 5% of older individuals that did not experienced side effects\n    random_older = randint(65,100)\n    test_samples.append(random_older)\n    test_labels.append(0)\n    \nfor i in range(200):\n    #The 95% of younger individuals that did not experience side effects\n    random_younger = randint(13,64)\n    test_samples.append(random_younger)\n    test_labels.append(0)\n    \n    #The 95% of older individuals that did experience side effects\n    random_older = randint(65,100)\n    test_samples.append(random_older)\n    test_labels.append(1)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:42:49.413953Z","iopub.execute_input":"2024-06-20T01:42:49.414289Z","iopub.status.idle":"2024-06-20T01:42:49.422107Z","shell.execute_reply.started":"2024-06-20T01:42:49.414257Z","shell.execute_reply":"2024-06-20T01:42:49.421262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_labels = np.array(test_labels)\ntest_samples = np.array(test_samples)\ntest_labels, test_samples = shuffle(test_labels, test_samples)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:42:49.423171Z","iopub.execute_input":"2024-06-20T01:42:49.423504Z","iopub.status.idle":"2024-06-20T01:42:49.433763Z","shell.execute_reply.started":"2024-06-20T01:42:49.423481Z","shell.execute_reply":"2024-06-20T01:42:49.433016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaled_test_samples = scaler.fit_transform(test_samples.reshape(-1,1))","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:42:49.434769Z","iopub.execute_input":"2024-06-20T01:42:49.435092Z","iopub.status.idle":"2024-06-20T01:42:49.442883Z","shell.execute_reply.started":"2024-06-20T01:42:49.435044Z","shell.execute_reply":"2024-06-20T01:42:49.441970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Predict","metadata":{}},{"cell_type":"code","source":"predictions = model.predict(x=scaled_test_samples, batch_size=10, verbose=0)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:42:49.444497Z","iopub.execute_input":"2024-06-20T01:42:49.444866Z","iopub.status.idle":"2024-06-20T01:42:49.920183Z","shell.execute_reply.started":"2024-06-20T01:42:49.444834Z","shell.execute_reply":"2024-06-20T01:42:49.919372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in predictions:\n    print(i)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2024-06-20T01:42:49.921323Z","iopub.execute_input":"2024-06-20T01:42:49.921587Z","iopub.status.idle":"2024-06-20T01:42:49.969247Z","shell.execute_reply.started":"2024-06-20T01:42:49.921562Z","shell.execute_reply":"2024-06-20T01:42:49.968427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rounded_predictions = np.argmax(predictions, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:42:49.970378Z","iopub.execute_input":"2024-06-20T01:42:49.970644Z","iopub.status.idle":"2024-06-20T01:42:49.975042Z","shell.execute_reply.started":"2024-06-20T01:42:49.970613Z","shell.execute_reply":"2024-06-20T01:42:49.974041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in rounded_predictions:\n    print(i)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2024-06-20T01:42:49.976273Z","iopub.execute_input":"2024-06-20T01:42:49.976558Z","iopub.status.idle":"2024-06-20T01:42:49.986022Z","shell.execute_reply.started":"2024-06-20T01:42:49.976527Z","shell.execute_reply":"2024-06-20T01:42:49.985116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n\n## Create a Confusion Matrix for Neural Network Predictions","metadata":{}},{"cell_type":"code","source":"%matplotlib inline\nfrom sklearn.metrics import confusion_matrix\nimport itertools\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:42:49.987026Z","iopub.execute_input":"2024-06-20T01:42:49.987306Z","iopub.status.idle":"2024-06-20T01:42:50.062750Z","shell.execute_reply.started":"2024-06-20T01:42:49.987283Z","shell.execute_reply":"2024-06-20T01:42:50.062087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = confusion_matrix(y_true = test_labels, y_pred=rounded_predictions)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:42:50.063748Z","iopub.execute_input":"2024-06-20T01:42:50.064057Z","iopub.status.idle":"2024-06-20T01:42:50.069742Z","shell.execute_reply.started":"2024-06-20T01:42:50.064027Z","shell.execute_reply":"2024-06-20T01:42:50.068852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_confusion_matrix(cm, classes,\n                        normalize=False,\n                        title='Confusion matrix',\n                        cmap=plt.cm.Blues):\n    \"\"\"\n    This function prints and plots the confusion matrix.\n    Normalization can be applied by setting `normalize=True`.\n    \"\"\"\n    plt.imshow(cm, interpolation='nearest', cmap=cmap)\n    plt.title(title)\n    plt.colorbar()\n    tick_marks = np.arange(len(classes))\n    plt.xticks(tick_marks, classes, rotation=45)\n    plt.yticks(tick_marks, classes)\n\n    if normalize:\n        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n        print(\"Normalized confusion matrix\")\n    else:\n        print('Confusion matrix, without normalization')\n\n    print(cm)\n\n    thresh = cm.max() / 2.\n    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n        plt.text(j, i, cm[i, j],\n            horizontalalignment=\"center\",\n            color=\"white\" if cm[i, j] > thresh else \"black\")\n\n    plt.tight_layout()\n    plt.ylabel('True label')\n    plt.xlabel('Predicted label')","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:42:50.072688Z","iopub.execute_input":"2024-06-20T01:42:50.072962Z","iopub.status.idle":"2024-06-20T01:42:50.082246Z","shell.execute_reply.started":"2024-06-20T01:42:50.072939Z","shell.execute_reply":"2024-06-20T01:42:50.081375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm_plot_labels = ['no_side_effects', 'had_side_effects']\nplot_confusion_matrix(cm=cm, classes=cm_plot_labels, title='Confusion Matrix')","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:42:50.083321Z","iopub.execute_input":"2024-06-20T01:42:50.083589Z","iopub.status.idle":"2024-06-20T01:42:50.474481Z","shell.execute_reply.started":"2024-06-20T01:42:50.083567Z","shell.execute_reply":"2024-06-20T01:42:50.473591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n\n## Image Preparation for Convolutional Neural Networks","metadata":{}},{"cell_type":"code","source":"!rm -rf /kaggle/working/*","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:42:50.475814Z","iopub.execute_input":"2024-06-20T01:42:50.476478Z","iopub.status.idle":"2024-06-20T01:42:51.442780Z","shell.execute_reply.started":"2024-06-20T01:42:50.476444Z","shell.execute_reply":"2024-06-20T01:42:51.441610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip /kaggle/input/dogs-vs-cats/train.zip -d /kaggle/working/","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2024-06-20T01:42:51.450817Z","iopub.execute_input":"2024-06-20T01:42:51.451141Z","iopub.status.idle":"2024-06-20T01:43:02.153302Z","shell.execute_reply.started":"2024-06-20T01:42:51.451111Z","shell.execute_reply":"2024-06-20T01:43:02.152224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport tensorflow as tf\nimport tf_keras as tfk\n\nfrom tf_keras.models import Sequential\nfrom tf_keras.layers import Activation, Dense, Flatten, BatchNormalization, Conv2D, MaxPool2D\nfrom tf_keras.optimizers import Adam\nfrom tf_keras.metrics import categorical_crossentropy\nfrom tf_keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.metrics import confusion_matrix\n\nimport itertools\nimport os\nimport shutil\nimport random\nimport glob\nimport matplotlib.pyplot as plt\nimport warnings\n\nwarnings.simplefilter(action='ignore', category=FutureWarning)\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:02.154857Z","iopub.execute_input":"2024-06-20T01:43:02.155281Z","iopub.status.idle":"2024-06-20T01:43:02.167014Z","shell.execute_reply.started":"2024-06-20T01:43:02.155237Z","shell.execute_reply":"2024-06-20T01:43:02.166119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"physical_devices = tf.config.experimental.list_physical_devices('GPU')\nprint(\"Num GPUs Available: \", len(physical_devices))\ntf.config.experimental.set_memory_growth(physical_devices[0], True)\ntf.config.experimental.set_memory_growth(physical_devices[1], True)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:02.168217Z","iopub.execute_input":"2024-06-20T01:43:02.168516Z","iopub.status.idle":"2024-06-20T01:43:02.176479Z","shell.execute_reply.started":"2024-06-20T01:43:02.168491Z","shell.execute_reply":"2024-06-20T01:43:02.175413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Data preparation","metadata":{}},{"cell_type":"code","source":"os.chdir('/kaggle/working/train')\nif os.path.isdir('train/dog') is False:\n    os.makedirs('train/dog')\n    os.makedirs('train/cat')\n    os.makedirs('valid/dog')\n    os.makedirs('valid/cat')\n    os.makedirs('test/dog')\n    os.makedirs('test/cat')\n    \nfor c in random.sample(glob.glob('cat*'), 500):\n    shutil.move(c, 'train/cat')\nfor c in random.sample(glob.glob('dog*'), 500):\n    shutil.move(c, 'train/dog')\nfor c in random.sample(glob.glob('cat*'), 100):\n    shutil.move(c, 'valid/cat')\nfor c in random.sample(glob.glob('dog*'), 100):\n    shutil.move(c, 'valid/dog')\nfor c in random.sample(glob.glob('cat*'), 50):\n    shutil.move(c, 'test/cat')\nfor c in random.sample(glob.glob('dog*'), 50):\n    shutil.move(c, 'test/dog')\n    \nos.chdir('../../')","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:02.177564Z","iopub.execute_input":"2024-06-20T01:43:02.177836Z","iopub.status.idle":"2024-06-20T01:43:02.480923Z","shell.execute_reply.started":"2024-06-20T01:43:02.177810Z","shell.execute_reply":"2024-06-20T01:43:02.479930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '/kaggle/working/train/train'\nvalid_path = '/kaggle/working/train/valid'\ntest_path = '/kaggle/working/train/test'","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:02.482220Z","iopub.execute_input":"2024-06-20T01:43:02.482511Z","iopub.status.idle":"2024-06-20T01:43:02.486616Z","shell.execute_reply.started":"2024-06-20T01:43:02.482486Z","shell.execute_reply":"2024-06-20T01:43:02.485759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_batches = ImageDataGenerator(preprocessing_function=tfk.applications.vgg16.preprocess_input).flow_from_directory(directory=train_path, target_size=(224,224), classes=['cat', 'dog'], batch_size=10)\nvalid_batches = ImageDataGenerator(preprocessing_function=tfk.applications.vgg16.preprocess_input).flow_from_directory(directory=valid_path, target_size=(224,224), classes=['cat', 'dog'], batch_size=10)\ntest_batches = ImageDataGenerator(preprocessing_function=tfk.applications.vgg16.preprocess_input).flow_from_directory(directory=test_path, target_size=(224,224), classes=['cat', 'dog'], batch_size=10, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:02.487667Z","iopub.execute_input":"2024-06-20T01:43:02.487894Z","iopub.status.idle":"2024-06-20T01:43:02.636808Z","shell.execute_reply.started":"2024-06-20T01:43:02.487874Z","shell.execute_reply":"2024-06-20T01:43:02.636115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgs, labels = next(train_batches)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:02.637761Z","iopub.execute_input":"2024-06-20T01:43:02.638024Z","iopub.status.idle":"2024-06-20T01:43:02.671651Z","shell.execute_reply.started":"2024-06-20T01:43:02.638002Z","shell.execute_reply":"2024-06-20T01:43:02.670941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plotImages(images_arr):\n    fig, axes = plt.subplots(1, 10, figsize=(20,20))\n    axes = axes.flatten()\n    for img, ax in zip(images_arr, axes):\n        ax.imshow(img)\n        ax.axis('off')\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:02.672727Z","iopub.execute_input":"2024-06-20T01:43:02.673005Z","iopub.status.idle":"2024-06-20T01:43:02.678861Z","shell.execute_reply.started":"2024-06-20T01:43:02.672981Z","shell.execute_reply":"2024-06-20T01:43:02.677801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotImages(imgs)\nprint(labels)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:02.680340Z","iopub.execute_input":"2024-06-20T01:43:02.680666Z","iopub.status.idle":"2024-06-20T01:43:03.330232Z","shell.execute_reply.started":"2024-06-20T01:43:02.680641Z","shell.execute_reply":"2024-06-20T01:43:03.328973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n\n## Build and Train a CNN","metadata":{}},{"cell_type":"code","source":"model = Sequential([\n    Conv2D(filters=32, kernel_size=(3,3), activation='relu', padding='same', input_shape=(224,224,3)),\n    MaxPool2D(pool_size=(2,2), strides=2),\n    Conv2D(filters=64, kernel_size=(3,3), activation='relu', padding='same'),\n    MaxPool2D(pool_size=(2,2), strides=2),\n    Flatten(),\n    Dense(units=2, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:03.331549Z","iopub.execute_input":"2024-06-20T01:43:03.331980Z","iopub.status.idle":"2024-06-20T01:43:03.404647Z","shell.execute_reply.started":"2024-06-20T01:43:03.331938Z","shell.execute_reply":"2024-06-20T01:43:03.403923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:03.405627Z","iopub.execute_input":"2024-06-20T01:43:03.405869Z","iopub.status.idle":"2024-06-20T01:43:03.430511Z","shell.execute_reply.started":"2024-06-20T01:43:03.405847Z","shell.execute_reply":"2024-06-20T01:43:03.429625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=Adam(learning_rate=0.0001), loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:03.431553Z","iopub.execute_input":"2024-06-20T01:43:03.431810Z","iopub.status.idle":"2024-06-20T01:43:03.441996Z","shell.execute_reply.started":"2024-06-20T01:43:03.431788Z","shell.execute_reply":"2024-06-20T01:43:03.441125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(x=train_batches, validation_data=valid_batches, epochs=10, verbose=2)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:03.443289Z","iopub.execute_input":"2024-06-20T01:43:03.443549Z","iopub.status.idle":"2024-06-20T01:43:37.507731Z","shell.execute_reply.started":"2024-06-20T01:43:03.443527Z","shell.execute_reply":"2024-06-20T01:43:37.506885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n\n### Predictions","metadata":{}},{"cell_type":"code","source":"test_imgs, test_labels = next(test_batches)\nplotImages(test_imgs)\nprint(test_labels)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:37.508867Z","iopub.execute_input":"2024-06-20T01:43:37.509203Z","iopub.status.idle":"2024-06-20T01:43:38.109443Z","shell.execute_reply.started":"2024-06-20T01:43:37.509176Z","shell.execute_reply":"2024-06-20T01:43:38.108546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_batches.classes","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:38.110583Z","iopub.execute_input":"2024-06-20T01:43:38.110867Z","iopub.status.idle":"2024-06-20T01:43:38.117140Z","shell.execute_reply.started":"2024-06-20T01:43:38.110840Z","shell.execute_reply":"2024-06-20T01:43:38.116248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(x=test_batches, verbose=0)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:38.118496Z","iopub.execute_input":"2024-06-20T01:43:38.118838Z","iopub.status.idle":"2024-06-20T01:43:38.675897Z","shell.execute_reply.started":"2024-06-20T01:43:38.118811Z","shell.execute_reply":"2024-06-20T01:43:38.675129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.round(predictions)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2024-06-20T01:43:38.677044Z","iopub.execute_input":"2024-06-20T01:43:38.677371Z","iopub.status.idle":"2024-06-20T01:43:38.687302Z","shell.execute_reply.started":"2024-06-20T01:43:38.677346Z","shell.execute_reply":"2024-06-20T01:43:38.686354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_batches.class_indices","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:38.688438Z","iopub.execute_input":"2024-06-20T01:43:38.688740Z","iopub.status.idle":"2024-06-20T01:43:38.694448Z","shell.execute_reply.started":"2024-06-20T01:43:38.688711Z","shell.execute_reply":"2024-06-20T01:43:38.693602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = confusion_matrix(y_true=test_batches.classes, y_pred=np.argmax(predictions, axis=-1))","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:38.695481Z","iopub.execute_input":"2024-06-20T01:43:38.695760Z","iopub.status.idle":"2024-06-20T01:43:38.701974Z","shell.execute_reply.started":"2024-06-20T01:43:38.695738Z","shell.execute_reply":"2024-06-20T01:43:38.700891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm_plot_labels = ['cats', 'dogs']\nplot_confusion_matrix(cm=cm, classes=cm_plot_labels, title='Confusion Matrix')","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:38.703118Z","iopub.execute_input":"2024-06-20T01:43:38.703906Z","iopub.status.idle":"2024-06-20T01:43:39.043367Z","shell.execute_reply.started":"2024-06-20T01:43:38.703876Z","shell.execute_reply":"2024-06-20T01:43:39.042582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n## Data Augmentation","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nimport os\nimport random\nimport tensorflow as tf\nimport tf_keras as tfk\nfrom tf_keras.preprocessing.image import ImageDataGenerator\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:39.044867Z","iopub.execute_input":"2024-06-20T01:43:39.045480Z","iopub.status.idle":"2024-06-20T01:43:39.051503Z","shell.execute_reply.started":"2024-06-20T01:43:39.045445Z","shell.execute_reply":"2024-06-20T01:43:39.050674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plotImages(images_arr):\n    fig, axes = plt.subplots(1, 10, figsize=(20,20))\n    axes = axes.flatten()\n    for img, ax in zip(images_arr, axes):\n        ax.imshow(img)\n        ax.axis('off')\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:39.052640Z","iopub.execute_input":"2024-06-20T01:43:39.052908Z","iopub.status.idle":"2024-06-20T01:43:39.060559Z","shell.execute_reply.started":"2024-06-20T01:43:39.052886Z","shell.execute_reply":"2024-06-20T01:43:39.059727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gen = ImageDataGenerator(rotation_range=10, width_shift_range=0.1,\n                        height_shift_range=0.1, shear_range=0.15,\n                        zoom_range=0.1, channel_shift_range=10.,\n                        horizontal_flip=True)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:39.061552Z","iopub.execute_input":"2024-06-20T01:43:39.061809Z","iopub.status.idle":"2024-06-20T01:43:39.068611Z","shell.execute_reply.started":"2024-06-20T01:43:39.061787Z","shell.execute_reply":"2024-06-20T01:43:39.067652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"chosen_image = random.choice(os.listdir('/kaggle/working/train/train/dog'))","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:39.069631Z","iopub.execute_input":"2024-06-20T01:43:39.069881Z","iopub.status.idle":"2024-06-20T01:43:39.077005Z","shell.execute_reply.started":"2024-06-20T01:43:39.069860Z","shell.execute_reply":"2024-06-20T01:43:39.076130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path = '/kaggle/working/train/train/dog/' + chosen_image","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:39.078165Z","iopub.execute_input":"2024-06-20T01:43:39.078897Z","iopub.status.idle":"2024-06-20T01:43:39.085751Z","shell.execute_reply.started":"2024-06-20T01:43:39.078873Z","shell.execute_reply":"2024-06-20T01:43:39.084841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = np.expand_dims(plt.imread(image_path), 0)\nplt.imshow(image[0])","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:39.087249Z","iopub.execute_input":"2024-06-20T01:43:39.088803Z","iopub.status.idle":"2024-06-20T01:43:39.403921Z","shell.execute_reply.started":"2024-06-20T01:43:39.087516Z","shell.execute_reply":"2024-06-20T01:43:39.403087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Generate batches of augmented images from this image\naug_iter = gen.flow(image)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:39.404951Z","iopub.execute_input":"2024-06-20T01:43:39.405306Z","iopub.status.idle":"2024-06-20T01:43:39.409512Z","shell.execute_reply.started":"2024-06-20T01:43:39.405281Z","shell.execute_reply":"2024-06-20T01:43:39.408511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get 10 samples of augmented images\naug_images = [next(aug_iter)[0].astype(np.uint8) for i in range(10)]","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:39.410620Z","iopub.execute_input":"2024-06-20T01:43:39.410908Z","iopub.status.idle":"2024-06-20T01:43:39.478637Z","shell.execute_reply.started":"2024-06-20T01:43:39.410874Z","shell.execute_reply":"2024-06-20T01:43:39.477935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotImages(aug_images)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:39.479673Z","iopub.execute_input":"2024-06-20T01:43:39.479931Z","iopub.status.idle":"2024-06-20T01:43:40.088015Z","shell.execute_reply.started":"2024-06-20T01:43:39.479909Z","shell.execute_reply":"2024-06-20T01:43:40.087204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n\n## Build Fine-tuned VGG16 model","metadata":{}},{"cell_type":"code","source":"vgg16_model = tfk.applications.vgg16.VGG16()","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:40.089035Z","iopub.execute_input":"2024-06-20T01:43:40.089329Z","iopub.status.idle":"2024-06-20T01:43:45.592079Z","shell.execute_reply.started":"2024-06-20T01:43:40.089304Z","shell.execute_reply":"2024-06-20T01:43:45.591224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg16_model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:45.593289Z","iopub.execute_input":"2024-06-20T01:43:45.593588Z","iopub.status.idle":"2024-06-20T01:43:45.640599Z","shell.execute_reply.started":"2024-06-20T01:43:45.593563Z","shell.execute_reply":"2024-06-20T01:43:45.637272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(vgg16_model)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:45.641669Z","iopub.execute_input":"2024-06-20T01:43:45.641925Z","iopub.status.idle":"2024-06-20T01:43:45.697092Z","shell.execute_reply.started":"2024-06-20T01:43:45.641903Z","shell.execute_reply":"2024-06-20T01:43:45.696050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tfk.Sequential()\nfor layer in vgg16_model.layers[:-1]:\n    model.add(layer)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:45.698273Z","iopub.execute_input":"2024-06-20T01:43:45.698612Z","iopub.status.idle":"2024-06-20T01:43:45.811963Z","shell.execute_reply.started":"2024-06-20T01:43:45.698581Z","shell.execute_reply":"2024-06-20T01:43:45.811005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()\n#Last layer is removed.","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:45.813520Z","iopub.execute_input":"2024-06-20T01:43:45.813840Z","iopub.status.idle":"2024-06-20T01:43:45.857403Z","shell.execute_reply.started":"2024-06-20T01:43:45.813811Z","shell.execute_reply":"2024-06-20T01:43:45.855932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in model.layers:\n    layer.trainable = False\n    #Freezes weights and biases -- not retrained","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:45.863674Z","iopub.execute_input":"2024-06-20T01:43:45.863970Z","iopub.status.idle":"2024-06-20T01:43:45.868820Z","shell.execute_reply.started":"2024-06-20T01:43:45.863945Z","shell.execute_reply":"2024-06-20T01:43:45.867959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#adding own output layer\nmodel.add(tfk.layers.Dense(units=2, activation=\"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:45.870151Z","iopub.execute_input":"2024-06-20T01:43:45.870435Z","iopub.status.idle":"2024-06-20T01:43:45.895701Z","shell.execute_reply.started":"2024-06-20T01:43:45.870411Z","shell.execute_reply":"2024-06-20T01:43:45.894848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:45.896648Z","iopub.execute_input":"2024-06-20T01:43:45.896871Z","iopub.status.idle":"2024-06-20T01:43:45.947728Z","shell.execute_reply.started":"2024-06-20T01:43:45.896851Z","shell.execute_reply":"2024-06-20T01:43:45.946815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Train the finetuned model","metadata":{}},{"cell_type":"code","source":"model.compile(\n    optimizer=Adam(learning_rate=0.0001),\n    loss='categorical_crossentropy',\n    metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:45.949237Z","iopub.execute_input":"2024-06-20T01:43:45.949559Z","iopub.status.idle":"2024-06-20T01:43:45.962347Z","shell.execute_reply.started":"2024-06-20T01:43:45.949530Z","shell.execute_reply":"2024-06-20T01:43:45.961507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(x=train_batches, validation_data=valid_batches, epochs=5, verbose=2)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:43:45.963422Z","iopub.execute_input":"2024-06-20T01:43:45.963724Z","iopub.status.idle":"2024-06-20T01:44:26.256966Z","shell.execute_reply.started":"2024-06-20T01:43:45.963695Z","shell.execute_reply":"2024-06-20T01:44:26.255968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Predict using finetuned model","metadata":{}},{"cell_type":"code","source":"predictions = model.predict(x=test_batches, verbose=0)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:44:26.258417Z","iopub.execute_input":"2024-06-20T01:44:26.258772Z","iopub.status.idle":"2024-06-20T01:44:27.690893Z","shell.execute_reply.started":"2024-06-20T01:44:26.258740Z","shell.execute_reply":"2024-06-20T01:44:27.690043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_batches.classes","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:44:27.692230Z","iopub.execute_input":"2024-06-20T01:44:27.692545Z","iopub.status.idle":"2024-06-20T01:44:27.698859Z","shell.execute_reply.started":"2024-06-20T01:44:27.692517Z","shell.execute_reply":"2024-06-20T01:44:27.697906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = confusion_matrix(y_true=test_batches.classes, y_pred=np.argmax(predictions, axis=-1))","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:44:27.700103Z","iopub.execute_input":"2024-06-20T01:44:27.700441Z","iopub.status.idle":"2024-06-20T01:44:27.707682Z","shell.execute_reply.started":"2024-06-20T01:44:27.700410Z","shell.execute_reply":"2024-06-20T01:44:27.706714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_batches.class_indices","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:44:27.708811Z","iopub.execute_input":"2024-06-20T01:44:27.709152Z","iopub.status.idle":"2024-06-20T01:44:27.716387Z","shell.execute_reply.started":"2024-06-20T01:44:27.709122Z","shell.execute_reply":"2024-06-20T01:44:27.715442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm_plot_labels=['cat','dog']\nplot_confusion_matrix(cm=cm, classes=cm_plot_labels, title='Confusion Matrix')","metadata":{"execution":{"iopub.status.busy":"2024-06-20T01:44:27.717829Z","iopub.execute_input":"2024-06-20T01:44:27.718805Z","iopub.status.idle":"2024-06-20T01:44:28.047094Z","shell.execute_reply.started":"2024-06-20T01:44:27.718776Z","shell.execute_reply":"2024-06-20T01:44:28.046214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}