text_html_output_1.png
import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import pathlib

import random

import seaborn as sns

import tensorflow as tf

train_df = pd.read_csv('../input/train.csv')
test_df = pd.read_csv('../input/test.csv')
sample_sub_df = pd.read_csv('../input/train.csv')

train_images_path = '../input/train_images'
test_images_path = '../input/test_images'

train_df[train_df.id_code == '5d024177e214']

classes_dist = pd.DataFrame(train_df['diagnosis'].value_counts()/train_df.shape[0]).reset_index()

# barplot 
ax = sns.barplot(x="index", y="diagnosis", data=classes_dist)

# Imbalanced dataset with 49% - no DR, 8% proliferative - i.e most severe DR
# Model Building - Need to do oversampling for minority classes

root_path = pathlib.Path(train_images_path)
for item in root_path.iterdir():
    break

all_paths = list(root_path.glob('*.png'))
all_paths[0]

all_paths = [str(path) for path in all_paths]
random.shuffle(all_paths)

img = tf.read_file(all_paths)
img

def preprocess_image(image):
    img_tensor = tf.image.decode_png(image, channels=3)
    img_tensor = tf.cast(img_tensor, tf.float32)
    img_tensor /= 255.0
    return img_tensor
def load_and_preprocess_image(path):
    image = tf.read_file(path)
    return preprocess_image(image)

train_df.columns
train_df['image_path'] = '../input/train_images/' + train_df['id_code']

np.array(train_df['diagnosis'])

labels = tf.convert_to_tensor(np.array(train_df['diagnosis']), dtype=tf.int32)
paths = tf.convert_to_tensor(np.array(train_df['image_path']), dtype=tf.string)
image, label = tf.train.slice_input_producer([paths, labels], shuffle=True)

path_ds = tf.data.Dataset.from_tensor_slices(train_df['image_path'])

AUTOTUNE = tf.data.experimental.AUTOTUNE
image_ds = path_ds.map(load_and_preprocess_image, num_parallel_calls=AUTOTUNE)

image_ds.take(1)

import matplotlib.pyplot as plt
plt.figure(figsize=(8, 8))
for n, image in enumerate(image_ds.take(4)):
    print(image.shape)
    plt.subplot(2, 2, n + 1)
    plt.imshow(image)
    plt.grid(False)
    plt.xticks([])
    plt.yticks([])
    plt.xlabel(caption_image(all_image_paths[n]))
    plt.show()