Beispiel #1
0
    shapes = []
    for i in range(nprims):
        shapes.append([go(), go(), go(), gogo()])
    return np.array(shapes, dtype=config.floatX)

def genshapebatch(nprims, nbatch):
    shapes = np.random.rand(nprims, nbatch, 4)*2 - 2
    return np.array(shapes, dtype=config.floatX)

width = 224
height = 224
# Generate initial rays
exfragcoords = gen_fragcoords(width, height)
nprims = 500
print("Compiling Renderer")
render = make_render(nprims, width, height)
shapes = genshapebatch(nprims, 2)
print("Rendering")
img = render(exfragcoords, shapes)

from ig import display
display.draw(img[:,:,0])


# np.save('data/observed_img', img, allow_pickle=True, fix_imports=True)
#
# # print("Drawing Img")
# # draw(img)
#
# print("Doing Pixel Comparison")
# cost = similarity_cost2(img, nprims, width, height)
Beispiel #2
0
def second_order(nprims = 200, nbatch = 50):
    """Creates a network which takes as input a image and returns a cost.
    Network extracts features of image to create shape params which are rendered.
    The similarity between the rendered image and the actual image is the cost
    """
    width = 224
    height = 224
    params_per_prim = 3
    nshape_params = nprims * params_per_prim

    img = T.tensor4("input image")
    net = {}
    net['input'] = InputLayer((nbatch, 1, 224, 224), input_var = img)
    net['conv1'] = ConvLayer(net['input'], num_filters=96, filter_size=7, stride=2)
    net['norm1'] = NormLayer(net['conv1'], alpha=0.0001) # caffe has alpha = alpha * pool_size
    net['pool1'] = PoolLayer(net['norm1'], pool_size=3, stride=3, ignore_border=False)
    net['conv2'] = ConvLayer(net['pool1'], num_filters=256, filter_size=5)
    net['pool2'] = PoolLayer(net['conv2'], pool_size=2, stride=2, ignore_border=False)
    net['conv3'] = ConvLayer(net['pool2'], num_filters=512, filter_size=3, pad=1)
    net['conv4'] = ConvLayer(net['conv3'], num_filters=512, filter_size=3, pad=1)
    net['conv5'] = ConvLayer(net['conv4'], num_filters=512, filter_size=3, pad=1)
    net['pool5'] = PoolLayer(net['conv5'], pool_size=3, stride=3, ignore_border=False)
    net['fc6'] = DenseLayer(net['pool5'], num_units=4096)
    net['drop6'] = DropoutLayer(net['fc6'], p=0.5)
    net['fc7'] = DenseLayer(net['drop6'], num_units=nshape_params)
    # net['fc7'] = DenseLayer(net['pool5'], num_units=nshape_params, nonlinearity=lasagne.nonlinearities.tanh)
    output_layer = net['fc7']
    output = lasagne.layers.get_output(output_layer)
    scaled_output = output - 1

    ## Render these parameters
    shape_params = T.reshape(scaled_output, (nprims, nbatch, params_per_prim))
    fragCoords = T.tensor3('fragCoords')
    print "Symbolic Render"
    res, scan_updates = symbolic_render(nprims, shape_params, fragCoords, width, height)
    res_reshape = res.dimshuffle([2,'x',0,1])

    # Simply using pixel distance
    eps = 1e-9

    diff = T.maximum(eps, (res_reshape - img)**2)
    loss1 = T.sum(diff) / (224*224*nbatch)

    mean_shape = T.mean(shape_params, axis=1) # mean across batches
    mean_shape = T.reshape(mean_shape, (nprims, 1, params_per_prim))
    scale = 1.0
    diff2 = T.maximum(eps, (mean_shape - shape_params)**2)
    loss2 = T.sum(diff2) / (nprims * params_per_prim * nbatch)
    mu = 0
    sigma = 4
    a = 1/(sigma*np.sqrt(2*np.pi))
    b = mu
    c = sigma
    loss2 = -a*T.exp((-loss2**2)/(2*c**2))

    loss = loss2 + loss1
    # Create update expressions for training, i.e., how to modify the
    # parameters at each training step. Here, we'll use Stochastic Gradient
    # Descent (SGD) with Nesterov momentum, but Lasagne offers plenty more.
    params = lasagne.layers.get_all_params(output_layer, trainable=True)
    network_updates = lasagne.updates.nesterov_momentum(loss, params, learning_rate=0.01, momentum=0.9)

    ## Merge Updates
    for k in network_updates.keys():
        assert not(scan_updates.has_key(k))
        scan_updates[k] = network_updates[k]

    print("Compiling Loss Function")
    grad = T.grad(loss, params[0])
    netcost = function([fragCoords, img], [loss, grad, res_reshape, shape_params, diff, loss1, loss2], updates=scan_updates, mode=curr_mode)
    # netcost = function([fragCoords, img], loss, updates=scan_updates, mode=curr_mode)

    ## Generate Render Function to make data
    # Generate initial rays
    exfragcoords = gen_fragcoords(width, height)
    print("Compiling Renderer")
    render = make_render(nprims, width, height)
    return render, netcost, output_layer