<jupyter_start><jupyter_text>DataWe assume that data has already been downloaded in via notebook [1_data.ipynb](1_data.ipynb).  We now load images from the sub-folder `test` that have not been used during training.<jupyter_code>X = sorted(glob('data/test/images/*.tif'))
X = list(map(imread,X))

n_channel = 1 if X[0].ndim == 3 else X[0].shape[-1]
axis_norm = (0,1,2)   # normalize channels independently
# axis_norm = (0,1,2,3) # normalize channels jointly
if n_channel > 1:
    print("Normalizing image channels %s." % ('jointly' if axis_norm is None or 2 in axis_norm else 'independently'))
# show all test images
if True:
    fig, ax = plt.subplots(1,3, figsize=(16,16))
    for i,(a,x) in enumerate(zip(ax.flat, X)):
        a.imshow(x[x.shape[0]//2],cmap='gray')
        a.set_title(i)
    [a.axis('off') for a in ax.flat]
    plt.tight_layout()
None;<jupyter_output><empty_output><jupyter_text>Load trained modelIf you trained your own StarDist model (and optimized its thresholds) via notebook [2_training.ipynb](2_training.ipynb), then please set `demo_model = False` below.<jupyter_code>demo_model = True

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 = StarDist3D(None, name='3D_demo', basedir='../../models/examples')
else:
    model = StarDist3D(None, name='stardist', basedir='models')
None;<jupyter_output>NOTE: This is loading a previously trained demo model!
      Please set the variable 'demo_model = False' to load your own trained model.<jupyter_text>PredictionMake sure to normalize the input image beforehand or supply a `normalizer` to the prediction function.Calling `model.predict_instances` will- predict object probabilities and star-convex polygon distances (see `model.predict` if you want those)- perform non-maximum suppression (with overlap threshold `nms_thresh`) for polygons above object probability threshold `prob_thresh`.- render all remaining polygon instances in a label image- return the label instances image and also the details (coordinates, etc.) of all remaining polygons<jupyter_code>img = normalize(X[2], 1,99.8, axis=axis_norm)
labels, details = model.predict_instances(img)
plt.figure(figsize=(13,10))
z = max(0, img.shape[0] // 2 - 5)
plt.subplot(121)
plt.imshow((img if img.ndim==3 else img[...,:3])[z], clim=(0,1), cmap='gray')
plt.title('Raw image (XY slice)')
plt.axis('off')
plt.subplot(122)
plt.imshow((img if img.ndim==3 else img[...,:3])[z], clim=(0,1), cmap='gray')
plt.imshow(labels[z], cmap=lbl_cmap, alpha=0.5)
plt.title('Image and predicted labels (XY slice)')
plt.axis('off');<jupyter_output><empty_output><jupyter_text>Example results<jupyter_code>def example(model, i, show_dist=True):
    img = normalize(X[i], 1,99.8, axis=axis_norm)
    labels, details = model.predict_instances(img)

    plt.figure(figsize=(13,8))
    z = img.shape[0] // 2
    y = img.shape[1] // 2
    img_show = img if img.ndim==3 else img[...,:3]    
    plt.subplot(221); plt.imshow(img_show[z],   cmap='gray', clim=(0,1)); plt.axis('off'); plt.title('XY slice')
    plt.subplot(222); plt.imshow(img_show[:,y], cmap='gray', clim=(0,1)); plt.axis('off'); plt.title('XZ slice')
    plt.subplot(223); plt.imshow(img_show[z],   cmap='gray', clim=(0,1)); plt.axis('off'); plt.title('XY slice')
    plt.imshow(labels[z], cmap=lbl_cmap, alpha=0.5)
    plt.subplot(224); plt.imshow(img_show[:,y], cmap='gray', clim=(0,1)); plt.axis('off'); plt.title('XZ slice')
    plt.imshow(labels[:,y], cmap=lbl_cmap, alpha=0.5)
    plt.tight_layout()
    plt.show()
example(model, 0)
example(model, 1)
example(model, 2)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15662330767188745, 'fraction_numerical': 0.026015396867533847, 'mean_word_length': 4.8600311041990665, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '176381', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1379, 'n_tokens_neox': 1324, 'n_words': 344, 'path': 'examples/3D/3_prediction.ipynb', 'repo_name': 'feberhardt/stardist', 'size': '3767'}