0

This script should take a file from an input folder, put it through a pretrained model, and put the results in the output folder. I get no errors.

import os
import numpy as np
import imageio.v2 as imageio
from cellpose import models

model_path = r"C:\Users\TomoCube\Desktop\New Testing\trained_model\models\cellpose_residual_on_style_on_concatenation_off_trained_model_2025_05_29_14_15_22.066364"
input_folder = r"C:\Users\TomoCube\Desktop\Input Folder"
output_folder = r"C:\Users\TomoCube\Desktop\Output Folder"
os.makedirs(output_folder, exist_ok=True)

# 🔧 TEST: Write a dummy file to confirm save path works
dummy_test_path = os.path.join(output_folder, "dummy_test.tif")
imageio.imwrite(dummy_test_path, np.zeros((10, 10), dtype='uint16'))
print(f"✅ Dummy test file saved: {dummy_test_path}")

model = models.CellposeModel(pretrained_model=model_path, gpu=False)

image_files = [
    f for f in os.listdir(input_folder)
    if f.lower().endswith(('.png', '.jpg', '.tif')) and '_masks' not in f
]
image_files.sort()

for filename in image_files:
    image_path = os.path.join(input_folder, filename)
    print(f"\n🔍 Processing: {filename}")

    img = imageio.imread(image_path)
    print(f"  ➤ Image shape: {img.shape}, dtype: {img.dtype}")

    masks, flows, styles = model.eval(img, channels=[0, 0])
    print(f"  ➤ masks.max(): {masks.max()}, dtype: {masks.dtype}, shape: {masks.shape}")

    output_filename = f"{os.path.splitext(filename)[0]}_mask.tif"
    output_path = os.path.join(output_folder, output_filename)

    try:
        imageio.imwrite(output_path, masks.astype('uint16'))
        print(f"  ✅ Mask saved: {output_path}")
    except Exception as e:
        print(f"  ❌ Failed to save mask for {filename}: {e}")

print("\n✅ All done! Masks saved to:", output_folder)

I tried updating all possible issues but I was getting nothing.

3
  • Does it output anything to the console? Commented May 29 at 18:49
  • maybe first check print( image_files ) or print( len(image_files) ) Commented May 29 at 20:02
  • After image_files.sort() add print(image_files) What do you see? Commented May 30 at 8:00

1 Answer 1

1

The most probable cause is that your image_files list is empty, and as a result, the loop does not get executed.

Try printing out some valuable information to see what's contained in your input folder.

image_files = [
    f for f in os.listdir(input_folder)
    if f.lower().endswith(('.png', '.jpg', '.tif')) and '_masks' not in f
]
image_files.sort()

print("Input folder:", input_folder)
print("Raw contents:", os.listdir(input_folder))
print("Filtered images:", image_files)

If Raw contents is non-empty but Filtered images is [] , then you need to check your filtering logic and make it more flexible.
If Raw contents is indeed empty, then you most likely have the wrong path or input_folder is empty.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.