a, b, Example images of GCaMP6s expression in VGluT2+ cells (green), with schematic showing GRIN lens placement in the dPAG (a) and dmSC (b). c, d, Raster plots showing active (colour squares) and non-active cells (black squares) in a single FOV imaged over multiple trials. A total of 8 FOVs were imaged in the dPAG (c) with a mean of 18 cells per FOV (range = 7–30) and 11 trials per FOV; and in the dmSC (d), 11 FOVs were imaged with a mean of 20 cells per FOV (range = 7–31) and 20 trials per FOV. There was a mean of 7 escape-responding cells per dPAG FOV and 16 escape-responding cells per dmSC FOV. e, f, Reliability of escape-responding cells showing a response over multiple trials for all trials (left) and for the first and second half of trials separately (right). Mean reliability across all trials was 28 ± 3% for dPAG and 35 ± 3% for dmSC, and stable over multiple trials (P = 0.44 for dPAG, P = 0.11 for dmSC, comparison between the two groups of trials, U-test). g, h, Fraction of all cells in a FOV that were active on each trial for all trials (left) and for the first and second half of trials separately (right). The active fraction across all trials was 14 ± 3% for dPAG and 23 ± 6% for dmSC, and stable over multiple trials (P = 0.21 for dPAG, P = 0.08 for dmSC, comparison between the two groups of trials, U-test). i, Correlation between the rise slope of the population activity and escape latency (n = 75 trials, P = 0.0048, Pearson’s r). j, Average population calcium signal in the dmSC for escape trials in response to 98% contrast spots and sound stimuli. The slope of the signal rise is steeper for sound-evoked escape. k, Left, ROC AUC for the dmSC signal before spontaneous escape onset after conditioning (AUC at escape onset = 0.74, significantly above chance 2.1 s before escape, n = 57 trials). Right, average population calcium signal in the dmSC during threat-evoked escape trials where the mouse was already facing the shelter and therefore did not rotate the head (n = 5 trials). l, Summary quantification of dPAG population calcium signals during threat-evoked escape and spontaneous foraging running bouts of similar speed (top; n = 6 escape trials and n = 6 running bouts, speed not significantly different, P = 0.64, t-test), showing that activity increase in the dPAG is specific for escape (bottom; P = 0.0018, t-test). Shaded areas show s.e.m., box-and-whisker plots show median, IQR and range. m, Correlation between the population activity of dPAG (top; n = 39 trials, P = 6.7 × 10−7, Pearson’s r) and dmSC (bottom; n = 64 trials, P = 0.04, Pearson’s r) and escape speed. Each data point is a single trial. n, Placement of GRIN lenses in the dmSC (magenta circles) and dPAG (blue circles), coordinates are in mm and from bregma. Mouse brain images adapted from ref. 46 and reproduced with permission from Elsevier. Box-and-whisker plots show median, IQR and range.