MCPcopy Create free account
hub / github.com/tensorpack/tensorpack / view_warp

Function view_warp

examples/SpatialTransformer/mnist-addition.py:204–235  ·  view source on GitHub ↗
(modelpath)

Source from the content-addressed store, hash-verified

202
203
204def view_warp(modelpath):
205 pred = OfflinePredictor(PredictConfig(
206 session_init=SmartInit(modelpath),
207 model=Model(),
208 input_names=['input'],
209 output_names=['visualization/viz', 'STN1/affine', 'STN2/affine']))
210
211 xys = np.array([[0, 0, 1],
212 [WARP_TARGET_SIZE, 0, 1],
213 [WARP_TARGET_SIZE, WARP_TARGET_SIZE, 1],
214 [0, WARP_TARGET_SIZE, 1]], dtype='float32')
215
216 def draw_rect(img, affine, c, offset=(0, 0)):
217 a = np.transpose(affine) # 3x2
218 a = (np.matmul(xys, a) + list(offset)).astype('int32')
219 cv2.line(img, tuple(a[0][::-1]), tuple(a[1][::-1]), c)
220 cv2.line(img, tuple(a[1][::-1]), tuple(a[2][::-1]), c)
221 cv2.line(img, tuple(a[2][::-1]), tuple(a[3][::-1]), c)
222 cv2.line(img, tuple(a[3][::-1]), tuple(a[0][::-1]), c)
223
224 ds = get_data(False)
225 ds.reset_state()
226 for k in ds:
227 img, label = k
228 outputs, affine1, affine2 = pred(img)
229 for idx, viz in enumerate(outputs):
230 viz = cv2.cvtColor(viz, cv2.COLOR_GRAY2BGR)
231 # Here we assume the second branch focuses on the first digit
232 draw_rect(viz, affine2[idx], (0, 0, 255))
233 draw_rect(viz, affine1[idx], (0, 0, 255), offset=[IMAGE_SIZE, 0])
234 cv2.imwrite('{:03d}.png'.format(idx), (viz + 0.5) * 255)
235 break
236
237
238def get_config():

Callers 1

mnist-addition.pyFile · 0.85

Calls 8

OfflinePredictorClass · 0.85
PredictConfigClass · 0.85
SmartInitFunction · 0.85
draw_rectFunction · 0.85
formatMethod · 0.80
ModelClass · 0.70
get_dataFunction · 0.70
reset_stateMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…