{"metadata":{"kernelspec":{"display_name":"Python 3","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.11.11"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":8078,"databundleVersionId":862231,"sourceType":"competition"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true},"papermill":{"default_parameters":{},"duration":13056.963137,"end_time":"2025-04-28T12:28:41.411828","environment_variables":{},"exception":true,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2025-04-28T08:51:04.448691","version":"2.3.3"}},"nbformat_minor":4,"nbformat":4,"cells":[{"id":"d404da7c","cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport cv2\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nfrom PIL import Image\nimport albumentations as A\n\nfrom sklearn.metrics import f1_score, classification_report\nimport pickle\nfrom sklearn.model_selection import StratifiedKFold\nfrom sklearn.preprocessing import OneHotEncoder, LabelEncoder\nfrom numpy import array\nfrom random import shuffle, seed","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2025-04-28T08:51:08.509803Z","iopub.status.busy":"2025-04-28T08:51:08.509121Z","iopub.status.idle":"2025-04-28T08:51:28.687657Z","shell.execute_reply":"2025-04-28T08:51:28.687052Z"},"id":"dcO2IxLbiqQY","papermill":{"duration":20.188489,"end_time":"2025-04-28T08:51:28.688847","exception":false,"start_time":"2025-04-28T08:51:08.500358","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"caa6cb51","cell_type":"code","source":"list_paths = []\nfor subdir, dirs, files in os.walk(\"../input\"):\n    for file in files:\n        filepath = subdir + os.sep + file\n        list_paths.append(filepath)\n        \nlist_train = [filepath for filepath in list_paths if \"train/\" in filepath]\nseed(420)\nshuffle(list_train)\nlist_test = [filepath for filepath in list_paths if \"test/\" in filepath]","metadata":{"execution":{"iopub.execute_input":"2025-04-28T08:51:28.703058Z","iopub.status.busy":"2025-04-28T08:51:28.702623Z","iopub.status.idle":"2025-04-28T08:51:42.910254Z","shell.execute_reply":"2025-04-28T08:51:42.909469Z"},"id":"sBo5sya-iqQc","papermill":{"duration":14.215925,"end_time":"2025-04-28T08:51:42.911705","exception":false,"start_time":"2025-04-28T08:51:28.69578","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"0fe537e6","cell_type":"code","source":"list_train[:5]","metadata":{"execution":{"iopub.execute_input":"2025-04-28T08:51:42.925776Z","iopub.status.busy":"2025-04-28T08:51:42.925001Z","iopub.status.idle":"2025-04-28T08:51:42.930249Z","shell.execute_reply":"2025-04-28T08:51:42.929695Z"},"id":"jBtM1tHSiqQd","outputId":"a065480b-8b49-46da-fca4-0634b3b4dfcf","papermill":{"duration":0.01293,"end_time":"2025-04-28T08:51:42.931195","exception":false,"start_time":"2025-04-28T08:51:42.918265","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"8645bb6f","cell_type":"code","source":"def get_class_from_path(filepath):\n    return os.path.dirname(filepath).split(os.sep)[-1]","metadata":{"execution":{"iopub.execute_input":"2025-04-28T08:51:42.944669Z","iopub.status.busy":"2025-04-28T08:51:42.94427Z","iopub.status.idle":"2025-04-28T08:51:42.947892Z","shell.execute_reply":"2025-04-28T08:51:42.947367Z"},"id":"6oa-5IQtiqQf","papermill":{"duration":0.011389,"end_time":"2025-04-28T08:51:42.948846","exception":false,"start_time":"2025-04-28T08:51:42.937457","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"cdc60065","cell_type":"code","source":"labels = [get_class_from_path(filepath) for filepath in list_train]","metadata":{"execution":{"iopub.execute_input":"2025-04-28T08:51:42.962099Z","iopub.status.busy":"2025-04-28T08:51:42.961869Z","iopub.status.idle":"2025-04-28T08:51:42.967527Z","shell.execute_reply":"2025-04-28T08:51:42.966992Z"},"id":"ThwZsJumiqQf","papermill":{"duration":0.013505,"end_time":"2025-04-28T08:51:42.968596","exception":false,"start_time":"2025-04-28T08:51:42.955091","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"23436744","cell_type":"code","source":"labels[:5]","metadata":{"execution":{"iopub.execute_input":"2025-04-28T08:51:42.982032Z","iopub.status.busy":"2025-04-28T08:51:42.981841Z","iopub.status.idle":"2025-04-28T08:51:42.985944Z","shell.execute_reply":"2025-04-28T08:51:42.985307Z"},"id":"HD66I9YLiqQf","outputId":"122acab9-fd91-41b9-dcea-4a7388e9335e","papermill":{"duration":0.011812,"end_time":"2025-04-28T08:51:42.987032","exception":false,"start_time":"2025-04-28T08:51:42.97522","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"f2c5650c","cell_type":"code","source":"train_data = pd.DataFrame(labels, columns=['class'])\ntrain_data['path'] = list_train\ntrain_data.head()","metadata":{"execution":{"iopub.execute_input":"2025-04-28T08:51:43.000208Z","iopub.status.busy":"2025-04-28T08:51:42.999806Z","iopub.status.idle":"2025-04-28T08:51:43.021565Z","shell.execute_reply":"2025-04-28T08:51:43.020971Z"},"id":"l9eW-fUviqQg","outputId":"52944f46-c1f0-4718-96e3-bac1fe179949","papermill":{"duration":0.029369,"end_time":"2025-04-28T08:51:43.022606","exception":false,"start_time":"2025-04-28T08:51:42.993237","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"7290816a","cell_type":"code","source":"train_data['class'].value_counts().sort_values().plot(kind='bar')","metadata":{"execution":{"iopub.execute_input":"2025-04-28T08:51:43.037352Z","iopub.status.busy":"2025-04-28T08:51:43.036787Z","iopub.status.idle":"2025-04-28T08:51:43.302564Z","shell.execute_reply":"2025-04-28T08:51:43.301922Z"},"id":"MORnNa2JiqQi","outputId":"873a62bf-4d96-4dbb-e0dc-8060e83078cf","papermill":{"duration":0.274012,"end_time":"2025-04-28T08:51:43.303657","exception":false,"start_time":"2025-04-28T08:51:43.029645","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"1d8de26a","cell_type":"code","source":"X = train_data['path']\ny = train_data['class']","metadata":{"execution":{"iopub.execute_input":"2025-04-28T08:51:43.318947Z","iopub.status.busy":"2025-04-28T08:51:43.318572Z","iopub.status.idle":"2025-04-28T08:51:43.321893Z","shell.execute_reply":"2025-04-28T08:51:43.321258Z"},"id":"UmClv0eQiqQi","papermill":{"duration":0.011816,"end_time":"2025-04-28T08:51:43.322922","exception":false,"start_time":"2025-04-28T08:51:43.311106","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"664ac4f4","cell_type":"code","source":"y.shape","metadata":{"execution":{"iopub.execute_input":"2025-04-28T08:51:43.337127Z","iopub.status.busy":"2025-04-28T08:51:43.336928Z","iopub.status.idle":"2025-04-28T08:51:43.340599Z","shell.execute_reply":"2025-04-28T08:51:43.340135Z"},"id":"yP5qjuQTiqQj","outputId":"01cc4295-17a6-4876-fc64-8c17c4dd6257","papermill":{"duration":0.011777,"end_time":"2025-04-28T08:51:43.341529","exception":false,"start_time":"2025-04-28T08:51:43.329752","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"9d22bae5","cell_type":"code","source":"y = pd.get_dummies(y)\ny.head()","metadata":{"execution":{"iopub.execute_input":"2025-04-28T08:51:43.355964Z","iopub.status.busy":"2025-04-28T08:51:43.355586Z","iopub.status.idle":"2025-04-28T08:51:43.368445Z","shell.execute_reply":"2025-04-28T08:51:43.367934Z"},"id":"YCiaU66DiqQk","outputId":"4c00d237-6325-486b-df7a-999707a4c04e","papermill":{"duration":0.021094,"end_time":"2025-04-28T08:51:43.369492","exception":false,"start_time":"2025-04-28T08:51:43.348398","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"f50e0957","cell_type":"code","source":"dict_map = {\"0\": \"HTC-1-M7\", \n            \"1\": \"LG-Nexus-5x\", \n            \"2\": \"Motorola-Droid-Maxx\", \n            \"3\": \"Motorola-Nexus-6\", \n            \"4\": \"Motorola-X\", \n            \"5\": \"Samsung-Galaxy-Note3\",\n            \"6\": \"Samsung-Galaxy-S4\",\n            \"7\": \"Sony-NEX-7\",\n            \"8\": \"iPhone-4s\",\n            \"9\": \"iPhone-6\"}","metadata":{"execution":{"iopub.execute_input":"2025-04-28T08:51:43.384503Z","iopub.status.busy":"2025-04-28T08:51:43.383998Z","iopub.status.idle":"2025-04-28T08:51:43.387269Z","shell.execute_reply":"2025-04-28T08:51:43.386773Z"},"id":"l7n6ZreuiqQk","papermill":{"duration":0.011659,"end_time":"2025-04-28T08:51:43.388299","exception":false,"start_time":"2025-04-28T08:51:43.37664","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"792c8d39","cell_type":"code","source":"","metadata":{"id":"5-w86rRNiqQk","papermill":{"duration":0.006838,"end_time":"2025-04-28T08:51:43.402178","exception":false,"start_time":"2025-04-28T08:51:43.39534","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"37219339","cell_type":"code","source":"def open_image(img_path):\n    image = cv2.imread(img_path)\n#     array_img = np.array(image)\n    return image","metadata":{"execution":{"iopub.execute_input":"2025-04-28T08:51:43.416727Z","iopub.status.busy":"2025-04-28T08:51:43.416536Z","iopub.status.idle":"2025-04-28T08:51:43.419506Z","shell.execute_reply":"2025-04-28T08:51:43.419004Z"},"id":"2CCRFsD5iqQl","papermill":{"duration":0.011355,"end_time":"2025-04-28T08:51:43.420499","exception":false,"start_time":"2025-04-28T08:51:43.409144","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"1aa898e9","cell_type":"code","source":"def preprocess(images):\n    return (images / 127.5) - 1.0","metadata":{"execution":{"iopub.execute_input":"2025-04-28T08:51:43.43477Z","iopub.status.busy":"2025-04-28T08:51:43.434587Z","iopub.status.idle":"2025-04-28T08:51:43.437472Z","shell.execute_reply":"2025-04-28T08:51:43.436988Z"},"id":"bT_Ov_EWiqQl","papermill":{"duration":0.011122,"end_time":"2025-04-28T08:51:43.438443","exception":false,"start_time":"2025-04-28T08:51:43.427321","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"fce9d0f4","cell_type":"code","source":"SHAPE = 512\n\ntrain_augmentations = A.Compose([#A.RGBShift(),\n                                 A.RandomCrop(height=SHAPE,width=SHAPE),\n                                 A.RandomGamma(gamma_limit=(80, 120), p=0.8),\n                                #  A.Blur(),\n                                #  A.GaussNoise(),\n                                 # A.JpegCompression(quality_lower=70, quality_upper=90, p=0.9),\n                                 A.GridDistortion(interpolation=cv2.INTER_CUBIC, p=0.9)\n                                 ])\n\nteste_augmentations = A.Compose([#A.RGBShift(),\n                                 A.CenterCrop(height=SHAPE,width=SHAPE)\n#                                  A.RandomGamma(),\n                                #  A.Blur(),\n                                #  A.GaussNoise(),\n#                                  A.JpegCompression(quality_lower=70, quality_upper=100, p=0.5),\n#                                  A.GridDistortion(interpolation=cv2.INTER_CUBIC, p=0.5)                                 \n                                 ])","metadata":{"execution":{"iopub.execute_input":"2025-04-28T08:51:43.453236Z","iopub.status.busy":"2025-04-28T08:51:43.452755Z","iopub.status.idle":"2025-04-28T08:51:43.460716Z","shell.execute_reply":"2025-04-28T08:51:43.460098Z"},"id":"vNXtb6FUiqQl","papermill":{"duration":0.016277,"end_time":"2025-04-28T08:51:43.461694","exception":false,"start_time":"2025-04-28T08:51:43.445417","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"a6da9a04","cell_type":"code","source":"class CameraDataset(tf.keras.utils.Sequence):\n    def __init__(self, X_set, y_set, batch_size, augmenter=None, test=False, *args, **kwargs):\n        \n        self.batch_size = batch_size\n        self.x_set = X_set\n        self.y_set = y_set\n        self.test = test\n        self.augmenter = augmenter\n        \n    def __len__(self):\n        return int(len(self.x_set) / self.batch_size)\n    \n    \n    def __getitem__(self, index):\n        X = self.x_set[index * self.batch_size : (index + 1) * self.batch_size]        \n        y = self.y_set[index * self.batch_size : (index + 1) * self.batch_size]\n        \n        X = [(self.augmenter(image=open_image(x))['image']) for x in X]\n        \n        if self.test:\n            return np.array(X)\n        \n        return np.array(X), y.values","metadata":{"execution":{"iopub.execute_input":"2025-04-28T08:51:43.477205Z","iopub.status.busy":"2025-04-28T08:51:43.476877Z","iopub.status.idle":"2025-04-28T08:51:43.536565Z","shell.execute_reply":"2025-04-28T08:51:43.53584Z"},"id":"3ppMFlmiiqQm","papermill":{"duration":0.069333,"end_time":"2025-04-28T08:51:43.537906","exception":false,"start_time":"2025-04-28T08:51:43.468573","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"efc6b7d0","cell_type":"code","source":"train_dataset = CameraDataset(X, y, batch_size=8, augmenter=train_augmentations)","metadata":{"execution":{"iopub.execute_input":"2025-04-28T08:51:43.603291Z","iopub.status.busy":"2025-04-28T08:51:43.602586Z","iopub.status.idle":"2025-04-28T08:51:43.606093Z","shell.execute_reply":"2025-04-28T08:51:43.605563Z"},"id":"7_5V1Nj6iqQn","papermill":{"duration":0.061673,"end_time":"2025-04-28T08:51:43.607062","exception":false,"start_time":"2025-04-28T08:51:43.545389","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"6e9b71cd","cell_type":"code","source":"x_set, y_set = train_dataset.__getitem__(50)","metadata":{"execution":{"iopub.execute_input":"2025-04-28T08:51:43.622154Z","iopub.status.busy":"2025-04-28T08:51:43.621916Z","iopub.status.idle":"2025-04-28T08:51:44.677827Z","shell.execute_reply":"2025-04-28T08:51:44.677015Z"},"id":"JI3nF2RHiqQo","papermill":{"duration":1.064872,"end_time":"2025-04-28T08:51:44.679218","exception":false,"start_time":"2025-04-28T08:51:43.614346","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"bceac20d","cell_type":"code","source":"x_set[0].shape","metadata":{"execution":{"iopub.execute_input":"2025-04-28T08:51:44.694693Z","iopub.status.busy":"2025-04-28T08:51:44.694471Z","iopub.status.idle":"2025-04-28T08:51:44.698868Z","shell.execute_reply":"2025-04-28T08:51:44.698332Z"},"id":"6hF83IR9iqQo","outputId":"adc9a688-94d4-4b9c-f7f6-41eef75e2789","papermill":{"duration":0.013038,"end_time":"2025-04-28T08:51:44.699798","exception":false,"start_time":"2025-04-28T08:51:44.68676","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"b69d8d5e","cell_type":"code","source":"y_set[0]","metadata":{"execution":{"iopub.execute_input":"2025-04-28T08:51:44.714843Z","iopub.status.busy":"2025-04-28T08:51:44.714669Z","iopub.status.idle":"2025-04-28T08:51:44.718716Z","shell.execute_reply":"2025-04-28T08:51:44.718204Z"},"id":"-sekgQXoiqQo","outputId":"f9bca4ec-040c-48ff-e44f-1cc712bb6714","papermill":{"duration":0.012654,"end_time":"2025-04-28T08:51:44.719716","exception":false,"start_time":"2025-04-28T08:51:44.707062","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"e04d0500","cell_type":"code","source":"plt.imshow(x_set[3])","metadata":{"execution":{"iopub.execute_input":"2025-04-28T08:51:44.734601Z","iopub.status.busy":"2025-04-28T08:51:44.734403Z","iopub.status.idle":"2025-04-28T08:51:44.977976Z","shell.execute_reply":"2025-04-28T08:51:44.977327Z"},"id":"jRosfbPXiqQp","outputId":"7bc65ed1-044b-4d8b-b9a4-6cad6e6bf325","papermill":{"duration":0.257271,"end_time":"2025-04-28T08:51:44.984148","exception":false,"start_time":"2025-04-28T08:51:44.726877","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"a9c8f351","cell_type":"code","source":"def build_model():\n#     inputs =tf.keras.layers.Input(shape=(112,112,3))    \n#     model = tf.keras.applications.EfficientNetB0(include_top=False, input_tensor=inputs, weights=\"imagenet\", classes=10)\n#     model.trainable = True    \n#     x = tf.keras.layers.GlobalAveragePooling2D(name=\"avg_pool\")(model.output)\n#     x = tf.keras.layers.BatchNormalization()(x)\n#     top_dropout_rate = 0.5\n#     x = tf.keras.layers.Dropout(top_dropout_rate)(x)\n#     x = tf.keras.layers.Dense(512, activation='relu')(x)\n#     x = tf.keras.layers.Dropout(top_dropout_rate)(x)\n#     x = tf.keras.layers.BatchNormalization()(x)\n#     x = tf.keras.layers.Dropout(top_dropout_rate)(x)\n#     x = tf.keras.layers.Dense(512, activation='relu')(x)\n#     x = tf.keras.layers.BatchNormalization()(x)\n#     x = tf.keras.layers.Flatten()(x)    \n#     outputs = tf.keras.layers.Dense(10, activation='softmax')(x)    \n#     model =tf.keras.Model(inputs=inputs, outputs=outputs)    \n#     model.compile(optimizer=tf.keras.optimizers.Adam(lr=1e-4), loss='categorical_crossentropy', metrics=['accuracy'])\n#     model.summary()\n    #-------------------------------------------------------------------\n    base_model = tf.keras.applications.InceptionV3(weights='imagenet', include_top=False, input_shape=[SHAPE, SHAPE, 3])\n    base_model.trainable = True  # Allow fine-tuning of the InceptionV3 model\n\n    # Define the input layer\n    inputs = tf.keras.layers.Input(shape=(SHAPE, SHAPE, 3))\n\n    # Preprocess the input using Inception-specific preprocessing\n    x = tf.keras.applications.inception_v3.preprocess_input(inputs)\n\n    # Pass the input through the base InceptionV3 model\n    x = base_model(x, training=True)\n\n    # Add global average pooling layer to reduce dimensions\n    x = tf.keras.layers.GlobalAveragePooling2D()(x)\n\n    # Add fully connected dense layers\n    x = tf.keras.layers.Dense(64, activation='relu')(x)\n    x = tf.keras.layers.Dropout(0.3)(x)\n    x = tf.keras.layers.Dense(32, activation='relu')(x)\n    x = tf.keras.layers.Dropout(0.3)(x)\n\n    # Output layer for classification\n    outputs = tf.keras.layers.Dense(10, activation='softmax')(x)\n\n    # Create the model\n    model = tf.keras.Model(inputs=inputs, outputs=outputs)\n\n    # Compile the model with optimizer, loss, and metrics\n    model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=1e-4), \n                  loss='categorical_crossentropy', \n                  metrics=['accuracy'])\n\n    # Print the model summary\n    model.summary()\n    \n    return model\n","metadata":{"execution":{"iopub.execute_input":"2025-04-28T08:51:45.008591Z","iopub.status.busy":"2025-04-28T08:51:45.008358Z","iopub.status.idle":"2025-04-28T08:51:45.014371Z","shell.execute_reply":"2025-04-28T08:51:45.013866Z"},"id":"Aywspy-liqQp","papermill":{"duration":0.019609,"end_time":"2025-04-28T08:51:45.01548","exception":false,"start_time":"2025-04-28T08:51:44.995871","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"4ebe814e","cell_type":"code","source":"# model = build_model()","metadata":{"execution":{"iopub.execute_input":"2025-04-28T08:51:45.039853Z","iopub.status.busy":"2025-04-28T08:51:45.039226Z","iopub.status.idle":"2025-04-28T08:51:45.04228Z","shell.execute_reply":"2025-04-28T08:51:45.041785Z"},"id":"eHphXy6viqQq","papermill":{"duration":0.016049,"end_time":"2025-04-28T08:51:45.043325","exception":false,"start_time":"2025-04-28T08:51:45.027276","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"cc2f34d6","cell_type":"code","source":"def create_inception_model(SHAPE):\n    # Load the base InceptionV3 model with pre-trained weights\n    base_model = tf.keras.applications.InceptionV3(weights='imagenet', include_top=False, input_shape=[SHAPE, SHAPE, 3])\n    base_model.trainable = True  # Allow fine-tuning of the InceptionV3 model\n\n    # Define the input layer\n    inputs = tf.keras.layers.Input(shape=(SHAPE, SHAPE, 3))\n\n    # Preprocess the input using Inception-specific preprocessing\n    x = tf.keras.applications.inception_v3.preprocess_input(inputs)\n\n    # Pass the input through the base InceptionV3 model\n    x = base_model(x, training=True)\n\n    # Add global average pooling layer to reduce dimensions\n    x = tf.keras.layers.GlobalAveragePooling2D()(x)\n\n    # Add fully connected dense layers\n    x = tf.keras.layers.Dense(64, activation='relu')(x)\n    x = tf.keras.layers.Dropout(0.3)(x)\n    x = tf.keras.layers.Dense(32, activation='relu')(x)\n    x = tf.keras.layers.Dropout(0.3)(x)\n\n    # Output layer for classification\n    outputs = tf.keras.layers.Dense(10, activation='softmax')(x)\n\n    # Create the model\n    model = tf.keras.Model(inputs=inputs, outputs=outputs)\n\n    # Compile the model with optimizer, loss, and metrics\n    model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=1e-4), \n                  loss='categorical_crossentropy', \n                  metrics=['accuracy'])\n\n    # Print the model summary\n    model.summary()\n    \n    return model\n","metadata":{"execution":{"iopub.execute_input":"2025-04-28T08:51:45.068256Z","iopub.status.busy":"2025-04-28T08:51:45.067816Z","iopub.status.idle":"2025-04-28T08:51:45.073588Z","shell.execute_reply":"2025-04-28T08:51:45.072909Z"},"id":"KfW25Su1iqQq","papermill":{"duration":0.019619,"end_time":"2025-04-28T08:51:45.07466","exception":false,"start_time":"2025-04-28T08:51:45.055041","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"07760a38","cell_type":"code","source":"def create_resnet_model(SHAPE):\n    # Load the base ResNet50 model with pre-trained weights\n    base_model = tf.keras.applications.ResNet50(weights='imagenet', include_top=False, input_shape=[SHAPE, SHAPE, 3])\n    base_model.trainable = True  # Allow fine-tuning of the ResNet50 model\n\n    # Define the input layer\n    inputs = tf.keras.layers.Input(shape=(SHAPE, SHAPE, 3))\n\n    # Preprocess the input using ResNet-specific preprocessing\n    x = tf.keras.applications.resnet.preprocess_input(inputs)\n\n    # Pass the input through the base ResNet50 model\n    x = base_model(x, training=True)\n\n    # Add global average pooling layer to reduce dimensions\n    x = tf.keras.layers.GlobalAveragePooling2D()(x)\n\n    # Add fully connected dense layers\n    x = tf.keras.layers.Dense(64, activation='relu')(x)\n    x = tf.keras.layers.Dropout(0.3)(x)\n    x = tf.keras.layers.Dense(32, activation='relu')(x)\n    x = tf.keras.layers.Dropout(0.3)(x)\n\n    # Output layer for classification\n    outputs = tf.keras.layers.Dense(10, activation='softmax')(x)\n\n    # Create the model\n    model = tf.keras.Model(inputs=inputs, outputs=outputs)\n\n    # Compile the model with optimizer, loss, and metrics\n    model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=1e-4), \n                  loss='categorical_crossentropy', \n                  metrics=['accuracy'])\n\n    # Print the model summary\n    model.summary()\n    \n    return model\n","metadata":{"execution":{"iopub.execute_input":"2025-04-28T08:51:45.098755Z","iopub.status.busy":"2025-04-28T08:51:45.098194Z","iopub.status.idle":"2025-04-28T08:51:45.103717Z","shell.execute_reply":"2025-04-28T08:51:45.103208Z"},"id":"WBy5rxbUiqQq","papermill":{"duration":0.018526,"end_time":"2025-04-28T08:51:45.104708","exception":false,"start_time":"2025-04-28T08:51:45.086182","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"828bc169","cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n# First split: 80% training + 20% test\nX_train_full, X_test, y_train_full, y_test = train_test_split(\n    X, y, test_size=0.2, random_state=42, shuffle=True, stratify=y)\n\n# Second split: 10% of total data as validation (12.5% of 80%)\nX_train, X_val, y_train, y_val = train_test_split(\n    X_train_full, y_train_full, test_size=0.125, random_state=42, shuffle=True, stratify=y_train_full)\n\n# Verify the splits\nprint(f\"Training data: {len(X_train)}, Validation data: {len(X_val)}, Test data: {len(X_test)}\")\n\n# Create datasets\ntrain_dataset = CameraDataset(X_train, y_train, batch_size=10, augmenter=train_augmentations)\nval_dataset = CameraDataset(X_val, y_val, batch_size=10, augmenter=teste_augmentations)\ntest_dataset = CameraDataset(X_test, y_test, batch_size=10, augmenter=None)\n","metadata":{"execution":{"iopub.execute_input":"2025-04-28T08:51:45.128243Z","iopub.status.busy":"2025-04-28T08:51:45.128013Z","iopub.status.idle":"2025-04-28T08:51:45.185138Z","shell.execute_reply":"2025-04-28T08:51:45.184464Z"},"id":"4xJs9aO3oNh4","papermill":{"duration":0.070153,"end_time":"2025-04-28T08:51:45.186285","exception":false,"start_time":"2025-04-28T08:51:45.116132","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"859c7725","cell_type":"code","source":"denseNet_model = build_model()","metadata":{"execution":{"iopub.execute_input":"2025-04-28T08:51:45.210065Z","iopub.status.busy":"2025-04-28T08:51:45.209867Z","iopub.status.idle":"2025-04-28T08:51:48.532885Z","shell.execute_reply":"2025-04-28T08:51:48.532361Z"},"id":"iWk54FjwO_OB","papermill":{"duration":3.335973,"end_time":"2025-04-28T08:51:48.533853","exception":false,"start_time":"2025-04-28T08:51:45.19788","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"4d79b685","cell_type":"code","source":"file_path = \"output/weights.best.keras\"\nn = 0\n\ncheckpoint = tf.keras.callbacks.ModelCheckpoint(file_path, monitor=\"val_accuracy\", save_best_only=True, mode='max')\n\nreduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor=\"val_accuracy\", factor=0.9, patience=2, min_lr=1e-6, mode=\"max\", verbose=True)\n\nearly_stopping = tf.keras.callbacks.EarlyStopping(monitor=\"val_accuracy\", patience=5, mode=\"max\", verbose=True)\n\ncallbacks_list = [checkpoint, reduce_lr, early_stopping]\n\n# model = tf.keras.models.load_model(file_path)\n\nhistory = denseNet_model.fit(train_dataset, validation_data=val_dataset, epochs=10, callbacks=callbacks_list)\n\nwhile(n<5):\n  print(n)\n  if n>0:\n    denseNet_model = tf.keras.models.load_model(file_path)\n\n  n += 1\n\n  history = denseNet_model.fit(train_dataset, validation_data=val_dataset, epochs=10, batch_size=10, callbacks=callbacks_list)","metadata":{"execution":{"iopub.execute_input":"2025-04-28T08:51:48.562684Z","iopub.status.busy":"2025-04-28T08:51:48.562456Z","iopub.status.idle":"2025-04-28T12:28:22.199228Z","shell.execute_reply":"2025-04-28T12:28:22.198492Z"},"id":"IE3mrj0uqGHw","outputId":"790eea60-c159-42d3-bbd3-b0aab1eb5505","papermill":{"duration":12994.068708,"end_time":"2025-04-28T12:28:22.61713","exception":false,"start_time":"2025-04-28T08:51:48.548422","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"1bd7a693","cell_type":"code","source":"","metadata":{"papermill":{"duration":0.520569,"end_time":"2025-04-28T12:28:23.562516","exception":false,"start_time":"2025-04-28T12:28:23.041947","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"c9d8f492","cell_type":"code","source":"file_path = \"output/weights.best.keras\"\nmodel = tf.keras.models.load_model(file_path)\nmodel.save('/kaggle/working/resnetModelFinal.keras')\nimport os\nprint(os.listdir('/kaggle/working'))\n","metadata":{"execution":{"iopub.execute_input":"2025-04-28T12:28:24.395188Z","iopub.status.busy":"2025-04-28T12:28:24.394876Z","iopub.status.idle":"2025-04-28T12:28:28.260554Z","shell.execute_reply":"2025-04-28T12:28:28.259793Z"},"id":"q7rIVBen5L4r","outputId":"8ccd3617-78ff-4935-c148-21b98986cd1c","papermill":{"duration":4.287384,"end_time":"2025-04-28T12:28:28.261759","exception":false,"start_time":"2025-04-28T12:28:23.974375","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"ef352fdd","cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\n\n# Replace `y_val` with your actual validation labels\ny_val = np.random.randint(0, 10, 275)  # Example: Random labels for demonstration\n\n# Count samples for each class\nnum_classes = 10\nclass_counts = np.bincount(y_val, minlength=num_classes)\n\n# Display class-wise counts\nprint(\"Class-wise Sample Counts in Validation Dataset:\")\nfor i, count in enumerate(class_counts):\n    print(f\"Class {i}: {count} samples\")\n# Plot the class distribution as a bar graph\nplt.figure(figsize=(8, 5))\nbars = plt.bar(range(num_classes), class_counts, color=\"#88CCEE\")\n\n# Add counts on top of each bar\nfor bar, count in zip(bars, class_counts):\n    plt.text(bar.get_x() + bar.get_width() / 2, bar.get_height() + 1, \n             str(count), ha='center', va='bottom', fontsize=10)\n\n# Labels and title\nplt.xlabel(\"Class\")\nplt.ylabel(\"Number of Samples\")\nplt.title(\"Validation Dataset Class Distribution\")\nplt.ylim(0, 38)\nplt.xticks(range(num_classes))\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.execute_input":"2025-04-28T12:28:29.194382Z","iopub.status.busy":"2025-04-28T12:28:29.193516Z","iopub.status.idle":"2025-04-28T12:28:29.393416Z","shell.execute_reply":"2025-04-28T12:28:29.392731Z"},"id":"I71hhaDd54V1","papermill":{"duration":0.715594,"end_time":"2025-04-28T12:28:29.394577","exception":false,"start_time":"2025-04-28T12:28:28.678983","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"e59ab431","cell_type":"code","source":"X_test[:5]","metadata":{"execution":{"iopub.execute_input":"2025-04-28T12:28:30.254672Z","iopub.status.busy":"2025-04-28T12:28:30.254394Z","iopub.status.idle":"2025-04-28T12:28:30.259963Z","shell.execute_reply":"2025-04-28T12:28:30.25943Z"},"id":"u7VUDzqAuKVJ","outputId":"a6385d4b-75a5-426f-9c84-115532de7d3f","papermill":{"duration":0.45334,"end_time":"2025-04-28T12:28:30.260987","exception":false,"start_time":"2025-04-28T12:28:29.807647","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"8721b154","cell_type":"code","source":"# def read_and_resize(filepath):\n#     im_array = np.array(Image.open((filepath)), dtype=\"uint8\")\n#     pil_im = Image.fromarray(im_array)\n#     new_array = np.array(pil_im)\n#     return new_array\ndef read_and_resize(filepath, target_size=(512, 512)):\n    img = tf.keras.utils.load_img(filepath, target_size=target_size)  # Load and resize the image\n    img_array = tf.keras.utils.img_to_array(img)  # Convert to NumPy array\n    return img_array","metadata":{"execution":{"iopub.execute_input":"2025-04-28T12:28:31.199646Z","iopub.status.busy":"2025-04-28T12:28:31.199347Z","iopub.status.idle":"2025-04-28T12:28:31.203486Z","shell.execute_reply":"2025-04-28T12:28:31.202942Z"},"id":"t6icVzxS6rG4","papermill":{"duration":0.422453,"end_time":"2025-04-28T12:28:31.204717","exception":false,"start_time":"2025-04-28T12:28:30.782264","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"3070a284","cell_type":"code","source":"#DenseNet201\n\nimport matplotlib.pyplot as plt\n\n# Assuming history contains the training and validation accuracy and loss\nhistory = {\n    'accuracy': [0.2092, 0.4316, 0.5198, 0.6118, 0.6655, 0.6682, 0.7548, 0.7407, 0.7807, 0.7819,\n                 0.8066, 0.8243, 0.8101, 0.8474, 0.8400, 0.8405, 0.8966, 0.8659, 0.8798, 0.8831,\n                 0.8893, 0.8858, 0.8977, 0.8920, 0.8880, 0.8889, 0.9045, 0.8981, 0.8912, 0.8911,\n                 0.8962, 0.8963, 0.8965, 0.9168, 0.9234, 0.9234, 0.9191, 0.9178, 0.8978, 0.9261,\n                 0.8988, 0.9151, 0.9150, 0.9153, 0.9318, 0.9324, 0.9407, 0.9407, 0.9454, 0.9454],\n    'val_accuracy': [0.4704, 0.5889, 0.7519, 0.7519, 0.7852, 0.8074, 0.7556, 0.9185, 0.8704, 0.7333,\n                     0.8963, 0.9111, 0.9074, 0.9333, 0.8852, 0.9630, 0.9222, 0.9370, 0.9741, 0.9370,\n                     0.9407, 0.9667, 0.9667, 0.9556, 0.9815, 0.9926, 0.9778, 0.9778, 0.9556, 0.9519,\n                     0.9037, 0.9667, 0.9778, 0.9778, 0.9778, 0.9626, 0.9667, 0.9333, 0.9370, 0.9407,\n                     0.9593, 0.9556, 0.9815, 0.9778, 0.9815, 0.9630, 0.9722],\n    'loss': [2.2271, 1.6331, 1.4256, 1.1983, 1.0747, 1.0034, 0.7953, 0.9016, 0.7309, 0.7234,\n             0.6630, 0.5888, 0.6197, 0.5153, 0.5439, 0.5432, 0.4032, 0.5034, 0.4446, 0.3948,\n             0.3468, 0.3914, 0.3629, 0.3864, 0.3657, 0.3611, 0.3456, 0.3611, 0.3589, 0.3591,\n             0.3427, 0.3426, 0.3351, 0.3041, 0.2713, 0.2713, 0.2686, 0.2882, 0.3322, 0.2705,\n             0.3612, 0.3227, 0.3229, 0.2954, 0.2296, 0.2392, 0.2242, 0.2243, 0.1943, 0.2201],\n    'val_loss': [1.9137, 1.4564, 1.0292, 0.9981, 0.8943, 0.8363, 0.9781, 0.4327, 0.7119, 0.9395,\n                 0.5594, 0.5053, 0.5797, 0.4699, 0.5511, 0.4585, 0.2995, 0.4735, 0.3941, 0.4624,\n                 0.4417, 0.4073, 0.4392, 0.4020, 0.3949, 0.3719, 0.3650, 0.3447, 0.3536, 0.3393,\n                 0.3404, 0.3381, 0.3119, 0.2864, 0.2669, 0.2732, 0.2694, 0.2525, 0.2716, 0.2591,\n                 0.2836, 0.2995, 0.2972, 0.2697, 0.2702, 0.2589, 0.2273, 0.2178, 0.2278, 0.2175]\n}\n\n# Plotting accuracy and validation accuracy\nplt.figure(figsize=(12, 6))\n\n\n\n# Accuracy plot\nplt.subplot(1, 2, 1)\n# plt.plot(history['accuracy'], label='Training Accuracy')\nplt.plot(history['val_accuracy'], label='Accuracy', marker='o')\nplt.title('Accuracy Curve')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend(loc='lower right')\nplt.ylim(0, 1.1)\n\nmax_acc = max(history['val_accuracy'])\nmax_acc_index = history['val_accuracy'].index(max_acc)\n\n# Mark the highest accuracy on the plot\nplt.scatter(max_acc_index, max_acc, color='black', zorder=5)\nplt.text(max_acc_index+0.5, max_acc+0.02, f'{max_acc:.4f}', color='black', fontsize=12, ha='left')\n\n# Loss plot\nplt.subplot(1, 2, 2)\n# plt.plot(history['loss'], label='Training Loss')\nplt.plot(history['val_loss'], label='Loss', marker='o')\nplt.title('Loss Curve')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\n\n# Show the plot\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.execute_input":"2025-04-28T12:28:32.039766Z","iopub.status.busy":"2025-04-28T12:28:32.039483Z","iopub.status.idle":"2025-04-28T12:28:32.387344Z","shell.execute_reply":"2025-04-28T12:28:32.386725Z"},"id":"JnXIbHOC6wK1","papermill":{"duration":0.77259,"end_time":"2025-04-28T12:28:32.388926","exception":false,"start_time":"2025-04-28T12:28:31.616336","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"c46bf103","cell_type":"code","source":"#ResNet50\n\nimport matplotlib.pyplot as plt\n\n# Assuming history contains the training and validation accuracy and loss\nhistory = {\n    'val_accuracy': [0.3296, 0.4444, 0.4370, 0.4519, 0.5296, 0.6370, 0.6370, 0.8741, 0.6889, 0.8037, \n                 0.8815, 0.8815, 0.8148, 0.8741, 0.8222, 0.8667, 0.8963, 0.8963, 0.7259, \n                 0.8296, 0.8593, 0.8259, 0.7296, 0.9296, 0.8519, 0.9519, 0.8593, 0.8778, \n                 0.9370, 0.8296, 0.9111, 0.8481, 0.9296, 0.9037, 0.8593, 0.9037, 0.9222, \n                 0.9370, 0.8889, 0.9074, 0.9222],\n    'val_loss': [1.9969, 1.8511, 1.6538, 1.9321, 1.7060, 1.0209, 1.2679, 0.5289, 1.4508, 0.7908, \n             0.4620, 0.4071, 0.9042, 0.7023, 0.6250, 0.5562, 0.3992, 0.3609, 1.5946, \n             0.8391, 0.6153, 0.8547, 1.2339, 0.2981, 0.6860, 0.2098, 0.7334, 0.4385, \n             0.4022, 0.8391, 0.4599, 0.8970, 0.2535, 0.7815, 0.7477, 0.5009]\n\n}\n\n# Plotting accuracy and validation accuracy\nplt.figure(figsize=(12, 6))\n\n\n# Accuracy plot\nplt.subplot(1, 2, 1)\n# plt.plot(history['accuracy'], label='Training Accuracy')\nplt.plot(history['val_accuracy'], label='Accuracy', marker='o')\nplt.title('Accuracy Curve')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend(loc='lower right')\nplt.ylim(0, 1.05)\n\nmax_acc = max(history['val_accuracy'])\nmax_acc_index = history['val_accuracy'].index(max_acc)\n\n# Mark the highest accuracy on the plot\nplt.scatter(max_acc_index, max_acc, color='black', zorder=5)\nplt.text(max_acc_index+0.5, max_acc+0.02, f'{max_acc:.4f}', color='black', fontsize=12, ha='left')\n\n# Loss plot\nplt.subplot(1, 2, 2)\n# plt.plot(history['loss'], label='Training Loss')\nplt.plot(history['val_loss'], label='Loss', marker= 'o')\nplt.title('Loss Curve')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\n\n# Show the plot\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.execute_input":"2025-04-28T12:28:33.319017Z","iopub.status.busy":"2025-04-28T12:28:33.318735Z","iopub.status.idle":"2025-04-28T12:28:33.689425Z","shell.execute_reply":"2025-04-28T12:28:33.688643Z"},"id":"JnXIbHOC6wK1","papermill":{"duration":0.790944,"end_time":"2025-04-28T12:28:33.690785","exception":false,"start_time":"2025-04-28T12:28:32.899841","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"c36a182f","cell_type":"code","source":"#inception\nimport matplotlib.pyplot as plt\n\n# Assuming the history dictionary contains training and validation accuracy/loss\nhistory = {\n    'accuracy': [0.1505, 0.3327, 0.4282, 0.5138, 0.5586, 0.6320, 0.6651, 0.6700, 0.6803, 0.7167, 0.7429, 0.7399, 0.7615, 0.7617, 0.7879, 0.7995, 0.8112, 0.7953, 0.8296, 0.8431, 0.8306, 0.8301, 0.8549, 0.8407, 0.8597, 0.8631, 0.8423, 0.8490, 0.8909, 0.8801, 0.8758, 0.8758, 0.8667],\n    'loss': [2.2621, 1.9167, 1.6692, 1.5002, 1.3662, 1.1712, 1.0617, 1.0406, 1.0033, 0.9229, 0.8859, 0.7966, 0.7426, 0.7557, 0.6847, 0.6910, 0.6844, 0.6935, 0.5622, 0.5522, 0.5869, 0.5413, 0.4841, 0.5166, 0.4933, 0.4748, 0.5550, 0.4982, 0.3915, 0.4368, 0.4264, 0.4263, 0.4984],\n    'val_accuracy': [0.4333, 0.5333, 0.5185, 0.6593, 0.6444, 0.6630, 0.7519, 0.8222, 0.6333, 0.8444, 0.8037, 0.8222, 0.8630, 0.7481, 0.8926, 0.8148, 0.9222, 0.8556, 0.8815, 0.9370, 0.9074, 0.8852, 0.8407, 0.9296, 0.8852, 0.9074, 0.9593, 0.9519, 0.9630, 0.8741, 0.8778, 0.9259, 0.9481],\n    'val_loss': [1.7328, 1.4468, 2.2240, 1.1228, 1.7268, 1.3329, 0.9265, 0.6009, 1.6321, 0.5132, 0.7312, 0.6262, 0.5411, 0.8318, 0.3795, 0.8945, 0.2263, 0.5532, 0.4135, 0.2605, 0.3028, 0.6072, 0.7493, 0.2318, 0.5691, 0.3440, 0.1151, 0.1381, 0.1442, 0.4861, 0.4708, 0.5204, 0.2434, 0.1823]\n}\n\n# Plotting accuracy and validation accuracy\nplt.figure(figsize=(12, 6))\n\n# Accuracy plot\nplt.subplot(1, 2, 1)\n# plt.plot(history['accuracy'], label='Training Accuracy')\nplt.plot(history['val_accuracy'], label='Accuracy', marker='o')\nplt.title('Accuracy Curve')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend(loc='lower right')\nplt.ylim(0, 1.05)\n\nmax_acc = max(history['val_accuracy'])\nmax_acc_index = history['val_accuracy'].index(max_acc)\n\n# Mark the highest accuracy on the plot\nplt.scatter(max_acc_index, max_acc, color='black', zorder=5)\nplt.text(max_acc_index+0.5, max_acc+0.02, f'{max_acc:.4f}', color='black', fontsize=12, ha='left')\n\n# Loss plot\nplt.subplot(1, 2, 2)\n# plt.plot(history['loss'], label='Training Loss')\nplt.plot(history['val_loss'], label='Loss', marker='o')\nplt.title('Loss Curve')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\n\n# Show the plot\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.execute_input":"2025-04-28T12:28:34.62521Z","iopub.status.busy":"2025-04-28T12:28:34.624899Z","iopub.status.idle":"2025-04-28T12:28:34.968929Z","shell.execute_reply":"2025-04-28T12:28:34.968014Z"},"id":"JnXIbHOC6wK1","papermill":{"duration":0.801594,"end_time":"2025-04-28T12:28:34.970475","exception":false,"start_time":"2025-04-28T12:28:34.168881","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"aa5c5589","cell_type":"code","source":"def crop(img):\n    width, height = img.size  # Get dimensions\n\n    left = (width - 224) / 2\n    top = (height - 224) / 2\n    right = (width + 224) / 2\n    bottom = (height + 224) / 2\n\n    return img.crop((left, top, right, bottom))","metadata":{"execution":{"iopub.execute_input":"2025-04-28T12:28:35.929043Z","iopub.status.busy":"2025-04-28T12:28:35.928753Z","iopub.status.idle":"2025-04-28T12:28:35.933217Z","shell.execute_reply":"2025-04-28T12:28:35.932571Z"},"id":"JnXIbHOC6wK1","papermill":{"duration":0.42546,"end_time":"2025-04-28T12:28:35.93446","exception":false,"start_time":"2025-04-28T12:28:35.509","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"07e1163a","cell_type":"markdown","source":"import tensorflow as tf\nimport numpy as np\nfile_path = \"output/weights.best.keras\"\nmodel = tf.keras.models.load_model(file_path)\n# X_test = np.array([read_and_resize(filepath) for filepath in X_test])\npred_mean = model.predict(X_test)","metadata":{"execution":{"iopub.execute_input":"2025-04-27T10:49:06.440724Z","iopub.status.busy":"2025-04-27T10:49:06.440051Z","iopub.status.idle":"2025-04-27T10:49:08.986627Z","shell.execute_reply":"2025-04-27T10:49:08.985461Z","shell.execute_reply.started":"2025-04-27T10:49:06.440706Z"},"id":"ighrzBmHqaFC","papermill":{"duration":0.431385,"end_time":"2025-04-28T12:28:36.783925","exception":false,"start_time":"2025-04-28T12:28:36.35254","status":"completed"},"tags":[]}},{"id":"82537cc1","cell_type":"code","source":"from sklearn.metrics import accuracy_score\n\nlabels_test=[]\nfor item in pred_mean.argmax(axis=1):\n    labels_test.append(dict_map[str(item)])\n    \ntrue_classes = np.array(y_test)  # Ground truth strings\npredicted_classes = np.array(labels_test) \n# # Calculate accuracy, precision, recall, and F1 score\naccuracy = accuracy_score(true_classes, predicted_classes)\n\n# # Print the metrics\nprint(f\"Accuracy: {accuracy}\")\n\n# # If you need to map predictions back to labels (e.g., to names)\n# labels_test = []\n# for item in predicted_classes:\n#     labels_test.append(dict_map[str(item)])  # Ensure dict_map maps class indices to names\nprint(f\"Shape of y_test: {pred_mean[:5]}\")\nprint(f\"Sample of y_test: {labels_test[:20]}\")\nprint(f\"Sample of y_test: {y_test[:20]}\")","metadata":{"execution":{"iopub.execute_input":"2025-04-28T12:28:37.713575Z","iopub.status.busy":"2025-04-28T12:28:37.712895Z","iopub.status.idle":"2025-04-28T12:28:37.755978Z","shell.execute_reply":"2025-04-28T12:28:37.75506Z"},"id":"p-JoHZcTqVfS","papermill":{"duration":0.461814,"end_time":"2025-04-28T12:28:37.757162","exception":true,"start_time":"2025-04-28T12:28:37.295348","status":"failed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"23466d2c","cell_type":"code","source":"pred_mean.argmax(axis=1)[:10]","metadata":{"execution":{"iopub.status.busy":"2025-04-27T10:49:08.988675Z","iopub.status.idle":"2025-04-27T10:49:08.988981Z","shell.execute_reply":"2025-04-27T10:49:08.988867Z","shell.execute_reply.started":"2025-04-27T10:49:08.988853Z"},"id":"xoTqrRQUlZGN","outputId":"fd12fbaa-d0f2-46ae-980c-cc27b4163df7","papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"outputs":[],"execution_count":null},{"id":"0fada95f","cell_type":"code","source":"labels_test[:10]","metadata":{"execution":{"iopub.status.busy":"2025-04-27T10:49:08.989906Z","iopub.status.idle":"2025-04-27T10:49:08.990221Z","shell.execute_reply":"2025-04-27T10:49:08.99008Z","shell.execute_reply.started":"2025-04-27T10:49:08.990067Z"},"id":"gQh7XRbEl3Ma","outputId":"f2976cfe-8404-49f0-943a-e9dcc3d31cac","papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"outputs":[],"execution_count":null},{"id":"191719e6","cell_type":"code","source":"sample_submission['camera'] = labels_test\nsample_submission.head()","metadata":{"execution":{"iopub.status.busy":"2025-04-27T10:49:08.991447Z","iopub.status.idle":"2025-04-27T10:49:08.991725Z","shell.execute_reply":"2025-04-27T10:49:08.991585Z","shell.execute_reply.started":"2025-04-27T10:49:08.99157Z"},"id":"W16SuUPFmAk7","outputId":"5190573d-bed5-44b0-9087-96f896512150","papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"outputs":[],"execution_count":null},{"id":"25c4985e","cell_type":"code","source":"  sample_submission['camera'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2025-04-27T10:49:08.992618Z","iopub.status.idle":"2025-04-27T10:49:08.992955Z","shell.execute_reply":"2025-04-27T10:49:08.992791Z","shell.execute_reply.started":"2025-04-27T10:49:08.992777Z"},"id":"HO4DFP6UsBaR","outputId":"af05b6d6-4723-4118-989c-47e4a659221e","papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"outputs":[],"execution_count":null},{"id":"cb9fdda4","cell_type":"code","source":"sample_submission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2025-04-27T10:49:08.993787Z","iopub.status.idle":"2025-04-27T10:49:08.994051Z","shell.execute_reply":"2025-04-27T10:49:08.993943Z","shell.execute_reply.started":"2025-04-27T10:49:08.993931Z"},"id":"mcSR_8gpfx5G","papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"outputs":[],"execution_count":null},{"id":"4a484e57","cell_type":"code","source":"sample_submission['camera'].value_counts().sort_values().plot(kind='bar')","metadata":{"execution":{"iopub.status.busy":"2025-04-27T10:49:08.995103Z","iopub.status.idle":"2025-04-27T10:49:08.99539Z","shell.execute_reply":"2025-04-27T10:49:08.995275Z","shell.execute_reply.started":"2025-04-27T10:49:08.99526Z"},"id":"KXk1itbSfxvj","outputId":"99efa709-af89-459c-e71f-103118036407","papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"outputs":[],"execution_count":null},{"id":"4b4004e3","cell_type":"code","source":"import numpy as np\nfrom sklearn.metrics import accuracy_score, classification_report\n\n# 1) Predict on the validation dataset. Make sure `val_dataset` only returns X when used with predict()\npred_mean = model.predict(val_dataset, verbose=1)\n\n# 2) Predicted class indices\npred_idx = pred_mean.argmax(axis=1)\n\n# 3) True class indices: if y_val is 1D, use it directly; if 2D, argmax over axis 1\nif y_val.ndim == 1:\n    true_idx = y_val\nelse:\n    true_idx = np.argmax(y_val, axis=1)\n\n# 4) (Optional) trim to the same length\nmin_len = min(len(true_idx), len(pred_idx))\ntrue_idx = true_idx[:min_len]\npred_idx = pred_idx[:min_len]\n\n# 5) Compute accuracy and classification report\nacc = accuracy_score(true_idx, pred_idx)\nprint(f\"Validation Accuracy: {acc:.4f}\\n\")\nprint(classification_report(true_idx, pred_idx))\n","metadata":{"execution":{"iopub.execute_input":"2025-04-27T20:14:17.556821Z","iopub.status.busy":"2025-04-27T20:14:17.556186Z","iopub.status.idle":"2025-04-27T20:14:47.916754Z","shell.execute_reply":"2025-04-27T20:14:47.915983Z","shell.execute_reply.started":"2025-04-27T20:14:17.556796Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"outputs":[],"execution_count":null},{"id":"cd9af27d","cell_type":"code","source":"base_model.trainable = False\n# build and compile model as before\nhistory1 = model.fit(train_dataset, validation_data=val_dataset, epochs=5)\n\n# now fine-tune:\nbase_model.trainable = True\n# freeze all layers except, say, the last 30\nfor layer in base_model.layers[:-30]:\n    layer.trainable = False\n\nmodel.compile(\n    optimizer=tf.keras.optimizers.Adam(learning_rate=1e-5),\n    loss='categorical_crossentropy',\n    metrics=['accuracy']\n)\nhistory2 = model.fit(\n    train_ds,\n    validation_data=val_ds,\n    epochs=5,\n    callbacks=[\n        tf.keras.callbacks.ReduceLROnPlateau(patience=2, factor=0.5),\n        tf.keras.callbacks.EarlyStopping(patience=3, restore_best_weights=True)\n    ]\n)\n","metadata":{"execution":{"iopub.execute_input":"2025-04-27T20:31:27.151028Z","iopub.status.busy":"2025-04-27T20:31:27.150381Z","iopub.status.idle":"2025-04-27T20:31:27.168552Z","shell.execute_reply":"2025-04-27T20:31:27.16765Z","shell.execute_reply.started":"2025-04-27T20:31:27.151006Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"outputs":[],"execution_count":null}]}