{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":70497,"databundleVersionId":7713244,"sourceType":"competition"},{"sourceId":8344310,"sourceType":"datasetVersion","datasetId":4956531}],"dockerImageVersionId":30699,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install rembg","metadata":{"execution":{"iopub.status.busy":"2024-05-07T09:16:39.219497Z","iopub.execute_input":"2024-05-07T09:16:39.219818Z","iopub.status.idle":"2024-05-07T09:16:55.697331Z","shell.execute_reply.started":"2024-05-07T09:16:39.219795Z","shell.execute_reply":"2024-05-07T09:16:55.696179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom pathlib import Path\nimport numpy as np\nimport pandas as pd\nfrom keras.callbacks import LearningRateScheduler\nimport matplotlib.pyplot as plt\nfrom keras import backend as K \nfrom zipfile import ZipFile\nfrom os import listdir, walk\nfrom os.path import isfile, join\nfrom tensorflow.keras.layers import Dropout\n\nimport tensorflow as tf\nfrom tensorflow.keras import Input, Model\nfrom tensorflow.keras.applications import ResNet101V2, Xception, InceptionV3, MobileNetV2\nfrom tensorflow.keras.applications.imagenet_utils import preprocess_input\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, Dense\nfrom tensorflow.keras.optimizers import Adam, SGD\nfrom tensorflow.keras import layers\nfrom sklearn.utils.class_weight import compute_class_weight\nimport os \nK.clear_session()\n# Set seeds for reproducibility\nos.environ[\"CUDA_VISIBLE_DEVICES\"] = '0' \nnp.random.seed(42)\ntf.random.set_seed(42)","metadata":{"execution":{"iopub.status.busy":"2024-05-07T09:04:28.069409Z","iopub.execute_input":"2024-05-07T09:04:28.069845Z","iopub.status.idle":"2024-05-07T09:04:42.340556Z","shell.execute_reply.started":"2024-05-07T09:04:28.069802Z","shell.execute_reply":"2024-05-07T09:04:42.339715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_SIZE = 224\n\n# datagen = ImageDataGenerator(\n#         rescale=1/255.,\n#         horizontal_flip=True,\n#         vertical_flip=True,\n#         samplewise_center=True,\n#         )\n\ndatagen = ImageDataGenerator(\n        rescale=1/255.,\n        horizontal_flip=True,\n        vertical_flip=True,\n        samplewise_center=True,\n        zoom_range=0.1,\n        width_shift_range=0.1,\n        height_shift_range=0.1,\n        rotation_range=10,\n        )","metadata":{"execution":{"iopub.status.busy":"2024-05-07T09:04:42.342463Z","iopub.execute_input":"2024-05-07T09:04:42.343115Z","iopub.status.idle":"2024-05-07T09:04:42.349036Z","shell.execute_reply.started":"2024-05-07T09:04:42.34308Z","shell.execute_reply":"2024-05-07T09:04:42.347947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_it = datagen.flow_from_directory('/kaggle/input/rotten-and-fresh-mixed/rotten-and-fresh-mixed/train/train', \n                                       target_size=(IMG_SIZE, IMG_SIZE), \n                                       color_mode='rgb', \n                                       class_mode=\"categorical\",\n                                       shuffle=True,\n                                       seed=42)","metadata":{"execution":{"iopub.status.busy":"2024-05-07T09:05:11.442971Z","iopub.execute_input":"2024-05-07T09:05:11.443333Z","iopub.status.idle":"2024-05-07T09:05:14.990928Z","shell.execute_reply.started":"2024-05-07T09:05:11.443305Z","shell.execute_reply":"2024-05-07T09:05:14.989976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_counts = train_it.classes\nunique_classes = np.unique(class_counts)\nclass_frequency = {class_name: np.sum(class_counts == class_idx) for class_name, class_idx in train_it.class_indices.items()}\n\nprint(\"Class frequency:\")\nfor class_name, count in class_frequency.items():\n    print(f\"{class_name}: {count} items\")","metadata":{"execution":{"iopub.status.busy":"2024-05-07T09:05:16.426612Z","iopub.execute_input":"2024-05-07T09:05:16.427537Z","iopub.status.idle":"2024-05-07T09:05:16.437149Z","shell.execute_reply.started":"2024-05-07T09:05:16.427496Z","shell.execute_reply":"2024-05-07T09:05:16.436211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_it = datagen.flow_from_directory('/kaggle/input/rotten-and-fresh-mixed/rotten-and-fresh-mixed/test/test', \n                                       target_size=(IMG_SIZE, IMG_SIZE), \n                                       color_mode='rgb', \n                                       class_mode=\"categorical\",\n                                       shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2024-05-07T09:05:21.163197Z","iopub.execute_input":"2024-05-07T09:05:21.16386Z","iopub.status.idle":"2024-05-07T09:05:22.408006Z","shell.execute_reply.started":"2024-05-07T09:05:21.163829Z","shell.execute_reply":"2024-05-07T09:05:22.407221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_counts = valid_it.classes\nunique_classes = np.unique(class_counts)\nclass_frequency = {class_name: np.sum(class_counts == class_idx) for class_name, class_idx in train_it.class_indices.items()}\n\nprint(\"Class frequency:\")\nfor class_name, count in class_frequency.items():\n    print(f\"{class_name}: {count} items\")","metadata":{"execution":{"iopub.status.busy":"2024-05-07T09:05:24.167402Z","iopub.execute_input":"2024-05-07T09:05:24.168533Z","iopub.status.idle":"2024-05-07T09:05:24.176505Z","shell.execute_reply.started":"2024-05-07T09:05:24.168493Z","shell.execute_reply":"2024-05-07T09:05:24.175513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get a batch of images and labels from the train and test datasets\ntrain_images, train_labels = next(train_it)\ntest_images, test_labels = next(valid_it)","metadata":{"execution":{"iopub.status.busy":"2024-05-07T09:05:26.073723Z","iopub.execute_input":"2024-05-07T09:05:26.074049Z","iopub.status.idle":"2024-05-07T09:05:27.290409Z","shell.execute_reply.started":"2024-05-07T09:05:26.074023Z","shell.execute_reply":"2024-05-07T09:05:27.289594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model = ResNet101V2(\n    weights='imagenet',\n    input_shape=(IMG_SIZE, IMG_SIZE, 3),\n    include_top=False)\n\nbase_model.trainable = False","metadata":{"execution":{"iopub.status.busy":"2024-05-07T09:05:27.291847Z","iopub.execute_input":"2024-05-07T09:05:27.292139Z","iopub.status.idle":"2024-05-07T09:05:29.222149Z","shell.execute_reply.started":"2024-05-07T09:05:27.292113Z","shell.execute_reply":"2024-05-07T09:05:29.221412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the input shape for the model, assuming IMG_SIZE x IMG_SIZE images with 3 color channels (RGB)\ninputs = Input(shape=(IMG_SIZE, IMG_SIZE, 3))\n\n# Connect the input to a pre-trained base model, with training set to False to freeze the base model's weights\nx = base_model(inputs, training=False)\n\n# Apply Global Average Pooling to reduce spatial dimensions and extract important features\nx = GlobalAveragePooling2D()(x)\n\n# Connect the output of the Global Average Pooling layer to a Dense layer with softmax activation\n# The number of units in the Dense layer is set to the number of classes in the training dataset\noutputs = Dense(len(train_it.class_indices), activation='softmax')(x)\n\nmodel = Model(inputs, outputs)","metadata":{"execution":{"iopub.status.busy":"2024-05-07T09:05:33.8124Z","iopub.execute_input":"2024-05-07T09:05:33.813255Z","iopub.status.idle":"2024-05-07T09:05:33.835011Z","shell.execute_reply.started":"2024-05-07T09:05:33.813216Z","shell.execute_reply":"2024-05-07T09:05:33.834092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 10\nopt = tf.keras.optimizers.SGD(learning_rate=0.0001, decay=0.01 / 40, momentum=0.9, nesterov=True)\n# model.load_weights('main_weights.h5')\nmodel.compile(loss='categorical_crossentropy', optimizer=opt, metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2024-05-07T09:10:29.121636Z","iopub.execute_input":"2024-05-07T09:10:29.122001Z","iopub.status.idle":"2024-05-07T09:10:29.133294Z","shell.execute_reply.started":"2024-05-07T09:10:29.12197Z","shell.execute_reply":"2024-05-07T09:10:29.13225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    train_it,\n    epochs=epochs,\n    validation_data=valid_it,\n)","metadata":{"execution":{"iopub.status.busy":"2024-05-07T09:10:29.457809Z","iopub.execute_input":"2024-05-07T09:10:29.458516Z","iopub.status.idle":"2024-05-07T09:14:27.814666Z","shell.execute_reply.started":"2024-05-07T09:10:29.458486Z","shell.execute_reply":"2024-05-07T09:14:27.813703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights('/kaggle/working/Allfruits-ResNet101V2.weights.h5')","metadata":{"execution":{"iopub.status.busy":"2024-05-07T09:14:31.911668Z","iopub.execute_input":"2024-05-07T09:14:31.91202Z","iopub.status.idle":"2024-05-07T09:14:32.170072Z","shell.execute_reply.started":"2024-05-07T09:14:31.911991Z","shell.execute_reply":"2024-05-07T09:14:32.16911Z"},"trusted":true},"execution_count":null,"outputs":[]}]}