Exemplo n.º 1
0
def re_label(label_input, num_data, labels):
    relabel = []
    for i in range(num_data):
        data_label = np.load(label_input[i])['vol_data']
        data_label = generators._relabel(data_label, labels)
        relabel.append(data_label)
    return relabel
Exemplo n.º 2
0
def vol_and_label_generator_patch(vol_name,
                                  label_name,
                                  trf_vol_model,
                                  trf_label_model,
                                  resize_model,
                                  num_data,
                                  patch_size,
                                  labels,
                                  num_labels=30,
                                  stride_patch=32):

    vol_patch = np.empty([120, 64, 64, 64, 1])
    labels_patch = np.empty([120, 64, 64, 64, 30])
    count1 = 0
    count2 = 0
    vol_size = (160, 192, 224)
    i = np.random.randint(num_data)
    # for i in range(num_data):
    da_factor = np.random.random(1) * 5
    # print(da_factor)
    rand_phi = np.random.randn(10, 12, 14, 3) * da_factor
    rand_phi = rand_phi[np.newaxis, ...]
    resize_vol = resize_model.predict([rand_phi])

    data_vol = np.load(
        vol_name[i])['vol_data']  # load the volume data from the list
    # print("volume data",i,":",vol_name[i]) # print the volume data used for training

    ##### data agmentation part
    data_vol = data_vol[np.newaxis, ..., np.newaxis]
    data_vol = trf_vol_model.predict([data_vol, resize_vol])[0, ..., 0]
    ######

    data_label = np.load(label_name[i])['vol_data']  # load the label data
    data_label = generators._relabel(data_label, labels)  # relabel
    # print("label data",i,":",label_name[i])

    ##### data agmentation part
    data_label = to_categorical(data_label, num_labels)  # to one_hot
    data_label = data_label[np.newaxis, ...]
    data_label = trf_label_model.predict([data_label, resize_vol
                                          ])[0, ..., :]  # spatial tranform
    data_label = np.argmax(data_label, axis=-1)
    ######

    for item in patchlib.patch_gen(data_vol, patch_size, stride=stride_patch):
        vol_patch[count1, :, :, :, 0] = item
        count1 += 1

    for item in patchlib.patch_gen(data_label, patch_size,
                                   stride=stride_patch):
        item = to_categorical(item,
                              num_labels)  # change to one-hot representation
        labels_patch[count2, :, :, :, :] = item
        count2 += 1

    return vol_patch, labels_patch
Exemplo n.º 3
0
def vol_and_label_generator_patch(vol_name,
                                  label_name,
                                  trf_vol_model,
                                  trf_label_model,
                                  resize_model,
                                  num_data,
                                  patch_size,
                                  labels,
                                  num_labels=30,
                                  stride_patch=32):

    # random field deformation augmentation is performed which makes model robust.
    vol_patch = np.empty(
        [120, 64, 64, 64,
         1])  # patch size is 64,64,64 and 120 patches per volume
    labels_patch = np.empty([120, 64, 64, 64, 30])  # 30 label is used
    vol_size = (160, 192, 224)  # size of the image
    i = np.random.randint(num_data)  # to randomly pick the data
    da_factor = np.random.random(
        1) * 5  # control the deformation by restrict the max

    rand_phi = np.random.randn(
        10, 12, 14, 3
    ) * da_factor  # play around the initial random array to find best fit for data
    rand_phi = rand_phi[np.newaxis, ...]
    resize_vol = resize_model.predict(
        [rand_phi])  # resize the random field to become smooth

    data_vol = np.load(
        vol_name[i])['vol_data']  # load the volume data from the list

    data_vol = data_vol[np.newaxis, ..., np.newaxis]
    data_vol = trf_vol_model.predict(
        [data_vol, resize_vol])[0, ...,
                                0]  # warp the the image to random field

    data_label = np.load(label_name[i])['vol_data']  # load the label data
    data_label = generators._relabel(data_label, labels)  # relabel
    data_label = to_categorical(data_label, num_labels)  # to one_hot matrix
    data_label = data_label[np.newaxis, ...]
    data_label = trf_label_model.predict(
        [data_label, resize_vol])[0, ..., :]  # warp label to random field
    data_label = np.argmax(data_label, axis=-1)

    for idx, item in enumerate(
            patchlib.patch_gen(data_vol, patch_size, stride=stride_patch)):
        vol_patch[idx, :, :, :, 0] = item

    for idx, item in enumerate(
            patchlib.patch_gen(data_label, patch_size, stride=stride_patch)):
        item = to_categorical(item,
                              num_labels)  # change to one-hot representation
        labels_patch[idx, :, :, :, :] = item

    return vol_patch, labels_patch
Exemplo n.º 4
0
def re_label(label_input, num_data, labels):
    '''
    This function relabels the given label
    input - label_input: input label data(3D)
            num_data: number of data in label_input
            labels: labels of interest
    This function usese generators function from Neuron toolbox
    '''
    relabel = []
    for i in range(num_data):
        data_label = np.load(label_input[i])['vol_data']
        data_label = generators._relabel(data_label, labels)
        relabel.append(data_label)
    return relabel
Exemplo n.º 5
0
def label_generator_patch(label_name,
                          num_data,
                          patch_size,
                          labels,
                          num_labels,
                          stride_patch=32,
                          out=1):

    # 120 = number of patches for one volume, 30 = number of labels of interest
    labels_patch = np.empty([num_data * 120, 64, 64, 64, 30])
    labels_patch2 = []
    patch_loc = []
    count = 0  # count the batch size for the network
    for i in range(num_data):
        data_label = np.load(label_name[i])['vol_data']
        print("label data", i, ":", label_name[i])
        data_label = generators._relabel(data_label, labels)
        temp = []
        loc_temp = []
        if out == 2:
            # generate the patch and store them in a list
            for item, loc in patchlib.patch_gen(data_label,
                                                patch_size,
                                                stride=stride_patch,
                                                nargout=out):
                item = to_categorical(
                    item, num_labels)  # change to one-hot representation
                item = np.reshape(item, (1, ) + item.shape)
                temp.append(item)
                loc_temp.append(loc)
            labels_patch2.append(temp)
            patch_loc.append(loc_temp)
        elif out == 1:
            for item in patchlib.patch_gen(data_label,
                                           patch_size,
                                           stride=stride_patch):
                item = to_categorical(
                    item, num_labels)  # change to one-hot representation
                # vol_patch = [batch size, (dimension), channel(30 as default)]
                labels_patch[count, :, :, :, :] = item
                count += 1
    if out == 1:
        return labels_patch
    elif out == 2:
        return labels_patch2, patch_loc