Instructions to use BiomedSyn/ours with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use BiomedSyn/ours with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("BiomedSyn/ours", 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
File size: 5,074 Bytes
75b1a45 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | {
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from PIL import Image\n",
"import numpy as np\n",
"import torch\n",
"from diffusers import (\n",
" StableDiffusionInstructPix2PixPipeline,\n",
" UNet2DConditionModel,\n",
" ControlNetModel,\n",
" StableDiffusionControlNetPipeline,\n",
" UniPCMultistepScheduler,\n",
")\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from PIL import Image\n",
"import numpy as np\n",
"from PIL import Image\n",
"from scipy.ndimage import binary_erosion, binary_dilation\n",
"from skimage.morphology import disk\n",
"\n",
"\n",
"def find_region(generated_image, erosion_dilation_radius=5):\n",
" red_channel = generated_image[:, :, 0] \n",
" green_channel = generated_image[:, :, 1] \n",
" blue_channel = generated_image[:, :, 2] \n",
" red_region = (red_channel > 100) & (green_channel < 80) & (blue_channel < 80)\n",
" selem = disk(erosion_dilation_radius) \n",
" mask = binary_erosion(red_region, structure=selem).astype(np.uint8) \n",
" mask = binary_dilation(mask, structure=selem).astype(np.uint8)\n",
" return mask\n",
"\n",
"def post_process(generated_image, organ, disease):\n",
" generated_image = np.array(generated_image)\n",
" mask = find_region(generated_image)\n",
" color_map = {\n",
" \"Atelectasis\": (255, 0, 0), \n",
" \"Calcification\": (0, 255, 0), \n",
" \"Cardiomegaly\": (0, 0, 255), \n",
" \"Consolidation\": (255, 255, 0), \n",
" \"Diffuse Nodule\": (255, 165, 0), \n",
" \"Effusion\": (0, 255, 255), \n",
" \"Emphysema\": (255, 0, 255), \n",
" \"Fibrosis\": (128, 0, 128), \n",
" \"Fracture\": (255, 192, 203), \n",
" \"Mass\": (173, 255, 47), \n",
" \"Nodule\": (0, 128, 255), \n",
" \"Pleural Thickening\": (75, 0, 130), \n",
" \"Pneumothorax\": (255, 105, 180) \n",
" }\n",
" organ_np = np.array(organ)\n",
" color = color_map.get(disease, [0, 0, 0]) \n",
" organ_np[mask == 1] = color\n",
" return Image.fromarray(organ_np)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Load models"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t2l_pipeline = StableDiffusionInstructPix2PixPipeline.from_pretrained('CompVis/stable-diffusion-v1-4')\n",
"t2l_pipeline.unet = UNet2DConditionModel.from_pretrained('/home/AURA/text2layout/instruct-pix2pix-model/checkpoint-7000/', subfolder=\"unet\")\n",
"t2l_pipeline.safety_checker = None\n",
"t2l_pipeline.to(device)\n",
"\n",
"controlnet = ControlNetModel.from_pretrained('/home/AURA/layout2image_multi/controlnet-model_multi/checkpoint-7000/controlnet', use_safetensors=True)\n",
"l2i_pipeline = StableDiffusionControlNetPipeline.from_pretrained('/home/AURA/roentgen/', controlnet=controlnet)\n",
"l2i_pipeline.scheduler = UniPCMultistepScheduler.from_config(l2i_pipeline .scheduler.config)\n",
"l2i_pipeline.safety_checker = None\n",
"l2i_pipeline.to(device)\n",
"\n",
"print('Loaded models')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"organ_mask_path = '/home/AURA/test/41130/organ_41130.png'\n",
"organ = Image.open(organ_mask_path).convert(\"RGB\")\n",
"organ.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"prompt = \"mild Atelectasis on right upper lung\"\n",
"disease = prompt.split()[1] # disease = \"Atelectasis\"\n",
"generated_mask = t2l_pipeline(prompt, organ, num_inference_steps=20, guidance_scale=7).images[0]\n",
"overlap = post_process(generated_mask, organ, disease)\n",
"overlap.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"generated_image = l2i_pipeline(prompt, overlap, num_inference_steps=70, guidance_scale=8).images[0]\n",
"generated_image.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.15"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|