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
|
| 2 |
+
"""
|
| 3 |
+
precompute_teacher.py
|
| 4 |
+
Pre-computes DLoc teacher outputs for all training samples and saves them.
|
| 5 |
+
Run this ONCE before distillation — eliminates teacher forward pass during training.
|
| 6 |
+
|
| 7 |
+
Usage:
|
| 8 |
+
python precompute_teacher.py
|
| 9 |
+
|
| 10 |
+
Output:
|
| 11 |
+
./data/teacher_train_outputs.pt (~2.3 GB for 17K samples at [1,161,361])
|
| 12 |
+
./data/teacher_test_outputs.pt
|
| 13 |
+
"""
|
| 14 |
+
import os
|
| 15 |
+
import time
|
| 16 |
+
import torch
|
| 17 |
+
import numpy as np
|
| 18 |
+
import warnings
|
| 19 |
+
with warnings.catch_warnings():
|
| 20 |
+
warnings.filterwarnings("ignore", category=FutureWarning)
|
| 21 |
+
|
| 22 |
+
from utils import *
|
| 23 |
+
from data_loader import load_data
|
| 24 |
+
from modelADT import ModelADT
|
| 25 |
+
from joint_model import Enc_2Dec_Network
|
| 26 |
+
|
| 27 |
+
# ============================================================
|
| 28 |
+
# CONFIG
|
| 29 |
+
# ============================================================
|
| 30 |
+
TEACHER_RUN_DIR = './weights'
|
| 31 |
+
TEACHER_EPOCH = 'best'
|
| 32 |
+
BATCH_SIZE = 64
|
| 33 |
+
|
| 34 |
+
trainpath = [
|
| 35 |
+
'./data/dataset_jacobs_July28.mat',
|
| 36 |
+
'./data/dataset_non_fov_train_jacobs_July28_2.mat',
|
| 37 |
+
'./data/dataset_fov_train_jacobs_July28_2.mat',
|
| 38 |
+
]
|
| 39 |
+
testpath = [
|
| 40 |
+
'./data/dataset_fov_test_jacobs_July28_2.mat',
|
| 41 |
+
'./data/dataset_non_fov_test_jacobs_July28_2.mat',
|
| 42 |
+
]
|
| 43 |
+
|
| 44 |
+
# ============================================================
|
| 45 |
+
# LOAD DATA
|
| 46 |
+
# ============================================================
|
| 47 |
+
device = torch.device('cuda:0')
|
| 48 |
+
|
| 49 |
+
print('Loading training data...')
|
| 50 |
+
train_wo, train_w, train_lbl = [], [], []
|
| 51 |
+
for fp in trainpath:
|
| 52 |
+
wo, w, lbl = load_data(fp)
|
| 53 |
+
if lbl.dim() == 3:
|
| 54 |
+
lbl = lbl.unsqueeze(1)
|
| 55 |
+
train_wo.append(wo)
|
| 56 |
+
train_w.append(w)
|
| 57 |
+
train_lbl.append(lbl)
|
| 58 |
+
train_wo = torch.cat(train_wo, dim=0)
|
| 59 |
+
train_w = torch.cat(train_w, dim=0)
|
| 60 |
+
train_lbl = torch.cat(train_lbl, dim=0)
|
| 61 |
+
print(f'Training samples: {train_w.shape[0]}')
|
| 62 |
+
|
| 63 |
+
print('Loading testing data...')
|
| 64 |
+
test_wo, test_w, test_lbl = [], [], []
|
| 65 |
+
for fp in testpath:
|
| 66 |
+
wo, w, lbl = load_data(fp)
|
| 67 |
+
if lbl.dim() == 3:
|
| 68 |
+
lbl = lbl.unsqueeze(1)
|
| 69 |
+
test_wo.append(wo)
|
| 70 |
+
test_w.append(w)
|
| 71 |
+
test_lbl.append(lbl)
|
| 72 |
+
test_wo = torch.cat(test_wo, dim=0)
|
| 73 |
+
test_w = torch.cat(test_w, dim=0)
|
| 74 |
+
test_lbl = torch.cat(test_lbl, dim=0)
|
| 75 |
+
print(f'Testing samples: {test_w.shape[0]}')
|
| 76 |
+
|
| 77 |
+
# ============================================================
|
| 78 |
+
# LOAD TEACHER
|
| 79 |
+
# ============================================================
|
| 80 |
+
print('Loading teacher model...')
|
| 81 |
+
from params_storage.params_fig10b_single_gpu import opt_exp, opt_encoder, opt_decoder, opt_offset_decoder
|
| 82 |
+
|
| 83 |
+
opt_encoder.checkpoints_load_dir = TEACHER_RUN_DIR
|
| 84 |
+
opt_decoder.checkpoints_load_dir = TEACHER_RUN_DIR
|
| 85 |
+
opt_offset_decoder.checkpoints_load_dir = TEACHER_RUN_DIR
|
| 86 |
+
|
| 87 |
+
enc_model = ModelADT()
|
| 88 |
+
enc_model.initialize(opt_encoder)
|
| 89 |
+
enc_model.setup(opt_encoder)
|
| 90 |
+
|
| 91 |
+
dec_model = ModelADT()
|
| 92 |
+
dec_model.initialize(opt_decoder)
|
| 93 |
+
dec_model.setup(opt_decoder)
|
| 94 |
+
|
| 95 |
+
offset_dec_model = ModelADT()
|
| 96 |
+
offset_dec_model.initialize(opt_offset_decoder)
|
| 97 |
+
offset_dec_model.setup(opt_offset_decoder)
|
| 98 |
+
|
| 99 |
+
teacher = Enc_2Dec_Network()
|
| 100 |
+
teacher.initialize(opt_exp, enc_model, dec_model, offset_dec_model, gpu_ids=opt_exp.gpu_ids)
|
| 101 |
+
|
| 102 |
+
enc_model.load_networks(TEACHER_EPOCH, load_dir=TEACHER_RUN_DIR)
|
| 103 |
+
dec_model.load_networks(TEACHER_EPOCH, load_dir=TEACHER_RUN_DIR)
|
| 104 |
+
offset_dec_model.load_networks(TEACHER_EPOCH, load_dir=TEACHER_RUN_DIR)
|
| 105 |
+
teacher.eval()
|
| 106 |
+
print('Teacher loaded.')
|
| 107 |
+
|
| 108 |
+
# ============================================================
|
| 109 |
+
# PRE-COMPUTE OUTPUTS
|
| 110 |
+
# ============================================================
|
| 111 |
+
def precompute(name, w_data, lbl_data, wo_data):
|
| 112 |
+
"""Run teacher on all samples, return stacked outputs on CPU."""
|
| 113 |
+
loader = torch.utils.data.DataLoader(
|
| 114 |
+
torch.utils.data.TensorDataset(wo_data, w_data, lbl_data),
|
| 115 |
+
batch_size=BATCH_SIZE, shuffle=False,
|
| 116 |
+
num_workers=4, pin_memory=True,
|
| 117 |
+
)
|
| 118 |
+
outputs = []
|
| 119 |
+
t0 = time.time()
|
| 120 |
+
with torch.no_grad():
|
| 121 |
+
for i, data in enumerate(loader):
|
| 122 |
+
teacher.set_input(data[1], data[2], data[0], shuffle_channel=False)
|
| 123 |
+
teacher.test()
|
| 124 |
+
outputs.append(teacher.decoder.output.cpu())
|
| 125 |
+
if (i + 1) % 50 == 0:
|
| 126 |
+
print(f' {name}: {(i+1)*BATCH_SIZE}/{len(w_data)} samples')
|
| 127 |
+
result = torch.cat(outputs, dim=0)
|
| 128 |
+
elapsed = time.time() - t0
|
| 129 |
+
print(f' {name}: {result.shape} in {elapsed:.1f}s')
|
| 130 |
+
return result
|
| 131 |
+
|
| 132 |
+
print('\nPre-computing teacher outputs for training set...')
|
| 133 |
+
train_teacher_out = precompute('train', train_w, train_lbl, train_wo)
|
| 134 |
+
|
| 135 |
+
print('Pre-computing teacher outputs for test set...')
|
| 136 |
+
test_teacher_out = precompute('test', test_w, test_lbl, test_wo)
|
| 137 |
+
|
| 138 |
+
# ============================================================
|
| 139 |
+
# SAVE
|
| 140 |
+
# ============================================================
|
| 141 |
+
train_path = './data/teacher_train_outputs.pt'
|
| 142 |
+
test_path = './data/teacher_test_outputs.pt'
|
| 143 |
+
|
| 144 |
+
torch.save(train_teacher_out, train_path)
|
| 145 |
+
torch.save(test_teacher_out, test_path)
|
| 146 |
+
|
| 147 |
+
train_size = os.path.getsize(train_path) / 1e9
|
| 148 |
+
test_size = os.path.getsize(test_path) / 1e9
|
| 149 |
+
print(f'\nSaved:')
|
| 150 |
+
print(f' {train_path} ({train_size:.2f} GB)')
|
| 151 |
+
print(f' {test_path} ({test_size:.2f} GB)')
|
| 152 |
+
print('Done. Now run train_distillation.py')
|