MCPcopy Create free account
hub / github.com/Royalvice/DocDiff / test

Method test

src/trainer.py:108–168  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

106 self.high_filter = Laplacian().to(self.device)
107
108 def test(self):
109 def crop_concat(img, size=128):
110 shape = img.shape
111 correct_shape = (size*(shape[2]//size+1), size*(shape[3]//size+1))
112 one = torch.ones((shape[0], shape[1], correct_shape[0], correct_shape[1]))
113 one[:, :, :shape[2], :shape[3]] = img
114 # crop
115 for i in range(shape[2]//size+1):
116 for j in range(shape[3]//size+1):
117 if i == 0 and j == 0:
118 crop = one[:, :, i*size:(i+1)*size, j*size:(j+1)*size]
119 else:
120 crop = torch.cat((crop, one[:, :, i*size:(i+1)*size, j*size:(j+1)*size]), dim=0)
121 return crop
122 def crop_concat_back(img, prediction, size=128):
123 shape = img.shape
124 for i in range(shape[2]//size+1):
125 for j in range(shape[3]//size+1):
126 if j == 0:
127 crop = prediction[(i*(shape[3]//size+1)+j)*shape[0]:(i*(shape[3]//size+1)+j+1)*shape[0], :, :, :]
128 else:
129 crop = torch.cat((crop, prediction[(i*(shape[3]//size+1)+j)*shape[0]:(i*(shape[3]//size+1)+j+1)*shape[0], :, :, :]), dim=3)
130 if i == 0:
131 crop_concat = crop
132 else:
133 crop_concat = torch.cat((crop_concat, crop), dim=2)
134 return crop_concat[:, :, :shape[2], :shape[3]]
135
136 def min_max(array):
137 return (array - array.min()) / (array.max() - array.min())
138 with torch.no_grad():
139 self.network.init_predictor.load_state_dict(torch.load(self.TEST_INITIAL_PREDICTOR_WEIGHT_PATH))
140 self.network.denoiser.load_state_dict(torch.load(self.TEST_DENOISER_WEIGHT_PATH))
141 print('Test Model loaded')
142 self.network.eval()
143 tq = tqdm(self.dataloader_test)
144 sampler = self.diffusion
145 iteration = 0
146 for img, gt, name in tq:
147 tq.set_description(f'Iteration {iteration} / {len(self.dataloader_test.dataset)}')
148 iteration += 1
149 if self.native_resolution == 'True':
150 temp = img
151 img = crop_concat(img)
152 noisyImage = torch.randn_like(img).to(self.device)
153 init_predict = self.network.init_predictor(img.to(self.device), 0)
154
155 if self.DPM_SOLVER == 'True':
156 sampledImgs = dpm_solver(self.schedule.get_betas(), self.network,
157 torch.cat((noisyImage, img.to(self.device)), dim=1), self.DPM_STEP)
158 else:
159 sampledImgs = sampler(noisyImage.cuda(), init_predict, self.pre_ori)
160 finalImgs = (sampledImgs + init_predict)
161 if self.native_resolution == 'True':
162 finalImgs = crop_concat_back(temp, finalImgs)
163 init_predict = crop_concat_back(temp, init_predict)
164 sampledImgs = crop_concat_back(temp, sampledImgs)
165 img = temp

Callers 1

testFunction · 0.95

Calls 2

dpm_solverFunction · 0.85
get_betasMethod · 0.80

Tested by

no test coverage detected