{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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":30734,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Import Libraries","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"import glob\nimport torch\nfrom torch import nn\nimport tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2024-07-10T07:29:54.332961Z","iopub.execute_input":"2024-07-10T07:29:54.333610Z","iopub.status.idle":"2024-07-10T07:30:30.103045Z","shell.execute_reply.started":"2024-07-10T07:29:54.333579Z","shell.execute_reply":"2024-07-10T07:30:30.102040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Loading Data","metadata":{}},{"cell_type":"code","source":"IMG_SIZE_STRING = \"512x512\"\n# Get a list of train, val and test dataset\ntrain_files = glob.glob(f'/kaggle/input/flower-classification-with-tpus/tfrecords-jpeg-{IMG_SIZE_STRING}/train/*.tfrec')\nval_files = glob.glob(f'/kaggle/input/flower-classification-with-tpus/tfrecords-jpeg-{IMG_SIZE_STRING}/val/*.tfrec')\ntest_files = glob.glob(f'/kaggle/input/flower-classification-with-tpus/tfrecords-jpeg-{IMG_SIZE_STRING}/test/*.tfrec')\n\nlen(train_files), len(val_files), len(test_files)","metadata":{"execution":{"iopub.status.busy":"2024-07-10T07:30:30.104825Z","iopub.execute_input":"2024-07-10T07:30:30.105394Z","iopub.status.idle":"2024-07-10T07:30:30.191537Z","shell.execute_reply.started":"2024-07-10T07:30:30.105348Z","shell.execute_reply":"2024-07-10T07:30:30.190667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_feature_description = {\n    'class': tf.io.FixedLenFeature([], tf.int64),\n    'id': tf.io.FixedLenFeature([], tf.string),\n    'image': tf.io.FixedLenFeature([], tf.string),\n}\n\ndef _parse_image_function(example_proto):\n    # Parse the input tf.Example proto using the dictionary above.\n    return tf.io.parse_single_example(example_proto, train_feature_description)\n\ntrain_ids = []\ntrain_class = []\ntrain_images = []\n\nfor i in train_files:\n    train_image_dataset = tf.data.TFRecordDataset(i)\n    train_image_dataset = train_image_dataset.map(_parse_image_function)\n\n    ids = [str(id_features['id'].numpy())[2:-1] for id_features in train_image_dataset] # [2:-1] is done to remove b' from 1st and 'from last in train id names\n    train_ids = train_ids + ids\n\n    classes = [int(class_features['class'].numpy()) for class_features in train_image_dataset]\n    train_class = train_class + classes\n\n    images = [image_features['image'].numpy() for image_features in train_image_dataset]\n    train_images = train_images + images","metadata":{"execution":{"iopub.status.busy":"2024-07-10T07:33:06.117797Z","iopub.execute_input":"2024-07-10T07:33:06.119118Z","iopub.status.idle":"2024-07-10T07:33:37.723970Z","shell.execute_reply.started":"2024-07-10T07:33:06.119054Z","shell.execute_reply":"2024-07-10T07:33:37.722684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_feature_description = {\n    'class': tf.io.FixedLenFeature([], tf.int64),\n    'id': tf.io.FixedLenFeature([], tf.string),\n    'image': tf.io.FixedLenFeature([], tf.string),\n}\n\ndef _parse_image_function(example_proto):\n    # Parse the input tf.Example proto using the dictionary above.\n    return tf.io.parse_single_example(example_proto, val_feature_description)\n\nval_ids = []\nval_class = []\nval_images = []\n\nfor i in val_files:\n    val_image_dataset = tf.data.TFRecordDataset(i)\n\n    val_image_dataset = val_image_dataset.map(_parse_image_function)\n\n    ids = [str(image_features['id'].numpy())[2:-1] for image_features in val_image_dataset]\n    val_ids += ids\n\n    classes = [int(image_features['class'].numpy()) for image_features in val_image_dataset]\n    val_class += classes \n\n    images = [image_features['image'].numpy() for image_features in val_image_dataset]\n    val_images += images","metadata":{"execution":{"iopub.status.busy":"2024-07-10T07:40:45.825385Z","iopub.execute_input":"2024-07-10T07:40:45.826335Z","iopub.status.idle":"2024-07-10T07:40:54.275021Z","shell.execute_reply.started":"2024-07-10T07:40:45.826292Z","shell.execute_reply":"2024-07-10T07:40:54.273780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_feature_description = {\n    'id': tf.io.FixedLenFeature([], tf.string),\n    'image': tf.io.FixedLenFeature([], tf.string),\n}\n\ndef _parse_image_function_test(example_proto):\n    return tf.io.parse_single_example(example_proto, test_feature_description)\n\ntest_ids = []\ntest_images = []\nfor i in test_files:\n    test_image_dataset = tf.data.TFRecordDataset(i)\n    \n    test_image_dataset = test_image_dataset.map(_parse_image_function_test)\n\n    ids = [str(id_features['id'].numpy())[2:-1] for id_features in test_image_dataset]\n    test_ids = test_ids + ids\n\n    images = [image_features['image'].numpy() for image_features in test_image_dataset]\n    test_images = test_images + images","metadata":{"execution":{"iopub.status.busy":"2024-07-10T07:40:57.315402Z","iopub.execute_input":"2024-07-10T07:40:57.316465Z","iopub.status.idle":"2024-07-10T07:41:12.642597Z","shell.execute_reply.started":"2024-07-10T07:40:57.316423Z","shell.execute_reply":"2024-07-10T07:41:12.641505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import IPython.display as display\n\ndisplay.display(display.Image(data=val_images[1]))","metadata":{"execution":{"iopub.status.busy":"2024-07-10T07:41:37.551667Z","iopub.execute_input":"2024-07-10T07:41:37.552566Z","iopub.status.idle":"2024-07-10T07:41:37.558649Z","shell.execute_reply.started":"2024-07-10T07:41:37.552525Z","shell.execute_reply":"2024-07-10T07:41:37.557906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}