In [22]:
%pylab inline
import warnings
warnings.filterwarnings("ignore")
import nolearn
from nolearn.lasagne import NeuralNet
import readdata
import lasagne
from lasagne import layers
from sklearn import metrics
import detectobjects as det
import os.path
from scipy import misc
import cv2
from progress_bar import ProgressBar
import shapefeatures
from sklearn import ensemble


Populating the interactive namespace from numpy and matplotlib
WARNING: pylab import has clobbered these variables: ['f', 'det', 'clf']
`%matplotlib` prevents importing * from pylab and numpy

In [23]:
opts = {'img_dir': '../data/plasmodium/',
        'annotation_dir': '../data/plasmodium/',
        'detection_probability_threshold': 0.5,
        'detection_overlap_threshold': 0.3, 
        'gauss': 1,
        'patch_size': (50,50),
        'image_downsample' : 2,
        'detection_step': 5,
        'patch_creation_step': 40,
        'object_class': None,
        'negative_training_discard_rate': .9
       }
opts['patch_stride_training'] = int(opts['patch_size'][0]*.25)

In [24]:
trainfiles, valfiles, testfiles = readdata.create_sets(opts['img_dir'], train_set_proportion=.5, 
                                                  test_set_proportion=.5,
                                                  val_set_proportion=0)

train_y, train_X = readdata.create_patches(trainfiles, opts['annotation_dir'], opts['img_dir'], opts['patch_size'][0], opts['patch_stride_training'], grayscale=False, progressbar=True, downsample=opts['image_downsample'], objectclass=opts['object_class'], negative_discard_rate=opts['negative_training_discard_rate'])
test_y, test_X = readdata.create_patches(testfiles,  opts['annotation_dir'], opts['img_dir'], opts['patch_size'][0], opts['patch_stride_training'], grayscale=False, progressbar=True, downsample=opts['image_downsample'], objectclass=opts['object_class'], negative_discard_rate=opts['negative_training_discard_rate'])

# Cut down on disproportionately large numbers of negative patches
train_X, train_y = readdata.balance(train_X, train_y, mult_neg=100)
#test_X, test_y = readdata.balance(test_X, test_y, mult_neg=100)

# Create rotated and flipped versions of the positive patches
train_X, train_y = readdata.augment_positives(train_X, train_y)
test_X, test_y = readdata.augment_positives(test_X, test_y)

print '\n'
print '%d positive training examples, %d negative training examples' % (sum(train_y), len(train_y)-sum(train_y))
print '%d positive testing examples, %d negative testing examples' % (sum(test_y), len(test_y)-sum(test_y))
print '%d patches (%.1f%% positive)' % (len(train_y)+len(test_y), 100.*((sum(train_y)+sum(test_y))/(len(train_y)+len(test_y))))


[****************100%******************]  1352 of 1352 complete 

184616 positive training examples, 507549 negative training examples
187896 positive testing examples, 507140 negative testing examples
1387201 patches (26.9% positive)

View a random selection of positive and negative patches to see if they look right


In [25]:
N_samples_to_display = 10
pos_indices = np.where(test_y)[0]
pos_indices = pos_indices[np.random.permutation(len(pos_indices))]
for i in range(N_samples_to_display):
    plt.subplot(2,N_samples_to_display,i+1)
    example_pos = test_X[pos_indices[i],:,:,:]
    example_pos = np.swapaxes(example_pos,0,2)
    plt.imshow(example_pos)
    plt.tick_params(axis='both', which='both', bottom='off', top='off', labelbottom='off', right='off', left='off', labelleft='off')    

neg_indices = np.where(test_y==0)[0]
neg_indices = neg_indices[np.random.permutation(len(neg_indices))]
for i in range(N_samples_to_display,2*N_samples_to_display):
    plt.subplot(2,N_samples_to_display,i+1)
    example_neg = test_X[neg_indices[i],:,:,:]
    example_neg = np.swapaxes(example_neg,0,2)
    plt.imshow(example_neg)
    plt.tick_params(axis='both', which='both', bottom='off', top='off', labelbottom='off', right='off', left='off', labelleft='off')
plt.gcf().set_size_inches(1.5*N_samples_to_display,3)


CNN training


In [ ]:
def CNN(n_epochs):
    net1 = NeuralNet(
        layers=[
        ('input', layers.InputLayer),
        ('conv1', layers.Conv2DLayer),      #Convolutional layer.  Params defined below
        ('pool1', layers.MaxPool2DLayer),   # Like downsampling, for execution speed
        ('conv2', layers.Conv2DLayer),
        ('hidden3', layers.DenseLayer),
        ('output', layers.DenseLayer),
        ],
        
    input_shape=(None, 3, opts['patch_size'][0]/opts['image_downsample'], 
                 opts['patch_size'][0]/opts['image_downsample']),
    conv1_num_filters=7, 
    conv1_filter_size=(5, 5), 
    conv1_nonlinearity=lasagne.nonlinearities.rectify,
        
    pool1_pool_size=(2, 2),
        
    conv2_num_filters=12, 
    conv2_filter_size=(2, 2),    
    conv2_nonlinearity=lasagne.nonlinearities.rectify,
        
    hidden3_num_units=500,
    output_num_units=2, 
    output_nonlinearity=lasagne.nonlinearities.softmax,

    update_learning_rate=0.0001,
    update_momentum=0.9,

    max_epochs=n_epochs,
    verbose=1,
    )
    return net1

cnn = CNN(500).fit(train_X, train_y)


# Neural Network with 488382 learnable parameters

## Layer information

  #  name     size
---  -------  -------
  0  input    3x25x25
  1  conv1    7x21x21
  2  pool1    7x10x10
  3  conv2    12x9x9
  4  hidden3  500
  5  output   2

  epoch    train loss    valid loss    train/val    valid acc  dur
-------  ------------  ------------  -----------  -----------  ------
      1       0.36777       0.15799      2.32782      0.93933  28.52s
      2       0.12145       0.10520      1.15439      0.96125  28.90s
      3       0.09681       0.09278      1.04338      0.96664  28.26s
      4       0.08882       0.08770      1.01280      0.96863  29.03s
      5       0.08398       0.08470      0.99148      0.96944  28.79s
      6       0.08053       0.08179      0.98465      0.97086  30.24s
      7       0.07790       0.08068      0.96560      0.97117  28.22s
      8       0.07585       0.07968      0.95190      0.97145  28.95s
      9       0.07412       0.07874      0.94132      0.97165  28.29s
     10       0.07269       0.07930      0.91661      0.97158  29.38s
     11       0.07142       0.07742      0.92251      0.97226  35.95s
     12       0.07033       0.07703      0.91312      0.97252  37.72s
     13       0.06937       0.07682      0.90292      0.97266  33.58s
     14       0.06849       0.07669      0.89299      0.97258  28.73s
     15       0.06768       0.07562      0.89500      0.97305  29.28s
     16       0.06699       0.07437      0.90078      0.97342  28.86s
     17       0.06635       0.07446      0.89104      0.97347  28.28s
     18       0.06572       0.07435      0.88393      0.97341  28.90s
     19       0.06516       0.07397      0.88084      0.97349  35.94s
     20       0.06463       0.07329      0.88184      0.97378  38.06s
     21       0.06417       0.07343      0.87390      0.97380  37.46s
     22       0.06374       0.07285      0.87490      0.97400  37.92s
     23       0.06330       0.07229      0.87566      0.97419  37.49s
     24       0.06289       0.07222      0.87083      0.97421  38.04s
     25       0.06251       0.07174      0.87139      0.97445  37.46s
     26       0.06216       0.07062      0.88028      0.97484  38.07s
     27       0.06182       0.07091      0.87177      0.97466  37.36s
     28       0.06149       0.06962      0.88325      0.97539  38.14s
     29       0.06119       0.06944      0.88112      0.97547  37.42s
     30       0.06091       0.06929      0.87909      0.97553  38.53s
     31       0.06061       0.06866      0.88279      0.97580  38.38s
     32       0.06037       0.06849      0.88133      0.97582  37.56s
     33       0.06013       0.06826      0.88084      0.97601  37.36s
     34       0.05984       0.06802      0.87974      0.97601  38.88s
     35       0.05960       0.06764      0.88120      0.97631  38.74s
     36       0.05936       0.06817      0.87075      0.97607  40.44s
     37       0.05915       0.06810      0.86866      0.97614  37.57s
     38       0.05893       0.06752      0.87272      0.97635  39.24s
     39       0.05872       0.06759      0.86874      0.97630  36.58s
     40       0.05852       0.06781      0.86294      0.97609  29.01s
     41       0.05831       0.06711      0.86881      0.97646  28.24s
     42       0.05811       0.06733      0.86303      0.97626  28.95s
     43       0.05791       0.06666      0.86867      0.97662  28.26s
     44       0.05774       0.06728      0.85812      0.97626  28.97s
     45       0.05755       0.06682      0.86130      0.97662  28.25s
     46       0.05739       0.06661      0.86155      0.97669  28.96s
     47       0.05721       0.06666      0.85823      0.97665  28.28s
     48       0.05706       0.06653      0.85770      0.97666  28.97s
     49       0.05687       0.06641      0.85635      0.97677  28.47s
     50       0.05669       0.06641      0.85368      0.97659  29.92s
     51       0.05652       0.06597      0.85682      0.97679  28.33s
     52       0.05637       0.06557      0.85968      0.97707  28.46s
     53       0.05622       0.06587      0.85347      0.97680  28.16s
     54       0.05605       0.06538      0.85737      0.97714  28.90s
     55       0.05589       0.06509      0.85867      0.97735  28.14s
     56       0.05573       0.06521      0.85464      0.97703  28.67s
     57       0.05558       0.06530      0.85107      0.97700  28.10s
     58       0.05545       0.06523      0.85004      0.97709  28.74s
     59       0.05528       0.06523      0.84744      0.97710  28.12s
     60       0.05514       0.06539      0.84323      0.97696  28.62s
     61       0.05500       0.06459      0.85150      0.97725  28.08s
     62       0.05488       0.06464      0.84898      0.97718  28.54s
     63       0.05475       0.06440      0.85017      0.97734  28.08s
     64       0.05462       0.06455      0.84628      0.97726  28.55s
     65       0.05448       0.06388      0.85284      0.97756  28.11s
     66       0.05438       0.06394      0.85049      0.97762  28.59s
     67       0.05424       0.06418      0.84506      0.97748  28.06s
     68       0.05411       0.06471      0.83619      0.97729  28.71s
     69       0.05399       0.06426      0.84023      0.97751  28.15s
     70       0.05387       0.06434      0.83733      0.97723  28.54s
     71       0.05376       0.06436      0.83533      0.97736  28.06s
     72       0.05365       0.06465      0.82981      0.97737  28.61s
     73       0.05354       0.06487      0.82535      0.97731  28.11s
     74       0.05344       0.06420      0.83230      0.97742  28.83s
     75       0.05332       0.06401      0.83312      0.97746  30.13s
     76       0.05321       0.06393      0.83226      0.97755  38.21s
     77       0.05312       0.06434      0.82550      0.97725  37.18s
     78       0.05300       0.06411      0.82675      0.97742  38.28s
     79       0.05290       0.06434      0.82219      0.97739  37.48s
     80       0.05281       0.06414      0.82327      0.97749  38.39s
     81       0.05272       0.06394      0.82439      0.97760  37.75s
     82       0.05261       0.06397      0.82251      0.97744  38.26s
     83       0.05250       0.06393      0.82123      0.97752  37.69s
     84       0.05239       0.06405      0.81789      0.97752  38.17s
     85       0.05229       0.06424      0.81402      0.97739  37.62s
     86       0.05222       0.06445      0.81023      0.97730  38.12s
     87       0.05212       0.06432      0.81030      0.97739  37.45s
     88       0.05202       0.06437      0.80814      0.97741  38.32s
     89       0.05193       0.06398      0.81171      0.97746  37.57s
     90       0.05184       0.06432      0.80592      0.97744  38.14s
     91       0.05176       0.06418      0.80644      0.97749  37.39s
     92       0.05166       0.06412      0.80561      0.97752  38.32s
     93       0.05158       0.06419      0.80353      0.97757  37.76s
     94       0.05150       0.06417      0.80251      0.97752  38.18s
     95       0.05141       0.06445      0.79769      0.97732  37.69s
     96       0.05132       0.06417      0.79971      0.97741  34.99s
     97       0.05124       0.06424      0.79769      0.97744  28.14s
     98       0.05117       0.06424      0.79661      0.97740  28.88s
     99       0.05109       0.06425      0.79507      0.97741  28.16s
    100       0.05101       0.06448      0.79116      0.97723  28.85s
    101       0.05093       0.06466      0.78771      0.97721  28.18s
    102       0.05088       0.06480      0.78516      0.97713  28.81s
    103       0.05080       0.06448      0.78797      0.97739  28.83s
    104       0.05073       0.06437      0.78811      0.97733  29.43s
    105       0.05066       0.06400      0.79154      0.97739  28.38s
    106       0.05060       0.06441      0.78557      0.97723  29.07s
    107       0.05052       0.06444      0.78405      0.97734  28.36s
    108       0.05044       0.06448      0.78223      0.97730  29.03s
    109       0.05038       0.06455      0.78052      0.97724  28.34s
    110       0.05032       0.06458      0.77916      0.97714  28.99s
    111       0.05024       0.06475      0.77588      0.97719  28.33s
    112       0.05018       0.06450      0.77803      0.97726  28.98s
    113       0.05011       0.06448      0.77718      0.97722  28.38s
    114       0.05004       0.06424      0.77892      0.97735  29.04s
    115       0.04997       0.06419      0.77847      0.97742  28.35s
    116       0.04991       0.06428      0.77647      0.97744  29.04s
    117       0.04983       0.06440      0.77374      0.97731  28.38s
    118       0.04977       0.06452      0.77141      0.97741  29.04s
    119       0.04971       0.06473      0.76793      0.97735  28.35s
    120       0.04963       0.06429      0.77198      0.97765  29.02s
    121       0.04957       0.06445      0.76908      0.97747  28.34s
    122       0.04947       0.06439      0.76837      0.97745  29.01s
    123       0.04943       0.06381      0.77468      0.97764  28.36s
    124       0.04936       0.06426      0.76824      0.97750  29.03s
    125       0.04929       0.06407      0.76924      0.97756  28.31s
    126       0.04925       0.06418      0.76737      0.97747  29.02s
    127       0.04919       0.06434      0.76443      0.97750  28.33s
    128       0.04913       0.06414      0.76597      0.97755  29.03s
    129       0.04903       0.06433      0.76218      0.97757  28.29s
    130       0.04898       0.06421      0.76280      0.97762  30.87s
    131       0.04891       0.06437      0.75992      0.97751  28.27s
    132       0.04885       0.06421      0.76074      0.97760  28.96s
    133       0.04879       0.06414      0.76070      0.97762  28.25s
    134       0.04872       0.06424      0.75835      0.97767  29.04s
    135       0.04865       0.06395      0.76080      0.97776  28.26s
    136       0.04861       0.06398      0.75970      0.97773  28.96s
    137       0.04853       0.06441      0.75340      0.97750  28.29s
    138       0.04849       0.06422      0.75497      0.97766  29.00s
    139       0.04841       0.06429      0.75299      0.97763  28.27s
    140       0.04835       0.06395      0.75611      0.97790  28.99s
    141       0.04829       0.06405      0.75402      0.97793  28.28s
    142       0.04826       0.06408      0.75302      0.97769  28.98s
    143       0.04819       0.06375      0.75587      0.97774  28.27s
    144       0.04812       0.06382      0.75407      0.97771  29.01s
    145       0.04808       0.06378      0.75371      0.97775  28.29s
    146       0.04801       0.06367      0.75412      0.97780  28.97s
    147       0.04796       0.06390      0.75050      0.97763  28.27s
    148       0.04792       0.06370      0.75221      0.97772  28.93s
    149       0.04788       0.06396      0.74848      0.97769  28.30s
    150       0.04779       0.06399      0.74677      0.97773  28.90s
    151       0.04774       0.06368      0.74974      0.97777  28.27s
    152       0.04769       0.06374      0.74817      0.97777  28.96s
    153       0.04766       0.06381      0.74685      0.97773  28.23s
    154       0.04760       0.06381      0.74609      0.97776  28.97s
    155       0.04754       0.06402      0.74261      0.97772  28.30s
    156       0.04750       0.06390      0.74334      0.97778  29.09s
    157       0.04743       0.06371      0.74447      0.97782  28.25s
    158       0.04738       0.06375      0.74323      0.97783  28.95s
    159       0.04734       0.06391      0.74068      0.97775  28.26s
    160       0.04728       0.06380      0.74109      0.97779  28.94s
    161       0.04724       0.06382      0.74015      0.97775  28.27s
    162       0.04719       0.06378      0.73984      0.97779  28.95s
    163       0.04714       0.06379      0.73901      0.97781  28.25s
    164       0.04708       0.06411      0.73443      0.97759  28.90s
    165       0.04705       0.06383      0.73704      0.97780  28.20s
    166       0.04700       0.06393      0.73511      0.97783  28.83s
    167       0.04694       0.06399      0.73364      0.97775  28.25s
    168       0.04691       0.06409      0.73196      0.97778  28.93s
    169       0.04687       0.06403      0.73201      0.97775  28.27s
    170       0.04681       0.06430      0.72804      0.97769  28.95s
    171       0.04677       0.06412      0.72939      0.97777  28.26s
    172       0.04672       0.06431      0.72641      0.97771  28.93s
    173       0.04667       0.06423      0.72668      0.97780  28.26s
    174       0.04664       0.06449      0.72316      0.97765  28.97s
    175       0.04658       0.06459      0.72108      0.97755  28.27s
    176       0.04655       0.06455      0.72112      0.97745  28.97s
    177       0.04651       0.06441      0.72206      0.97761  28.29s
    178       0.04643       0.06420      0.72333      0.97773  28.99s
    179       0.04639       0.06448      0.71942      0.97755  28.23s
    180       0.04636       0.06472      0.71642      0.97754  28.97s
    181       0.04632       0.06424      0.72097      0.97762  28.28s
    182       0.04628       0.06432      0.71954      0.97763  28.98s
    183       0.04625       0.06471      0.71472      0.97750  28.25s
    184       0.04620       0.06418      0.71979      0.97763  28.96s
    185       0.04614       0.06433      0.71715      0.97770  28.29s
    186       0.04609       0.06460      0.71353      0.97753  28.98s
    187       0.04606       0.06468      0.71218      0.97753  28.27s
    188       0.04601       0.06437      0.71485      0.97758  28.96s
    189       0.04597       0.06442      0.71355      0.97753  28.25s
    190       0.04593       0.06435      0.71365      0.97751  28.99s
    191       0.04588       0.06382      0.71893      0.97769  28.29s
    192       0.04585       0.06440      0.71199      0.97752  28.92s
    193       0.04579       0.06434      0.71177      0.97749  28.26s
    194       0.04575       0.06431      0.71139      0.97760  28.96s
    195       0.04571       0.06386      0.71578      0.97767  28.24s
    196       0.04567       0.06414      0.71204      0.97756  28.97s
    197       0.04562       0.06413      0.71131      0.97760  28.25s
    198       0.04558       0.06377      0.71474      0.97768  28.96s
    199       0.04552       0.06376      0.71402      0.97780  28.27s
    200       0.04549       0.06414      0.70920      0.97752  28.95s
    201       0.04545       0.06418      0.70814      0.97757  28.28s
    202       0.04541       0.06407      0.70879      0.97759  28.98s
    203       0.04537       0.06400      0.70892      0.97763  28.26s
    204       0.04532       0.06383      0.71010      0.97776  28.87s
    205       0.04529       0.06403      0.70731      0.97770  28.25s
    206       0.04524       0.06430      0.70355      0.97757  28.98s
    207       0.04520       0.06384      0.70794      0.97768  28.28s
    208       0.04516       0.06391      0.70656      0.97757  28.99s
    209       0.04512       0.06413      0.70346      0.97757  28.29s
    210       0.04508       0.06416      0.70265      0.97767  28.85s
    211       0.04504       0.06415      0.70206      0.97755  28.23s
    212       0.04502       0.06417      0.70159      0.97760  28.76s
    213       0.04497       0.06411      0.70145      0.97762  28.14s
    214       0.04492       0.06393      0.70275      0.97770  28.47s
    215       0.04486       0.06390      0.70203      0.97770  28.14s
    216       0.04483       0.06400      0.70042      0.97767  28.75s
    217       0.04480       0.06407      0.69920      0.97776  28.25s
    218       0.04476       0.06395      0.69995      0.97773  28.69s
    219       0.04472       0.06389      0.70000      0.97784  28.20s
    220       0.04469       0.06399      0.69835      0.97783  28.86s
    221       0.04464       0.06398      0.69767      0.97789  28.06s
    222       0.04462       0.06382      0.69906      0.97793  28.73s
    223       0.04456       0.06381      0.69838      0.97791  28.16s
    224       0.04452       0.06379      0.69799      0.97791  28.84s
    225       0.04450       0.06378      0.69764      0.97791  28.22s
    226       0.04447       0.06391      0.69574      0.97786  28.71s
    227       0.04442       0.06373      0.69693      0.97799  28.11s
    228       0.04438       0.06359      0.69786      0.97819  28.62s
    229       0.04433       0.06350      0.69824      0.97811  28.23s
    230       0.04430       0.06340      0.69879      0.97802  28.67s
    231       0.04427       0.06332      0.69912      0.97810  28.17s
    232       0.04423       0.06364      0.69506      0.97799  28.77s
    233       0.04418       0.06356      0.69510      0.97814  28.17s
    234       0.04415       0.06356      0.69465      0.97797  28.76s
    235       0.04410       0.06345      0.69505      0.97815  28.22s
    236       0.04408       0.06359      0.69315      0.97808  28.85s
    237       0.04403       0.06351      0.69323      0.97802  28.21s
    238       0.04401       0.06329      0.69538      0.97813  28.78s
    239       0.04396       0.06281      0.69979      0.97821  28.31s
    240       0.04394       0.06336      0.69345      0.97817  31.64s
    241       0.04390       0.06316      0.69503      0.97817  37.38s
    242       0.04385       0.06336      0.69201      0.97818  34.29s
    243       0.04382       0.06315      0.69400      0.97822  37.31s
    244       0.04378       0.06338      0.69086      0.97822  35.41s
    245       0.04375       0.06320      0.69221      0.97824  31.90s
    246       0.04372       0.06352      0.68826      0.97806  37.58s
    247       0.04368       0.06333      0.68963      0.97819  37.20s
    248       0.04366       0.06328      0.68989      0.97828  37.82s
    249       0.04363       0.06328      0.68947      0.97825  37.26s
    250       0.04360       0.06326      0.68914      0.97822  38.03s
    251       0.04355       0.06334      0.68761      0.97826  37.22s
    252       0.04353       0.06332      0.68735      0.97824  38.06s
    253       0.04350       0.06334      0.68673      0.97824  37.34s
    254       0.04345       0.06335      0.68589      0.97821  38.03s
    255       0.04341       0.06290      0.69010      0.97835  37.57s
    256       0.04338       0.06295      0.68913      0.97840  38.16s
    257       0.04335       0.06286      0.68964      0.97843  37.51s
    345       0.04075       0.06312      0.64565      0.97879  29.70s
    346       0.04071       0.06329      0.64330      0.97882  29.21s
    347       0.04068       0.06328      0.64287      0.97874  28.54s
    348       0.04065       0.06323      0.64291      0.97878  29.33s
    349       0.04063       0.06312      0.64370      0.97887  28.58s
    350       0.04061       0.06312      0.64344      0.97892  29.50s
    351       0.04059       0.06304      0.64386      0.97885  28.63s
    352       0.04056       0.06314      0.64229      0.97888  29.32s
    353       0.04053       0.06309      0.64239      0.97886  28.61s
    354       0.04051       0.06313      0.64167      0.97884  29.21s
    355       0.04048       0.06343      0.63825      0.97878  28.40s
    356       0.04045       0.06331      0.63895      0.97879  28.97s
    357       0.04041       0.06313      0.64007      0.97879  28.51s
    358       0.04040       0.06343      0.63691      0.97871  28.85s
    359       0.04037       0.06320      0.63874      0.97882  28.42s
    360       0.04033       0.06334      0.63677      0.97887  28.80s
    361       0.04031       0.06334      0.63642      0.97879  28.30s
    362       0.04027       0.06327      0.63647      0.97888  28.77s
    363       0.04023       0.06335      0.63509      0.97890  28.38s
    364       0.04022       0.06341      0.63429      0.97877  28.84s
    365       0.04019       0.06356      0.63231      0.97872  28.35s
    366       0.04018       0.06343      0.63339      0.97874  28.75s
    367       0.04014       0.06348      0.63237      0.97876  28.27s
    368       0.04011       0.06340      0.63274      0.97877  28.71s
    369       0.04008       0.06322      0.63400      0.97883  28.21s
    370       0.04007       0.06315      0.63455      0.97881  28.73s
    371       0.04004       0.06338      0.63186      0.97884  28.20s
    372       0.04001       0.06324      0.63265      0.97885  28.59s
    373       0.03998       0.06304      0.63410      0.97898  28.23s
    374       0.03995       0.06305      0.63365      0.97885  28.75s
    375       0.03993       0.06312      0.63255      0.97892  28.35s
    376       0.03990       0.06313      0.63201      0.97888  28.77s
    377       0.03986       0.06323      0.63046      0.97887  28.35s
    378       0.03984       0.06330      0.62939      0.97890  28.91s
    379       0.03981       0.06338      0.62811      0.97890  28.44s
    380       0.03978       0.06291      0.63229      0.97895  29.03s
    381       0.03974       0.06315      0.62934      0.97881  28.64s
    382       0.03973       0.06304      0.63018      0.97896  29.26s
    383       0.03971       0.06329      0.62747      0.97886  28.36s
    384       0.03969       0.06314      0.62855      0.97892  28.57s
    385       0.03966       0.06324      0.62708      0.97881  28.05s
    386       0.03963       0.06322      0.62676      0.97882  28.52s
    387       0.03961       0.06331      0.62560      0.97880  28.16s
    388       0.03957       0.06318      0.62622      0.97883  28.64s
    389       0.03956       0.06308      0.62711      0.97887  28.18s
    390       0.03953       0.06287      0.62868      0.97896  28.60s
    391       0.03950       0.06305      0.62645      0.97888  28.09s
    392       0.03947       0.06298      0.62667      0.97886  28.65s
    393       0.03944       0.06313      0.62485      0.97890  28.22s
    394       0.03941       0.06313      0.62423      0.97883  28.79s
    395       0.03939       0.06318      0.62351      0.97893  28.23s
    396       0.03937       0.06324      0.62256      0.97885  28.66s
    397       0.03936       0.06325      0.62218      0.97882  28.12s
    398       0.03932       0.06324      0.62177      0.97884  28.85s
    399       0.03930       0.06336      0.62024      0.97889  28.11s
    400       0.03929       0.06312      0.62240      0.97891  28.60s
    401       0.03925       0.06320      0.62111      0.97893  28.10s
    402       0.03923       0.06350      0.61777      0.97877  28.56s
    403       0.03921       0.06335      0.61899      0.97892  28.14s
    404       0.03919       0.06360      0.61618      0.97891  28.74s
    405       0.03918       0.06351      0.61699      0.97892  28.03s
    406       0.03915       0.06347      0.61675      0.97887  28.56s
    407       0.03913       0.06340      0.61726      0.97887  28.06s
    408       0.03911       0.06343      0.61653      0.97891  28.63s
    409       0.03907       0.06337      0.61650      0.97887  28.09s
    410       0.03906       0.06349      0.61528      0.97884  28.61s
    411       0.03904       0.06363      0.61347      0.97880  27.99s
    412       0.03900       0.06353      0.61391      0.97887  28.45s
    413       0.03899       0.06343      0.61463      0.97892  27.99s
    414       0.03896       0.06347      0.61383      0.97890  28.59s
    415       0.03894       0.06354      0.61281      0.97895  28.06s
    416       0.03891       0.06352      0.61261      0.97883  28.52s
    417       0.03888       0.06345      0.61284      0.97887  27.94s
    418       0.03886       0.06363      0.61080      0.97892  28.32s
    419       0.03884       0.06371      0.60963      0.97883  28.00s
    420       0.03882       0.06360      0.61041      0.97887  28.63s
    421       0.03880       0.06350      0.61102      0.97890  28.04s
    422       0.03878       0.06368      0.60897      0.97878  28.42s
    423       0.03875       0.06363      0.60895      0.97880  28.06s
    424       0.03873       0.06370      0.60808      0.97874  28.48s
    425       0.03872       0.06358      0.60906      0.97882  28.14s
    426       0.03869       0.06385      0.60593      0.97881  28.59s
    427       0.03867       0.06379      0.60623      0.97874  28.14s
    428       0.03865       0.06360      0.60768      0.97884  28.48s
    429       0.03862       0.06380      0.60535      0.97883  28.13s
    430       0.03861       0.06371      0.60604      0.97885  28.73s
    431       0.03859       0.06390      0.60385      0.97870  28.05s
    432       0.03855       0.06396      0.60282      0.97866  28.75s
    433       0.03854       0.06401      0.60208      0.97882  28.19s
    434       0.03852       0.06413      0.60067      0.97875  28.93s
    435       0.03850       0.06392      0.60236      0.97878  28.19s
    436       0.03848       0.06389      0.60236      0.97884  28.58s
    437       0.03846       0.06393      0.60153      0.97877  28.13s
    438       0.03844       0.06395      0.60115      0.97888  28.59s
    439       0.03842       0.06408      0.59956      0.97874  28.06s
    440       0.03840       0.06410      0.59897      0.97878  28.59s
    441       0.03838       0.06406      0.59905      0.97885  28.10s
    442       0.03835       0.06414      0.59789      0.97875  28.57s
    443       0.03832       0.06399      0.59894      0.97882  28.09s
    444       0.03829       0.06408      0.59760      0.97885  28.55s
    445       0.03827       0.06408      0.59716      0.97882  28.08s
    446       0.03825       0.06421      0.59561      0.97874  28.64s
    447       0.03823       0.06409      0.59653      0.97896  28.20s
    448       0.03822       0.06419      0.59534      0.97890  28.51s
    449       0.03819       0.06417      0.59508      0.97884  28.25s
    450       0.03816       0.06436      0.59293      0.97879  28.67s
    451       0.03814       0.06401      0.59589      0.97891  28.23s
    452       0.03811       0.06423      0.59335      0.97884  28.82s
    453       0.03811       0.06433      0.59237      0.97882  28.25s
    454       0.03808       0.06420      0.59324      0.97892  28.79s
    455       0.03804       0.06435      0.59119      0.97885  28.24s
    456       0.03802       0.06453      0.58924      0.97877  29.01s
    457       0.03799       0.06434      0.59049      0.97885  28.06s
    458       0.03799       0.06417      0.59201      0.97891  28.78s
    459       0.03797       0.06431      0.59046      0.97878  28.00s
    460       0.03794       0.06455      0.58783      0.97872  28.34s
    461       0.03792       0.06469      0.58626      0.97879  28.14s
    462       0.03790       0.06437      0.58885      0.97885  28.49s
    463       0.03788       0.06451      0.58721      0.97876  28.06s
    464       0.03785       0.06421      0.58953      0.97886  28.43s
    465       0.03784       0.06467      0.58510      0.97877  28.08s
    466       0.03781       0.06472      0.58427      0.97870  28.44s
    467       0.03780       0.06456      0.58546      0.97875  28.16s
    468       0.03776       0.06448      0.58559      0.97884  28.56s
    469       0.03775       0.06467      0.58384      0.97871  28.00s
    470       0.03772       0.06449      0.58493      0.97882  28.55s
    471       0.03768       0.06460      0.58334      0.97878  28.02s
    472       0.03767       0.06458      0.58332      0.97883  28.59s
    473       0.03765       0.06450      0.58378      0.97882  28.07s
    474       0.03764       0.06465      0.58223      0.97882  28.41s
    475       0.03762       0.06451      0.58313      0.97887  28.04s
    476       0.03759       0.06482      0.57987      0.97873  28.50s
    477       0.03757       0.06466      0.58110      0.97882  28.00s
    478       0.03756       0.06464      0.58102      0.97879  28.60s
    479       0.03753       0.06465      0.58054      0.97879  28.06s
    480       0.03750       0.06464      0.58021      0.97886  28.49s
    481       0.03748       0.06505      0.57613      0.97871  27.99s
    482       0.03746       0.06470      0.57892      0.97884  28.52s
    483       0.03745       0.06484      0.57751      0.97877  28.01s
    484       0.03742       0.06484      0.57702      0.97879  28.75s
    485       0.03739       0.06488      0.57635      0.97876  28.09s
    486       0.03739       0.06478      0.57714      0.97894  28.64s
    487       0.03735       0.06485      0.57598      0.97884  28.08s
    488       0.03733       0.06499      0.57440      0.97881  28.78s
    489       0.03731       0.06488      0.57499      0.97887  28.09s
    490       0.03730       0.06501      0.57371      0.97880  28.77s
    491       0.03726       0.06506      0.57274      0.97887  28.12s
    492       0.03725       0.06509      0.57230      0.97887  28.74s
    493       0.03723       0.06495      0.57319      0.97885  28.11s
    494       0.03721       0.06517      0.57100      0.97881  28.74s
    495       0.03718       0.06511      0.57105      0.97880  28.27s
    496       0.03717       0.06510      0.57100      0.97882  28.92s
    497       0.03715       0.06516      0.57016      0.97879  28.28s
    498       0.03712       0.06518      0.56948      0.97882  28.59s
    499       0.03711       0.06508      0.57020      0.97887  28.28s
    500       0.03710       0.06516      0.56935      0.97882  28.64s

Make predictions and evaluate on test data


In [31]:
y_pred = cnn.predict_proba(test_X)

In [33]:
false_positive_rate, true_positive_rate, thresholds = metrics.roc_curve(test_y, y_pred[:,1])
roc_auc = metrics.auc(false_positive_rate, true_positive_rate)

precision, recall, thresholds = metrics.precision_recall_curve(test_y, y_pred[:,1])
average_precision = metrics.average_precision_score(test_y, y_pred[:, 1])

subplot(121)
plt.title('ROC: AUC = %0.2f'% roc_auc)
plt.plot(false_positive_rate, true_positive_rate, 'b')
plt.legend(loc='lower right')
plt.plot([0,1],[0,1],'r--')
plt.ylim([-.05, 1.05])
plt.xlim([-.05, 1.0])
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')

subplot(122)
plt.plot(recall, precision)
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.ylim([0.0, 1.05])
plt.xlim([0.0, 1.0])
plt.title('Precision-Recall: AP={0:0.2f}'.format(average_precision))
plt.legend(loc="lower left")

plt.gcf().set_size_inches(10,4)

#plt.savefig('figs/plasmodium-patchevaluation.png', bbox_inches='tight')



In [34]:
false_positive_rate, true_positive_rate, thresholds = metrics.roc_curve(test_y, y_pred[:,1])
true_positive_rate.shape, thresholds.shape
plt.plot(true_positive_rate, thresholds,label='True positive rate')
plt.plot(false_positive_rate, thresholds, label='False positive rate')
plt.xlabel('Threshold')
plt.legend(loc='upper left')


Out[34]:
<matplotlib.legend.Legend at 0x7f6db813e910>

Examine mistakes to understand network performance: false positives.

Find the negative-labelled patches with highest prediction score


In [35]:
neg_indices = np.where(test_y==0)[0]
neg_scores = y_pred[neg_indices,1]
neg_indices = neg_indices[neg_scores.argsort()]
neg_indices = neg_indices[::-1]

neg_scores = y_pred[neg_indices,1]

N_samples_to_display = 12
offset = 55
for i in range(N_samples_to_display,2*N_samples_to_display):
    plt.subplot(2,N_samples_to_display,i+1)
    example_neg = test_X[neg_indices[i+offset],:,:,:]
    example_neg = np.swapaxes(example_neg,0,2)
    plt.imshow(example_neg)
    plt.title('%.3f' % neg_scores[i+offset])
    plt.tick_params(axis='both', which='both', bottom='off', top='off', labelbottom='off', right='off', left='off', labelleft='off')    

plt.gcf().set_size_inches(1.5*N_samples_to_display,3) 

plt.savefig('figs/plasmodium-falsedetections.png', bbox_inches='tight')


See highest-scored test patches


In [36]:
prob_range = [.95,1.]

tmp_scores = y_pred.copy()[:,1]
tmp_scores[tmp_scores<prob_range[0]] = -1
tmp_scores[tmp_scores>prob_range[1]] = -1

pos_indices = tmp_scores.argsort()
pos_indices = pos_indices[::-1]

N_samples_to_display = 12
offset = 0
for i in range(N_samples_to_display,2*N_samples_to_display):
    plt.subplot(2,N_samples_to_display,i+1)
    example_neg = test_X[pos_indices[i+offset],:,:,:]
    example_neg = np.swapaxes(example_neg,0,2)
    plt.imshow(example_neg)
    plt.title('%.3f' % (tmp_scores[pos_indices[i+offset]]))
    plt.tick_params(axis='both', which='both', bottom='off', top='off', labelbottom='off', right='off', left='off', labelleft='off')    

plt.gcf().set_size_inches(1.5*N_samples_to_display,3) 

plt.savefig('figs/plasmodium-detectedpatches.png', bbox_inches='tight')


See lowest scored test patches


In [37]:
pos_indices = y_pred[:,1].argsort()

N_samples_to_display = 12

for i in range(N_samples_to_display,2*N_samples_to_display):
    plt.subplot(2,N_samples_to_display,i+1)
    example_neg = test_X[pos_indices[i],:,:,:]
    example_neg = np.swapaxes(example_neg,0,2)
    plt.imshow(example_neg)
    plt.title('%.3f' % (y_pred[pos_indices[i],1]))
    plt.tick_params(axis='both', which='both', bottom='off', top='off', labelbottom='off', right='off', left='off', labelleft='off')    

plt.gcf().set_size_inches(1.5*N_samples_to_display,3) 

plt.savefig('figs/plasmodium-testpatches-lowprob.png', bbox_inches='tight')


Example of objects detected in an entire image

The white boxes represent annotations in the training data. Red boxes are detections by the convnet.


In [ ]:
reload(det)

fname = testfiles[5]
imfile = opts['img_dir'] + fname
opts['detection_probability_threshold'] = 0.5
opts['detection_overlap_threshold'] = 0.1
found = det.detect(imfile, cnn, opts)

im = misc.imread(imfile)

plt.box(False)
plt.xticks([])
plt.yticks([])

annofile = opts['annotation_dir'] + fname[:-3] + 'xml'
bboxes = readdata.get_bounding_boxes_for_single_image(annofile)
for bb in bboxes:
    bb = bb.astype(int)
    cv2.rectangle(im, (bb[0],bb[2]), (bb[1],bb[3]), (255,255,255), 2)  

for f in found:
    f = f.astype(int)
    cv2.rectangle(im, (f[0],f[1]), (f[2],f[3]), (255,0,0), 2)

plt.gcf().set_size_inches(10,10)
plt.title('Detected objects in %s' % (imfile))
plt.imshow(im)

#cv2.imwrite('detectionimages/detected-' + os.path.basename(imfile),im)

Evaluation: compare with classification based on morphological feature extraction


In [ ]:
featureset = [3,7,11,12,15,17]
centiles = [0,25,50,75,100]

pb = ProgressBar(train_X.shape[0])
train_X_f = []
for i in range(train_X.shape[0]):
    if i % 100 == 0:
        pb.step(i)
    graypatch = cv2.cvtColor(np.swapaxes(train_X[i,:,:,:],0,2).astype('uint8'), cv2.COLOR_BGR2GRAY)
    train_X_f.append(shapefeatures.extract(graypatch,attributes=featureset,centiles=centiles, momentfeatures=True))
train_X_f = np.vstack(train_X_f)

test_X_f = []
for i in range(test_X.shape[0]):
    if i % 100 == 0:
        pb.step(i)
    graypatch = cv2.cvtColor(np.swapaxes(test_X[i,:,:,:],0,2).astype('uint8'), cv2.COLOR_BGR2GRAY)
    test_X_f.append(shapefeatures.extract(graypatch,attributes=featureset,centiles=centiles, momentfeatures=True))
test_X_f = np.vstack(test_X_f)


[*****************83%************      ]  575701 of 692165 complete

In [ ]:
clf = ensemble.ExtraTreesClassifier(n_estimators=100, max_depth=5, n_jobs=-1)
clf.fit(train_X_f, train_y)
y_pred_CLF = clf.predict_proba(test_X_f)

In [ ]:
false_positive_rate_CNN, true_positive_rate_CNN, thresholds_CNN = metrics.roc_curve(test_y, y_pred[:,1])
roc_auc_CNN = metrics.auc(false_positive_rate_CNN, true_positive_rate_CNN)

precision_CNN, recall_CNN, thresholds_CNN = metrics.precision_recall_curve(test_y, y_pred[:,1])
average_precision_CNN = metrics.average_precision_score(test_y, y_pred[:, 1])

false_positive_rate_CLF, true_positive_rate_CLF, thresholds_CLF = metrics.roc_curve(test_y, y_pred_CLF[:,1])
roc_auc_CLF = metrics.auc(false_positive_rate_CLF, true_positive_rate_CLF)

precision_CLF, recall_CLF, thresholds_CLF = metrics.precision_recall_curve(test_y, y_pred_CLF[:,1])
average_precision_CLF = metrics.average_precision_score(test_y, y_pred_CLF[:, 1])

subplot(211)
plt.title('ROC' )
plt.plot(false_positive_rate_CNN, true_positive_rate_CNN, 'b', label='CNN: AUC=%.2f' % (roc_auc_CNN))
plt.plot(false_positive_rate_CLF, true_positive_rate_CLF, 'k--', label='ERT: AUC=%.2f' % (roc_auc_CLF))
plt.legend(loc='lower right')
plt.ylim([-.05, 1.05])
plt.xlim([-.05, 1.0])
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')

subplot(212)
plt.plot(recall_CNN, precision_CNN, label='CNN: AP=%.2f' % (average_precision_CNN))
plt.plot(recall_CLF, precision_CLF,'k--', label='ERT: AP=%.2f' % (average_precision_CLF))
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.ylim([0.0, 1.05])
plt.xlim([0.0, 1.0])
plt.title('Precision-Recall')
plt.legend(loc="lower left")

plt.gcf().set_size_inches(4,10)

plt.savefig('figs/plasmodium-patchevaluation.png', bbox_inches='tight')

In [ ]:
results = {
"false_positive_rate_CNN": false_positive_rate_CNN,
"true_positive_rate_CNN": true_positive_rate_CNN,
"false_positive_rate_CLF": false_positive_rate_CLF,
"true_positive_rate_CLF": true_positive_rate_CLF,
"roc_auc_CNN": roc_auc_CNN,
"roc_auc_CLF": roc_auc_CLF,
"recall_CNN": recall_CNN,
"precision_CNN": precision_CNN,
"average_precision_CNN": average_precision_CNN,
"recall_CLF": recall_CLF,
"precision_CLF": precision_CLF,
"average_precision_CLF": average_precision_CLF,
"opts": opts
}
import pickle
pickle.dump(results,open('plasmodium-results.pkl','w'))

In [ ]: