------------- User's settings -------------


In [3]:
# Location of digested data
input_directory = '/digested/'

# Desired location to save temporary PNG outputs:
png_directory = '/digested_png/'

# Desired location to save trained model
model_directory = '/model_directory/'

Download pre-trained model:


In [ ]:
import keras.applications

# Define native shape of the transferred model (refer to model documentation)
shape=(197,197,3)

loaded_model = keras.applications.ResNet50(include_top=False, 
                                           weights='imagenet',
                                           input_tensor = keras.layers.Input( shape ),
                                           classes = classes
                                          )

Pre-built Keras models:

Very Deep Convolutional Networks for Large-Scale Image Recognition https://arxiv.org/abs/1409.1556

  • VGG16
  • VGG19

Deep Residual Learning for Image Recognition https://arxiv.org/abs/1512.03385

  • ResNet50

Inception Architecture for Computer Vision https://arxiv.org/abs/1512.00567

  • Inception v3

Note:

Pre-trained networks are not well-suited for bioimaging analysis of multichannel fluorescent images, i.e. they were built for 3 channels of RGB images. In contrast to our built-in CNN, here one should selectively choose maximum 3 channels that provide sufficient information for making prediction, and should omit the channels that may introduce noise.

To make it plausible for the pre-trained network’s requirement of RGB and computationally efficient, single-channel images of an object will be stacked to form an RGB image and saved in PNG format. User should be informed that several PNG files will be stored in the hard disk as the location of png_directory.

------------- (semi)-Automatic -------------


In [1]:
import keras
import keras.preprocessing.image
import numpy
import pickle
import os.path
import tensorflow
import digest
import time


Using TensorFlow backend.

Configure CPU/GPU devices:


In [ ]:
# -------- If using Tensorflow-GPU: -------- #

configuration = tensorflow.ConfigProto()

configuration.gpu_options.allow_growth = True

configuration.gpu_options.visible_device_list = "0"

session = tensorflow.Session(config=configuration)

keras.backend.set_session(session)


# -------- If using Tensorflow (CPU) : -------- #

# configuration = tensorflow.ConfigProto()

# session = tensorflow.Session(config=configuration)

# keras.backend.set_session(session)

In [6]:
if not os.path.exists(model_directory):
    os.makedirs(model_directory)

Load data and labels:


In [5]:
class_weights = pickle.load(open(os.path.join(input_directory, "class_weights.sav"), 'rb'))

classes = len( pickle.load(open(os.path.join(input_directory, "class_names.sav"), 'rb')) )

In [ ]:
training_x = numpy.load(os.path.join(input_directory, "training_x.npy"))

training_y = numpy.load(os.path.join(input_directory, "training_y.npy"))

validation_x = numpy.load(os.path.join(input_directory, "validation_x.npy"))

validation_y = numpy.load(os.path.join(input_directory, "validation_y.npy"))

Stack single-channel images (maximum 3 channels) and store to PNG files


In [ ]:
%%capture
digest.save_png(training_x, os.path.join(png_directory,"Training") )
digest.save_png(validation_x, os.path.join(png_directory,"Validation") )

Note:

Since several PNG files will be stored during data digestion, if user desires to clean png_directory, for e.g. to start over, it is highly recommended to perform this clearance in command line.

In Linux/UNIX system:

mkdir empty_dir
rsync -a --delete empty_dir/ png_directory/

In [ ]:
options = {
    "rotation_range" : 180
    "featurewise_center": True,
    "featurewise_std_normalization": True,
    "horizontal_flip": True,
    "vertical_flip": True
}

In [ ]:
training_generator = keras.preprocessing.image.ImageDataGenerator( **options )

training_generator = training_generator.flow_from_directory(
    batch_size=32,
    color_mode="rgb",
    directory= os.path.join(png_directory,"Training"),
    target_size=(shape[0], shape[1])
)

In [ ]:
validation_generator = keras.preprocessing.image.ImageDataGenerator()

validation_generator = validation_generator.flow_from_directory(
    batch_size=32,
    color_mode="rgb",
    directory= os.path.join(png_directory,"Validation"),
    target_size=(shape[0], shape[1])
)

In [ ]:
loaded_model.summary()

In [ ]:
# first: train only the top layers (which were randomly initialized)
# i.e. freeze all convolutional layers
for layer in loaded_model.layers:
    layer.trainable = False

x = loaded_model.output

x = keras.layers.GlobalAveragePooling2D()(x)

x = keras.layers.Dense(2048, activation='relu')(x)

# # A logistic prediction layer
predictions = keras.layers.Dense(classes, activation='softmax')(x)

# # this is the model we will train
model = keras.models.Model(input=loaded_model.input, output = predictions)

In [11]:
model.summary()


____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to                     
====================================================================================================
input_1 (InputLayer)             (None, 197, 197, 3)   0                                            
____________________________________________________________________________________________________
zero_padding2d_1 (ZeroPadding2D) (None, 203, 203, 3)   0           input_1[0][0]                    
____________________________________________________________________________________________________
conv1 (Conv2D)                   (None, 99, 99, 64)    9472        zero_padding2d_1[0][0]           
____________________________________________________________________________________________________
bn_conv1 (BatchNormalization)    (None, 99, 99, 64)    256         conv1[0][0]                      
____________________________________________________________________________________________________
activation_1 (Activation)        (None, 99, 99, 64)    0           bn_conv1[0][0]                   
____________________________________________________________________________________________________
max_pooling2d_1 (MaxPooling2D)   (None, 49, 49, 64)    0           activation_1[0][0]               
____________________________________________________________________________________________________
res2a_branch2a (Conv2D)          (None, 49, 49, 64)    4160        max_pooling2d_1[0][0]            
____________________________________________________________________________________________________
bn2a_branch2a (BatchNormalizatio (None, 49, 49, 64)    256         res2a_branch2a[0][0]             
____________________________________________________________________________________________________
activation_2 (Activation)        (None, 49, 49, 64)    0           bn2a_branch2a[0][0]              
____________________________________________________________________________________________________
res2a_branch2b (Conv2D)          (None, 49, 49, 64)    36928       activation_2[0][0]               
____________________________________________________________________________________________________
bn2a_branch2b (BatchNormalizatio (None, 49, 49, 64)    256         res2a_branch2b[0][0]             
____________________________________________________________________________________________________
activation_3 (Activation)        (None, 49, 49, 64)    0           bn2a_branch2b[0][0]              
____________________________________________________________________________________________________
res2a_branch2c (Conv2D)          (None, 49, 49, 256)   16640       activation_3[0][0]               
____________________________________________________________________________________________________
res2a_branch1 (Conv2D)           (None, 49, 49, 256)   16640       max_pooling2d_1[0][0]            
____________________________________________________________________________________________________
bn2a_branch2c (BatchNormalizatio (None, 49, 49, 256)   1024        res2a_branch2c[0][0]             
____________________________________________________________________________________________________
bn2a_branch1 (BatchNormalization (None, 49, 49, 256)   1024        res2a_branch1[0][0]              
____________________________________________________________________________________________________
add_1 (Add)                      (None, 49, 49, 256)   0           bn2a_branch2c[0][0]              
                                                                   bn2a_branch1[0][0]               
____________________________________________________________________________________________________
activation_4 (Activation)        (None, 49, 49, 256)   0           add_1[0][0]                      
____________________________________________________________________________________________________
res2b_branch2a (Conv2D)          (None, 49, 49, 64)    16448       activation_4[0][0]               
____________________________________________________________________________________________________
bn2b_branch2a (BatchNormalizatio (None, 49, 49, 64)    256         res2b_branch2a[0][0]             
____________________________________________________________________________________________________
activation_5 (Activation)        (None, 49, 49, 64)    0           bn2b_branch2a[0][0]              
____________________________________________________________________________________________________
res2b_branch2b (Conv2D)          (None, 49, 49, 64)    36928       activation_5[0][0]               
____________________________________________________________________________________________________
bn2b_branch2b (BatchNormalizatio (None, 49, 49, 64)    256         res2b_branch2b[0][0]             
____________________________________________________________________________________________________
activation_6 (Activation)        (None, 49, 49, 64)    0           bn2b_branch2b[0][0]              
____________________________________________________________________________________________________
res2b_branch2c (Conv2D)          (None, 49, 49, 256)   16640       activation_6[0][0]               
____________________________________________________________________________________________________
bn2b_branch2c (BatchNormalizatio (None, 49, 49, 256)   1024        res2b_branch2c[0][0]             
____________________________________________________________________________________________________
add_2 (Add)                      (None, 49, 49, 256)   0           bn2b_branch2c[0][0]              
                                                                   activation_4[0][0]               
____________________________________________________________________________________________________
activation_7 (Activation)        (None, 49, 49, 256)   0           add_2[0][0]                      
____________________________________________________________________________________________________
res2c_branch2a (Conv2D)          (None, 49, 49, 64)    16448       activation_7[0][0]               
____________________________________________________________________________________________________
bn2c_branch2a (BatchNormalizatio (None, 49, 49, 64)    256         res2c_branch2a[0][0]             
____________________________________________________________________________________________________
activation_8 (Activation)        (None, 49, 49, 64)    0           bn2c_branch2a[0][0]              
____________________________________________________________________________________________________
res2c_branch2b (Conv2D)          (None, 49, 49, 64)    36928       activation_8[0][0]               
____________________________________________________________________________________________________
bn2c_branch2b (BatchNormalizatio (None, 49, 49, 64)    256         res2c_branch2b[0][0]             
____________________________________________________________________________________________________
activation_9 (Activation)        (None, 49, 49, 64)    0           bn2c_branch2b[0][0]              
____________________________________________________________________________________________________
res2c_branch2c (Conv2D)          (None, 49, 49, 256)   16640       activation_9[0][0]               
____________________________________________________________________________________________________
bn2c_branch2c (BatchNormalizatio (None, 49, 49, 256)   1024        res2c_branch2c[0][0]             
____________________________________________________________________________________________________
add_3 (Add)                      (None, 49, 49, 256)   0           bn2c_branch2c[0][0]              
                                                                   activation_7[0][0]               
____________________________________________________________________________________________________
activation_10 (Activation)       (None, 49, 49, 256)   0           add_3[0][0]                      
____________________________________________________________________________________________________
res3a_branch2a (Conv2D)          (None, 25, 25, 128)   32896       activation_10[0][0]              
____________________________________________________________________________________________________
bn3a_branch2a (BatchNormalizatio (None, 25, 25, 128)   512         res3a_branch2a[0][0]             
____________________________________________________________________________________________________
activation_11 (Activation)       (None, 25, 25, 128)   0           bn3a_branch2a[0][0]              
____________________________________________________________________________________________________
res3a_branch2b (Conv2D)          (None, 25, 25, 128)   147584      activation_11[0][0]              
____________________________________________________________________________________________________
bn3a_branch2b (BatchNormalizatio (None, 25, 25, 128)   512         res3a_branch2b[0][0]             
____________________________________________________________________________________________________
activation_12 (Activation)       (None, 25, 25, 128)   0           bn3a_branch2b[0][0]              
____________________________________________________________________________________________________
res3a_branch2c (Conv2D)          (None, 25, 25, 512)   66048       activation_12[0][0]              
____________________________________________________________________________________________________
res3a_branch1 (Conv2D)           (None, 25, 25, 512)   131584      activation_10[0][0]              
____________________________________________________________________________________________________
bn3a_branch2c (BatchNormalizatio (None, 25, 25, 512)   2048        res3a_branch2c[0][0]             
____________________________________________________________________________________________________
bn3a_branch1 (BatchNormalization (None, 25, 25, 512)   2048        res3a_branch1[0][0]              
____________________________________________________________________________________________________
add_4 (Add)                      (None, 25, 25, 512)   0           bn3a_branch2c[0][0]              
                                                                   bn3a_branch1[0][0]               
____________________________________________________________________________________________________
activation_13 (Activation)       (None, 25, 25, 512)   0           add_4[0][0]                      
____________________________________________________________________________________________________
res3b_branch2a (Conv2D)          (None, 25, 25, 128)   65664       activation_13[0][0]              
____________________________________________________________________________________________________
bn3b_branch2a (BatchNormalizatio (None, 25, 25, 128)   512         res3b_branch2a[0][0]             
____________________________________________________________________________________________________
activation_14 (Activation)       (None, 25, 25, 128)   0           bn3b_branch2a[0][0]              
____________________________________________________________________________________________________
res3b_branch2b (Conv2D)          (None, 25, 25, 128)   147584      activation_14[0][0]              
____________________________________________________________________________________________________
bn3b_branch2b (BatchNormalizatio (None, 25, 25, 128)   512         res3b_branch2b[0][0]             
____________________________________________________________________________________________________
activation_15 (Activation)       (None, 25, 25, 128)   0           bn3b_branch2b[0][0]              
____________________________________________________________________________________________________
res3b_branch2c (Conv2D)          (None, 25, 25, 512)   66048       activation_15[0][0]              
____________________________________________________________________________________________________
bn3b_branch2c (BatchNormalizatio (None, 25, 25, 512)   2048        res3b_branch2c[0][0]             
____________________________________________________________________________________________________
add_5 (Add)                      (None, 25, 25, 512)   0           bn3b_branch2c[0][0]              
                                                                   activation_13[0][0]              
____________________________________________________________________________________________________
activation_16 (Activation)       (None, 25, 25, 512)   0           add_5[0][0]                      
____________________________________________________________________________________________________
res3c_branch2a (Conv2D)          (None, 25, 25, 128)   65664       activation_16[0][0]              
____________________________________________________________________________________________________
bn3c_branch2a (BatchNormalizatio (None, 25, 25, 128)   512         res3c_branch2a[0][0]             
____________________________________________________________________________________________________
activation_17 (Activation)       (None, 25, 25, 128)   0           bn3c_branch2a[0][0]              
____________________________________________________________________________________________________
res3c_branch2b (Conv2D)          (None, 25, 25, 128)   147584      activation_17[0][0]              
____________________________________________________________________________________________________
bn3c_branch2b (BatchNormalizatio (None, 25, 25, 128)   512         res3c_branch2b[0][0]             
____________________________________________________________________________________________________
activation_18 (Activation)       (None, 25, 25, 128)   0           bn3c_branch2b[0][0]              
____________________________________________________________________________________________________
res3c_branch2c (Conv2D)          (None, 25, 25, 512)   66048       activation_18[0][0]              
____________________________________________________________________________________________________
bn3c_branch2c (BatchNormalizatio (None, 25, 25, 512)   2048        res3c_branch2c[0][0]             
____________________________________________________________________________________________________
add_6 (Add)                      (None, 25, 25, 512)   0           bn3c_branch2c[0][0]              
                                                                   activation_16[0][0]              
____________________________________________________________________________________________________
activation_19 (Activation)       (None, 25, 25, 512)   0           add_6[0][0]                      
____________________________________________________________________________________________________
res3d_branch2a (Conv2D)          (None, 25, 25, 128)   65664       activation_19[0][0]              
____________________________________________________________________________________________________
bn3d_branch2a (BatchNormalizatio (None, 25, 25, 128)   512         res3d_branch2a[0][0]             
____________________________________________________________________________________________________
activation_20 (Activation)       (None, 25, 25, 128)   0           bn3d_branch2a[0][0]              
____________________________________________________________________________________________________
res3d_branch2b (Conv2D)          (None, 25, 25, 128)   147584      activation_20[0][0]              
____________________________________________________________________________________________________
bn3d_branch2b (BatchNormalizatio (None, 25, 25, 128)   512         res3d_branch2b[0][0]             
____________________________________________________________________________________________________
activation_21 (Activation)       (None, 25, 25, 128)   0           bn3d_branch2b[0][0]              
____________________________________________________________________________________________________
res3d_branch2c (Conv2D)          (None, 25, 25, 512)   66048       activation_21[0][0]              
____________________________________________________________________________________________________
bn3d_branch2c (BatchNormalizatio (None, 25, 25, 512)   2048        res3d_branch2c[0][0]             
____________________________________________________________________________________________________
add_7 (Add)                      (None, 25, 25, 512)   0           bn3d_branch2c[0][0]              
                                                                   activation_19[0][0]              
____________________________________________________________________________________________________
activation_22 (Activation)       (None, 25, 25, 512)   0           add_7[0][0]                      
____________________________________________________________________________________________________
res4a_branch2a (Conv2D)          (None, 13, 13, 256)   131328      activation_22[0][0]              
____________________________________________________________________________________________________
bn4a_branch2a (BatchNormalizatio (None, 13, 13, 256)   1024        res4a_branch2a[0][0]             
____________________________________________________________________________________________________
activation_23 (Activation)       (None, 13, 13, 256)   0           bn4a_branch2a[0][0]              
____________________________________________________________________________________________________
res4a_branch2b (Conv2D)          (None, 13, 13, 256)   590080      activation_23[0][0]              
____________________________________________________________________________________________________
bn4a_branch2b (BatchNormalizatio (None, 13, 13, 256)   1024        res4a_branch2b[0][0]             
____________________________________________________________________________________________________
activation_24 (Activation)       (None, 13, 13, 256)   0           bn4a_branch2b[0][0]              
____________________________________________________________________________________________________
res4a_branch2c (Conv2D)          (None, 13, 13, 1024)  263168      activation_24[0][0]              
____________________________________________________________________________________________________
res4a_branch1 (Conv2D)           (None, 13, 13, 1024)  525312      activation_22[0][0]              
____________________________________________________________________________________________________
bn4a_branch2c (BatchNormalizatio (None, 13, 13, 1024)  4096        res4a_branch2c[0][0]             
____________________________________________________________________________________________________
bn4a_branch1 (BatchNormalization (None, 13, 13, 1024)  4096        res4a_branch1[0][0]              
____________________________________________________________________________________________________
add_8 (Add)                      (None, 13, 13, 1024)  0           bn4a_branch2c[0][0]              
                                                                   bn4a_branch1[0][0]               
____________________________________________________________________________________________________
activation_25 (Activation)       (None, 13, 13, 1024)  0           add_8[0][0]                      
____________________________________________________________________________________________________
res4b_branch2a (Conv2D)          (None, 13, 13, 256)   262400      activation_25[0][0]              
____________________________________________________________________________________________________
bn4b_branch2a (BatchNormalizatio (None, 13, 13, 256)   1024        res4b_branch2a[0][0]             
____________________________________________________________________________________________________
activation_26 (Activation)       (None, 13, 13, 256)   0           bn4b_branch2a[0][0]              
____________________________________________________________________________________________________
res4b_branch2b (Conv2D)          (None, 13, 13, 256)   590080      activation_26[0][0]              
____________________________________________________________________________________________________
bn4b_branch2b (BatchNormalizatio (None, 13, 13, 256)   1024        res4b_branch2b[0][0]             
____________________________________________________________________________________________________
activation_27 (Activation)       (None, 13, 13, 256)   0           bn4b_branch2b[0][0]              
____________________________________________________________________________________________________
res4b_branch2c (Conv2D)          (None, 13, 13, 1024)  263168      activation_27[0][0]              
____________________________________________________________________________________________________
bn4b_branch2c (BatchNormalizatio (None, 13, 13, 1024)  4096        res4b_branch2c[0][0]             
____________________________________________________________________________________________________
add_9 (Add)                      (None, 13, 13, 1024)  0           bn4b_branch2c[0][0]              
                                                                   activation_25[0][0]              
____________________________________________________________________________________________________
activation_28 (Activation)       (None, 13, 13, 1024)  0           add_9[0][0]                      
____________________________________________________________________________________________________
res4c_branch2a (Conv2D)          (None, 13, 13, 256)   262400      activation_28[0][0]              
____________________________________________________________________________________________________
bn4c_branch2a (BatchNormalizatio (None, 13, 13, 256)   1024        res4c_branch2a[0][0]             
____________________________________________________________________________________________________
activation_29 (Activation)       (None, 13, 13, 256)   0           bn4c_branch2a[0][0]              
____________________________________________________________________________________________________
res4c_branch2b (Conv2D)          (None, 13, 13, 256)   590080      activation_29[0][0]              
____________________________________________________________________________________________________
bn4c_branch2b (BatchNormalizatio (None, 13, 13, 256)   1024        res4c_branch2b[0][0]             
____________________________________________________________________________________________________
activation_30 (Activation)       (None, 13, 13, 256)   0           bn4c_branch2b[0][0]              
____________________________________________________________________________________________________
res4c_branch2c (Conv2D)          (None, 13, 13, 1024)  263168      activation_30[0][0]              
____________________________________________________________________________________________________
bn4c_branch2c (BatchNormalizatio (None, 13, 13, 1024)  4096        res4c_branch2c[0][0]             
____________________________________________________________________________________________________
add_10 (Add)                     (None, 13, 13, 1024)  0           bn4c_branch2c[0][0]              
                                                                   activation_28[0][0]              
____________________________________________________________________________________________________
activation_31 (Activation)       (None, 13, 13, 1024)  0           add_10[0][0]                     
____________________________________________________________________________________________________
res4d_branch2a (Conv2D)          (None, 13, 13, 256)   262400      activation_31[0][0]              
____________________________________________________________________________________________________
bn4d_branch2a (BatchNormalizatio (None, 13, 13, 256)   1024        res4d_branch2a[0][0]             
____________________________________________________________________________________________________
activation_32 (Activation)       (None, 13, 13, 256)   0           bn4d_branch2a[0][0]              
____________________________________________________________________________________________________
res4d_branch2b (Conv2D)          (None, 13, 13, 256)   590080      activation_32[0][0]              
____________________________________________________________________________________________________
bn4d_branch2b (BatchNormalizatio (None, 13, 13, 256)   1024        res4d_branch2b[0][0]             
____________________________________________________________________________________________________
activation_33 (Activation)       (None, 13, 13, 256)   0           bn4d_branch2b[0][0]              
____________________________________________________________________________________________________
res4d_branch2c (Conv2D)          (None, 13, 13, 1024)  263168      activation_33[0][0]              
____________________________________________________________________________________________________
bn4d_branch2c (BatchNormalizatio (None, 13, 13, 1024)  4096        res4d_branch2c[0][0]             
____________________________________________________________________________________________________
add_11 (Add)                     (None, 13, 13, 1024)  0           bn4d_branch2c[0][0]              
                                                                   activation_31[0][0]              
____________________________________________________________________________________________________
activation_34 (Activation)       (None, 13, 13, 1024)  0           add_11[0][0]                     
____________________________________________________________________________________________________
res4e_branch2a (Conv2D)          (None, 13, 13, 256)   262400      activation_34[0][0]              
____________________________________________________________________________________________________
bn4e_branch2a (BatchNormalizatio (None, 13, 13, 256)   1024        res4e_branch2a[0][0]             
____________________________________________________________________________________________________
activation_35 (Activation)       (None, 13, 13, 256)   0           bn4e_branch2a[0][0]              
____________________________________________________________________________________________________
res4e_branch2b (Conv2D)          (None, 13, 13, 256)   590080      activation_35[0][0]              
____________________________________________________________________________________________________
bn4e_branch2b (BatchNormalizatio (None, 13, 13, 256)   1024        res4e_branch2b[0][0]             
____________________________________________________________________________________________________
activation_36 (Activation)       (None, 13, 13, 256)   0           bn4e_branch2b[0][0]              
____________________________________________________________________________________________________
res4e_branch2c (Conv2D)          (None, 13, 13, 1024)  263168      activation_36[0][0]              
____________________________________________________________________________________________________
bn4e_branch2c (BatchNormalizatio (None, 13, 13, 1024)  4096        res4e_branch2c[0][0]             
____________________________________________________________________________________________________
add_12 (Add)                     (None, 13, 13, 1024)  0           bn4e_branch2c[0][0]              
                                                                   activation_34[0][0]              
____________________________________________________________________________________________________
activation_37 (Activation)       (None, 13, 13, 1024)  0           add_12[0][0]                     
____________________________________________________________________________________________________
res4f_branch2a (Conv2D)          (None, 13, 13, 256)   262400      activation_37[0][0]              
____________________________________________________________________________________________________
bn4f_branch2a (BatchNormalizatio (None, 13, 13, 256)   1024        res4f_branch2a[0][0]             
____________________________________________________________________________________________________
activation_38 (Activation)       (None, 13, 13, 256)   0           bn4f_branch2a[0][0]              
____________________________________________________________________________________________________
res4f_branch2b (Conv2D)          (None, 13, 13, 256)   590080      activation_38[0][0]              
____________________________________________________________________________________________________
bn4f_branch2b (BatchNormalizatio (None, 13, 13, 256)   1024        res4f_branch2b[0][0]             
____________________________________________________________________________________________________
activation_39 (Activation)       (None, 13, 13, 256)   0           bn4f_branch2b[0][0]              
____________________________________________________________________________________________________
res4f_branch2c (Conv2D)          (None, 13, 13, 1024)  263168      activation_39[0][0]              
____________________________________________________________________________________________________
bn4f_branch2c (BatchNormalizatio (None, 13, 13, 1024)  4096        res4f_branch2c[0][0]             
____________________________________________________________________________________________________
add_13 (Add)                     (None, 13, 13, 1024)  0           bn4f_branch2c[0][0]              
                                                                   activation_37[0][0]              
____________________________________________________________________________________________________
activation_40 (Activation)       (None, 13, 13, 1024)  0           add_13[0][0]                     
____________________________________________________________________________________________________
res5a_branch2a (Conv2D)          (None, 7, 7, 512)     524800      activation_40[0][0]              
____________________________________________________________________________________________________
bn5a_branch2a (BatchNormalizatio (None, 7, 7, 512)     2048        res5a_branch2a[0][0]             
____________________________________________________________________________________________________
activation_41 (Activation)       (None, 7, 7, 512)     0           bn5a_branch2a[0][0]              
____________________________________________________________________________________________________
res5a_branch2b (Conv2D)          (None, 7, 7, 512)     2359808     activation_41[0][0]              
____________________________________________________________________________________________________
bn5a_branch2b (BatchNormalizatio (None, 7, 7, 512)     2048        res5a_branch2b[0][0]             
____________________________________________________________________________________________________
activation_42 (Activation)       (None, 7, 7, 512)     0           bn5a_branch2b[0][0]              
____________________________________________________________________________________________________
res5a_branch2c (Conv2D)          (None, 7, 7, 2048)    1050624     activation_42[0][0]              
____________________________________________________________________________________________________
res5a_branch1 (Conv2D)           (None, 7, 7, 2048)    2099200     activation_40[0][0]              
____________________________________________________________________________________________________
bn5a_branch2c (BatchNormalizatio (None, 7, 7, 2048)    8192        res5a_branch2c[0][0]             
____________________________________________________________________________________________________
bn5a_branch1 (BatchNormalization (None, 7, 7, 2048)    8192        res5a_branch1[0][0]              
____________________________________________________________________________________________________
add_14 (Add)                     (None, 7, 7, 2048)    0           bn5a_branch2c[0][0]              
                                                                   bn5a_branch1[0][0]               
____________________________________________________________________________________________________
activation_43 (Activation)       (None, 7, 7, 2048)    0           add_14[0][0]                     
____________________________________________________________________________________________________
res5b_branch2a (Conv2D)          (None, 7, 7, 512)     1049088     activation_43[0][0]              
____________________________________________________________________________________________________
bn5b_branch2a (BatchNormalizatio (None, 7, 7, 512)     2048        res5b_branch2a[0][0]             
____________________________________________________________________________________________________
activation_44 (Activation)       (None, 7, 7, 512)     0           bn5b_branch2a[0][0]              
____________________________________________________________________________________________________
res5b_branch2b (Conv2D)          (None, 7, 7, 512)     2359808     activation_44[0][0]              
____________________________________________________________________________________________________
bn5b_branch2b (BatchNormalizatio (None, 7, 7, 512)     2048        res5b_branch2b[0][0]             
____________________________________________________________________________________________________
activation_45 (Activation)       (None, 7, 7, 512)     0           bn5b_branch2b[0][0]              
____________________________________________________________________________________________________
res5b_branch2c (Conv2D)          (None, 7, 7, 2048)    1050624     activation_45[0][0]              
____________________________________________________________________________________________________
bn5b_branch2c (BatchNormalizatio (None, 7, 7, 2048)    8192        res5b_branch2c[0][0]             
____________________________________________________________________________________________________
add_15 (Add)                     (None, 7, 7, 2048)    0           bn5b_branch2c[0][0]              
                                                                   activation_43[0][0]              
____________________________________________________________________________________________________
activation_46 (Activation)       (None, 7, 7, 2048)    0           add_15[0][0]                     
____________________________________________________________________________________________________
res5c_branch2a (Conv2D)          (None, 7, 7, 512)     1049088     activation_46[0][0]              
____________________________________________________________________________________________________
bn5c_branch2a (BatchNormalizatio (None, 7, 7, 512)     2048        res5c_branch2a[0][0]             
____________________________________________________________________________________________________
activation_47 (Activation)       (None, 7, 7, 512)     0           bn5c_branch2a[0][0]              
____________________________________________________________________________________________________
res5c_branch2b (Conv2D)          (None, 7, 7, 512)     2359808     activation_47[0][0]              
____________________________________________________________________________________________________
bn5c_branch2b (BatchNormalizatio (None, 7, 7, 512)     2048        res5c_branch2b[0][0]             
____________________________________________________________________________________________________
activation_48 (Activation)       (None, 7, 7, 512)     0           bn5c_branch2b[0][0]              
____________________________________________________________________________________________________
res5c_branch2c (Conv2D)          (None, 7, 7, 2048)    1050624     activation_48[0][0]              
____________________________________________________________________________________________________
bn5c_branch2c (BatchNormalizatio (None, 7, 7, 2048)    8192        res5c_branch2c[0][0]             
____________________________________________________________________________________________________
add_16 (Add)                     (None, 7, 7, 2048)    0           bn5c_branch2c[0][0]              
                                                                   activation_46[0][0]              
____________________________________________________________________________________________________
activation_49 (Activation)       (None, 7, 7, 2048)    0           add_16[0][0]                     
____________________________________________________________________________________________________
avg_pool (AveragePooling2D)      (None, 1, 1, 2048)    0           activation_49[0][0]              
____________________________________________________________________________________________________
global_average_pooling2d_1 (Glob (None, 2048)          0           avg_pool[0][0]                   
____________________________________________________________________________________________________
dense_1 (Dense)                  (None, 2048)          4196352     global_average_pooling2d_1[0][0] 
____________________________________________________________________________________________________
dense_2 (Dense)                  (None, 7)             14343       dense_1[0][0]                    
====================================================================================================
Total params: 27,798,407
Trainable params: 4,210,695
Non-trainable params: 23,587,712
____________________________________________________________________________________________________

In [12]:
optimizer = keras.optimizers.Adam(0.00001)

loss = keras.losses.categorical_crossentropy

metrics = [
    "accuracy"
]

model.compile(optimizer, loss, metrics)

Train the network


In [ ]:
start = time.time()

# -------- If using Tensorflow (CPU) : -------- #

# init = tensorflow.global_variables_initializer()

# session.run(init)

# -------------------------------------------- #

model.fit_generator(
    callbacks=[
        keras.callbacks.CSVLogger( os.path.join(model_directory, 'training.csv') ),
        keras.callbacks.ModelCheckpoint( os.path.join(model_directory, 'checkpoint.hdf5') )
    ],
    epochs=18,
    class_weight = class_weights,
    verbose = 0, # ON/OFF printing output
    generator = training_generator,
    max_q_size = 256,
    steps_per_epoch=2500,
    validation_data = validation_generator,
    validation_steps = 2500
)

stop = time.time()
print(stop - start, "s")

In [14]:
model.save( os.path.join(model_directory, 'model.h5') )

In [15]:
session.close()