{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":7327,"databundleVersionId":861871,"sourceType":"competition"}],"dockerImageVersionId":30804,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport cv2\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.utils import to_categorical\n\n# Path to the input data\ndata_dir = '/kaggle/input/dog-breed-identification/'\ntrain_dir = os.path.join(data_dir, 'train')\ntest_dir = os.path.join(data_dir, 'test')\n\n# # List files in the directory\n# for dirname, _, filenames in os.walk(data_dir):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# Load labels and sample submission CSV\nlabels_df = pd.read_csv(os.path.join(data_dir, 'labels.csv'))\nbreed_names = labels_df['breed'].unique()\nprint(f\"Number of unique breeds: {len(breed_names)}\")\nsubmission_df = pd.read_csv(os.path.join(data_dir, 'sample_submission.csv'))\n\n# Map each breed to a number\nbreed_to_id = {breed: idx for idx, breed in enumerate(breed_names)}\nlabels_df['breed_id'] = labels_df['breed'].map(breed_to_id)\n\nprint(\"Labels preview:\")\nprint(labels_df.head())\n\nprint(f'\\n\\n')\n\nprint(\"\\nSample Submission preview:\")\n# Load sample submission CSV\nprint(submission_df.head())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T20:39:14.117949Z","iopub.execute_input":"2024-12-18T20:39:14.118751Z","iopub.status.idle":"2024-12-18T20:39:14.509730Z","shell.execute_reply.started":"2024-12-18T20:39:14.118703Z","shell.execute_reply":"2024-12-18T20:39:14.508683Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Explore image folders\ntrain_images = os.listdir(os.path.join(data_dir, 'train'))\ntest_images = os.listdir(os.path.join(data_dir, 'test'))\n\nprint(f\"\\nNumber of training images: {len(train_images)}\")\nprint(f\"Number of test images: {len(test_images)}\")\n\n# Example image file name\nprint(f\"Example train image: {train_images[0]}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T20:39:14.511778Z","iopub.execute_input":"2024-12-18T20:39:14.512236Z","iopub.status.idle":"2024-12-18T20:39:14.526816Z","shell.execute_reply.started":"2024-12-18T20:39:14.512186Z","shell.execute_reply":"2024-12-18T20:39:14.525798Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom PIL import Image\n\n# Show a few sample training images\nfig, axes = plt.subplots(1, 5, figsize=(20, 5))\n\nfor i, img_name in enumerate(train_images[:5]):\n    img_path = os.path.join(data_dir, 'train', img_name)\n    img = Image.open(img_path)\n    axes[i].imshow(img)\n    axes[i].axis('off')\n    axes[i].set_title(f\"Image: {img_name}\")\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T20:39:14.528058Z","iopub.execute_input":"2024-12-18T20:39:14.528430Z","iopub.status.idle":"2024-12-18T20:39:15.286099Z","shell.execute_reply.started":"2024-12-18T20:39:14.528398Z","shell.execute_reply":"2024-12-18T20:39:15.284947Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import seaborn as sns\n\n# Check the distribution of breeds\nprint(f\"Number of unique breeds: {labels_df['breed'].nunique()}\")\n\n# Plot the top 10 most common breeds\nplt.figure(figsize=(12, 6))\nsns.countplot(y=labels_df['breed'], order=labels_df['breed'].value_counts().index[:10])\nplt.title(\"Top 10 Most Common Dog Breeds in Training Data\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T20:39:15.288569Z","iopub.execute_input":"2024-12-18T20:39:15.289331Z","iopub.status.idle":"2024-12-18T20:39:15.610096Z","shell.execute_reply.started":"2024-12-18T20:39:15.289279Z","shell.execute_reply":"2024-12-18T20:39:15.609016Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"/\\ semi balanceado","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nIMG_SIZE = 224  # CNN input size\nBATCH_SIZE = 16  # Adjust batch size based on memory availability\n\n# Initialize the ImageDataGenerator with augmentation\ndatagen = ImageDataGenerator(\n    rescale=1.0 / 255,  # Normalize images to [0, 1]\n    rotation_range=20,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    validation_split=0.3  # Reserve 30% for validation\n)\n\nlabels_df['id'] = labels_df['id'] + '.jpg'\n\n# Training data generator\ntrain_generator = datagen.flow_from_dataframe(\n    dataframe=labels_df,\n    directory=train_dir,\n    x_col='id',\n    y_col='breed',\n    target_size=(IMG_SIZE, IMG_SIZE),\n    batch_size=BATCH_SIZE,\n    class_mode='categorical',\n    subset='training'\n)\n\n# Validation data generator\nval_generator = datagen.flow_from_dataframe(\n    dataframe=labels_df,\n    directory=train_dir,\n    x_col='id',\n    y_col='breed',\n    target_size=(IMG_SIZE, IMG_SIZE),\n    batch_size=BATCH_SIZE,\n    class_mode='categorical',\n    subset='validation'\n)\n\nprint(f\"Training data batches: {len(train_generator)}\")\nprint(f\"Validation data batches: {len(val_generator)}\")\n\nprint(train_dir)  # Should output the full path to the 'train' folder\nprint(os.listdir(train_dir)[:5])  # Should show filenames in the 'train' directory\n\nprint(labels_df['id'].head())  # Check the first few IDs\nprint(labels_df['id'].str.endswith('.jpg').all())  # Verify if IDs include '.jpg'\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T20:39:15.611489Z","iopub.execute_input":"2024-12-18T20:39:15.611803Z","iopub.status.idle":"2024-12-18T20:39:20.709575Z","shell.execute_reply.started":"2024-12-18T20:39:15.611772Z","shell.execute_reply":"2024-12-18T20:39:20.708527Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"----\n----\n----\n----\nCNN","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\nfrom tensorflow.keras.layers import Input\n\n# Build a CNN with adjusted filters and layers\nmodel = Sequential([\n    Input(shape=(IMG_SIZE, IMG_SIZE, 3)),\n    # Conv2D(256, (3, 3), activation='relu'),  # Reduced filter size\n    # MaxPooling2D((2, 2)),\n\n    Conv2D(256, (3, 3), activation='relu'),  # Further reduced filter size\n    MaxPooling2D((2, 2)),\n    \n    Conv2D(128, (3, 3), activation='relu'),  # Further reduced filter size\n    MaxPooling2D((2, 2)),\n\n    Conv2D(64, (3, 3), activation='relu'),  # Further reduced filter size\n    MaxPooling2D((2, 2)),\n\n    Flatten(),\n    Dense(128, activation='relu'),\n    Dropout(0.5),  # Prevent overfitting\n    Dense(len(breed_names), activation='softmax')  # Output layer for classification\n])\n\n# Compile the model\nmodel.compile(optimizer='adam', \n              loss='categorical_crossentropy', \n              metrics=['accuracy'])\n\nmodel.summary()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T20:39:20.710770Z","iopub.execute_input":"2024-12-18T20:39:20.711111Z","iopub.status.idle":"2024-12-18T20:39:20.792798Z","shell.execute_reply.started":"2024-12-18T20:39:20.711081Z","shell.execute_reply":"2024-12-18T20:39:20.791988Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train the model\nhistory = model.fit(\n    train_generator,\n    validation_data=val_generator,\n    epochs=10,  # Start with 10 epochs\n    verbose=2\n)\n\n# Plot training and validation accuracy and loss\nimport matplotlib.pyplot as plt\n\n# Accuracy plot\nplt.figure(figsize=(12, 5))\nplt.plot(history.history['accuracy'], label='Training Accuracy', color='blue')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy', color='orange')\nplt.legend()\nplt.title(\"Training and Validation Accuracy\")\nplt.xlabel(\"Epochs\")\nplt.ylabel(\"Accuracy\")\nplt.grid(True)\nplt.show()\n\n# Loss plot\nplt.figure(figsize=(12, 5))\nplt.plot(history.history['loss'], label='Training Loss', color='blue')\nplt.plot(history.history['val_loss'], label='Validation Loss', color='orange')\nplt.legend()\nplt.title(\"Training and Validation Loss\")\nplt.xlabel(\"Epochs\")\nplt.ylabel(\"Loss\")\nplt.grid(True)\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T20:39:20.793911Z","iopub.execute_input":"2024-12-18T20:39:20.794185Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Test Data Generator\ntest_datagen = ImageDataGenerator(rescale=1.0 / 255)\n\nlabels_df['id'] = labels_df['id'] + '.jpg'\n\n# Test data generator\ntest_generator = test_datagen.flow_from_dataframe(\n    dataframe=submission_df,\n    directory=test_dir,\n    x_col='id',\n    y_col=None,  # No labels for test data\n    target_size=(IMG_SIZE, IMG_SIZE),\n    batch_size=BATCH_SIZE,\n    class_mode=None,  # No labels for test\n    shuffle=False  # Preserve order for submission\n)\n\n# Predict on test data\npredictions = model.predict(test_generator)\n\n# Convert predictions back to breed names\npredicted_breed_ids = np.argmax(predictions, axis=1)\npredicted_breeds = [breed_names[idx] for idx in predicted_breed_ids]\n\n# Prepare submission file\nsubmission_df = pd.DataFrame({'id': test_generator.filenames})\nsubmission_df['breed'] = predicted_breeds\nsubmission_df.to_csv('submission.csv', index=False)\nprint(\"Submission file saved!\")","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}