{"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-10-13T17:13:08.110857Z","iopub.execute_input":"2023-10-13T17:13:08.111239Z","iopub.status.idle":"2023-10-13T17:13:14.640256Z","shell.execute_reply.started":"2023-10-13T17:13:08.111208Z","shell.execute_reply":"2023-10-13T17:13:14.638943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os\nfrom PIL import Image\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\nfrom tensorflow.keras.utils import to_categorical\n","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:14:40.433321Z","iopub.execute_input":"2023-10-13T17:14:40.433898Z","iopub.status.idle":"2023-10-13T17:14:44.571976Z","shell.execute_reply.started":"2023-10-13T17:14:40.433864Z","shell.execute_reply":"2023-10-13T17:14:44.570679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\n","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:15:02.278808Z","iopub.execute_input":"2023-10-13T17:15:02.279522Z","iopub.status.idle":"2023-10-13T17:15:02.320574Z","shell.execute_reply.started":"2023-10-13T17:15:02.279486Z","shell.execute_reply":"2023-10-13T17:15:02.319321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = \"/kaggle/input/dog-breed-identification/train\"\ntest_dir = \"/kaggle/input/dog-breed-identification/test\"","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:15:18.104612Z","iopub.execute_input":"2023-10-13T17:15:18.105008Z","iopub.status.idle":"2023-10-13T17:15:18.109696Z","shell.execute_reply.started":"2023-10-13T17:15:18.104975Z","shell.execute_reply":"2023-10-13T17:15:18.108444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_images(directory):\n    images = []\n    for filename in os.listdir(directory):\n        img = Image.open(os.path.join(directory, filename))\n        img = img.resize((128, 128))\n        img = np.array(img)\n        images.append(img)\n    return np.array(images)","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:15:22.434664Z","iopub.execute_input":"2023-10-13T17:15:22.435042Z","iopub.status.idle":"2023-10-13T17:15:22.442514Z","shell.execute_reply.started":"2023-10-13T17:15:22.435014Z","shell.execute_reply":"2023-10-13T17:15:22.441134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = load_images(train_dir)\ntest_images = load_images(test_dir)","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:15:27.300415Z","iopub.execute_input":"2023-10-13T17:15:27.300853Z","iopub.status.idle":"2023-10-13T17:18:09.999846Z","shell.execute_reply.started":"2023-10-13T17:15:27.300799Z","shell.execute_reply":"2023-10-13T17:18:09.998669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_mapping = {label: i for i, label in enumerate(labels_df['breed'].unique())}\n\nlabels = labels_df['breed'].map(label_mapping).values\nprint(labels)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:19:52.940100Z","iopub.execute_input":"2023-10-13T17:19:52.940603Z","iopub.status.idle":"2023-10-13T17:19:52.968621Z","shell.execute_reply.started":"2023-10-13T17:19:52.940568Z","shell.execute_reply":"2023-10-13T17:19:52.967267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_size = 0.2\nrandom_seed = 42","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:21:39.486575Z","iopub.execute_input":"2023-10-13T17:21:39.487029Z","iopub.status.idle":"2023-10-13T17:21:39.492352Z","shell.execute_reply.started":"2023-10-13T17:21:39.486995Z","shell.execute_reply":"2023-10-13T17:21:39.491122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_samples = len(train_images)\nnum_validation_samples = int(test_size*num_samples)","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:22:34.443424Z","iopub.execute_input":"2023-10-13T17:22:34.443858Z","iopub.status.idle":"2023-10-13T17:22:34.449337Z","shell.execute_reply.started":"2023-10-13T17:22:34.443811Z","shell.execute_reply":"2023-10-13T17:22:34.448010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indices = np.arange(num_samples)\nnp.random.seed(random_seed)\nnp.random.shuffle(indices)","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:23:02.914351Z","iopub.execute_input":"2023-10-13T17:23:02.914752Z","iopub.status.idle":"2023-10-13T17:23:02.921369Z","shell.execute_reply.started":"2023-10-13T17:23:02.914721Z","shell.execute_reply":"2023-10-13T17:23:02.919565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_indices = indices[num_validation_samples:]\nval_indices = indices[:num_validation_samples]","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:23:11.815416Z","iopub.execute_input":"2023-10-13T17:23:11.815822Z","iopub.status.idle":"2023-10-13T17:23:11.821226Z","shell.execute_reply.started":"2023-10-13T17:23:11.815790Z","shell.execute_reply":"2023-10-13T17:23:11.820032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val = train_images[train_indices], train_images[val_indices]\ny_train, y_val = labels[train_indices], labels[val_indices]","metadata":{"execution":{"iopub.status.busy":"2023-10-13T18:08:31.144593Z","iopub.execute_input":"2023-10-13T18:08:31.145930Z","iopub.status.idle":"2023-10-13T18:08:31.337822Z","shell.execute_reply.started":"2023-10-13T18:08:31.145878Z","shell.execute_reply":"2023-10-13T18:08:31.336928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the Sequential model\nmodel = Sequential()\n\n# Add Convolutional layers\nmodel.add(Conv2D(32, (3, 3), activation='relu', input_shape=(128, 128, 3)))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Conv2D(64, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Conv2D(128, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D((2, 2)))\n\n# Flatten the feature maps for the fully connected layers\nmodel.add(Flatten())\n\n# Add fully connected layers\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dropout(0.5))  # Dropout layer for regularization\nmodel.add(Dense(64, activation='relu'))\nmodel.add(Dense(120, activation='softmax'))  # Adjust the output size for your specific dataset\n\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-10-13T18:05:06.699659Z","iopub.execute_input":"2023-10-13T18:05:06.700349Z","iopub.status.idle":"2023-10-13T18:05:06.843737Z","shell.execute_reply.started":"2023-10-13T18:05:06.700294Z","shell.execute_reply":"2023-10-13T18:05:06.842331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-10-13T18:05:10.874365Z","iopub.execute_input":"2023-10-13T18:05:10.875041Z","iopub.status.idle":"2023-10-13T18:05:10.888738Z","shell.execute_reply.started":"2023-10-13T18:05:10.875008Z","shell.execute_reply":"2023-10-13T18:05:10.887283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Summary of the model architecture\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-10-13T18:05:14.364072Z","iopub.execute_input":"2023-10-13T18:05:14.365058Z","iopub.status.idle":"2023-10-13T18:05:14.404536Z","shell.execute_reply.started":"2023-10-13T18:05:14.365023Z","shell.execute_reply":"2023-10-13T18:05:14.403226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32\nepochs = 20\n\nhistory = model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(X_val, y_val))\n","metadata":{"execution":{"iopub.status.busy":"2023-10-13T18:10:59.362572Z","iopub.execute_input":"2023-10-13T18:10:59.363138Z","iopub.status.idle":"2023-10-13T18:11:00.000642Z","shell.execute_reply.started":"2023-10-13T18:10:59.362992Z","shell.execute_reply":"2023-10-13T18:10:59.998905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test the prediction and print it ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#print(np.argmax(test_predictions[0]))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_breeds = [list(label_mapping.keys())[list(label_mapping.values()).index(np.argmax(prediction))] for prediction in test_predictions]\n\n# Create a DataFrame with image IDs and predicted breed labels\n----------------------------------------------------------------------\n\n# Save the submission file as a CSV\n----------------------------------------------------------------------","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#read the csv ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#print sub(head())\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}