{"cells":[{"metadata":{},"cell_type":"markdown","source":"- I could be mistaken, but maybe there is an error in the TFRecord file.\n- Images in jpeg and tfrecord files may be different."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nimport cv2\nimport math\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\n\n\nDATA_PATH = '/kaggle/input/cassava-leaf-disease-classification'\n\nJPEG_PATH = os.path.join(DATA_PATH, 'train_images')\nCSV_PATH = os.path.join(DATA_PATH, 'train.csv')\n\nfiles = sorted(os.listdir(JPEG_PATH))\ntrain_df = pd.read_csv(CSV_PATH)\ntargets = train_df['label']\n\ntrain_df.head(25)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## JPEG check"},{"metadata":{"trusted":true},"cell_type":"code","source":"def jpeg_display(directory_path):\n    fig, axes = plt.subplots(5, 5, figsize=(16, 16))\n    for i in range(25):\n        img = cv2.imread(os.path.join(directory_path, files[i]))\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        axes[i//5][i%5].imshow(img)\n        axes[i//5][i%5].set_title(f'{files[i]}: {targets[i]}')\n    plt.show()\n    \n    \njpeg_display(JPEG_PATH)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## TFRecords check"},{"metadata":{"trusted":true},"cell_type":"code","source":"def decode_image(image_data):\n    image = tf.image.decode_jpeg(image_data, channels=3)\n    image = tf.cast(image, tf.float32) / 255.0\n    return image\n\n\ndef parse_example(example):\n    LABELED_TFREC_FORMAT = {\n        'image': tf.io.FixedLenFeature([], tf.string),\n        'image_name': tf.io.FixedLenFeature([], tf.string),\n        'target': tf.io.FixedLenFeature([], tf.int64)\n    }\n    example = tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)\n    image = decode_image(example['image'])\n    label = example['image_name']\n    target = example['target']\n    return image, label, target\n\n\ndef display_one(image, title, target, subplot, red=False, titlesize=16):\n    plt.subplot(*subplot)\n    plt.axis('off')\n    plt.imshow(image)\n    plt.title(f'{title}: {target}')\n    return (subplot[0], subplot[1], subplot[2]+1)\n\n\ndef display_batch_of_images(databatch):\n    images, labels, targets = databatch\n    images = images.numpy()\n    labels = labels.numpy()\n    targets = targets.numpy()\n    if labels is None:\n        labels = [None for _ in enumerate(images)]\n    if targets is None:\n        targets = [None for _ in enumerate(targets)]\n        \n    # auto-squaring: this will drop data that does not fit into square or square-ish rectangle\n    rows = int(math.sqrt(len(images)))\n    cols = len(images)//rows\n        \n    # size and spacing\n    FIGSIZE = 13.0\n    SPACING = 0.2\n    subplot=(rows, cols, 1)\n    if rows < cols:\n        plt.figure(figsize=(FIGSIZE,FIGSIZE/cols*rows))\n    else:\n        plt.figure(figsize=(FIGSIZE/rows*cols,FIGSIZE))\n    \n    # display\n    for i, (image, label, target) in enumerate(zip(images[:rows*cols], labels[:rows*cols], targets[:rows*cols])):\n        title = label\n        title = title.decode('utf-8')\n        correct = True\n        dynamic_titlesize = FIGSIZE*SPACING/max(rows,cols)*40+3 # magic formula tested to work from 1x1 to 10x10 images\n        subplot = display_one(image, title, target, subplot, not correct, titlesize=dynamic_titlesize)\n    \n    #layout\n    plt.tight_layout()\n    if label is None and predictions is None:\n        plt.subplots_adjust(wspace=0.2, hspace=0.2)\n    else:\n        plt.subplots_adjust(wspace=SPACING, hspace=SPACING)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"file = '/kaggle/input/cassava-leaf-disease-classification/train_tfrecords/ld_train00-1338.tfrec'\ndataset = tf.data.TFRecordDataset([file]).map(parse_example).batch(25)\ndata = iter(dataset)\ndisplay_batch_of_images(next(data))","execution_count":null,"outputs":[]}],"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}