{"metadata":{"kernelspec":{"display_name":"Python 3 (ipykernel)","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.10.9"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":7327,"databundleVersionId":861871,"sourceType":"competition"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport tensorflow as tf\nfrom tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.model_selection import train_test_split\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Seed for reproducibility\nnp.random.seed(42)\ntf.random.set_seed(42)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load labels\nlabels_df = pd.read_csv(\"D:/year 3/semester 2/Deep/dog-breed-identification/labels.csv\")\ndog_breeds = sorted(list(set(labels_df['breed'])))\nn_classes = len(dog_breeds)\nclass_to_num = dict(zip(dog_breeds, range(n_classes)))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Directories\ntrain_dir = 'D:/year 3/semester 2/Deep/dog-breed-identification/train/'\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(128, 128, 3))\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Add new layers with regularization and dropout\nx = GlobalAveragePooling2D()(base_model.output)\nx = Dense(1024, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.01))(x)\nx = Dropout(0.5)(x)\npredictions = Dense(n_classes, activation='softmax')(x)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Final model\nmodel = Model(inputs=base_model.input, outputs=predictions)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Freeze base model layers\nfor layer in base_model.layers:\n    layer.trainable = False\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = Adam(learning_rate=0.0001)\nmodel.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Enhanced Data augmentation\ntrain_datagen = ImageDataGenerator(\n    rotation_range=40,\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    vertical_flip=True,  # added vertical flip\n    fill_mode='nearest',\n    preprocessing_function=preprocess_input\n)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prepare data generators\ndef prepare_images(data, batch_size):\n    datagen = ImageDataGenerator(preprocessing_function=preprocess_input)\n    generator = datagen.flow_from_dataframe(\n        dataframe=data,\n        directory=train_dir,\n        x_col='id',\n        y_col='breed',\n        target_size=(128, 128),\n        batch_size=batch_size,\n        class_mode='categorical',\n        shuffle=True\n    )\n    return generator","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split data\nlabels_df['id'] = labels_df['id'].apply(lambda x: x + '.jpg')\ntrain_data, val_data = train_test_split(labels_df, test_size=0.2, random_state=42)\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create generators\ntrain_generator = prepare_images(train_data, 64)\nval_generator = prepare_images(val_data, 32)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Callbacks\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau\n\nearly_stop = EarlyStopping(monitor='val_loss', patience=10, verbose=1, restore_best_weights=True)\nmodel_checkpoint = ModelCheckpoint(\n    'best_model.h5', monitor='val_loss', save_best_only=True, verbose=1)\nreduce_lr = ReduceLROnPlateau(\n    monitor='val_loss', factor=0.2, patience=10, verbose=1, min_lr=1e-6)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train model\nhistory = model.fit(\n    train_generator,\n    steps_per_epoch=len(train_generator),\n    epochs=25,\n    validation_data=val_generator,\n    validation_steps=len(val_generator),\n    callbacks=[early_stop, model_checkpoint, reduce_lr]  # Include new callback here\n)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\nepochs = range(1, len(acc) + 1)\n\n# Accuracy plot\nplt.figure(figsize=(12, 6))\nplt.subplot(1, 2, 1)\nplt.plot(epochs, acc, 'bo', label='Training acc')\nplt.plot(epochs, val_acc, 'b', label='Validation acc')\nplt.title('Training and Validation Accuracy')\nplt.legend()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.applications.resnet50 import preprocess_input\nimport numpy as np\nimport os\nimport random","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_and_preprocess_image(img_path):\n    img = image.load_img(img_path, target_size=(128, 128))\n    img_tensor = image.img_to_array(img)  # Convert the image to numpy array\n    img_tensor = np.expand_dims(img_tensor, axis=0)  # Add batch dimension\n    img_tensor = preprocess_input(img_tensor)  # Preprocess the image for ResNet50\n    return img_tensor","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"specific_img_path = 'D:/year 3/semester 2/Deep/Chow-chow.jpg'","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Choose a random image from the training directory\nimg_path = os.path.join(train_dir, random.choice(os.listdir(train_dir)))\n\n# Load and preprocess the image\nimg_tensor = load_and_preprocess_image(specific_img_path)\n\n# Make prediction\npredictions = model.predict(img_tensor)\nlabel_index = np.argmax(predictions[0])\n\n# Create a dictionary to map class indices to class labels\nnum_to_class = {v: k for k, v in class_to_num.items()}\npredicted_label = num_to_class[label_index]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display the results\nprint(\"Predicted label: \", predicted_label)\nplt.imshow(image.load_img(specific_img_path))  # Load and display the image with matplotlib\nplt.title(predicted_label)\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}