{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"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":27835,"databundleVersionId":2255017,"sourceType":"competition"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-02-15T23:23:02.554763Z","iopub.execute_input":"2024-02-15T23:23:02.555588Z","iopub.status.idle":"2024-02-15T23:23:22.611761Z","shell.execute_reply.started":"2024-02-15T23:23:02.555552Z","shell.execute_reply":"2024-02-15T23:23:22.610721Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"VALIDATION_PATH = '../input/dlai5-bad-apples/validation/validation'\nTEST_DIR_PATH = '../input/dlai5-bad-apples/test/test'\nTRAIN_DIR_PATH = '../input/dlai5-bad-apples/train/train'\nSAMPLE_SUBMISSION_FILE = '../input/dlai5-bad-apples/DLAI5_fruits_sample.csv'","metadata":{"execution":{"iopub.status.busy":"2024-02-15T23:23:25.764699Z","iopub.execute_input":"2024-02-15T23:23:25.765686Z","iopub.status.idle":"2024-02-15T23:23:25.770059Z","shell.execute_reply.started":"2024-02-15T23:23:25.765656Z","shell.execute_reply":"2024-02-15T23:23:25.768908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd","metadata":{"execution":{"iopub.status.busy":"2024-02-15T23:23:26.132403Z","iopub.execute_input":"2024-02-15T23:23:26.132758Z","iopub.status.idle":"2024-02-15T23:23:26.137353Z","shell.execute_reply.started":"2024-02-15T23:23:26.132723Z","shell.execute_reply":"2024-02-15T23:23:26.136276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-02-15T23:23:29.075269Z","iopub.execute_input":"2024-02-15T23:23:29.075922Z","iopub.status.idle":"2024-02-15T23:23:29.081058Z","shell.execute_reply.started":"2024-02-15T23:23:29.075885Z","shell.execute_reply":"2024-02-15T23:23:29.080068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\ntrain_gen = ImageDataGenerator(\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    rescale=1./255)\n\nvalid_gen = ImageDataGenerator(\n    rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2024-02-15T23:23:31.992964Z","iopub.execute_input":"2024-02-15T23:23:31.993301Z","iopub.status.idle":"2024-02-15T23:23:46.497853Z","shell.execute_reply.started":"2024-02-15T23:23:31.993275Z","shell.execute_reply":"2024-02-15T23:23:46.497004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_SIZE = 456","metadata":{"execution":{"iopub.status.busy":"2024-02-15T23:23:46.499408Z","iopub.execute_input":"2024-02-15T23:23:46.499978Z","iopub.status.idle":"2024-02-15T23:23:46.504554Z","shell.execute_reply.started":"2024-02-15T23:23:46.499948Z","shell.execute_reply":"2024-02-15T23:23:46.503501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = train_gen.flow_from_directory(\n    TRAIN_DIR_PATH,\n    target_size=(IMAGE_SIZE, IMAGE_SIZE),\n    batch_size=8,\n    class_mode='categorical'\n)\n\nvalid_generator = train_gen.flow_from_directory(\n    TEST_DIR_PATH,\n    target_size=(IMAGE_SIZE, IMAGE_SIZE),\n    batch_size=8,\n    class_mode='categorical'\n)","metadata":{"execution":{"iopub.status.busy":"2024-02-15T23:23:46.506129Z","iopub.execute_input":"2024-02-15T23:23:46.506927Z","iopub.status.idle":"2024-02-15T23:23:49.010524Z","shell.execute_reply.started":"2024-02-15T23:23:46.506893Z","shell.execute_reply":"2024-02-15T23:23:49.009529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications.efficientnet import EfficientNetB0","metadata":{"execution":{"iopub.status.busy":"2024-02-16T01:59:07.388754Z","iopub.execute_input":"2024-02-16T01:59:07.389648Z","iopub.status.idle":"2024-02-16T01:59:07.394279Z","shell.execute_reply.started":"2024-02-16T01:59:07.389592Z","shell.execute_reply":"2024-02-16T01:59:07.393147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model = EfficientNetB0(weights='imagenet', include_top=False, input_shape=(IMAGE_SIZE,IMAGE_SIZE, 3))\nbase_model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:48:38.266895Z","iopub.execute_input":"2024-02-16T02:48:38.267281Z","iopub.status.idle":"2024-02-16T02:48:41.798347Z","shell.execute_reply.started":"2024-02-16T02:48:38.267253Z","shell.execute_reply":"2024-02-16T02:48:41.797310Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\nfrom tensorflow.keras.layers import Conv2D\nfrom tensorflow.keras.regularizers import l2\n\nfor layer in tqdm(base_model.layers):\n    if isinstance(layer, Conv2D):\n        layer.kernel_regularizer = l2(0.01)  # Adjust the regularization strength as needed","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:48:50.917433Z","iopub.execute_input":"2024-02-16T02:48:50.918354Z","iopub.status.idle":"2024-02-16T02:48:50.931828Z","shell.execute_reply.started":"2024-02-16T02:48:50.918305Z","shell.execute_reply":"2024-02-16T02:48:50.930907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in base_model.layers[:160]:\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:48:54.241257Z","iopub.execute_input":"2024-02-16T02:48:54.241697Z","iopub.status.idle":"2024-02-16T02:48:54.253916Z","shell.execute_reply.started":"2024-02-16T02:48:54.241667Z","shell.execute_reply":"2024-02-16T02:48:54.252935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# len(base_model.layers)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:47:40.786250Z","iopub.execute_input":"2024-02-16T02:47:40.787114Z","iopub.status.idle":"2024-02-16T02:47:40.793211Z","shell.execute_reply.started":"2024-02-16T02:47:40.787067Z","shell.execute_reply":"2024-02-16T02:47:40.792158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.layers import Dense, Dropout, GlobalAveragePooling2D\nfrom keras.models import Model\n\nx = GlobalAveragePooling2D()(base_model.output)\n\nx = Dense(6, activation='softmax')(x)\n\nmodel = Model(inputs = base_model.input, outputs = x)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:49:04.613531Z","iopub.execute_input":"2024-02-16T02:49:04.613948Z","iopub.status.idle":"2024-02-16T02:49:04.657582Z","shell.execute_reply.started":"2024-02-16T02:49:04.613907Z","shell.execute_reply":"2024-02-16T02:49:04.656860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint\n\nreduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=5, min_lr=1e-6)\nearly_stopping = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)\nmodel_checkpoint = ModelCheckpoint('best_model.keras', monitor='val_loss', save_best_only=True)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:49:06.792473Z","iopub.execute_input":"2024-02-16T02:49:06.792972Z","iopub.status.idle":"2024-02-16T02:49:06.800871Z","shell.execute_reply.started":"2024-02-16T02:49:06.792931Z","shell.execute_reply":"2024-02-16T02:49:06.799109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\nhistory = model.fit(\n    train_generator,\n    steps_per_epoch=len(train_generator),\n    epochs=8,\n    validation_data=valid_generator,\n    validation_steps=len(valid_generator),\n    callbacks=[reduce_lr, early_stopping, model_checkpoint]\n)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:49:07.245169Z","iopub.execute_input":"2024-02-16T02:49:07.245942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate the model\nevaluation = model.evaluate(valid_generator, steps=len(valid_generator))\n\n# The evaluation result will be a list of metrics specified during model compilation\n# Example output: [loss, accuracy]\nprint(\"Loss:\", evaluation[0])\nprint(\"Accuracy:\", evaluation[1])","metadata":{"execution":{"iopub.status.busy":"2024-02-16T04:19:35.404819Z","iopub.execute_input":"2024-02-16T04:19:35.405223Z","iopub.status.idle":"2024-02-16T04:21:39.059619Z","shell.execute_reply.started":"2024-02-16T04:19:35.405194Z","shell.execute_reply":"2024-02-16T04:21:39.058490Z"},"trusted":true},"execution_count":null,"outputs":[]}]}