示例#1
0
文件: images.py 项目: multiv-ac/lucid
def image(
    w,
    h=None,
    batch=None,
    sd=None,
    decorrelate=True,
    fft=True,
    alpha=False,
    channels=None,
    init_val=None,
):
    h = h or w
    batch = batch or 1
    ch = channels or (4 if alpha else 3)
    shape = [batch, h, w, ch]
    param_f = fft_image if fft else pixel_image
    t = param_f(shape, sd=sd, init_val=init_val)
    if channels:
        output = tf.nn.sigmoid(t)
    else:
        output = to_valid_rgb(t[..., :3],
                              decorrelate=decorrelate,
                              sigmoid=True)
        if alpha:
            a = tf.nn.sigmoid(t[..., 3:])
            output = tf.concat([output, a], -1)
    return output
示例#2
0
def image(w, h=None, batch=None, sd=None, decorrelate=True, fft=True, alpha=False, init_val=None):
    h = h or w
    batch = batch or 1
    channels = 4 if alpha else 3
    shape = [batch, w, h, channels]
    
    if init_val is not None:
        t = tf.Variable( init_val )
        rgb = to_valid_rgb( t[..., :3], decorrelate=True, sigmoid=True )
    else:
        t = fft_image( shape, sd=sd ) if fft else pixel_image( shape, sd=sd )
        rgb = to_valid_rgb( t[..., :3], decorrelate=decorrelate, sigmoid=True )
    
    if alpha:
        a = tf.nn.sigmoid(t[..., 3:])
        return tf.concat([rgb, a], -1)
    
    return rgb
示例#3
0
def image(w, h=None, batch=None, sd=None, decorrelate=True, fft=True, alpha=False):
  h = h or w
  batch = batch or 1
  channels = 4 if alpha else 3
  shape = [batch, w, h, channels]
  param_f = fft_image if fft else naive
  t = param_f(shape, sd=sd)
  rgb = to_valid_rgb(t[..., :3], decorrelate=decorrelate, sigmoid=True)
  if alpha:
    a = tf.nn.sigmoid(t[..., 3:])
    return tf.concat([rgb, a], -1)
  return rgb
示例#4
0
文件: images.py 项目: pbashivan/lucid
def image(w,
          h=None,
          batch=None,
          sd=None,
          decorrelate=True,
          fft=True,
          alpha=False,
          gray=False,
          init_val=None,
          decay_power=None):
    h = h or w
    batch = batch or 1
    if gray:
        channels = 1
    else:
        if alpha:
            channels = 4
        else:
            channels = 3

    if decay_power is not None:
        fft_image_func = partial(fft_image, decay_power=decay_power)
    else:
        fft_image_func = fft_image
    shape = [batch, w, h, channels]
    param_f = fft_image_func if fft else pixel_image
    t = param_f(shape, sd=sd, init_val=init_val)
    if gray:
        rgb = tf.tile(t, [1, 1, 1, 3])
        return to_valid_rgb(rgb[..., :3], decorrelate=False, sigmoid=True)
    else:
        rgb = to_valid_rgb(t[..., :3], decorrelate=decorrelate, sigmoid=True)
        if alpha:
            a = tf.nn.sigmoid(t[..., 3:])
            return tf.concat([rgb, a], -1)
        return rgb
示例#5
0
def test_param_can_fit_image(param, test_image, maxsteps=1000):
    shape = (1, ) + test_image.shape
    with tf.Session() as sess:
        image_param_t = param(shape)
        image_t = to_valid_rgb(image_param_t)
        loss_t = tf.reduce_mean((image_t - test_image)**2)
        dist_t = tf.reduce_mean(tf.abs(image_t - test_image))

        optimizer = tf.train.AdamOptimizer(0.05)
        optimize_op = optimizer.minimize(loss_t)

        tf.global_variables_initializer().run()
        for step in range(maxsteps):
            mean_distance, _ = sess.run([dist_t, optimize_op])
            if mean_distance < 0.01:
                break
    assert mean_distance < 0.01
示例#6
0
def image_sample(shape, decorrelate=True, sd=None, decay_power=1):
    raw_spatial = rand_fft_image(shape, sd=sd, decay_power=decay_power)
    return to_valid_rgb(raw_spatial, decorrelate=decorrelate)
示例#7
0
def video_sample(shape, decorrelate=True, sd=None, decay_power=1):
    raw_spatial = fft_video(shape, sd=sd, decay_power=decay_power, rand=True)
    return to_valid_rgb(raw_spatial, decorrelate=decorrelate)