Python requires importing libraries and functions you need to access specific tools like science (scipy), linear algebra (numpy), and graphics (matplotlib). These libraries can be installed using the pip
command line tool. Alternatively you can install an python distribution like Anaconda or Canopy which have these and many other standard package pre-installed.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import matplotlib.pyplot as plt # plotting
from skimage.io import imread # read in images
from skimage.segmentation import mark_boundaries # mark labels
from sklearn.metrics import roc_curve, auc # roc curve tools
from skimage.color import label2rgb
import numpy as np # linear algebra / matrices
# make the notebook interactive
from ipywidgets import interact, interactive, fixed
import ipywidgets as widgets #add new widgets
from IPython.display import display
import os
base_path = '04-files'
seg_path = os.path.join(base_path,'DogVsMuffin_seg_bw.jpg')
rgb_path = os.path.join(base_path,'DogVsMuffin.jpg')
face_path = os.path.join(base_path,'DogVsMuffin_face.jpg')
seg_img = imread(seg_path)[80:520,:450]
rgb_img = imread(rgb_path)[80:520,:450,:]
face_img = imread(face_path)
print('RGB Size',rgb_img.shape,'Seg Size',seg_img.shape,'Face Size',face_img.shape)
RGB Size (440, 450, 3) Seg Size (440, 450) Face Size (111, 131, 3)
%matplotlib inline
fig, (ax1, ax2, ax3) = plt.subplots(1,3,figsize = (20,5))
ax1.imshow(rgb_img) # show the color image
ax1.set_title("Color Image")
ax2.imshow(seg_img, cmap='gray') # show the segments
ax2.set_title("Ground Truth")
ax3.imshow(mark_boundaries(rgb_img,seg_img))
ax3.set_title("Labeled Image")
Text(0.5,1,'Labeled Image')
We use the score function of taking the mean of the red green and blue channels $$ I = \frac{R+G+B}{3} $$ We then take the score by normalizing by the maximum value (since the image is 8bit this is 255) $$ s = \frac{I}{255} $$
ground_truth_labels = seg_img.flatten()>0
score_value = 1-np.mean(rgb_img.astype(np.float32),2).flatten()/255.0
fpr, tpr, _ = roc_curve(ground_truth_labels,score_value)
roc_auc = auc(fpr,tpr)
%matplotlib inline
fig, ax = plt.subplots(1,1)
ax.plot(fpr, tpr, label='ROC curve (area = %0.2f)' % roc_auc)
ax.plot([0, 1], [0, 1], 'k--')
ax.set_xlim([0.0, 1.0])
ax.set_ylim([0.0, 1.05])
ax.set_xlabel('False Positive Rate')
ax.set_ylabel('True Positive Rate')
ax.set_title('Receiver operating characteristic example')
ax.legend(loc="lower right")
<matplotlib.legend.Legend at 0x11e506e10>
We can add a filter to this process by importing a uniform_filter
and applying it before processing the image
%matplotlib inline
from scipy.ndimage.filters import uniform_filter
filter_size = 45
filtered_image = uniform_filter(np.mean(rgb_img,2),filter_size)
score_value = 1-filtered_image.astype(np.float32).flatten()/255.0
fpr2, tpr2, _ = roc_curve(ground_truth_labels,score_value)
roc_auc2 = auc(fpr2,tpr2)
fig, ax = plt.subplots(1,1)
ax.plot(fpr, tpr, label='Raw ROC curve (area = %0.2f)' % roc_auc)
ax.plot(fpr2, tpr2, label='Filtered ROC curve (area = %0.2f)' % roc_auc2)
ax.plot([0, 1], [0, 1], 'k--')
ax.set_xlim([0.0, 1.0])
ax.set_ylim([0.0, 1.05])
ax.set_xlabel('False Positive Rate')
ax.set_ylabel('True Positive Rate')
ax.set_title('Receiver operating characteristic example')
ax.legend(loc="lower right")
<matplotlib.legend.Legend at 0x11f5addd8>
from scipy.optimize import fmin
) to find the optimum parmeters for the highers area (hint: fmin finds the minimum value)from scipy.optimize import fmin
def calc_auc(rv, gv, bv, fsize):
filter_size = 45
gray_image = (rv*rgb_img[:,:,0]+gv*rgb_img[:,:,1]+bv*rgb_img[:,:,2])/(rv+gv+bv)
filtered_image = uniform_filter(gray_image,filter_size)
score_value = filtered_image.astype(np.float32).flatten()/255.0
fpr2, tpr2, _ = roc_curve(ground_truth_labels,score_value)
return {'fpr':fpr2, 'tpr':tpr2, 'auc':auc(fpr2,tpr2), 'gimg': gray_image, 'fimg': filtered_image}
# test the function to make sure it works
min_func = lambda args: 1-calc_auc(*args)['auc']
min_start = [1,1,1,20]
min_func(min_start)
0.5706766670578615
opt_res = fmin(min_func,min_start)
Optimization terminated successfully. Current function value: 0.334108 Iterations: 299 Function evaluations: 606
opt_values = calc_auc(*opt_res)
tprOpt = opt_values['tpr']
fprOpt = opt_values['fpr']
roc_aucOpt = opt_values['auc']
fig, (ax_img,ax) = plt.subplots(1,2, figsize = (20,10))
ax_img.imshow(opt_values['gimg'], cmap = 'gray')
ax_img.set_title('Transformed Color Image')
ax.plot(fpr, tpr, label='Raw ROC curve (area = %0.2f)' % roc_auc)
ax.plot(fprOpt, tprOpt, label='Optimized ROC curve (area = %0.2f)' % roc_aucOpt)
ax.plot([0, 1], [0, 1], 'k--')
ax.set_xlim([0.0, 1.0])
ax.set_ylim([0.0, 1.05])
ax.set_xlabel('False Positive Rate')
ax.set_ylabel('True Positive Rate')
ax.set_title('Receiver operating characteristic example')
ax.legend(loc="lower right")
<matplotlib.legend.Legend at 0x11f29ceb8>
Here we use non-linear approaches to improve the quality of the results
def relu(x):
return (x+np.abs(x))/2
def calc_auc_nl(rv, rm, gv, gm, bv, bm):
filter_size = 45
gray_image = (rv*relu(rgb_img[:,:,0]/255.0-rm)+gv*relu(rgb_img[:,:,1]/255.0-gm)+
bv*relu(rgb_img[:,:,2]/255.0-bm))/(rv+gv+bv)
score_value = gray_image.astype(np.float32).flatten()
fpr2, tpr2, _ = roc_curve(ground_truth_labels,score_value)
return {'fpr': fpr2, 'tpr': tpr2, 'auc':auc(fpr2,tpr2), 'gimg': gray_image, 'fimg': filtered_image}
# test the function to make sure it works
min_func = lambda args: 1-calc_auc_nl(*args)['auc']
min_start = [1,0,1,0,1,0]
min_start[0] = opt_res[0]
min_start[2] = opt_res[1]
min_start[4] = opt_res[2]
min_func(min_start)
0.38524423892945336
opt_res = fmin(min_func,min_start, maxiter = 100)
Warning: Maximum number of iterations has been exceeded.
opt_values_nl = calc_auc_nl(*opt_res)
tprOpt_nl = opt_values_nl['tpr']
fprOpt_nl = opt_values_nl['fpr']
roc_aucOpt_nl = opt_values_nl['auc']
fig, (ax_img,ax) = plt.subplots(1,2, figsize = (20,10))
ax_img.imshow(opt_values_nl['gimg'], cmap='gray')
ax_img.set_title('Transformed Color Image')
ax.plot(fpr, tpr, label='Raw ROC curve (area = %0.2f)' % roc_auc)
ax.plot(fprOpt, tprOpt, label='Optimized ROC curve (area = %0.2f)' % roc_aucOpt)
ax.plot(fprOpt_nl, tprOpt_nl, label='NL Optimized ROC curve (area = %0.2f)' % roc_aucOpt_nl)
ax.plot([0, 1], [0, 1], 'k--')
ax.set_xlim([0.0, 1.0])
ax.set_ylim([0.0, 1.05])
ax.set_xlabel('False Positive Rate')
ax.set_ylabel('True Positive Rate')
ax.set_title('Receiver operating characteristic example')
ax.legend(loc="lower right")
<matplotlib.legend.Legend at 0x11f8b27b8>
Rather than simply adjusting basic parameters, we can adjust entire arrays of information. The example below is the a convolutional neural network with one two layers
#from scipy.ndimage.filters import convolve
from scipy.signal import fftconvolve
convolve = lambda img1,img2: fftconvolve(img1,img2,mode='same')
CONV_SIZE = (10,10,1)
grey_img = np.reshape(np.mean(rgb_img,2)/255.0,(rgb_img.shape[0],rgb_img.shape[1],1))
def calc_auc_conv(rcoefs):
coefs = rcoefs.reshape(CONV_SIZE)/rcoefs.sum()
score_image = relu(convolve(grey_img,coefs))
score_value = score_image.flatten()
fpr2, tpr2, _ = roc_curve(ground_truth_labels,score_value)
return {'fpr': fpr2, 'tpr': tpr2, 'auc': auc(fpr2,tpr2), 'gimg': score_image}
# test the function to make sure it works
min_func = lambda rcoefs: 1-calc_auc_conv(rcoefs)['auc']
min_start = np.random.uniform(-1, 1, size = CONV_SIZE)
min_func(min_start)
0.6067390144774316
%%time
opt_res_conv = fmin(min_func,
min_start,
maxiter = 100)
Warning: Maximum number of iterations has been exceeded. CPU times: user 19 s, sys: 5.68 s, total: 24.7 s Wall time: 40.7 s
opt_values_conv = calc_auc_conv(opt_res_conv)
tprOpt_conv = opt_values_conv['tpr']
fprOpt_conv = opt_values_conv['fpr']
roc_aucOpt_conv = opt_values_conv['auc']
out_kernel = opt_res_conv.reshape(CONV_SIZE)/opt_res_conv.sum()
fig, ax_all = plt.subplots(1,out_kernel.shape[2])
for i,c_ax in enumerate(np.array(ax_all).flatten()):
c_ax.imshow(out_kernel[:,:,i])
c_ax.set_title(str(i))
fig, (ax_img,ax) = plt.subplots(1,2, figsize = (20,10))
ax_img.imshow(opt_values_conv['gimg'].squeeze(), cmap='gray')
ax_img.set_title('Transformed Color Image')
ax.plot(fpr, tpr, label='Raw ROC curve (area = %0.2f)' % roc_auc)
ax.plot(fprOpt, tprOpt, label='Optimized ROC curve (area = %0.2f)' % roc_aucOpt)
ax.plot(fprOpt_conv, tprOpt_conv, label='CNN Optimized ROC curve (area = %0.2f)' % roc_aucOpt_conv)
ax.plot([0, 1], [0, 1], 'k--')
ax.set_xlim([0.0, 1.0])
ax.set_ylim([0.0, 1.05])
ax.set_xlabel('False Positive Rate')
ax.set_ylabel('True Positive Rate')
ax.set_title('Receiver operating characteristic example')
ax.legend(loc="lower right")
<matplotlib.legend.Legend at 0x121698710>
Using the RGB instead of the gray value for the CNN
CONV_SIZE = (10, 10, 3)
def calc_auc_conv2d(rcoefs):
coefs = rcoefs.reshape(CONV_SIZE)/rcoefs.sum()
score_image = relu(convolve(grey_img,coefs))
score_value = score_image.flatten()
fpr2, tpr2, _ = roc_curve(ground_truth_labels,score_value)
return {'fpr': fpr2, 'tpr': tpr2, 'auc': auc(fpr2,tpr2), 'gimg': score_image}
%%time
# test the function to make sure it works
min_func = lambda rcoefs: 1-calc_auc_conv2d(rcoefs)['auc']
min_start = np.random.uniform(-1, 1, size = CONV_SIZE).ravel()
for i in range(10): min_func(min_start)
CPU times: user 1.42 s, sys: 564 ms, total: 1.98 s Wall time: 3.36 s
%%time
opt_res_conv2d = fmin(min_func, min_start, maxfun = 2, maxiter = 1)
#opt_res_conv2d = min_start
Warning: Maximum number of function evaluations has been exceeded. CPU times: user 34.8 s, sys: 13.8 s, total: 48.6 s Wall time: 1min
%matplotlib inline
opt_values_conv = calc_auc_conv2d(opt_res_conv2d)
tprOpt_conv = opt_values_conv['tpr']
fprOpt_conv = opt_values_conv['fpr']
roc_aucOpt_conv = opt_values_conv['auc']
out_kernel = opt_res_conv2d.reshape(CONV_SIZE)/opt_res_conv.sum()
fig, ax_all = plt.subplots(1,out_kernel.shape[2])
for i,c_ax in enumerate(np.array(ax_all).flatten()):
c_ax.imshow(out_kernel[:,:,i])
c_ax.set_title(str(i))
fig, (ax_img,ax) = plt.subplots(1,2, figsize = (20,10))
ax_img.imshow(mark_boundaries(opt_values_conv['gimg'].squeeze(),seg_img), cmap='gray')
ax_img.set_title('Transformed Color Image')
ax.plot(fpr, tpr, label='Raw ROC curve (area = %0.2f)' % roc_auc)
ax.plot(fprOpt, tprOpt, label='Optimized ROC curve (area = %0.2f)' % roc_aucOpt)
ax.plot(fprOpt_conv, tprOpt_conv, label='CNN Optimized ROC curve (area = %0.2f)' % roc_aucOpt_conv)
ax.plot([0, 1], [0, 1], 'k--')
ax.set_xlim([0.0, 1.0])
ax.set_ylim([0.0, 1.05])
ax.set_xlabel('False Positive Rate')
ax.set_ylabel('True Positive Rate')
ax.set_title('Receiver operating characteristic example')
ax.legend(loc="lower right")
<matplotlib.legend.Legend at 0x11f98c5c0>
from scipy.optimize import fmin
) to find the optimum parmeters for the highers area (hint: fmin finds the minimum value)