In [1]:
%load_ext autoreload
%autoreload 2

In [192]:
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
import caffe
import cv2
import pandas as pd
from pandas import read_csv
import os
import skimage

%matplotlib inline

In [3]:
from utils import parse_folder, bbox, image_load, make_folder_tree
from utils import extract_filename_in_path, scaleRadius, pad_img, subsample_inner_circle_img

In [ ]:
from joblib import Parallel, delayed

In [4]:
root_folder = '/media/arya/3296EADC96EA9F99/DiabeticRetinopathy/'
run_mode = 'train_orig'

In [5]:
mode = run_mode
in_folder = os.path.join(root_folder, mode)
out_folder = '/home/arya/data/train'

In [6]:
names = parse_folder(in_folder, "jpeg")

In [179]:
del pdd, pdlab

In [180]:
pdlab = read_csv(os.path.join(root_folder, "labels.csv"), names=['image', 'label'], header=0)
pdlab.head()


Out[180]:
image label
0 10_left 0
1 10_right 0
2 13_left 0
3 13_right 0
4 15_left 1

In [184]:
def sample_train_val_split(names, pdlab, valsplit):
    pdlab = pdlab.reset_index()
    counts = pd.Series(pdlab.label).value_counts()
    samples_per_label = {}
    total_num = pdlab.count()['label']
    val_idx = np.zeros(total_num, dtype=np.uint8)
    for k in counts.keys():
        samples_per_label[k] = int(round(valsplit * counts[k] / total_num))
        b = np.arange(counts[k])
        np.random.shuffle(b)
        b = b[:samples_per_label[k]]
        val_idx[pdlab.index[pdlab.label==k][b]] = 1
    pdlab['val'] = pd.Series(val_idx, index=pdlab.index)
    pdlab = pdlab.set_index('image')
    return pdlab

In [185]:
pdlab = sample_train_val_split(names, pdlab, 1500)

In [130]:
def preproc_tight_bb_crop10p(img, scale):
    """Crop the extra black region, outer 10% of 
    the retinal circle and 10 pixels from top and bottom 
    of the image.
    
    Return
        out: shape (2 * scale, 2 * scale, 3)
    """
    simg = scaleRadius(img, round(scale/.9))
    suimg = subsample_inner_circle_img(simg, round(scale/.9))
    cimg = bbox(suimg)
    pimg = pad_img(cimg, (2*scale, 2*scale, 3))
    pimg[:10, :, :] = 0
    pimg[-10:, :, :] = 0
    return pimg

In [131]:
for n in names:
    img = image_load(n)
    pimg = preproc_tight_bb_crop10p(img, 128)
    plt.imshow(pimg)
    name_key = extract_filename_in_path(n)
    out_name = os.path.join(out_folder, "%d" % (pdlab.ix[name_key]['label']), '%s.jpeg' % name_key)
    make_folder_tree(out_name)
    print out_name
    break


/home/arya/data/train/0/29432_left.jpeg

Image processing trials


In [72]:
pd.ix[pd.label==4]


Out[72]:
label
image
16_left 4
16_right 4
217_left 4
217_right 4
294_left 4
294_right 4
326_left 4
326_right 4
367_left 4
367_right 4
405_left 4
405_right 4
406_left 4
406_right 4
439_right 4
458_right 4
670_left 4
670_right 4
936_left 4
936_right 4
986_left 4
1084_left 4
1084_right 4
1099_right 4
1138_left 4
1138_right 4
1350_left 4
1430_left 4
1430_right 4
1663_left 4
... ...
41761_right 4
41852_right 4
42022_right 4
42316_left 4
42358_left 4
42442_right 4
42484_left 4
42484_right 4
42815_left 4
42815_right 4
43008_left 4
43050_left 4
43050_right 4
43141_left 4
43141_right 4
43199_left 4
43199_right 4
43339_left 4
43339_right 4
43379_left 4
43379_right 4
43839_right 4
43997_left 4
43997_right 4
43998_left 4
44100_left 4
44247_left 4
44247_right 4
44249_left 4
44249_right 4

708 rows × 1 columns


In [122]:
img = image_load(os.path.join(in_folder, "1138_left.jpeg"))
plt.imshow(img);

In [129]:
scale = 128
simg = scaleRadius(img, round(scale/.9))
suimg = subsample_inner_circle_img(simg, round(scale/.9))
cimg = bbox(suimg)
print "Shape after cropping:", cimg.shape
pimg = pad_img(cimg, (2*scale, 2*scale, 3))
pimg[:10, :, :] = 0
pimg[-10:, :, :] = 0
plt.imshow(pimg);
print "Shape after padding:", pimg.shape


Shape after cropping: (254, 254, 3)
Shape after padding: (256, 256, 3)

In [126]:
gimg = cv2.cvtColor(pimg, cv2.COLOR_RGB2GRAY)
plt.imshow(gimg, cmap='gray')


Out[126]:
<matplotlib.image.AxesImage at 0x7fc029c33850>

Morphological BlackHat op to extract the nerves and spots


In [127]:
dimg = cv2.morphologyEx(gimg, cv2.MORPH_BLACKHAT, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)))
timg = cv2.morphologyEx(dimg, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)))
timg[:, :5] = 0
timg[:, -5:] = 0
timg[:5, :] = 0
timg[-5:, :] = 0
plt.imshow(timg, cmap='gray')


Out[127]:
<matplotlib.image.AxesImage at 0x7fc029b741d0>

Try and extract the optic disk


In [ ]:
from skimage import exposure, img_as_float

In [ ]:
nimg = img[:,:,1]
nimg = img_as_float(nimg)
p2, p98 = np.percentile(nimg, (2, 98))
aimg = exposure.rescale_intensity(nimg, in_range=(p2, p98))
plt.imshow(aimg, cmap='gray')

In [ ]:


In [ ]:


In [ ]: