Faster R-CNN Algorithm for the Full Blood Cell Detection

Zhaolun Liu
Nov 12, 2019


Faster-rcnn.png

Faster-rcnn

Objective

  1. Learn about setting up faster R-CNN training with Keras
  2. Use faster R-CNN network to do object detection of input images

Prerequestion

Before we actually get into the model building phase, we need to ensure that the right libraries and frameworks have been installed. The below libraries are required to run this project:

  • tensorflow 1.11.0
  • keras 2.2.4
  • pandas
  • matplotlib
  • numpy
  • opencv-python
  • sklearn
  • h5py

Procedure

  1. Download the code and unzip it.
  2. The whole Dataset is in folder "cell_data/BCCD" and the test dataset is in "test_image".
  3. The Main faster RCNN codes for training and testing are "train_frcnn.py" and "test_frcnn.py", respectively. There are some other subroutines in folder "keras_frcnn".

Exercises

  1. Run Code: Run the code U_Net.ipynb to get the results from simple training.
  2. Change to a larger training Data Size: Load the pretrained model using 145 images and test it with "!python test_frcnn.py -p test_image --model_path=model_frcnn_145pics_22epoch.hdf5", compare the predicted result.
  3. Change to a different step length method. Comment on effectiveness of your choice.
  4. Possible Project: Replace pictures of labeled blood samples with labeled pictures of rock thin sections that contain fossils. Use labeled pictures to train the R-CNN, and then use the trained network to window and label fossil thin sections. If actual labeled data is not available, download thin-section pictures of fossils from the WWW, snip out the relevant identified fossils, and create a large synthetic set of fossil tin sections.

Data Exploration

Let's firstly look at the dataset in "cell_data/BCCD". You can download it from [github] (https://github.com/Shenggan/BCCD_Dataset).

  • You can see a example of the labeled cell image.

    We have three kind of labels :

    • RBC (Red Blood Cell)
    • WBC (White Blood Cell)
    • Platelets

example

The structure of the cell_data is:

  ├── BCCD
  │   ├── Annotations
  │   │       └── BloodImage_00XYZ.xml (364 items)
  │   ├── ImageSets       # Contain four Main/*.txt which split the dataset
  │   └── JPEGImages
  │       └── BloodImage_00XYZ.jpg (364 items)
  ├── example.jpg         # A example labeled img generated by visualize.py
  • The JPEGImages:

    • Image Type : jpeg(JPEG)
    • Width x Height : 640 x 480
  • The Annotations : The VOC format .xml for Object Detection, automatically generate by the label tools. Below is an example of .xml file, which includes the bounding boxes.

    <annotation>
      <folder>JPEGImages</folder>
      <filename>BloodImage_00000.jpg</filename>
      <path>/home/pi/detection_dataset/JPEGImages/BloodImage_00000.jpg</path>
      <source>
          <database>Unknown</database>
      </source>
      <size>
          <width>640</width>
          <height>480</height>
          <depth>3</depth>
      </size>
      <segmented>0</segmented>
      <object>
          <name>WBC</name>
          <pose>Unspecified</pose>
          <truncated>0</truncated>
          <difficult>0</difficult>
          <bndbox>
              <xmin>260</xmin>
              <ymin>177</ymin>
              <xmax>491</xmax>
              <ymax>376</ymax>
          </bndbox>
      </object>
        ...
      <object>
          ...
      </object>
    </annotation>
    

Here we modified the data in the following aspects:

  • The bounding boxes have been converted from the given .xml format to a .csv format
  • The whole dataset is splited into the training and test sets by randomly picking images

Firstly, we will randomly select the training images from the dataset and convert the .xml file to .csv file.

In [42]:
import xml.etree.ElementTree as ET
import csv
from random import seed
import os.path
from random import randint
import random
# seed random number generator
seed(1)

# open a file for writing

csv_data = open('./cell_data/train.csv', 'w')
# create the csv writer object
csvwriter = csv.writer(csv_data)
csv_head = ['Image_names','cell_type','xmin','xmax','ymin','ymax']
csvwriter.writerow(csv_head)
# generate 10 integers randomly between 0 and 410
count = 0
namelist = []
lis=range(0, 410);
alldatalist = random.sample(lis,20)
for i in range(20):
    value = alldatalist[i]
    imagename = "./cell_data/BCCD/Annotations/BloodImage_%05d.xml" % (value)
    if os.path.isfile(imagename):
        count = count + 1
        namelist.append(value)
        tree=ET.parse(imagename)
        root=tree.getroot()
        filename=root.find('filename').text
        print(filename)
        for region in root.findall('object'):
            csv = []
            csv.append(filename)
            name = region.find('name').text
            csv.append(name)
            xmin = region.find('bndbox').find('xmin').text
            csv.append(xmin)
            xmax = region.find('bndbox').find('xmax').text
            csv.append(xmax)
            ymin = region.find('bndbox').find('ymin').text
            csv.append(ymin)
            ymax = region.find('bndbox').find('ymax').text
            csv.append(ymax)
            #print('cell_type='+name,'xmin='+xmin,'xmax='+xmax,'ymin='+ymin,'ymax='+ymax)
            csvwriter.writerow(csv)
csv_data.close()
print('The above %d images have been selected.' % (count))
BloodImage_00055.jpg
BloodImage_00347.jpg
BloodImage_00313.jpg
BloodImage_00104.jpg
BloodImage_00203.jpg
BloodImage_00184.jpg
BloodImage_00267.jpg
BloodImage_00323.jpg
BloodImage_00038.jpg
BloodImage_00011.jpg
BloodImage_00342.jpg
BloodImage_00177.jpg
BloodImage_00312.jpg
BloodImage_00000.jpg
BloodImage_00182.jpg
BloodImage_00295.jpg
BloodImage_00093.jpg
BloodImage_00387.jpg
BloodImage_00369.jpg
BloodImage_00012.jpg
The above 20 images have been selected.

The reminding pictures can be used to test our results. So, we will select 10 images from the remaining images for testing.

In [43]:
from shutil import copyfile
test_num = 10
count = 1
for value in range(0,410):
    if value not in namelist:
        #print(value)
        imagename = "./cell_data/BCCD/JPEGImages/BloodImage_%05d.jpg" % (value)
        
        if os.path.isfile(imagename):
            dist = "test_image/BloodImage_%05d.jpg" % (value)
            copyfile(imagename,dist)
            count += 1
            if count == test_num:
                break
        

Let’s read the .csv file and print out the first few rows.

In [44]:
# importing required libraries
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
from matplotlib import patches
# read the csv file using read_csv function of pandas
train = pd.read_csv('./cell_data/train.csv')
train.head()
Out[44]:
Image_names cell_type xmin xmax ymin ymax
0 BloodImage_00055.jpg RBC 330 439 15 100
1 BloodImage_00055.jpg RBC 236 337 20 131
2 BloodImage_00055.jpg RBC 319 405 231 327
3 BloodImage_00055.jpg RBC 487 594 143 265
4 BloodImage_00055.jpg RBC 280 366 407 480

There are 6 columns in the train file. Let’s understand what each column represents:

  1. Image_names: contains the name of the image
  2. cell_type: denotes the type of the cell
  3. xmin: x-coordinate of the bottom left part of the image
  4. xmax: x-coordinate of the top right part of the image
  5. ymin: y-coordinate of the bottom left part of the image
  6. ymax: y-coordinate of the top right part of the image

Let’s now print an image to visualize what we’re working with:

In [45]:
# reading single image using imread function of matplotlib
fileno = 55
newimage='./cell_data/BCCD/JPEGImages/BloodImage_%05d.jpg' % (fileno)
image = plt.imread(newimage)
plt.imshow(image)
Out[45]:
<matplotlib.image.AxesImage at 0x2afcd4bd7b90>

This is what a blood cell image looks like. Here, the blue part represents the WBCs, and the slightly red parts represent the RBCs. Let’s look at how many images, and the different type of classes, there are in our training set.

In [46]:
# Number of unique training images
train['Image_names'].nunique()
Out[46]:
20

So, we have 10 training images.

In [47]:
# Number of classes
train['cell_type'].value_counts()
Out[47]:
RBC          236
Platelets     26
WBC           21
Name: cell_type, dtype: int64

We have three different classes of cells, i.e., RBC, WBC and Platelets. Finally, let’s look at how an image with detected objects will look like:

In [48]:
fig = plt.figure()

#add axes to the image
ax = fig.add_axes([0,0,1,1])

# read and plot the image
image = plt.imread(newimage)
plt.imshow(image)

# iterating over the image for different objects
for _,row in train[train.Image_names == "BloodImage_%05d.jpg" % (fileno)].iterrows():
    xmin = row.xmin
    xmax = row.xmax
    ymin = row.ymin
    ymax = row.ymax
    
    width = xmax - xmin
    height = ymax - ymin
    
    # assign different color to different classes of objects
    if row.cell_type == 'RBC':
        edgecolor = 'r'
        ax.annotate('RBC', xy=(xmax-40,ymin+20))
    elif row.cell_type == 'WBC':
        edgecolor = 'b'
        ax.annotate('WBC', xy=(xmax-40,ymin+20))
    elif row.cell_type == 'Platelets':
        edgecolor = 'g'
        ax.annotate('Platelets', xy=(xmax-40,ymin+20))
        
    # add bounding boxes to the image
    rect = patches.Rectangle((xmin,ymin), width, height, edgecolor = edgecolor, facecolor = 'none')
    
    ax.add_patch(rect)

We will be using the keras_frcnn library to train our model as well as to get predictions on the test images.

Finally, we will convert .csv file to the file annotate.txt that our code can use. The annoate.txt should have the following format:

filepath,x1,y1,x2,y2,class_name

where

  • filepath is the path of the training image
  • x1 is the xmin coordinate for bounding box
  • y1 is the ymin coordinate for bounding box
  • x2 is the xmax coordinate for bounding box
  • y2 is the ymax coordinate for bounding box
  • class_name is the name of the class in that bounding box
In [49]:
data = pd.DataFrame()
data['format'] = train['Image_names']

# as the images are in train_images folder, add train_images before the image name
for i in range(data.shape[0]):
    data['format'][i] = 'cell_data/BCCD/JPEGImages/' + data['format'][i]

# add xmin, ymin, xmax, ymax and class as per the format required
for i in range(data.shape[0]):
    data['format'][i] = data['format'][i] + ',' + str(train['xmin'][i]) + ',' + str(train['ymin'][i]) + ',' + str(train['xmax'][i]) + ',' + str(train['ymax'][i]) + ',' + train['cell_type'][i]

data.to_csv('./annotate.txt', header=None, index=None, sep=' ')

Implementing Faster R-CNN

Let's train our model! We will be using the train_frcnn.py file to train the model.

In [50]:
!python train_frcnn.py -o simple -p annotate.txt --num_epochs=100
Using TensorFlow backend.
Parsing annotation files
Training images per class:
{'Platelets': 26, 'RBC': 236, 'WBC': 21, 'bg': 0}
Num classes (including bg) = 4
Config has been written to config.pickle, and can be loaded when testing to ensure correct results
Num train samples 20
Num val samples 0
Tensor("max_pooling2d_1/MaxPool:0", shape=(?, ?, ?, 64), dtype=float32)
[<tf.Tensor 'rpn_out_class/Sigmoid:0' shape=(?, ?, ?, 9) dtype=float32>, <tf.Tensor 'rpn_out_regress/BiasAdd:0' shape=(?, ?, ?, 36) dtype=float32>]
loading weights from resnet50_weights_tf_dim_ordering_tf_kernels.h5
l3
resnet50_weights_tf_dim_ordering_tf_kernels.h5
Could not load pretrained model weights. Weights can be found in the keras application folder 		https://github.com/fchollet/keras/tree/master/keras/applications
Starting training
Epoch 1/100
2019-11-12 16:52:08.901096: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
20/20 [==============================] - 132s 7s/step - rpn_cls: 7.3008 - rpn_regr: 0.1332 - detector_cls: 0.8095 - detector_regr: 0.4700
Mean number of bounding boxes from RPN overlapping ground truth boxes: 15.85
Classifier accuracy for bounding boxes from RPN: 0.5765625
Loss RPN classifier: 7.30082942247
Loss RPN regression: 0.133201095648
Loss Detector classifier: 0.809488993883
Loss Detector regression: 0.469982549548
Elapsed time: 131.889513016
Total loss decreased from inf to 8.71350206155, saving weights
Epoch 2/100
Average number of overlapping bounding boxes from RPN = 15.85 for 20 previous iterations
20/20 [==============================] - 113s 6s/step - rpn_cls: 4.3027 - rpn_regr: 0.1303 - detector_cls: 0.7329 - detector_regr: 0.3536
Mean number of bounding boxes from RPN overlapping ground truth boxes: 15.85
Classifier accuracy for bounding boxes from RPN: 0.56875
Loss RPN classifier: 4.30267465115
Loss RPN regression: 0.130319829285
Loss Detector classifier: 0.732907667756
Loss Detector regression: 0.353639576584
Elapsed time: 125.478996992
Total loss decreased from 8.71350206155 to 5.51954172477, saving weights
Epoch 3/100
Average number of overlapping bounding boxes from RPN = 15.85 for 20 previous iterations
20/20 [==============================] - 113s 6s/step - rpn_cls: 4.4665 - rpn_regr: 0.1285 - detector_cls: 0.6657 - detector_regr: 0.3581
Mean number of bounding boxes from RPN overlapping ground truth boxes: 16.75
Classifier accuracy for bounding boxes from RPN: 0.6203125
Loss RPN classifier: 4.46649169922
Loss RPN regression: 0.128542614728
Loss Detector classifier: 0.665717759728
Loss Detector regression: 0.358142252266
Elapsed time: 113.292642117
Epoch 4/100
Average number of overlapping bounding boxes from RPN = 16.75 for 20 previous iterations
20/20 [==============================] - 118s 6s/step - rpn_cls: 4.5815 - rpn_regr: 0.1275 - detector_cls: 0.6746 - detector_regr: 0.3389
Mean number of bounding boxes from RPN overlapping ground truth boxes: 14.5
Classifier accuracy for bounding boxes from RPN: 0.7078125
Loss RPN classifier: 4.58153648376
Loss RPN regression: 0.127460453659
Loss Detector classifier: 0.674597470462
Loss Detector regression: 0.338866973668
Elapsed time: 118.151384115
Epoch 5/100
Average number of overlapping bounding boxes from RPN = 14.5 for 20 previous iterations
20/20 [==============================] - 116s 6s/step - rpn_cls: 4.2932 - rpn_regr: 0.1268 - detector_cls: 0.5906 - detector_regr: 0.3493
Mean number of bounding boxes from RPN overlapping ground truth boxes: 16.45
Classifier accuracy for bounding boxes from RPN: 0.728125
Loss RPN classifier: 4.29318318367
Loss RPN regression: 0.126751405187
Loss Detector classifier: 0.590607343614
Loss Detector regression: 0.349296908081
Elapsed time: 116.473963976
Total loss decreased from 5.51954172477 to 5.35983884055, saving weights
Epoch 6/100
Average number of overlapping bounding boxes from RPN = 16.45 for 20 previous iterations
20/20 [==============================] - 112s 6s/step - rpn_cls: 4.3225 - rpn_regr: 0.1256 - detector_cls: 0.5087 - detector_regr: 0.3378
Mean number of bounding boxes from RPN overlapping ground truth boxes: 16.95
Classifier accuracy for bounding boxes from RPN: 0.75625
Loss RPN classifier: 4.32254288197
Loss RPN regression: 0.125561562181
Loss Detector classifier: 0.508739701658
Loss Detector regression: 0.33777519241
Elapsed time: 112.168544054
Total loss decreased from 5.35983884055 to 5.29461933821, saving weights
Epoch 7/100
Average number of overlapping bounding boxes from RPN = 16.95 for 20 previous iterations
20/20 [==============================] - 113s 6s/step - rpn_cls: 4.4085 - rpn_regr: 0.1250 - detector_cls: 0.4840 - detector_regr: 0.3461
Mean number of bounding boxes from RPN overlapping ground truth boxes: 16.85
Classifier accuracy for bounding boxes from RPN: 0.76875
Loss RPN classifier: 4.40847640038
Loss RPN regression: 0.125006032363
Loss Detector classifier: 0.484049990401
Loss Detector regression: 0.346108664572
Elapsed time: 113.753868103
Epoch 8/100
Average number of overlapping bounding boxes from RPN = 16.85 for 20 previous iterations
20/20 [==============================] - 112s 6s/step - rpn_cls: 4.2503 - rpn_regr: 0.1241 - detector_cls: 0.4675 - detector_regr: 0.3054
Mean number of bounding boxes from RPN overlapping ground truth boxes: 18.65
Classifier accuracy for bounding boxes from RPN: 0.7765625
Loss RPN classifier: 4.25032154322
Loss RPN regression: 0.124140638858
Loss Detector classifier: 0.467540325224
Loss Detector regression: 0.305377660692
Elapsed time: 111.580003977
Total loss decreased from 5.29461933821 to 5.14738016799, saving weights
Epoch 9/100
Average number of overlapping bounding boxes from RPN = 18.65 for 20 previous iterations
20/20 [==============================] - 114s 6s/step - rpn_cls: 4.2536 - rpn_regr: 0.1230 - detector_cls: 0.4548 - detector_regr: 0.3111
Mean number of bounding boxes from RPN overlapping ground truth boxes: 19.2
Classifier accuracy for bounding boxes from RPN: 0.784375
Loss RPN classifier: 4.25364557505
Loss RPN regression: 0.123045318201
Loss Detector classifier: 0.454788575321
Loss Detector regression: 0.311094161123
Elapsed time: 114.840797186
Total loss decreased from 5.14738016799 to 5.14257362969, saving weights
Epoch 10/100
Average number of overlapping bounding boxes from RPN = 19.2 for 20 previous iterations
20/20 [==============================] - 115s 6s/step - rpn_cls: 4.2837 - rpn_regr: 0.1225 - detector_cls: 0.5358 - detector_regr: 0.3193
Mean number of bounding boxes from RPN overlapping ground truth boxes: 19.35
Classifier accuracy for bounding boxes from RPN: 0.7375
Loss RPN classifier: 4.28374946117
Loss RPN regression: 0.12246769499
Loss Detector classifier: 0.535753034055
Loss Detector regression: 0.319330323488
Elapsed time: 115.876113892
Epoch 11/100
Average number of overlapping bounding boxes from RPN = 19.35 for 20 previous iterations
20/20 [==============================] - 114s 6s/step - rpn_cls: 4.3887 - rpn_regr: 0.1218 - detector_cls: 0.4694 - detector_regr: 0.2715
Mean number of bounding boxes from RPN overlapping ground truth boxes: 19.2
Classifier accuracy for bounding boxes from RPN: 0.7625
Loss RPN classifier: 4.38872398138
Loss RPN regression: 0.121821837313
Loss Detector classifier: 0.469396989793
Loss Detector regression: 0.271472126245
Elapsed time: 114.447825909
Epoch 12/100
Average number of overlapping bounding boxes from RPN = 19.2 for 20 previous iterations
20/20 [==============================] - 123s 6s/step - rpn_cls: 4.0845 - rpn_regr: 0.1208 - detector_cls: 0.4205 - detector_regr: 0.2673
Mean number of bounding boxes from RPN overlapping ground truth boxes: 19.85
Classifier accuracy for bounding boxes from RPN: 0.809375
Loss RPN classifier: 4.08448622227
Loss RPN regression: 0.12080501616
Loss Detector classifier: 0.420466131717
Loss Detector regression: 0.267253109813
Elapsed time: 123.287686825
Total loss decreased from 5.14257362969 to 4.89301047996, saving weights
Epoch 13/100
Average number of overlapping bounding boxes from RPN = 19.85 for 20 previous iterations
20/20 [==============================] - 124s 6s/step - rpn_cls: 4.1763 - rpn_regr: 0.1207 - detector_cls: 0.4156 - detector_regr: 0.2563
Mean number of bounding boxes from RPN overlapping ground truth boxes: 18.3
Classifier accuracy for bounding boxes from RPN: 0.8078125
Loss RPN classifier: 4.17627340555
Loss RPN regression: 0.120736445114
Loss Detector classifier: 0.415619750693
Loss Detector regression: 0.256305102259
Elapsed time: 124.377295017
Epoch 14/100
Average number of overlapping bounding boxes from RPN = 18.3 for 20 previous iterations
20/20 [==============================] - 120s 6s/step - rpn_cls: 3.2766 - rpn_regr: 0.1216 - detector_cls: 0.5411 - detector_regr: 0.3049
Mean number of bounding boxes from RPN overlapping ground truth boxes: 11.85
Classifier accuracy for bounding boxes from RPN: 0.73125
Loss RPN classifier: 3.27659729719
Loss RPN regression: 0.121647755802
Loss Detector classifier: 0.54111148268
Loss Detector regression: 0.304946859181
Elapsed time: 120.01109004
Total loss decreased from 4.89301047996 to 4.24430339485, saving weights
Epoch 15/100
Average number of overlapping bounding boxes from RPN = 11.85 for 20 previous iterations
20/20 [==============================] - 115s 6s/step - rpn_cls: 2.9802 - rpn_regr: 0.1202 - detector_cls: 0.4359 - detector_regr: 0.2664
Mean number of bounding boxes from RPN overlapping ground truth boxes: 9.0
Classifier accuracy for bounding boxes from RPN: 0.8
Loss RPN classifier: 2.98022363782
Loss RPN regression: 0.12017969545
Loss Detector classifier: 0.435947601264
Loss Detector regression: 0.266384289041
Elapsed time: 115.752846003
Total loss decreased from 4.24430339485 to 3.80273522357, saving weights
Epoch 16/100
Average number of overlapping bounding boxes from RPN = 9.0 for 20 previous iterations
20/20 [==============================] - 116s 6s/step - rpn_cls: 3.1121 - rpn_regr: 0.1199 - detector_cls: 0.4278 - detector_regr: 0.2771
Mean number of bounding boxes from RPN overlapping ground truth boxes: 10.4
Classifier accuracy for bounding boxes from RPN: 0.840625
Loss RPN classifier: 3.1121314466
Loss RPN regression: 0.119879955426
Loss Detector classifier: 0.427762426808
Loss Detector regression: 0.277121723071
Elapsed time: 116.899129152
Epoch 17/100
Average number of overlapping bounding boxes from RPN = 10.4 for 20 previous iterations
20/20 [==============================] - 117s 6s/step - rpn_cls: 3.0791 - rpn_regr: 0.1198 - detector_cls: 0.3908 - detector_regr: 0.2526
Mean number of bounding boxes from RPN overlapping ground truth boxes: 12.5
Classifier accuracy for bounding boxes from RPN: 0.825
Loss RPN classifier: 3.07911897898
Loss RPN regression: 0.119825992174
Loss Detector classifier: 0.390803912282
Loss Detector regression: 0.252594032884
Elapsed time: 117.245656013
Epoch 18/100
Average number of overlapping bounding boxes from RPN = 12.5 for 20 previous iterations
20/20 [==============================] - 115s 6s/step - rpn_cls: 3.2361 - rpn_regr: 0.1195 - detector_cls: 0.4478 - detector_regr: 0.2928
Mean number of bounding boxes from RPN overlapping ground truth boxes: 19.4
Classifier accuracy for bounding boxes from RPN: 0.8125
Loss RPN classifier: 3.23612074852
Loss RPN regression: 0.11946579311
Loss Detector classifier: 0.447831952572
Loss Detector regression: 0.292825061083
Elapsed time: 115.480653048
Epoch 19/100
Average number of overlapping bounding boxes from RPN = 19.4 for 20 previous iterations
20/20 [==============================] - 113s 6s/step - rpn_cls: 3.2449 - rpn_regr: 0.1204 - detector_cls: 0.5286 - detector_regr: 0.2662
Mean number of bounding boxes from RPN overlapping ground truth boxes: 34.9
Classifier accuracy for bounding boxes from RPN: 0.7125
Loss RPN classifier: 3.24487290382
Loss RPN regression: 0.12040950302
Loss Detector classifier: 0.528644366562
Loss Detector regression: 0.266216636449
Elapsed time: 113.350762129
Epoch 20/100
Average number of overlapping bounding boxes from RPN = 34.9 for 20 previous iterations
20/20 [==============================] - 113s 6s/step - rpn_cls: 2.9361 - rpn_regr: 0.1190 - detector_cls: 0.4823 - detector_regr: 0.2813
Mean number of bounding boxes from RPN overlapping ground truth boxes: 50.85
Classifier accuracy for bounding boxes from RPN: 0.778125
Loss RPN classifier: 2.93613996506
Loss RPN regression: 0.119024828449
Loss Detector classifier: 0.482282310724
Loss Detector regression: 0.281279720366
Elapsed time: 113.135446072
Epoch 21/100
Average number of overlapping bounding boxes from RPN = 50.85 for 20 previous iterations
20/20 [==============================] - 116s 6s/step - rpn_cls: 1.4869 - rpn_regr: 0.1202 - detector_cls: 0.5419 - detector_regr: 0.2655
Mean number of bounding boxes from RPN overlapping ground truth boxes: 63.9
Classifier accuracy for bounding boxes from RPN: 0.7140625
Loss RPN classifier: 1.4868774727
Loss RPN regression: 0.120168553665
Loss Detector classifier: 0.541904942691
Loss Detector regression: 0.265531690419
Elapsed time: 116.187630892
Total loss decreased from 3.80273522357 to 2.41448265947, saving weights
Epoch 22/100
Average number of overlapping bounding boxes from RPN = 63.9 for 20 previous iterations
20/20 [==============================] - 118s 6s/step - rpn_cls: 1.1039 - rpn_regr: 0.1189 - detector_cls: 0.5061 - detector_regr: 0.2593
Mean number of bounding boxes from RPN overlapping ground truth boxes: 67.55
Classifier accuracy for bounding boxes from RPN: 0.7390625
Loss RPN classifier: 1.10386256576
Loss RPN regression: 0.118917386793
Loss Detector classifier: 0.506141330302
Loss Detector regression: 0.259325120598
Elapsed time: 119.029846907
Total loss decreased from 2.41448265947 to 1.98824640345, saving weights
Epoch 23/100
Average number of overlapping bounding boxes from RPN = 67.55 for 20 previous iterations
20/20 [==============================] - 120s 6s/step - rpn_cls: 1.5782 - rpn_regr: 0.1185 - detector_cls: 0.4551 - detector_regr: 0.2303
Mean number of bounding boxes from RPN overlapping ground truth boxes: 58.0
Classifier accuracy for bounding boxes from RPN: 0.76875
Loss RPN classifier: 1.5782070756
Loss RPN regression: 0.118457105011
Loss Detector classifier: 0.455076152086
Loss Detector regression: 0.230300462991
Elapsed time: 120.178640842
Epoch 24/100
Average number of overlapping bounding boxes from RPN = 58.0 for 20 previous iterations
20/20 [==============================] - 113s 6s/step - rpn_cls: 1.4401 - rpn_regr: 0.1182 - detector_cls: 0.5310 - detector_regr: 0.2693
Mean number of bounding boxes from RPN overlapping ground truth boxes: 61.85
Classifier accuracy for bounding boxes from RPN: 0.7328125
Loss RPN classifier: 1.44008875936
Loss RPN regression: 0.1182056861
Loss Detector classifier: 0.53095253855
Loss Detector regression: 0.269329628348
Elapsed time: 113.436048985
Epoch 25/100
Average number of overlapping bounding boxes from RPN = 61.85 for 20 previous iterations
20/20 [==============================] - 114s 6s/step - rpn_cls: 1.0395 - rpn_regr: 0.1169 - detector_cls: 0.5142 - detector_regr: 0.2509
Mean number of bounding boxes from RPN overlapping ground truth boxes: 66.2
Classifier accuracy for bounding boxes from RPN: 0.7578125
Loss RPN classifier: 1.03954550326
Loss RPN regression: 0.116875948943
Loss Detector classifier: 0.514249350131
Loss Detector regression: 0.250933809578
Elapsed time: 114.033133984
Total loss decreased from 1.98824640345 to 1.92160461191, saving weights
Epoch 26/100
Average number of overlapping bounding boxes from RPN = 66.2 for 20 previous iterations
20/20 [==============================] - 117s 6s/step - rpn_cls: 1.2417 - rpn_regr: 0.1174 - detector_cls: 0.4540 - detector_regr: 0.2374
Mean number of bounding boxes from RPN overlapping ground truth boxes: 65.1
Classifier accuracy for bounding boxes from RPN: 0.7859375
Loss RPN classifier: 1.24167495817
Loss RPN regression: 0.117379262112
Loss Detector classifier: 0.453951409459
Loss Detector regression: 0.237352351099
Elapsed time: 117.942809105
Epoch 27/100
Average number of overlapping bounding boxes from RPN = 65.1 for 20 previous iterations
20/20 [==============================] - 113s 6s/step - rpn_cls: 1.1950 - rpn_regr: 0.1159 - detector_cls: 0.4761 - detector_regr: 0.2275
Mean number of bounding boxes from RPN overlapping ground truth boxes: 63.45
Classifier accuracy for bounding boxes from RPN: 0.7609375
Loss RPN classifier: 1.19504354298
Loss RPN regression: 0.115856908076
Loss Detector classifier: 0.476113592088
Loss Detector regression: 0.227535064518
Elapsed time: 112.70400095
Epoch 28/100
Average number of overlapping bounding boxes from RPN = 63.45 for 20 previous iterations
20/20 [==============================] - 118s 6s/step - rpn_cls: 1.3852 - rpn_regr: 0.1165 - detector_cls: 0.4319 - detector_regr: 0.2414
Mean number of bounding boxes from RPN overlapping ground truth boxes: 64.1
Classifier accuracy for bounding boxes from RPN: 0.7953125
Loss RPN classifier: 1.38522835523
Loss RPN regression: 0.116522045434
Loss Detector classifier: 0.431861996651
Loss Detector regression: 0.24139341563
Elapsed time: 117.773166895
Epoch 29/100
Average number of overlapping bounding boxes from RPN = 64.1 for 20 previous iterations
16/20 [=======================>......] - ETA: 23s - rpn_cls: 1.1048 - rpn_regr: 0.1189 - detector_cls: 0.4638 - detector_regr: 0.2329^C

Let’s make the predictions for the new images:

In [51]:
!python  test_frcnn.py -p test_image
Using TensorFlow backend.
{0: 'RBC', 1: 'WBC', 2: 'Platelets', 3: 'bg'}
Tensor("max_pooling2d_1/MaxPool:0", shape=(?, ?, ?, 64), dtype=float32)
Loading default weights from ./model_frcnn.hdf5
2019-11-12 17:55:34.927804: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
BloodImage_00000.jpg
Elapsed time = 3.52116584778
[('RBC', 90.83995223045349), ('RBC', 88.50674033164978), ('RBC', 87.52412796020508), ('RBC', 86.31283640861511), ('WBC', 86.34433150291443)]
BloodImage_00001.jpg
Elapsed time = 2.23393702507
[('RBC', 89.30439352989197), ('RBC', 89.01771306991577), ('RBC', 87.17451095581055), ('RBC', 86.96817755699158), ('RBC', 84.65638756752014), ('RBC', 83.12140703201294), ('RBC', 82.63852000236511), ('RBC', 80.41877746582031), ('WBC', 92.79226064682007)]
BloodImage_00002.jpg
Elapsed time = 2.33596801758
[('RBC', 86.98082566261292), ('RBC', 84.6459448337555), ('RBC', 82.76601433753967), ('RBC', 80.57249784469604), ('RBC', 80.35200834274292)]
BloodImage_00003.jpg
Elapsed time = 2.22724294662
[('RBC', 88.07953596115112), ('RBC', 87.99017071723938), ('RBC', 82.65669345855713), ('RBC', 82.10171461105347), ('RBC', 81.5495491027832), ('RBC', 81.21842741966248), ('RBC', 80.9681236743927)]
BloodImage_00004.jpg
Elapsed time = 1.8977792263
[('RBC', 90.8416211605072)]
BloodImage_00005.jpg
Elapsed time = 2.21882295609
[('RBC', 91.79213643074036), ('RBC', 91.29474759101868), ('RBC', 89.18049335479736), ('RBC', 88.65684866905212), ('RBC', 87.40739226341248), ('RBC', 86.79218888282776), ('RBC', 85.88364124298096), ('RBC', 82.42835402488708), ('RBC', 82.12583661079407), ('RBC', 80.94229698181152), ('RBC', 80.05352020263672), ('WBC', 88.14239501953125)]
BloodImage_00006.jpg
Elapsed time = 2.41539812088
[('RBC', 92.52308011054993), ('RBC', 87.24987506866455), ('RBC', 84.69476103782654), ('RBC', 82.54182934761047), ('RBC', 82.31835961341858), ('RBC', 81.89834356307983), ('RBC', 80.24029731750488), ('RBC', 80.02498745918274)]
BloodImage_00007.jpg
Elapsed time = 1.86560106277
[('RBC', 95.33414840698242), ('RBC', 90.42670726776123), ('RBC', 89.71666693687439), ('RBC', 89.61407542228699), ('RBC', 89.56639766693115), ('RBC', 86.25394105911255), ('RBC', 85.79636216163635), ('RBC', 83.82293581962585), ('RBC', 83.23704600334167)]
BloodImage_00008.jpg
Elapsed time = 2.3194038868
[('RBC', 89.21140432357788), ('RBC', 86.68155074119568), ('WBC', 97.69703149795532), ('WBC', 85.3283166885376)]
BloodImage_00009.jpg
Elapsed time = 2.42653989792
[('RBC', 91.31351113319397), ('RBC', 89.82036113739014), ('RBC', 84.31739807128906), ('RBC', 83.91759991645813), ('RBC', 83.28421115875244), ('RBC', 80.60757517814636), ('RBC', 80.27276396751404), ('RBC', 80.14600276947021), ('WBC', 89.75512385368347)]
In [62]:
# reading results
fileno = 0
f = plt.figure(figsize=(15,15))
for fileno in range(4):
    plt.subplot(2,2,fileno+1)
    newimage='./results_imgs/%d.png' % (fileno)
    image = plt.imread(newimage)
    plt.imshow(image)
plt.show()