示例#1
0
def test_image_chunker_convolution():
    # Apply convolution to an entire target image, and to image input and output chunk pairs
    # Results should be the same
    chunk_size = 70
    window_size = 50
    stride = 1
    num_classes = 1
    test_image = numpy.random.rand(1, 100, 100).astype(numpy.float32)
    chunker = detection.ImageChunkerWithOutput(test_image)
    chunker.set_chunk_size(chunk_size)
    chunker.set_classifier_window_size(window_size)
    chunker.set_classifier_window_stride(stride)
    chunker.set_number_of_output_classes(num_classes)

    mean_kernel = (numpy.ones(
        (window_size, window_size)) / window_size**2).astype(numpy.float32)
    expected_output = signal.convolve2d(test_image[0, ...],
                                        mean_kernel,
                                        mode='valid')
    expected_output = expected_output[numpy.newaxis, ...]

    output = chunker.allocate_output()
    for in_chunk, out_chunk in chunker:
        out_chunk[...] = signal.convolve2d(in_chunk[0, ...],
                                           mean_kernel,
                                           mode='valid')

    # Convolving chunk by chunk should produce the same results as convolving the whole image
    assert (output.shape == expected_output.shape)
    assert (output == expected_output).all()
示例#2
0
def main():
    caffe.set_mode_gpu()

    print "Configuring Net"
    net = caffe.Net(net_path, model_path, caffe.TEST)

    print "Reshaping input"
    net.blobs['data'].reshape(1, 1, chunk_size, chunk_size)

    print "Loading %s" % image_path
    image = caffe.io.load_image(image_path)
    cfos = image[:, :, 1][numpy.newaxis, ...]

    print "Configuring Chunker"
    chunker = detection.ImageChunkerWithOutput(cfos,
                                               num_classes=num_output_classes,
                                               window_size=window_size,
                                               chunk_size=chunk_size,
                                               stride=stride)

    chunker_params = {
        'chunk_size': chunk_size,
        'window_size': window_size,
        'stride': stride,
        'num_classes': num_output_classes
    }

    cell_detector = detection.CellDetector(net=net,
                                           cell_radius=12,
                                           signal_channel=1,
                                           chunker_params=chunker_params)
    cell_detector.set_image(image)
    cells = cell_detector.detect_cells()
    detector_mask = cell_detector.get_fish_net_mask(scaled=False)
    scaled_clean_mask = cell_detector.get_fish_net_mask(cleaned=True,
                                                        scaled=True)

    display_image(detector_mask, block=False)
    display_image(scaled_clean_mask, block=True)

    print "Computing by chunks"
    output = chunker.allocate_output()
    for in_chunk, out_chunk in chunker:
        net_out = net.forward_all(data=in_chunk)
        out_chunk[...] = net_out['prob'][...]

    display_image(output.argmax(0))
    io.imsave(path.expanduser('~/Desktop/tile_out.tif'), output.argmax(0))

    return

    print "Computing serially"
    caffe.set_mode_cpu()
    net.blobs['data'].reshape(1, *cfos.shape)
    full_out = net.forward_all(data=cfos)
    io.imsave(path.expanduser('~/Desktop/full_out.tif'), output.argmax(0))
    io.imsave(path.expanduser('~/Desktop/prob_pos.tif'), output[1, ...])
    io.imsave(path.expanduser('~/Desktop/prob_neg.tif'), output[0, ...])
示例#3
0
def test_image_chunker_wo_get_output_chunk_boundry():
    chunk_size = 70
    window_size = 50
    stride = 6
    num_classes = 2
    chunker = detection.ImageChunkerWithOutput(image)
    chunker.set_chunk_size(chunk_size)
    chunker.set_classifier_window_size(window_size)
    chunker.set_classifier_window_stride(stride)
    chunker.set_number_of_output_classes(num_classes)

    output = chunker.allocate_output()
    expected_chunk = output[:, -5:, -5:]
    test_chunk = chunker._get_output_chunk(10, 10)
    assert_same_slice(expected_chunk, test_chunk)
示例#4
0
def test_image_chunker_wo_allocate_output():
    chunk_size = 70
    window_size = 50
    stride = 6
    num_classes = 2
    expected_shape = (num_classes, 10, 10)
    chunker = detection.ImageChunkerWithOutput(image)
    chunker.set_chunk_size(chunk_size)
    chunker.set_classifier_window_size(window_size)
    chunker.set_classifier_window_stride(stride)
    chunker.set_number_of_output_classes(num_classes)
    output = chunker.allocate_output()

    assert output.shape == expected_shape
    assert output.dtype == numpy.float32
示例#5
0
def test_image_chunker_with_output_initialization():
    chunk_size = 70
    window_size = 50
    stride = 6
    num_classes = 2
    chunker = detection.ImageChunkerWithOutput(image,
                                               chunk_size=chunk_size,
                                               window_size=window_size,
                                               stride=stride,
                                               num_classes=num_classes)

    assert chunker.get_classifier_window_size() == window_size
    assert chunker.get_classifier_window_stride() == stride
    assert chunker.get_number_of_output_classes() == num_classes

    assert chunker._window_size.dtype == numpy.float32
    assert chunker._stride.dtype == numpy.float32
示例#6
0
def test_image_chunker_wo_output_chunking_coverage():
    # Tests that the output chunks span the entire output array
    chunk_size = 70
    window_size = 50
    stride = 6
    num_classes = 2
    chunker = detection.ImageChunkerWithOutput(image)
    chunker.set_chunk_size(chunk_size)
    chunker.set_classifier_window_size(window_size)
    chunker.set_classifier_window_stride(stride)
    chunker.set_number_of_output_classes(num_classes)

    output = chunker.allocate_output()
    for _, output_chunk in chunker:
        output_chunk[...] = 1

    assert output.all()  # Each output value should be set to one
示例#7
0
def test_image_chunker_wo_unallocated_output_tests():
    chunk_size = 70
    window_size = 50
    stride = 6
    num_classes = 2
    chunker = detection.ImageChunkerWithOutput(image)
    chunker.set_chunk_size(chunk_size)
    chunker.set_classifier_window_size(window_size)
    chunker.set_classifier_window_stride(stride)
    chunker.set_number_of_output_classes(num_classes)

    expected_exception = error_handling.OutputUnallocatedException
    assert_raises(expected_exception, chunker.__iter__().next)
    assert_raises(expected_exception, chunker.get_output_chunk_size)
    assert_raises(expected_exception, chunker._assert_allocated_output)
    assert not chunker.output_is_allocated()

    chunker.allocate_output()
    assert chunker.output_is_allocated()
示例#8
0
    def refresh_chunkers(self, keys):
        """
        Reload the image and label that correspond to the given keys. Create a new image
         chunker as well as a new chunker_key
        """
        images = self.load_images(keys)
        labels = self.load_labels(keys)

        # The chunker creates a zeroed output buffer consistent with the image size and
        #  specified kernel size (window size) and stride. The loaded label data is dumped
        #  into the output buffer so that it can be iterated over
        self._chunkers = [
            detection.ImageChunkerWithOutput(image, **self.chunker_params) for image in images
        ]
        for chunker, label in izip(self._chunkers, labels):
            chunker.allocate_output(dtype=label.dtype)
            chunker._output[...] = label[...]

        # The image chunker is an iterable that returns (image, label) pairs
        # We will extract views from this list by randomly indexing into it
        self._chunk_lists = map(list, self._chunkers)

        self._chunk_csrs = [random.randint(0, len(chunk_list)-1) for chunk_list in self._chunk_lists]
示例#9
0
def test_image_chunker_wo_allocate_output_misconfig():
    # allocate_output() should raise an error if the window_size, stride, and num_classes are not set
    chunker = detection.ImageChunkerWithOutput(image)
    output = chunker.allocate_output()
示例#10
0
def main():
    # Configure parser
    parser = configure_argument_parser()
    args = parser.parse_args()

    # Configure Caffe
    if args.gpu:
        caffe.set_mode_gpu()
    else:
        caffe.set_mode_cpu()

    net = caffe.Net(args.net_path, args.model_path, caffe.TEST)


    print "Model Path: {}".format(args.model_path)
    print "Net Path: {}".format(args.net_path)

    chunker_params = {
        'chunk_size': args.chunk_size,
        'window_size': NET_PARAMS['kernel'], # TODO Pull this from the network
        'stride': args.step_size,
        'num_classes': 2
    }

    # Load input image
    if args.vsi:
        source_image = data_io.load_vsi(args.image_path)
    else:
        source_image = io.imread(args.image_path)

    input_image = extract_input_channels(source_image, args).astype(numpy.float32)
    input_image = transpose_input_image(input_image)
    input_image = rescale_image(input_image, args)

    print "Chunker Params: "
    print chunker_params

    chunker = detection.ImageChunkerWithOutput(input_image, **chunker_params)
    output = chunker.allocate_output()

    num_chunks = chunker.get_number_of_chunks()
    for i, (input_chunk, output_chunk )in enumerate(chunker):
        progress = 100.0 * i/num_chunks
        debug_out("{0:.2f}% progress".format(progress), args)
        output_chunk[...] = compute_network_outputs(input_chunk, net, args)[...]

    mask = output.argmax(0)

    debug_out("Mask shape: {}".format(mask.shape), args)
    debug_out("Output shape: {}".format(output.shape), args)
    debug_out("Max Output: {}".format(mask.max()), args)
    debug_out("Mask mean: {}".format(mask.mean()), args)

    # Save output
    io.imsave(args.output, mask.astype(numpy.uint8))

    if args.hdf5_output is not None:
        hfile = h5py.File(args.hdf5_output, 'w')
        hfile.create_dataset("output", data=output)
        hfile.create_dataset("mask", data=mask)
        hfile.close()
    
    if args.display:
        figure()
        imshow(source_image)

        figure()
        imshow(mask)

        show()