Instructions to use mlii0117/sd1.5_MPECT with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use mlii0117/sd1.5_MPECT with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("mlii0117/sd1.5_MPECT", dtype=torch.bfloat16, device_map="cuda") prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] - Notebooks
- Google Colab
- Kaggle
Update README.md
Browse files
README.md
CHANGED
|
@@ -74,38 +74,63 @@ pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(
|
|
| 74 |
).to("cuda")
|
| 75 |
generator = torch.Generator("cuda").manual_seed(0)
|
| 76 |
|
| 77 |
-
|
|
|
|
| 78 |
|
|
|
|
| 79 |
def load_dicom_folder(dicom_folder):
|
| 80 |
dicom_folder = os.path.join(dicom_folder, 'DICOM')
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
```
|
| 110 |
|
| 111 |
---
|
|
|
|
| 74 |
).to("cuda")
|
| 75 |
generator = torch.Generator("cuda").manual_seed(0)
|
| 76 |
|
| 77 |
+
prompt_art = "Convert this non-contrast CT slice to mimic an arterial-phase contrast-enhanced CT. Brighten and enhance the aorta, major arteries, and adjacent organ boundaries to emphasize arterial flow, focusing on clarity and contrast in these areas while maintaining other features unchanged."
|
| 78 |
+
prompt_ven = "Convert this non-contrast CT slice to mimic a venous-phase contrast-enhanced CT. Brighten and enhance the veins, especially the portal and hepatic veins, and emphasize organ boundaries to mimic venous flow, focusing on brightness and contrast in these areas while maintaining other features unchanged."
|
| 79 |
|
| 80 |
+
# read all dicoms
|
| 81 |
def load_dicom_folder(dicom_folder):
|
| 82 |
dicom_folder = os.path.join(dicom_folder, 'DICOM')
|
| 83 |
+
dicom_files = sorted(glob(os.path.join(dicom_folder, "*")))
|
| 84 |
+
|
| 85 |
+
slices = []
|
| 86 |
+
for dicom_file in dicom_files:
|
| 87 |
+
ds = dcmread(dicom_file)
|
| 88 |
+
slices.append(ds.pixel_array.astype(np.float32))
|
| 89 |
+
|
| 90 |
+
dicom_array = np.stack(slices, axis=0)
|
| 91 |
+
dicom_array += ds.RescaleIntercept
|
| 92 |
+
dicom_array = np.clip(dicom_array, -1000, 1000)
|
| 93 |
+
dicom_array = (dicom_array + 1000) / 2000.0
|
| 94 |
+
return dicom_array
|
| 95 |
+
|
| 96 |
+
# transfer to RGB and send to diffusion
|
| 97 |
+
def process_slices(dicom_array):
|
| 98 |
+
outputs = []
|
| 99 |
+
for i in tqdm(range(dicom_array.shape[0])):
|
| 100 |
+
slice_img = (dicom_array[i] * 255).astype(np.uint8)
|
| 101 |
+
pil_img = Image.fromarray(slice_img).convert("RGB")
|
| 102 |
+
|
| 103 |
+
edited_image = pipe(
|
| 104 |
+
prompt, #### chose prompt_art or prompt_ven
|
| 105 |
+
image=pil_img,
|
| 106 |
+
num_inference_steps=20,
|
| 107 |
+
image_guidance_scale=1.5,
|
| 108 |
+
guidance_scale=10,
|
| 109 |
+
generator=generator,
|
| 110 |
+
).images[0]
|
| 111 |
+
|
| 112 |
+
gray = edited_image.convert("L")
|
| 113 |
+
gray_np = np.array(gray).astype(np.float32) / 255.0
|
| 114 |
+
gray_np = gray_np * 2000 - 1000 # scale back to [-1000, 1000]
|
| 115 |
+
outputs.append(gray_np)
|
| 116 |
+
|
| 117 |
+
volume = np.stack(outputs, axis=0)
|
| 118 |
+
return volume
|
| 119 |
+
|
| 120 |
+
# save to nii.gz
|
| 121 |
+
def save_nifti(volume, output_path):
|
| 122 |
+
affine = np.eye(4)
|
| 123 |
+
nii = nib.Nifti1Image(volume, affine)
|
| 124 |
+
nib.save(nii, output_path)
|
| 125 |
+
|
| 126 |
+
# main function
|
| 127 |
+
def main(input_dicom_path, output_nifti_path):
|
| 128 |
+
dicom_array = load_dicom_folder(input_dicom_path)
|
| 129 |
+
edited_volume = process_slices(dicom_array)
|
| 130 |
+
save_nifti(edited_volume, output_nifti_path)
|
| 131 |
+
|
| 132 |
+
# DMEO
|
| 133 |
+
# main("/path/to/dicom_folder", "/path/to/output.nii.gz")
|
| 134 |
```
|
| 135 |
|
| 136 |
---
|