{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.15","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"tpu1vmV38","dataSources":[{"sourceId":18278,"databundleVersionId":968043,"sourceType":"competition"}],"dockerImageVersionId":30788,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false},"colab":{"provenance":[],"gpuType":"V28"},"accelerator":"TPU"},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# A Simple TF 2.1x notebook\n\n\n\nThis is based entirely off of Martin Gorner's excellent starter notebook, and is intended solely as a simple, shorter introduction to the operations being performed there.","metadata":{"_uuid":"a1c0e91b-82e0-4038-b999-f69d51230b34","_cell_guid":"0ab7baae-f97d-4871-af10-b7c550a5b0b7","trusted":true,"id":"dITuW0lECLAj"}},{"cell_type":"code","source":"import tensorflow as tf\n\nimport numpy as np\n\nprint(\"Tensorflow version \" + tf.__version__)","metadata":{"_uuid":"c68fda87-0dad-447f-9f6a-ed6fa365b64e","_cell_guid":"ac894bd1-2ecc-49c2-9164-957bcc3b27bb","jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-11-10T21:04:51.409240Z","iopub.execute_input":"2024-11-10T21:04:51.409928Z","iopub.status.idle":"2024-11-10T21:04:51.415180Z","shell.execute_reply.started":"2024-11-10T21:04:51.409885Z","shell.execute_reply":"2024-11-10T21:04:51.414343Z"},"trusted":true,"id":"5eTwTWQrCLAl","outputId":"6cb5798c-d468-4c85-af16-40b24f8a8c1b","collapsed":false},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Detect my accelerator","metadata":{"_uuid":"3b5d615a-3182-4f81-a8f3-cb6e54264e63","_cell_guid":"93dde2ac-0d09-4d46-a121-f2ecae88c786","trusted":true,"id":"HY2JyW3PCLAl"}},{"cell_type":"code","source":"tpu = tf.distribute.cluster_resolver.TPUClusterResolver(tpu='local')\n\ntf.config.experimental_connect_to_cluster(tpu)\n\n# This is the TPU initialization code that has to be at the beginning.\n\ntf.tpu.experimental.initialize_tpu_system(tpu)\n\nprint(\"All devices: \", tf.config.list_logical_devices('TPU'))","metadata":{"id":"iT1-fluRrBWo","outputId":"efcde507-3434-4660-91a3-7553da131d6f","trusted":true,"execution":{"iopub.status.busy":"2024-11-10T21:04:54.901768Z","iopub.execute_input":"2024-11-10T21:04:54.902741Z","iopub.status.idle":"2024-11-10T21:05:03.610556Z","shell.execute_reply.started":"2024-11-10T21:04:54.902704Z","shell.execute_reply":"2024-11-10T21:05:03.609495Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"strategy = tf.distribute.TPUStrategy(tpu)","metadata":{"id":"PEaEluSI12wc","trusted":true,"execution":{"iopub.status.busy":"2024-11-10T21:05:12.272562Z","iopub.execute_input":"2024-11-10T21:05:12.273575Z","iopub.status.idle":"2024-11-10T21:05:12.288893Z","shell.execute_reply.started":"2024-11-10T21:05:12.273538Z","shell.execute_reply":"2024-11-10T21:05:12.287670Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Get my data path","metadata":{"_uuid":"0ac5b0f1-82b4-4f11-9a6a-20c5e9e0c217","_cell_guid":"2eef11e3-52b1-48c6-aced-1b23877d2542","trusted":true,"id":"BLpZzg80CLAl"}},{"cell_type":"code","source":"# CHANGED FOR TPU 1VM: Direct access to the filesystem, no longer need to get_gcs_path()!\n\nGCS_DS_PATH = '/kaggle/input/flower-classification-with-tpus'","metadata":{"_uuid":"1cd5541c-e170-43a3-b2dd-edfbc5e9c1b0","_cell_guid":"501e1373-26a2-4f6c-a106-438be52d9a21","jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-11-10T21:05:21.565897Z","iopub.execute_input":"2024-11-10T21:05:21.566882Z","iopub.status.idle":"2024-11-10T21:05:21.570869Z","shell.execute_reply.started":"2024-11-10T21:05:21.566843Z","shell.execute_reply":"2024-11-10T21:05:21.569799Z"},"trusted":true,"id":"1fXYOlU_CLAl","collapsed":false},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Set some parameters","metadata":{"_uuid":"242649f3-dd3c-48d3-8be1-e2cb9bc70583","_cell_guid":"e80238ac-95c4-471c-86b7-bd07d6a6db7e","trusted":true,"id":"t5_B6s7BCLAl"}},{"cell_type":"code","source":"IMAGE_SIZE = [192, 192] # at this size, a GPU will run out of memory. Use the TPU\n\nEPOCHS = 10\n\nBATCH_SIZE = 16 * strategy.num_replicas_in_sync\n\n\n\nNUM_TRAINING_IMAGES = 12753\n\nNUM_TEST_IMAGES = 7382\n\nSTEPS_PER_EPOCH = NUM_TRAINING_IMAGES // BATCH_SIZE\n\nAUTO = tf.data.experimental.AUTOTUNE","metadata":{"_uuid":"fd506828-093f-4291-b8e4-bb3512509cf7","_cell_guid":"fff4b014-6d83-4909-a9fd-396584ac486b","jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-11-10T21:05:24.196961Z","iopub.execute_input":"2024-11-10T21:05:24.197973Z","iopub.status.idle":"2024-11-10T21:05:24.202856Z","shell.execute_reply.started":"2024-11-10T21:05:24.197932Z","shell.execute_reply":"2024-11-10T21:05:24.201949Z"},"trusted":true,"id":"paZIIDutCLAl","collapsed":false},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Load my data\n\n\n\nThis data is loaded from Kaggle and automatically sharded to maximize parallelization.","metadata":{"_uuid":"0eaf2aba-6dff-4726-933d-f5c3a40d2b44","_cell_guid":"5b03ba57-3f00-404f-939a-cd401bd2c350","trusted":true,"id":"uPdguiXpCLAm"}},{"cell_type":"code","source":"def decode_image(image_data):\n\n    image = tf.image.decode_jpeg(image_data, channels=3)\n\n    image = tf.cast(image, tf.float32) / 255.0  # convert image to floats in [0, 1] range\n\n    image = tf.reshape(image, [*IMAGE_SIZE, 3]) # explicit size needed for TPU\n\n    return image\n\n\n\ndef read_labeled_tfrecord(example):\n\n    LABELED_TFREC_FORMAT = {\n\n        \"image\": tf.io.FixedLenFeature([], tf.string), # tf.string means bytestring\n\n        \"class\": tf.io.FixedLenFeature([], tf.int64),  # shape [] means single element\n\n    }\n\n    example = tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)\n\n    image = decode_image(example['image'])\n\n    label = tf.cast(example['class'], tf.int32)\n\n    return image, label # returns a dataset of (image, label) pairs\n\n\n\ndef read_unlabeled_tfrecord(example):\n\n    UNLABELED_TFREC_FORMAT = {\n\n        \"image\": tf.io.FixedLenFeature([], tf.string), # tf.string means bytestring\n\n        \"id\": tf.io.FixedLenFeature([], tf.string),  # shape [] means single element\n\n        # class is missing, this competitions's challenge is to predict flower classes for the test dataset\n\n    }\n\n    example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)\n\n    image = decode_image(example['image'])\n\n    idnum = example['id']\n\n    return image, idnum # returns a dataset of image(s)\n\n\n\ndef load_dataset(filenames, labeled=True, ordered=False):\n\n    # Read from TFRecords. For optimal performance, reading from multiple files at once and\n\n    # disregarding data order. Order does not matter since we will be shuffling the data anyway.\n\n\n\n    ignore_order = tf.data.Options()\n\n    if not ordered:\n\n        ignore_order.experimental_deterministic = False # disable order, increase speed\n\n\n\n    dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTO) # automatically interleaves reads from multiple files\n\n    dataset = dataset.with_options(ignore_order) # uses data as soon as it streams in, rather than in its original order\n\n    dataset = dataset.map(read_labeled_tfrecord if labeled else read_unlabeled_tfrecord, num_parallel_calls=AUTO)\n\n    # returns a dataset of (image, label) pairs if labeled=True or (image, id) pairs if labeled=False\n\n    return dataset\n\n\n\ndef get_training_dataset():\n\n    dataset = load_dataset(tf.io.gfile.glob(GCS_DS_PATH + '/tfrecords-jpeg-192x192/train/*.tfrec'), labeled=True)\n\n    dataset = dataset.repeat() # the training dataset must repeat for several epochs\n\n    dataset = dataset.shuffle(2048)\n\n    dataset = dataset.batch(BATCH_SIZE)\n\n    dataset = dataset.prefetch(AUTO) # prefetch next batch while training (autotune prefetch buffer size)\n\n    return dataset\n\n\n\ndef get_validation_dataset():\n\n    dataset = load_dataset(tf.io.gfile.glob(GCS_DS_PATH + '/tfrecords-jpeg-192x192/val/*.tfrec'), labeled=True, ordered=False)\n\n    dataset = dataset.batch(BATCH_SIZE)\n\n    dataset = dataset.cache()\n\n    dataset = dataset.prefetch(AUTO) # prefetch next batch while training (autotune prefetch buffer size)\n\n    return dataset\n\n\n\ndef get_test_dataset(ordered=False):\n\n    dataset = load_dataset(tf.io.gfile.glob(GCS_DS_PATH + '/tfrecords-jpeg-192x192/test/*.tfrec'), labeled=False, ordered=ordered)\n\n    dataset = dataset.batch(BATCH_SIZE)\n\n    dataset = dataset.prefetch(AUTO) # prefetch next batch while training (autotune prefetch buffer size)\n\n    return dataset\n\n\n\ntraining_dataset = get_training_dataset()\n\nvalidation_dataset = get_validation_dataset()","metadata":{"_uuid":"b8a52a0f-50a0-4dab-a702-955a35600a17","_cell_guid":"71de7242-f9d9-4c29-8dfd-0ae79b8641c7","jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-11-10T21:05:28.810696Z","iopub.execute_input":"2024-11-10T21:05:28.811133Z","iopub.status.idle":"2024-11-10T21:05:29.018332Z","shell.execute_reply.started":"2024-11-10T21:05:28.811098Z","shell.execute_reply":"2024-11-10T21:05:29.017418Z"},"trusted":true,"id":"_8TJApJiCLAm","collapsed":false},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Build a model on TPU (or GPU, or CPU...) with Tensorflow 2.1!","metadata":{"_uuid":"d679cfbe-ba20-4c5d-a1a6-b0650058bc75","_cell_guid":"f0938c02-03e8-4603-9b17-38ef39ac3330","trusted":true,"id":"Ph4G90dFCLAm"}},{"cell_type":"code","source":"with strategy.scope():\n\n    pretrained_model = tf.keras.applications.VGG16(weights='imagenet', include_top=False ,input_shape=[*IMAGE_SIZE, 3])\n\n    pretrained_model.trainable = False # tramsfer learning\n\n\n\n    model = tf.keras.Sequential([\n\n        pretrained_model,\n\n        tf.keras.layers.GlobalAveragePooling2D(),\n\n        tf.keras.layers.Dense(104, activation='softmax')\n\n    ])\n\n\n\n    model.compile(\n\n        optimizer='adam',\n\n        loss = 'sparse_categorical_crossentropy',\n\n        metrics=['sparse_categorical_accuracy']\n\n    )\n\n\n","metadata":{"_uuid":"43c90f57-a8fb-4844-ae42-954e291d10aa","_cell_guid":"4423286c-bbc9-46e5-a20e-49600014692c","jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-11-10T21:05:31.348051Z","iopub.execute_input":"2024-11-10T21:05:31.348947Z","iopub.status.idle":"2024-11-10T21:05:36.086566Z","shell.execute_reply.started":"2024-11-10T21:05:31.348899Z","shell.execute_reply":"2024-11-10T21:05:36.085533Z"},"trusted":true,"id":"UHIhhbOGCLAm","collapsed":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"historical = model.fit(training_dataset,\n\n    steps_per_epoch=STEPS_PER_EPOCH,\n\n    epochs=EPOCHS,\n\n    validation_data=validation_dataset)","metadata":{"id":"8zWOmo7FqSjD","outputId":"54bf755c-81ab-4167-f2ae-29b6463545f3","trusted":true,"execution":{"iopub.status.busy":"2024-11-10T21:05:42.247533Z","iopub.execute_input":"2024-11-10T21:05:42.248449Z","iopub.status.idle":"2024-11-10T21:06:55.613267Z","shell.execute_reply.started":"2024-11-10T21:05:42.248414Z","shell.execute_reply":"2024-11-10T21:06:55.611850Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Compute your predictions on the test set!\n\n\n\nThis will create a file that can be submitted to the competition.","metadata":{"_uuid":"6e881ca4-4aed-4f90-8b32-99991d607929","_cell_guid":"decd9920-8ff4-4a82-babe-033d1249a358","trusted":true,"id":"QUFtK6MdCLAm"}},{"cell_type":"code","source":"test_ds = get_test_dataset(ordered=True) # since we are splitting the dataset and iterating separately on images and ids, order matters.\n\n\n\nprint('Computing predictions...')\n\ntest_images_ds = test_ds.map(lambda image, idnum: image)\n\nprobabilities = model.predict(test_images_ds)\n\npredictions = np.argmax(probabilities, axis=-1)\n\nprint(predictions)\n\n\n","metadata":{"_uuid":"aed47871-2d46-4b6f-8647-03368eabdd11","_cell_guid":"5562fa5e-a996-4f4c-baf0-768064ede725","jupyter":{"outputs_hidden":false},"trusted":true,"id":"0uHHrRTUCLAm","outputId":"4a173f01-4c08-44d6-b11a-11b3696eb99f","collapsed":false,"execution":{"iopub.status.busy":"2024-11-10T21:08:07.709866Z","iopub.execute_input":"2024-11-10T21:08:07.710404Z","iopub.status.idle":"2024-11-10T21:08:23.052295Z","shell.execute_reply.started":"2024-11-10T21:08:07.710371Z","shell.execute_reply":"2024-11-10T21:08:23.051247Z"}},"outputs":[],"execution_count":null}]}