{"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-27T04:53:29.235901Z","iopub.execute_input":"2023-05-27T04:53:29.236256Z","iopub.status.idle":"2023-05-27T04:54:00.744153Z","shell.execute_reply.started":"2023-05-27T04:53:29.236223Z","shell.execute_reply":"2023-05-27T04:54:00.738944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\n\n# load data set\n\ndf= pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-27T04:55:11.406147Z","iopub.execute_input":"2023-05-27T04:55:11.406444Z","iopub.status.idle":"2023-05-27T04:55:11.463544Z","shell.execute_reply.started":"2023-05-27T04:55:11.406421Z","shell.execute_reply":"2023-05-27T04:55:11.462587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check the shape of the dataset\nprint(\"Dataset shape:\", df.shape)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T04:55:28.037717Z","iopub.execute_input":"2023-05-27T04:55:28.038048Z","iopub.status.idle":"2023-05-27T04:55:28.044293Z","shell.execute_reply.started":"2023-05-27T04:55:28.038019Z","shell.execute_reply":"2023-05-27T04:55:28.042561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Check for missing values\nprint(\"Missing values:\\n\", df.isnull().sum())\n\n","metadata":{"execution":{"iopub.status.busy":"2023-05-27T04:55:35.385636Z","iopub.execute_input":"2023-05-27T04:55:35.385982Z","iopub.status.idle":"2023-05-27T04:55:35.397494Z","shell.execute_reply.started":"2023-05-27T04:55:35.385955Z","shell.execute_reply":"2023-05-27T04:55:35.396715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data types\ndf.dtypes","metadata":{"execution":{"iopub.status.busy":"2023-05-27T04:55:44.460297Z","iopub.execute_input":"2023-05-27T04:55:44.460608Z","iopub.status.idle":"2023-05-27T04:55:44.467397Z","shell.execute_reply.started":"2023-05-27T04:55:44.460586Z","shell.execute_reply":"2023-05-27T04:55:44.466435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2023-05-27T04:55:50.608010Z","iopub.execute_input":"2023-05-27T04:55:50.608314Z","iopub.status.idle":"2023-05-27T04:55:50.633019Z","shell.execute_reply.started":"2023-05-27T04:55:50.608288Z","shell.execute_reply":"2023-05-27T04:55:50.631874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-05-27T04:55:55.869289Z","iopub.execute_input":"2023-05-27T04:55:55.869714Z","iopub.status.idle":"2023-05-27T04:55:55.884054Z","shell.execute_reply.started":"2023-05-27T04:55:55.869686Z","shell.execute_reply":"2023-05-27T04:55:55.882687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Dataset summary\ntotal_images = len(df)\nunique_breeds = df[\"breed\"].nunique()\n\n# Class distribution\nclass_counts = df[\"breed\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-05-27T04:56:01.435328Z","iopub.execute_input":"2023-05-27T04:56:01.435667Z","iopub.status.idle":"2023-05-27T04:56:01.445475Z","shell.execute_reply.started":"2023-05-27T04:56:01.435644Z","shell.execute_reply":"2023-05-27T04:56:01.444558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check the class distribution\nclass_counts = df['breed'].value_counts()\nprint(\"Class distribution:\\n\", class_counts)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-05-27T04:56:06.644319Z","iopub.execute_input":"2023-05-27T04:56:06.644691Z","iopub.status.idle":"2023-05-27T04:56:06.652637Z","shell.execute_reply.started":"2023-05-27T04:56:06.644663Z","shell.execute_reply":"2023-05-27T04:56:06.651719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set the size of the plot\nfig, ax = plt.subplots(figsize=(10, 6))\n\n# Visualization\nclass_counts.plot(kind=\"bar\", ax=ax)\nplt.xlabel(\"Dog Breed\")\nplt.ylabel(\"Number of Images\")\nplt.title(\"Class Distribution\")\nplt.xticks(rotation=90)\nplt.show()\n\n# Generate EDA report\neda_report = f\"Dataset Summary:\\nTotal Images: {total_images}\\nUnique Breeds: {unique_breeds}\\n\\nClass Distribution:\\n{class_counts}\\n\\n\"\n\n","metadata":{"execution":{"iopub.status.busy":"2023-05-27T04:56:12.365836Z","iopub.execute_input":"2023-05-27T04:56:12.366168Z","iopub.status.idle":"2023-05-27T04:56:13.307203Z","shell.execute_reply.started":"2023-05-27T04:56:12.366143Z","shell.execute_reply":"2023-05-27T04:56:13.306061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Observation:\nWe have total 120 unique breeds\nWe have atleast 60 images per category of breed\nGoogle recommends at least 10 images per class to bet a better model\nMore sample available ----- > More efficeint the model will be","metadata":{"execution":{"iopub.status.busy":"2023-05-27T04:56:41.326976Z","iopub.execute_input":"2023-05-27T04:56:41.327330Z","iopub.status.idle":"2023-05-27T04:56:41.333875Z","shell.execute_reply.started":"2023-05-27T04:56:41.327303Z","shell.execute_reply":"2023-05-27T04:56:41.332724Z"}}},{"cell_type":"code","source":"col_list=[]\nfor i in df.columns:\n    if((df[i].dtypes=='object')&(i!='y')):\n        col_list.append(i)\n\nfrom sklearn.preprocessing import LabelEncoder\n\nL=LabelEncoder()\n\nfor i in col_list:\n    df[i]=L.fit_transform(df[i])\n\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-27T04:57:29.104674Z","iopub.execute_input":"2023-05-27T04:57:29.105032Z","iopub.status.idle":"2023-05-27T04:57:29.642432Z","shell.execute_reply.started":"2023-05-27T04:57:29.105005Z","shell.execute_reply":"2023-05-27T04:57:29.641114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x= df.iloc[:, :-1].values\ny=df.iloc[:, -1].values  ","metadata":{"execution":{"iopub.status.busy":"2023-05-27T04:57:35.397928Z","iopub.execute_input":"2023-05-27T04:57:35.398299Z","iopub.status.idle":"2023-05-27T04:57:35.407269Z","shell.execute_reply.started":"2023-05-27T04:57:35.398269Z","shell.execute_reply":"2023-05-27T04:57:35.406296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check the class distribution\nnum_classes = df['breed'].value_counts()\nprint(\"Class distribution:\\n\", num_classes)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T04:57:40.258480Z","iopub.execute_input":"2023-05-27T04:57:40.258869Z","iopub.status.idle":"2023-05-27T04:57:40.268467Z","shell.execute_reply.started":"2023-05-27T04:57:40.258840Z","shell.execute_reply":"2023-05-27T04:57:40.266690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train, x_val, y_train, y_val = train_test_split(x, y, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T04:57:45.046012Z","iopub.execute_input":"2023-05-27T04:57:45.046353Z","iopub.status.idle":"2023-05-27T04:57:45.164087Z","shell.execute_reply.started":"2023-05-27T04:57:45.046320Z","shell.execute_reply":"2023-05-27T04:57:45.162942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense\nimport pandas as pd\nimport numpy as np\n\n# Create a directory to store TensorBoard logs\nlog_dir = \"logs/\"\nos.makedirs(log_dir, exist_ok=True)\n\n# Define the number of experiments and different architectures/topologies\nnum_experiments = 5\narchitectures = [\n    [64, 64],\n    [128, 128, 64],\n    [32, 32, 32, 32],\n]\n\n# Determine the number of classes\nnum_classes = np.unique(y_train).size\n\n# One-hot encode the target variable\ny_train_one_hot = tf.keras.utils.to_categorical(y_train, num_classes)\ny_val_one_hot = tf.keras.utils.to_categorical(y_val, num_classes)\n\n# Iterate over different architectures and topologies\nfor i, architecture in enumerate(architectures):\n    # Create the model\n    model = Sequential()\n    for units in architecture:\n        model.add(Dense(units, activation='relu'))\n    model.add(Dense(num_classes, activation='softmax'))\n\n    # Compile the model\n    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\n\n    # Create a unique log directory for each experiment\n    experiment_log_dir = os.path.join(log_dir, f\"experiment_{i+1}\")\n\n    # Create a TensorBoard callback for the experiment\n    tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=experiment_log_dir, histogram_freq=1)\n\n    # Train the model with TensorBoard callback\n    model.fit(x_train, y_train_one_hot, batch_size=32, epochs=10, validation_data=(x_val, y_val_one_hot),\n              callbacks=[tensorboard_callback])\n\n    # Get major metrics (e.g., accuracy, loss)\n    train_loss, train_accuracy = model.evaluate(x_train, y_train_one_hot, verbose=0)\n    val_loss, val_accuracy = model.evaluate(x_val, y_val_one_hot, verbose=0)\n\n    # Save major metrics in an Excel file\n    metrics_data = {\n        'Train Loss': [train_loss],\n        'Train Accuracy': [train_accuracy],\n        'Validation Loss': [val_loss],\n        'Validation Accuracy': [val_accuracy]\n    }\n\n    metrics_df = pd.DataFrame(metrics_data)\n    metrics_df.to_excel(f\"experiment_{i+1}_metrics.xlsx\", index=False)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-27T04:57:51.269026Z","iopub.execute_input":"2023-05-27T04:57:51.269396Z","iopub.status.idle":"2023-05-27T04:58:25.633786Z","shell.execute_reply.started":"2023-05-27T04:57:51.269348Z","shell.execute_reply":"2023-05-27T04:58:25.632719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}