{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"papermill":{"default_parameters":{},"duration":18323.842364,"end_time":"2021-07-07T00:22:51.855711","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2021-07-06T19:17:28.013347","version":"2.3.3"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":8078,"databundleVersionId":862231,"sourceType":"competition"}],"dockerImageVersionId":31013,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"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","id":"dcO2IxLbiqQY","papermill":{"duration":6.383448,"end_time":"2021-07-06T19:17:41.011649","exception":false,"start_time":"2021-07-06T19:17:34.628201","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T07:54:12.963769Z","iopub.execute_input":"2025-04-27T07:54:12.964273Z","iopub.status.idle":"2025-04-27T07:54:31.991963Z","shell.execute_reply.started":"2025-04-27T07:54:12.964249Z","shell.execute_reply":"2025-04-27T07:54:31.99137Z"}},"outputs":[],"execution_count":null},{"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":{"id":"sBo5sya-iqQc","papermill":{"duration":3.652441,"end_time":"2021-07-06T19:17:44.695022","exception":false,"start_time":"2021-07-06T19:17:41.042581","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T07:54:31.993334Z","iopub.execute_input":"2025-04-27T07:54:31.994116Z","iopub.status.idle":"2025-04-27T07:54:40.788564Z","shell.execute_reply.started":"2025-04-27T07:54:31.994094Z","shell.execute_reply":"2025-04-27T07:54:40.788024Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"list_train[:5]","metadata":{"id":"jBtM1tHSiqQd","outputId":"a065480b-8b49-46da-fca4-0634b3b4dfcf","papermill":{"duration":0.040226,"end_time":"2021-07-06T19:17:44.765984","exception":false,"start_time":"2021-07-06T19:17:44.725758","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T07:54:40.789252Z","iopub.execute_input":"2025-04-27T07:54:40.789472Z","iopub.status.idle":"2025-04-27T07:54:40.794751Z","shell.execute_reply.started":"2025-04-27T07:54:40.789454Z","shell.execute_reply":"2025-04-27T07:54:40.79424Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_class_from_path(filepath):\n    return os.path.dirname(filepath).split(os.sep)[-1]","metadata":{"id":"6oa-5IQtiqQf","papermill":{"duration":0.036783,"end_time":"2021-07-06T19:17:44.834041","exception":false,"start_time":"2021-07-06T19:17:44.797258","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T07:54:40.796222Z","iopub.execute_input":"2025-04-27T07:54:40.796427Z","iopub.status.idle":"2025-04-27T07:54:40.809434Z","shell.execute_reply.started":"2025-04-27T07:54:40.796412Z","shell.execute_reply":"2025-04-27T07:54:40.808782Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels = [get_class_from_path(filepath) for filepath in list_train]","metadata":{"id":"ThwZsJumiqQf","papermill":{"duration":0.042386,"end_time":"2021-07-06T19:17:44.907254","exception":false,"start_time":"2021-07-06T19:17:44.864868","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T07:54:40.810091Z","iopub.execute_input":"2025-04-27T07:54:40.810324Z","iopub.status.idle":"2025-04-27T07:54:40.82696Z","shell.execute_reply.started":"2025-04-27T07:54:40.810308Z","shell.execute_reply":"2025-04-27T07:54:40.82635Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels[:5]","metadata":{"id":"HD66I9YLiqQf","outputId":"122acab9-fd91-41b9-dcea-4a7388e9335e","papermill":{"duration":0.037311,"end_time":"2021-07-06T19:17:44.975987","exception":false,"start_time":"2021-07-06T19:17:44.938676","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T07:54:40.827639Z","iopub.execute_input":"2025-04-27T07:54:40.827827Z","iopub.status.idle":"2025-04-27T07:54:40.842208Z","shell.execute_reply.started":"2025-04-27T07:54:40.827812Z","shell.execute_reply":"2025-04-27T07:54:40.841655Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data = pd.DataFrame(labels, columns=['class'])\ntrain_data['path'] = list_train\ntrain_data.head()","metadata":{"id":"l9eW-fUviqQg","outputId":"52944f46-c1f0-4718-96e3-bac1fe179949","papermill":{"duration":0.053928,"end_time":"2021-07-06T19:17:45.060643","exception":false,"start_time":"2021-07-06T19:17:45.006715","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T07:54:40.842988Z","iopub.execute_input":"2025-04-27T07:54:40.843225Z","iopub.status.idle":"2025-04-27T07:54:40.875214Z","shell.execute_reply.started":"2025-04-27T07:54:40.843205Z","shell.execute_reply":"2025-04-27T07:54:40.874532Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data['class'].value_counts().sort_values().plot(kind='bar')","metadata":{"id":"MORnNa2JiqQi","outputId":"873a62bf-4d96-4dbb-e0dc-8060e83078cf","papermill":{"duration":0.210437,"end_time":"2021-07-06T19:17:45.37054","exception":false,"start_time":"2021-07-06T19:17:45.160103","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T07:54:40.876286Z","iopub.execute_input":"2025-04-27T07:54:40.876543Z","iopub.status.idle":"2025-04-27T07:54:41.154271Z","shell.execute_reply.started":"2025-04-27T07:54:40.876525Z","shell.execute_reply":"2025-04-27T07:54:41.15355Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = train_data['path']\ny = train_data['class']","metadata":{"id":"UmClv0eQiqQi","papermill":{"duration":0.04297,"end_time":"2021-07-06T19:17:45.451361","exception":false,"start_time":"2021-07-06T19:17:45.408391","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T07:54:41.155181Z","iopub.execute_input":"2025-04-27T07:54:41.155454Z","iopub.status.idle":"2025-04-27T07:54:41.159205Z","shell.execute_reply.started":"2025-04-27T07:54:41.155431Z","shell.execute_reply":"2025-04-27T07:54:41.158487Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y.shape","metadata":{"id":"yP5qjuQTiqQj","outputId":"01cc4295-17a6-4876-fc64-8c17c4dd6257","papermill":{"duration":0.038832,"end_time":"2021-07-06T19:17:45.600704","exception":false,"start_time":"2021-07-06T19:17:45.561872","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T07:54:41.161505Z","iopub.execute_input":"2025-04-27T07:54:41.16171Z","iopub.status.idle":"2025-04-27T07:54:41.174117Z","shell.execute_reply.started":"2025-04-27T07:54:41.161696Z","shell.execute_reply":"2025-04-27T07:54:41.173563Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y = pd.get_dummies(y)\ny.head()","metadata":{"id":"YCiaU66DiqQk","outputId":"4c00d237-6325-486b-df7a-999707a4c04e","papermill":{"duration":0.047199,"end_time":"2021-07-06T19:17:45.680334","exception":false,"start_time":"2021-07-06T19:17:45.633135","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T07:54:41.174771Z","iopub.execute_input":"2025-04-27T07:54:41.175254Z","iopub.status.idle":"2025-04-27T07:54:41.197829Z","shell.execute_reply.started":"2025-04-27T07:54:41.175233Z","shell.execute_reply":"2025-04-27T07:54:41.197076Z"}},"outputs":[],"execution_count":null},{"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":{"id":"l7n6ZreuiqQk","papermill":{"duration":0.039056,"end_time":"2021-07-06T19:17:45.752481","exception":false,"start_time":"2021-07-06T19:17:45.713425","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T07:54:41.198434Z","iopub.execute_input":"2025-04-27T07:54:41.198625Z","iopub.status.idle":"2025-04-27T07:54:41.202096Z","shell.execute_reply.started":"2025-04-27T07:54:41.19861Z","shell.execute_reply":"2025-04-27T07:54:41.201365Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"id":"5-w86rRNiqQk","papermill":{"duration":0.032838,"end_time":"2021-07-06T19:17:45.818583","exception":false,"start_time":"2021-07-06T19:17:45.785745","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"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":{"id":"2CCRFsD5iqQl","papermill":{"duration":0.039239,"end_time":"2021-07-06T19:17:45.891114","exception":false,"start_time":"2021-07-06T19:17:45.851875","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T07:54:41.202878Z","iopub.execute_input":"2025-04-27T07:54:41.203148Z","iopub.status.idle":"2025-04-27T07:54:41.216131Z","shell.execute_reply.started":"2025-04-27T07:54:41.203133Z","shell.execute_reply":"2025-04-27T07:54:41.215511Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def preprocess(images):\n    return (images / 127.5) - 1.0","metadata":{"id":"bT_Ov_EWiqQl","papermill":{"duration":0.03881,"end_time":"2021-07-06T19:17:45.96297","exception":false,"start_time":"2021-07-06T19:17:45.92416","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T07:54:41.21698Z","iopub.execute_input":"2025-04-27T07:54:41.21719Z","iopub.status.idle":"2025-04-27T07:54:41.229955Z","shell.execute_reply.started":"2025-04-27T07:54:41.217173Z","shell.execute_reply":"2025-04-27T07:54:41.229313Z"}},"outputs":[],"execution_count":null},{"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":{"id":"vNXtb6FUiqQl","papermill":{"duration":0.040472,"end_time":"2021-07-06T19:17:46.036405","exception":false,"start_time":"2021-07-06T19:17:45.995933","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T07:54:41.230594Z","iopub.execute_input":"2025-04-27T07:54:41.230801Z","iopub.status.idle":"2025-04-27T07:54:41.248168Z","shell.execute_reply.started":"2025-04-27T07:54:41.230786Z","shell.execute_reply":"2025-04-27T07:54:41.24758Z"}},"outputs":[],"execution_count":null},{"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":{"id":"3ppMFlmiiqQm","papermill":{"duration":0.042476,"end_time":"2021-07-06T19:17:46.180187","exception":false,"start_time":"2021-07-06T19:17:46.137711","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T07:54:41.248809Z","iopub.execute_input":"2025-04-27T07:54:41.249059Z","iopub.status.idle":"2025-04-27T07:54:41.3156Z","shell.execute_reply.started":"2025-04-27T07:54:41.249037Z","shell.execute_reply":"2025-04-27T07:54:41.31515Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dataset = CameraDataset(X, y, batch_size=8, augmenter=train_augmentations)","metadata":{"id":"7_5V1Nj6iqQn","papermill":{"duration":0.03877,"end_time":"2021-07-06T19:17:46.252006","exception":false,"start_time":"2021-07-06T19:17:46.213236","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T07:54:41.316242Z","iopub.execute_input":"2025-04-27T07:54:41.316465Z","iopub.status.idle":"2025-04-27T07:54:41.319627Z","shell.execute_reply.started":"2025-04-27T07:54:41.316444Z","shell.execute_reply":"2025-04-27T07:54:41.31902Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_set, y_set = train_dataset.__getitem__(50)","metadata":{"id":"JI3nF2RHiqQo","papermill":{"duration":1.551845,"end_time":"2021-07-06T19:17:47.837103","exception":false,"start_time":"2021-07-06T19:17:46.285258","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T07:54:41.320306Z","iopub.execute_input":"2025-04-27T07:54:41.320546Z","iopub.status.idle":"2025-04-27T07:54:42.365516Z","shell.execute_reply.started":"2025-04-27T07:54:41.320525Z","shell.execute_reply":"2025-04-27T07:54:42.364969Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_set[0].shape","metadata":{"id":"6hF83IR9iqQo","outputId":"adc9a688-94d4-4b9c-f7f6-41eef75e2789","papermill":{"duration":0.04217,"end_time":"2021-07-06T19:17:47.913089","exception":false,"start_time":"2021-07-06T19:17:47.870919","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T07:54:42.366211Z","iopub.execute_input":"2025-04-27T07:54:42.366427Z","iopub.status.idle":"2025-04-27T07:54:42.37122Z","shell.execute_reply.started":"2025-04-27T07:54:42.366411Z","shell.execute_reply":"2025-04-27T07:54:42.370508Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_set[0]","metadata":{"id":"-sekgQXoiqQo","outputId":"f9bca4ec-040c-48ff-e44f-1cc712bb6714","papermill":{"duration":0.042342,"end_time":"2021-07-06T19:17:47.989123","exception":false,"start_time":"2021-07-06T19:17:47.946781","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T07:54:42.372042Z","iopub.execute_input":"2025-04-27T07:54:42.372481Z","iopub.status.idle":"2025-04-27T07:54:42.386883Z","shell.execute_reply.started":"2025-04-27T07:54:42.372465Z","shell.execute_reply":"2025-04-27T07:54:42.386236Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(x_set[3])","metadata":{"id":"jRosfbPXiqQp","outputId":"7bc65ed1-044b-4d8b-b9a4-6cad6e6bf325","papermill":{"duration":0.197937,"end_time":"2021-07-06T19:17:48.221105","exception":false,"start_time":"2021-07-06T19:17:48.023168","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T07:54:42.387583Z","iopub.execute_input":"2025-04-27T07:54:42.387763Z","iopub.status.idle":"2025-04-27T07:54:42.652148Z","shell.execute_reply.started":"2025-04-27T07:54:42.387749Z","shell.execute_reply":"2025-04-27T07:54:42.651446Z"}},"outputs":[],"execution_count":null},{"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":{"id":"Aywspy-liqQp","papermill":{"duration":0.048451,"end_time":"2021-07-06T19:17:48.307514","exception":false,"start_time":"2021-07-06T19:17:48.259063","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T07:54:42.652896Z","iopub.execute_input":"2025-04-27T07:54:42.65313Z","iopub.status.idle":"2025-04-27T07:54:42.659709Z","shell.execute_reply.started":"2025-04-27T07:54:42.653106Z","shell.execute_reply":"2025-04-27T07:54:42.659042Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# model = build_model()","metadata":{"id":"eHphXy6viqQq","papermill":{"duration":0.043199,"end_time":"2021-07-06T19:17:48.388196","exception":false,"start_time":"2021-07-06T19:17:48.344997","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T07:54:42.660451Z","iopub.execute_input":"2025-04-27T07:54:42.660703Z","iopub.status.idle":"2025-04-27T07:54:42.675625Z","shell.execute_reply.started":"2025-04-27T07:54:42.660683Z","shell.execute_reply":"2025-04-27T07:54:42.674864Z"}},"outputs":[],"execution_count":null},{"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":{"id":"KfW25Su1iqQq","papermill":{"duration":0.080832,"end_time":"2021-07-06T19:17:48.522321","exception":false,"start_time":"2021-07-06T19:17:48.441489","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T07:54:42.676399Z","iopub.execute_input":"2025-04-27T07:54:42.676625Z","iopub.status.idle":"2025-04-27T07:54:42.684381Z","shell.execute_reply.started":"2025-04-27T07:54:42.67661Z","shell.execute_reply":"2025-04-27T07:54:42.683718Z"}},"outputs":[],"execution_count":null},{"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":{"id":"WBy5rxbUiqQq","papermill":{"duration":0.071153,"end_time":"2021-07-06T19:17:48.654201","exception":false,"start_time":"2021-07-06T19:17:48.583048","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T07:54:42.685043Z","iopub.execute_input":"2025-04-27T07:54:42.685235Z","iopub.status.idle":"2025-04-27T07:54:42.698415Z","shell.execute_reply.started":"2025-04-27T07:54:42.685221Z","shell.execute_reply":"2025-04-27T07:54:42.697777Z"}},"outputs":[],"execution_count":null},{"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":{"id":"4xJs9aO3oNh4","papermill":{"duration":0.151461,"end_time":"2021-07-06T19:17:48.872401","exception":false,"start_time":"2021-07-06T19:17:48.72094","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T07:54:42.699039Z","iopub.execute_input":"2025-04-27T07:54:42.699273Z","iopub.status.idle":"2025-04-27T07:54:42.768503Z","shell.execute_reply.started":"2025-04-27T07:54:42.699257Z","shell.execute_reply":"2025-04-27T07:54:42.76778Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"denseNet_model = build_model()","metadata":{"id":"iWk54FjwO_OB","papermill":{"duration":8.359684,"end_time":"2021-07-06T19:17:57.293623","exception":false,"start_time":"2021-07-06T19:17:48.933939","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T07:54:42.769443Z","iopub.execute_input":"2025-04-27T07:54:42.769709Z","iopub.status.idle":"2025-04-27T07:54:46.151561Z","shell.execute_reply.started":"2025-04-27T07:54:42.769693Z","shell.execute_reply":"2025-04-27T07:54:46.151038Z"}},"outputs":[],"execution_count":null},{"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":{"id":"IE3mrj0uqGHw","outputId":"790eea60-c159-42d3-bbd3-b0aab1eb5505","papermill":{"duration":18129.324275,"end_time":"2021-07-07T00:20:06.658627","exception":false,"start_time":"2021-07-06T19:17:57.334352","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T07:54:46.155275Z","iopub.execute_input":"2025-04-27T07:54:46.155545Z","iopub.status.idle":"2025-04-27T10:49:05.065299Z","shell.execute_reply.started":"2025-04-27T07:54:46.155529Z","shell.execute_reply":"2025-04-27T10:49:05.064495Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"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":{"id":"q7rIVBen5L4r","outputId":"8ccd3617-78ff-4935-c148-21b98986cd1c","papermill":{"duration":1.834267,"end_time":"2021-07-07T00:20:10.253061","exception":false,"start_time":"2021-07-07T00:20:08.418794","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T11:40:41.965392Z","iopub.execute_input":"2025-04-27T11:40:41.965662Z","iopub.status.idle":"2025-04-27T11:40:45.794553Z","shell.execute_reply.started":"2025-04-27T11:40:41.965641Z","shell.execute_reply":"2025-04-27T11:40:45.793756Z"}},"outputs":[],"execution_count":null},{"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":{"id":"I71hhaDd54V1","papermill":{"duration":2.126222,"end_time":"2021-07-07T00:20:14.146975","exception":false,"start_time":"2021-07-07T00:20:12.020753","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T10:49:05.085806Z","iopub.execute_input":"2025-04-27T10:49:05.086104Z","iopub.status.idle":"2025-04-27T10:49:05.301261Z","shell.execute_reply.started":"2025-04-27T10:49:05.086085Z","shell.execute_reply":"2025-04-27T10:49:05.300636Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_test[:5]","metadata":{"id":"u7VUDzqAuKVJ","outputId":"a6385d4b-75a5-426f-9c84-115532de7d3f","papermill":{"duration":1.796429,"end_time":"2021-07-07T00:20:17.66783","exception":false,"start_time":"2021-07-07T00:20:15.871401","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T10:49:05.302103Z","iopub.execute_input":"2025-04-27T10:49:05.302386Z","iopub.status.idle":"2025-04-27T10:49:05.308324Z","shell.execute_reply.started":"2025-04-27T10:49:05.302359Z","shell.execute_reply":"2025-04-27T10:49:05.307669Z"}},"outputs":[],"execution_count":null},{"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":{"id":"t6icVzxS6rG4","papermill":{"duration":1.76491,"end_time":"2021-07-07T00:20:21.187493","exception":false,"start_time":"2021-07-07T00:20:19.422583","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T10:49:05.30916Z","iopub.execute_input":"2025-04-27T10:49:05.309462Z","iopub.status.idle":"2025-04-27T10:49:05.32262Z","shell.execute_reply.started":"2025-04-27T10:49:05.30944Z","shell.execute_reply":"2025-04-27T10:49:05.321876Z"}},"outputs":[],"execution_count":null},{"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":{"id":"JnXIbHOC6wK1","papermill":{"duration":1.953658,"end_time":"2021-07-07T00:20:24.884393","exception":false,"start_time":"2021-07-07T00:20:22.930735","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T10:49:05.323416Z","iopub.execute_input":"2025-04-27T10:49:05.323762Z","iopub.status.idle":"2025-04-27T10:49:05.695955Z","shell.execute_reply.started":"2025-04-27T10:49:05.323741Z","shell.execute_reply":"2025-04-27T10:49:05.695215Z"}},"outputs":[],"execution_count":null},{"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":{"id":"JnXIbHOC6wK1","papermill":{"duration":1.953658,"end_time":"2021-07-07T00:20:24.884393","exception":false,"start_time":"2021-07-07T00:20:22.930735","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T10:49:05.696816Z","iopub.execute_input":"2025-04-27T10:49:05.697621Z","iopub.status.idle":"2025-04-27T10:49:06.079909Z","shell.execute_reply.started":"2025-04-27T10:49:05.697594Z","shell.execute_reply":"2025-04-27T10:49:06.07889Z"}},"outputs":[],"execution_count":null},{"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":{"id":"JnXIbHOC6wK1","papermill":{"duration":1.953658,"end_time":"2021-07-07T00:20:24.884393","exception":false,"start_time":"2021-07-07T00:20:22.930735","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T10:49:06.080797Z","iopub.execute_input":"2025-04-27T10:49:06.081203Z","iopub.status.idle":"2025-04-27T10:49:06.433364Z","shell.execute_reply.started":"2025-04-27T10:49:06.081181Z","shell.execute_reply":"2025-04-27T10:49:06.432496Z"}},"outputs":[],"execution_count":null},{"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":{"id":"JnXIbHOC6wK1","papermill":{"duration":1.953658,"end_time":"2021-07-07T00:20:24.884393","exception":false,"start_time":"2021-07-07T00:20:22.930735","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T10:49:06.434242Z","iopub.execute_input":"2025-04-27T10:49:06.434989Z","iopub.status.idle":"2025-04-27T10:49:06.439185Z","shell.execute_reply.started":"2025-04-27T10:49:06.434961Z","shell.execute_reply":"2025-04-27T10:49:06.438396Z"}},"outputs":[],"execution_count":null},{"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":{"id":"ighrzBmHqaFC","papermill":{"duration":109.792501,"end_time":"2021-07-07T00:22:16.49666","exception":false,"start_time":"2021-07-07T00:20:26.704159","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2025-04-27T10:49:06.440051Z","iopub.execute_input":"2025-04-27T10:49:06.440724Z","iopub.status.idle":"2025-04-27T10:49:08.986627Z","shell.execute_reply.started":"2025-04-27T10:49:06.440706Z","shell.execute_reply":"2025-04-27T10:49:08.985461Z"}}},{"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":{"id":"p-JoHZcTqVfS","papermill":{"duration":1.77915,"end_time":"2021-07-07T00:22:20.055402","exception":false,"start_time":"2021-07-07T00:22:18.276252","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T10:49:08.987275Z","iopub.status.idle":"2025-04-27T10:49:08.987613Z","shell.execute_reply.started":"2025-04-27T10:49:08.987443Z","shell.execute_reply":"2025-04-27T10:49:08.98746Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred_mean.argmax(axis=1)[:10]","metadata":{"id":"xoTqrRQUlZGN","outputId":"fd12fbaa-d0f2-46ae-980c-cc27b4163df7","papermill":{"duration":1.739393,"end_time":"2021-07-07T00:22:23.526564","exception":false,"start_time":"2021-07-07T00:22:21.787171","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T10:49:08.988675Z","iopub.status.idle":"2025-04-27T10:49:08.988981Z","shell.execute_reply.started":"2025-04-27T10:49:08.988853Z","shell.execute_reply":"2025-04-27T10:49:08.988867Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels_test[:10]","metadata":{"id":"gQh7XRbEl3Ma","outputId":"f2976cfe-8404-49f0-943a-e9dcc3d31cac","papermill":{"duration":1.729289,"end_time":"2021-07-07T00:22:27.290085","exception":false,"start_time":"2021-07-07T00:22:25.560796","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T10:49:08.989906Z","iopub.status.idle":"2025-04-27T10:49:08.990221Z","shell.execute_reply.started":"2025-04-27T10:49:08.990067Z","shell.execute_reply":"2025-04-27T10:49:08.99008Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_submission['camera'] = labels_test\nsample_submission.head()","metadata":{"id":"W16SuUPFmAk7","outputId":"5190573d-bed5-44b0-9087-96f896512150","papermill":{"duration":1.773357,"end_time":"2021-07-07T00:22:30.795584","exception":false,"start_time":"2021-07-07T00:22:29.022227","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T10:49:08.991447Z","iopub.status.idle":"2025-04-27T10:49:08.991725Z","shell.execute_reply.started":"2025-04-27T10:49:08.99157Z","shell.execute_reply":"2025-04-27T10:49:08.991585Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"  sample_submission['camera'].value_counts()","metadata":{"id":"HO4DFP6UsBaR","outputId":"af05b6d6-4723-4118-989c-47e4a659221e","papermill":{"duration":1.741005,"end_time":"2021-07-07T00:22:34.327117","exception":false,"start_time":"2021-07-07T00:22:32.586112","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T10:49:08.992618Z","iopub.status.idle":"2025-04-27T10:49:08.992955Z","shell.execute_reply.started":"2025-04-27T10:49:08.992777Z","shell.execute_reply":"2025-04-27T10:49:08.992791Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_submission.to_csv(\"submission.csv\", index=False)","metadata":{"id":"mcSR_8gpfx5G","papermill":{"duration":2.724296,"end_time":"2021-07-07T00:22:39.099858","exception":false,"start_time":"2021-07-07T00:22:36.375562","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T10:49:08.993787Z","iopub.status.idle":"2025-04-27T10:49:08.994051Z","shell.execute_reply.started":"2025-04-27T10:49:08.993931Z","shell.execute_reply":"2025-04-27T10:49:08.993943Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_submission['camera'].value_counts().sort_values().plot(kind='bar')","metadata":{"id":"KXk1itbSfxvj","outputId":"99efa709-af89-459c-e71f-103118036407","papermill":{"duration":1.948886,"end_time":"2021-07-07T00:22:42.878467","exception":false,"start_time":"2021-07-07T00:22:40.929581","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T10:49:08.995103Z","iopub.status.idle":"2025-04-27T10:49:08.99539Z","shell.execute_reply.started":"2025-04-27T10:49:08.99526Z","shell.execute_reply":"2025-04-27T10:49:08.995275Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T20:14:17.556186Z","iopub.execute_input":"2025-04-27T20:14:17.556821Z","iopub.status.idle":"2025-04-27T20:14:47.916754Z","shell.execute_reply.started":"2025-04-27T20:14:17.556796Z","shell.execute_reply":"2025-04-27T20:14:47.915983Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-27T20:31:27.150381Z","iopub.execute_input":"2025-04-27T20:31:27.151028Z","iopub.status.idle":"2025-04-27T20:31:27.168552Z","shell.execute_reply.started":"2025-04-27T20:31:27.151006Z","shell.execute_reply":"2025-04-27T20:31:27.16765Z"}},"outputs":[],"execution_count":null}]}