ValueError:没有为任何变量提供渐变:

时间:2018-06-16 10:59:19

标签: python tensorflow deep-learning depth resnet

我试图在conv5层或使用CVR转换的resnet50模块上微调monodepth模型(https://github.com/mrharicot/monodepth)。在更新我的渐变时,我收到错误说明

ValueError: No gradients provided for any variable: ['<tensorflow.python.training.optimizer._RefVariableProcessor object at 0x7f8f00022f60>', '<tensorflow.python.training.optimizer._RefVariableProcessor object at 0x7f8f0002cfd0>', '<tensorflow.python.training.optimizer._RefVariableProcessor object at 0x7f8f00037b38>', '<tensorflow.python.training.optimizer._RefVariableProcessor object at 0x7f8f00037eb8>', '<tensorflow.python.training.optimizer._RefVariableProcessor object at 0x7f8f00037438>', '<tensorflow.python.training.optimizer._RefVariableProcessor object at 0x7f8f000377b8>', '<tensorflow.python.training.optimizer._RefVariableProcessor object at 0x7f8f00037f28>', '<tensorflow.python.training.optimizer._RefVariableProcessor object at 0x7f8f00037e48>', '<tensorflow.python.training.optimizer._RefVariableProcessor object at 0x7f8f00037f60>', '<tensorflow.python.training.optimizer._RefVariableProcessor object at 0x7f8f00037f98>', '<tensorflow.python.training.optimizer._RefVariableProcessor object at 0x7f8f00037fd0>', '<tensorflow.python.training.optimizer._RefVariableProcessor object at 0x7f8f0003d0b8>', '<tensorflow.python.training.optimizer._RefVariableProcessor object at 0x7f8f0003d048>', '<tensorflow.python.training.optimizer._RefVariableProcessor object at 0x7f8f0003d080>', '<tensorflow.python.training.optimizer._RefVariableProcessor object at 0x7f8f0003d2e8>', '<tensorflow.python.training.optimizer._RefVariableProcessor object at 0x7f8f0003d358>', '<tensorflow.python.training.optimizer._RefVariableProcessor object at 0x7f8f0003d278>', '<tensorflow.python.training.optimizer._RefVariableProcessor object at 0x7f8f0003d400>', '<tensorflow.python.training.optimizer._RefVariableProcessor object at 0x7f8f0003d710>', '<tensorflow.python.training.optimizer._RefVariableProcessor object at 0x7f8f0003d588>', '<tensorflow.python.training.optimizer._RefVariableProcessor object at 0x7f8f0003db00>', '<tensorflow.python.training.optimizer._RefVariableProcessor object at 0x7f8f0003db70>', '<tensorflow.python.training.optimizer._RefVariableProcessor object at 0x7f8f0003dba8>', '<tensorflow.python.training.optimizer._RefVariableProcessor object at 0x7f8f0003d6a0>']

我正在尝试在最大深度点应用CVR变换。然后用可用的地面实况计算预测深度的损失(l2)。在应用渐变时,我得到上面给出的错误。任何人都可以告诉我哪里出错了?

代码:

width_to_focal = dict()
width_to_focal[1242] = 721.5377
width_to_focal[1241] = 718.856
width_to_focal[1224] = 707.0493
width_to_focal[1238] = 718.3351
width_to_focal[1226] = 707.0912

def post_process_disparity(disp):
    _, h, w = disp.shape
    l_disp = disp[0,:,:]
    r_disp = np.fliplr(disp[1,:,:])
    m_disp = 0.5 * (l_disp + r_disp)
    l, _ = np.meshgrid(np.linspace(0, 1, w), np.linspace(0, 1, h))
    l_mask = 1.0 - np.clip(20 * (l - 0.05), 0, 1)
    r_mask = np.fliplr(l_mask)
    return r_mask * l_disp + l_mask * r_disp + (1.0 - l_mask - r_mask) * m_disp

def count_text_lines(file_path):
    f = open(file_path, 'r')
    lines = f.readlines()
    f.close()
    return len(lines)

def point_encoder_simple(depth_batch):

    index = np.zeros((2))
    index[:] = unravel_index(depth_batch[:,:].argmin(), depth_batch[:,:].shape)
    index[:] = index[::-1]

    return index

def train(params):
    """Training loop."""

    # with tf.Graph().as_default(), tf.device('/cpu:0'):
    with tf.Graph().as_default():

        global_step = tf.Variable(0, trainable=False)

        # INPUT IMAGE PATH
        path = '/home/krishna/datasets/'

        # GT IMAGE PATH
        f1 = tuple(open('/home/krishna/kitti/file_lists/kitti/kitti_gt.txt','r'))
        gt_image_path = []
        for line in f1:
            gt_image_path.append(path + line[:-1])


        # OPTIMIZER
        num_training_samples = count_text_lines(args.filenames_file)

        num_total_steps = params.num_epochs * num_training_samples
        start_learning_rate = args.learning_rate

        boundaries = [np.int32((3/5) * num_total_steps), np.int32((4/5) * num_total_steps)]
        values = [args.learning_rate, args.learning_rate / 2, args.learning_rate / 4]
        learning_rate = tf.train.piecewise_constant(global_step, boundaries, values)

        opt_step = tf.train.AdamOptimizer(learning_rate)

        print("total number of samples: {}".format(num_training_samples))
        print("total number of steps: {}".format(num_total_steps))

        dataloader_test = MonodepthDataloader(args.data_path, args.filenames_file, params, args.dataset, 'test')
        left_test  = dataloader_test.left_image_batch
        right_test = dataloader_test.right_image_batch

        model_test = MonodepthModel(params, 'test', left_test, right_test)

        # SESSION
        os.environ["CUDA_VISIBLE_DEVICES"]="0"
        config = tf.ConfigProto(allow_soft_placement=True)
        config.gpu_options.allow_growth = True
        sess = tf.Session(config=config)

        # SAVER
        summary_writer = tf.summary.FileWriter(args.log_directory + '/' + args.model_name, sess.graph)
        train_saver = tf.train.Saver()

        # COUNT PARAMS
        total_num_parameters = 0
        for variable in tf.trainable_variables(scope='model/encoder'):
            total_num_parameters += np.array(variable.get_shape().as_list()).prod()
        print("number of trainable parameters: {}".format(total_num_parameters))


        a = tf.trainable_variables(scope='model/encoder')
        variable_list = a[106:]


        # INIT
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        coordinator = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coordinator)

        # LOAD CHECKPOINT IF SET
        if args.checkpoint_path != '':
            train_saver.restore(sess, args.checkpoint_path.split(".")[0])

            if args.retrain:
                sess.run(global_step.assign(0))



        print('now testing {} files'.format(num_training_samples))
        disparities_pp = np.zeros((num_training_samples, params.height, params.width), dtype=np.float32)
        pred_after_tf_disparities_pp = np.zeros((num_training_samples, params.height, params.width), dtype=np.float32)

        # LOSS and GRAD INTIALIZER
        tower_grads  = []
        tower_losses = []
        reuse_variables = True
        with tf.variable_scope(tf.get_variable_scope()):

            with tf.device('/gpu:0'):

                for step in range(num_training_samples):

                    # if (step+1)%10==0:
                    print ('Iteration : ', step+1)


                    # reading gt image
                    gt_disp  = cv2.imread(gt_image_path[step],-1)
                    original_height,original_width= gt_disp.shape
                    gt_disp = gt_disp.astype(np.float32) / 256

                    # running the model initially
                    disp = sess.run(model_test.disp_left_est[0])
                    disparities_pp[step] = post_process_disparity(disp.squeeze())

                    # resized output
                    disparities_pp_resize = cv2.resize(disparities_pp[step].squeeze(), (original_width, original_height))

                    # index of max depth
                    index_simple = point_encoder_simple(disparities_pp_resize)
                    # print (index_simple)

                    # INITIALIZING CVR PARAMETERS
                    input_ph = tf.placeholder(tf.float32, shape=[None, original_height, original_width, 3], name='input_image')
                    input_ph_inv = tf.placeholder(tf.float32, shape=[None, original_height, original_width, 1], name='input_image')
                    fovea_tf = tf.placeholder(tf.float32, shape=[None, 2], name='fovea_tf') ## foveaX, foveaY
                    alpha_tf = tf.placeholder(tf.float32, shape=[None, 2], name='alpha_tf') ## alphaX, alphaY
                    transformed_cvr_batch = cvr_forward(input_ph, fovea_tf, alpha_tf, False, original_width, original_height)
                    inverse_batch = cvr_inverse(input_ph_inv, fovea_tf, alpha_tf, False, original_width, original_height)

                    # initiatizing fovea and scaling factor
                    alpha_np = np.zeros((1,2))
                    fovea_np = np.zeros((1,2))
                    alpha_np[0,:] = [0.003,0.003]
                    fovea_np[0,:] = index_simple[::]

                    # input rgb for cvr transform
                    image = tf.image.resize_bilinear(left_test, (original_height, original_width))
                    input_img = tf.cast(image,np.float32).eval(session=sess)

                    # cvr transform
                    after_transform = sess.run(transformed_cvr_batch,feed_dict={input_ph:input_img,alpha_tf:alpha_np,fovea_tf:fovea_np})

                    # input to model (cvr images)
                    input_image_ = cv2.resize(after_transform[0,:,:,:].squeeze(), (512, 256))
                    input_image_ = input_image_.astype(np.float32) / 255
                    input_images = np.stack((input_image_, np.fliplr(input_image_)), 0)

                    # running model for the CVR transformed image
                    pred_after_tf = sess.run(model_test.disp_left_est[0], feed_dict={left_test: input_images})
                    pred_after_tf_disparities_pp[step] = post_process_disparity(pred_after_tf.squeeze())

                    # input for inverse cvr
                    pred_after_tf_disparities_pp_expanded = np.expand_dims(pred_after_tf_disparities_pp,axis=3)
                    pred_after_tf_disparities_pp_resize = tf.image.resize_bilinear(pred_after_tf_disparities_pp_expanded, (original_height, original_width))
                    output_image = tf.cast(pred_after_tf_disparities_pp_resize,np.float32).eval(session=sess)

                    # Inverse CVR transform of prediction
                    after_inv_transform = sess.run(inverse_batch,feed_dict={input_ph_inv:output_image,alpha_tf:alpha_np,fovea_tf:fovea_np})

                    ow = tf.cast(after_inv_transform[step,:,:,0], tf.float32)
                    pred_disp = tf.scalar_mul(original_width,ow)
                    mask = gt_disp > 0

                    gt = tf.add(gt_disp, tf.subtract(1.0,mask))
                    gt_depth = tf.multiply(width_to_focal[original_width], tf.divide(0.54,gt))
                    pred_depth = tf.multiply(width_to_focal[original_width], tf.divide(0.54,pred_disp))

                    pred_depth_masked = tf.multiply(pred_depth,mask)

                    gt_depth = tf.multiply(gt_depth,mask)

                    loss = tf.nn.l2_loss(tf.subtract(gt_depth,pred_depth_masked))
                    # print (loss)
                    tower_losses.append(loss)
                    reuse_variables = True
                    grads = opt_step.compute_gradients(loss, var_list = variable_list)
                    tf.summary.scalar('loss', loss, ['model_0'])
                    # print (grads)
                    apply_gradient_op = opt_step.apply_gradients(grads, global_step=global_step)
                    tower_grads.append(grads)

0 个答案:

没有答案