segment-anything-models-java / SAMJ-IJ

An ImageJ/Fiji plugin designed to effortlessly integrate Segment-Anything models (SAMs) without code.
Apache License 2.0
54 stars 6 forks source link

error after installation; plugin stuck #25

Closed mocherry closed 3 weeks ago

mocherry commented 1 month ago

Dear developer,

I just came across SAMJ-IJ, installed it without any obvious error messages. However, when I run it I get an error: java.lang.NukkPointer Exception and the following long (sorry about that) console output (see below).

I have no idea why and where things go wrong Start the encoding task.update('start') import numpy as np from skimage import measure measure.label(np.ones((10, 10)), connectivity=1) import torch import sys sys.path.append(r'C:\Users\kirsch\Fiji.app\appose_x86_64\envs\efficient_sam_env\EfficientSAM') from multiprocessing import shared_memory task.update('import sam') from efficient_sam.efficient_sam import build_efficient_sam task.update('imported')

predictor = build_efficient_sam(encoder_patch_embed_dim=384,encoder_num_heads=6,checkpoint=r'C:\Users\kirsch\Fiji.app\appose_x86_64\envs\efficient_sam_env\EfficientSAM\weights\efficient_sam_vits.pt',).eval() task.update('created predictor') encodings_map = {} globals()['encodings_map'] = encodings_map globals()['shared_memory'] = shared_memory globals()['measure'] = measure globals()['np'] = np globals()['torch'] = torch globals()['predictor'] = predictor def is_edge_pixel(image, cx,cy):

assuming image[cy,cx] != 0

h,w = image.shape
if cx < 0 or cx >= w or cy < 0 or cy >= h:
    return False
# NB: cx,cy are valid coords
return cy == 0 or image[cy-1,cx] == 0 or cx == 0 or image[cy,cx-1] == 0 or cx == w-1 or image[cy,cx+1] == 0 or cy == h-1 or image[cy+1,cx] == 0

def find_contour_neighbors(image, cx,cy, last_forward_dir): ccw_dir = [8,9,6,3,2,1,4,7] # "numpad directions"

so, directions are coded, and numbers 1-4,6-9 are used,

# let's use them as indices to the arrays below:
dir_idx = [0, 5,4,3, 6,0,2, 7,0,1]             # where is the code in the ccw_dir
counter_shifted_dir = [0, 6,9,8, 3,0,7, 2,1,4] # opposite code plus one in ccw
dir_dx = [0, -1,0,+1, -1,0,+1, -1,0,1]         # code translated to shift in x-axis
dir_dy = [0, +1,+1,+1, 0,0,0,  -1,-1,-1]       # code translated to shift in y-axis
#
# find first bro in the ccw direction starting from the direction
# from which we have come to this position
test_dir = counter_shifted_dir[last_forward_dir]
#print(f"starting pos [{cx},{cy}], forward dir code {last_forward_dir}, thus examine code {test_dir}")
nx = cx+dir_dx[test_dir]
ny = cy+dir_dy[test_dir]
while not (is_edge_pixel(image,nx,ny) and image[ny,nx] != 0):
    #print(f"  not contour at [{nx},{ny}], examined dir code {test_dir}")
    test_dir = ccw_dir[ (dir_idx[test_dir]+1)%8 ]
    nx = cx+dir_dx[test_dir]
    ny = cy+dir_dy[test_dir]
#print(f"  happy at [{nx},{ny}], examined dir code {test_dir}")
return nx,ny,test_dir

def trace_contour(image, max_iters, offset_x = 0, offset_y = 0): sy = 0 sx = np.where(image[0] != 0)[0][0] last_forward_dir = 1 # x_coords = [int(sx+offset_x)] y_coords = [int(sy+offset_y)] x,y,last_forward_dir = find_contour_neighbors(image, sx,sy,last_forward_dir) cnt = 1 while not (x == sx and y == sy) and cnt < max_iters: x_coords.append(int(x+offset_x)) y_coords.append(int(y+offset_y)) x,y,last_forward_dir = find_contour_neighbors(image, x,y,last_forward_dir) cnt += 1 # return x_coords,y_coords

def get_polygons_from_binary_mask(sam_result, at_least_of_this_size = 3, only_biggest=False): labels = measure.regionprops( measure.label(sam_result,connectivity=1) ) x_contours = [] y_contours = [] sizes = [] for obj in labels: if obj.num_pixels >= at_least_of_this_size: x_coords,y_coords = trace_contour(obj.image, obj.num_pixels, obj.bbox[1],obj.bbox[0]) x_contours.append(x_coords) y_contours.append(y_coords) sizes.append(obj.num_pixels) if only_biggest: max_size_pos = np.array(sizes).argmax() x_contours = [x_contours[max_size_pos]] y_contours = [y_contours[max_size_pos]] return x_contours,y_contours globals()['is_edge_pixel'] = is_edge_pixel globals()['find_contour_neighbors'] = find_contour_neighbors globals()['trace_contour'] = trace_contour globals()['get_polygons_from_binary_mask'] = get_polygons_from_binary_mask

Finished the encoding Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: com.sun.jna.Native.load(Ljava/lang/String;Ljava/lang/Class;Ljava/util/Map;)Lcom/sun/jna/Library; at com.sun.jna.platform.win32.Kernel32.(Kernel32.java:44) at io.bioimage.modelrunner.tensor.shm.SharedMemoryArrayWin.checkSHMExists(SharedMemoryArrayWin.java:280) at io.bioimage.modelrunner.tensor.shm.SharedMemoryArrayWin.(SharedMemoryArrayWin.java:221) at io.bioimage.modelrunner.tensor.shm.SharedMemoryArrayWin.(SharedMemoryArrayWin.java:169) at io.bioimage.modelrunner.tensor.shm.SharedMemoryArrayWin.create(SharedMemoryArrayWin.java:348) at io.bioimage.modelrunner.tensor.shm.SharedMemoryArray.create(SharedMemoryArray.java:303) at ai.nets.samj.models.EfficientSamJ.createSHMArray(EfficientSamJ.java:423) at ai.nets.samj.models.AbstractSamJ.sendCropAsNp(AbstractSamJ.java:339) at ai.nets.samj.models.AbstractSamJ.reencodeCrop(AbstractSamJ.java:295) at ai.nets.samj.models.AbstractSamJ.reencodeCrop(AbstractSamJ.java:290) at ai.nets.samj.models.AbstractSamJ.processBox(AbstractSamJ.java:630) at ai.nets.samj.communication.model.EfficientSAM.fetch2dSegmentation(EfficientSAM.java:190) at ai.nets.samj.ij.ui.IJ1PromptsProvider.submitRectPrompt(IJ1PromptsProvider.java:598) at ai.nets.samj.ij.ui.IJ1PromptsProvider.mouseReleased(IJ1PromptsProvider.java:421) at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:290) at java.awt.Component.processMouseEvent(Component.java:6539) at java.awt.Component.processEvent(Component.java:6304) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84) at java.awt.EventQueue$4.run(EventQueue.java:733) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74) at java.awt.EventQueue.dispatchEvent(EventQueue.java:730) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

carlosuc3m commented 1 month ago

Hello @mocherry thanks a lot for trying and for your feedback! Looking into it now I will get back soon!

carlosuc3m commented 1 month ago

Hello again @mocherry I have updated the SAMJ version in Fiji, could you please update it and try if it is working? In the updated version SAM2 Tiny is the first model on the left. Sorry for the inconvenience and thanks for your patience! Regards, Carlos

carlosuc3m commented 1 month ago

Also just so you know, the new plugin implements crtl+Z for undo and ctrl+Y for redo

carlosuc3m commented 3 weeks ago

I will close the issue as i am assuming it is already corrected. Please feel free to reopen it if it is still not working! @mocherry