{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport IPython.display as display\nimport json","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv('../input/cassava-leaf-disease-classification/train.csv')\ntrain_df.head()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.label.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"(train_df.label.value_counts()/train_df.shape[0])*100","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels_file = '../input/cassava-leaf-disease-classification/label_num_to_disease_map.json'\nf= open(labels_file)\nlabels = json.load(f)\nf.close()\nlabels","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Data is higly imabalanced towards 'Cassava Mosaic Disease (CMD)'**"},{"metadata":{},"cell_type":"markdown","source":"## Load tf records"},{"metadata":{"trusted":true},"cell_type":"code","source":"TRAIN_FILES = tf.io.gfile.glob('../input/cassava-leaf-disease-classification/train_tfrecords'+'/*.tfrec')\nTRAIN_FILES","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"raw_dataset = tf.data.TFRecordDataset(TRAIN_FILES)\nraw_dataset","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Know the feature desction of tfrecord files"},{"metadata":{"trusted":true},"cell_type":"code","source":"for raw_record in raw_dataset.take(1):\n    example = tf.train.Example()\n    example.ParseFromString(raw_record.numpy())\n    print(example)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_feature_description = {\n    \n    \"image\": tf.io.FixedLenFeature([],tf.string),\n    \"image_name\": tf.io.FixedLenFeature([],tf.string),\n    \"target\": tf.io.FixedLenFeature([],tf.int64)\n    \n}\n\ndef _parse_image_function(example_proto):\n    return tf.io.parse_single_example(example_proto, image_feature_description)\nparsed_image_dataset = raw_dataset.map(_parse_image_function)\nparsed_image_dataset","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## 5 image exapmples from each category"},{"metadata":{"trusted":true},"cell_type":"code","source":"count = {\n    '0':0,\n    '1':0,\n    '2':0,\n    '3':0,\n    '4':0\n}\n\ni=0\n\nfor image_features in parsed_image_dataset.take(200):\n\n    if i>=25:\n        break\n    image_raw = image_features['image'].numpy()\n    label = image_features['target'].numpy().astype(str)\n    if count[label]<5:\n        i+=1\n        print(i)\n        count[label]+=1\n        print(labels[label])\n        display.display(display.Image(data=image_raw))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Next: Training and inference on TPU using TF Records https://www.kaggle.com/senkmp/simple-tpu-training-and-inference-using-tf-records"},{"metadata":{},"cell_type":"markdown","source":"Let me know if this notebook is helpful"},{"metadata":{},"cell_type":"markdown","source":"🌝"}],"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":4,"nbformat_minor":4}