Generate Brain MRI Images with Cognitive Actions: A Developer's Guide

In the realm of medical imaging, the ability to generate high-quality brain MRI images is invaluable for research and clinical applications. The jschoormans/brain-mri Cognitive Actions provide a powerful toolset for generating 2D axial brain MRI images. These images are fine-tuned using advanced techniques on a dataset of 100 MRIs with various sequences. In this article, we’ll explore how to leverage this Cognitive Action to create brain MRI images that can enhance your applications.
Prerequisites
Before diving into the usage of the Cognitive Actions, ensure you have the following:
- An API key for the Cognitive Actions platform to authenticate your requests.
- Basic knowledge of JSON formatting and HTTP requests.
Authentication typically involves passing your API key in the headers of your HTTP requests to access the Cognitive Actions services.
Cognitive Actions Overview
Generate Brain MRI Image
The Generate Brain MRI Image action creates 2D axial brain MRI images based on a set of customizable parameters. This action falls under the medical-image-analysis category.
Input
The input for this action is structured as follows:
- mask (string, optional): A URI for an input mask used in inpaint mode. Black areas will be preserved, while white areas will be inpainted.
- seed (integer, optional): A random seed used for generating unique results. If left blank, a random seed value will be used.
- image (string, optional): A URI for the input image used in img2img or inpaint mode.
- width (integer, default: 1024): The width of the output image in pixels.
- height (integer, default: 1024): The height of the output image in pixels.
- prompt (string, default: "An astronaut riding a rainbow unicorn"): Text input to guide the image generation process.
- loraScale (number, default: 0.6): Adjusts the LoRA additive scale, applicable only to trained models.
- guidanceScale (number, default: 7.5): Adjusts the scale for classifier-free guidance.
- schedulerType (string, default: "K_EULER"): Defines the type of scheduler to be used.
- promptStrength (number, default: 0.8): Strength of the prompt when using img2img or inpaint mode.
- refinementSteps (integer, optional): Number of steps to refine the image.
- refinementStyle (string, default: "no_refiner"): Selects the refinement style to use.
- includeWatermark (boolean, default: true): Whether to apply a watermark to the generated images.
- outputImageCount (integer, default: 1): Indicates the number of images to generate (1 to 4).
- highNoiseFraction (number, default: 0.8): Fraction of noise used by the expert ensemble refiner.
- inferenceStepsCount (integer, default: 50): Number of steps for image denoising.
- negativeInputPrompt (string, optional): Guides against specific content in the generated image.
- safetyCheckerDisabled (boolean, default: false): Enables bypassing the safety checker for generated images.
Example Input:
{
"width": 1024,
"height": 1024,
"prompt": "a TOK",
"loraScale": 0.6,
"guidanceScale": 7.5,
"schedulerType": "K_EULER",
"promptStrength": 0.8,
"refinementStyle": "no_refiner",
"includeWatermark": true,
"outputImageCount": 1,
"highNoiseFraction": 0.8,
"inferenceStepsCount": 50
}
Output
The action typically returns a list of URLs pointing to the generated images.
Example Output:
[
"https://assets.cognitiveactions.com/invocations/51fc1550-07ef-4a9a-9007-764669610279/3b8d0819-60af-420a-ab7d-0f3687bc3bb2.png"
]
Conceptual Usage Example (Python)
Here’s a conceptual example of how to call the Generate Brain MRI Image action using Python:
import requests
import json
# Replace with your Cognitive Actions API key and endpoint
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute" # Hypothetical endpoint
action_id = "e2a6e46e-abb4-4dc1-b35d-444316d279a0" # Action ID for Generate Brain MRI Image
# Construct the input payload based on the action's requirements
payload = {
"width": 1024,
"height": 1024,
"prompt": "a TOK",
"loraScale": 0.6,
"guidanceScale": 7.5,
"schedulerType": "K_EULER",
"promptStrength": 0.8,
"refinementStyle": "no_refiner",
"includeWatermark": True,
"outputImageCount": 1,
"highNoiseFraction": 0.8,
"inferenceStepsCount": 50
}
headers = {
"Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
"Content-Type": "application/json"
}
try:
response = requests.post(
COGNITIVE_ACTIONS_EXECUTE_URL,
headers=headers,
json={"action_id": action_id, "inputs": payload} # Hypothetical structure
)
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
result = response.json()
print("Action executed successfully:")
print(json.dumps(result, indent=2))
except requests.exceptions.RequestException as e:
print(f"Error executing action {action_id}: {e}")
if e.response is not None:
print(f"Response status: {e.response.status_code}")
try:
print(f"Response body: {e.response.json()}")
except json.JSONDecodeError:
print(f"Response body: {e.response.text}")
In this code snippet, replace YOUR_COGNITIVE_ACTIONS_API_KEY with your actual API key. The action ID and input payload are structured as required for the request. The endpoint URL and request structure are illustrative and may vary based on the actual API specifications.
Conclusion
The jschoormans/brain-mri Cognitive Actions provide a comprehensive solution for generating high-quality brain MRI images. By leveraging these actions, developers can create innovative applications in medical imaging and research. Next steps could include experimenting with different input parameters or integrating this functionality into a larger application workflow. Happy coding!