コード例 #1
0
def build_model():
    logging.info('building model')
    img_prep = ImagePreprocessing()
    img_prep.add_featurewise_zero_center()
    img_prep.add_featurewise_stdnorm()

    encoder = input_data(shape=(None, IMAGE_INPUT_SIZE[0], IMAGE_INPUT_SIZE[1],
                                3), data_preprocessing=img_prep)
    encoder = conv_2d(encoder, 16, 7, activation='relu')
    encoder = dropout(encoder, 0.25)  # you can have noisy input instead
    encoder = max_pool_2d(encoder, 2)
    encoder = conv_2d(encoder, 16, 7, activation='relu')
    encoder = max_pool_2d(encoder, 2)
    encoder = conv_2d(encoder, 8, 7, activation='relu')
    encoder = max_pool_2d(encoder, 2)
    
    decoder = conv_2d(encoder, 8, 7, activation='relu')
    decoder = upsample_2d(decoder, 2)
    decoder = conv_2d(decoder, 16, 7, activation='relu')
    decoder = upsample_2d(decoder, 2)
    decoder = conv_2d(decoder, 16, 7, activation='relu')
    decoder = upsample_2d(decoder, 2)
    decoder = conv_2d(decoder, 3, 7)

    encoded_str = re.search(r', (.*)\)', str(encoder.get_shape)).group(1)
    encoded_size = np.prod([int(o) for o in encoded_str.split(', ')])
    
    original_img_size = np.prod(IMAGE_INPUT_SIZE) * 3
    
    percentage = round(encoded_size / original_img_size, 2) * 100
    logging.debug('the encoded representation is {}% of the original \
image'.format(percentage))
    
    return regression(decoder, optimizer='adadelta',
                      loss='binary_crossentropy', learning_rate=0.005)
コード例 #2
0
def build():
	input_img = input_data(shape=(HEIGHT, WIDTH, CHANNELS), name='input');print(input_img.shape)
	INPUT = input_img
	x = conv_2d(input_img, 16, (3, 3), activation='relu', padding='same');print(x.shape)
	x = max_pool_2d(x, (2, 2), padding='same');print(x.shape)
	x = conv_2d(x, 8, (3, 3), activation='relu', padding='same');print(x.shape)
	x = max_pool_2d(x, (2, 2), padding='same');print(x.shape)
	x = conv_2d(x, 8, (3, 3), activation='relu', padding='same');print(x.shape)
	encoded = max_pool_2d(x, (2, 2), padding='same');print(encoded.shape)  # at this point the representation is (4, 4, 8) i.e. 128-dimensional

	HIDDEN_STATE = encoded
	print("middle")

	x = conv_2d(encoded, 8, (3, 3), activation='relu', padding='same', name='input2');print(x.shape)
	x = upsample_2d(x, (2, 2));print(x.shape)
	x = conv_2d(x, 8, (3, 3), activation='relu', padding='same');print(x.shape)
	x = upsample_2d(x, (2, 2));print(x.shape)
	x = conv_2d(x, 16, (3, 3), activation='relu', padding='same');print(x.shape)
	x = upsample_2d(x, (2, 2));print(x.shape)
	decoded = conv_2d(x, CHANNELS, (3, 3), activation='sigmoid', padding='same');print(decoded.shape)
	OUTPUT = decoded
	autoencoder = regression(decoded, optimizer='momentum', loss='mean_square',
							 learning_rate=0.005, name='targets')
	model = tflearn.DNN(autoencoder, checkpoint_path=MODEL_NAME,
						max_checkpoints=1, tensorboard_verbose=0, tensorboard_dir='log')
	return model, INPUT, HIDDEN_STATE, OUTPUT
コード例 #3
0
def build_fcn_all(network):
    #Pool1
    conv1 = conv_2d(network, 8, 7, activation='relu')
    pool1 = max_pool_2d(conv1, 2)
    #Pool2
    conv2 = conv_2d(pool1, 16, 5, activation='relu')
    pool2 = max_pool_2d(conv2, 2)
    #Pool3
    conv3 = conv_2d(pool2, 32, 5, activation='relu')
    pool3 = max_pool_2d(conv3, 2)  # output 8x_downsampled
    #Pool4
    conv4 = conv_2d(pool3, 64, 3, activation='relu')
    pool4 = max_pool_2d(conv4, 2)  # output 16x_downsampled

    #start FCN-32s -----------------------------------
    #Pool5
    conv5 = conv_2d(pool4, 128, 3, activation='relu')
    pool5 = max_pool_2d(conv5, 2)
    #Conv6-7
    conv6 = conv_2d(pool5, 128, 3, activation='relu')
    conv7 = conv_2d(conv6, 128, 3, activation='relu')  # output 32x_downsampled
    #end FCN-32s -----------------------------------

    ##start FCN-16s -----------------------------------
    #network_32_UP2 = upsample_2d(network_32, 2)
    #network_16 = merge([network_32_UP2, network_4], mode='concat', axis=3)
    #network_16 = conv_2d(network_16, 3, 128, activation='relu') # output 16x_downsampled
    ##end FCN-16s -----------------------------------

    ##start FCN-8s -----------------------------------
    #network_32_UP4 = upsample_2d(network_32, 4)
    #network_16_UP2  = upsample_2d(network_16, 2)
    #network_3_UP8   = upsample_2d(network_3, 8)
    pool4_x2 = upsample_2d(pool4, 2)
    conv7_x4 = upsample_2d(conv7, 4)
    #network_8 = merge([network_32_UP4, network_4_UP2, network_3], mode='concat', axis=3)
    fcn_8s = merge([pool3, pool4_x2, conv7_x4], mode='concat', axis=3)
    fcn_8s = conv_2d(fcn_8s, 3, 1, activation='relu')
    ##end FCN-8s -----------------------------------

    out = conv_2d(fcn_8s, CHANNELS, 1, activation='relu')
    out = upsample_2d(out, 8)
    #network_8 = upscore_layer(network_8, num_classes=3, kernel_size=2, strides=8, shape=[384, 1216, 3])

    network = tflearn.regression(
        out,
        loss='mean_square',
        #loss='weak_cross_entropy_2d',
    )

    return network
コード例 #4
0
 def add_upsample2d_layer(self,
                          window_size,
                          prev_layer_name=None,
                          exclude_from_path=True,
                          **kwargs):
     prev_name = prev_layer_name or self._curr_layer_name
     prev = self._layers[prev_name]
     layer = {
         "layer": conv.upsample_2d(prev, window_size, **kwargs),
         "type": "Upsample2D",
         "categories": ["hidden"]
     }
     return self._add_layer(layer, exclude_from_path)
コード例 #5
0
ファイル: buildModel.py プロジェクト: dhinkris/fetal-code
def CNN(input, reuse=False):
    with tf.variable_scope('CNN'):
        width = 96
        height = 96
        n_channels = 1
        n_classes = 2
        ################Create Model######################
        with tf.variable_scope('Block1'):
            conv1 = conv_2d(input,
                            32,
                            3,
                            activation='relu',
                            padding='same',
                            regularizer="L2",
                            reuse=reuse,
                            scope='conv1')
            conv1 = conv_2d(conv1,
                            32,
                            3,
                            activation='relu',
                            padding='same',
                            regularizer="L2",
                            reuse=reuse,
                            scope='conv2')
            pool1 = max_pool_2d(conv1, 2)

        with tf.variable_scope('Block2'):
            conv2 = conv_2d(pool1,
                            64,
                            3,
                            activation='relu',
                            padding='same',
                            regularizer="L2",
                            reuse=reuse,
                            scope='conv3')
            conv2 = conv_2d(conv2,
                            64,
                            3,
                            activation='relu',
                            padding='same',
                            regularizer="L2",
                            reuse=reuse,
                            scope='conv4')
            pool2 = max_pool_2d(conv2, 2)

        with tf.variable_scope('Block3'):
            conv3 = conv_2d(pool2,
                            128,
                            3,
                            activation='relu',
                            padding='same',
                            regularizer="L2",
                            reuse=reuse,
                            scope='conv5')
            conv3 = conv_2d(conv3,
                            128,
                            3,
                            activation='relu',
                            padding='same',
                            regularizer="L2",
                            reuse=reuse,
                            scope='conv6')
            pool3 = max_pool_2d(conv3, 2)

        with tf.variable_scope('Block4'):
            conv4 = conv_2d(pool3,
                            256,
                            3,
                            activation='relu',
                            padding='same',
                            regularizer="L2",
                            reuse=reuse,
                            scope='conv7')
            conv4 = conv_2d(conv4,
                            256,
                            3,
                            activation='relu',
                            padding='same',
                            regularizer="L2",
                            reuse=reuse,
                            scope='conv8')
            pool4 = max_pool_2d(conv4, 2)

        with tf.variable_scope('Block5'):
            conv5 = conv_2d(pool4,
                            512,
                            3,
                            activation='relu',
                            padding='same',
                            regularizer="L2",
                            reuse=reuse,
                            scope='conv9')
            conv5 = conv_2d(conv5,
                            512,
                            3,
                            activation='relu',
                            padding='same',
                            regularizer="L2",
                            reuse=reuse,
                            scope='conv10')

        with tf.variable_scope('Block6'):
            up6 = upsample_2d(conv5, 2)
            up6 = tflearn.layers.merge_ops.merge([up6, conv4],
                                                 'concat',
                                                 axis=3)
            conv6 = conv_2d(up6,
                            256,
                            3,
                            activation='relu',
                            padding='same',
                            regularizer="L2",
                            reuse=reuse,
                            scope='conv11')
            conv6 = conv_2d(conv6,
                            256,
                            3,
                            activation='relu',
                            padding='same',
                            regularizer="L2",
                            reuse=reuse,
                            scope='conv12')

        with tf.variable_scope('Block7'):
            up7 = upsample_2d(conv6, 2)
            up7 = tflearn.layers.merge_ops.merge([up7, conv3],
                                                 'concat',
                                                 axis=3)
            conv7 = conv_2d(up7,
                            128,
                            3,
                            activation='relu',
                            padding='same',
                            regularizer="L2",
                            reuse=reuse,
                            scope='conv13')
            conv7 = conv_2d(conv7,
                            128,
                            3,
                            activation='relu',
                            padding='same',
                            regularizer="L2",
                            reuse=reuse,
                            scope='conv14')

        with tf.variable_scope('Block8'):
            up8 = upsample_2d(conv7, 2)
            up8 = tflearn.layers.merge_ops.merge([up8, conv2],
                                                 'concat',
                                                 axis=3)
            conv8 = conv_2d(up8,
                            64,
                            3,
                            activation='relu',
                            padding='same',
                            regularizer="L2",
                            reuse=reuse,
                            scope='conv15')
            conv8 = conv_2d(conv8,
                            64,
                            3,
                            activation='relu',
                            padding='same',
                            regularizer="L2",
                            reuse=reuse,
                            scope='conv16')

        with tf.variable_scope('Block9'):
            up9 = upsample_2d(conv8, 2)
            up9 = tflearn.layers.merge_ops.merge([up9, conv1],
                                                 'concat',
                                                 axis=3)
            conv9 = conv_2d(up9,
                            32,
                            3,
                            activation='relu',
                            padding='same',
                            regularizer="L2",
                            reuse=reuse,
                            scope='conv17')
            conv9 = conv_2d(conv9,
                            32,
                            3,
                            activation='relu',
                            padding='same',
                            regularizer="L2",
                            reuse=reuse,
                            scope='conv18')

        with tf.variable_scope('Output'):
            pred = conv_2d(conv9,
                           2,
                           1,
                           activation='linear',
                           padding='valid',
                           reuse=reuse,
                           scope='conv19')

            pred_reshape = tf.reshape(pred, [-1, width, height, n_classes])
    return pred_reshape
コード例 #6
0
conv2 = conv_2d(pool1, 64, 3, activation='relu', padding='same', regularizer="L2")
conv2 = conv_2d(conv2, 64, 3, activation='relu', padding='same', regularizer="L2")
pool2 = max_pool_2d(conv2, 2)

conv3 = conv_2d(pool2, 128, 3, activation='relu', padding='same', regularizer="L2")
conv3 = conv_2d(conv3, 128, 3, activation='relu', padding='same', regularizer="L2")
pool3 = max_pool_2d(conv3, 2)

conv4 = conv_2d(pool3, 256, 3, activation='relu', padding='same', regularizer="L2")
conv4 = conv_2d(conv4, 256, 3, activation='relu', padding='same', regularizer="L2")
pool4 = max_pool_2d(conv4, 2)

conv5 = conv_2d(pool4, 512, 3, activation='relu', padding='same', regularizer="L2")
conv5 = conv_2d(conv5, 512, 3, activation='relu', padding='same', regularizer="L2")

up6 = upsample_2d(conv5,2)
up6 = tflearn.layers.merge_ops.merge([up6, conv4], 'concat',axis=3)
conv6 = conv_2d(up6, 256, 3, activation='relu', padding='same', regularizer="L2")
conv6 = conv_2d(conv6, 256, 3, activation='relu', padding='same', regularizer="L2")

up7 = upsample_2d(conv6,2)
up7 = tflearn.layers.merge_ops.merge([up7, conv3],'concat', axis=3)
conv7 = conv_2d(up7, 128, 3, activation='relu', padding='same', regularizer="L2")
conv7 = conv_2d(conv7, 128, 3, activation='relu', padding='same', regularizer="L2")

up8 = upsample_2d(conv7,2)
up8 = tflearn.layers.merge_ops.merge([up8, conv2],'concat', axis=3)
conv8 = conv_2d(up8, 64, 3, activation='relu', padding='same', regularizer="L2")
conv8 = conv_2d(conv8, 64, 3, activation='relu', padding='same', regularizer="L2")

up9 = upsample_2d(conv8,2)
def extractBrain(dataPath, modelCkptLoc, thresholdLoc, modelCkptSeg,
                 thresholdSeg, bidsDir, out_postfix):

    # Step1: Main part brain localization
    normalize = "local_max"
    width = 128
    height = 128
    border_x = 15
    border_y = 15
    n_channels = 1

    img_nib = nibabel.load(os.path.join(dataPath))
    image_data = img_nib.get_data()
    images = np.zeros((image_data.shape[2], width, height, n_channels))
    pred3dFinal = np.zeros((image_data.shape[2], image_data.shape[0],
                            image_data.shape[1], n_channels))

    slice_counter = 0
    for ii in range(image_data.shape[2]):
        img_patch = cv2.resize(image_data[:, :, ii],
                               dsize=(width, height),
                               fx=width,
                               fy=height)

        if normalize:
            if normalize == "local_max":
                images[slice_counter, :, :, 0] = img_patch / np.max(img_patch)
            elif normalize == "mean_std":
                images[slice_counter, :, :,
                       0] = (img_patch -
                             np.mean(img_patch)) / np.std(img_patch)
            else:
                raise ValueError('Please select a valid normalization')
        else:
            images[slice_counter, :, :, 0] = img_patch

        slice_counter += 1

    # Tensorflow graph

    g = tf.Graph()
    with g.as_default():

        with tf.name_scope('inputs'):

            x = tf.placeholder(tf.float32, [None, width, height, n_channels])

        conv1 = conv_2d(x,
                        32,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")
        conv1 = conv_2d(conv1,
                        32,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")
        pool1 = max_pool_2d(conv1, 2)

        conv2 = conv_2d(pool1,
                        64,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")
        conv2 = conv_2d(conv2,
                        64,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")
        pool2 = max_pool_2d(conv2, 2)

        conv3 = conv_2d(pool2,
                        128,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")
        conv3 = conv_2d(conv3,
                        128,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")
        pool3 = max_pool_2d(conv3, 2)

        conv4 = conv_2d(pool3,
                        256,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")
        conv4 = conv_2d(conv4,
                        256,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")
        pool4 = max_pool_2d(conv4, 2)

        conv5 = conv_2d(pool4,
                        512,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")
        conv5 = conv_2d(conv5,
                        512,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")

        up6 = upsample_2d(conv5, 2)
        up6 = tflearn.layers.merge_ops.merge([up6, conv4], 'concat', axis=3)
        conv6 = conv_2d(up6,
                        256,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")
        conv6 = conv_2d(conv6,
                        256,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")

        up7 = upsample_2d(conv6, 2)
        up7 = tflearn.layers.merge_ops.merge([up7, conv3], 'concat', axis=3)
        conv7 = conv_2d(up7,
                        128,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")
        conv7 = conv_2d(conv7,
                        128,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")

        up8 = upsample_2d(conv7, 2)
        up8 = tflearn.layers.merge_ops.merge([up8, conv2], 'concat', axis=3)
        conv8 = conv_2d(up8,
                        64,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")
        conv8 = conv_2d(conv8,
                        64,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")

        up9 = upsample_2d(conv8, 2)
        up9 = tflearn.layers.merge_ops.merge([up9, conv1], 'concat', axis=3)
        conv9 = conv_2d(up9,
                        32,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")
        conv9 = conv_2d(conv9,
                        32,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")

        pred = conv_2d(conv9, 2, 1, activation='linear', padding='valid')

    # Thresholding parameter to binarize predictions
    percentileLoc = thresholdLoc * 100
    im = np.zeros((1, width, height, n_channels))
    pred3d = []
    with tf.Session(graph=g) as sess_test_loc:
        # Restore the model
        tf_saver = tf.train.Saver()
        tf_saver.restore(sess_test_loc, modelCkptLoc)

        for idx in range(images.shape[0]):

            im = np.reshape(images[idx, :, :, :],
                            [1, width, height, n_channels])

            feed_dict = {x: im}
            pred_ = sess_test_loc.run(pred, feed_dict=feed_dict)

            theta = np.percentile(pred_, percentileLoc)
            pred_bin = np.where(pred_ > theta, 1, 0)
            pred3d.append(pred_bin[0, :, :, 0].astype('float64'))

#####

        pred3d = np.asarray(pred3d)
        heights = []
        widths = []
        coms_x = []
        coms_y = []

        # Apply PPP
        ppp = True
        if ppp:
            pred3d = post_processing(pred3d)

        pred3d = [
            cv2.resize(elem,
                       dsize=(image_data.shape[1], image_data.shape[0]),
                       interpolation=cv2.INTER_NEAREST) for elem in pred3d
        ]
        pred3d = np.asarray(pred3d)
        for i in range(np.asarray(pred3d).shape[0]):
            if np.sum(pred3d[i, :, :]) != 0:
                pred3d[i, :, :] = extractLargestCC(
                    pred3d[i, :, :].astype('uint8'))
                contours, hierarchy = cv2.findContours(
                    pred3d[i, :, :].astype('uint8'), cv2.RETR_EXTERNAL,
                    cv2.CHAIN_APPROX_SIMPLE)
                area = cv2.minAreaRect(np.squeeze(contours))
                heights.append(area[1][0])
                widths.append(area[1][1])
                bbox = cv2.boxPoints(area).astype('int')
                coms_x.append(
                    int((np.max(bbox[:, 1]) + np.min(bbox[:, 1])) / 2))
                coms_y.append(
                    int((np.max(bbox[:, 0]) + np.min(bbox[:, 0])) / 2))

# Saving localization points
        med_x = int(np.median(coms_x))
        med_y = int(np.median(coms_y))
        half_max_x = int(np.max(heights) / 2)
        half_max_y = int(np.max(widths) / 2)
        x_beg = med_x - half_max_x - border_x
        x_end = med_x + half_max_x + border_x
        y_beg = med_y - half_max_y - border_y
        y_end = med_y + half_max_y + border_y

    # Step2: Brain segmentation
    width = 96
    height = 96
    images = np.zeros((image_data.shape[2], width, height, n_channels))

    slice_counter = 0
    for ii in range(image_data.shape[2]):
        img_patch = cv2.resize(image_data[x_beg:x_end, y_beg:y_end, ii],
                               dsize=(width, height))

        if normalize:
            if normalize == "local_max":
                images[slice_counter, :, :, 0] = img_patch / np.max(img_patch)
            elif normalize == "global_max":
                images[slice_counter, :, :, 0] = img_patch / max_val
            elif normalize == "mean_std":
                images[slice_counter, :, :,
                       0] = (img_patch -
                             np.mean(img_patch)) / np.std(img_patch)
            else:
                raise ValueError('Please select a valid normalization')
        else:
            images[slice_counter, :, :, 0] = img_patch

        slice_counter += 1

    g = tf.Graph()
    with g.as_default():

        with tf.name_scope('inputs'):

            x = tf.placeholder(tf.float32, [None, width, height, n_channels])

        conv1 = conv_2d(x,
                        32,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")
        conv1 = conv_2d(conv1,
                        32,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")
        pool1 = max_pool_2d(conv1, 2)

        conv2 = conv_2d(pool1,
                        64,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")
        conv2 = conv_2d(conv2,
                        64,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")
        pool2 = max_pool_2d(conv2, 2)

        conv3 = conv_2d(pool2,
                        128,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")
        conv3 = conv_2d(conv3,
                        128,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")
        pool3 = max_pool_2d(conv3, 2)

        conv4 = conv_2d(pool3,
                        256,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")
        conv4 = conv_2d(conv4,
                        256,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")
        pool4 = max_pool_2d(conv4, 2)

        conv5 = conv_2d(pool4,
                        512,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")
        conv5 = conv_2d(conv5,
                        512,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")

        up6 = upsample_2d(conv5, 2)
        up6 = tflearn.layers.merge_ops.merge([up6, conv4], 'concat', axis=3)
        conv6 = conv_2d(up6,
                        256,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")
        conv6 = conv_2d(conv6,
                        256,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")

        up7 = upsample_2d(conv6, 2)
        up7 = tflearn.layers.merge_ops.merge([up7, conv3], 'concat', axis=3)
        conv7 = conv_2d(up7,
                        128,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")
        conv7 = conv_2d(conv7,
                        128,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")

        up8 = upsample_2d(conv7, 2)
        up8 = tflearn.layers.merge_ops.merge([up8, conv2], 'concat', axis=3)
        conv8 = conv_2d(up8,
                        64,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")
        conv8 = conv_2d(conv8,
                        64,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")

        up9 = upsample_2d(conv8, 2)
        up9 = tflearn.layers.merge_ops.merge([up9, conv1], 'concat', axis=3)
        conv9 = conv_2d(up9,
                        32,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")
        conv9 = conv_2d(conv9,
                        32,
                        3,
                        activation='relu',
                        padding='same',
                        regularizer="L2")

        pred = conv_2d(conv9, 2, 1, activation='linear', padding='valid')

    with tf.Session(graph=g) as sess_test_seg:
        # Restore the model
        tf_saver = tf.train.Saver()
        tf_saver.restore(sess_test_seg, modelCkptSeg)

        for idx in range(images.shape[0]):

            im = np.reshape(images[idx, :, :], [1, width, height, n_channels])

            feed_dict = {x: im}
            pred_ = sess_test_seg.run(pred, feed_dict=feed_dict)
            percentileSeg = thresholdSeg * 100
            theta = np.percentile(pred_, percentileSeg)
            pred_bin = np.where(pred_ > theta, 1, 0)
            #Map predictions to original indices and size
            pred_bin = cv2.resize(pred_bin[0, :, :, 0],
                                  dsize=(y_end - y_beg, x_end - x_beg),
                                  interpolation=cv2.INTER_NEAREST)
            pred3dFinal[idx, x_beg:x_end, y_beg:y_end,
                        0] = pred_bin.astype('float64')

        pppp = True
        if pppp:
            pred3dFinal = post_processing(np.asarray(pred3dFinal))
        pred3d = [
            cv2.resize(elem,
                       dsize=(image_data.shape[1], image_data.shape[0]),
                       interpolation=cv2.INTER_NEAREST) for elem in pred3dFinal
        ]
        pred3d = np.asarray(pred3d)
        upsampled = np.swapaxes(
            np.swapaxes(pred3d, 1, 2), 0,
            2)  #if Orient module applied, no need for this line(?)
        up_mask = nibabel.Nifti1Image(upsampled, img_nib.affine)
        # Save
        _, name, ext = split_filename(os.path.abspath(dataPath))
        save_file = os.path.join(os.getcwd().replace(bidsDir, '/fetaldata'),
                                 ''.join((name, out_postfix, ext)))
        nibabel.save(up_mask, save_file)
コード例 #8
0
from tflearn.layers.estimator import regression
from tflearn.layers.normalization import local_response_normalization
from tflearn.layers.recurrent import gru


input_img = input_data(shape=(28, 28, 1), name='input')                 ; print(input_img.shape)
x = conv_2d(input_img, 16, (3, 3), activation='relu', padding='same')   ; print(x.shape)
x = max_pool_2d(x, (2, 2), padding='same')                              ; print(x.shape)
x = conv_2d(x, 8, (3, 3), activation='relu', padding='same')            ; print(x.shape)
x = max_pool_2d(x, (2, 2), padding='same')                              ; print(x.shape)
x = conv_2d(x, 8, (3, 3), activation='relu', padding='same')            ; print(x.shape)
print("here")
encoded = max_pool_2d(x, (2, 2), padding='same')                        ; print(x.shape)# at this point the representation is (4, 4, 8) i.e. 128-dimensional
x = conv_2d(encoded, 8, (3, 3), activation='relu', padding='same')      ; print(x.shape)
print("here")
x = upsample_2d (x, (2, 2))                                             ; print(x.shape)
x = conv_2d(x, 8, (3, 3), activation='relu', padding='same')            ; print(x.shape)
x = upsample_2d (x, (2, 2))                                             ; print(x.shape)
x = conv_2d(x, 16, (3, 3), activation='relu')                           ; print(x.shape)
x = upsample_2d (x, (2, 2))                                             ; print(x.shape)
decoded = conv_2d(x, 1, (3, 3), activation='sigmoid', padding='same')   ; print(x.shape)

autoencoder = regression(decoded, optimizer='momentum',loss='categorical_crossentropy',
                         learning_rate=0.001, name='targets')
model = tflearn.DNN(autoencoder, checkpoint_path='autoencoder',
                        max_checkpoints=1, tensorboard_verbose=0, tensorboard_dir='log')


from keras.datasets import mnist
import numpy as np
コード例 #9
0
conv2_1 = conv_2d(pool1, 64, 3, 3, activation='relu', name="conv2_1")
conv2_2 = conv_2d(conv2_1, 64, 3, 3, activation='relu', name="conv2_2")
pool2 = max_pool_2d(conv2_2, 2)

conv3_1 = conv_2d(pool2, 128, 3, 3, activation='relu', name="conv3_1")
conv3_2 = conv_2d(conv3_1, 128, 3, 3, activation='relu', name="conv3_2")
pool3 = max_pool_2d(conv3_2, 2)

conv4_1 = conv_2d(pool3, 256, 3, 3, activation='relu', name="conv4_1")
conv4_2 = conv_2d(conv4_1, 256, 3, 3, activation='relu', name="conv4_2")
pool4 = max_pool_2d(conv4_2, 2)

conv5_1 = conv_2d(pool4, 512, 3, 3, activation='relu', name="conv5_1")
conv5_2 = conv_2d(conv5_1, 512, 3, 3, activation='relu', name="conv5_2")

up6 = merge([upsample_2d(conv5_2, 2), conv4_2], mode='concat', axis=1, name='upsamle-5-merge-4')
conv6_1 = conv_2d(up6, 256, 3, 3, activation='relu', name="conv6_1")
conv6_2 = conv_2d(conv6_1, 256, 3, 3, activation='relu', name="conv6_2")

up7 = merge([upsample_2d(conv6_2, 2), conv3_2], mode='concat', axis=1, name='upsamle-6-merge-3')
conv7_1 = conv_2d(up7, 128, 3, 3, activation='relu', name="conv7_1")
conv7_2 = conv_2d(conv7_1, 128, 3, 3, activation='relu', name="conv7_2")

up8 = merge([upsample_2d(conv7_2, 2), conv2_2], mode='concat', axis=1, name='upsamle-7-merge-2')        
conv8_1 = conv_2d(up8, 64, 3, 3, activation='relu', name="conv8_1")
conv8_2 = conv_2d(conv8_1, 64, 3, 3, activation='relu', name="conv8_2")

up9 = merge([upsample_2d(conv8_2, 2), conv1_2], mode='concat', axis=1, name='upsamle-8-merge-1')
conv9_1 = conv_2d(up9, 32, 3, 3, activation='relu', name="conv9_1")
conv9_2 = conv_2d(conv9_1, 32, 3, 3, activation='relu', name="conv9_2")
コード例 #10
0
encoder = conv_2d(encoder, 1, 7, activation='crelu')  #1x10

#encoder = max_pool_2d(encoder, [2,1])
print(encoder.get_shape, file=arq)
encoder = tflearn.layers.normalization.batch_normalization(encoder)

# 4x4
# importante: 5 filtros
print(encoder.get_shape, file=arq)

#encoder = max_pool_2d(encoder, [2,2])
# 4x4
# importante: 5 filtros
print(encoder.get_shape, file=arq)
decoder = conv_2d(encoder, 8, 7, activation='crelu')
decoder = upsample_2d(decoder, [2, 2])  # 16x20
# 8x8
print(decoder.get_shape, file=arq)
decoder = conv_2d(decoder, 16, 7, activation='crelu')
decoder = upsample_2d(decoder, [1, 1])  #16x20
# 16x16
print(decoder.get_shape, file=arq)
decoder = conv_2d(decoder, 16, 7, activation='crelu')
decoder = upsample_2d(decoder, [1, 2])  #16x20
# 16x64

decoder = conv_2d(decoder, 1, 7)
print(decoder.get_shape, file=arq)

network = regression(decoder,
                     optimizer='adam',