BoyaWu10 commited on
Commit
a2f2ef1
1 Parent(s): a950ee6
Files changed (26) hide show
  1. __pycache__/utils.cpython-39.pyc +0 -0
  2. app.py +25 -0
  3. model/__pycache__/inference_cpu.cpython-39.pyc +0 -0
  4. model/data_process/__pycache__/demo_data_process.cpython-39.pyc +0 -0
  5. model/data_process/demo_data_process.py +5 -3
  6. model/inference_cpu.py +2 -3
  7. model/network/__pycache__/model.cpython-39.pyc +0 -0
  8. model/segment_anything_volumetric/__pycache__/__init__.cpython-39.pyc +0 -0
  9. model/segment_anything_volumetric/__pycache__/automatic_mask_generator.cpython-39.pyc +0 -0
  10. model/segment_anything_volumetric/__pycache__/build_sam.cpython-39.pyc +0 -0
  11. model/segment_anything_volumetric/__pycache__/predictor.cpython-39.pyc +0 -0
  12. model/segment_anything_volumetric/modeling/__pycache__/__init__.cpython-39.pyc +0 -0
  13. model/segment_anything_volumetric/modeling/__pycache__/common.cpython-39.pyc +0 -0
  14. model/segment_anything_volumetric/modeling/__pycache__/image_encoder.cpython-39.pyc +0 -0
  15. model/segment_anything_volumetric/modeling/__pycache__/image_encoder_swin.cpython-39.pyc +0 -0
  16. model/segment_anything_volumetric/modeling/__pycache__/mask_decoder.cpython-39.pyc +0 -0
  17. model/segment_anything_volumetric/modeling/__pycache__/prompt_encoder.cpython-39.pyc +0 -0
  18. model/segment_anything_volumetric/modeling/__pycache__/sam.cpython-39.pyc +0 -0
  19. model/segment_anything_volumetric/modeling/__pycache__/transformer.cpython-39.pyc +0 -0
  20. model/segment_anything_volumetric/utils/__pycache__/__init__.cpython-39.pyc +0 -0
  21. model/segment_anything_volumetric/utils/__pycache__/amg.cpython-39.pyc +0 -0
  22. model/segment_anything_volumetric/utils/__pycache__/transforms.cpython-39.pyc +0 -0
  23. model/utils/__pycache__/monai_inferers_utils.cpython-39.pyc +0 -0
  24. model/utils/__pycache__/visualize.cpython-39.pyc +0 -0
  25. model/utils/monai_inferers_utils.py +1 -1
  26. utils.py +1 -1
__pycache__/utils.cpython-39.pyc CHANGED
Binary files a/__pycache__/utils.cpython-39.pyc and b/__pycache__/utils.cpython-39.pyc differ
 
app.py CHANGED
@@ -9,6 +9,8 @@ import matplotlib.pyplot as plt
9
  from PIL import Image, ImageDraw
10
  import monai.transforms as transforms
11
  from utils import show_points, make_fig, reflect_points_into_model, initial_rectangle, reflect_json_data_to_3D_box, reflect_box_into_model, run
 
 
12
 
13
  print('script run')
14
 
@@ -24,6 +26,7 @@ if 'reset_demo_case' not in st.session_state:
24
 
25
  if 'preds_3D' not in st.session_state:
26
  st.session_state.preds_3D = None
 
27
 
28
  if 'data_item' not in st.session_state:
29
  st.session_state.data_item = None
@@ -117,6 +120,7 @@ if st.session_state.option is not None and \
117
  st.session_state.data_item = process_ct_gt(st.session_state.option)
118
  st.session_state.reset_demo_case = False
119
  st.session_state.preds_3D = None
 
120
 
121
  prompt_col1, prompt_col2 = st.columns(2)
122
 
@@ -150,6 +154,7 @@ with prompt_col2:
150
  ["Point prompt", "Box prompt"],
151
  on_change=clear_prompts,
152
  disabled=(not spatial_prompt_on))
 
153
 
154
  if spatial_prompt == "Point prompt":
155
  st.session_state.use_point_prompt = True
@@ -284,8 +289,28 @@ with col1:
284
  disabled=(st.session_state.option is None or (len(st.session_state.points)==0 and not st.session_state.use_box_prompt and st.session_state.preds_3D is None))):
285
  clear_prompts()
286
  st.session_state.preds_3D = None
 
287
  st.rerun()
288
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
289
  with col3:
290
  run_button_name = 'Run'if not st.session_state.running else 'Running'
291
  if st.button(run_button_name, type="primary", use_container_width=True,
 
9
  from PIL import Image, ImageDraw
10
  import monai.transforms as transforms
11
  from utils import show_points, make_fig, reflect_points_into_model, initial_rectangle, reflect_json_data_to_3D_box, reflect_box_into_model, run
12
+ import nibabel as nib
13
+ import tempfile
14
 
15
  print('script run')
16
 
 
26
 
27
  if 'preds_3D' not in st.session_state:
28
  st.session_state.preds_3D = None
29
+ st.session_state.preds_3D_ori = None
30
 
31
  if 'data_item' not in st.session_state:
32
  st.session_state.data_item = None
 
120
  st.session_state.data_item = process_ct_gt(st.session_state.option)
121
  st.session_state.reset_demo_case = False
122
  st.session_state.preds_3D = None
123
+ st.session_state.preds_3D_ori = None
124
 
125
  prompt_col1, prompt_col2 = st.columns(2)
126
 
 
154
  ["Point prompt", "Box prompt"],
155
  on_change=clear_prompts,
156
  disabled=(not spatial_prompt_on))
157
+ st.session_state.enforce_zoom = st.checkbox('Enforce zoom-out-zoom-in')
158
 
159
  if spatial_prompt == "Point prompt":
160
  st.session_state.use_point_prompt = True
 
289
  disabled=(st.session_state.option is None or (len(st.session_state.points)==0 and not st.session_state.use_box_prompt and st.session_state.preds_3D is None))):
290
  clear_prompts()
291
  st.session_state.preds_3D = None
292
+ st.session_state.preds_3D_ori = None
293
  st.rerun()
294
 
295
+ with col2:
296
+ img_nii = None
297
+ if st.session_state.preds_3D_ori is not None and st.session_state.data_item is not None:
298
+ meta_dict = st.session_state.data_item['meta']
299
+ pred_array = st.session_state.preds_3D_ori.transpose(2, 1, 0)
300
+ img_nii = nib.Nifti1Image(pred_array, affine=meta_dict['affine'])
301
+
302
+ with tempfile.NamedTemporaryFile(suffix=".nii.gz") as tmpfile:
303
+ nib.save(img_nii, tmpfile.name)
304
+ with open(tmpfile.name, "rb") as f:
305
+ bytes_data = f.read()
306
+ st.download_button(
307
+ label="Download result(.nii.gz)",
308
+ data=bytes_data,
309
+ file_name="segvol_preds.nii.gz",
310
+ mime="application/octet-stream",
311
+ disabled=img_nii is None
312
+ )
313
+
314
  with col3:
315
  run_button_name = 'Run'if not st.session_state.running else 'Running'
316
  if st.button(run_button_name, type="primary", use_container_width=True,
model/__pycache__/inference_cpu.cpython-39.pyc CHANGED
Binary files a/model/__pycache__/inference_cpu.cpython-39.pyc and b/model/__pycache__/inference_cpu.cpython-39.pyc differ
 
model/data_process/__pycache__/demo_data_process.cpython-39.pyc CHANGED
Binary files a/model/data_process/__pycache__/demo_data_process.cpython-39.pyc and b/model/data_process/__pycache__/demo_data_process.cpython-39.pyc differ
 
model/data_process/demo_data_process.py CHANGED
@@ -60,7 +60,7 @@ def process_ct_gt(case_path, spatial_size=(32,256,256)):
60
  DimTranspose(keys=["image"]),
61
  MinMaxNormalization(),
62
  transforms.SpatialPadd(keys=["image"], spatial_size=spatial_size, mode='constant'),
63
- transforms.CropForegroundd(keys=["image"], source_key="image"),
64
  transforms.ToTensord(keys=["image"]),
65
  ]
66
  )
@@ -70,13 +70,14 @@ def process_ct_gt(case_path, spatial_size=(32,256,256)):
70
  item = {}
71
  # generate ct_voxel_ndarray
72
  if type(case_path) is str:
73
- ct_voxel_ndarray, _ = img_loader(case_path)
74
  else:
75
  bytes_data = case_path.read()
76
  with tempfile.NamedTemporaryFile(suffix='.nii.gz') as tmp:
77
  tmp.write(bytes_data)
78
  tmp.seek(0)
79
- ct_voxel_ndarray, _ = img_loader(tmp.name)
 
80
  ct_voxel_ndarray = np.array(ct_voxel_ndarray).squeeze()
81
  ct_voxel_ndarray = np.expand_dims(ct_voxel_ndarray, axis=0)
82
  item['image'] = ct_voxel_ndarray
@@ -88,4 +89,5 @@ def process_ct_gt(case_path, spatial_size=(32,256,256)):
88
 
89
  item_z = z_transform(item)
90
  item['z_image'] = item_z['image']
 
91
  return item
 
60
  DimTranspose(keys=["image"]),
61
  MinMaxNormalization(),
62
  transforms.SpatialPadd(keys=["image"], spatial_size=spatial_size, mode='constant'),
63
+ # transforms.CropForegroundd(keys=["image"], source_key="image"),
64
  transforms.ToTensord(keys=["image"]),
65
  ]
66
  )
 
70
  item = {}
71
  # generate ct_voxel_ndarray
72
  if type(case_path) is str:
73
+ ct_voxel_ndarray, meta_tensor_dict = img_loader(case_path)
74
  else:
75
  bytes_data = case_path.read()
76
  with tempfile.NamedTemporaryFile(suffix='.nii.gz') as tmp:
77
  tmp.write(bytes_data)
78
  tmp.seek(0)
79
+ ct_voxel_ndarray, meta_tensor_dict = img_loader(tmp.name)
80
+
81
  ct_voxel_ndarray = np.array(ct_voxel_ndarray).squeeze()
82
  ct_voxel_ndarray = np.expand_dims(ct_voxel_ndarray, axis=0)
83
  item['image'] = ct_voxel_ndarray
 
89
 
90
  item_z = z_transform(item)
91
  item['z_image'] = item_z['image']
92
+ item['meta'] = meta_tensor_dict
93
  return item
model/inference_cpu.py CHANGED
@@ -167,7 +167,6 @@ def inference_case(_image, _image_zoom_out, _point_prompt, text_prompt, _box_pro
167
  transforms.Resize((325,325,325), mode='trilinear')
168
  ]
169
  )
170
- logits = resize_transform(logits)[0]
171
- print(logits.shape)
172
- return (torch.sigmoid(logits) > 0.5).int().numpy()
173
 
 
167
  transforms.Resize((325,325,325), mode='trilinear')
168
  ]
169
  )
170
+ logits_resize = resize_transform(logits)[0]
171
+ return (torch.sigmoid(logits_resize) > 0.5).int().numpy(), (torch.sigmoid(logits) > 0.5).int().numpy()
 
172
 
model/network/__pycache__/model.cpython-39.pyc CHANGED
Binary files a/model/network/__pycache__/model.cpython-39.pyc and b/model/network/__pycache__/model.cpython-39.pyc differ
 
model/segment_anything_volumetric/__pycache__/__init__.cpython-39.pyc CHANGED
Binary files a/model/segment_anything_volumetric/__pycache__/__init__.cpython-39.pyc and b/model/segment_anything_volumetric/__pycache__/__init__.cpython-39.pyc differ
 
model/segment_anything_volumetric/__pycache__/automatic_mask_generator.cpython-39.pyc CHANGED
Binary files a/model/segment_anything_volumetric/__pycache__/automatic_mask_generator.cpython-39.pyc and b/model/segment_anything_volumetric/__pycache__/automatic_mask_generator.cpython-39.pyc differ
 
model/segment_anything_volumetric/__pycache__/build_sam.cpython-39.pyc CHANGED
Binary files a/model/segment_anything_volumetric/__pycache__/build_sam.cpython-39.pyc and b/model/segment_anything_volumetric/__pycache__/build_sam.cpython-39.pyc differ
 
model/segment_anything_volumetric/__pycache__/predictor.cpython-39.pyc CHANGED
Binary files a/model/segment_anything_volumetric/__pycache__/predictor.cpython-39.pyc and b/model/segment_anything_volumetric/__pycache__/predictor.cpython-39.pyc differ
 
model/segment_anything_volumetric/modeling/__pycache__/__init__.cpython-39.pyc CHANGED
Binary files a/model/segment_anything_volumetric/modeling/__pycache__/__init__.cpython-39.pyc and b/model/segment_anything_volumetric/modeling/__pycache__/__init__.cpython-39.pyc differ
 
model/segment_anything_volumetric/modeling/__pycache__/common.cpython-39.pyc CHANGED
Binary files a/model/segment_anything_volumetric/modeling/__pycache__/common.cpython-39.pyc and b/model/segment_anything_volumetric/modeling/__pycache__/common.cpython-39.pyc differ
 
model/segment_anything_volumetric/modeling/__pycache__/image_encoder.cpython-39.pyc CHANGED
Binary files a/model/segment_anything_volumetric/modeling/__pycache__/image_encoder.cpython-39.pyc and b/model/segment_anything_volumetric/modeling/__pycache__/image_encoder.cpython-39.pyc differ
 
model/segment_anything_volumetric/modeling/__pycache__/image_encoder_swin.cpython-39.pyc CHANGED
Binary files a/model/segment_anything_volumetric/modeling/__pycache__/image_encoder_swin.cpython-39.pyc and b/model/segment_anything_volumetric/modeling/__pycache__/image_encoder_swin.cpython-39.pyc differ
 
model/segment_anything_volumetric/modeling/__pycache__/mask_decoder.cpython-39.pyc CHANGED
Binary files a/model/segment_anything_volumetric/modeling/__pycache__/mask_decoder.cpython-39.pyc and b/model/segment_anything_volumetric/modeling/__pycache__/mask_decoder.cpython-39.pyc differ
 
model/segment_anything_volumetric/modeling/__pycache__/prompt_encoder.cpython-39.pyc CHANGED
Binary files a/model/segment_anything_volumetric/modeling/__pycache__/prompt_encoder.cpython-39.pyc and b/model/segment_anything_volumetric/modeling/__pycache__/prompt_encoder.cpython-39.pyc differ
 
model/segment_anything_volumetric/modeling/__pycache__/sam.cpython-39.pyc CHANGED
Binary files a/model/segment_anything_volumetric/modeling/__pycache__/sam.cpython-39.pyc and b/model/segment_anything_volumetric/modeling/__pycache__/sam.cpython-39.pyc differ
 
model/segment_anything_volumetric/modeling/__pycache__/transformer.cpython-39.pyc CHANGED
Binary files a/model/segment_anything_volumetric/modeling/__pycache__/transformer.cpython-39.pyc and b/model/segment_anything_volumetric/modeling/__pycache__/transformer.cpython-39.pyc differ
 
model/segment_anything_volumetric/utils/__pycache__/__init__.cpython-39.pyc CHANGED
Binary files a/model/segment_anything_volumetric/utils/__pycache__/__init__.cpython-39.pyc and b/model/segment_anything_volumetric/utils/__pycache__/__init__.cpython-39.pyc differ
 
model/segment_anything_volumetric/utils/__pycache__/amg.cpython-39.pyc CHANGED
Binary files a/model/segment_anything_volumetric/utils/__pycache__/amg.cpython-39.pyc and b/model/segment_anything_volumetric/utils/__pycache__/amg.cpython-39.pyc differ
 
model/segment_anything_volumetric/utils/__pycache__/transforms.cpython-39.pyc CHANGED
Binary files a/model/segment_anything_volumetric/utils/__pycache__/transforms.cpython-39.pyc and b/model/segment_anything_volumetric/utils/__pycache__/transforms.cpython-39.pyc differ
 
model/utils/__pycache__/monai_inferers_utils.cpython-39.pyc CHANGED
Binary files a/model/utils/__pycache__/monai_inferers_utils.cpython-39.pyc and b/model/utils/__pycache__/monai_inferers_utils.cpython-39.pyc differ
 
model/utils/__pycache__/visualize.cpython-39.pyc CHANGED
Binary files a/model/utils/__pycache__/visualize.cpython-39.pyc and b/model/utils/__pycache__/visualize.cpython-39.pyc differ
 
model/utils/monai_inferers_utils.py CHANGED
@@ -192,7 +192,7 @@ def sliding_window_inference(
192
  slices = dense_patch_slices(image_size, roi_size, scan_interval)
193
  num_win = len(slices) # number of windows per image
194
  total_slices = num_win * batch_size # total number of windows
195
- if total_slices > 10:
196
  return logits_global_single
197
 
198
  # Create window-level importance map
 
192
  slices = dense_patch_slices(image_size, roi_size, scan_interval)
193
  num_win = len(slices) # number of windows per image
194
  total_slices = num_win * batch_size # total number of windows
195
+ if total_slices > 10 and not st.session_state.enforce_zoom:
196
  return logits_global_single
197
 
198
  # Create window-level importance map
utils.py CHANGED
@@ -61,7 +61,7 @@ def run():
61
  if st.session_state.use_box_prompt:
62
  box_prompt = reflect_box_into_model(st.session_state.rectangle_3Dbox)
63
  inference_case.clear()
64
- st.session_state.preds_3D = inference_case(image, image_zoom_out,
65
  text_prompt=text_prompt,
66
  _point_prompt=point_prompt,
67
  _box_prompt=box_prompt)
 
61
  if st.session_state.use_box_prompt:
62
  box_prompt = reflect_box_into_model(st.session_state.rectangle_3Dbox)
63
  inference_case.clear()
64
+ st.session_state.preds_3D, st.session_state.preds_3D_ori = inference_case(image, image_zoom_out,
65
  text_prompt=text_prompt,
66
  _point_prompt=point_prompt,
67
  _box_prompt=box_prompt)