{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","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"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30839,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import rarfile \nimport os \nimport tensorflow as tf\nimport os\nimport numpy as np\nfrom tensorflow.keras.preprocessing import image\nfrom sklearn.preprocessing import LabelEncoder\nfrom tqdm import tqdm\nimport matplotlib.pyplot as pl\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout\nfrom tensorflow.keras.models import Model\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Flatten, Dense, Dropout, BatchNormalization\nfrom tensorflow.keras.applications import MobileNetV3Large, MobileNetV3Small\nfrom sklearn.model_selection import StratifiedKFold\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau\nfrom tensorflow.keras.regularizers import l2\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom sklearn.model_selection import StratifiedKFold\nimport numpy as np\nfrom tensorflow.keras.optimizers import Adam","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#train_path\ntrain_dataset_path = r\"D:\\ML\\Dogs vs Cats\\dataset\\train\"\n","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(train_dataset_path)\nprint(len(os.listdir(train_dataset_path)))\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def read_image_data(image_dir):\n \n    # Ensure image_dir is a valid string path\n    if not isinstance(image_dir, (str, bytes, os.PathLike)):\n        raise TypeError(f\"image_dir must be a string, bytes, or os.PathLike, not {type(image_dir)}\")\n\n    # Get all image file paths\n    image_paths = [os.path.join(image_dir, fname) for fname in os.listdir(image_dir) if fname.endswith(('.jpg', '.jpeg', '.png', '.bmp', '.gif'))]\n    \n    # Extract labels from filenames (assumes label is the first part of the filename, e.g., 'cat_1.jpg' -> 'cat')\n    labels = [fname.split('.')[0] for fname in os.listdir(image_dir) if fname.endswith(('.jpg', '.jpeg', '.png', '.bmp', '.gif'))]\n    \n    # Use LabelEncoder to encode string labels into integers\n    label_encoder = LabelEncoder()\n    encoded_labels = label_encoder.fit_transform(labels)  # Encode the labels to integers\n\n    return image_paths, encoded_labels, label_encoder\n\n\nimage_paths,labels,label_encoder = read_image_data(train_dataset_path)","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def make_dataset(image_paths, labels, batch_size=32, shuffle=True, img_size=(224, 224)):\n    # Create dataset from slices of paths and labels\n    dataset = tf.data.Dataset.from_tensor_slices((image_paths, labels))\n    \n    # Shuffle the dataset at the path level to improve efficiency\n    dataset = dataset.shuffle(buffer_size=len(image_paths), reshuffle_each_iteration=False)\n    \n    def load_and_preprocess_image(path, label):\n        # Read and decode image\n        image_bytes = tf.io.read_file(path)\n        image = tf.image.decode_image(image_bytes, channels=3, expand_animations=False)\n        # Resize and normalize\n        image = tf.image.resize(image, img_size)\n        image = image / 255.0\n        return image, label\n    \n    # Map preprocessing function across the dataset\n    dataset = dataset.map(load_and_preprocess_image, num_parallel_calls=tf.data.AUTOTUNE)\n    \n    # Batch and prefetch\n    dataset = dataset.batch(batch_size).prefetch(tf.data.AUTOTUNE)\n    \n    return dataset\n\n# Create the TensorFlow dataset\nbatch_size = 32\ntrain_dataset = make_dataset(image_paths, labels, batch_size=batch_size)","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\ndef visualize_samples(dataset, label_encoder, num_samples=10):\n\n    # Get a batch of images and labels\n    for images, labels in dataset.take(1):\n        images = images.numpy()\n        labels = labels.numpy()\n    \n    # Decode integer labels to original class names\n    class_names = label_encoder.inverse_transform(labels)\n    \n    # Setup plot dimensions\n    plt.figure(figsize=(15, 8))\n    num_cols = 5\n    num_rows = int(np.ceil(num_samples / num_cols))\n    \n    for i in range(num_samples):\n        plt.subplot(num_rows, num_cols, i+1)\n        plt.imshow(images[i])\n        plt.title(class_names[i])\n        plt.axis('off')\n    \n    plt.tight_layout()\n    plt.show()\n\n# Usage example\nvisualize_samples(train_dataset, label_encoder, num_samples=15)\n","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Split data into training and validation sets\nX_train, X_val, y_train, y_val = train_test_split(\n    image_paths, labels, test_size=0.2, random_state=42\n)\n","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.applications import MobileNetV3Large, MobileNetV3Small\n\n\ndef create_mobilenetv3_model(input_shape, num_classes, model_type='large'):\n    # Load the MobileNetV3 model with pre-trained weights from ImageNet\n    if model_type == 'large':\n        base_model = MobileNetV3Large(weights='imagenet', include_top=False, input_shape=input_shape, include_preprocessing=False)\n    elif model_type == 'small':\n        base_model = MobileNetV3Small(weights='imagenet', include_top=False, input_shape=input_shape, include_preprocessing=False)\n    else:\n        raise ValueError(\"model_type must be 'large' or 'small'\")\n\n    # Freeze the base_model\n    base_model.trainable = False\n\n    x = base_model.output\n    x = GlobalAveragePooling2D()(x)  # Use GlobalAveragePooling2D instead of Flatten for efficiency\n\n    # Add Dense layers with L2 regularization\n    x = Dense(512, activation='relu', kernel_regularizer=l2(0.001))(x)\n    x = BatchNormalization()(x)\n    x = Dropout(0.5)(x)\n\n    x = Dense(256, activation='relu', kernel_regularizer=l2(0.001))(x)\n    x = BatchNormalization()(x)\n    x = Dropout(0.5)(x)\n\n    predictions = Dense(num_classes, activation='softmax')(x)\n\n    # Construct the final model\n    model = Model(inputs=base_model.input, outputs=predictions)\n\n    return model\n","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.optimizers import Adam\n\n# Convert image_paths and labels to NumPy arrays\nimage_paths = np.array(image_paths)\nlabels = np.array(labels)\n\n# Single train-validation split instead of cross-validation\nX_train, X_val, y_train, y_val = train_test_split(\n    image_paths,\n    labels,\n    test_size=0.2,  # 20% validation split\n    stratify=labels,  # Maintain class distribution\n    random_state=42\n)\n\ninput_shape = (224, 224, 3)  # Your image dimensions\n\n# Define callbacks\nearly_stopping = EarlyStopping(\n    monitor=\"val_loss\",\n    patience=5,\n    restore_best_weights=True,\n)\n\n# Create TensorFlow datasets\ntrain_dataset = make_dataset(X_train, y_train, batch_size=batch_size, shuffle=True)\nval_dataset = make_dataset(X_val, y_val, batch_size=batch_size, shuffle=False)\n\n# Create and compile model\nmodel = create_mobilenetv3_model(input_shape, 2)\n\n# Set learning rate to 0.001 in the optimizer\noptimizer = Adam(learning_rate=0.001)  # Fixed learning rate\n\nmodel.compile(\n    optimizer=optimizer,\n    loss='sparse_categorical_crossentropy',\n    metrics=['accuracy']\n)\n\n# Train the model\nhistory = model.fit(\n    train_dataset,\n    epochs=20,\n    validation_data=val_dataset,\n    callbacks=[early_stopping],  # Only early stopping callback\n    verbose=1\n)\n\n# Save history after training\nnp.save('training_history.npy', history.history)\n\n# Evaluate on validation data\nval_loss, val_accuracy = model.evaluate(val_dataset, verbose=0)\nprint(f\"Final Validation Loss = {val_loss:.4f}, Validation Accuracy = {val_accuracy:.4f}\")","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for images, labels in val_dataset.take(1):\n    predictions = model.predict(images)\n    predicted_labels = np.argmax(predictions, axis=1)\n    print(\"True labels:\", labels)\n    print(\"Predicted labels:\", predicted_labels)","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def plot_training_history(history):\n    \"\"\"\n    Plot training and validation accuracy and loss.\n\n    Args:\n        history: Keras history object returned by model.fit().\n    \"\"\"\n    plt.figure(figsize=(12, 6))\n\n    # Plot accuracy\n    plt.subplot(1, 2, 1)\n    plt.plot(history.history['accuracy'], label='Training Accuracy')\n    plt.plot(history.history['val_accuracy'], label='Validation Accuracy')\n    plt.title('Training and Validation Accuracy')\n    plt.xlabel('Epochs')\n    plt.ylabel('Accuracy')\n    plt.legend()\n\n    # Plot loss\n    plt.subplot(1, 2, 2)\n    plt.plot(history.history['loss'], label='Training Loss')\n    plt.plot(history.history['val_loss'], label='Validation Loss')\n    plt.title('Training and Validation Loss')\n    plt.xlabel('Epochs')\n    plt.ylabel('Loss')\n    plt.legend()\n\n    plt.tight_layout()\n    plt.show()\n    \n    \nplot_training_history(history)","metadata":{},"outputs":[],"execution_count":null}]}