Skip to content

Commit 9cf43fc

Browse files
author
Tian-hao Zhang
committed
put training, inference, eval info into rag docs
1 parent c305e92 commit 9cf43fc

11 files changed

Lines changed: 1317 additions & 88 deletions

pytorch_connectomics

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Subproject commit 0a0dceb8bc03e0afbb843aef17181c9f854209da
1+
Subproject commit 20ccfde6f85868351b00d7b795d4cf89a251d6be

server_api/chatbot/chatbot.py

Lines changed: 75 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -23,69 +23,62 @@
2323

2424
TRAINING_AGENT_PROMPT = """You are a **Training Agent** for PyTorch Connectomics.
2525
26-
You help users set up and configure training jobs for biomedical image segmentation.
27-
28-
CRITICAL RULES:
29-
1. **Only report values that your tools return.** Do NOT invent hyperparameter values, config names, or file paths.
30-
2. **Always use tools before answering.** Call list_training_configs or read_config first — never guess.
31-
3. **Be concise.** Report the facts, generate the command, and stop.
26+
RULES:
27+
1. Only report values that your tools return. Do NOT invent config names, paths, or settings.
28+
2. Never tell the user to write a YAML from scratch. Always start from an existing config.
29+
3. If the task is unsupported, say so. PyTC only does segmentation.
30+
4. Be concise. State the facts, generate the command, stop.
3231
33-
Tools:
34-
- list_training_configs: List available config files with descriptions
35-
- read_config: Read a config file to see its hyperparameters
32+
WORKFLOW: The available configs are provided in your task message. Pick the best match, then:
33+
1. Call read_config on the chosen config path to see its YAML overrides.
34+
2. For common parameters (learning rate, batch size, iterations, optimizer, checkpoint interval), ALWAYS use the keys listed below. DO NOT search for these.
35+
3. For specialized parameters (augmentation settings, loss functions, architecture details), call search_documentation.
36+
4. Build the command with overrides using the SECTION.KEY=value format.
3637
37-
Workflow:
38-
1. Use list_training_configs to find configs matching user's task
39-
2. Use read_config to examine the config's current settings
40-
3. Compare user requirements with config defaults
41-
4. Generate the training command with appropriate overrides
38+
IMPORTANT: YAML configs only show overrides — many valid keys exist in the defaults but are not shown in read_config output.
4239
43-
Command Format:
44-
```
45-
python scripts/main.py --config <config_path> [OVERRIDES]
46-
```
40+
Common override keys (ALWAYS use these exact keys, never search for alternatives):
41+
- SOLVER.BASE_LR, SOLVER.SAMPLES_PER_BATCH, SOLVER.ITERATION_TOTAL
42+
- SOLVER.ITERATION_SAVE (checkpoint save interval), SOLVER.ITERATION_STEP (LR decay steps)
43+
- SOLVER.NAME (values: SGD, Adam, AdamW)
44+
- SOLVER.LR_SCHEDULER_NAME (values: WarmupMultiStepLR, WarmupCosineLR)
45+
- SOLVER.CLIP_GRADIENTS.ENABLED (True/False), SOLVER.CLIP_GRADIENTS.CLIP_VALUE
46+
- MODEL.ARCHITECTURE, MODEL.BLOCK_TYPE, MODEL.FILTERS
4747
48-
Overrides use YAML key paths appended to the command: SECTION.KEY=value
49-
Example:
50-
```
51-
python scripts/main.py --config configs/Lucchi-Mitochondria.yaml SOLVER.BASE_LR=0.001 SOLVER.SAMPLES_PER_BATCH=16
52-
```
53-
Use read_config output to determine the correct key paths for any parameter.
48+
NEVER invent keys like TRAIN.MAX_ITER, TRAINING.BATCH_SIZE, or CLI flags like --batch-size, --checkpoint-interval — these do not exist.
5449
55-
Always generate commands for the user to run - never execute directly."""
50+
Command format: `python scripts/main.py --config-file <path> [SECTION.KEY=value ...]`
51+
Always generate commands for the user to run — never execute directly."""
5652

5753

5854
INFERENCE_AGENT_PROMPT = """You are an **Inference Agent** for PyTorch Connectomics.
5955
60-
You help users run inference and evaluation with trained segmentation models.
61-
62-
CRITICAL RULES:
63-
1. **Only report values that your tools return.** Do NOT invent checkpoint paths, config names, or settings.
64-
2. **Always use tools before answering.** Call list_checkpoints or read_config first — never guess.
65-
3. **Be concise.** Report the facts, generate the command, and stop.
56+
RULES:
57+
1. Only report values that your tools return. Do NOT invent checkpoint paths, config names, or settings.
58+
2. Be concise. State the facts, generate the command, stop.
6659
67-
Tools:
68-
- list_checkpoints: Find available trained model checkpoints
69-
- read_config: Read config to find default inference settings
60+
WORKFLOW:
61+
1. If the user did NOT provide a checkpoint path, call list_checkpoints first to see available checkpoints.
62+
2. If the user DID provide a checkpoint path (e.g., outputs/model/checkpoint.pth.tar), skip list_checkpoints.
63+
3. Call read_config to see the INFERENCE section keys.
64+
4. For specialized inference parameters, call search_documentation if needed.
7065
71-
Workflow:
72-
1. Use list_checkpoints to find available models
73-
2. Use read_config to check inference settings (INFERENCE section)
74-
3. Generate the inference command
66+
Here is the correct override key mapping (use these exact keys):
67+
- Output path → INFERENCE.OUTPUT_PATH
68+
- TTA augmentation count → INFERENCE.AUG_NUM
69+
- TTA mode → INFERENCE.AUG_MODE (values: mean, max)
70+
- Blending → INFERENCE.BLENDING (values: gaussian, bump)
71+
- Stride → INFERENCE.STRIDE
72+
- Process volumes one at a time → INFERENCE.DO_SINGLY
73+
- Batch size → INFERENCE.SAMPLES_PER_BATCH
7574
76-
Command Format:
77-
```
78-
python scripts/main.py --config <config_path> --checkpoint <checkpoint_path> --inference [OVERRIDES]
79-
```
75+
Command format: `python scripts/main.py --config-file <path> --inference --checkpoint <ckpt> [SECTION.KEY=value ...]`
8076
81-
Overrides use YAML key paths appended to the command: SECTION.KEY=value
82-
Example:
83-
```
84-
python scripts/main.py --config configs/Lucchi-Mitochondria.yaml --checkpoint outputs/Lucchi/checkpoint_100000.pth --inference INFERENCE.OUTPUT_PATH=/path/to/output
85-
```
86-
Use read_config output to determine the correct key paths for any parameter.
77+
IMPORTANT: Overrides use SECTION.KEY=value format (NO -- prefix). Example:
78+
✅ CORRECT: INFERENCE.AUG_NUM=8
79+
❌ WRONG: --inference.AUG_NUM=8
8780
88-
Always generate commands for the user to run - never execute directly."""
81+
Always generate commands for the user to run never execute directly."""
8982

9083

9184
SUPERVISOR_PROMPT = """You are the **Supervisor Agent** for PyTorch Connectomics (PyTC Client).
@@ -94,6 +87,7 @@
9487
9588
ROUTING — decide which tool to use BEFORE calling anything:
9689
- **UI, navigation, features, shortcuts, workflows, "how do I..." questions** → search_documentation
90+
- **General PyTC questions** (what architectures are supported, what augmentations exist, what loss functions are available, etc.) → search_documentation
9791
- **Generate a specific training/inference command** → delegate_to_training_agent or delegate_to_inference_agent
9892
- **General/greeting/off-topic** → answer directly, no tool needed
9993
@@ -103,18 +97,19 @@
10397
3. **For application questions, ground answers in retrieved documentation.** Call search_documentation and base your answer on the returned text. Do NOT invent features, shortcuts, buttons, or workflows.
10498
4. **Do not fabricate specifics.** Never make up keyboard shortcuts, button labels, or step-by-step instructions unless they come from retrieved docs or a sub-agent response.
10599
4a. **NEVER use command-line instructions for UI questions.** The PyTC Client is a desktop GUI application. If the user asks how to do something, explain the UI workflow (buttons, tabs, forms) from the documentation. Do NOT provide Python scripts, bash commands, or CLI examples unless the sub-agent explicitly generates them.
100+
4b. **NEVER fabricate file paths or scripts.** Do NOT invent scripts like `scripts/evaluate.py`, `scripts/resume_training.py`, or any other files that don't exist. If evaluation requires Python code, show inline code using `connectomics.utils.evaluate`, not fake script paths.
106101
5. **Answer every part of the user's question.** If they ask about two things, address both.
107102
6. **Use retrieved content even if wording differs.** If the documentation describes relevant features or workflows, use that information to answer the question. Don't claim something isn't documented just because it uses different terminology than the user's question.
108-
7. **HARD LIMIT: You may call search_documentation EXACTLY 2 times per user question.** After the second call, you MUST answer with the information already retrieved. Do NOT attempt a third search. If the tool returns "Search limit reached", immediately stop and answer based on what you already have.
103+
7. **HARD LIMIT: You may call search_documentation at most 3 times yourself.** Sub-agents also have access to search_documentation. If the tool returns "Search limit reached", immediately stop and answer based on what you already have.
109104
110105
Sub-agents:
111106
- **Training Agent**: Config selection, training job setup, hyperparameter overrides
112107
- **Inference Agent**: Checkpoint management, inference/evaluation commands
113108
114109
Tools:
115-
- search_documentation: Search PyTC docs for UI guides and feature explanations. Use ONLY for questions about the application interface, pages, buttons, or workflows.
116-
- delegate_to_training_agent: Send training-related tasks to training agent
117-
- delegate_to_inference_agent: Send inference-related tasks to inference agent"""
110+
- search_documentation: Search PyTC docs for UI guides, feature explanations, training/inference config references, model architectures, augmentation options, and bundled configs.
111+
- delegate_to_training_agent: Send training-related tasks to training agent (config selection, command generation, hyperparameter tuning)
112+
- delegate_to_inference_agent: Send inference-related tasks to inference agent (checkpoint listing, inference commands, evaluation setup)"""
118113

119114

120115
def build_chain():
@@ -152,23 +147,11 @@ def build_chain():
152147
def reset_search_counter():
153148
_search_call_count[0] = 0
154149

155-
training_agent = create_agent(
156-
model=llm,
157-
tools=[list_training_configs, read_config],
158-
system_prompt=TRAINING_AGENT_PROMPT,
159-
)
160-
161-
inference_agent = create_agent(
162-
model=llm,
163-
tools=[list_checkpoints, read_config],
164-
system_prompt=INFERENCE_AGENT_PROMPT,
165-
)
166-
167150
@tool
168151
def search_documentation(query: str) -> str:
169152
"""
170-
Search PyTC documentation for how-to guides, UI explanations, and feature descriptions.
171-
Use this for questions about the application interface or general usage.
153+
Search PyTC documentation for UI guides, feature descriptions, training/inference
154+
configuration references, model architectures, augmentation options, and bundled configs.
172155
173156
Args:
174157
query: The user's question
@@ -180,8 +163,8 @@ def search_documentation(query: str) -> str:
180163
print(
181164
f"[TOOL] search_documentation(query={query!r}) [call {_search_call_count[0]}]"
182165
)
183-
if _search_call_count[0] > 2:
184-
print("[TOOL] search limit reached (max 2 per question)")
166+
if _search_call_count[0] > 6:
167+
print("[TOOL] search limit reached (max 6 per question)")
185168
return "Search limit reached. Please answer based on the documentation already retrieved."
186169

187170
# Primary: FAISS semantic search (chunked embeddings)
@@ -213,6 +196,18 @@ def search_documentation(query: str) -> str:
213196
print("[TOOL] search_documentation → no results")
214197
return "No relevant documentation found."
215198

199+
training_agent = create_agent(
200+
model=llm,
201+
tools=[list_training_configs, read_config, search_documentation],
202+
system_prompt=TRAINING_AGENT_PROMPT,
203+
)
204+
205+
inference_agent = create_agent(
206+
model=llm,
207+
tools=[list_checkpoints, read_config, search_documentation],
208+
system_prompt=INFERENCE_AGENT_PROMPT,
209+
)
210+
216211
@tool
217212
def delegate_to_training_agent(task: str) -> str:
218213
"""
@@ -226,8 +221,18 @@ def delegate_to_training_agent(task: str) -> str:
226221
Response from the training agent
227222
"""
228223
print(f"[TOOL] delegate_to_training_agent(task={task!r})")
224+
# Auto-inject available configs so the agent doesn't need to call list_training_configs
225+
configs = list_training_configs.invoke({})
226+
config_summary = "\n".join(
227+
f"- {c['name']} ({c['model']}) → {c['path']}" for c in configs if isinstance(c, dict) and 'name' in c
228+
)
229+
enriched_task = (
230+
f"{task}\n\n"
231+
f"AVAILABLE CONFIGS (already fetched for you):\n{config_summary}\n\n"
232+
f"Pick the best match and call read_config on its path to see the exact YAML keys before generating the command."
233+
)
229234
result = training_agent.invoke(
230-
{"messages": [{"role": "user", "content": task}]}
235+
{"messages": [{"role": "user", "content": enriched_task}]}
231236
)
232237
messages = result.get("messages", [])
233238
response = (
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
# PyTC Data Augmentation
2+
3+
This document describes all data augmentation options available in PyTorch Connectomics. These augmentations are specifically designed for electron microscopy (EM) and biomedical volumetric data.
4+
5+
All augmentations are controlled under the `AUGMENTOR` section of the YAML config. Set `AUGMENTOR.ENABLED: False` to disable all augmentation.
6+
7+
## Available Augmentations
8+
9+
### Rotation (`AUGMENTOR.ROTATE`)
10+
Applies random 90-degree rotations.
11+
12+
| Key | Default | Description |
13+
|-----|---------|-------------|
14+
| `ENABLED` | `True` | Enable rotation |
15+
| `ROT90` | `True` | Restrict to 90° increments |
16+
| `P` | `1.0` | Probability of applying |
17+
18+
### Rescale (`AUGMENTOR.RESCALE`)
19+
Randomly rescales the volume.
20+
21+
| Key | Default | Description |
22+
|-----|---------|-------------|
23+
| `ENABLED` | `True` | Enable rescaling |
24+
| `FIX_ASPECT` | `False` | Keep aspect ratio fixed |
25+
| `P` | `0.5` | Probability |
26+
27+
### Flip (`AUGMENTOR.FLIP`)
28+
Randomly flips along axes. For isotropic data, enable z-axis flips.
29+
30+
| Key | Default | Description |
31+
|-----|---------|-------------|
32+
| `ENABLED` | `True` | Enable flipping |
33+
| `DO_ZTRANS` | `0` | Set to `1` to enable x-z and y-z flips (for isotropic cubic data) |
34+
| `P` | `1.0` | Probability |
35+
36+
### Elastic Deformation (`AUGMENTOR.ELASTIC`)
37+
Applies smooth elastic deformation to simulate tissue warping.
38+
39+
| Key | Default | Description |
40+
|-----|---------|-------------|
41+
| `ENABLED` | `True` | Enable elastic deformation |
42+
| `ALPHA` | `16.0` | Maximum pixel displacement |
43+
| `SIGMA` | `4.0` | Gaussian filter standard deviation |
44+
| `P` | `0.75` | Probability |
45+
46+
### Grayscale Augmentation (`AUGMENTOR.GRAYSCALE`)
47+
Randomly adjusts brightness and contrast.
48+
49+
| Key | Default | Description |
50+
|-----|---------|-------------|
51+
| `ENABLED` | `True` | Enable grayscale augmentation |
52+
| `P` | `0.75` | Probability |
53+
54+
### Missing Parts (`AUGMENTOR.MISSINGPARTS`)
55+
Simulates missing tissue regions (common artifact in EM data).
56+
57+
| Key | Default | Description |
58+
|-----|---------|-------------|
59+
| `ENABLED` | `True` | Enable missing parts simulation |
60+
| `ITER` | `64` | Number of missing region iterations |
61+
| `P` | `0.9` | Probability |
62+
63+
### Missing Section (`AUGMENTOR.MISSINGSECTION`)
64+
Simulates entirely missing z-slices (another common EM artifact).
65+
66+
| Key | Default | Description |
67+
|-----|---------|-------------|
68+
| `ENABLED` | `True` | Enable missing section simulation |
69+
| `NUM_SECTION` | `2` | Number of sections to remove |
70+
| `P` | `0.5` | Probability |
71+
72+
### Misalignment (`AUGMENTOR.MISALIGNMENT`)
73+
Simulates section-to-section misalignment (shift or rotation between z-slices).
74+
75+
| Key | Default | Description |
76+
|-----|---------|-------------|
77+
| `ENABLED` | `True` | Enable misalignment simulation |
78+
| `DISPLACEMENT` | `16` | Maximum pixel displacement |
79+
| `ROTATE_RATIO` | `0.5` | Fraction of misalignments that use rotation instead of translation |
80+
| `P` | `0.5` | Probability |
81+
82+
### Motion Blur (`AUGMENTOR.MOTIONBLUR`)
83+
Simulates motion blur artifacts in EM sections.
84+
85+
| Key | Default | Description |
86+
|-----|---------|-------------|
87+
| `ENABLED` | `True` | Enable motion blur |
88+
| `SECTIONS` | `2` | Number of sections to blur |
89+
| `KERNEL_SIZE` | `11` | Blur kernel size |
90+
| `P` | `0.5` | Probability |
91+
92+
### CutBlur (`AUGMENTOR.CUTBLUR`)
93+
Replaces a rectangular region with a downsampled-then-upsampled version, simulating resolution variation.
94+
95+
| Key | Default | Description |
96+
|-----|---------|-------------|
97+
| `ENABLED` | `True` | Enable CutBlur |
98+
| `LENGTH_RATIO` | `0.4` | Ratio of region size to volume size |
99+
| `DOWN_RATIO_MIN` | `2.0` | Minimum downsampling factor |
100+
| `DOWN_RATIO_MAX` | `8.0` | Maximum downsampling factor |
101+
| `DOWNSAMPLE_Z` | `False` | Also downsample along z-axis |
102+
| `P` | `0.5` | Probability |
103+
104+
### CutNoise (`AUGMENTOR.CUTNOISE`)
105+
Adds noise to a rectangular region.
106+
107+
| Key | Default | Description |
108+
|-----|---------|-------------|
109+
| `ENABLED` | `True` | Enable CutNoise |
110+
| `LENGTH_RATIO` | `0.4` | Ratio of region size |
111+
| `SCALE` | `0.3` | Noise scale |
112+
| `P` | `0.75` | Probability |
113+
114+
### CopyPaste (`AUGMENTOR.COPYPASTE`)
115+
Copy-pastes object instances for data augmentation (disabled by default).
116+
117+
| Key | Default | Description |
118+
|-----|---------|-------------|
119+
| `ENABLED` | `False` | Enable CopyPaste (disabled by default) |
120+
| `AUG_THRES` | `0.7` | Augmentation threshold |
121+
| `P` | `0.8` | Probability |
122+
123+
## Global Augmentation Options
124+
125+
| Key | Default | Description |
126+
|-----|---------|-------------|
127+
| `AUGMENTOR.ENABLED` | `True` | Master switch for all augmentations |
128+
| `AUGMENTOR.SMOOTH` | `False` | Apply Gaussian smoothing to label masks after augmentation. WARNING: can erase thin structures. |
129+
| `AUGMENTOR.ADDITIONAL_TARGETS_NAME` | `['label']` | Names of additional targets to augment alongside the image |
130+
| `AUGMENTOR.ADDITIONAL_TARGETS_TYPE` | `['mask']` | Type of each additional target (`'mask'` uses nearest-neighbor interpolation) |
131+
132+
## Recommended Settings by Data Type
133+
134+
### Anisotropic EM data (e.g., SNEMI, CREMI)
135+
- `AUGMENTOR.FLIP.DO_ZTRANS: 0` (do not flip across z)
136+
- All other augmentations enabled by default
137+
- Consider disabling `CUTNOISE` for clean datasets
138+
139+
### Isotropic EM data (e.g., Lucchi)
140+
- `AUGMENTOR.FLIP.DO_ZTRANS: 1` (enable z-axis flips)
141+
- `AUGMENTOR.CUTBLUR.DOWNSAMPLE_Z: True`
142+
143+
### 2D datasets (e.g., Cellpose)
144+
- Disable EM-specific augmentations: `ELASTIC.ENABLED: False`, `RESCALE.ENABLED: False`, `MISSINGPARTS.ENABLED: False`
145+
- Keep flip, rotation, and grayscale augmentations
146+
147+
### Sparse labels (e.g., Scutoid, NucMM)
148+
- Enable reject sampling: `DATASET.REJECT_SAMPLING.SIZE_THRES: 1000`
149+
- Consider disabling `CUTNOISE` to avoid corrupting sparse regions
150+
151+
## Per-Augmentation Skipping
152+
153+
Each augmentation has a `SKIP` parameter (list of sample keys to skip). This allows skipping certain augmentations for specific data channels. Default is an empty list (no skipping).

0 commit comments

Comments
 (0)