Skip to content

Commit

Permalink
add the python part to create rle masks
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Oct 25, 2024
1 parent 07cef6c commit 298da10
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
8 changes: 5 additions & 3 deletions src/main/java/ai/nets/samj/models/EfficientSamJ.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ protected void processMasksWithSam(SharedMemoryArray shmArr, boolean returnAll)
//code += "print(different_mask_vals)" + System.lineSeparator();
code += "cont_x = []" + System.lineSeparator();
code += "cont_y = []" + System.lineSeparator();
code += "rle_masks = []" + System.lineSeparator();
code += "for val in different_mask_vals:" + System.lineSeparator()
+ " if val < 1:" + System.lineSeparator()
+ " continue" + System.lineSeparator()
Expand Down Expand Up @@ -270,9 +271,10 @@ protected void processMasksWithSam(SharedMemoryArray shmArr, boolean returnAll)
+ " predicted_logits = torch.take_along_dim(predicted_logits, sorted_ids[..., None, None], dim=2)" + System.lineSeparator()
+ " mask_val = torch.ge(predicted_logits[0, 0, 0, :, :], 0).cpu().detach().numpy()" + System.lineSeparator()
+ (this.isIJROIManager ? "mask_val[1:, 1:] += mask_val[:-1, :-1]" : "") + System.lineSeparator()
+ " cont_x_val,cont_y_val = get_polygons_from_binary_mask(mask_val, only_biggest=" + (!returnAll ? "True" : "False") + ")" + System.lineSeparator()
+ " cont_x_val,cont_y_val,rle_val = get_polygons_from_binary_mask(mask_val, only_biggest=" + (!returnAll ? "True" : "False") + ")" + System.lineSeparator()
+ " cont_x += cont_x_val" + System.lineSeparator()
+ " cont_y += cont_y_val" + System.lineSeparator()
+ " rle_masks += rle_val" + System.lineSeparator()
+ "task.update('all contours traced')" + System.lineSeparator()
+ "task.outputs['contours_x'] = cont_x" + System.lineSeparator()
+ "task.outputs['contours_y'] = cont_y" + System.lineSeparator()
Expand Down Expand Up @@ -317,7 +319,7 @@ protected void processPointsWithSAM(int nPoints, int nNegPoints, boolean returnA
+ "task.update(str(mask.shape))" + System.lineSeparator()
//+ "np.save('/temp/aa.npy', mask)" + System.lineSeparator()
+ (this.isIJROIManager ? "mask[1:, 1:] += mask[:-1, :-1]" : "") + System.lineSeparator()
+ "contours_x,contours_y = get_polygons_from_binary_mask(mask, only_biggest=" + (!returnAll ? "True" : "False") + ")" + System.lineSeparator()
+ "contours_x,contours_y,rle_masks = get_polygons_from_binary_mask(mask, only_biggest=" + (!returnAll ? "True" : "False") + ")" + System.lineSeparator()
+ "task.update('all contours traced')" + System.lineSeparator()
+ "task.outputs['contours_x'] = contours_x" + System.lineSeparator()
+ "task.outputs['contours_y'] = contours_y" + System.lineSeparator()
Expand Down Expand Up @@ -349,7 +351,7 @@ protected void processBoxWithSAM(boolean returnAll) {
+ "task.update(str(mask.shape))" + System.lineSeparator()
//+ "np.save('/home/carlos/git/mask.npy', mask)" + System.lineSeparator()
+ (this.isIJROIManager ? "mask[1:, 1:] += mask[:-1, :-1]" : "") + System.lineSeparator()
+ "contours_x,contours_y = get_polygons_from_binary_mask(mask, only_biggest=" + (!returnAll ? "True" : "False") + ")" + System.lineSeparator()
+ "contours_x,contours_y,rle_masks = get_polygons_from_binary_mask(mask, only_biggest=" + (!returnAll ? "True" : "False") + ")" + System.lineSeparator()
+ "task.update('all contours traced')" + System.lineSeparator()
+ "task.outputs['contours_x'] = contours_x" + System.lineSeparator()
+ "task.outputs['contours_y'] = contours_y" + System.lineSeparator()
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/ai/nets/samj/models/EfficientViTSamJ.java
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ protected void processMasksWithSam(SharedMemoryArray shmArr, boolean returnAll)
code += "different_mask_vals = np.unique(mask)" + System.lineSeparator();
code += "contours_x = []" + System.lineSeparator();
code += "contours_y = []" + System.lineSeparator();
code += "rle_masks = []" + System.lineSeparator();
code += "for val in different_mask_vals:" + System.lineSeparator()
+ " if val < 1:" + System.lineSeparator()
+ " continue" + System.lineSeparator()
Expand All @@ -362,9 +363,10 @@ protected void processMasksWithSam(SharedMemoryArray shmArr, boolean returnAll)
+ " box=None,)" + System.lineSeparator()
//+ "np.save('/temp/aa.npy', mask)" + System.lineSeparator()
+ (this.isIJROIManager ? "mask_val[0, 1:, 1:] += mask_val[0, :-1, :-1]" : "") + System.lineSeparator()
+ " contours_x_val,contours_y_val = get_polygons_from_binary_mask(mask_val[0], only_biggest=" + (!returnAll ? "True" : "False") + ")" + System.lineSeparator()
+ " contours_x_val,contours_y_val,rle_val = get_polygons_from_binary_mask(mask_val[0], only_biggest=" + (!returnAll ? "True" : "False") + ")" + System.lineSeparator()
+ " contours_x += contours_x_val" + System.lineSeparator()
+ " contours_y += contours_y_val" + System.lineSeparator()
+ " rle_masks += rle_val" + System.lineSeparator()
+ "task.update('all contours traced')" + System.lineSeparator()
+ "task.outputs['contours_x'] = contours_x" + System.lineSeparator()
+ "task.outputs['contours_y'] = contours_y" + System.lineSeparator()
Expand Down Expand Up @@ -403,7 +405,7 @@ protected void processPointsWithSAM(int nPoints, int nNegPoints, boolean returnA
+ (this.isIJROIManager ? "mask[0, 1:, 1:] += mask[0, :-1, :-1]" : "") + System.lineSeparator()
//+ (this.isIJROIManager ? "mask[0, :, 1:] += mask[0, :, :-1]" : "") + System.lineSeparator()
//+ "np.save('/home/carlos/git/aa.npy', mask)" + System.lineSeparator()
+ "contours_x,contours_y = get_polygons_from_binary_mask(mask[0], only_biggest=" + (!returnAll ? "True" : "False") + ")" + System.lineSeparator()
+ "contours_x, contours_y, rle_masks = get_polygons_from_binary_mask(mask[0], only_biggest=" + (!returnAll ? "True" : "False") + ")" + System.lineSeparator()
+ "task.update('all contours traced')" + System.lineSeparator()
+ "task.outputs['contours_x'] = contours_x" + System.lineSeparator()
+ "task.outputs['contours_y'] = contours_y" + System.lineSeparator()
Expand All @@ -425,7 +427,7 @@ protected void processBoxWithSAM(boolean returnAll) {
+ "task.update(str(mask.shape))" + System.lineSeparator()
//+ "np.save('/home/carlos/git/mask.npy', mask)" + System.lineSeparator()
+ (this.isIJROIManager ? "mask[0, 1:, 1:] += mask[0, :-1, :-1]" : "") + System.lineSeparator()
+ "contours_x,contours_y = get_polygons_from_binary_mask(mask[0], only_biggest=" + (!returnAll ? "True" : "False") + ")" + System.lineSeparator()
+ "contours_x,contours_y,rle_masks = get_polygons_from_binary_mask(mask[0], only_biggest=" + (!returnAll ? "True" : "False") + ")" + System.lineSeparator()
+ "task.update('all contours traced')" + System.lineSeparator()
+ "task.outputs['contours_x'] = contours_x" + System.lineSeparator()
+ "task.outputs['contours_y'] = contours_y" + System.lineSeparator()
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/ai/nets/samj/models/PythonMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,22 @@ public class PythonMethods {
+ " labels = measure.regionprops( measure.label(sam_result,connectivity=1) )" + System.lineSeparator()
+ " x_contours = []" + System.lineSeparator()
+ " y_contours = []" + System.lineSeparator()
+ " rles = []" + System.lineSeparator()
+ " sizes = []" + System.lineSeparator()
+ " for obj in labels:" + System.lineSeparator()
+ " if obj.num_pixels >= at_least_of_this_size:" + System.lineSeparator()
+ " x_coords,y_coords = trace_contour(obj.image, obj.num_pixels, obj.bbox[1],obj.bbox[0])" + System.lineSeparator()
+ " np.save('/home/carlos/git/test.npy', obj.image)" + System.lineSeparator()
+ " rles.append(encode_rle(obj.image))" + System.lineSeparator()
+ " x_contours.append(x_coords)" + System.lineSeparator()
+ " y_contours.append(y_coords)" + System.lineSeparator()
+ " sizes.append(obj.num_pixels)" + System.lineSeparator()
+ " if only_biggest:" + System.lineSeparator()
+ " max_size_pos = np.array(sizes).argmax()" + System.lineSeparator()
+ " x_contours = [x_contours[max_size_pos]]" + System.lineSeparator()
+ " y_contours = [y_contours[max_size_pos]]" + System.lineSeparator()
+ " return x_contours,y_contours" + System.lineSeparator()
+ " rles = [rles[max_size_pos]]" + System.lineSeparator()
+ " return x_contours, y_contours, rles" + System.lineSeparator()
+ "globals()['is_edge_pixel'] = is_edge_pixel" + System.lineSeparator()
+ "globals()['find_contour_neighbors'] = find_contour_neighbors" + System.lineSeparator()
+ "globals()['trace_contour'] = trace_contour" + System.lineSeparator()
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/ai/nets/samj/models/Sam2.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ protected void processMasksWithSam(SharedMemoryArray shmArr, boolean returnAll)
code += "different_mask_vals = np.unique(mask)" + System.lineSeparator();
code += "contours_x = []" + System.lineSeparator();
code += "contours_y = []" + System.lineSeparator();
code += "rle_masks = []" + System.lineSeparator();
code += "for val in different_mask_vals:" + System.lineSeparator()
+ " if val < 1:" + System.lineSeparator()
+ " continue" + System.lineSeparator()
Expand All @@ -343,13 +344,14 @@ protected void processMasksWithSam(SharedMemoryArray shmArr, boolean returnAll)
+ " box=None,)" + System.lineSeparator()
//+ "np.save('/temp/aa.npy', mask)" + System.lineSeparator()
+ (this.isIJROIManager ? "mask_val[0, 1:, 1:] += mask_val[0, :-1, :-1]" : "") + System.lineSeparator()
+ " contours_x_val,contours_y_val = get_polygons_from_binary_mask(mask_val[0], only_biggest=" + (!returnAll ? "True" : "False") + ")" + System.lineSeparator()
+ " contours_x_val,contours_y_val, rles_vals = get_polygons_from_binary_mask(mask_val[0], only_biggest=" + (!returnAll ? "True" : "False") + ")" + System.lineSeparator()
+ " contours_x += contours_x_val" + System.lineSeparator()
+ " contours_y += contours_y_val" + System.lineSeparator()
+ " rle_masks += rles_vals" + System.lineSeparator()
+ "task.update('all contours traced')" + System.lineSeparator()
+ "task.outputs['contours_x'] = contours_x" + System.lineSeparator()
+ "task.outputs['contours_y'] = contours_y" + System.lineSeparator()
+ "task.outputs['rle'] = rle_masks" + System.lineSeparator();
+ "task.outputs['rle'] = rle_masks" + System.lineSeparator();
code += "mask = 0" + System.lineSeparator();
code += "shm_mask.close()" + System.lineSeparator();
code += "shm_mask.unlink()" + System.lineSeparator();
Expand Down Expand Up @@ -384,7 +386,7 @@ protected void processPointsWithSAM(int nPoints, int nNegPoints, boolean returnA
+ (this.isIJROIManager ? "mask[0, 1:, 1:] += mask[0, :-1, :-1]" : "") + System.lineSeparator()
//+ (this.isIJROIManager ? "mask[0, :, 1:] += mask[0, :, :-1]" : "") + System.lineSeparator()
//+ "np.save('/home/carlos/git/aa.npy', mask)" + System.lineSeparator()
+ "contours_x,contours_y = get_polygons_from_binary_mask(mask[0], only_biggest=" + (!returnAll ? "True" : "False") + ")" + System.lineSeparator()
+ "contours_x, contours_y, rle_masks = get_polygons_from_binary_mask(mask[0], only_biggest=" + (!returnAll ? "True" : "False") + ")" + System.lineSeparator()
+ "task.update('all contours traced')" + System.lineSeparator()
+ "task.outputs['contours_x'] = contours_x" + System.lineSeparator()
+ "task.outputs['contours_y'] = contours_y" + System.lineSeparator()
Expand All @@ -406,7 +408,7 @@ protected void processBoxWithSAM(boolean returnAll) {
+ "task.update(str(mask.shape))" + System.lineSeparator()
//+ "np.save('/home/carlos/git/mask.npy', mask)" + System.lineSeparator()
+ (this.isIJROIManager ? "mask[0, 1:, 1:] += mask[0, :-1, :-1]" : "") + System.lineSeparator()
+ "contours_x,contours_y = get_polygons_from_binary_mask(mask[0], only_biggest=" + (!returnAll ? "True" : "False") + ")" + System.lineSeparator()
+ "contours_x, contours_y, rle_masks = get_polygons_from_binary_mask(mask[0], only_biggest=" + (!returnAll ? "True" : "False") + ")" + System.lineSeparator()
+ "task.update('all contours traced')" + System.lineSeparator()
+ "task.outputs['contours_x'] = contours_x" + System.lineSeparator()
+ "task.outputs['contours_y'] = contours_y" + System.lineSeparator()
Expand Down

0 comments on commit 298da10

Please sign in to comment.