import numpy from gbp import GBP # Setup a chain... print 'Whole chain...' solver = GBP(5) solver.unary(0, 0.0, 15.0) solver.unary(4, 10.0, 15.0) solver.pairwise(slice(None, -1), slice(1, None), 0.5, 1.0) iters = solver.solve_bp() print 'iters =', iters mean, prec = solver.result() print 'Mean: ' + ' '.join(['%.2f' % v for v in mean]) print 'Precison: ' + ' '.join(['%.2f' % v for v in prec]) print # Disable middle and resolve... print 'Middle gone...' solver.disable(2) iters = solver.solve_bp() print 'iters =', iters mean, prec = solver.result() print 'Mean: ' + ' '.join(['%.2f' % v for v in mean])
from gbp import GBP # Setup a chain... print 'Whole chain...' solver = GBP(5) solver.unary(0, 0.0, 15.0) solver.unary(4, 10.0, 15.0) solver.pairwise(slice(None,-1), slice(1, None), 0.5, 1.0) iters = solver.solve_bp() print 'iters =', iters mean, prec = solver.result() print 'Mean: ' + ' '.join(['%.2f'%v for v in mean]) print 'Precison: ' + ' '.join(['%.2f'%v for v in prec]) print # Disable middle and resolve... print 'Middle gone...' solver.disable(2) iters = solver.solve_bp() print 'iters =', iters mean, prec = solver.result()
print 'Single Pivot:' solver = GBP(25) #5x5 solver.unary(12, 5.0, 4.0) for row in xrange(5): solver.pairwise(slice(row*5,(row+1)*5-1), slice(row*5+1,(row+1)*5), 1.0, 1.0) for col in xrange(5): solver.pairwise(slice(col,col+20,5), slice(col+5,col+25,5), 1.0, 1.0) iters = solver.solve() print 'iters =', iters for row in xrange(5): mean, prec = solver.result(slice(row*5, (row+1)*5)) print ' '.join(['%.4f'%v for v in mean]) print ' '.join(['(%.2f)'%v for v in prec]) print # Grid 2... print 'Stretch:' solver = GBP(25) #5x5 solver.unary(0, 0.0, 5.0) solver.unary(4, 9.0, 5.0) solver.unary(20, 9.0, 5.0) solver.unary(24, 0.0, 5.0)
solver.unary(12, 5.0, 4.0) for row in xrange(5): solver.pairwise(slice(row * 5, (row + 1) * 5 - 1), slice(row * 5 + 1, (row + 1) * 5), 1.0, 1.0) for col in xrange(5): solver.pairwise(slice(col, col + 20, 5), slice(col + 5, col + 25, 5), 1.0, 1.0) iters = solver.solve() print 'iters =', iters for row in xrange(5): mean, prec = solver.result(slice(row * 5, (row + 1) * 5)) print ' '.join(['%.4f' % v for v in mean]) print ' '.join(['(%.2f)' % v for v in prec]) print # Grid 2... print 'Stretch:' solver = GBP(25) #5x5 solver.unary(0, 0.0, 5.0) solver.unary(4, 9.0, 5.0) solver.unary(20, 9.0, 5.0) solver.unary(24, 0.0, 5.0) for row in xrange(5):
iters = 0 start = time.clock() while True: it = solver.solve_trws(args.report, args.epsilon) iters += it print(' %i iters, delta = %f (target = %f)' % (iters, solver.last_delta, args.epsilon)) if it != args.report: break end = time.clock() print('...solved in %.1f seconds' % (end - start)) # Convert back to a normal field... height, _ = solver.result() height = height.reshape((image.shape[0], image.shape[1])) grad_x = height.copy() grad_x[:, 0] = height[:, 1] - height[:, 0] grad_x[:, 1:-1] = 0.5 * (height[:, 2:] - height[:, :-2]) grad_x[:, -1] = height[:, -1] - height[:, -2] grad_x *= -1.0 grad_y = height.copy() grad_y[0, :] = height[1, :] - height[0, :] grad_y[1:-1, :] = 0.5 * (height[2:, :] - height[:-2, :]) grad_y[-1, :] = height[-1, :] - height[-2, :] norm = numpy.concatenate( (grad_x[:, :, numpy.newaxis], grad_y[:, :, numpy.newaxis],
start = time.clock() while True: it = solver.solve_trws(args.report, args.epsilon) iters += it print(' %i iters, delta = %f (target = %f)' % (iters, solver.last_delta, args.epsilon)) if it!=args.report: break end = time.clock() print('...solved in %.1f seconds' % (end - start)) # Convert back to a normal field... height, _ = solver.result() height = height.reshape((image.shape[0], image.shape[1])) grad_x = height.copy() grad_x[:,0] = height[:,1] - height[:,0] grad_x[:,1:-1] = 0.5 * (height[:,2:] - height[:,:-2]) grad_x[:,-1] = height[:,-1] - height[:,-2] grad_x *= -1.0 grad_y = height.copy() grad_y[0,:] = height[1,:] - height[0,:] grad_y[1:-1,:] = 0.5 * (height[2:,:] - height[:-2,:]) grad_y[-1,:] = height[-1,:] - height[-2,:] norm = numpy.concatenate((grad_x[:,:,numpy.newaxis], grad_y[:,:,numpy.newaxis], numpy.ones((height.shape[0], height.shape[1], 1), dtype=numpy.float32)), axis=2)