Example #1
0
    def __getitem__(self, idx: int) -> Dict[str, Any]:
        image_path = self.file_names[idx]

        image = load_rgb(image_path)

        height, width = image.shape[:2]

        # Resize
        resizer = albu.Compose([albu.LongestMaxSize(max_size=768, p=1)], p=1)
        image = resizer(image=image)["image"]

        # pad
        image, pads = pad(image, factor=768)

        # apply augmentations
        image = self.transform(image=image)["image"]

        return {
            "image_id": image_path.stem,
            "features": tensor_from_rgb_image(image),
            "pads": np.array(pads).T,
            "height": height,
            "width": width,
        }
Example #2
0
    [albu.LongestMaxSize(max_size=MAX_SIZE), albu.Normalize(p=1)], p=1
)

st.title("Segment glomeruli")
# What about a TIFF image?
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png"])

if uploaded_file is not None:
    original_image = np.array(Image.open(uploaded_file))
    st.image(original_image, caption="Before", use_column_width=True)
    st.write("")
    st.write("Detecting glomeruli...")

    original_height, original_width = original_image.shape[:2]
    image = transform(image=original_image)["image"]
    padded_image, pads = pad(image, factor=MAX_SIZE, border=cv2.BORDER_CONSTANT)

    x = torch.unsqueeze(tensor_from_rgb_image(padded_image), 0)

    with torch.no_grad():
        prediction = model(x)[0][0]

    mask = (prediction > 0).cpu().numpy().astype(np.uint8)
    mask = unpad(mask, pads)
    mask = cv2.resize(
        mask, (original_width, original_height), interpolation=cv2.INTER_NEAREST
    )
    mask_3_channels = cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB)
    dst = cv2.addWeighted(
        original_image, 1, (mask_3_channels * (0, 255, 0)).astype(np.uint8), 0.5, 0
    )
def test_pad_grayscale(input_array, factor):
    padded_array, pads = pad(input_array, factor)
    unpadded_array = unpad(padded_array, pads)

    assert np.array_equal(input_array, unpadded_array)
Example #4
0
from iglovikov_helper_functions.utils.image_utils import load_rgb, pad, unpad
from iglovikov_helper_functions.dl.pytorch.utils import tensor_from_rgb_image

from midv500models.pre_trained_models import create_model

start = timeit.default_timer()

model = create_model('Unet_resnet34_2020-05-19')
model.eval()

image = load_rgb(sys.argv[1])
# im = Image.fromarray(image)
# im.save()

transform = albu.Compose([albu.Normalize(p=1)], p=1)
padded_image, pads = pad(image, factor=32, border=cv2.BORDER_CONSTANT)
x = transform(image=padded_image)['image']
x = torch.unsqueeze(tensor_from_rgb_image(x), 0)

with torch.no_grad():
    prediction = model(x)[0][0]

mask = (prediction > 0).cpu().numpy().astype(np.uint8)
mask = unpad(mask, pads)
# im = Image.fromarray(mask)
# im.save()

dst = cv2.addWeighted(image, 1, (cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB) *
                                 (0, 255, 0)).astype(np.uint8), 0.5, 0)
# im = Image.fromarray(dst)
# im.save()