Beispiel #1
0
def pred_single(mdl, predict_x, ih, iw, patch_shape, patch_step, patch_size,
                batch_size):
    predict_x = extract_3d(predict_x, patch_shape, patch_step)
    predict_x = np.reshape(predict_x,
                           (predict_x.shape[0], patch_size, patch_size, 1))
    predict_y = mdl.predict(predict_x, batch_size=batch_size)
    predict_y = np.reshape(predict_y,
                           (predict_y.shape[0], patch_size, patch_size))
    predict_y = reconstruct_patches(predict_y, (ih, iw), patch_step)
    return predict_y
Beispiel #2
0
# size of pooling area for max pooling
nb_pool = 2
# convolution kernel size
nb_conv = 3

fname = '../../test/test_data/1038.tiff'
ind_uncenter1 = range(1038, 1047)
ind_uncenter2 = range(1049, 1057)
uncenter1 = dxchange.read_tiff_stack(fname, ind=ind_uncenter1, digit=4)
uncenter2 = dxchange.read_tiff_stack(fname, ind=ind_uncenter2, digit=4)
uncenter = np.concatenate((uncenter1, uncenter2), axis=0)
uncenter = nor_data(uncenter)
print (uncenter.shape)
uncenter = img_window(uncenter[:, 360:1460, 440:1440], 200)
print (uncenter.shape)
uncenter_patches = extract_3d(uncenter, patch_size, 1)
np.random.shuffle(uncenter_patches)
print (uncenter_patches.shape)
# print uncenter_patches.shape
center_img = dxchange.read_tiff('../../test/test_data/1048.tiff')
center_img = nor_data(center_img)
print (center_img.shape)
center_img = img_window(center_img[360:1460, 440:1440], 400)
center_patches = extract_3d(center_img, patch_size, 1)
np.random.shuffle(center_patches)
print (center_patches.shape)
# plt.imshow(center_img, cmap='gray', interpolation= None)
# plt.show()

x_train = np.concatenate((uncenter_patches[0:50000], center_patches[0:50000]), axis=0)
x_test = np.concatenate((uncenter_patches[50000:60000], center_patches[50000:60000]), axis=0)
Beispiel #3
0
def seg_predict(img, wpath, patch_size = 32, patch_step = 1,
                  nb_conv=32, size_conv=3,
                  batch_size=1000, nb_down=2, nb_gpu = 1):
    """
    Function description
    Parameters
    ----------
    img : array
        The images need to be segmented.
    wpath: string
        The path where the trained weights of the model can be read.
    
    patch_size: int
        The size of the small patches extracted from the input images. This size should be big enough to cover the
        features of the segmentation object.
    patch_step: int
         The pixel steps between neighbour patches. Larger steps leads faster speed, but less quality. I recommend 1
         unless you need quick test of the algorithm.
    nb_conv: int
          Number of the covolutional kernals for the first layer. This number doubles after each downsampling layer.
    size_conv: int
          Size of the convolutional kernals.
    batch_size: int
          Batch size for the training. Bigger size leads faster speed. However, it is restricted by the memory size of
          the GPU. If the user got the memory error, please decrease the batch size.
    nb_epoch: int
          Number of the epoches for the training. It can be understand as the number of iterations during the training.
          Please define this number as the actual convergence for different data.
    nb_down: int
          Number of the downsampling for the images in the model.
    nb_gpu: int
          Number of GPUs you want to use for the training.
    Returns
    -------
    save the segmented images to the spath.
      """
    patch_shape = (patch_size, patch_size)
    img = np.float32(nor_data(img))
    mdl = model_choose(patch_size, patch_size, nb_conv, size_conv, nb_down, nb_gpu)
    # print(mdl.summary())
    mdl.load_weights(wpath)
    if img.ndim == 2:
        ih, iw = img.shape
        predict_x = extract_3d(img, patch_shape, patch_step)
        predict_x = np.reshape(predict_x, (predict_x.shape[0], patch_size, patch_size, 1))
        predict_y = mdl.predict(predict_x, batch_size=batch_size)
        predict_y = np.reshape(predict_y, (predict_y.shape[0],patch_size, patch_size))
        predict_y = reconstruct_patches(predict_y, (ih, iw), patch_step)
        return predict_y

    else:
        pn, ih, iw = img.shape
        images = np.empty(pn, dtype=object) # create empty array 
        for i in range(pn):
            print('Processing the %s th image' % i)
            tstart = time.time()
            predict_x = img[i]
            predict_x = extract_3d(predict_x, patch_shape, patch_step)
            predict_x = np.reshape(predict_x, (len(predict_x), patch_size, patch_size, 1))
            predict_y = mdl.predict(predict_x, batch_size=batch_size)
            predict_y = np.reshape(predict_y, (len(predict_y), patch_size, patch_size))
            predict_y = reconstruct_patches(predict_y, (ih, iw), patch_step)
            images[i]=predict_y
            return images
Beispiel #4
0
def seg_train(img_x,
              img_y,
              patch_size=32,
              patch_step=1,
              nb_conv=32,
              size_conv=3,
              batch_size=1000,
              nb_epoch=20,
              nb_down=2,
              nb_gpu=1):
    """
    Function description.

    Parameters
    ----------
    img_x: array, 2D or 3D
        Training input of the model. It is the raw image for the segmentation.

    img_y: array, 2D or 3D
        Training output of the model. It is the corresponding segmentation of the training input.

    patch_size: int
        The size of the small patches extracted from the input images. This size should be big enough to cover the
        features of the segmentation object.

    patch_step: int
         The pixel steps between neighbour patches. Larger steps leads faster speed, but less quality. I recommend 1
         unless you need quick test of the algorithm.

    nb_conv: int
          Number of the covolutional kernals for the first layer. This number doubles after each downsampling layer.

    size_conv: int
          Size of the convolutional kernals.

    batch_size: int
          Batch size for the training. Bigger size leads faster speed. However, it is restricted by the memory size of
          the GPU. If the user got the memory error, please decrease the batch size.

    nb_epoch: int
          Number of the epoches for the training. It can be understand as the number of iterations during the training.
          Please define this number as the actual convergence for different data.

    nb_down: int
          Number of the downsampling for the images in the model.

    nb_gpu: int
          Number of GPUs you want to use for the training.


    Returns
    -------
    mdl
        The trained CNN model for segmenation. The model can be saved for future segmentations.
    """
    # if img_x.ndim == 3:
    #     _, ih, iw = img_x.shape
    # else:
    #     ih, iw = img_x.shape
    patch_shape = (patch_size, patch_size)
    # print img_x.shape
    # print img_x.max(), img_x.min()
    img_x = nor_data(img_x)
    img_y = nor_data(img_y)
    # print img_x.shape
    # print img_x.max(), img_x.min()

    train_x = extract_3d(img_x, patch_shape, patch_step)
    train_y = extract_3d(img_y, patch_shape, patch_step)
    # print train_x.shape
    # print train_x.max(), train_x.min()
    train_x = np.reshape(train_x, (len(train_x), patch_size, patch_size, 1))
    train_y = np.reshape(train_y, (len(train_y), patch_size, patch_size, 1))
    mdl = model_choose(patch_size, patch_size, nb_conv, size_conv, nb_down,
                       nb_gpu)
    print(mdl.summary())
    mdl.fit(train_x, train_y, batch_size=batch_size, epochs=nb_epoch)
    return mdl
Beispiel #5
0
def seg_train(img_x, img_y, **kwargs):
    """
    Function description.

    Parameters
    ----------
    img_x: array, 2D or 3D
        Training input of the model. It is the raw image for the segmentation.

    img_y: array, 2D or 3D
        Training output of the model. It is the corresponding segmentation of the training input.

    patch_size: int
        The size of the small patches extracted from the input images. This size should be big enough to cover the
        features of the segmentation object.

    patch_step: int
         The pixel steps between neighbour patches. Larger steps leads faster speed, but less quality. I recommend 1
         unless you need quick test of the algorithm.

    conv_nb: int
          Number of the covolutional kernals for the first layer. This number doubles after each downsampling layer.

    conv_size: int
          Size of the convolutional kernals.

    batch_size: int
          Batch size for the training. Bigger size leads faster speed. However, it is restricted by the memory size of
          the GPU. If the user got the memory error, please decrease the batch size.

    nb_epoch: int
          Number of the epoches for the training. It can be understand as the number of iterations during the training.
          Please define this number as the actual convergence for different data.

    model_layers: int
          Number of the downsampling for the images in the model.

    gpu_nb: int
          Number of GPUs you want to use for the training.


    Returns
    -------
    mdl
        The trained CNN model for segmenation. The model can be saved for future segmentations.
    """
    seg_kwargs = [
        'patch_size', 'patch_step', 'conv_nb', 'conv_size', 'batch_size',
        'epoch_nb', 'model_layers', 'gpu_nb'
    ]
    kwargs_defaults = _get_seg_kwargs()
    for kw in seg_kwargs:
        kwargs.setdefault(kw, kwargs_defaults[kw])
    patch_shape = (kwargs['patch_size'], kwargs['patch_size'])
    # print img_x.shape
    # print img_x.max(), img_x.min()
    img_x = nor_data(img_x)
    img_y = nor_data(img_y)
    # print img_x.shape
    # print img_x.max(), img_x.min()

    train_x = extract_3d(img_x, patch_shape, kwargs['patch_step'])
    train_y = extract_3d(img_y, patch_shape, kwargs['patch_step'])
    # print train_x.shape
    # print train_x.max(), train_x.min()
    train_x = np.reshape(
        train_x, (len(train_x), kwargs['patch_size'], kwargs['patch_size'], 1))
    train_y = np.reshape(
        train_y, (len(train_y), kwargs['patch_size'], kwargs['patch_size'], 1))
    mdl = model_choose(kwargs['patch_size'], kwargs['patch_size'],
                       kwargs['conv_nb'], kwargs['conv_size'],
                       kwargs['model_layers'], kwargs['gpu_nb'])
    print(mdl.summary())
    mdl.fit(train_x,
            train_y,
            batch_size=kwargs['batch_size'],
            epochs=kwargs['nb_epoch'])
    return mdl