Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overflow error :int too big to convert #260

Open
AngelosDid opened this issue Dec 14, 2023 · 1 comment
Open

Overflow error :int too big to convert #260

AngelosDid opened this issue Dec 14, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@AngelosDid
Copy link

AngelosDid commented Dec 14, 2023

Describe the bug
Hello. I encounter an error while running the snippet for the predictions of cells. Specifically, I have images with a white background and a region of interest standing out from the background (I use a white background because erroneous predictions of stardist due to the contours' contrast are always on the outside part, hence easy to remove with code). Strangely enough, I don't get this error with my previous image datasets, although they are practically the same size and bit-size too.

To reproduce
I am using the snippet provided by stardist for predictions based on sb's trained model. I am also pasting it here but please note that it's slightly changed with respect to the paths, directories etc. But it's practically the same with the one provided by the site.

import os 

from __future__ import print_function, unicode_literals, absolute_import, division
import sys
import numpy as np
import matplotlib
matplotlib.rcParams["image.interpolation"] = 'none'
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'retina'

from glob import glob
from tifffile import imread
from csbdeep.utils import Path, normalize
from csbdeep.io import save_tiff_imagej_compatible

from stardist import random_label_cmap, _draw_polygons, export_imagej_rois
from stardist.models import StarDist2D

np.random.seed(6)
lbl_cmap = random_label_cmap()


os.chdir(r'C:\Users\angdid\Desktop')

X = sorted(glob('LA whitee/*.tiff'))       
X = list(map(imread,X))

n_channel = 1 if X[0].ndim == 2 else X[0].shape[-1]
axis_norm = (0,1)   # normalize channels independently
if n_channel > 1:
    print("Normalizing image channels %s." % ('jointly' if axis_norm is None or 2 in axis_norm else 'independently'))

if True:
    fig, ax = plt.subplots(9,8, figsize=(32,32))   # 5,5 to create a template of 25. For more images change it
    for i,(a,x) in enumerate(zip(ax.flat, X)):
        a.imshow(x,cmap='gray')
        a.set_title(i, fontsize=50)
    [a.axis('off') for a in ax.flat]
    plt.tight_layout()
None;

demo_model = False

if demo_model:
    print (
        "NOTE: This is loading a previously trained demo model!\n"
        "      Please set the variable 'demo_model = False' to load your own trained model.",
        file=sys.stderr, flush=True
    )
    model = StarDist2D.from_pretrained('2D_demo')
else:
    model = StarDist2D(None, name='tdTom', basedir='models')
None;


fig, ax = plt.subplots(9,8, figsize=(32,32))      # 5,5 to create a template of 25. For more images change it

os.chdir(r'C:\Users\angdid\Desktop\LA whitee')
images_names= sorted(glob('*.tiff')) 
os.chdir(r'C:\Users\angdid\Desktop\Results')

for index, (a,x) in enumerate (zip (ax.flat,X)):
    img = normalize(X[index], 1,99.8, axis=axis_norm)
    labels, details = model.predict_instances(img, prob_thresh=0.4)    # also try , prob_thresh=0.4
    a.imshow(labels,cmap='gray')
    a.set_title(index, fontsize=50)
    
    save_tiff_imagej_compatible(f'{images_names[index]}.tiff', img, axes='YX')
    save_tiff_imagej_compatible(f'{images_names[index]}-labels.tiff', labels, axes='YX')
    export_imagej_rois(f'{images_names[index]}.zip', details['coord'])

And the error ...

OverflowError                             Traceback (most recent call last)
Cell In[10], [line 15](vscode-notebook-cell:?execution_count=10&line=15)
     [13](vscode-notebook-cell:?execution_count=10&line=13) save_tiff_imagej_compatible(f'{images_names[index]}.tiff', img, axes='YX')
     [14](vscode-notebook-cell:?execution_count=10&line=14) save_tiff_imagej_compatible(f'{images_names[index]}-labels.tiff', labels, axes='YX')
---> [15](vscode-notebook-cell:?execution_count=10&line=15) export_imagej_rois(f'{images_names[index]}.zip', details['coord'])
     [16](vscode-notebook-cell:?execution_count=10&line=16) print('exported successfully')

File [~\AppData\Roaming\Python\Python311\site-packages\stardist\utils.py:267](https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/angdid/Desktop/Macros%20and%20scripts/Python%20Scripts%20for%20stardist/~/AppData/Roaming/Python/Python311/site-packages/stardist/utils.py:267), in export_imagej_rois(fname, polygons, set_position, subpixel, compression)
    [265](https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/angdid/Desktop/Macros%20and%20scripts/Python%20Scripts%20for%20stardist/~/AppData/Roaming/Python/Python311/site-packages/stardist/utils.py:265) for pos,polygroup in enumerate(polygons,start=1):
    [266](https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/angdid/Desktop/Macros%20and%20scripts/Python%20Scripts%20for%20stardist/~/AppData/Roaming/Python/Python311/site-packages/stardist/utils.py:266)     for i,poly in enumerate(polygroup,start=1):
--> [267](https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/angdid/Desktop/Macros%20and%20scripts/Python%20Scripts%20for%20stardist/~/AppData/Roaming/Python/Python311/site-packages/stardist/utils.py:267)         roi = polyroi_bytearray(poly[1],poly[0], pos=(pos if set_position else None), subpixel=subpixel)
    [268](https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/angdid/Desktop/Macros%20and%20scripts/Python%20Scripts%20for%20stardist/~/AppData/Roaming/Python/Python311/site-packages/stardist/utils.py:268)         roizip.writestr('{pos:03d}_{i:03d}.roi'.format(pos=pos,i=i), roi)

File [~\AppData\Roaming\Python\Python311\site-packages\stardist\utils.py:226](https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/angdid/Desktop/Macros%20and%20scripts/Python%20Scripts%20for%20stardist/~/AppData/Roaming/Python/Python311/site-packages/stardist/utils.py:226), in polyroi_bytearray(x, y, pos, subpixel)
    [224](https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/angdid/Desktop/Macros%20and%20scripts/Python%20Scripts%20for%20stardist/~/AppData/Roaming/Python/Python311/site-packages/stardist/utils.py:224) B[ 4: 6] = _int16(227)       # version
    [225](https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/angdid/Desktop/Macros%20and%20scripts/Python%20Scripts%20for%20stardist/~/AppData/Roaming/Python/Python311/site-packages/stardist/utils.py:225) B[ 6: 8] = _int16(0)         # roi type (0 = polygon)
--> [226](https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/angdid/Desktop/Macros%20and%20scripts/Python%20Scripts%20for%20stardist/~/AppData/Roaming/Python/Python311/site-packages/stardist/utils.py:226) B[ 8:10] = _int16(top)       # bbox top
    [227](https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/angdid/Desktop/Macros%20and%20scripts/Python%20Scripts%20for%20stardist/~/AppData/Roaming/Python/Python311/site-packages/stardist/utils.py:227) B[10:12] = _int16(left)      # bbox left
    [228](https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/angdid/Desktop/Macros%20and%20scripts/Python%20Scripts%20for%20stardist/~/AppData/Roaming/Python/Python311/site-packages/stardist/utils.py:228) B[12:14] = _int16(bottom)    # bbox bottom

File [~\AppData\Roaming\Python\Python311\site-packages\stardist\utils.py:202](https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/angdid/Desktop/Macros%20and%20scripts/Python%20Scripts%20for%20stardist/~/AppData/Roaming/Python/Python311/site-packages/stardist/utils.py:202), in polyroi_bytearray.<locals>._int16(x)
    [201](https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/angdid/Desktop/Macros%20and%20scripts/Python%20Scripts%20for%20stardist/~/AppData/Roaming/Python/Python311/site-packages/stardist/utils.py:201) def _int16(x):
--> [202](https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/angdid/Desktop/Macros%20and%20scripts/Python%20Scripts%20for%20stardist/~/AppData/Roaming/Python/Python311/site-packages/stardist/utils.py:202)     return int(x).to_bytes(2, byteorder='big', signed=True)

OverflowError: int too big to convert

The model that I am using is one that I trained (uploaded in WeTransfer).

Expected behavior
Here I would normally expect to get the output of the predictions, that is, a 'labels' image and a zip file with .roi files inside. My suspicion (although solely intuitive) is that the white background is somehow too much compared to the region standing out. Because previous datasets with larger regions in the same background worked fine.

Data and screenshots
Below two different images. The dataset with the images like the one on the left (larger area) works fine. The dataset with images like the one on the right yields the error.
example

Environment (please complete the following information):

  • StarDist version 0.8.5
  • CSBDeep version 0.7.4
  • TensorFlow version 2.14.0
  • OS: Windows
  • GPU memory (if applicable): 16 GB
@AngelosDid AngelosDid added the bug Something isn't working label Dec 14, 2023
@uschmidt83
Copy link
Member

Hi @AngelosDid, sorry for the very late reply.

The error indicates to me that the coordinates of the (to be exported) objects are not compatible with ImageJ's ROI export format, which uses a short integer data type (-32768...+32767) for the object coordinates.

I'm assuming your image is bigger than 32767 pixels in some dimension and that's the reason the ROI export fails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants