Example #1
0
def objective_func(x, net, all_target_blob_names, targets, target_data_list, tv_lambda, tv_beta):
    # Makes one iteration step and updates the gradient of the data blob

    get_data_blob(net).data[...] = np.reshape(x, get_data_blob(net).data.shape)
    get_data_blob(net).diff[...] = 0
    net.forward()

    loss = 0
    # Go through target blobs in reversed order
    for i in range(len(all_target_blob_names)):
        blob_i = len(all_target_blob_names) - 1 - i
        start = all_target_blob_names[blob_i]

        if blob_i == 0:
            end = None
        else:
            end = all_target_blob_names[blob_i - 1]

        # Get target blob
        target_blob = net.blobs[start]
        if i == 0:
            target_blob.diff[...] = 0

        gen_data = target_blob.data.copy()
        # Apply RELU
        pos_mask = gen_data > 0
        gen_data[~pos_mask] = 0

        # Go through all images and compute accumulated gradient for the current target blob
        target_blob_add_diff = np.zeros_like(target_blob.diff, dtype=np.float64)
        for target_i, (_, target_blob_names, is_gram, weight) in enumerate(targets):
            # Skip if the current blob is not among the target's blobs
            if start not in target_blob_names:
                continue

            target_data = target_data_list[target_i][start]
            if is_gram:
                c_loss, c_grad = style_grad(gen_data, target_data)
            else:
                c_loss, c_grad = content_grad(gen_data, target_data)

            # Apply RELU
            c_grad[~pos_mask] = 0
            target_blob_add_diff += c_grad * weight / len(target_blob_names)
            loss += c_loss * weight / len(target_blob_names)

        target_blob.diff[...] += target_blob_add_diff
        net.backward(start=start, end=end)

    if tv_lambda > 0:
        tv_loss, tv_grad = totalvariation.tv_norm(x.reshape(get_data_blob(net).data.shape),beta=tv_beta)
        return loss + tv_loss*tv_lambda, np.ravel(get_data_blob(net).diff).astype(np.float64) + np.ravel(tv_grad)*tv_lambda
    else:
        return loss, np.ravel(get_data_blob(net).diff).astype(np.float64)
Example #2
0
  def objective_fn(x, net, all_target_blob_names, targets, target_data_list, tv_lambda, tv_beta):
    # def objective_func(x, net, all_target_blob_names, targets, target_data_list, tv_lambda, tv_beta):
    # x = current solution image
    # returns loss, gradients
    deepart.get_data_blob(net).data[...]=np.reshape(x,deepart.get_data_blob(net).data.shape)
    deepart.get_data_blob(net).diff[...]=0
    net.forward()

    loss = 0
    # Go through target blobs in reversed order
    for i in range(len(all_target_blob_names)):
        blob_i = len(all_target_blob_names) - 1 - i
        start = all_target_blob_names[blob_i]

        if blob_i == 0:
            end = None
        else:
            end = all_target_blob_names[blob_i - 1]

        # Get target blob
        target_blob = net.blobs[start]
        if i == 0:
            target_blob.diff[...] = 0

        gen_data = target_blob.data.copy()
        print('gen_data',gen_data.shape,gen_data.dtype) # debug
        # Apply RELU
        pos_mask = gen_data > 0
        gen_data[~pos_mask] = 0

        # Go through all images and compute accumulated gradient for the current target blob
        target_blob_add_diff = np.zeros_like(target_blob.diff, dtype=np.float64)
        for target_i, (_, target_blob_names, is_gram, weight) in enumerate(targets):
            # Skip if the current blob is not among the target's blobs
            if start not in target_blob_names:
                continue

            target_data = target_data_list[target_i][start]
            if is_gram:
                c_loss, c_grad = deepart.style_grad(gen_data, target_data)
            else:
                c_loss, c_grad = deepart.content_grad(gen_data, target_data)

            # Apply RELU
            c_grad[~pos_mask] = 0
            target_blob_add_diff += c_grad * weight / len(target_blob_names)
            loss += c_loss * weight / len(target_blob_names)

        target_blob.diff[...] += target_blob_add_diff
        net.backward(start=start, end=end)

    print('loss',loss)
    grad = np.ravel(deepart.get_data_blob(net).diff).astype(np.float64)

    # debug
    for (gradient_target, gradient_weight) in gradient_space_targets:
      gen_data = x.reshape(deepart.get_data_blob(net).data.shape)
      fy = np.diff(gen_data, axis=2)
      fx = np.diff(gen_data, axis=3)
      loss_g, grad_g = deepart.gradient_grad(gen_data, gradient_target, gradient_weight)
      grad_g = np.ravel(grad_g).astype(np.float64)
      loss += loss_g
      grad += grad_g
      print('loss_g',loss_g)

    for (image_target, image_weight) in image_space_targets:
      loss_i, grad_i = deepart.content_grad(gen_data, image_target, weight=image_weight)
      grad_i = np.ravel(grad_i).astype(np.float64)
      loss += loss_i
      grad += grad_i
      print('loss_i',loss_i)

    if tv_lambda > 0:
        tv_loss, tv_grad = totalvariation.tv_norm(x.reshape(deepart.get_data_blob(net).data.shape),beta=tv_beta)
        print('loss_tv',tv_loss*tv_lambda)
        return loss + tv_loss*tv_lambda, grad + np.ravel(tv_grad)*tv_lambda
    else:
        return loss, grad
Example #3
0
def objective_func2(x, net, blob_names, target, target_data, tv_lambda, tv_beta):
    '''
    blob_names is a list of all blobs sorted front-to-back
    target and target_data are dicts of blob names
    target[blob name] is a list of 3-tuples (weight, target type, data index)
      weight is a scalar
      target type is 'gram' or 'l2'
      data index is a zero-based index into the target tensor
    target_data[blob name] is a tensor (image x channel x height x width)
    tv_lambda and tv_beta are scalars for the total variation regularizer
    '''

    # Makes one iteration step and updates the gradient of the data blob

    get_data_blob(net).data[...] = np.reshape(x, get_data_blob(net).data.shape)
    get_data_blob(net).diff[...] = 0
    net.forward()

    loss = 0
    # Go through target blobs in reversed order
    for i in range(len(blob_names)):
        blob_i = len(blob_names) - 1 - i
        start = blob_names[blob_i]

        if blob_i == 0:
            end = None
        else:
            end = blob_names[blob_i - 1]

        # Get target blob
        target_blob = net.blobs[start]
        if i == 0:
            target_blob.diff[...] = 0
        gen_data = target_blob.data.copy()

        # Apply RELU
        pos_mask = gen_data > 0
        gen_data[~pos_mask] = 0

        # Accumulate objectives
        target_blob_add_diff = np.zeros_like(target_blob.diff, dtype=np.float64)
        for (weight, target_type, data_i) in target[start]:
            if target_type=='gram':
                c_loss, c_grad = style_grad(gen_data, target_data[start][data_i])
            elif target_type=='l2':
                c_loss, c_grad = content_grad(gen_data, target_data[start][data_i])
            else:
                raise ValueError('Unknown target type: {}'.format(target_type))

            # Apply RELU
            c_grad[~pos_mask] = 0
            target_blob_add_diff += c_grad * weight
            loss += c_loss * weight

        # Update model
        target_blob.diff[...] += target_blob_add_diff
        net.backward(start=start, end=end)

    if tv_lambda > 0:
        tv_loss, tv_grad = totalvariation.tv_norm(x.reshape(get_data_blob(net).data.shape), beta=tv_beta)
        return loss + tv_loss * tv_lambda, np.ravel(get_data_blob(net).diff).astype(np.float64) + np.ravel(tv_grad) * tv_lambda
    else:
        return loss, np.ravel(get_data_blob(net).diff).astype(np.float64)
Example #4
0
def objective_func2(x, net, blob_names, target, target_data, tv_lambda,
                    tv_beta):
    '''
    blob_names is a list of all blobs sorted front-to-back
    target and target_data are dicts of blob names
    target[blob name] is a list of 3-tuples (weight, target type, data index)
      weight is a scalar
      target type is 'gram' or 'l2'
      data index is a zero-based index into the target tensor
    target_data[blob name] is a tensor (image x channel x height x width)
    tv_lambda and tv_beta are scalars for the total variation regularizer
    '''

    # Makes one iteration step and updates the gradient of the data blob

    get_data_blob(net).data[...] = np.reshape(x, get_data_blob(net).data.shape)
    get_data_blob(net).diff[...] = 0
    net.forward()

    loss = 0
    # Go through target blobs in reversed order
    for i in range(len(blob_names)):
        blob_i = len(blob_names) - 1 - i
        start = blob_names[blob_i]

        if blob_i == 0:
            end = None
        else:
            end = blob_names[blob_i - 1]

        # Get target blob
        target_blob = net.blobs[start]
        if i == 0:
            target_blob.diff[...] = 0
        gen_data = target_blob.data.copy()

        # Apply RELU
        pos_mask = gen_data > 0
        gen_data[~pos_mask] = 0

        # Accumulate objectives
        target_blob_add_diff = np.zeros_like(target_blob.diff,
                                             dtype=np.float64)
        for (weight, target_type, data_i) in target[start]:
            if target_type == 'gram':
                c_loss, c_grad = style_grad(gen_data,
                                            target_data[start][data_i])
            elif target_type == 'l2':
                c_loss, c_grad = content_grad(gen_data,
                                              target_data[start][data_i])
            else:
                raise ValueError('Unknown target type: {}'.format(target_type))

            # Apply RELU
            c_grad[~pos_mask] = 0
            target_blob_add_diff += c_grad * weight
            loss += c_loss * weight

        # Update model
        target_blob.diff[...] += target_blob_add_diff
        net.backward(start=start, end=end)

    if tv_lambda > 0:
        tv_loss, tv_grad = totalvariation.tv_norm(x.reshape(
            get_data_blob(net).data.shape),
                                                  beta=tv_beta)
        return loss + tv_loss * tv_lambda, np.ravel(
            get_data_blob(net).diff).astype(
                np.float64) + np.ravel(tv_grad) * tv_lambda
    else:
        return loss, np.ravel(get_data_blob(net).diff).astype(np.float64)
Example #5
0
def objective_func(x, net, all_target_blob_names, targets, target_data_list,
                   tv_lambda, tv_beta):
    # Makes one iteration step and updates the gradient of the data blob

    get_data_blob(net).data[...] = np.reshape(x, get_data_blob(net).data.shape)
    get_data_blob(net).diff[...] = 0
    net.forward()

    loss = 0
    # Go through target blobs in reversed order
    for i in range(len(all_target_blob_names)):
        blob_i = len(all_target_blob_names) - 1 - i
        start = all_target_blob_names[blob_i]

        if blob_i == 0:
            end = None
        else:
            end = all_target_blob_names[blob_i - 1]

        # Get target blob
        target_blob = net.blobs[start]
        if i == 0:
            target_blob.diff[...] = 0

        gen_data = target_blob.data.copy()
        # Apply RELU
        pos_mask = gen_data > 0
        gen_data[~pos_mask] = 0

        # Go through all images and compute accumulated gradient for the current target blob
        target_blob_add_diff = np.zeros_like(target_blob.diff,
                                             dtype=np.float64)
        for target_i, (_, target_blob_names, is_gram,
                       weight) in enumerate(targets):
            # Skip if the current blob is not among the target's blobs
            if start not in target_blob_names:
                continue

            target_data = target_data_list[target_i][start]
            if is_gram:
                c_loss, c_grad = style_grad(gen_data, target_data)
            else:
                c_loss, c_grad = content_grad(gen_data, target_data)

            # Apply RELU
            c_grad[~pos_mask] = 0
            target_blob_add_diff += c_grad * weight / len(target_blob_names)
            loss += c_loss * weight / len(target_blob_names)

        target_blob.diff[...] += target_blob_add_diff
        net.backward(start=start, end=end)

    if tv_lambda > 0:
        tv_loss, tv_grad = totalvariation.tv_norm(x.reshape(
            get_data_blob(net).data.shape),
                                                  beta=tv_beta)
        return loss + tv_loss * tv_lambda, np.ravel(
            get_data_blob(net).diff).astype(
                np.float64) + np.ravel(tv_grad) * tv_lambda
    else:
        return loss, np.ravel(get_data_blob(net).diff).astype(np.float64)