{"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":"# 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","execution":{"iopub.status.busy":"2023-05-02T17:44:53.230305Z","iopub.execute_input":"2023-05-02T17:44:53.231288Z","iopub.status.idle":"2023-05-02T17:44:53.244191Z","shell.execute_reply.started":"2023-05-02T17:44:53.23125Z","shell.execute_reply":"2023-05-02T17:44:53.243206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport os\nimport cv2\nimport random\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Flatten\nfrom tensorflow.keras.optimizers import SGD\nfrom sklearn.svm import SVC\nfrom sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix\nfrom sklearn import svm","metadata":{"execution":{"iopub.status.busy":"2023-05-02T17:44:53.246372Z","iopub.execute_input":"2023-05-02T17:44:53.246641Z","iopub.status.idle":"2023-05-02T17:45:02.263545Z","shell.execute_reply.started":"2023-05-02T17:44:53.246619Z","shell.execute_reply":"2023-05-02T17:45:02.262586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip \"/kaggle/input/dogs-vs-cats/train.zip\"","metadata":{"execution":{"iopub.status.busy":"2023-05-02T17:45:02.264916Z","iopub.execute_input":"2023-05-02T17:45:02.265614Z","iopub.status.idle":"2023-05-02T17:45:16.287173Z","shell.execute_reply.started":"2023-05-02T17:45:02.265579Z","shell.execute_reply":"2023-05-02T17:45:16.285934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the path to the data\ndata_path = 'train/'\n\n# Define the dimensions of the input images\nimg_width = 64\nimg_height = 64\n\n# Define the number of samples to use for training and validation\ntrain_samples = 2000\nval_samples = 1000\ntest_samples = 500\n\n# Define the batch size and number of epochs for training\nbatch_size = 32\nepochs = 10","metadata":{"execution":{"iopub.status.busy":"2023-05-02T17:45:16.290319Z","iopub.execute_input":"2023-05-02T17:45:16.291197Z","iopub.status.idle":"2023-05-02T17:45:16.297225Z","shell.execute_reply.started":"2023-05-02T17:45:16.291152Z","shell.execute_reply":"2023-05-02T17:45:16.296239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls","metadata":{"execution":{"iopub.status.busy":"2023-05-02T17:45:16.302078Z","iopub.execute_input":"2023-05-02T17:45:16.302803Z","iopub.status.idle":"2023-05-02T17:45:17.308509Z","shell.execute_reply.started":"2023-05-02T17:45:16.30277Z","shell.execute_reply":"2023-05-02T17:45:17.307393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nfrom sklearn.model_selection import train_test_split\nfrom tqdm import tqdm\n\n# Load the data and split into training and validation sets\ndef load_data():\n    # Define lists to store the images and labels\n    images = []\n    labels = []\n    \n    # Loop over the files in the data directory\n    for filename in tqdm(os.listdir(data_path)):\n        # Skip any files that are not JPG images\n        if not filename.endswith('.jpg'):\n            continue\n        \n        # Extract the label (cat or dog) from the filename\n        label = filename.split('.')[0]\n        \n        # Load the image and resize it to the desired dimensions\n        img = cv2.imread(os.path.join(data_path, filename))\n        img = cv2.resize(img, (img_width, img_height))\n        \n        # Append the image and label to the lists\n        images.append(img)\n        labels.append(label)\n    \n    # Convert the lists to numpy arrays\n    images = np.array(images)\n    labels = np.array(labels)\n    \n    # Shuffle the data\n    indices = np.arange(len(images))\n    np.random.shuffle(indices)\n    images = images[indices]\n    labels = labels[indices]\n    \n    # Split into training and validation sets\n    x_train = images[:train_samples]\n    y_train = labels[:train_samples]\n\n    x_val, x_test, y_val, y_test = train_test_split(images[train_samples:train_samples+val_samples], \n                                                labels[train_samples:train_samples+val_samples], \n                                                test_size=0.5, \n                                                random_state=42)\n    \n    \n    # Convert the labels to binary (cat = 0, dog = 1)\n    y_train = np.where(y_train == 'cat', 0, 1)\n    y_val = np.where(y_val == 'cat', 0, 1)\n    y_test = np.where(y_test == 'cat', 0, 1)\n    \n    # Convert the labels to binary categorical format (cat = 0, dog = 1)\n    y_train = to_categorical(np.where(y_train == 'cat', 0, 1), num_classes=2)\n    y_val = to_categorical(np.where(y_val == 'cat', 0, 1), num_classes=2)\n    y_test = to_categorical(np.where(y_test == 'cat', 0, 1), num_classes=2)\n    \n    return x_train, y_train, x_val, y_val, x_test, y_test\n\nx_train, y_train, x_val, y_val, x_test, y_test = load_data()","metadata":{"execution":{"iopub.status.busy":"2023-05-02T17:45:17.31157Z","iopub.execute_input":"2023-05-02T17:45:17.311942Z","iopub.status.idle":"2023-05-02T17:46:04.804775Z","shell.execute_reply.started":"2023-05-02T17:45:17.311907Z","shell.execute_reply":"2023-05-02T17:46:04.80293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the CNN model\nmodel = Sequential()\nmodel.add(Conv2D(32, (3,3), activation='relu', input_shape=(img_width,img_height,3)))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(64, (3,3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(128, (3,3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Flatten())\n\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dense(1, activation='softmax'))\n\n# Compile the model\nsgd = SGD(lr=0.01)\nmodel.compile(optimizer=sgd, loss='binary_crossentropy', metrics=['accuracy'])\n\n# Train the model\nmodel.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_val, y_val))","metadata":{"execution":{"iopub.status.busy":"2023-05-02T17:46:04.806113Z","iopub.status.idle":"2023-05-02T17:46:04.80657Z","shell.execute_reply.started":"2023-05-02T17:46:04.806346Z","shell.execute_reply":"2023-05-02T17:46:04.806368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report, confusion_matrix\n\n# Make predictions on the test set\ny_pred = model.predict(x_test)\ny_pred_binary = np.argmax(y_pred, axis=1)\ny_test_binary = np.argmax(y_test, axis=1)\n\n# Print the classification report and confusion matrix\nprint(\"Classification report:\\n\", classification_report(y_test_binary, y_pred_binary))\nprint(\"Confusion matrix:\\n\", confusion_matrix(y_test_binary, y_pred_binary))","metadata":{"execution":{"iopub.status.busy":"2023-05-02T17:46:04.807964Z","iopub.status.idle":"2023-05-02T17:46:04.808756Z","shell.execute_reply.started":"2023-05-02T17:46:04.808521Z","shell.execute_reply":"2023-05-02T17:46:04.808544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred","metadata":{"execution":{"iopub.status.busy":"2023-05-02T17:46:04.810091Z","iopub.status.idle":"2023-05-02T17:46:04.810907Z","shell.execute_reply.started":"2023-05-02T17:46:04.810665Z","shell.execute_reply":"2023-05-02T17:46:04.81069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extract features from the validation set using the fully connected layer\nfc_layer_model = Sequential()\nfc_layer_model.add(Flatten(input_shape=model.output_shape[1:]))\nfc_layer_model.add(Dense(128, activation='relu'))\n\nx_val_features = model.predict(x_val)\nx_val_features = fc_layer_model.predict(x_val_features)\ny_val_binary = np.where(y_val == 0, -1, y_val)\n\nx_val_features = np.nan_to_num(x_val_features, nan=0.0)","metadata":{"execution":{"iopub.status.busy":"2023-05-02T17:46:04.812383Z","iopub.status.idle":"2023-05-02T17:46:04.812803Z","shell.execute_reply.started":"2023-05-02T17:46:04.812588Z","shell.execute_reply":"2023-05-02T17:46:04.812608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_val_features","metadata":{"execution":{"iopub.status.busy":"2023-05-02T17:46:04.814801Z","iopub.status.idle":"2023-05-02T17:46:04.815468Z","shell.execute_reply.started":"2023-05-02T17:46:04.815239Z","shell.execute_reply":"2023-05-02T17:46:04.815261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train a SVM using the features extracted from the validation set\nclf = svm.SVC(kernel='linear')\nclf.fit(x_val_features, y_val_binary)\n\n# Extract features from the test set using the same fully connected layer\nx_test_features = model.predict(x_test)\nx_test_features = fc_layer_model.predict(x_test_features)\ny_test_binary = np.where(y_test == 0, -1, y_test)\n\n# Test the SVM on the test set\ny_pred = clf.predict(x_test_features)\n\n# Evaluate the SVM's performance on the test set\nacc = accuracy_score(y_test_binary, y_pred)\ncm = confusion_matrix(y_test_binary, y_pred)\n\nprint(\"Accuracy:\", acc)\nprint(\"Confusion matrix:\\n\", cm)","metadata":{"execution":{"iopub.status.busy":"2023-05-02T17:46:04.816915Z","iopub.status.idle":"2023-05-02T17:46:04.817721Z","shell.execute_reply.started":"2023-05-02T17:46:04.817487Z","shell.execute_reply":"2023-05-02T17:46:04.817509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}