Skip to content

Commit

Permalink
convert 16 bit greyscale images to rgb
Browse files Browse the repository at this point in the history
  • Loading branch information
metazool committed Sep 30, 2024
1 parent 87d74f5 commit 4e90224
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 20 deletions.
10 changes: 5 additions & 5 deletions scripts/dvc.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ stages:
outs:
- path: ../vectors
hash: md5
md5: 381c0f3d25a23daa7a3b741e9bb41a1d.dir
md5: 6be45d2101d76bd5f29c6ec858aecc8a.dir
size: 13566692
nfiles: 5
cluster:
cmd: python cluster.py
deps:
- path: ../vectors
hash: md5
md5: b56c902defd862c4326adc981abea25c.dir
md5: 18fe90eccd3a31d63eb81a0db701e375.dir
size: 13566692
nfiles: 5
outs:
- path: ../models
hash: md5
md5: d751713988987e9331980363e24189ce.dir
size: 0
nfiles: 0
md5: 17573502996bd38fde3c4304cb3d1d3d.dir
size: 84672
nfiles: 1
5 changes: 0 additions & 5 deletions scripts/dvc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,3 @@ stages:
- ../vectors
outs:
- ../models

artifacts:
kmeans_cluster: # artifact ID (name)
path: ../models/kmeans.pkl
type: model
3 changes: 2 additions & 1 deletion scripts/image_embeddings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Try to use the scivision pretrained model and tools against this collection"""
"""Extract and store image embeddings from a collection in s3,
using an off-the-shelf pre-trained model"""

import os
import logging
Expand Down
9 changes: 1 addition & 8 deletions scripts/image_metadata.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
"""Heavy-handed approach to create image metadata in usable with `intake`,
for trial use with `scivision`:
https://scivision.readthedocs.io/en/latest/api.html#scivision.io.reader.load_dataset
https://intake.readthedocs.io/en/latest/catalog.html#yaml-format
See also https://github.com/intake/intake-stac
Via https://gallery.pangeo.io/repos/pangeo-data/pangeo-tutorial-gallery/intake.html#Build-an-intake-catalog
"""Create a basic index for the images in an s3 collection
"""

import yaml
Expand Down
2 changes: 1 addition & 1 deletion scripts/params.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cluster:
n_clusters: 5

collection: untagged-images-lana
collection: untagged-images-wala
8 changes: 8 additions & 0 deletions src/cyto_ml/data/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ def prepare_image(image: Image) -> torch.Tensor:
a) Converts the image data to a PyTorch tensor
b) Accepts a single image or batch (no need for torch.stack)
"""
# Flow Cytometer images are 16-bit greyscale
# https://stackoverflow.com/questions/18522295/python-pil-change-greyscale-tif-to-rgb
# TODO revisit

if image.mode == "I;16":
image.point(lambda p: p * 0.0039063096, mode="RGB")
image = image.convert("RGB")

tensor_image = transforms.ToTensor()(image)

# Single image, add a batch dimension
Expand Down
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ def image_dir(fixture_dir):
return os.path.join(fixture_dir, "test_images")


@pytest.fixture
def greyscale_image(fixture_dir):
"""Directory with single plankton images"""
return os.path.join(fixture_dir, "greyscale", "TC18_280524_4840.tif")


@pytest.fixture
def single_image(image_dir):
# The file naming conventions were like this when i got here
Expand Down
7 changes: 7 additions & 0 deletions tests/test_prepare_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,12 @@ def test_single_image(single_image):

assert torch.all((prepared_image >= 0.0) & (prepared_image <= 1.0))

def test_greyscale_image(greyscale_image):

# Tensorise the image (potentially normalise if we have useful values)
prepared_image = load_image(greyscale_image)
assert torch.all((prepared_image >= 0.0) & (prepared_image <= 1.0))


if __name__ == "__main__":
pytest.main()

0 comments on commit 4e90224

Please sign in to comment.