{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"Published on August 30, 2023. By Marília Prata, mpwolke.","metadata":{}},{"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)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn import feature_extraction, linear_model, model_selection, preprocessing\nimport plotly.graph_objs as go\nimport plotly.offline as py\nimport plotly.express as px\n\n#Ignore warnings\nimport warnings\nwarnings.filterwarnings('ignore')\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-08-31T00:31:43.198951Z","iopub.execute_input":"2023-08-31T00:31:43.200273Z","iopub.status.idle":"2023-08-31T00:31:44.426241Z","shell.execute_reply.started":"2023-08-31T00:31:43.200228Z","shell.execute_reply":"2023-08-31T00:31:44.425212Z"},"_kg_hide-input":true,"_kg_hide-output":true,"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport cv2\nimport random\n\nfrom tensorflow.keras.layers import Input , Dense , Flatten , GlobalAveragePooling2D\nfrom tensorflow.keras.models import Sequential\n\n# for model building\nimport tensorflow as tf\nfrom tensorflow.keras.callbacks import ModelCheckpoint\ntf.random.set_seed(42)\n\n# some utils\nfrom sklearn.model_selection import train_test_split\nfrom random import randint","metadata":{"execution":{"iopub.status.busy":"2023-08-31T00:32:06.852784Z","iopub.execute_input":"2023-08-31T00:32:06.853437Z","iopub.status.idle":"2023-08-31T00:32:06.860463Z","shell.execute_reply.started":"2023-08-31T00:32:06.853400Z","shell.execute_reply":"2023-08-31T00:32:06.859403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Below sometimes is without the train to get the classes","metadata":{}},{"cell_type":"code","source":"path = '../input/face-recognition-xt122/Train/' #Don't forget that last bar","metadata":{"execution":{"iopub.status.busy":"2023-08-31T00:32:11.345395Z","iopub.execute_input":"2023-08-31T00:32:11.345794Z","iopub.status.idle":"2023-08-31T00:32:11.350971Z","shell.execute_reply.started":"2023-08-31T00:32:11.345762Z","shell.execute_reply":"2023-08-31T00:32:11.349383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = sorted(os.listdir(path))\nnum_classes = len(class_names)\n\nimg_size = (192, 192, 3)\n\nprint('classes: ', class_names)","metadata":{"execution":{"iopub.status.busy":"2023-08-31T00:32:15.828594Z","iopub.execute_input":"2023-08-31T00:32:15.829031Z","iopub.status.idle":"2023-08-31T00:32:15.838526Z","shell.execute_reply.started":"2023-08-31T00:32:15.828995Z","shell.execute_reply":"2023-08-31T00:32:15.837380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#CRN4TWW https://www.kaggle.com/code/crn4tww/dog-emotions-classifier\n\nlabels = []\nimages = []\n\nprint('images:\\n')\nfor cl in class_names:\n    print(cl, end=' -> ')\n    for img in os.listdir(path + cl):\n        label = np.zeros(num_classes)\n        label[class_names.index(cl)] = 1\n        labels.append(label)\n        \n        image = np.asarray(cv2.resize(cv2.imread(path + cl + '/' + img, cv2.IMREAD_COLOR), img_size[0:2])[:, :, ::-1])#That line remove Blue\n        \n        #image = np.asarray(cv2.resize(cv2.imread(data_path + cl + '/' + img, cv2.COLOR_BGR2RGB), img_size[0:2]))\n        images.append(image)\n    print('done')\n\n    \nlabels = np.asarray(labels)\nimages = np.asarray(images)\n\nprint(f'\\n\\nlabels shape: {labels.shape}')\nprint(f'images shape: {images.shape}')","metadata":{"execution":{"iopub.status.busy":"2023-08-31T00:33:03.645083Z","iopub.execute_input":"2023-08-31T00:33:03.645455Z","iopub.status.idle":"2023-08-31T00:33:12.894642Z","shell.execute_reply.started":"2023-08-31T00:33:03.645425Z","shell.execute_reply":"2023-08-31T00:33:12.893574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#CRN4TWW https://www.kaggle.com/code/crn4tww/cnn-cheetahs-and-lions-classifier\n\nfig, axs = plt.subplots(3, 3, figsize=(10, 10))\n\nfor x in range(3):\n    for y in range(3):\n        i = randint(0, len(images))\n        \n        axs[x][y].imshow(images[i])\n        \n                    \n        axs[x][y].set_xticks([])\n        axs[x][y].set_yticks([])\n        axs[x][y].set_xlabel(class_names[np.argmax(labels[i])])\n        \n        \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-31T00:33:18.875399Z","iopub.execute_input":"2023-08-31T00:33:18.875780Z","iopub.status.idle":"2023-08-31T00:33:19.776297Z","shell.execute_reply.started":"2023-08-31T00:33:18.875749Z","shell.execute_reply":"2023-08-31T00:33:19.775381Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Train/Test Split\n\nWe don't have train/test/validation","metadata":{}},{"cell_type":"code","source":"#CRN4TWW https://www.kaggle.com/code/crn4tww/dog-emotions-classifier\n\nX_train, X_val, y_train, y_val = train_test_split(images, labels, test_size=0.15, random_state=42)\n\nprint(f'train images shape: {X_train.shape}\\ntrain labels shape: {y_train.shape}\\n\\nvalidation images shape: {X_val.shape}\\nvalidation labels shape: {y_val.shape}\\n')","metadata":{"execution":{"iopub.status.busy":"2023-08-31T00:33:32.211129Z","iopub.execute_input":"2023-08-31T00:33:32.211574Z","iopub.status.idle":"2023-08-31T00:33:32.241605Z","shell.execute_reply.started":"2023-08-31T00:33:32.211538Z","shell.execute_reply":"2023-08-31T00:33:32.239972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Above Validation is 87 (out of b error on the last snippet)\n\nCNN Model","metadata":{}},{"cell_type":"code","source":"#CRN4TWW https://www.kaggle.com/code/crn4tww/dog-emotions-classifier\n\ncnn_model = tf.keras.Sequential()\n\n# Inputs and rescaling\ncnn_model.add(tf.keras.layers.Rescaling(scale=1. / 255, input_shape=(img_size)))\n\n# Convolutional block 1\ncnn_model.add(tf.keras.layers.Conv2D(64, (3, 3), activation='relu', padding='same'))\ncnn_model.add(tf.keras.layers.Conv2D(64, (3, 3), activation='relu', padding='same'))\ncnn_model.add(tf.keras.layers.MaxPooling2D(pool_size=2))\n\n# Convolutional block 2\ncnn_model.add(tf.keras.layers.Conv2D(128, (2, 2), activation='relu', padding='same'))\ncnn_model.add(tf.keras.layers.Conv2D(128, (2, 2), activation='relu', padding='same'))\ncnn_model.add(tf.keras.layers.MaxPooling2D(pool_size=2))\n\n# Convolutional block 3\ncnn_model.add(tf.keras.layers.Conv2D(256, (2, 2), activation='relu', padding='same'))\ncnn_model.add(tf.keras.layers.Conv2D(256, (2, 2), activation='relu', padding='same'))\ncnn_model.add(tf.keras.layers.MaxPooling2D(pool_size=2))\n\n# Convolutional block 4\ncnn_model.add(tf.keras.layers.Conv2D(512, (2, 2), activation='relu', padding='same'))\ncnn_model.add(tf.keras.layers.Conv2D(512, (2, 2), activation='relu', padding='same'))\ncnn_model.add(tf.keras.layers.MaxPooling2D(pool_size=2))\ncnn_model.add(tf.keras.layers.Flatten())\n\n# Dense block\ncnn_model.add(tf.keras.layers.Dense(256, activation='relu'))\ncnn_model.add(tf.keras.layers.Dense(128, activation='relu'))\ncnn_model.add(tf.keras.layers.Dense(64, activation='relu'))\ncnn_model.add(tf.keras.layers.Dense(num_classes, activation='softmax'))\n\n\ncnn_model.compile(optimizer='Adamax', loss='categorical_crossentropy', metrics=['accuracy'])\n\ncnn_model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-08-31T00:33:38.178906Z","iopub.execute_input":"2023-08-31T00:33:38.179300Z","iopub.status.idle":"2023-08-31T00:33:41.578176Z","shell.execute_reply.started":"2023-08-31T00:33:38.179272Z","shell.execute_reply":"2023-08-31T00:33:41.577333Z"},"_kg_hide-output":true,"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#CRN4TWW https://www.kaggle.com/code/crn4tww/dog-emotions-classifier\n\n# creating ModelChecpoint callback\ncheckpoint_callback = ModelCheckpoint('cnn_model/model{epoch:02d}')","metadata":{"execution":{"iopub.status.busy":"2023-08-31T00:33:50.972918Z","iopub.execute_input":"2023-08-31T00:33:50.973684Z","iopub.status.idle":"2023-08-31T00:33:50.979452Z","shell.execute_reply.started":"2023-08-31T00:33:50.973647Z","shell.execute_reply":"2023-08-31T00:33:50.978213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Epochs\n\nStarted 21:34 till 21:36","metadata":{}},{"cell_type":"code","source":"history = cnn_model.fit(images, labels, epochs=20, validation_data=(X_val, y_val), callbacks=[checkpoint_callback])","metadata":{"execution":{"iopub.status.busy":"2023-08-31T00:33:55.527355Z","iopub.execute_input":"2023-08-31T00:33:55.527752Z","iopub.status.idle":"2023-08-31T00:36:19.496942Z","shell.execute_reply.started":"2023-08-31T00:33:55.527720Z","shell.execute_reply":"2023-08-31T00:36:19.495762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Accuracy and Loss Plot","metadata":{}},{"cell_type":"code","source":"#CRN4TWW https://www.kaggle.com/code/crn4tww/dog-emotions-classifier\n\naccuracy = history.history['accuracy']\nval_accuracy = history.history['val_accuracy']\n\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(len(accuracy))\n\nplt.figure()\nplt.plot(epochs, accuracy, label='Training Accuracy')\nplt.plot(epochs, loss, label='Training Loss')\nplt.legend()\nplt.title('Training Accuracy and Loss')\n\nplt.figure()\nplt.plot(epochs, val_accuracy, label='Validation Accuracy')\nplt.plot(epochs, val_loss, label='Validation Loss')\nplt.legend()\nplt.title('Validation Accuracy and Loss')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-31T00:36:34.464611Z","iopub.execute_input":"2023-08-31T00:36:34.465073Z","iopub.status.idle":"2023-08-31T00:36:35.207801Z","shell.execute_reply.started":"2023-08-31T00:36:34.465037Z","shell.execute_reply":"2023-08-31T00:36:35.206863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Prediction\n\nAttention validation was 87 so we didn't have any issue below. Though I tried higher numbers of subplots and I got empty squares. Then, I decided to keep subplots(4, 4) and range 4 ","metadata":{}},{"cell_type":"code","source":"#CRN4TWW https://www.kaggle.com/code/crn4tww/dog-emotions-classifier\n\nfig, axs = plt.subplots(4, 4, figsize=(30, 30))#Original was 7,4 (28 is higher than 25) and figsize 15,15\n\ni = 0\nfor x in range(4): #Original was 7 so resulted in error 7X4 = 28\n    for y in range(4):\n        prediction = cnn_model.predict(X_val[i][None, ...], verbose=0)[0]\n        \n        axs[x][y].set_xticks([])\n        axs[x][y].set_yticks([])\n        axs[x][y].set_xlabel(f'prediction: {class_names[np.argmax(prediction)]} | label: {class_names[np.argmax(y_val[i])]}')\n        \n        axs[x][y].imshow(X_val[i])\n        \n        i += 1\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-31T00:39:59.587006Z","iopub.execute_input":"2023-08-31T00:39:59.587388Z","iopub.status.idle":"2023-08-31T00:40:04.334561Z","shell.execute_reply.started":"2023-08-31T00:39:59.587361Z","shell.execute_reply":"2023-08-31T00:40:04.326987Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Acknowledgements:\n\nmpwolke https://www.kaggle.com/code/mpwolke/avoid-opencv2-blue-images\n\nmpwolke https://www.kaggle.com/code/mpwolke/monkeys-cnn-classifier#CNN-(Convolutional-Neural-Network)\n\nmpwolke https://www.kaggle.com/code/mpwolke/coral-snake-cnn\n\nCRN4TWW https://www.kaggle.com/code/crn4tww/dog-emotions-classifier\n\nCRN4TWW https://www.kaggle.com/code/crn4tww/cnn-cheetahs-and-lions-classifier","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"}}