Failed precondition: Could not find variable Variable

Did tensorflow1.x support backward diffentiable?
Sorry, the question could be confusing.

I want to achieve following :

  1. I have trained a model , the input is X , and the out is Y. Now, I want to load this trained model and then input Y and optimizer the best X with the trained differentiable model. I call the process is ‘ik’
    and vice verse, fk.
    but the simple fk is not working, the error is following
C:\Users\raphaelmeng\.conda\envs\fdfd\python.exe E:/PycharmProjects/4dFaceOptimizerIK/approximation_v2.py
2022-09-22 22:24:55.363519: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-09-22 22:24:55.719180: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 7438 MB memory:  -> device: 0, name: NVIDIA GeForce RTX 3080, pci bus id: 0000:01:00.0, compute capability: 8.6
E:/PycharmProjects/4dFaceOptimizerIK/approximation_v2.py:71: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.
  approximationConfig = yaml.load(file)
C:\Users\raphaelmeng\.conda\envs\fdfd\lib\site-packages\keras\legacy_tf_layers\core.py:236: UserWarning: `tf.layers.dense` is deprecated and will be removed in a future version. Please use `tf.keras.layers.Dense` instead.
  warnings.warn('`tf.layers.dense` is deprecated and '
C:\Users\raphaelmeng\.conda\envs\fdfd\lib\site-packages\keras\engine\base_layer_v1.py:1676: UserWarning: `layer.apply` is deprecated and will be removed in a future version. Please use `layer.__call__` method instead.
  warnings.warn('`layer.apply` is deprecated and '
C:\Users\raphaelmeng\.conda\envs\fdfd\lib\site-packages\keras\legacy_tf_layers\convolutional.py:536: UserWarning: `tf.layers.conv2d` is deprecated and will be removed in a future version. Please Use `tf.keras.layers.Conv2D` instead.
  warnings.warn('`tf.layers.conv2d` is deprecated and '
len(dataset.usedUVs)= 3
len(dataset.usedVerts)= 3
E:/PycharmProjects/4dFaceOptimizerIK/approximation_v2.py:77: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.
  baseConfig = yaml.load(file)
type(imgMesh) = <class 'tensorflow.python.framework.ops.Tensor'>
**********
add linear!
len(dataset.usedUVs)= 5
len(dataset.usedVerts)= 5
type(imgMesh) = <class 'tensorflow.python.framework.ops.Tensor'>
**********
add linear!
Tensor("add_21:0", shape=(1, 23861, 3), dtype=float32)
<class 'tensorflow.python.framework.ops.Tensor'>
data['neutral'].shape= (24049, 3)
neutral.shape= (23861, 3)
type(final_refineMesh) = <class 'tensorflow.python.framework.ops.Tensor'>
[<tf.Variable 'Variable:0' shape=(1, 172) dtype=float32>, <tf.Variable 'refine/image-0/dense-0/dense/kernel:0' shape=(97, 256) dtype=float32>, <tf.Variable 'refine/image-0/dense-0/dense/bias:0' shape=(256,) dtype=float32>, <tf.Variable 'refine/image-0/dense-final/dense/kernel:0' shape=(256, 512) dtype=float32>, <tf.Variable 'refine/image-0/dense-final/dense/bias:0' shape=(512,) dtype=float32>, <tf.Variable 'refine/image-0/level-0/conv/conv2d/kernel:0' shape=(3, 3, 8, 16) dtype=float32>, <tf.Variable 'refine/image-0/level-0/conv/conv2d/bias:0' shape=(16,) dtype=float32>, <tf.Variable 'refine/image-0/level-1/conv/conv2d/kernel:0' shape=(3, 3, 16, 16) dtype=float32>, <tf.Variable 'refine/image-0/level-1/conv/conv2d/bias:0' shape=(16,) dtype=float32>, <tf.Variable 'refine/image-0/level-2/conv/conv2d/kernel:0' shape=(3, 3, 16, 16) dtype=float32>, <tf.Variable 'refine/image-0/level-2/conv/conv2d/bias:0' shape=(16,) dtype=float32>, <tf.Variable 'refine/image-0/level-extra/conv/conv2d/kernel:0' shape=(3, 3, 16, 16) dtype=float32>, <tf.Variable 'refine/image-0/level-extra/conv/conv2d/bias:0' shape=(16,) dtype=float32>, <tf.Variable 'refine/image-0/level-3/conv/conv2d/kernel:0' shape=(1, 1, 16, 3) dtype=float32>, <tf.Variable 'refine/image-0/level-3/conv/conv2d/bias:0' shape=(3,) dtype=float32>, <tf.Variable 'refine/image-1/dense-0/dense/kernel:0' shape=(123, 256) dtype=float32>, <tf.Variable 'refine/image-1/dense-0/dense/bias:0' shape=(256,) dtype=float32>, <tf.Variable 'refine/image-1/dense-final/dense/kernel:0' shape=(256, 512) dtype=float32>, <tf.Variable 'refine/image-1/dense-final/dense/bias:0' shape=(512,) dtype=float32>, <tf.Variable 'refine/image-1/level-0/conv/conv2d/kernel:0' shape=(3, 3, 8, 16) dtype=float32>, <tf.Variable 'refine/image-1/level-0/conv/conv2d/bias:0' shape=(16,) dtype=float32>, <tf.Variable 'refine/image-1/level-1/conv/conv2d/kernel:0' shape=(3, 3, 16, 16) dtype=float32>, <tf.Variable 'refine/image-1/level-1/conv/conv2d/bias:0' shape=(16,) dtype=float32>, <tf.Variable 'refine/image-1/level-2/conv/conv2d/kernel:0' shape=(3, 3, 16, 16) dtype=float32>, <tf.Variable 'refine/image-1/level-2/conv/conv2d/bias:0' shape=(16,) dtype=float32>, <tf.Variable 'refine/image-1/level-extra/conv/conv2d/kernel:0' shape=(3, 3, 16, 16) dtype=float32>, <tf.Variable 'refine/image-1/level-extra/conv/conv2d/bias:0' shape=(16,) dtype=float32>, <tf.Variable 'refine/image-1/level-3/conv/conv2d/kernel:0' shape=(1, 1, 16, 3) dtype=float32>, <tf.Variable 'refine/image-1/level-3/conv/conv2d/bias:0' shape=(3,) dtype=float32>, <tf.Variable 'refine/image-2/dense-0/dense/kernel:0' shape=(97, 256) dtype=float32>, <tf.Variable 'refine/image-2/dense-0/dense/bias:0' shape=(256,) dtype=float32>, <tf.Variable 'refine/image-2/dense-final/dense/kernel:0' shape=(256, 512) dtype=float32>, <tf.Variable 'refine/image-2/dense-final/dense/bias:0' shape=(512,) dtype=float32>, <tf.Variable 'refine/image-2/level-0/conv/conv2d/kernel:0' shape=(3, 3, 8, 16) dtype=float32>, <tf.Variable 'refine/image-2/level-0/conv/conv2d/bias:0' shape=(16,) dtype=float32>, <tf.Variable 'refine/image-2/level-1/conv/conv2d/kernel:0' shape=(3, 3, 16, 16) dtype=float32>, <tf.Variable 'refine/image-2/level-1/conv/conv2d/bias:0' shape=(16,) dtype=float32>, <tf.Variable 'refine/image-2/level-2/conv/conv2d/kernel:0' shape=(3, 3, 16, 16) dtype=float32>, <tf.Variable 'refine/image-2/level-2/conv/conv2d/bias:0' shape=(16,) dtype=float32>, <tf.Variable 'refine/image-2/level-extra/conv/conv2d/kernel:0' shape=(3, 3, 16, 16) dtype=float32>, <tf.Variable 'refine/image-2/level-extra/conv/conv2d/bias:0' shape=(16,) dtype=float32>, <tf.Variable 'refine/image-2/level-3/conv/conv2d/kernel:0' shape=(1, 1, 16, 3) dtype=float32>, <tf.Variable 'refine/image-2/level-3/conv/conv2d/bias:0' shape=(3,) dtype=float32>, <tf.Variable 'image-0/dense-0/dense/kernel:0' shape=(131, 256) dtype=float32>, <tf.Variable 'image-0/dense-0/dense/bias:0' shape=(256,) dtype=float32>, <tf.Variable 'image-0/dense-final/dense/kernel:0' shape=(256, 512) dtype=float32>, <tf.Variable 'image-0/dense-final/dense/bias:0' shape=(512,) dtype=float32>, <tf.Variable 'image-0/level-0/conv/conv2d/kernel:0' shape=(3, 3, 8, 16) dtype=float32>, <tf.Variable 'image-0/level-0/conv/conv2d/bias:0' shape=(16,) dtype=float32>, <tf.Variable 'image-0/level-1/conv/conv2d/kernel:0' shape=(3, 3, 16, 16) dtype=float32>, <tf.Variable 'image-0/level-1/conv/conv2d/bias:0' shape=(16,) dtype=float32>, <tf.Variable 'image-0/level-extra/conv/conv2d/kernel:0' shape=(3, 3, 16, 16) dtype=float32>, <tf.Variable 'image-0/level-extra/conv/conv2d/bias:0' shape=(16,) dtype=float32>, <tf.Variable 'image-0/level-2/conv/conv2d/kernel:0' shape=(1, 1, 16, 3) dtype=float32>, <tf.Variable 'image-0/level-2/conv/conv2d/bias:0' shape=(3,) dtype=float32>, <tf.Variable 'image-1/dense-0/dense/kernel:0' shape=(115, 256) dtype=float32>, <tf.Variable 'image-1/dense-0/dense/bias:0' shape=(256,) dtype=float32>, <tf.Variable 'image-1/dense-final/dense/kernel:0' shape=(256, 512) dtype=float32>, <tf.Variable 'image-1/dense-final/dense/bias:0' shape=(512,) dtype=float32>, <tf.Variable 'image-1/level-0/conv/conv2d/kernel:0' shape=(3, 3, 8, 16) dtype=float32>, <tf.Variable 'image-1/level-0/conv/conv2d/bias:0' shape=(16,) dtype=float32>, <tf.Variable 'image-1/level-1/conv/conv2d/kernel:0' shape=(3, 3, 16, 16) dtype=float32>, <tf.Variable 'image-1/level-1/conv/conv2d/bias:0' shape=(16,) dtype=float32>, <tf.Variable 'image-1/level-extra/conv/conv2d/kernel:0' shape=(3, 3, 16, 16) dtype=float32>, <tf.Variable 'image-1/level-extra/conv/conv2d/bias:0' shape=(16,) dtype=float32>, <tf.Variable 'image-1/level-2/conv/conv2d/kernel:0' shape=(1, 1, 16, 3) dtype=float32>, <tf.Variable 'image-1/level-2/conv/conv2d/bias:0' shape=(3,) dtype=float32>, <tf.Variable 'image-2/dense-0/dense/kernel:0' shape=(20, 256) dtype=float32>, <tf.Variable 'image-2/dense-0/dense/bias:0' shape=(256,) dtype=float32>, <tf.Variable 'image-2/dense-final/dense/kernel:0' shape=(256, 512) dtype=float32>, <tf.Variable 'image-2/dense-final/dense/bias:0' shape=(512,) dtype=float32>, <tf.Variable 'image-2/level-0/conv/conv2d/kernel:0' shape=(3, 3, 8, 16) dtype=float32>, <tf.Variable 'image-2/level-0/conv/conv2d/bias:0' shape=(16,) dtype=float32>, <tf.Variable 'image-2/level-1/conv/conv2d/kernel:0' shape=(3, 3, 16, 16) dtype=float32>, <tf.Variable 'image-2/level-1/conv/conv2d/bias:0' shape=(16,) dtype=float32>, <tf.Variable 'image-2/level-extra/conv/conv2d/kernel:0' shape=(3, 3, 16, 16) dtype=float32>, <tf.Variable 'image-2/level-extra/conv/conv2d/bias:0' shape=(16,) dtype=float32>, <tf.Variable 'image-2/level-2/conv/conv2d/kernel:0' shape=(1, 1, 16, 3) dtype=float32>, <tf.Variable 'image-2/level-2/conv/conv2d/bias:0' shape=(3,) dtype=float32>, <tf.Variable 'image-3/dense-0/dense/kernel:0' shape=(116, 256) dtype=float32>, <tf.Variable 'image-3/dense-0/dense/bias:0' shape=(256,) dtype=float32>, <tf.Variable 'image-3/dense-final/dense/kernel:0' shape=(256, 512) dtype=float32>, <tf.Variable 'image-3/dense-final/dense/bias:0' shape=(512,) dtype=float32>, <tf.Variable 'image-3/level-0/conv/conv2d/kernel:0' shape=(3, 3, 8, 16) dtype=float32>, <tf.Variable 'image-3/level-0/conv/conv2d/bias:0' shape=(16,) dtype=float32>, <tf.Variable 'image-3/level-1/conv/conv2d/kernel:0' shape=(3, 3, 16, 16) dtype=float32>, <tf.Variable 'image-3/level-1/conv/conv2d/bias:0' shape=(16,) dtype=float32>, <tf.Variable 'image-3/level-extra/conv/conv2d/kernel:0' shape=(3, 3, 16, 16) dtype=float32>, <tf.Variable 'image-3/level-extra/conv/conv2d/bias:0' shape=(16,) dtype=float32>, <tf.Variable 'image-3/level-2/conv/conv2d/kernel:0' shape=(1, 1, 16, 3) dtype=float32>, <tf.Variable 'image-3/level-2/conv/conv2d/bias:0' shape=(3,) dtype=float32>, <tf.Variable 'image-4/dense-0/dense/kernel:0' shape=(24, 256) dtype=float32>, <tf.Variable 'image-4/dense-0/dense/bias:0' shape=(256,) dtype=float32>, <tf.Variable 'image-4/dense-final/dense/kernel:0' shape=(256, 512) dtype=float32>, <tf.Variable 'image-4/dense-final/dense/bias:0' shape=(512,) dtype=float32>, <tf.Variable 'image-4/level-0/conv/conv2d/kernel:0' shape=(3, 3, 8, 16) dtype=float32>, <tf.Variable 'image-4/level-0/conv/conv2d/bias:0' shape=(16,) dtype=float32>, <tf.Variable 'image-4/level-1/conv/conv2d/kernel:0' shape=(3, 3, 16, 16) dtype=float32>, <tf.Variable 'image-4/level-1/conv/conv2d/bias:0' shape=(16,) dtype=float32>, <tf.Variable 'image-4/level-extra/conv/conv2d/kernel:0' shape=(3, 3, 16, 16) dtype=float32>, <tf.Variable 'image-4/level-extra/conv/conv2d/bias:0' shape=(16,) dtype=float32>, <tf.Variable 'image-4/level-2/conv/conv2d/kernel:0' shape=(1, 1, 16, 3) dtype=float32>, <tf.Variable 'image-4/level-2/conv/conv2d/bias:0' shape=(3,) dtype=float32>]
Traceback (most recent call last):
  File "C:\Users\raphaelmeng\.conda\envs\fdfd\lib\site-packages\tensorflow\python\client\session.py", line 1375, in _do_call
    return fn(*args)
  File "C:\Users\raphaelmeng\.conda\envs\fdfd\lib\site-packages\tensorflow\python\client\session.py", line 1360, in _run_fn
    target_list, run_metadata)
  File "C:\Users\raphaelmeng\.conda\envs\fdfd\lib\site-packages\tensorflow\python\client\session.py", line 1453, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.FailedPreconditionError: 2 root error(s) found.
  (0) Failed precondition: Could not find variable Variable. This could mean that the variable has been deleted. In TF1, it can also mean the variable is uninitialized. Debug info: container=localhost, status=Not found: Resource localhost/Variable/class tensorflow::Var does not exist.
	 [[{{node image-0/GatherV2/ReadVariableOp}}]]
	 [[strided_slice_54/_289]]
  (1) Failed precondition: Could not find variable Variable. This could mean that the variable has been deleted. In TF1, it can also mean the variable is uninitialized. Debug info: container=localhost, status=Not found: Resource localhost/Variable/class tensorflow::Var does not exist.
	 [[{{node image-0/GatherV2/ReadVariableOp}}]]
0 successful operations.
0 derived errors ignored.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:/PycharmProjects/4dFaceOptimizerIK/approximation_v2.py", line 166, in <module>
    verts0=approx.fk(conbtrol0)
  File "E:/PycharmProjects/4dFaceOptimizerIK/approximation_v2.py", line 146, in fk
    final_refineMesh=self.sess.run([self.final_refineMesh])
  File "C:\Users\raphaelmeng\.conda\envs\fdfd\lib\site-packages\tensorflow\python\client\session.py", line 968, in run
    run_metadata_ptr)
  File "C:\Users\raphaelmeng\.conda\envs\fdfd\lib\site-packages\tensorflow\python\client\session.py", line 1191, in _run
    feed_dict_tensor, options, run_metadata)
  File "C:\Users\raphaelmeng\.conda\envs\fdfd\lib\site-packages\tensorflow\python\client\session.py", line 1369, in _do_run
    run_metadata)
  File "C:\Users\raphaelmeng\.conda\envs\fdfd\lib\site-packages\tensorflow\python\client\session.py", line 1394, in _do_call
    raise type(e)(node_def, op, message)  # pylint: disable=no-value-for-parameter
tensorflow.python.framework.errors_impl.FailedPreconditionError: 2 root error(s) found.
  (0) Failed precondition: Could not find variable Variable. This could mean that the variable has been deleted. In TF1, it can also mean the variable is uninitialized. Debug info: container=localhost, status=Not found: Resource localhost/Variable/class tensorflow::Var does not exist.
	 [[node image-0/GatherV2/ReadVariableOp (defined at E:\PycharmProjects\4dFaceOptimizerIK\cnnModel\cnnModel.py:98) ]]
	 [[strided_slice_54/_289]]
  (1) Failed precondition: Could not find variable Variable. This could mean that the variable has been deleted. In TF1, it can also mean the variable is uninitialized. Debug info: container=localhost, status=Not found: Resource localhost/Variable/class tensorflow::Var does not exist.
	 [[node image-0/GatherV2/ReadVariableOp (defined at E:\PycharmProjects\4dFaceOptimizerIK\cnnModel\cnnModel.py:98) ]]
0 successful operations.
0 derived errors ignored.

Errors may have originated from an input operation.
Input Source operations connected to node image-0/GatherV2/ReadVariableOp:
 Variable (defined at E:/PycharmProjects/4dFaceOptimizerIK/approximation_v2.py:67)

Input Source operations connected to node image-0/GatherV2/ReadVariableOp:
 Variable (defined at E:/PycharmProjects/4dFaceOptimizerIK/approximation_v2.py:67)

Original stack trace for 'image-0/GatherV2/ReadVariableOp':
  File "E:/PycharmProjects/4dFaceOptimizerIK/approximation_v2.py", line 162, in <module>
    approx=Approximator(sess)
  File "E:/PycharmProjects/4dFaceOptimizerIK/approximation_v2.py", line 78, in __init__
    mesh = buildModel(baseConfig, rigControls)
  File "E:/PycharmProjects/4dFaceOptimizerIK/approximation_v2.py", line 58, in buildModel
    model = cnnModel.buildModel(data,dataset,neutral,config)
  File "E:\PycharmProjects\4dFaceOptimizerIK\cnnModel\cnnModel.py", line 98, in buildModel
    imageInput = tf.gather(pose,index,axis=1)
  File "C:\Users\raphaelmeng\.conda\envs\fdfd\lib\site-packages\tensorflow\python\util\deprecation.py", line 549, in new_func
    return func(*args, **kwargs)
  File "C:\Users\raphaelmeng\.conda\envs\fdfd\lib\site-packages\tensorflow\python\util\dispatch.py", line 206, in wrapper
    return target(*args, **kwargs)
  File "C:\Users\raphaelmeng\.conda\envs\fdfd\lib\site-packages\tensorflow\python\ops\array_ops.py", line 5052, in gather
    params, indices, axis, batch_dims=batch_dims, name=name)
  File "C:\Users\raphaelmeng\.conda\envs\fdfd\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 3821, in gather_v2
    batch_dims=batch_dims, name=name)
  File "C:\Users\raphaelmeng\.conda\envs\fdfd\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 522, in _apply_op_helper
    preferred_dtype=default_dtype)
  File "C:\Users\raphaelmeng\.conda\envs\fdfd\lib\site-packages\tensorflow\python\profiler\trace.py", line 163, in wrapped
    return func(*args, **kwargs)
  File "C:\Users\raphaelmeng\.conda\envs\fdfd\lib\site-packages\tensorflow\python\framework\ops.py", line 1566, in convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "C:\Users\raphaelmeng\.conda\envs\fdfd\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py", line 2023, in _dense_var_to_tensor
    return var._dense_var_to_tensor(dtype=dtype, name=name, as_ref=as_ref)  # pylint: disable=protected-access
  File "C:\Users\raphaelmeng\.conda\envs\fdfd\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py", line 1421, in _dense_var_to_tensor
    return self.value()
  File "C:\Users\raphaelmeng\.conda\envs\fdfd\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py", line 576, in value
    return self._read_variable_op()
  File "C:\Users\raphaelmeng\.conda\envs\fdfd\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py", line 683, in _read_variable_op
    result = read_and_set_handle()
  File "C:\Users\raphaelmeng\.conda\envs\fdfd\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py", line 674, in read_and_set_handle
    self.handle, self._dtype)
  File "C:\Users\raphaelmeng\.conda\envs\fdfd\lib\site-packages\tensorflow\python\ops\gen_resource_variable_ops.py", line 484, in read_variable_op
    "ReadVariableOp", resource=resource, dtype=dtype, name=name)
  File "C:\Users\raphaelmeng\.conda\envs\fdfd\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 750, in _apply_op_helper
    attrs=attr_protos, op_def=op_def)
  File "C:\Users\raphaelmeng\.conda\envs\fdfd\lib\site-packages\tensorflow\python\framework\ops.py", line 3569, in _create_op_internal
    op_def=op_def)
  File "C:\Users\raphaelmeng\.conda\envs\fdfd\lib\site-packages\tensorflow\python\framework\ops.py", line 2045, in __init__
    self._traceback = tf_stack.extract_stack_for_node(self._c_op)

My code is like follwing, note I simplify many details but retain the main code. I use tensorflow1.12

# Build the approximation model
import os
import pickle
import sys
from pathlib import Path

import numpy as np
import yaml
import tensorflow.compat.v1 as tf
from collections import namedtuple

sys.path.append(os.path.join('..','cnnModel'))
sys.path.append('..')
from cnnModel import cnnModel,rigidDeformer

def buildModel(config,pose,addNeutral=True):

    with open(config['data_params']['cache_file'],'rb') as file:
        data = pickle.load(file)
    cache = data
    parts = data['vCharts']
    faces = data['faces']
    neutral = data['neutral'][data['active']].astype('float32')
    uvs = data['uv']
    if 'parameter_mask' in data:
        mask = data['parameter_mask']
    else:
        mask = None

    # Create the model
    partCount = np.max(parts)+1
    data = {'pose':pose}
    usedVerts = []
    usedUVs = []
    for i in range(partCount):
        if np.sum(parts==i) > 0:
            data['image-'+str(i)] = tf.ones(1)
        else:
            data['image-'+str(i)] = None
        ref = faces.reshape(-1)
        idx = np.arange(len(neutral))[parts==i]
        if len(idx) == 0:
            continue
        usedFaces = [True if v in idx else False for v in ref]
        usedFaces = np.sum(np.asarray(usedFaces).reshape((-1,3)),-1) == 3
        faceIdx = np.arange(len(faces))[usedFaces]
        uv = uvs[idx]
        usedUVs.append(uv)
        usedVerts.append(idx)
    idx = np.concatenate(usedVerts)
    linear = np.zeros(neutral.shape,dtype='float32')
    if addNeutral:
        linear[idx] = neutral[idx]
    else:
        neutral = linear
    data['linear'] = linear
    dataset = namedtuple('Dataset','mask usedUVs usedVerts')(mask,usedUVs,usedVerts)
    model = cnnModel.buildModel(data,dataset,neutral,config)
    model['parts'] = parts
    model['cache'] = cache
    return model

class Approximator():

    def __init__(self,  sess):
        ####
        rigControls = tf.Variable(tf.zeros([1, 172], tf.float32))

        approximation_config = 'experiments/v00_refine_model_leaky.yaml'
        with open(approximation_config) as file:
            approximationConfig = yaml.load(file)
        with tf.variable_scope('refine'):
            refineMesh = buildModel(approximationConfig, rigControls, addNeutral=False)

        base_config = 'experiments/v00_base_model_leaky.yaml'
        with open(base_config) as file:
            baseConfig = yaml.load(file)
        mesh = buildModel(baseConfig, rigControls)

        have_refineMesh = True
        refineMesh['output'] = mesh['output'] + refineMesh['output']

        print(refineMesh['output'])
        print(type(refineMesh['output']))

        # Apply ridig deformer
        # Load info about the mesh
        with open(os.path.join(baseConfig['data_params']['cache_file']), 'rb') as file:
            data = pickle.load(file)
        parts = data['vCharts']
        neutral = data['neutral'][data['active']]
        print("data['neutral'].shape=", data['neutral'].shape)
        print('neutral.shape=', neutral.shape)
        faces = data['faces']
        mask = np.arange(len(parts))[parts > -1]

        if 'rigid_files' in baseConfig['data_params']:

            cur_rigidDeformer = rigidDeformer.RigidDeformer(neutral, [f for f in
                                                                      baseConfig['data_params']['rigid_files']], mask)
            final_base_mesh = cur_rigidDeformer.deformTF(mesh['output'][0])[np.newaxis]
            if have_refineMesh:
                final_refineMesh = cur_rigidDeformer.deformTF(refineMesh['output'][0])[np.newaxis]
        else:
            final_base_mesh = mesh['output']
            if have_refineMesh:
                final_refineMesh = refineMesh['output']

        # print('final_refineMesh.shape =', final_refineMesh.eval())
        print('type(final_refineMesh) =', type(final_refineMesh))

        ####
        vars = tf.trainable_variables()
        print(vars)
        vars_to_train = vars[0]
        vars_to_load = vars[1:]
        self.saver = tf.train.Saver(vars_to_load)

        checkpoint_dir = 'E:\\PycharmProjects\\FDFD-Metahuman-Y-up\\output\\v00_refine_model_leaky'
        self.checkpointFile = tf.train.latest_checkpoint(checkpoint_dir)
        self.saver.restore(sess, self.checkpointFile)

        self.rigControls=rigControls
        self.sess=sess
        self.final_refineMesh=final_refineMesh


    def fk(self,control):
        # vars = tf.trainable_variables()
        # print("after_vars =",vars)
        self.rigControls= control
        # print('self.rigControls=',self.rigControls)
        #
        # # init = tf.global_variables_initializer()
        # # self.sess.run(init)
        # vars = tf.trainable_variables()
        # print(vars)
        # vars_to_train = vars[0]
        # vars_to_load = vars[1:]
        # saver = tf.train.Saver(vars_to_load)
        #
        # checkpoint_dir = 'E:\\PycharmProjects\\FDFD-Metahuman-Y-up\\output\\v00_refine_model_leaky'
        # checkpointFile = tf.train.latest_checkpoint(checkpoint_dir)
        # saver.restore(sess, checkpointFile)

        final_refineMesh=self.sess.run([self.final_refineMesh])
        return final_refineMesh

    def ik(self,verts):
        pass








if __name__ == '__main__':
    # Load the model from file
    with tf.Session() as sess:
        approx=Approximator(sess)


        conbtrol0=tf.zeros([1, 172], tf.float32)
        verts0=approx.fk(conbtrol0)
        print('verts0=',verts0)

        conbtrol1 = tf.ones([1, 172], tf.float32)
        verts1 = approx.fk(conbtrol1)
        print('verts1=', verts1)