Exemple #1
0
def net_nodule2d_swethasubramanian(image_dims):

    #image augmentation
    img_aug = ImageAugmentation()
    img_aug.add_random_flip_leftright()
    img_aug.add_random_flip_updown()
    img_aug.add_random_rotation(max_angle=25.)
    img_aug.add_random_blur(sigma_max=3.)
    
    #image pre-processing
    img_prep = ImagePreprocessing()
    img_prep.add_featurewise_zero_center()
    img_prep.add_featurewise_stdnorm()
    
    net = layers.core.input_data(shape=[None, image_dims[0], image_dims[1], image_dims[2], image_dims[3]], dtype=tf.float32, data_preprocessing=img_prep, data_augmentation=img_aug)
    
    net = layers.conv.conv_2d(net, 50, 3, activation='relu')
    net = layers.conv.max_pool_2d(net, 2)
    net = layers.conv.conv_2d(net, 64, 3, activation='relu')
    net = layers.conv.conv_2d(net, 64, 3, activation='relu')
    net = layers.conv.max_pool_2d(net, 2)
    net = layers.core.fully_connected(net, 512, activation='relu')
    net = layers.core.dropout(net, 0.5)
    net = layers.core.fully_connected(net, 2, activation='softmax')

    net = layers.estimator.regression(net, optimizer='adam',
                                      loss='categorical_crossentropy',
                                      learning_rate=0.001)
    return net
Exemple #2
0
def network(img_shape, name, LR):
    # # Real-time data preprocessing
    img_prep = ImagePreprocessing()
    img_prep.add_featurewise_zero_center()
    img_prep.add_featurewise_stdnorm()
    #
    # # Real-time data augmentation
    img_aug = ImageAugmentation()
    img_aug.add_random_blur(sigma_max=3.0)
    img_aug.add_random_flip_leftright()
    img_aug.add_random_flip_updown()
    img_aug.add_random_90degrees_rotation(rotations=[0, 2])

    network = input_data(shape=img_shape,
                         name=name,
                         data_preprocessing=img_prep,
                         data_augmentation=img_aug)
    # def rete(img_shape, name, LR):
    #     network = input_data(shape=img_shape, name=name)
    network = conv_2d(network, 32, 3, activation='relu')
    network = max_pool_2d(network, 2)
    network = conv_2d(network, 64, 3, activation='relu')
    network = conv_2d(network, 64, 3, activation='relu')
    network = max_pool_2d(network, 2)
    network = fully_connected(network, 512, activation='relu')
    network = dropout(network, 0.5)
    network = fully_connected(network, 2, activation='softmax')
    network = regression(network,
                         optimizer='adam',
                         loss='categorical_crossentropy',
                         learning_rate=LR,
                         name='targets')
    return network
Exemple #3
0
def network(img_shape, name, LR):

    img_prep = ImagePreprocessing()
    img_prep.add_featurewise_zero_center()
    img_prep.add_featurewise_stdnorm()
    #
    # # Real-time data augmentation
    img_aug = ImageAugmentation()
    img_aug.add_random_blur (sigma_max=3.0)
    img_aug.add_random_flip_leftright()
    img_aug.add_random_flip_updown()
    img_aug.add_random_90degrees_rotation(rotations=[0, 2])    

    # Building 'AlexNet'
    network = input_data(shape=img_shape, name=name, data_preprocessing=img_prep, data_augmentation=img_aug  )
    network = conv_2d(network, 96, 11, strides=4, activation='relu')
    network = max_pool_2d(network, 3, strides=2)
    network = local_response_normalization(network)
    network = conv_2d(network, 256, 5, activation='relu')
    network = max_pool_2d(network, 3, strides=2)
    network = local_response_normalization(network)
    network = conv_2d(network, 384, 3, activation='relu')
    network = conv_2d(network, 384, 3, activation='relu')
    network = conv_2d(network, 256, 3, activation='relu')
    network = max_pool_2d(network, 3, strides=2)
    network = local_response_normalization(network)
    network = fully_connected(network, 4096, activation='tanh')
    network = dropout(network, 0.5)
    network = fully_connected(network, 4096, activation='tanh')
    network = dropout(network, 0.5)
    network = fully_connected(network, 2, activation='softmax')
    network = regression(network, optimizer='momentum',
                         loss='categorical_crossentropy',
                         learning_rate=LR, name='targets')
    return network
Exemple #4
0
def train_model(images,labels,input_size,kernel_size,cwd_data,cwd_checkpoint,run_name,num_epoch=40,num_labels=6):
	'''Trains a CNN network and saves the trained model in cwd_checkpoint path

	:param np.float32 images: RGB images with shape (training_size, input_size, input_size, 3)
	:param np.float32 labels: labels with shape (training_size, num_labels)
	:param int input_size: width and height of images
	:param int kernel_size: kernel size of network
	:param str cwd_data: path to data folder
	:param str cwd_data: path to checkpoint folder
	:param int num_epoch: number of epochs model should train for
	:param int num_labels: number of classes the network trains for
	'''

	# Real-time data preprocessing
	img_prep = ImagePreprocessing()
	img_prep.add_featurewise_zero_center()
	img_prep.add_featurewise_stdnorm()

	# Real-time data augmentation
	img_aug = ImageAugmentation()
	img_aug.add_random_flip_leftright()
	img_aug.add_random_rotation(max_angle=360.)
	img_aug.add_random_blur(sigma_max=3.)
	img_aug.add_random_flip_updown()

	# Convolutional network building
	network = input_data(shape=[None, input_size, input_size, 3],
                     data_preprocessing=img_prep,
                     data_augmentation=img_aug)
	network = conv_2d(network, input_size/2, kernel_size, activation='relu')
	network = max_pool_2d(network, 2)
	network = conv_2d(network, input_size, kernel_size, activation='relu')
	network = max_pool_2d(network, 2)
	network = conv_2d(network, input_size*2, kernel_size, activation='relu')
	network = max_pool_2d(network, 2)
	network = conv_2d(network, input_size*2*2, kernel_size, activation='relu')
	network = max_pool_2d(network, 2)
	network = fully_connected(network, 128, activation='relu')
	network = dropout(network, 0.5)
	network = fully_connected(network, 128, activation='relu')
	network = dropout(network, 0.5)
	network = fully_connected(network, num_labels, activation='softmax')
	network = regression(network, optimizer='adam',
                     loss='categorical_crossentropy',
                     learning_rate=0.001)

	# Train using classifier
	model = tflearn.DNN(network, tensorboard_verbose=0,tensorboard_dir=cwd_data,checkpoint_path=cwd_checkpoint,max_checkpoints=2)
	#model.load(cwd_data+'oct-cvn-48bal-6c-114300')
	model.fit(images, labels, n_epoch=num_epoch, validation_set=0.1, show_metric=True, run_id=run_name, snapshot_epoch=True)
Exemple #5
0
def network(img_shape, name, LR):

    img_prep = ImagePreprocessing()
    img_prep.add_featurewise_zero_center()
    img_prep.add_featurewise_stdnorm()
    #
    # # Real-time data augmentation
    img_aug = ImageAugmentation()
    img_aug.add_random_blur(sigma_max=3.0)
    img_aug.add_random_flip_leftright()
    img_aug.add_random_flip_updown()
    img_aug.add_random_90degrees_rotation(rotations=[0, 2])

    # Building Residual Network
    network = tflearn.input_data(shape=img_shape,
                                 name=name,
                                 data_preprocessing=img_prep,
                                 data_augmentation=img_aug)
    network = tflearn.conv_2d(network,
                              16,
                              3,
                              regularizer='L2',
                              weight_decay=0.0001)
    network = tflearn.resnext_block(network, n, 16, 32)
    network = tflearn.resnext_block(network, 1, 32, 32, downsample=True)
    network = tflearn.resnext_block(network, n - 1, 32, 32)
    network = tflearn.resnext_block(network, 1, 64, 32, downsample=True)
    network = tflearn.resnext_block(network, n - 1, 64, 32)
    network = tflearn.batch_normalization(network)
    network = tflearn.activation(network, 'relu')
    network = tflearn.global_avg_pool(network)
    # Regression
    network = tflearn.fully_connected(network, 2, activation='softmax')
    opt = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True)
    network = tflearn.regression(network,
                                 optimizer=opt,
                                 name='targets',
                                 loss='categorical_crossentropy')
    return network
Exemple #6
0
XA=np.load('/home/u3749/code/64_rescale_gray_all_images_as_vecs.npy')
YA=np.load('/home/u3749/code/64_rescale_all_image_types.npy')


# Real-time data preprocessing
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()

# Real-time data augmentation
img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_rotation(max_angle=25.)
img_aug.add_random_blur(sigma_max=3.)
img_aug.add_random_flip_updown()


# Convolutional network building
network = input_data(shape=[None, 64, 64, 3],
                     data_preprocessing=img_prep,
                     data_augmentation=img_aug)
#network = input_data(shape=[None, 32, 32, 3])
network = conv_2d(network, 32, 3, activation='relu')
network = max_pool_2d(network, 2)
network = conv_2d(network, 32, 3, activation='relu')
network = max_pool_2d(network, 2)
network = conv_2d(network, 32, 3, activation='relu')
network = max_pool_2d(network, 2)
network = fully_connected(network, 512, activation='relu',regularizer='L2')
network = dropout(network, 0.5)
Exemple #7
0
def network(img_shape, name, LR):

    img_prep = ImagePreprocessing()
    img_prep.add_featurewise_zero_center()
    img_prep.add_featurewise_stdnorm()
    #
    # # Real-time data augmentation
    img_aug = ImageAugmentation()
    img_aug.add_random_blur (sigma_max=3.0)
    img_aug.add_random_flip_leftright()
    img_aug.add_random_flip_updown()
    img_aug.add_random_90degrees_rotation(rotations=[0, 2])    

    network = input_data(shape=img_shape, name=name, data_preprocessing=img_prep, data_augmentation=img_aug  ) 
    conv1a_3_3 = relu(batch_normalization(conv_2d(network, 32, 3, strides=2, bias=False, padding='VALID',activation=None,name='Conv2d_1a_3x3')))
    conv2a_3_3 = relu(batch_normalization(conv_2d(conv1a_3_3, 32, 3, bias=False, padding='VALID',activation=None, name='Conv2d_2a_3x3')))
    conv2b_3_3 = relu(batch_normalization(conv_2d(conv2a_3_3, 64, 3, bias=False, activation=None, name='Conv2d_2b_3x3')))
    maxpool3a_3_3 = max_pool_2d(conv2b_3_3, 3, strides=2, padding='VALID', name='MaxPool_3a_3x3')
    conv3b_1_1 = relu(batch_normalization(conv_2d(maxpool3a_3_3, 80, 1, bias=False, padding='VALID',activation=None, name='Conv2d_3b_1x1')))
    conv4a_3_3 = relu(batch_normalization(conv_2d(conv3b_1_1, 192, 3, bias=False, padding='VALID',activation=None, name='Conv2d_4a_3x3')))
    maxpool5a_3_3 = max_pool_2d(conv4a_3_3, 3, strides=2, padding='VALID', name='MaxPool_5a_3x3')

    tower_conv = relu(batch_normalization(conv_2d(maxpool5a_3_3, 96, 1, bias=False, activation=None, name='Conv2d_5b_b0_1x1')))

    tower_conv1_0 = relu(batch_normalization(conv_2d(maxpool5a_3_3, 48, 1, bias=False, activation=None, name='Conv2d_5b_b1_0a_1x1')))
    tower_conv1_1 = relu(batch_normalization(conv_2d(tower_conv1_0, 64, 5, bias=False, activation=None, name='Conv2d_5b_b1_0b_5x5')))

    tower_conv2_0 = relu(batch_normalization(conv_2d(maxpool5a_3_3, 64, 1, bias=False, activation=None, name='Conv2d_5b_b2_0a_1x1')))
    tower_conv2_1 = relu(batch_normalization(conv_2d(tower_conv2_0, 96, 3, bias=False, activation=None, name='Conv2d_5b_b2_0b_3x3')))
    tower_conv2_2 = relu(batch_normalization(conv_2d(tower_conv2_1, 96, 3, bias=False, activation=None,name='Conv2d_5b_b2_0c_3x3')))

    tower_pool3_0 = avg_pool_2d(maxpool5a_3_3, 3, strides=1, padding='same', name='AvgPool_5b_b3_0a_3x3')
    tower_conv3_1 = relu(batch_normalization(conv_2d(tower_pool3_0, 64, 1, bias=False, activation=None,name='Conv2d_5b_b3_0b_1x1')))

    tower_5b_out = merge([tower_conv, tower_conv1_1, tower_conv2_2, tower_conv3_1], mode='concat', axis=3)

    net = repeat(tower_5b_out, 10, block35, scale=0.17)

    tower_conv = relu(batch_normalization(conv_2d(net, 384, 3, bias=False, strides=2,activation=None, padding='VALID', name='Conv2d_6a_b0_0a_3x3')))
    tower_conv1_0 = relu(batch_normalization(conv_2d(net, 256, 1, bias=False, activation=None, name='Conv2d_6a_b1_0a_1x1')))
    tower_conv1_1 = relu(batch_normalization(conv_2d(tower_conv1_0, 256, 3, bias=False, activation=None, name='Conv2d_6a_b1_0b_3x3')))
    tower_conv1_2 = relu(batch_normalization(conv_2d(tower_conv1_1, 384, 3, bias=False, strides=2, padding='VALID', activation=None,name='Conv2d_6a_b1_0c_3x3')))
    tower_pool = max_pool_2d(net, 3, strides=2, padding='VALID',name='MaxPool_1a_3x3')
    net = merge([tower_conv, tower_conv1_2, tower_pool], mode='concat', axis=3)
    net = repeat(net, 20, block17, scale=0.1)

    tower_conv = relu(batch_normalization(conv_2d(net, 256, 1, bias=False, activation=None, name='Conv2d_0a_1x1')))
    # tower_conv0_1 = relu(batch_normalization(conv_2d(tower_conv, 384, 3, bias=False, strides=2, padding='VALID', activation=None,name='Conv2d_0a_1x1')))
    tower_conv0_1 = relu(batch_normalization(conv_2d(tower_conv, 384, 1, bias=False, strides=2, padding='VALID', activation=None,name='Conv2d_0a_1x1')))

    tower_conv1 = relu(batch_normalization(conv_2d(net, 256, 1, bias=False, padding='VALID', activation=None,name='Conv2d_0a_1x1')))
    # tower_conv1_1 = relu(batch_normalization(conv_2d(tower_conv1,288,3, bias=False, strides=2, padding='VALID',activation=None, name='COnv2d_1a_3x3')))
    tower_conv1_1 = relu(batch_normalization(conv_2d(tower_conv1,288,1, bias=False, strides=2, padding='VALID',activation=None, name='COnv2d_1a_3x3')))

    tower_conv2 = relu(batch_normalization(conv_2d(net, 256,1, bias=False, activation=None,name='Conv2d_0a_1x1')))
    tower_conv2_1 = relu(batch_normalization(conv_2d(tower_conv2, 288,3, bias=False, name='Conv2d_0b_3x3',activation=None)))
    # tower_conv2_2 = relu(batch_normalization(conv_2d(tower_conv2_1, 320, 3, bias=False, strides=2, padding='VALID',activation=None, name='Conv2d_1a_3x3')))
    tower_conv2_2 = relu(batch_normalization(conv_2d(tower_conv2_1, 320, 1, bias=False, strides=2, padding='VALID',activation=None, name='Conv2d_1a_3x3')))

    # tower_pool = max_pool_2d(net, 3, strides=2, padding='VALID', name='MaxPool_1a_3x3')
    tower_pool = max_pool_2d(net, 1, strides=2, padding='VALID', name='MaxPool_1a_3x3')
    net = merge([tower_conv0_1, tower_conv1_1,tower_conv2_2, tower_pool], mode='concat', axis=3)

    net = repeat(net, 9, block8, scale=0.2)
    net = block8(net, activation=None)

    net = relu(batch_normalization(conv_2d(net, 1536, 1, bias=False, activation=None, name='Conv2d_7b_1x1')))
    net = avg_pool_2d(net, net.get_shape().as_list()[1:3],strides=2, padding='VALID', name='AvgPool_1a_8x8')
    net = flatten(net)
    net = dropout(net, dropout_keep_prob)
    loss = fully_connected(net, num_classes,activation='softmax')


    network = tflearn.regression(loss, optimizer='RMSprop',
                         loss='categorical_crossentropy',
                         learning_rate=0.0001, name='targets')
    return network
from __future__ import division, print_function, absolute_import

import tflearn
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d, avg_pool_2d
from tflearn.layers.normalization import local_response_normalization
from tflearn.layers.merge_ops import merge
from tflearn.layers.estimator import regression
from tflearn.data_utils import image_preloader
from tflearn.data_utils import image_preloader
from tflearn.data_augmentation import ImageAugmentation


img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_flip_updown()
img_aug.add_random_crop([224, 224], 10)
img_aug.add_random_blur()
img_aug.add_random_rotation(max_angle=25.)

X,Y = image_preloader('files_list', image_shape = (224,224),mode='file',categorical_labels=True,normalize=True,files_extension=['.jpg', '.jpeg','.png'])


network = input_data(shape=[None, 224, 224, 3],data_augmentation=img_aug)
conv1_7_7 = conv_2d(network, 64, 7, strides=2, activation='relu', name = 'conv1_7_7_s2')
pool1_3_3 = max_pool_2d(conv1_7_7, 3,strides=2)
pool1_3_3 = local_response_normalization(pool1_3_3)
conv2_3_3_reduce = conv_2d(pool1_3_3, 64,1, activation='relu',name = 'conv2_3_3_reduce')
conv2_3_3 = conv_2d(conv2_3_3_reduce, 192,3, activation='relu', name='conv2_3_3')
conv2_3_3 = local_response_normalization(conv2_3_3)
pool2_3_3 = max_pool_2d(conv2_3_3, kernel_size=3, strides=2, name='pool2_3_3_s2')
def label_on_fly(im, model_name,cwd_checkpoint, stride, box_size):
    '''Converts pixels of image into labels

    Goes through smaller image and prepares it in the same way the images 
    were prepared for training the model which will be used to predict a label
    Goes through the image line by line to avoid using too much memory 

    :param PIL image im: for prediction, 
    :parammodel to load, 
    :param path to model, 
    :param stride of scanning image
    :param box_size
    :return: rgb values of the label for each pixel
    :rtype: int tuples
    '''
    input_size = box_size
    kernel_size = get_kernel_size(input_size)
    # Real-time data preprocessing
    im_prep = ImagePreprocessing()
    im_prep.add_featurewise_zero_center()
    im_prep.add_featurewise_stdnorm()
    # Real-time data augmentation
    im_aug = ImageAugmentation()
    im_aug.add_random_flip_leftright()
    im_aug.add_random_rotation(max_angle=360.)
    im_aug.add_random_blur(sigma_max=3.)
    im_aug.add_random_flip_updown()
    # Convolutional network building
    network = input_data(shape=[None, input_size, input_size, 3],
                     data_preprocessing=im_prep,
                     data_augmentation=im_aug)
    network = conv_2d(network, input_size/2, kernel_size, activation='relu')
    network = max_pool_2d(network, 2)
    network = conv_2d(network, input_size, kernel_size, activation='relu')
    network = max_pool_2d(network, 2)
    network = conv_2d(network, input_size*2, kernel_size, activation='relu')
    network = max_pool_2d(network, 2)
    network = conv_2d(network, input_size*2*2, kernel_size, activation='relu')
    network = max_pool_2d(network, 2)
    network = fully_connected(network, 128, activation='relu')
    network = dropout(network, 0.5)
    network = fully_connected(network, 128, activation='relu')
    network = dropout(network, 0.5)
    network = fully_connected(network, 6, activation='softmax')
    network = regression(network, optimizer='adam',
                     loss='categorical_crossentropy',
                     learning_rate=0.001)
    # Defining model
    model = tflearn.DNN(network, tensorboard_verbose=0,tensorboard_dir=cwd_checkpoint,checkpoint_path=cwd_checkpoint)
    print('Loading model:', model_name)
    model.load(model_name)
    print('Sucessfully loaded model')
    
    max_box_size = box_size
    labels_predcited = []
    #Define the width and the height of the image to be cut up in smaller images
    width, height = im.size
    box = 0.2*width,0.2*height,0.8*width,0.8*height
    im = im.crop(box)
    width, height = im.size
    #Go through the height (y-axes) of the image
    for i in xrange(int((height-max_box_size)/stride)):
        center_point_y = max_box_size/2+i*stride
        im_temp = []
        predictions_temp = []
        labels_temp = []
        #Go through the width (x-axes) of the image using the same centerpoint independent of boxsize
        for j in xrange(int((width- max_box_size)/stride)):
            center_point_x = max_box_size/2+j*stride
            box = center_point_x-box_size/2, center_point_y-box_size/2, center_point_x+box_size/2,center_point_y+box_size/2
            im_temp.append(im.crop(box))
        predictions_temp = model.predict(im_temp)
        #labels_temp = [colorizer(get_label_from_cnn(predictions_temp[k])) for k in xrange(len(predictions_temp))]
        labels_temp = [get_label_from_cnn(predictions_temp[k]) for k in xrange(len(predictions_temp))]
        labels_predcited.append(labels_temp)
        print('Line %s done' % str(i))
    labels_final = [item for m in xrange(len(labels_predcited)) for item in labels_predcited[m]]
    return(labels_final)
Exemple #10
0
def network(img_shape, name, LR):

    img_prep = ImagePreprocessing()
    img_prep.add_featurewise_zero_center()
    img_prep.add_featurewise_stdnorm()
    #
    # # Real-time data augmentation
    img_aug = ImageAugmentation()
    img_aug.add_random_blur(sigma_max=3.0)
    img_aug.add_random_flip_leftright()
    img_aug.add_random_flip_updown()
    img_aug.add_random_90degrees_rotation(rotations=[0, 2])

    network = input_data(shape=img_shape,
                         name=name,
                         data_preprocessing=img_prep,
                         data_augmentation=img_aug)
    conv1_7_7 = conv_2d(network,
                        64,
                        7,
                        strides=2,
                        activation='relu',
                        name='conv1_7_7_s2')
    pool1_3_3 = max_pool_2d(conv1_7_7, 3, strides=2)
    pool1_3_3 = local_response_normalization(pool1_3_3)
    conv2_3_3_reduce = conv_2d(pool1_3_3,
                               64,
                               1,
                               activation='relu',
                               name='conv2_3_3_reduce')
    conv2_3_3 = conv_2d(conv2_3_3_reduce,
                        192,
                        3,
                        activation='relu',
                        name='conv2_3_3')
    conv2_3_3 = local_response_normalization(conv2_3_3)
    pool2_3_3 = max_pool_2d(conv2_3_3,
                            kernel_size=3,
                            strides=2,
                            name='pool2_3_3_s2')
    inception_3a_1_1 = conv_2d(pool2_3_3,
                               64,
                               1,
                               activation='relu',
                               name='inception_3a_1_1')
    inception_3a_3_3_reduce = conv_2d(pool2_3_3,
                                      96,
                                      1,
                                      activation='relu',
                                      name='inception_3a_3_3_reduce')
    inception_3a_3_3 = conv_2d(inception_3a_3_3_reduce,
                               128,
                               filter_size=3,
                               activation='relu',
                               name='inception_3a_3_3')
    inception_3a_5_5_reduce = conv_2d(pool2_3_3,
                                      16,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_3a_5_5_reduce')
    inception_3a_5_5 = conv_2d(inception_3a_5_5_reduce,
                               32,
                               filter_size=5,
                               activation='relu',
                               name='inception_3a_5_5')
    inception_3a_pool = max_pool_2d(
        pool2_3_3,
        kernel_size=3,
        strides=1,
    )
    inception_3a_pool_1_1 = conv_2d(inception_3a_pool,
                                    32,
                                    filter_size=1,
                                    activation='relu',
                                    name='inception_3a_pool_1_1')

    # merge the inception_3a__
    inception_3a_output = merge([
        inception_3a_1_1, inception_3a_3_3, inception_3a_5_5,
        inception_3a_pool_1_1
    ],
                                mode='concat',
                                axis=3)

    inception_3b_1_1 = conv_2d(inception_3a_output,
                               128,
                               filter_size=1,
                               activation='relu',
                               name='inception_3b_1_1')
    inception_3b_3_3_reduce = conv_2d(inception_3a_output,
                                      128,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_3b_3_3_reduce')
    inception_3b_3_3 = conv_2d(inception_3b_3_3_reduce,
                               192,
                               filter_size=3,
                               activation='relu',
                               name='inception_3b_3_3')
    inception_3b_5_5_reduce = conv_2d(inception_3a_output,
                                      32,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_3b_5_5_reduce')
    inception_3b_5_5 = conv_2d(inception_3b_5_5_reduce,
                               96,
                               filter_size=5,
                               name='inception_3b_5_5')
    inception_3b_pool = max_pool_2d(inception_3a_output,
                                    kernel_size=3,
                                    strides=1,
                                    name='inception_3b_pool')
    inception_3b_pool_1_1 = conv_2d(inception_3b_pool,
                                    64,
                                    filter_size=1,
                                    activation='relu',
                                    name='inception_3b_pool_1_1')

    #merge the inception_3b_*
    inception_3b_output = merge([
        inception_3b_1_1, inception_3b_3_3, inception_3b_5_5,
        inception_3b_pool_1_1
    ],
                                mode='concat',
                                axis=3,
                                name='inception_3b_output')

    pool3_3_3 = max_pool_2d(inception_3b_output,
                            kernel_size=3,
                            strides=2,
                            name='pool3_3_3')
    inception_4a_1_1 = conv_2d(pool3_3_3,
                               192,
                               filter_size=1,
                               activation='relu',
                               name='inception_4a_1_1')
    inception_4a_3_3_reduce = conv_2d(pool3_3_3,
                                      96,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_4a_3_3_reduce')
    inception_4a_3_3 = conv_2d(inception_4a_3_3_reduce,
                               208,
                               filter_size=3,
                               activation='relu',
                               name='inception_4a_3_3')
    inception_4a_5_5_reduce = conv_2d(pool3_3_3,
                                      16,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_4a_5_5_reduce')
    inception_4a_5_5 = conv_2d(inception_4a_5_5_reduce,
                               48,
                               filter_size=5,
                               activation='relu',
                               name='inception_4a_5_5')
    inception_4a_pool = max_pool_2d(pool3_3_3,
                                    kernel_size=3,
                                    strides=1,
                                    name='inception_4a_pool')
    inception_4a_pool_1_1 = conv_2d(inception_4a_pool,
                                    64,
                                    filter_size=1,
                                    activation='relu',
                                    name='inception_4a_pool_1_1')

    inception_4a_output = merge([
        inception_4a_1_1, inception_4a_3_3, inception_4a_5_5,
        inception_4a_pool_1_1
    ],
                                mode='concat',
                                axis=3,
                                name='inception_4a_output')

    inception_4b_1_1 = conv_2d(inception_4a_output,
                               160,
                               filter_size=1,
                               activation='relu',
                               name='inception_4a_1_1')
    inception_4b_3_3_reduce = conv_2d(inception_4a_output,
                                      112,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_4b_3_3_reduce')
    inception_4b_3_3 = conv_2d(inception_4b_3_3_reduce,
                               224,
                               filter_size=3,
                               activation='relu',
                               name='inception_4b_3_3')
    inception_4b_5_5_reduce = conv_2d(inception_4a_output,
                                      24,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_4b_5_5_reduce')
    inception_4b_5_5 = conv_2d(inception_4b_5_5_reduce,
                               64,
                               filter_size=5,
                               activation='relu',
                               name='inception_4b_5_5')

    inception_4b_pool = max_pool_2d(inception_4a_output,
                                    kernel_size=3,
                                    strides=1,
                                    name='inception_4b_pool')
    inception_4b_pool_1_1 = conv_2d(inception_4b_pool,
                                    64,
                                    filter_size=1,
                                    activation='relu',
                                    name='inception_4b_pool_1_1')

    inception_4b_output = merge([
        inception_4b_1_1, inception_4b_3_3, inception_4b_5_5,
        inception_4b_pool_1_1
    ],
                                mode='concat',
                                axis=3,
                                name='inception_4b_output')

    inception_4c_1_1 = conv_2d(inception_4b_output,
                               128,
                               filter_size=1,
                               activation='relu',
                               name='inception_4c_1_1')
    inception_4c_3_3_reduce = conv_2d(inception_4b_output,
                                      128,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_4c_3_3_reduce')
    inception_4c_3_3 = conv_2d(inception_4c_3_3_reduce,
                               256,
                               filter_size=3,
                               activation='relu',
                               name='inception_4c_3_3')
    inception_4c_5_5_reduce = conv_2d(inception_4b_output,
                                      24,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_4c_5_5_reduce')
    inception_4c_5_5 = conv_2d(inception_4c_5_5_reduce,
                               64,
                               filter_size=5,
                               activation='relu',
                               name='inception_4c_5_5')

    inception_4c_pool = max_pool_2d(inception_4b_output,
                                    kernel_size=3,
                                    strides=1)
    inception_4c_pool_1_1 = conv_2d(inception_4c_pool,
                                    64,
                                    filter_size=1,
                                    activation='relu',
                                    name='inception_4c_pool_1_1')

    inception_4c_output = merge([
        inception_4c_1_1, inception_4c_3_3, inception_4c_5_5,
        inception_4c_pool_1_1
    ],
                                mode='concat',
                                axis=3,
                                name='inception_4c_output')

    inception_4d_1_1 = conv_2d(inception_4c_output,
                               112,
                               filter_size=1,
                               activation='relu',
                               name='inception_4d_1_1')
    inception_4d_3_3_reduce = conv_2d(inception_4c_output,
                                      144,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_4d_3_3_reduce')
    inception_4d_3_3 = conv_2d(inception_4d_3_3_reduce,
                               288,
                               filter_size=3,
                               activation='relu',
                               name='inception_4d_3_3')
    inception_4d_5_5_reduce = conv_2d(inception_4c_output,
                                      32,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_4d_5_5_reduce')
    inception_4d_5_5 = conv_2d(inception_4d_5_5_reduce,
                               64,
                               filter_size=5,
                               activation='relu',
                               name='inception_4d_5_5')
    inception_4d_pool = max_pool_2d(inception_4c_output,
                                    kernel_size=3,
                                    strides=1,
                                    name='inception_4d_pool')
    inception_4d_pool_1_1 = conv_2d(inception_4d_pool,
                                    64,
                                    filter_size=1,
                                    activation='relu',
                                    name='inception_4d_pool_1_1')

    inception_4d_output = merge([
        inception_4d_1_1, inception_4d_3_3, inception_4d_5_5,
        inception_4d_pool_1_1
    ],
                                mode='concat',
                                axis=3,
                                name='inception_4d_output')

    inception_4e_1_1 = conv_2d(inception_4d_output,
                               256,
                               filter_size=1,
                               activation='relu',
                               name='inception_4e_1_1')
    inception_4e_3_3_reduce = conv_2d(inception_4d_output,
                                      160,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_4e_3_3_reduce')
    inception_4e_3_3 = conv_2d(inception_4e_3_3_reduce,
                               320,
                               filter_size=3,
                               activation='relu',
                               name='inception_4e_3_3')
    inception_4e_5_5_reduce = conv_2d(inception_4d_output,
                                      32,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_4e_5_5_reduce')
    inception_4e_5_5 = conv_2d(inception_4e_5_5_reduce,
                               128,
                               filter_size=5,
                               activation='relu',
                               name='inception_4e_5_5')
    inception_4e_pool = max_pool_2d(inception_4d_output,
                                    kernel_size=3,
                                    strides=1,
                                    name='inception_4e_pool')
    inception_4e_pool_1_1 = conv_2d(inception_4e_pool,
                                    128,
                                    filter_size=1,
                                    activation='relu',
                                    name='inception_4e_pool_1_1')

    inception_4e_output = merge([
        inception_4e_1_1, inception_4e_3_3, inception_4e_5_5,
        inception_4e_pool_1_1
    ],
                                axis=3,
                                mode='concat')

    pool4_3_3 = max_pool_2d(inception_4e_output,
                            kernel_size=3,
                            strides=2,
                            name='pool_3_3')

    inception_5a_1_1 = conv_2d(pool4_3_3,
                               256,
                               filter_size=1,
                               activation='relu',
                               name='inception_5a_1_1')
    inception_5a_3_3_reduce = conv_2d(pool4_3_3,
                                      160,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_5a_3_3_reduce')
    inception_5a_3_3 = conv_2d(inception_5a_3_3_reduce,
                               320,
                               filter_size=3,
                               activation='relu',
                               name='inception_5a_3_3')
    inception_5a_5_5_reduce = conv_2d(pool4_3_3,
                                      32,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_5a_5_5_reduce')
    inception_5a_5_5 = conv_2d(inception_5a_5_5_reduce,
                               128,
                               filter_size=5,
                               activation='relu',
                               name='inception_5a_5_5')
    inception_5a_pool = max_pool_2d(pool4_3_3,
                                    kernel_size=3,
                                    strides=1,
                                    name='inception_5a_pool')
    inception_5a_pool_1_1 = conv_2d(inception_5a_pool,
                                    128,
                                    filter_size=1,
                                    activation='relu',
                                    name='inception_5a_pool_1_1')

    inception_5a_output = merge([
        inception_5a_1_1, inception_5a_3_3, inception_5a_5_5,
        inception_5a_pool_1_1
    ],
                                axis=3,
                                mode='concat')

    inception_5b_1_1 = conv_2d(inception_5a_output,
                               384,
                               filter_size=1,
                               activation='relu',
                               name='inception_5b_1_1')
    inception_5b_3_3_reduce = conv_2d(inception_5a_output,
                                      192,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_5b_3_3_reduce')
    inception_5b_3_3 = conv_2d(inception_5b_3_3_reduce,
                               384,
                               filter_size=3,
                               activation='relu',
                               name='inception_5b_3_3')
    inception_5b_5_5_reduce = conv_2d(inception_5a_output,
                                      48,
                                      filter_size=1,
                                      activation='relu',
                                      name='inception_5b_5_5_reduce')
    inception_5b_5_5 = conv_2d(inception_5b_5_5_reduce,
                               128,
                               filter_size=5,
                               activation='relu',
                               name='inception_5b_5_5')
    inception_5b_pool = max_pool_2d(inception_5a_output,
                                    kernel_size=3,
                                    strides=1,
                                    name='inception_5b_pool')
    inception_5b_pool_1_1 = conv_2d(inception_5b_pool,
                                    128,
                                    filter_size=1,
                                    activation='relu',
                                    name='inception_5b_pool_1_1')
    inception_5b_output = merge([
        inception_5b_1_1, inception_5b_3_3, inception_5b_5_5,
        inception_5b_pool_1_1
    ],
                                axis=3,
                                mode='concat')

    pool5_7_7 = avg_pool_2d(inception_5b_output, kernel_size=7, strides=1)
    pool5_7_7 = dropout(pool5_7_7, 0.4)
    loss = fully_connected(pool5_7_7, class_num, activation='softmax')
    network = regression(loss,
                         optimizer='momentum',
                         loss='categorical_crossentropy',
                         learning_rate=LR,
                         name='targets')
    return network
Exemple #11
0
def cnn_model(x_shape, y_shape, archi="AlexNet"):
    image_aug = ImageAugmentation()
    image_aug.add_random_blur(1)
    image_aug.add_random_flip_leftright()
    image_aug.add_random_flip_updown()
    image_aug.add_random_rotation()
    image_aug.add_random_90degrees_rotation()

    # AlexNet, replacing local normalization with batch normalization.
    if archi == "AlexNet":
        net = input_data(shape=[None] + list(x_shape[1:]),
                         data_augmentation=image_aug)
        net = conv_2d(net, 96, 7, strides=2, activation='relu')

        net = batch_normalization(net)
        net = max_pool_2d(net, 2)
        net = dropout(net, 0.8)

        net = conv_2d(net, 256, 5, strides=2, activation='relu')
        net = batch_normalization(net)

        net = max_pool_2d(net, 2)
        net = dropout(net, 0.8)

        net = conv_2d(net, 384, 3, activation='relu')
        net = conv_2d(net, 384, 3, activation='relu')
        net = conv_2d(net, 256, 3, activation='relu')
        net = batch_normalization(net)
        net = max_pool_2d(net, 2)
        net = dropout(net, 0.8)

        net = fully_connected(net, 4096, activation='tanh')
        net = dropout(net, 0.5)
        net = fully_connected(net, 4096, activation='tanh')
        net = dropout(net, 0.5)
        net = fully_connected(net, y_shape[1], activation='softmax')
        net = regression(net,
                         optimizer='adam',
                         loss='categorical_crossentropy',
                         learning_rate=0.0001)

    # ResNet, with dropout.
    if archi == "ResNet":
        n = 5
        net = tflearn.input_data(shape=[None] + list(x_shape[1:]),
                                 data_augmentation=image_aug)
        net = tflearn.conv_2d(net,
                              16,
                              5,
                              strides=2,
                              regularizer='L2',
                              weight_decay=0.0001)
        net = tflearn.residual_block(net, n, 16)
        net = tflearn.residual_block(net, 1, 32, downsample=True)
        net = tflearn.dropout(net, 0.8)
        net = tflearn.residual_block(net, n - 1, 32)
        net = tflearn.residual_block(net, 1, 64, downsample=True)
        net = tflearn.dropout(net, 0.8)
        net = tflearn.residual_block(net, n - 1, 64)
        net = tflearn.batch_normalization(net)
        net = tflearn.activation(net, 'relu')
        net = tflearn.global_avg_pool(net)
        net = tflearn.fully_connected(net, y_shape[1], activation='softmax')
        net = tflearn.regression(net,
                                 optimizer='adam',
                                 loss='categorical_crossentropy',
                                 learning_rate=0.0001)

    return net