funkelab / gunpowder

A library to facilitate machine learning on multi-dimensional images.
https://funkelab.github.io/gunpowder/
MIT License
79 stars 56 forks source link

gunpowder.Stack only output a single random image #191

Closed lyehe closed 1 year ago

lyehe commented 1 year ago

I am trying to use the gunpowder.Stack class to request a stack of random images from the source (saying 5). The returned image stacks always have one new random image appended on the previous batch request. (saying 4 old images + 1 new image).

To reproduce the issue:

source = gp.ZarrSource(
    pathZarr,
    {
      raw: 'tr/tr000',
      gt: 'gt/gt000'
    },
    {
      raw: gp.ArraySpec(interpolatable=True),
      gt: gp.ArraySpec(interpolatable=False)
    })

pipeline = (
    source
    + gp.RandomLocation()
    + gp.Stack(5)
)

request = gp.BatchRequest()
request[raw] = gp.Roi((0, 0), (256, 256))
request[gt] = gp.Roi((0, 0), (256, 256))

with gp.build(pipeline):
    for i in range(3):
        batch = pipeline.request_batch(request)
        imshow(batch[raw].data, batch[gt].data) # custom plot function 

The image shows outputs from batch requests:

image
# custom imshow function
def imshow(*args, n=5, figurezize=(10, 4)):
    num_args = len(args)
    plt.figure(figsize=figurezize)
    for row_idx, image_set in enumerate(args): 
        image_set = np.array(image_set)
        n = min(n, image_set.shape[0])
        for col_idx in range(n):
            ax = plt.subplot(num_args, n, row_idx * n + col_idx + 1)
            plt.imshow(image_set[col_idx])
            plt.axis('off')
    plt.show()
lyehe commented 1 year ago

``Temporary (?) solution

Keep the batch request in the for loop should solve the problem. e.g.,

with gp.build(pipeline):
   for i in range(3):
        request = gp.BatchRequest()
        request[raw] = gp.Roi((0, 0), (256, 256))
        request[gt] = gp.Roi((0, 0), (256, 256))
        batch = pipeline.request_batch(request)
        imshow(batch[raw].data, batch[gt].data) # custom plot function 
pattonw commented 1 year ago

This has been solved: https://github.com/funkelab/gunpowder/commit/dfde284bde5420e206db9ba99795514aa1cd2fe4