コード例 #1
0
ファイル: conv.py プロジェクト: rajr0/convolver
def main():
    p = argparse.ArgumentParser(description='Convolve an image with a kernel.')
    p.add_argument('img', help='input image filename')
    p.add_argument('k', help='path to kernel ')
    p.add_argument(
        '-gamma',
        type=float,
        default=1.0,
        help='gamma correction to use for images (default: no correction)')
    p.add_argument('-out', help='output to *.png file instead of viewing')
    args = p.parse_args()

    img = util.load_image(args.img, args, gray=False)
    n, h, w, c = img.shape
    assert n == 1, n
    out = np.zeros((h, w, c), dtype=np.float32)
    step, kernel = util.load_kernel(args.k)
    sess = util.make_session()
    for i in range(c):
        chan = sess.run(
            util.convolve(tf.constant(img[:, :, :, i:i + 1]), kernel))
        out[:, :, i] = chan[0, :, :, 0]
    out = util.from_float(out, args.gamma)

    if args.out is not None:
        util.save_image(args.out, out)
        print('Written to', args.out)
    else:
        print('Press ESC to close window.')
        util.viewer(None, lambda: out)
コード例 #2
0
ファイル: cost.py プロジェクト: rajr0/convolver
def main():
    p = argparse.ArgumentParser(
        description=
        'Given two images and some kernels, report the difference per kernel.')
    p.add_argument('a', help='input image filename')
    p.add_argument('b', help='expected image filename')
    p.add_argument('kernels', nargs='*', help='kernel directory')
    p.add_argument(
        '-gamma',
        type=float,
        default=1.0,
        help='gamma correction to use for images (default: no correction)')
    p.add_argument('-crop_x',
                   type=int,
                   default=0,
                   help='crop X offset in pixels, range is [0..width-1]')
    p.add_argument(
        '-crop_y',
        type=int,
        default=0,
        help=
        'crop Y offset in pixels, range is [0..height-1] where 0 is the TOP')
    p.add_argument('-crop_w', type=int, default=0, help='crop width in pixels')
    p.add_argument('-crop_h',
                   type=int,
                   default=0,
                   help='crop height in pixels')
    args = p.parse_args()

    img1 = util.load_image(args.a, args)
    img2 = util.load_image(args.b, args)
    assert img1.shape == img2.shape, (img1.shape, img2.shape)
    print('# Loaded images. Shape is', img1.shape)

    img_input = tf.constant(img1)
    img_expected = tf.constant(img2)
    sess = util.make_session()

    for kfn in args.kernels:
        step, kernel = util.load_kernel(kfn)
        n = kernel.shape[0]
        border = (n + 1) // 2

        # Convolve and calculate costs.
        img_actual = util.convolve(img_input, kernel)
        dcost = sess.run(
            util.diff_cost(util.diff(img_actual, img_expected, border)))
        rcost = sess.run(util.reg_cost(kernel))

        print(kfn, 'n', n, 'diffcost %.12f' % dcost, 'regcost', rcost,
              'avg-px-err', util.avg_px_err(dcost, args.gamma))
コード例 #3
0
def main():
    p = argparse.ArgumentParser(
        description='Given two images, determine the convolution kernel so that '
        'a * k = b')
    p.add_argument('ka', help='input kernel')
    p.add_argument('kb', help='output kernel directory')
    p.add_argument('n', type=int, help='output kernel size')
    p.add_argument('-mul',
                   type=float,
                   default=.5,
                   help='multiplier for random fill')
    p.add_argument(
        '-norm',
        type=float,
        default=0,
        help='normalize sum to this amount (default: zero: no normalization)')
    args = p.parse_args()

    os.mkdir(args.kb)
    step, kernel = util.load_kernel(args.ka)

    print('input kernel size', kernel.shape)
    e = np.mean(np.abs(kernel))
    print('input kernel mean', e)

    na = kernel.shape[0]
    nb = args.n

    outk = np.random.normal(size=(nb, nb, 1, 1)).astype(np.float32)
    oute = np.mean(np.abs(outk))
    outk *= e * args.mul / oute

    if nb > na:
        h = (nb - na) // 2
        print('grow: offset', h)
        outk[h:h + na, h:h + na, :, :] = kernel
    else:
        h = (na - nb) // 2
        print('shrink: offset', h)
        outk = kernel[h:h + nb, h:h + nb, :, :]

    if args.norm != 0:
        outk = outk / np.sum(outk) * args.norm

    oute = np.mean(np.abs(outk))
    print('output kernel mean', oute)

    util.save_kernel(args.kb, step, outk)
    print('resized from', kernel.shape, 'to', outk.shape)
コード例 #4
0
def main():
    p = argparse.ArgumentParser(description='Display a kernel.')
    p.add_argument('-out', help='output to *.png file instead of viewing')
    p.add_argument('k', nargs='*', help='path to kernel(s)')
    args = p.parse_args()

    out = None

    for fn in args.k:
        print('Loading', fn)
        step, kernel = util.load_kernel(fn)
        print('  Step', step)
        print('  Kernel shape is', kernel.shape)
        print('  Min', np.min(kernel))
        print('  Max', np.max(kernel))
        print('  Mean', np.mean(kernel))
        print('  Sum', np.sum(kernel))
        print('  Sum of abs', np.sum(np.abs(kernel)))
        print('  RMS', np.sqrt(np.mean(kernel * kernel)))

        render = util.vis_hwoi(kernel, doubles=2)
        render = util.hstack([render, util.make_label(fn)], 5)

        if out is None:
            out = render
        else:
            out = util.vstack([out, render], 5)

    out = util.border(out, 5)

    if args.out is not None:
        util.save_image(args.out, out)
        print('Written to', args.out)
    else:
        print('Press ESC to close window.')

        def render_fn():
            return out

        util.viewer(None, render_fn)
コード例 #5
0
def main():
    MAIN_T = time.time()
    p = argparse.ArgumentParser(
        description='Given two images, determine the convolution kernel so that '
        'a * k = b')
    p.add_argument('a', help='input image filename')
    p.add_argument('b', help='expected image filename')
    p.add_argument('k', help='kernel directory')
    p.add_argument(
        '-n',
        type=int,
        default=5,
        help='kernel size is NxN (default: 5, or automatically set to size '
        'of loaded kernel)')
    p.add_argument(
        '-sym',
        type=boolchoice,
        default=True,
        choices=[True, False],
        help='kernel will be symmetric if set to True (default: True)')
    p.add_argument(
        '-gamma',
        type=float,
        default=1.0,
        help='gamma correction to use for images (default: no correction)')
    p.add_argument(
        '-reg_cost',
        type=float,
        default=0.,
        help='regularization cost: the sum of weights is multiplied by this '
        'and added to the cost (default: zero: no regularization)')
    p.add_argument(
        '-border',
        type=int,
        default=-1,
        help='how many pixels to remove from the border (from every edge of the '
        'image) before calculating the difference (default: auto based on '
        'kernel size)')
    p.add_argument('-learn_rate',
                   type=float,
                   default=2.**-10,
                   help='learning rate for the optimizer')
    p.add_argument('-epsilon',
                   type=float,
                   default=.09,
                   help='epsilon for the optimizer')
    p.add_argument(
        '-max_steps',
        type=int,
        default=0,
        help='stop after this many steps (default: zero: never stop)')
    p.add_argument('-log_every',
                   type=int,
                   default=100,
                   help='log stats every N steps (0 to disable)')
    p.add_argument('-save_every',
                   type=int,
                   default=500,
                   help='save kernel and image every N steps (0 to disable)')
    p.add_argument('-crop_x',
                   type=int,
                   default=0,
                   help='crop X offset in pixels, range is [0..width-1]')
    p.add_argument(
        '-crop_y',
        type=int,
        default=0,
        help=
        'crop Y offset in pixels, range is [0..height-1] where 0 is the TOP')
    p.add_argument('-crop_w', type=int, default=0, help='crop width in pixels')
    p.add_argument('-crop_h',
                   type=int,
                   default=0,
                   help='crop height in pixels')
    p.add_argument(
        '-fps',
        type=float,
        default=5,
        help='how often to update the viewer, set to zero to disable viewer')
    args = p.parse_args()

    if not os.path.exists(args.k):
        os.mkdir(args.k)
        step = -1
    else:
        step, w1 = util.load_kernel(args.k)
        args.n = w1.shape[0]

    if step >= args.max_steps and args.max_steps != 0:
        print('Current step %d is over max %d. Exiting.' %
              (step, args.max_steps))
        return 0

    log = util.Logger(args.k + '/log.txt')
    log.log('--- Start of run ---')
    log.log('Cmdline:', sys.argv)

    # Load images.
    img1 = util.load_image(args.a, args)
    img2 = util.load_image(args.b, args)
    assert img1.shape == img2.shape, (img1.shape, img2.shape)
    log.log('Loaded images. Shape is', img1.shape, '(NHWC)')

    vimg1 = util.vis_nhwc(img1, doubles=0, gamma=args.gamma)
    vimg2 = util.vis_nhwc(img2, doubles=0, gamma=args.gamma)

    # Load and initialize weights.
    if step >= 0:
        log.log('Loaded weights, shape is', w1.shape, '(HWIO)')
    else:
        assert step == -1, step
        step = 0
        log.log('Starting with random weights.')
        w1 = np.random.normal(size=(args.n, args.n, 1, 1),
                              scale=.2).astype(np.float32)
        m = args.n // 2
        w1[m, m, 0, 0] = 1.  # Bright middle pixel.
    if args.sym:
        w1 = util.make_symmetric(w1)
    else:
        w1 = tf.Variable(w1)

    if args.border == -1:
        args.border = (args.n + 1) // 2
        log.log('Automatically set border to', args.border)

    log.log('Current args:', args.__dict__)
    log.log('Starting at step', step)

    # Convolution.
    input_img = tf.constant(img1)
    expected_img = tf.constant(img2)
    actual_img = util.convolve(input_img, w1)  # <-- THIS IS THE CALCULATION.

    # Cost.
    diff = util.diff(actual_img, expected_img, args.border)
    diffcost = util.diff_cost(diff)  # L2
    cost = diffcost

    # Regularization.
    reg = util.reg_cost(w1)  # L1
    if args.reg_cost != 0:
        cost += reg * args.reg_cost

    # Optimizer.
    global_step = tf.Variable(step,
                              dtype=tf.int32,
                              trainable=False,
                              name='global_step')
    train_step = tf.train.AdamOptimizer(
        args.learn_rate, args.epsilon).minimize(cost, global_step=global_step)

    log.log('Starting TF session.')
    sess = util.make_session(outdir=args.k)

    # Get ready for viewer.
    log_last_step = [step]
    log_last_time = [time.time()]

    def periodic_log():
        """Does a log.log() of stats like step number and current error."""
        now = time.time()
        rstep, rcost, rreg, rdiffcost = sess.run(
            [global_step, cost, reg, diffcost])
        if log_last_step[0] == rstep: return  # Dupe call.
        log.log(
            'steps',
            rstep,
            'total-cost %.9f' % rcost,
            'diffcost %.9f' % rdiffcost,
            'reg %.9f' % rreg,
            'avg-px-err %.6f' % util.avg_px_err(rdiffcost, args.gamma),
            'steps/sec %.2f' % ((rstep - log_last_step[0]) /
                                (now - log_last_time[0])),
        )
        log_last_step[0] = rstep
        log_last_time[0] = now

    render_time = [0.]

    def render():
        """Returns an image showing the current weights and output."""
        # TODO: vertically align labels.
        t0 = time.time()
        rout, rdiff, rw = sess.run([actual_img, diff, w1])
        render_out = util.vstack([
            util.hstack([
                util.vstack([util.cache_label('input:'), vimg1], 5),
                util.vstack([
                    util.cache_label('actual:'),
                    util.vis_nhwc(rout, doubles=0, gamma=args.gamma)
                ], 5),
                util.vstack([util.cache_label('expected:'), vimg2], 5),
            ], 5),
            util.cache_label('difference:'),
            util.vis_nhwc(rdiff, doubles=0),
            util.cache_label('kernel:'),
            util.vis_hwoi(rw, doubles=2),
        ], 5)
        render_out = util.border(render_out, 5)
        t1 = time.time()
        render_time[0] += t1 - t0
        return render_out

    def periodic_save():
        rstep, rdiffcost, rw = sess.run([global_step, diffcost, w1])
        util.save_kernel(args.k, rstep, rw)
        rfn = args.k + '/render-step%08d-diff%.9f.png' % (rstep, rdiffcost)
        util.save_image(rfn, render())

    calc_time = [0.]

    def calc_fn():
        """
    Run train_step.
    Then do every-N-steps housekeeping.
    """
        t0 = time.time()
        sess.run(train_step)  # <--- THIS IS WHERE THE MAGIC HAPPENS.
        t1 = time.time()
        calc_time[0] += t1 - t0
        nsteps = sess.run(global_step)
        if args.log_every != 0:
            if nsteps == 1 or nsteps % args.log_every == 0:
                periodic_log()
        if args.save_every != 0:
            if nsteps % args.save_every == 0:
                periodic_save()
        if args.max_steps == 0:
            return True  # Loop forever.
        return nsteps < args.max_steps

    log.log('Start optimizer.')
    START_T = time.time()
    if args.fps == 0:
        while True:
            if not calc_fn(): break
    else:
        util.viewer(calc_fn, render, fps=args.fps, hang=False)
    STOP_T = time.time()
    # Final log and save.
    log.log('Stop optimizer.')
    log.log('Render time %.3fs (%.02f%% of optimizer)' %
            (render_time[0], 100. * render_time[0] / (STOP_T - START_T)))
    periodic_log()
    periodic_save()
    nsteps = sess.run(global_step) - step
    log.log('Steps this session %d, calc time %.3fs (%.02f%% of optimizer)' %
            (nsteps, calc_time[0], 100. * calc_time[0] / (STOP_T - START_T)))
    log.log('Calc steps/sec %.3f, with overhead steps/sec %.3f' %
            (nsteps / calc_time[0], nsteps / (STOP_T - START_T)))
    END_T = time.time()
    log.log('Total time spent: %.3fs' % (END_T - INIT_T))
    for k, v in [
        ('before main', MAIN_T - INIT_T),
        ('setting up', START_T - MAIN_T),
        ('optimizing', STOP_T - START_T),
        ('finishing up', END_T - STOP_T),
    ]:
        log.log(' - time spent %s: %.3fs (%.02f%% of total)' %
                (k, v, 100. * v / (END_T - INIT_T)))
    log.close()
コード例 #6
0
# -*- coding: utf-8 -*-

import ctypes as ct
import socket
import weakref

from ip4tc import Rule, Table, IPTCError
from util import find_library, load_kernel
from xtables import (XT_INV_PROTO, NFPROTO_IPV6, xt_align, xt_counters)

__all__ = ["Table6", "Rule6"]

load_kernel("ip6_tables")

_IFNAMSIZ = 16


def is_table6_available(name):
    try:
        Table6(name)
        return True
    except IPTCError:
        pass
    return False


class in6_addr(ct.Structure):
    """This class is a representation of the C struct in6_addr."""
    _fields_ = [("s6_addr", ct.c_uint8 * 16)]  # IPv6 address

コード例 #7
0
ファイル: ip6tc.py プロジェクト: bcl/python-iptables
# -*- coding: utf-8 -*-

import ctypes as ct
import socket
import weakref

from ip4tc import Rule, Table, IPTCError
from util import find_library, load_kernel
from xtables import (XT_INV_PROTO, NFPROTO_IPV6, xt_align, xt_counters)

__all__ = ["Table6", "Rule6"]

load_kernel("ip6_tables")

_IFNAMSIZ = 16


def is_table6_available(name):
    try:
        Table6(name)
        return True
    except IPTCError:
        pass
    return False


class in6_addr(ct.Structure):
    """This class is a representation of the C struct in6_addr."""
    _fields_ = [("s6_addr", ct.c_uint8 * 16)]  # IPv6 address