Esempio n. 1
0
def load_prediction_data(path_to_img, channels, x_scale, y_scale, z_scale,
                         normalize, mu, sig, region_of_interest):

    # read image data
    img, img_header, img_ext = load_data(path_to_img,
                                         'first_queue',
                                         return_extension=True)
    if img is None:
        InputError.message = "Invalid image data %s." % (
            os.path.basename(path_to_img))
        raise InputError()
    if img_ext != '.am':
        img_header = None
    z_shape, y_shape, x_shape = img.shape

    # automatic cropping of image to region of interest
    if np.any(region_of_interest):
        min_z, max_z, min_y, max_y, min_x, max_x = region_of_interest[:]
        min_z = min(min_z, z_shape)
        min_y = min(min_y, y_shape)
        min_x = min(min_x, x_shape)
        max_z = min(max_z, z_shape)
        max_y = min(max_y, y_shape)
        max_x = min(max_x, x_shape)
        if max_z - min_z < z_shape:
            min_z, max_z = 0, z_shape
        if max_y - min_y < y_shape:
            min_y, max_y = 0, y_shape
        if max_x - min_x < x_shape:
            min_x, max_x = 0, x_shape
        img = np.copy(img[min_z:max_z, min_y:max_y, min_x:max_x], order='C')
        region_of_interest = np.array([
            min_z, max_z, min_y, max_y, min_x, max_x, z_shape, y_shape, x_shape
        ])
        z_shape, y_shape, x_shape = max_z - min_z, max_y - min_y, max_x - min_x

    # scale image data
    img = img.astype(np.float32)
    img = img_resize(img, z_scale, y_scale, x_scale)
    img -= np.amin(img)
    img /= np.amax(img)
    if normalize:
        mu_tmp, sig_tmp = np.mean(img), np.std(img)
        img = (img - mu_tmp) / sig_tmp
        img = img * sig + mu
        img[img < 0] = 0
        img[img > 1] = 1

    # compute position data
    position = None
    if channels == 2:
        position = np.empty((z_scale, y_scale, x_scale), dtype=np.float32)
        position = compute_position(position, z_scale, y_scale, x_scale)
        position = np.sqrt(position)
        position /= np.amax(position)

    return img, img_header, position, z_shape, y_shape, x_shape, region_of_interest
Esempio n. 2
0
def create_slices(path_to_data, path_to_label):

    try:

        if path_to_data:
            path_to_slices = path_to_data.replace('images', 'sliceviewer', 1)

        if path_to_label:
            path_to_label_slices = path_to_label.replace(
                'images', 'sliceviewer', 1)

        if path_to_data:

            # load data
            path_to_dir, extension = os.path.splitext(path_to_data)
            if extension == '.gz':
                path_to_dir, extension = os.path.splitext(path_to_dir)
            if extension == '.tar':
                img_names = []
                for data_type in [
                        '.tif', '.tiff', '.am', '.hdr', '.mhd', '.mha',
                        '.nrrd', '.nii', '.nii.gz'
                ]:
                    tmp_img_names = glob(path_to_dir + '/**/*' + data_type,
                                         recursive=True)
                    tmp_img_names = sorted(tmp_img_names)
                    img_names.extend(tmp_img_names)
                raw, _ = load_data(img_names[0], 'create_slices')
                zsh, ysh, xsh = raw.shape
                m = max(ysh, xsh)
                m = max(zsh, m)
                scale = float(256) / float(m)
                z_scale = int(zsh * scale)
                y_scale = int(ysh * scale)
                x_scale = int(xsh * scale)
                raw = img_resize(raw, z_scale, y_scale, x_scale)
                for name in img_names[1:]:
                    tmp, _ = load_data(name, 'create_slices')
                    tmp = img_resize(tmp, z_scale, y_scale, x_scale)
                    raw = np.append(raw, tmp, axis=0)
            else:
                raw, _ = load_data(path_to_data, 'create_slices')

            # increase contrast
            raw = img_to_uint8(raw)
            raw = contrast(raw)
            zsh, ysh, xsh = raw.shape

            if not path_to_label:

                # create slices for slice viewer
                if not os.path.isdir(path_to_slices):

                    # make directory
                    os.makedirs(path_to_slices)
                    os.chmod(path_to_slices, 0o770)

                    # reduce image size
                    m = min(ysh, xsh)
                    if m > 400:
                        scale = float(400) / float(m)
                        y_shape = int(ysh * scale)
                        x_shape = int(xsh * scale)
                        for k in range(zsh):
                            tmp = cv2.resize(raw[k], (x_shape, y_shape),
                                             interpolation=cv2.INTER_AREA)
                            tmp = tmp.astype(np.uint8)
                            im = Image.fromarray(tmp)
                            im.save(path_to_slices + '/%s.png' % (k))
                    else:
                        for k in range(zsh):
                            im = Image.fromarray(raw[k])
                            im.save(path_to_slices + '/%s.png' % (k))

        if path_to_label:

            # load data
            path_to_dir, extension = os.path.splitext(path_to_label)
            if extension == '.gz':
                path_to_dir, extension = os.path.splitext(path_to_dir)
            if extension == '.tar':
                img_names = []
                for data_type in [
                        '.tif', '.tiff', '.am', '.hdr', '.mhd', '.mha',
                        '.nrrd', '.nii', '.nii.gz'
                ]:
                    tmp_img_names = glob(path_to_dir + '/**/*' + data_type,
                                         recursive=True)
                    tmp_img_names = sorted(tmp_img_names)
                    img_names.extend(tmp_img_names)
                # load and scale label data corresponding to img data
                mask = np.zeros((0, y_scale, x_scale), dtype=np.uint8)
                for name in img_names:
                    arr, _ = load_data(name, 'create_slices')
                    arr = arr.astype(np.uint8)
                    np_unique = np.unique(arr)[1:]
                    next_mask = np.zeros((z_scale, y_scale, x_scale),
                                         dtype=arr.dtype)
                    for k in np_unique:
                        tmp = np.zeros_like(arr)
                        tmp[arr == k] = 1
                        tmp = img_resize(tmp, z_scale, y_scale, x_scale)
                        next_mask[tmp == 1] = k
                    mask = np.append(mask, next_mask, axis=0)
            else:
                mask, _ = load_data(path_to_label, 'create_slices')

            # img to uint8
            mask = color_to_gray(mask)
            mask = img_to_uint8(mask)

            # create slices for slice viewer
            if path_to_data and mask.shape == raw.shape:

                if not os.path.isdir(path_to_label_slices):

                    # make directory
                    os.makedirs(path_to_label_slices)
                    os.chmod(path_to_label_slices, 0o770)

                    # define colors
                    Color = [(255, 0, 0), (255, 255, 0), (0, 0, 255),
                             (0, 100, 0), (0, 255, 0), (255, 165, 0),
                             (139, 0, 0), (255, 20, 147), (255, 105, 180),
                             (255, 0, 0), (139, 0, 139), (255, 0, 255),
                             (160, 32, 240), (184, 134, 11), (255, 185, 15),
                             (255, 215, 0), (0, 191, 255), (16, 78, 139),
                             (104, 131, 139), (255, 64, 64), (165, 42, 42),
                             (255, 127, 36), (139, 90, 43), (110, 139, 61),
                             (0, 255, 127), (255, 127, 80), (139, 10, 80),
                             (219, 112, 147), (178, 34, 34), (255, 48, 48),
                             (205, 79, 57), (160, 32, 240), (255, 100, 0)] * 8
                    labels = np.unique(mask)[1:]
                    Color = Color[:len(labels)]
                    labels = labels[:len(labels)]
                    Color = np.array(Color, dtype=np.uint8)

                    # reduce image size
                    m = min(ysh, xsh)
                    if m > 400:
                        scale = float(400) / float(m)
                        ysh = int(ysh * scale)
                        xsh = int(xsh * scale)

                    # allocate memory
                    out = np.empty((ysh, xsh, 3), dtype=np.uint8)
                    gradient = np.empty((ysh, xsh), dtype=np.uint8)

                    for k in range(zsh):

                        # resize slice
                        if m > 400:
                            raw_tmp = cv2.resize(raw[k], (xsh, ysh),
                                                 interpolation=cv2.INTER_AREA)
                            mask_tmp = np.zeros((ysh, xsh), dtype=mask.dtype)
                            for l in labels:
                                tmp = np.zeros_like(mask[k])
                                tmp[mask[k] == l] = 1
                                tmp = cv2.resize(tmp, (xsh, ysh),
                                                 interpolation=cv2.INTER_AREA)
                                mask_tmp[tmp == 1] = l
                            raw_tmp = raw_tmp.astype(np.uint8)
                            mask_tmp = mask_tmp.astype(np.uint8)
                        else:
                            raw_tmp = raw[k]
                            mask_tmp = mask[k]

                        # compute gradient
                        gradient.fill(0)
                        tmp = np.abs(mask_tmp[:-1] - mask_tmp[1:])
                        tmp[tmp > 0] = 1
                        gradient[:-1] += tmp
                        gradient[1:] += tmp
                        tmp = np.abs(mask_tmp[:, :-1] - mask_tmp[:, 1:])
                        tmp[tmp > 0] = 1
                        gradient[:, :-1] += tmp
                        gradient[:, 1:] += tmp

                        # create output slice
                        for l in range(3):
                            out[:, :, l] = raw_tmp

                        # colorize
                        for j, label in enumerate(labels):
                            C = Color[j]
                            tmp = np.logical_and(gradient > 0,
                                                 mask_tmp == label)
                            out[:, :][tmp] = C

                        # save slice
                        im = Image.fromarray(out)
                        im.save(path_to_label_slices + '/%s.png' % (k))
    except:
        pass
Esempio n. 3
0
def predict_pre_final(img, path_to_model, x_scale, y_scale, z_scale, z_patch, y_patch, x_patch, \
                      normalize, mu, sig, channels, stride_size, batch_size):

    # img shape
    z_shape, y_shape, x_shape = img.shape

    # load position data
    if channels == 2:
        position = np.empty((z_scale, y_scale, x_scale), dtype=np.float32)
        position = compute_position(position, z_scale, y_scale, x_scale)
        position = np.sqrt(position)
        position /= np.amax(position)

    # resize img data
    img = img.astype(np.float32)
    img = img_resize(img, z_scale, y_scale, x_scale)
    img -= np.amin(img)
    img /= np.amax(img)
    if normalize:
        mu_tmp, sig_tmp = np.mean(img), np.std(img)
        img = (img - mu_tmp) / sig_tmp
        img = img * sig + mu
        img[img < 0] = 0
        img[img > 1] = 1

    # img shape
    zsh, ysh, xsh = img.shape

    # get number of 3D-patches
    nb = 0
    for k in range(0, zsh - z_patch + 1, stride_size):
        for l in range(0, ysh - y_patch + 1, stride_size):
            for m in range(0, xsh - x_patch + 1, stride_size):
                nb += 1

    # allocate memory
    x_test = np.empty((nb, z_patch, y_patch, x_patch, channels),
                      dtype=img.dtype)

    # create testing set
    nb = 0
    for k in range(0, zsh - z_patch + 1, stride_size):
        for l in range(0, ysh - y_patch + 1, stride_size):
            for m in range(0, xsh - x_patch + 1, stride_size):
                x_test[nb, :, :, :, 0] = img[k:k + z_patch, l:l + y_patch,
                                             m:m + x_patch]
                if channels == 2:
                    x_test[nb, :, :, :,
                           1] = position[k:k + z_patch, l:l + y_patch,
                                         m:m + x_patch]
                nb += 1

    # reshape testing set
    x_test = x_test.reshape(nb, z_patch, y_patch, x_patch, channels)

    # create a MirroredStrategy
    if os.name == 'nt':
        cdo = tf.distribute.HierarchicalCopyAllReduce()
    else:
        cdo = tf.distribute.NcclAllReduce()
    strategy = tf.distribute.MirroredStrategy(cross_device_ops=cdo)

    # load model
    with strategy.scope():
        model = load_model(str(path_to_model))

    # predict
    tmp = model.predict(x_test, batch_size=batch_size, verbose=0, steps=None)

    # create final
    final = np.zeros((zsh, ysh, xsh, tmp.shape[4]), dtype=np.float32)
    nb = 0
    for k in range(0, zsh - z_patch + 1, stride_size):
        for l in range(0, ysh - y_patch + 1, stride_size):
            for m in range(0, xsh - x_patch + 1, stride_size):
                final[k:k + z_patch, l:l + y_patch, m:m + x_patch] += tmp[nb]
                nb += 1

    # get final
    out = np.argmax(final, axis=3)
    out = out.astype(np.uint8)

    # rescale final to input size
    np_unique = np.unique(out)
    label = np.zeros((z_shape, y_shape, x_shape), dtype=out.dtype)
    for k in np_unique:
        tmp = np.zeros_like(out)
        tmp[out == k] = 1
        tmp = img_resize(tmp, z_shape, y_shape, x_shape)
        label[tmp == 1] = k

    return label
Esempio n. 4
0
def predict_semantic_segmentation(img, position, path_to_model, path_to_final,
                                  z_patch, y_patch, x_patch, z_shape, y_shape,
                                  x_shape, compress, header, img_header,
                                  channels, stride_size, allLabels, batch_size,
                                  region_of_interest):

    # img shape
    zsh, ysh, xsh = img.shape

    # list of IDs
    list_IDs = []

    # get nIds of patches
    for k in range(0, zsh - z_patch + 1, stride_size):
        for l in range(0, ysh - y_patch + 1, stride_size):
            for m in range(0, xsh - x_patch + 1, stride_size):
                list_IDs.append(k * ysh * xsh + l * xsh + m)

    # make length of list divisible by batch size
    rest = batch_size - (len(list_IDs) % batch_size)
    list_IDs = list_IDs + list_IDs[:rest]

    # parameters
    params = {
        'dim': (z_patch, y_patch, x_patch),
        'dim_img': (zsh, ysh, xsh),
        'batch_size': batch_size,
        'n_channels': channels
    }

    # data generator
    predict_generator = PredictDataGenerator(img, position, list_IDs, **params)

    # create a MirroredStrategy
    if os.name == 'nt':
        cdo = tf.distribute.HierarchicalCopyAllReduce()
    else:
        cdo = tf.distribute.NcclAllReduce()
    strategy = tf.distribute.MirroredStrategy(cross_device_ops=cdo)

    # load model
    with strategy.scope():
        model = load_model(str(path_to_model))

    # predict
    probabilities = model.predict(predict_generator, verbose=0, steps=None)

    # create final
    final = np.zeros((zsh, ysh, xsh, probabilities.shape[4]), dtype=np.float32)
    nb = 0
    for k in range(0, zsh - z_patch + 1, stride_size):
        for l in range(0, ysh - y_patch + 1, stride_size):
            for m in range(0, xsh - x_patch + 1, stride_size):
                final[k:k + z_patch, l:l + y_patch,
                      m:m + x_patch] += probabilities[nb]
                nb += 1

    # get final
    out = np.argmax(final, axis=3)
    out = out.astype(np.uint8)

    # rescale final to input size
    np_unique = np.unique(out)
    label = np.zeros((z_shape, y_shape, x_shape), dtype=out.dtype)
    for k in np_unique:
        tmp = np.zeros_like(out)
        tmp[out == k] = 1
        tmp = img_resize(tmp, z_shape, y_shape, x_shape)
        label[tmp == 1] = k

    # revert automatic cropping
    if np.any(region_of_interest):
        min_z, max_z, min_y, max_y, min_x, max_x, z_shape, y_shape, x_shape = region_of_interest[:]
        tmp = np.zeros((z_shape, y_shape, x_shape), dtype=out.dtype)
        tmp[min_z:max_z, min_y:max_y, min_x:max_x] = label
        label = np.copy(tmp)

    # save final
    label = label.astype(np.uint8)
    label = get_labels(label, allLabels)
    if header is not None:
        header = get_image_dimensions(header, label)
        if img_header is not None:
            try:
                header = get_physical_size(header, img_header)
            except:
                pass
    save_data(path_to_final, label, header=header, compress=compress)
Esempio n. 5
0
def load_training_data(normalize, img_list, label_list, channels, x_scale,
                       y_scale, z_scale, crop_data):

    # get filenames
    img_names, label_names = [], []
    for img_name, label_name in zip(img_list, label_list):

        img_dir, img_ext = os.path.splitext(img_name)
        if img_ext == '.gz':
            img_dir, img_ext = os.path.splitext(img_dir)

        label_dir, label_ext = os.path.splitext(label_name)
        if label_ext == '.gz':
            label_dir, label_ext = os.path.splitext(label_dir)

        if img_ext == '.tar' and label_ext == '.tar':
            for data_type in [
                    '.am', '.tif', '.tiff', '.hdr', '.mhd', '.mha', '.nrrd',
                    '.nii', '.nii.gz'
            ]:
                tmp_img_names = glob(img_dir + '/**/*' + data_type,
                                     recursive=True)
                tmp_label_names = glob(label_dir + '/**/*' + data_type,
                                       recursive=True)
                tmp_img_names = sorted(tmp_img_names)
                tmp_label_names = sorted(tmp_label_names)
                img_names.extend(tmp_img_names)
                label_names.extend(tmp_label_names)
            if len(img_names) == 0:
                InputError.message = "Invalid image TAR file."
                raise InputError()
            if len(label_names) == 0:
                InputError.message = "Invalid label TAR file."
                raise InputError()
        else:
            img_names.append(img_name)
            label_names.append(label_name)

    # load first label
    region_of_interest = None
    a, header, extension = load_data(label_names[0], 'first_queue', True)
    if a is None:
        InputError.message = "Invalid label data %s." % (os.path.basename(
            label_names[0]))
        raise InputError()
    if crop_data:
        region_of_interest = np.zeros(6)
        argmin_z, argmax_z, argmin_y, argmax_y, argmin_x, argmax_x = predict_blocksize(
            a)
        a = np.copy(a[argmin_z:argmax_z, argmin_y:argmax_y, argmin_x:argmax_x],
                    order='C')
        region_of_interest += [
            argmin_z, argmax_z, argmin_y, argmax_y, argmin_x, argmax_x
        ]
    a = a.astype(np.uint8)
    np_unique = np.unique(a)
    label = np.zeros((z_scale, y_scale, x_scale), dtype=a.dtype)
    for k in np_unique:
        tmp = np.zeros_like(a)
        tmp[a == k] = 1
        tmp = img_resize(tmp, z_scale, y_scale, x_scale)
        label[tmp == 1] = k

    # load first img
    img, _ = load_data(img_names[0], 'first_queue')
    if img is None:
        InputError.message = "Invalid image data %s." % (os.path.basename(
            img_names[0]))
        raise InputError()
    if crop_data:
        img = np.copy(img[argmin_z:argmax_z, argmin_y:argmax_y,
                          argmin_x:argmax_x],
                      order='C')
    img = img.astype(np.float32)
    img = img_resize(img, z_scale, y_scale, x_scale)
    img -= np.amin(img)
    img /= np.amax(img)
    mu, sig = np.mean(img), np.std(img)

    for img_name, label_name in zip(img_names[1:], label_names[1:]):

        # append label
        a, _ = load_data(label_name, 'first_queue')
        if a is None:
            InputError.message = "Invalid label data %s." % (
                os.path.basename(name))
            raise InputError()
        if crop_data:
            argmin_z, argmax_z, argmin_y, argmax_y, argmin_x, argmax_x = predict_blocksize(
                a)
            a = np.copy(a[argmin_z:argmax_z, argmin_y:argmax_y,
                          argmin_x:argmax_x],
                        order='C')
            region_of_interest += [
                argmin_z, argmax_z, argmin_y, argmax_y, argmin_x, argmax_x
            ]
        a = a.astype(np.uint8)
        np_unique = np.unique(a)
        next_label = np.zeros((z_scale, y_scale, x_scale), dtype=a.dtype)
        for k in np_unique:
            tmp = np.zeros_like(a)
            tmp[a == k] = 1
            tmp = img_resize(tmp, z_scale, y_scale, x_scale)
            next_label[tmp == 1] = k
        label = np.append(label, next_label, axis=0)

        # append image
        a, _ = load_data(img_name, 'first_queue')
        if a is None:
            InputError.message = "Invalid image data %s." % (
                os.path.basename(name))
            raise InputError()
        if crop_data:
            a = np.copy(a[argmin_z:argmax_z, argmin_y:argmax_y,
                          argmin_x:argmax_x],
                        order='C')
        a = a.astype(np.float32)
        a = img_resize(a, z_scale, y_scale, x_scale)
        a -= np.amin(a)
        a /= np.amax(a)
        if normalize:
            mu_tmp, sig_tmp = np.mean(a), np.std(a)
            a = (a - mu_tmp) / sig_tmp
            a = a * sig + mu
        img = np.append(img, a, axis=0)

    # automatic cropping
    if crop_data:
        region_of_interest /= float(len(img_names))
        region_of_interest = np.round(region_of_interest)
        region_of_interest[region_of_interest < 0] = 0
        region_of_interest = region_of_interest.astype(int)

    # scale image data to [0,1]
    img[img < 0] = 0
    img[img > 1] = 1

    # compute position data
    position = None
    if channels == 2:
        position = np.empty((z_scale, y_scale, x_scale), dtype=np.float32)
        position = compute_position(position, z_scale, y_scale, x_scale)
        position = np.sqrt(position)
        position /= np.amax(position)
        for k in range(len(img_names[1:])):
            a = np.copy(position)
            position = np.append(position, a, axis=0)

    # labels must be in ascending order
    allLabels = np.unique(label)
    for k, l in enumerate(allLabels):
        label[label == l] = k

    return img, label, position, allLabels, mu, sig, header, extension, region_of_interest
Esempio n. 6
0
def load_training_data(normalize, img_list, label_list, channels, x_scale, y_scale, z_scale,
        crop_data, x_puffer=25, y_puffer=25, z_puffer=25):

    # get filenames
    img_names, label_names = [], []
    for img_name, label_name in zip(img_list, label_list):

        # check for tarball
        img_dir, img_ext = os.path.splitext(img_name)
        if img_ext == '.gz':
            img_dir, img_ext = os.path.splitext(img_dir)

        label_dir, label_ext = os.path.splitext(label_name)
        if label_ext == '.gz':
            label_dir, label_ext = os.path.splitext(label_dir)

        if (img_ext == '.tar' and label_ext == '.tar') or (os.path.isdir(img_name) and os.path.isdir(label_name)):

            # extract files if necessary
            if img_ext == '.tar' and not os.path.exists(img_dir):
                tar = tarfile.open(img_name)
                tar.extractall(path=img_dir)
                tar.close()
            if label_ext == '.tar' and not os.path.exists(label_dir):
                tar = tarfile.open(label_name)
                tar.extractall(path=label_dir)
                tar.close()

            for data_type in ['.am','.tif','.tiff','.hdr','.mhd','.mha','.nrrd','.nii','.nii.gz']:
                tmp_img_names = glob(img_dir+'/**/*'+data_type, recursive=True)
                tmp_label_names = glob(label_dir+'/**/*'+data_type, recursive=True)
                tmp_img_names = sorted(tmp_img_names)
                tmp_label_names = sorted(tmp_label_names)
                img_names.extend(tmp_img_names)
                label_names.extend(tmp_label_names)
            if len(img_names)==0:
                InputError.message = "Invalid image TAR file."
                raise InputError()
            if len(label_names)==0:
                InputError.message = "Invalid label TAR file."
                raise InputError()
        else:
            img_names.append(img_name)
            label_names.append(label_name)

    # load first label
    a, header, extension = load_data(label_names[0], 'first_queue', True)
    if a is None:
        InputError.message = "Invalid label data %s." %(os.path.basename(label_names[0]))
        raise InputError()
    if crop_data:
        argmin_z,argmax_z,argmin_y,argmax_y,argmin_x,argmax_x = predict_blocksize(a, x_puffer, y_puffer, z_puffer)
        a = np.copy(a[argmin_z:argmax_z,argmin_y:argmax_y,argmin_x:argmax_x], order='C')
    a = a.astype(np.uint8)
    np_unique = np.unique(a)
    label = np.zeros((z_scale, y_scale, x_scale), dtype=a.dtype)
    for k in np_unique:
        tmp = np.zeros_like(a)
        tmp[a==k] = 1
        tmp = img_resize(tmp, z_scale, y_scale, x_scale)
        label[tmp==1] = k

    # load first img
    img, _ = load_data(img_names[0], 'first_queue')
    if img is None:
        InputError.message = "Invalid image data %s." %(os.path.basename(img_names[0]))
        raise InputError()
    if crop_data:
        img = np.copy(img[argmin_z:argmax_z,argmin_y:argmax_y,argmin_x:argmax_x], order='C')
    img = img.astype(np.float32)
    img = img_resize(img, z_scale, y_scale, x_scale)
    img -= np.amin(img)
    img /= np.amax(img)
    mu, sig = np.mean(img), np.std(img)

    for img_name, label_name in zip(img_names[1:], label_names[1:]):

        # append label
        a, _ = load_data(label_name, 'first_queue')
        if a is None:
            InputError.message = "Invalid label data %s." %(os.path.basename(name))
            raise InputError()
        if crop_data:
            argmin_z,argmax_z,argmin_y,argmax_y,argmin_x,argmax_x = predict_blocksize(a, x_puffer, y_puffer, z_puffer)
            a = np.copy(a[argmin_z:argmax_z,argmin_y:argmax_y,argmin_x:argmax_x], order='C')
        a = a.astype(np.uint8)
        np_unique = np.unique(a)
        next_label = np.zeros((z_scale, y_scale, x_scale), dtype=a.dtype)
        for k in np_unique:
            tmp = np.zeros_like(a)
            tmp[a==k] = 1
            tmp = img_resize(tmp, z_scale, y_scale, x_scale)
            next_label[tmp==1] = k
        label = np.append(label, next_label, axis=0)

        # append image
        a, _ = load_data(img_name, 'first_queue')
        if a is None:
            InputError.message = "Invalid image data %s." %(os.path.basename(name))
            raise InputError()
        if crop_data:
            a = np.copy(a[argmin_z:argmax_z,argmin_y:argmax_y,argmin_x:argmax_x], order='C')
        a = a.astype(np.float32)
        a = img_resize(a, z_scale, y_scale, x_scale)
        a -= np.amin(a)
        a /= np.amax(a)
        if normalize:
            mu_tmp, sig_tmp = np.mean(a), np.std(a)
            a = (a - mu_tmp) / sig_tmp
            a = a * sig + mu
        img = np.append(img, a, axis=0)

    # scale image data to [0,1]
    img[img<0] = 0
    img[img>1] = 1

    # compute position data
    position = None
    if channels == 2:
        position = np.empty((z_scale, y_scale, x_scale), dtype=np.float32)
        position = compute_position(position, z_scale, y_scale, x_scale)
        position = np.sqrt(position)
        position /= np.amax(position)
        for k in range(len(img_names[1:])):
            a = np.copy(position)
            position = np.append(position, a, axis=0)

    # labels must be in ascending order
    allLabels, counts = np.unique(label, return_counts=True)
    for k, l in enumerate(allLabels):
        label[label==l] = k

    # configuration data
    configuration_data = np.array([channels, x_scale, y_scale, z_scale, normalize, mu, sig])

    return img, label, position, allLabels, configuration_data, header, extension, len(img_names), counts