def inner(x): axis = get_channel_axis() shape_x = K.int_shape(x) # Get image size if axis == 1: img_size = shape_x[2:] else: img_size = shape_x[1:-1] img_size = np.array(img_size) # Perform pyramid pooling num_filters = shape_x[axis] / len(pyramid_bins) pooled = [] for b_sz in pyramid_bins: size = tuple(img_size / b_sz) tmp = AveragePoolingND(x, pool_size=size)(x) tmp = conv(num_filters, kernel_size=1, activation=activation, conv_order=conv_order, use_batch_norm=use_batch_norm)(tmp) if upsampling: tmp = UpSamplingND(tmp, size=size)(tmp) else: tmp = Flatten()(tmp) pooled.append(tmp) x = Concatenate(axis=axis)(pooled) return x
def get_segmentation(image, nclass, ncluster): seg_mc = AE_model.predict(image).argmax(get_channel_axis()) seg = seg_mc.copy() ind = nclass for i in range(nclass): for j in range(ncluster[i] - 1): seg[seg == ind] = i ind = ind + 1 return seg, seg_mc
def get_segmentation_from_prediction(predicted, nclass, ncluster): seg_mc = predicted.argmax(get_channel_axis()) seg = seg_mc.copy() ind = nclass for i in range(nclass): for j in range(ncluster[i] - 1): seg[seg == ind] = i ind = ind + 1 return seg, seg_mc
def composite_layer(x): """ Arguments: x: tensor from the previous layer. """ name = 'expand_%d' % depth num_filters = self.base_num_filters * 2 ** depth x = UpSamplingND(x)(x) x = Concatenate(axis=get_channel_axis(), name='concat_%d' % depth)([x, self.contr_tensors[depth]]) x = self.conv_depth(num_filters=num_filters, name=name, kernel_cc_weight=kernel_cc_weight)(x) return x
def inner(tensor): local_gru = GRU(units=units, activation=None, implementation=2, dropout=dropout, return_sequences=return_sequences ) # Default GRU activation is 'tanh'. tensor = local_gru(tensor) if conv_order == 'conv_first' else tensor tensor = BatchNormalization( axis=get_channel_axis())(tensor) if use_batch_norm else tensor tensor = Activation(activation)(tensor) tensor = local_gru(tensor) if conv_order == 'conv_last' else tensor return tensor
def inner(x): conv_first = conv_order == 'conv_first' if conv_first: x = ConvNDTranspose(x, filters=num_filters, kernel_size=kernel_size, padding='same', **conv_kwargs)(x) x = BatchNormalization( axis=get_channel_axis())(x) if use_batch_norm else x x = Activation( activation, name=name)(x) if conv_first else Activation(activation)(x) if not conv_first: x = ConvNDTranspose(x, filters=num_filters, kernel_size=kernel_size, padding='same', name=name, **conv_kwargs)(x) return x
def inner(x, tensor): # Modifies x to be the same size as tensor. if use_identity_mappings: x = resize_x()([x, tensor]) else: # Activation should be linear (None) dim = K.ndim(x) axis = get_channel_axis() num_channels = K.int_shape(tensor)[axis] if dim == 2: # batch size, channels x = Dense(num_channels)(x) elif 3 <= dim <= 5: x = ConvND(x, filters=num_channels, kernel_size=1, padding='same')(x) else: raise ValueError( 'Only 2D, 3D, 4D, and 5D tensors are supported.') x = Add(name=name)([tensor, x]) return x
imgs = generator_prediction(id) sz = imgs.shape asegZ = np.zeros((sz[0], sz[1], sz[2], 5)) aseg = np.zeros((sz[0], sz[1], sz[2]), np.int8) aseg_slice = np.zeros((sz[1], sz[2]), np.int8) for j in range(sz[0]): seg_mc_Z = segmentation_model.predict( np.expand_dims(imgs[j, :, :, :], axis=0)) asegZ[j, :, :, :] = seg_mc_Z[0, :, :, :] ##new for CS527 slice = asegZ[j].argmax(get_channel_axis()) for i in range(nlabel): for j in range(ncluster[i] - 1): slice[slice == ind] = i ind = ind + 1 for row in range(sz[1]): aseg_slice[row, :] = slice[row, :] itkimage = sitk.GetImageFromArray(aseg_slice) sitk.WriteImage( itkimage, output_folder + str(id) + '_' + str(j) + '_autoseg.nii.gz', True) print('save seg slice to ' + output_folder + str(id) + '_' + str(j) + '_autoseg.nii.gz')