{"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":"import math\nimport numpy as np # linear algebra\nimport pandas as pd \nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nimport tensorflow as tf\nfrom skimage.transform import resize\nfrom keras.applications import MobileNetV2\nfrom keras.layers import GlobalAveragePooling2D, Dense\nfrom keras.models import Model\nfrom keras.optimizers import Adam\nfrom keras.utils import to_categorical\nfrom tqdm import tqdm\n\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\nfrom keras.preprocessing import image\nimport matplotlib.pyplot as plt\n\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-01T03:13:52.551249Z","iopub.execute_input":"2023-08-01T03:13:52.551625Z","iopub.status.idle":"2023-08-01T03:14:22.048785Z","shell.execute_reply.started":"2023-08-01T03:13:52.551572Z","shell.execute_reply":"2023-08-01T03:14:22.047805Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reading Files","metadata":{}},{"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-08-01T03:14:22.050317Z","iopub.execute_input":"2023-08-01T03:14:22.051978Z","iopub.status.idle":"2023-08-01T03:14:22.057521Z","shell.execute_reply.started":"2023-08-01T03:14:22.051938Z","shell.execute_reply":"2023-08-01T03:14:22.056645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading Labels and Display of first 5 images","metadata":{}},{"cell_type":"code","source":"from matplotlib import image as mpimg\n# Load the labels\nlabels = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')","metadata":{"execution":{"iopub.status.busy":"2023-08-01T03:14:22.058551Z","iopub.execute_input":"2023-08-01T03:14:22.058925Z","iopub.status.idle":"2023-08-01T03:14:22.090495Z","shell.execute_reply.started":"2023-08-01T03:14:22.058893Z","shell.execute_reply":"2023-08-01T03:14:22.089616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training images","metadata":{}},{"cell_type":"code","source":"\n\n# Display the first 5 train images and their labels\nprint('Train Images:')\nfor i in range(5):\n    # Get the image filename and label\n    filename = labels.iloc[i]['id'] + '.jpg'\n    label = labels.iloc[i]['breed']\n    \n    # Load and display the image\n    img_path = os.path.join(train_dir, filename)\n    img = mpimg.imread(img_path)\n    plt.imshow(img)\n    plt.title(label)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-01T03:14:22.093272Z","iopub.execute_input":"2023-08-01T03:14:22.093645Z","iopub.status.idle":"2023-08-01T03:14:23.904407Z","shell.execute_reply.started":"2023-08-01T03:14:22.093611Z","shell.execute_reply":"2023-08-01T03:14:23.903493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test images","metadata":{}},{"cell_type":"code","source":"# Display the first 5 test images\nprint('Test Images:')\nfor i in range(5):\n    # Get the image filename\n    filename = os.listdir(test_dir)[i]\n    \n    # Load and display the image\n    img_path = os.path.join(test_dir, filename)\n    img = mpimg.imread(img_path)\n    plt.imshow(img)\n    plt.title(filename)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-01T03:14:23.905929Z","iopub.execute_input":"2023-08-01T03:14:23.906543Z","iopub.status.idle":"2023-08-01T03:14:25.844383Z","shell.execute_reply.started":"2023-08-01T03:14:23.906511Z","shell.execute_reply":"2023-08-01T03:14:25.843534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Sorting of Breeds","metadata":{}},{"cell_type":"code","source":"breeds = sorted(labels['breed'].unique())\nnum_classes = len(breeds)","metadata":{"execution":{"iopub.status.busy":"2023-08-01T03:14:25.845966Z","iopub.execute_input":"2023-08-01T03:14:25.846612Z","iopub.status.idle":"2023-08-01T03:14:25.854545Z","shell.execute_reply.started":"2023-08-01T03:14:25.846561Z","shell.execute_reply":"2023-08-01T03:14:25.853594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Mapping func","metadata":{}},{"cell_type":"code","source":"# Create a mapping from breed to integer label\nbreed_to_label = {breed: i for i, breed in enumerate(breeds)}","metadata":{"execution":{"iopub.status.busy":"2023-08-01T03:14:25.856075Z","iopub.execute_input":"2023-08-01T03:14:25.856718Z","iopub.status.idle":"2023-08-01T03:14:25.867262Z","shell.execute_reply.started":"2023-08-01T03:14:25.856685Z","shell.execute_reply":"2023-08-01T03:14:25.866255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Image Sizing","metadata":{}},{"cell_type":"code","source":"# Set the image size and batch size\nimg_size = 224\nbatch_size = 16","metadata":{"execution":{"iopub.status.busy":"2023-08-01T03:14:25.868756Z","iopub.execute_input":"2023-08-01T03:14:25.869282Z","iopub.status.idle":"2023-08-01T03:14:25.876331Z","shell.execute_reply.started":"2023-08-01T03:14:25.869248Z","shell.execute_reply":"2023-08-01T03:14:25.875119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocessing","metadata":{}},{"cell_type":"code","source":"def load_and_preprocess_image(img_path, label):\n    img = tf.io.read_file(img_path)\n    img = tf.image.decode_jpeg(img, channels=3)\n    img = tf.image.resize(img, [img_size, img_size])\n    img = tf.cast(img, tf.float32) / 255.0\n    \n    # One-hot encode the label\n    label = tf.one_hot(label, num_classes)\n    \n    return img, label\n","metadata":{"execution":{"iopub.status.busy":"2023-08-01T03:14:25.877987Z","iopub.execute_input":"2023-08-01T03:14:25.878431Z","iopub.status.idle":"2023-08-01T03:14:25.886755Z","shell.execute_reply.started":"2023-08-01T03:14:25.878399Z","shell.execute_reply":"2023-08-01T03:14:25.885831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_dataset(df, train_dir, batch_size):\n    filenames = df['id'].apply(lambda x: os.path.join(train_dir, f\"{x}.jpg\")).values\n    labels = df['breed'].map(breed_to_label).values\n    dataset = tf.data.Dataset.from_tensor_slices((filenames, labels))\n    dataset = dataset.map(load_and_preprocess_image, num_parallel_calls=tf.data.experimental.AUTOTUNE)\n    dataset = dataset.batch(batch_size).prefetch(buffer_size=tf.data.experimental.AUTOTUNE)\n    return dataset","metadata":{"execution":{"iopub.status.busy":"2023-08-01T03:14:25.890612Z","iopub.execute_input":"2023-08-01T03:14:25.891058Z","iopub.status.idle":"2023-08-01T03:14:25.899631Z","shell.execute_reply.started":"2023-08-01T03:14:25.891034Z","shell.execute_reply":"2023-08-01T03:14:25.898645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# MobileNetV2 architecture","metadata":{}},{"cell_type":"code","source":"base_model = MobileNetV2(weights='imagenet', include_top=False)","metadata":{"execution":{"iopub.status.busy":"2023-08-01T03:14:25.901266Z","iopub.execute_input":"2023-08-01T03:14:25.901654Z","iopub.status.idle":"2023-08-01T03:14:30.883635Z","shell.execute_reply.started":"2023-08-01T03:14:25.901622Z","shell.execute_reply":"2023-08-01T03:14:30.882635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Activation Func","metadata":{}},{"cell_type":"code","source":"# Add a global average pooling layer, followed by a dense layer with softmax activation\nx = base_model.output\nx = GlobalAveragePooling2D()(x)\npredictions = Dense(num_classes, activation='softmax')(x)\n\n# Create a new model with the added layers\nmodel = Model(inputs=base_model.input, outputs=predictions)","metadata":{"execution":{"iopub.status.busy":"2023-08-01T03:14:30.885076Z","iopub.execute_input":"2023-08-01T03:14:30.885412Z","iopub.status.idle":"2023-08-01T03:14:30.924608Z","shell.execute_reply.started":"2023-08-01T03:14:30.885380Z","shell.execute_reply":"2023-08-01T03:14:30.923247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modelling","metadata":{}},{"cell_type":"code","source":"# Freeze the base model layers\nfor layer in base_model.layers:\n    layer.trainable = False\n\n# Compile the model using the Adam optimizer and categorical crossentropy loss\nmodel.compile(optimizer=Adam(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-08-01T03:14:30.925973Z","iopub.execute_input":"2023-08-01T03:14:30.926412Z","iopub.status.idle":"2023-08-01T03:14:30.954442Z","shell.execute_reply.started":"2023-08-01T03:14:30.926379Z","shell.execute_reply":"2023-08-01T03:14:30.953539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Calculate the number of batches per epoch\nbatches_per_epoch = math.ceil(len(labels) / batch_size)\n\n\n# Train the model for 10 epochs using tf.data.Dataset\nfor epoch in range(10):\n    print('Epoch', epoch + 1)\n\n    # Create a progress bar object for this epoch\n    pbar = tqdm(total=batches_per_epoch)\n\n    # Shuffle the data\n    labels = labels.sample(frac=1).reset_index(drop=True)\n\n    # Load and preprocess the data using tf.data.Dataset\n    dataset = create_dataset(labels, train_dir, batch_size)\n    \n    # Train the model on the dataset\n    for x_batch, y_batch in dataset:\n        model.train_on_batch(x_batch, y_batch.numpy())  # Convert y_batch to numpy array\n\n        # Update the progress bar\n        pbar.update(1)\n    pbar.close()\n","metadata":{"execution":{"iopub.status.busy":"2023-08-01T03:14:30.955943Z","iopub.execute_input":"2023-08-01T03:14:30.956254Z","iopub.status.idle":"2023-08-01T03:20:34.879121Z","shell.execute_reply.started":"2023-08-01T03:14:30.956224Z","shell.execute_reply":"2023-08-01T03:20:34.877487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_filenames = os.listdir(test_dir)\n\n# Create a function to load and preprocess a test image\ndef load_and_preprocess_test_image(img_path):\n    img = tf.io.read_file(img_path)\n    img = tf.image.decode_jpeg(img, channels=3)\n    img = tf.image.resize(img, [img_size, img_size])\n    img = tf.cast(img, tf.float32) / 255.0\n    return img\n\n# Create a list to store the predictions\npredictions_list = []\n\n# Load and preprocess the test data and make predictions\nfor filename in tqdm(test_filenames):\n    img_path = os.path.join(test_dir, filename)\n    img = load_and_preprocess_test_image(img_path)\n    img = np.expand_dims(img, axis=0)  # Add batch dimension\n    prediction = model.predict(img)\n    predicted_label = breeds[np.argmax(prediction)]  # Convert prediction to label\n    predictions_list.append((filename.split('.')[0], predicted_label))\n\n# Create a DataFrame from the list of predictions\nsubmission_df = pd.DataFrame(predictions_list, columns=['id', 'breed'])\n\n# Save the DataFrame to a CSV file\nsubmission_df.to_csv('Dog_breed_Submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-08-01T03:24:54.638574Z","iopub.execute_input":"2023-08-01T03:24:54.638996Z","iopub.status.idle":"2023-08-01T03:38:25.058330Z","shell.execute_reply.started":"2023-08-01T03:24:54.638964Z","shell.execute_reply":"2023-08-01T03:38:25.057378Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]}]}