{"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","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls ../input","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Detect hardware, return appropriate distribution strategy\ntry:\n    # TPU detection. No parameters necessary if TPU_NAME environment variable is set. On Kaggle this is always the case.\n    TPU = tf.distribute.cluster_resolver.TPUClusterResolver()  \nexcept ValueError:\n    TPU = None\n\nif TPU:\n    print(f\"\\n... RUNNING ON TPU - {TPU.master()}...\")\n    tf.config.experimental_connect_to_cluster(TPU)\n    tf.tpu.experimental.initialize_tpu_system(TPU)\n    strategy = tf.distribute.experimental.TPUStrategy(TPU)\nelse:\n    print(f\"\\n... RUNNING ON CPU/GPU ...\")\n    # Yield the default distribution strategy in Tensorflow\n    #   --> Works on CPU and single GPU.\n    strategy = tf.distribute.get_strategy() \n\n# What Is a Replica?\n#    --> A single Cloud TPU device consists of FOUR chips, each of which has TWO TPU cores. \n#    --> Therefore, for efficient utilization of Cloud TPU, a program should make use of each of the EIGHT (4x2) cores. \n#    --> Each replica is essentially a copy of the training graph that is run on each core and \n#        trains a mini-batch containing 1/8th of the overall batch size\nN_REPLICAS = strategy.num_replicas_in_sync\n    \nprint(f\"... # OF REPLICAS: {N_REPLICAS} ...\\n\")\n\nprint(f\"\\n... ACCELERATOR SETUP COMPLTED ...\\n\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"\\n... XLA OPTIMIZATIONS STARTING ...\\n\")\n\nprint(f\"\\n... CONFIGURE JIT (JUST IN TIME) COMPILATION ...\\n\")\n# enable XLA optmizations (10% speedup when using @tf.function calls)\ntf.config.optimizer.set_jit(True)\n\nprint(f\"\\n... XLA OPTIMIZATIONS COMPLETED ...\\n\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from kaggle_secrets import UserSecretsClient\nuser_secrets = UserSecretsClient()\nuser_credential = user_secrets.get_gcloud_credential()\n\n# Step 2: Set the credentials\nuser_secrets.set_tensorflow_credential(user_credential)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from kaggle_datasets import KaggleDatasets\nDATA_DIR = KaggleDatasets().get_gcs_path(\"siim-cocolike-tfrecords\")\nDATA_DIR","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MODEL_DIR = KaggleDatasets().get_gcs_path(\"effdet-pretrained-weights\")\nMODEL_DIR","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r ../input/effnetv2-rep/brain_automl brain_automl","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cd ./brain_automl/efficientdet && pip install -r requirements.txt","metadata":{"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile voc_config.yaml\nnum_classes: 2\nlabel_map: {1: opacity}\nlearning_rate: 0.005\nlr_warmup_init: 0.0005\nmoving_average_decay: 0.0\nimage_size: 1024\nautoaugment_policy: \"v2\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp ./brain_automl/efficientdet/dataset/inspect_tfrecords.py ./brain_automl/efficientdet/inspect_tfrecords.py","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cd ./brain_automl/efficientdet && python inspect_tfrecords.py --file_pattern=gs://kds-24cc4a89c1dd510052107a3e9e344adcf9a36e9cc81d0ae574a116ec/fold_4/*.tfrecord --hparams=../../voc_config.yaml","metadata":{"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls ./brain_automl/efficientdet/tfrecord_samples","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython import display\nimport os\ndisplay.display(display.Image(os.path.join(\"./brain_automl/efficientdet/tfrecord_samples\", 'sample2.jpg')))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nImage.open(os.path.join(\"./brain_automl/efficientdet/tfrecord_samples\", 'sample2.jpg')).size","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(6334/5)*4, (6334/5)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cd ./brain_automl/efficientdet && python main.py --mode=train \\\n    --tpu=grpc://10.0.0.2:8470 \\\n    --train_file_pattern=gs://kds-24cc4a89c1dd510052107a3e9e344adcf9a36e9cc81d0ae574a116ec/fold_4/train*.tfrecord \\\n    --model_name=efficientdet-d4 \\\n    --model_dir=gs://effdet_siim_output/efficientdet-d4-finetune/full/fold_4  \\\n    --ckpt=gs://kds-9b7a8288725c55f91e3ee1aabeb4922fd117820c7ce819c617c8a8f4/efficientdet-d4  \\\n    --num_examples_per_epoch=5067 --num_epochs=35  \\\n    --train_batch_size=32 \\\n    --save_checkpoints_steps=250 \\\n    --iterations_per_loop=250 \\\n    --hparams=../../voc_config.yaml \\\n    --strategy=tpu","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}