{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications.resnet50 import ResNet50\nfrom tensorflow.keras.applications import DenseNet121\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, GlobalAveragePooling2D, Activation, Dropout","metadata":{"execution":{"iopub.status.busy":"2023-10-14T18:42:36.207003Z","iopub.execute_input":"2023-10-14T18:42:36.207476Z","iopub.status.idle":"2023-10-14T18:42:43.529833Z","shell.execute_reply.started":"2023-10-14T18:42:36.207439Z","shell.execute_reply":"2023-10-14T18:42:43.528861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow.python.client.device_lib as device_lib\n\ndef is_kaggle_gpu_enabled():\n  \"\"\"Return whether GPU is enabled in the running Kaggle kernel\"\"\"\n\n  # When only CPU is enabled the list shows two CPU entries, otherwise there are more, listing GPU as well\n  return len(device_lib.list_local_devices()) > 2\n\n# Example usage:\n\nif is_kaggle_gpu_enabled():\n  print(\"GPU is enabled\")\nelse:\n  print(\"GPU is not enabled\")\n","metadata":{"execution":{"iopub.status.busy":"2023-10-14T18:42:43.531487Z","iopub.execute_input":"2023-10-14T18:42:43.532083Z","iopub.status.idle":"2023-10-14T18:42:46.184566Z","shell.execute_reply.started":"2023-10-14T18:42:43.532051Z","shell.execute_reply":"2023-10-14T18:42:46.183619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check if GPU is enabled in Kaggle notebook\nif is_kaggle_gpu_enabled():\n # Enable GPU usage\n tf.config.experimental.set_memory_growth(tf.config.list_physical_devices('GPU')[0], True)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-14T18:42:46.185572Z","iopub.execute_input":"2023-10-14T18:42:46.185947Z","iopub.status.idle":"2023-10-14T18:42:46.199037Z","shell.execute_reply.started":"2023-10-14T18:42:46.185916Z","shell.execute_reply":"2023-10-14T18:42:46.198033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Num GPUs Available: \", len(tf.config.experimental.list_physical_devices('GPU')))","metadata":{"execution":{"iopub.status.busy":"2023-10-14T18:42:46.201368Z","iopub.execute_input":"2023-10-14T18:42:46.202239Z","iopub.status.idle":"2023-10-14T18:42:46.215303Z","shell.execute_reply.started":"2023-10-14T18:42:46.202211Z","shell.execute_reply":"2023-10-14T18:42:46.214156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntrain_datagen = ImageDataGenerator(\n    preprocessing_function=tf.keras.applications.resnet50.preprocess_input,\n    shear_range=0.1,\n    zoom_range=0.1,\n    horizontal_flip=True,\n    vertical_flip=True,\n    fill_mode='nearest'\n)\ntrain_generator = train_datagen.flow_from_directory(\n        '/kaggle/input/hotel-id-to-combat-human-trafficking-2022-fgvc9/train_images/',\n        target_size=(224, 224),\n        batch_size=32,\n        class_mode='categorical'\n    )\n","metadata":{"execution":{"iopub.status.busy":"2023-10-14T18:42:46.216683Z","iopub.execute_input":"2023-10-14T18:42:46.217049Z","iopub.status.idle":"2023-10-14T18:42:54.402397Z","shell.execute_reply.started":"2023-10-14T18:42:46.217021Z","shell.execute_reply":"2023-10-14T18:42:54.401519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the base model\nbase_model = tf.keras.applications.ResNet50(weights='imagenet', include_top=False)\n#base_model = tf.keras.applications.DenseNet121(weights='imagenet', include_top=False)\n\n\n# Add new layers to the model\nx = base_model.output\nx = tf.keras.layers.GlobalAveragePooling2D()(x)\nx = Dense(512, activation='relu')(x) \nx = Dropout(0.5)(x)\nx = Dense(256, activation='relu')(x)\npredictions = tf.keras.layers.Dense(3116, activation='softmax')(x)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-10-14T18:42:54.403643Z","iopub.execute_input":"2023-10-14T18:42:54.40451Z","iopub.status.idle":"2023-10-14T18:43:00.066693Z","shell.execute_reply.started":"2023-10-14T18:42:54.40448Z","shell.execute_reply":"2023-10-14T18:43:00.065763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(base_model.summary())","metadata":{"execution":{"iopub.status.busy":"2023-10-14T18:43:00.06816Z","iopub.execute_input":"2023-10-14T18:43:00.068682Z","iopub.status.idle":"2023-10-14T18:43:00.073153Z","shell.execute_reply.started":"2023-10-14T18:43:00.06865Z","shell.execute_reply":"2023-10-14T18:43:00.072007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for i, layer in enumerate(base_model.layers):\n#   print(i, layer.name)","metadata":{"execution":{"iopub.status.busy":"2023-10-14T18:43:00.074492Z","iopub.execute_input":"2023-10-14T18:43:00.075569Z","iopub.status.idle":"2023-10-14T18:43:00.094631Z","shell.execute_reply.started":"2023-10-14T18:43:00.075539Z","shell.execute_reply":"2023-10-14T18:43:00.093622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Compile the model\nmodel = tf.keras.Model(inputs=base_model.input, outputs=predictions)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-14T18:43:00.096079Z","iopub.execute_input":"2023-10-14T18:43:00.096419Z","iopub.status.idle":"2023-10-14T18:43:00.117223Z","shell.execute_reply.started":"2023-10-14T18:43:00.096389Z","shell.execute_reply":"2023-10-14T18:43:00.116435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for i, layer in enumerate(model.layers):\n#   print(i, layer.name)","metadata":{"execution":{"iopub.status.busy":"2023-10-14T18:43:00.1203Z","iopub.execute_input":"2023-10-14T18:43:00.120527Z","iopub.status.idle":"2023-10-14T18:43:00.124289Z","shell.execute_reply.started":"2023-10-14T18:43:00.120507Z","shell.execute_reply":"2023-10-14T18:43:00.123454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfor layer in model.layers[:175]:\n  layer.trainable = False\n\nfor layer in model.layers[175:]:\n  layer.trainable = True","metadata":{"execution":{"iopub.status.busy":"2023-10-14T18:43:00.125457Z","iopub.execute_input":"2023-10-14T18:43:00.125972Z","iopub.status.idle":"2023-10-14T18:43:00.140212Z","shell.execute_reply.started":"2023-10-14T18:43:00.125942Z","shell.execute_reply":"2023-10-14T18:43:00.139321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define a custom learning rate\ncustom_learning_rate = 0.005  # You can adjust this value as needed\n\n# Create a custom Adam optimizer with the specified learning rate\ncustom_optimizer = tf.keras.optimizers.Adam(learning_rate=custom_learning_rate)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-14T18:43:00.141568Z","iopub.execute_input":"2023-10-14T18:43:00.142184Z","iopub.status.idle":"2023-10-14T18:43:00.15584Z","shell.execute_reply.started":"2023-10-14T18:43:00.142154Z","shell.execute_reply":"2023-10-14T18:43:00.154973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Compile the model using the custom optimizer\nmodel.compile(optimizer=custom_optimizer, loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-10-14T18:43:00.157233Z","iopub.execute_input":"2023-10-14T18:43:00.157859Z","iopub.status.idle":"2023-10-14T18:43:00.172938Z","shell.execute_reply.started":"2023-10-14T18:43:00.157829Z","shell.execute_reply":"2023-10-14T18:43:00.172049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import ModelCheckpoint\n\nmodel_checkpoint = ModelCheckpoint(\n    'best_model.h5',\n    monitor='val_loss',  # You can choose 'val_loss', 'val_accuracy', etc. as the monitoring metric\n    save_best_only=True,\n    save_weights_only=False,\n    mode='auto',\n    verbose=1\n)","metadata":{"execution":{"iopub.status.busy":"2023-10-14T18:43:00.174061Z","iopub.execute_input":"2023-10-14T18:43:00.175103Z","iopub.status.idle":"2023-10-14T18:43:00.180548Z","shell.execute_reply.started":"2023-10-14T18:43:00.175075Z","shell.execute_reply":"2023-10-14T18:43:00.179704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(\n    train_generator,\n    epochs=2,\n    callbacks=[model_checkpoint]  # Include the ModelCheckpoint callback\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-14T18:43:00.181693Z","iopub.execute_input":"2023-10-14T18:43:00.182487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Print the training loss and accuracy\ntrain_loss = model.history.history['loss']\ntrain_accuracy = model.history.history['accuracy']\n\nprint('Training loss:', train_loss[-1])\nprint('Training accuracy:', train_accuracy[-1])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n\n# Plot the loss and accuracy\nplt.plot(train_loss, label='Train loss')\nplt.plot(train_accuracy, label='Train accuracy')\nplt.legend()\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test \n# Load test data\ntest_datagen = ImageDataGenerator(preprocessing_function=tf.keras.applications.resnet50.preprocess_input)\ntest_generator = test_datagen.flow_from_directory(\n    '/kaggle/input/hotel-id-to-combat-human-trafficking-2022-fgvc9/test_images/', \n    target_size=(224, 224), \n    batch_size=32)\n\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load best model weights\n#model.load_weights('best_model.h5')\n\n# Generate predictions\ntest_preds = model.predict(test_generator, verbose=1)\n\n# Map predictions to ids\ntest_ids = test_generator.filenames\npred_ids = [test_ids[i] for i in range(len(test_preds))]\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create submission dataframe\nsub = pd.DataFrame({'id': pred_ids})\nsub['label'] = np.argmax(test_preds, axis=1)\nsub.to_csv('submission.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}