Example #1
0
  def get_variable_names(self):
    """Returns list of all variable names in this model.

    Returns:
      List of names.
    """
    return [name for name, _ in list_variables(self.model_dir)]
Example #2
0
 def init_from_file(cls, filename, vocab):
     var_shape = dict(list_variables(filename))
     try:
         embed_shape = var_shape['word_embedding/weights']
     except KeyError:
         raise VariableNotFoundException(variable='word_embedding/weights',
                                         where='file %s' % filename,
                                         msg='Try to initialize manually.')
     vocab_len, embed_len = embed_shape
     vocab_len = vocab_len - 1
     try:
         tree_lstm_weights_shape = var_shape[
             'tree_lstm_cell/fully_connected/weights']
     except KeyError:
         raise VariableNotFoundException(
             variable='tree_lstm_cell/fully_connected/weights',
             where='file %s' % filename,
             msg='Try to initialize manually.')
     tree_lstm_num_units = int(tree_lstm_weights_shape[1] / 5)
     assert tree_lstm_weights_shape[
         0] == embed_len + tree_lstm_num_units * 2
     if not vocab_len == len(vocab):
         raise RuntimeError(
             'Vocab used with saved model had a different size.\n \
                             Try to initialize manually\n \
                             Used vocab len: %d' % vocab_len)
     weights = np.zeros(embed_shape, dtype=np.float32)
     tree_lstm = cls(weights, vocab, tree_lstm_num_units)
     return tree_lstm
Example #3
0
  def _create_load_init_saver(self, filename):
    if self.load != "":
      return None
    if len(glob.glob(self.model_dir + self.model + "-*.index")) > 0:
      return None
    if filename == "" or filename.endswith(".pickle") or filename.startswith("DeepLabRGB:"):
      return None
    from tensorflow.contrib.framework import list_variables
    vars_and_shapes_file = [x for x in list_variables(filename) if x[0] != "global_step"]
    vars_file = [x[0] for x in vars_and_shapes_file]
    vars_to_shapes_file = {x[0]: x[1] for x in vars_and_shapes_file}
    vars_model = tf.global_variables()
    assert all([x.name.endswith(":0") for x in vars_model])
    vars_intersection = [x for x in vars_model if x.name[:-2] in vars_file]
    vars_missing_in_graph = [x for x in vars_model if x.name[:-2] not in vars_file and "Adam" not in x.name and
                             "beta1_power" not in x.name and "beta2_power" not in x.name]
    if len(vars_missing_in_graph) > 0:
      print("the following variables will not be initialized since they are not present in the initialization model",
            [v.name for v in vars_missing_in_graph], file=log.v1)

    var_names_model = [x.name for x in vars_model]
    vars_missing_in_file = [x for x in vars_file if x + ":0" not in var_names_model
                            and "RMSProp" not in x and "Adam" not in x and "Momentum" not in x]
    if len(vars_missing_in_file) > 0:
      print("the following variables will not be loaded from the file since they are not present in the graph",
            vars_missing_in_file, file=log.v1)

    vars_shape_mismatch = [x for x in vars_intersection if x.shape.as_list() != vars_to_shapes_file[x.name[:-2]]]
    if len(vars_shape_mismatch) > 0:
      print("the following variables will not be loaded from the file since the shapes in the graph and in the file "
            "don't match:", [(x.name, x.shape) for x in vars_shape_mismatch if "Adam" not in x.name], file=log.v1)
      vars_intersection = [x for x in vars_intersection if x not in vars_shape_mismatch]
    return tf.train.Saver(var_list=vars_intersection)
Example #4
0
    def get_variable_names(self):
        """Returns list of all variable names in this model.

        Returns:
            List of names.
        """
        return [name for name, _ in list_variables(self.model_dir)]
Example #5
0
def global_vars_init_or_restore(var_list=None):
    """
    For any scope added to RESTORABLE collection:
    variable will be restored from a checkpoint if it exists in the
    specified checkpoint and no scope ancestor can restore it.
    """
    if var_list is None:
        var_list = tf.global_variables()
    restorable = sorted(tf.get_collection(RESTORABLE), key=lambda x: x[0])
    restored_vars = {}
    for scope, checkpoint_name, checkpoint_scope in restorable:
        variables_in_scope = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                                               scope=scope)
        checkpoint_file = resolve_checkpoint(checkpoint_name)
        variables_in_file = [v for (v, _) in list_variables(checkpoint_file)]
        rename = lambda x: x.replace(scope, checkpoint_scope).replace(':0', '')
        to_restore = [
            v for v in variables_in_scope
            if v in var_list and rename(v.name) in variables_in_file
        ]
        for var in to_restore:
            if var in restored_vars:
                continue
            if '/' in rename(var.name):
                checkpoint_subscope, var_name = rename(var.name).rsplit('/', 1)
            else:
                checkpoint_subscope, var_name = None, rename(var.name)
            initializer = restore_initializer(checkpoint_name, var_name,
                                              checkpoint_subscope)
            restored_vars[var] = tf.assign(
                var, initializer(var.get_shape(), dtype=var.dtype))
    init_others = tf.variables_initializer(
        [v for v in var_list if v not in restored_vars])
    restore_op = tf.group(init_others, *list(restored_vars.values()))
    return restore_op
Example #6
0
 def _init_weights_file(self):
     if self._load_model == False:
         return
     
     if self._pretrain:
         self._base_layer_names = ["conv1_1_weights", "conv1_1_bias",
                                   "conv1_2_weights", "conv1_2_bias",
                                   "conv2_1_weights", "conv2_1_bias",
                                   "conv2_2_weights", "conv2_2_bias"]
     
     model_dir = "/home/scarab6/Desktop/gqcnn/models/gqcnn_example_pj"
     ckpt_file = os.path.join(model_dir, 'model.ckpt')
     
     with self.g.as_default():
         reader = tf.train.NewCheckpointReader(ckpt_file)
         ckpt_vars = tcf.list_variables(ckpt_file)
         full_var_names = []
         short_names = []
         self._weights = {}
         for variable, shape in ckpt_vars:
             full_var_names.append(variable)
             short_names.append(variable.split('/')[-1])
         # load variables
         if self._pretrain:
             for full_var_name, short_name in zip(full_var_names, short_names):
                 if short_name in self._base_layer_names:
                     self._weights[short_name] = tf.Variable(reader.get_tensor(full_var_name), name=full_var_name)
             return
         
         for full_var_name, short_name in zip(full_var_names, short_names):
             self._weights[short_name] = tf.Variable(reader.get_tensor(full_var_name), name=full_var_name)
Example #7
0
    def init_weights_file(self, ckpt_file):
        """ Initialize network weights from the specified model 

        Parameters
        ----------
        ckpt_file :obj: str
            Tensorflow checkpoint file from which to load model weights
        """
        with self._graph.as_default():
            # create new tf checkpoint reader
            reader = tf.train.NewCheckpointReader(ckpt_file)

            # create empty weight object
            self._weights = GQCNNWeights()

            # read/generate weight/bias variable names
            ckpt_vars = tcf.list_variables(ckpt_file)
            full_var_names = []
            short_names = []
            for variable, shape in ckpt_vars:
                full_var_names.append(variable)
                short_names.append(variable.split('/')[-1])

            # load variables
            for full_var_name, short_name in zip(full_var_names, short_names):
                self._weights.weights[short_name] = tf.Variable(
                    reader.get_tensor(full_var_name), name=full_var_name)
    def _read_weights_and_biases(self, model_dir):
        ckpt_file = model_dir + '/model.ckpt'

        reader = tf.train.NewCheckpointReader(ckpt_file)
        ckpt_vars = tcf.list_variables(ckpt_file)

        full_var_names = []
        short_names = []

        for variable, shape in ckpt_vars:
            full_var_names.append(variable)
            short_names.append(variable.split("/")[-1])
        # Load variables.
        self.w = []
        self.b = []

        with tf.Session() as sess:
            self.w.append(tf.Variable(reader.get_tensor('im_stream/conv1_1/conv1_1_weights')).initial_value.eval())
            self.w.append(tf.Variable(reader.get_tensor('im_stream/conv1_2/conv1_2_weights')).initial_value.eval())
            self.w.append(tf.Variable(reader.get_tensor('im_stream/conv2_1/conv2_1_weights')).initial_value.eval())
            self.w.append(tf.Variable(reader.get_tensor('im_stream/conv2_2/conv2_2_weights')).initial_value.eval())
            self.b.append(tf.Variable(reader.get_tensor('im_stream/conv1_1/conv1_1_bias')).initial_value.eval())
            self.b.append(tf.Variable(reader.get_tensor('im_stream/conv1_2/conv1_2_bias')).initial_value.eval())
            self.b.append(tf.Variable(reader.get_tensor('im_stream/conv2_1/conv2_1_bias')).initial_value.eval())
            self.b.append(tf.Variable(reader.get_tensor('im_stream/conv2_2/conv2_2_bias')).initial_value.eval())
            self.w.append(tf.Variable(reader.get_tensor('im_stream/fc3_weights')).initial_value.eval())
            self.b.append(tf.Variable(reader.get_tensor('im_stream/fc3_bias')).initial_value.eval())
            self.w.append(tf.Variable(reader.get_tensor('pose_stream/pc1_weights')).initial_value.eval())
            self.b.append(tf.Variable(reader.get_tensor('pose_stream/pc1_bias')).initial_value.eval())
            self.w.append(tf.Variable(reader.get_tensor('merge_stream/fc4_input_1_weights')).initial_value.eval())
            self.w.append(tf.Variable(reader.get_tensor('merge_stream/fc4_input_2_weights')).initial_value.eval())
            self.b.append(tf.Variable(reader.get_tensor('merge_stream/fc4_bias')).initial_value.eval())
            self.b.append(tf.Variable(reader.get_tensor('merge_stream/fc4_bias')).initial_value.eval())
            self.w.append(tf.Variable(reader.get_tensor('merge_stream/fc5_weights')).initial_value.eval())
            self.b.append(tf.Variable(reader.get_tensor('merge_stream/fc5_bias')).initial_value.eval())
Example #9
0
 def weights_(self):
     values = {}
     optimizer_regex = r".*/" + self._optimizer.get_name() + r"(_\d)?$"
     for name, _ in list_variables(self._model_dir):
         if (name.startswith("linear/") and name != "linear/bias_weight"
                 and not re.match(optimizer_regex, name)):
             values[name] = load_variable(self._model_dir, name)
     if len(values) == 1:
         return values[list(values.keys())[0]]
     return values
Example #10
0
def available_variables(checkpoint_dir):
    all_vars = tf.global_variables()
    all_available_vars = tff.list_variables(checkpoint_dir=checkpoint_dir)
    all_available_vars = dict(all_available_vars)
    available_vars = []
    for v in all_vars:
        vname = v.name.split(':')[0]
        if vname in all_available_vars and v.get_shape() == all_available_vars[vname]:
            available_vars.append(v)
    return available_vars
Example #11
0
 def weights_(self):
   values = {}
   optimizer_regex = r".*/"+self._optimizer.get_name() + r"(_\d)?$"
   for name, _ in list_variables(self._model_dir):
     if (name.startswith("linear/") and
         name != "linear/bias_weight" and
         not re.match(optimizer_regex, name)):
       values[name] = load_variable(self._model_dir, name)
   if len(values) == 1:
     return values[list(values.keys())[0]]
   return values
Example #12
0
 def _create_load_init_saver(self):
   if self.load_init != "" and not self.load_init.endswith(".pickle"):
     vars_file = [x[0] for x in list_variables(self.load_init)]
     vars_model = tf.global_variables()
     assert all([x.name.endswith(":0") for x in vars_model])
     vars_intersection = [x for x in vars_model if x.name[:-2] in vars_file]
     vars_missing = [x for x in vars_model if x.name[:-2] not in vars_file]
     if len(vars_missing) > 0:
       print >> log.v1, "the following variables will not be initialized since they are not present in the " \
                        "initialization model", [v.name for v in vars_missing]
     return tf.train.Saver(var_list=vars_intersection)
   else:
     return None
Example #13
0
def available_variables_without_global_step(checkpoint_dir):
    import tensorflow.contrib.framework as tff
    all_vars = tf.global_variables()
    all_available_vars = tff.list_variables(checkpoint_dir=checkpoint_dir)
    all_available_vars = dict(all_available_vars)
    available_vars = []
    for v in all_vars:
        vname = v.name.split(':')[0]
        if vname == 'global_step':
            continue
        if vname in all_available_vars and v.get_shape() == all_available_vars[vname]:
            available_vars.append(v)
    return available_vars
Example #14
0
    def read_model(self, step=-1):
        sess = tf.InteractiveSession()

        sess.run(tf.global_variables_initializer())

        ckpt = self.ckpt_step(step)

        if tf.train.checkpoint_exists(ckpt):
            names = set([a for a, _ in list_variables(ckpt)])
            vars = [v for v in tf.global_variables() if v.op.name in names]
            saver = tf.train.Saver(vars)
            saver.restore(sess, ckpt)

        return sess
Example #15
0
    def init_weights_file(self, ckpt_file):
        """ Load network weights from the given checkpoint file """
        raise NotImplementedError('This functionality has not yet been tested')

        with self._graph.as_default():
            ckpt_reader = tf.train.NewCheckpointReader(ckpt_file)
            self._weights = SiameseWeights()

            ckpt_vars = tcf.list_variables(ckpt_file)
            full_var_names = []
            short_var_names = []
            for full_variable_name, _ in ckpt_vars:
                full_var_names.append(full_variable_names)
                short_var_names.append(full_variable_name.split('/')[-1])

            for full_variable_name, short_variable_name in zip(full_var_names):
                self._weights.weights[short_variable_name] = tf.Variable(
                    ckpt_reader.get_tensor(full_variable_name))
  def get_weights(self, model_dir):
    """Returns weights per feature of the linear part.

    Args:
      model_dir: Directory where model parameters, graph and etc. are saved.

    Returns:
      The weights created by this model (without the optimizer weights).
    """
    all_variables = [name for name, _ in list_variables(model_dir)]
    values = {}
    optimizer_regex = r".*/" + self._get_optimizer().get_name() + r"(_\d)?$"
    for name in all_variables:
      if (name.startswith(self._scope + "/") and
          name != self._scope + "/bias_weight" and
          not re.match(optimizer_regex, name)):
        values[name] = load_variable(model_dir, name)
    if len(values) == 1:
      return values[list(values.keys())[0]]
    return values
Example #17
0
  def get_weights(self, model_dir):
    """Returns weights per feature of the linear part.

    Args:
      model_dir: Directory where model parameters, graph and etc. are saved.

    Returns:
      The weights created by this model (without the optimizer weights).
    """
    all_variables = [name for name, _ in list_variables(model_dir)]
    values = {}
    optimizer_regex = r".*/" + self._get_optimizer().get_name() + r"(_\d)?$"
    for name in all_variables:
      if (name.startswith(self._scope + "/") and
          name != self._scope + "/bias_weight" and
          not re.match(optimizer_regex, name)):
        values[name] = load_variable(model_dir, name)
    if len(values) == 1:
      return values[list(values.keys())[0]]
    return values
Example #18
0
 def init_from_file(cls,filename,tree_lstm):
     var_shape = dict(list_variables(filename))
     try:
         output_layer_weights_shape = var_shape['output_layer/weights']
     except KeyError:
         raise VariableNotFoundException(variable='output_layer/weights',
                                         where='file %s' % filename,
                                         msg='Try to initialize manually.')
     try:
         sent_cell_weights_shape = var_shape['sent_cell/weights']
     except KeyError:
         raise VariableNotFoundException(variable='sent_cell/weights',
                                         where='file %s' % filename,
                                         msg='Try to initialize manually.')
     num_classes = int(output_layer_weights_shape[1])
     sent_lstm_num_units = int(sent_cell_weights_shape[1]/4)
     assert sent_lstm_num_units == output_layer_weights_shape[0]
     if  not (sent_cell_weights_shape[0] ==
         sent_lstm_num_units + tree_lstm.tree_lstm_num_units*2):
         raise RuntimeError('Saved model and tree lstm not compatible.')
     model = cls(tree_lstm, sent_lstm_num_units, num_classes)
     return model
Example #19
0
    def set_base_network(self, model_dir):
        """ Initialize network weights from the base model.
        Useful for fine-tuning

        Parameters
        ----------
        model_dir : str
            path to the base model weights
        """
        # check architecture
        if 'base_model' not in self._architecture.keys():
            logging.warning(
                'Architecuture has no base model. The network has not been modified'
            )
            return False
        base_model_config = self._architecture['base_model']
        output_layer = base_model_config['output_layer']

        # read model
        ckpt_file = os.path.join(model_dir, 'model.ckpt')
        config_file = os.path.join(model_dir, 'architecture.json')
        base_arch = json.load(open(config_file, 'r'),
                              object_pairs_hook=OrderedDict)

        # read base layer names
        self._base_layer_names = []
        found_base_layer = False
        use_legacy = not ('im_stream' in base_arch.keys())
        if use_legacy:
            layer_iter = iter(base_arch)
            while not found_base_layer:
                layer_name = layer_iter.next()
                self._base_layer_names.append(layer_name)
                if layer_name == output_layer:
                    found_base_layer = True
        else:
            stream_iter = iter(base_arch)
            while not found_base_layer:
                stream_name = stream_iter.next()
                stream_arch = base_arch[stream_name]
                layer_iter = iter(stream_arch)
                stop = False
                while not found_base_layer and not stop:
                    try:
                        layer_name = layer_iter.next()
                        self._base_layer_names.append(layer_name)
                        if layer_name == output_layer:
                            found_base_layer = True
                    except StopIteration:
                        stop = True

        with self._graph.as_default():
            # create new tf checkpoint reader
            reader = tf.train.NewCheckpointReader(ckpt_file)

            # create empty weights object
            self._weights = GQCNNWeights()

            # read/generate weight/bias variable names
            ckpt_vars = tcf.list_variables(ckpt_file)
            full_var_names = []
            short_names = []
            for variable, shape in ckpt_vars:
                full_var_names.append(variable)
                short_names.append(variable.split('/')[-1])

            # load variables
            for full_var_name, short_name in zip(full_var_names, short_names):
                # check valid weights
                layer_name = weight_name_to_layer_name(short_name)

                # add weights
                if layer_name in self._base_layer_names:
                    self._weights.weights[short_name] = tf.Variable(
                        reader.get_tensor(full_var_name), name=full_var_name)
Example #20
0
def main():

    args = parse_args()
    BATCH_SIZE = 10

    train_data, train_labels, train_weights = load_pascal(args.data_dir,
                                                          split='trainval')
    eval_data, eval_labels, eval_weights = load_pascal(args.data_dir,
                                                       split='test')

    pascal_classifier = tf.estimator.Estimator(model_fn=partial(
        cnn_model_fn, num_classes=eval_labels.shape[1]),
                                               model_dir="./alexnet_models/")
    # Evaluate the model and print results
    eval_input_fn = tf.estimator.inputs.numpy_input_fn(x={
        "x": eval_data,
        "w": eval_weights
    },
                                                       y=eval_labels,
                                                       num_epochs=1,
                                                       shuffle=False)

    # Train the model
    train_input_fn = tf.estimator.inputs.numpy_input_fn(x={
        "x": train_data,
        "w": train_weights
    },
                                                        y=train_labels,
                                                        batch_size=BATCH_SIZE,
                                                        num_epochs=None,
                                                        shuffle=True)
    for given_iter in range(0, 5):
        if given_iter == 0:
            pascal_classifier.train(input_fn=train_input_fn, steps=1)
            print([
                name for name, _ in list_variables(pascal_classifier.model_dir)
            ])
            weights = load_variable(pascal_classifier.model_dir,
                                    'conv2d/kernel')
            f, axrr = plt.subplots(16, 6)
            for i in range(0, 96):
                im = Image.fromarray(
                    (weights[:, :, :, i] * 255).astype('uint8')).resize(
                        (50, 50)).convert('LA')
                axrr[int(i / 6)][int(i % 6)].axis('off')
                axrr[int(i / 6)][int(i % 6)].imshow(im)
            plt.axis('off')
            f.savefig('conv2d_' + str(given_iter) + '.png')
        elif given_iter == 2 or given_iter == 1:
            pascal_classifier.train(input_fn=train_input_fn, steps=10000)
            weights = load_variable(pascal_classifier.model_dir,
                                    'conv2d/kernel')
            f, axarr = plt.subplots(16, 6)
            for i in range(0, 96):
                im = Image.fromarray(
                    (weights[:, :, :, i] * 255).astype('uint8')).resize(
                        (50, 50)).convert('LA')
                axrr[int(i / 6)][int(i % 6)].axis('off')
                axrr[int(i / 6)][int(i % 6)].imshow(im)
            plt.axis('off')
            f.savefig('conv2d_' + str(given_iter) + '.png')

        else:
            pascal_classifier.train(input_fn=train_input_fn, steps=10000)
    weights = load_variable(pascal_classifier.model_dir, 'conv2d/kernel')
    f, axarr = plt.subplots(16, 6)
    for i in range(0, 96):
        im = Image.fromarray(
            (weights[:, :, :, i] * 255).astype('uint8')).resize(
                (50, 50)).convert('LA')
        axrr[int(i / 6)][int(i % 6)].axis('off')
        axrr[int(i / 6)][int(i % 6)].imshow(im)
    plt.axis('off')
    f.savefig('conv2d_final.png')
Example #21
0
 def get_variable_names(self):
     return [name for name, _ in list_variables(self._model_dir)]
Example #22
0
    def reload_network(self, ckpt_file=None):
        if ckpt_file:
            # Load what you want the initialisation to be, weight_file is the path './bvlc_alexnet.npy'
            # print('Loading weights from {0}'.format(weight_file))
            reader = tf.train.NewCheckpointReader(ckpt_file)

            # Create empty weight object.
            weights = {}

            # Read/generate weight/bias variable names.
            ckpt_vars = tcf.list_variables(ckpt_file)
            full_var_names = []
            short_names = []
            for variable, shape in ckpt_vars:
                full_var_names.append(variable)
                short_names.append(variable.split("/")[-1])

            # Load variables.
            for full_var_name, short_name in zip(full_var_names, short_names):
                weights[short_name] = tf.Variable(
                    reader.get_tensor(full_var_name), name=full_var_name)

            self.conv1W = weights["conv1W"]
            self.conv1b = weights["conv1b"]
            self.conv2W = weights["conv2W"]
            self.conv2b = weights["conv2b"]
            self.conv3W = weights["conv3W"]
            self.conv3b = weights["conv3b"]
            self.conv4W = weights["conv4W"]
            self.conv4b = weights["conv4b"]
            self.conv5W = weights["conv5W"]
            self.conv5b = weights["conv5b"]
            self.fc6W = weights["fc6W"]
            self.fc6b = weights["fc6b"]
            self.fc7W = weights["fc7W"]
            self.fc7b = weights["fc7b"]
            self.fc8W = weights["fc8W"]
            self.fc8b = weights["fc8b"]
        else:
            conv1W_init = tf.truncated_normal([11, 11, 3, 96], stddev=0.01)
            conv1b_init = tf.constant(0.1, shape=[96])
            conv2W_init = tf.truncated_normal([5, 5, 48, 256], stddev=0.01)
            conv2b_init = tf.constant(0.1, shape=[256])
            conv3W_init = tf.truncated_normal([3, 3, 256, 384], stddev=0.01)
            conv3b_init = tf.constant(0.1, shape=[384])
            conv4W_init = tf.truncated_normal([3, 3, 192, 384], stddev=0.01)
            conv4b_init = tf.constant(0.1, shape=[384])
            conv5W_init = tf.truncated_normal([3, 3, 192, 256], stddev=0.01)
            conv5b_init = tf.constant(0.1, shape=[256])
            fc6W_init = tf.truncated_normal([9216, 4096], stddev=0.01)
            fc6b_init = tf.constant(0.1, shape=[4096])
            fc7W_init = tf.truncated_normal([4096, 1024], stddev=0.01)
            fc7b_init = tf.constant(0.1, shape=[1024])
            fc8W_init = tf.truncated_normal([1024, self.num_classes],
                                            stddev=0.01)
            fc8b_init = tf.constant(0.1, shape=[self.num_classes])
            self.conv1W = tf.Variable(conv1W_init,
                                      trainable=False,
                                      name='conv1W')
            self.conv1b = tf.Variable(conv1b_init,
                                      trainable=False,
                                      name='conv1b')
            self.conv2W = tf.Variable(conv2W_init,
                                      trainable=False,
                                      name='conv2W')
            self.conv2b = tf.Variable(conv2b_init,
                                      trainable=False,
                                      name='conv2b')
            self.conv3W = tf.Variable(conv3W_init,
                                      trainable=False,
                                      name='conv3W')
            self.conv3b = tf.Variable(conv3b_init,
                                      trainable=False,
                                      name='conv3b')
            self.conv4W = tf.Variable(conv4W_init,
                                      trainable=False,
                                      name='conv4W')
            self.conv4b = tf.Variable(conv4b_init,
                                      trainable=False,
                                      name='conv4b')
            self.conv5W = tf.Variable(conv5W_init,
                                      trainable=False,
                                      name='conv5W')
            self.conv5b = tf.Variable(conv5b_init,
                                      trainable=False,
                                      name='conv5b')
            self.fc6W = tf.Variable(fc6W_init, name='fc6W')
            self.fc6b = tf.Variable(fc6b_init, name='fc6b')
            self.fc7W = tf.Variable(fc7W_init, name='fc7W')
            self.fc7b = tf.Variable(fc7b_init, name='fc7b')
            self.fc8W = tf.Variable(fc8W_init, name='fc8W')
            self.fc8b = tf.Variable(fc8b_init, name='fc8b')

        self.dropfc6 = 1
        self.dropfc7 = 1
Example #23
0
    def _read_weights_and_biases(self):
        ckpt_file = GQCNN_MODEL + '/model.ckpt'
        if tf.__version__ == '2.4.1':
            reader = tf.compat.v1.train.NewCheckpointReader(ckpt_file)
            ckpt_vars = tf.train.list_variables(ckpt_file)
        else:
            reader = tf.train.NewCheckpointReader(ckpt_file)
            ckpt_vars = tcf.list_variables(ckpt_file)

        full_var_names = []
        short_names = []

        for variable, shape in ckpt_vars:
            full_var_names.append(variable)
            short_names.append(variable.split("/")[-1])
        # Load variables.

        if tf.__version__ != '2.4.1':
            with tf.Session() as sess:
                self.w1_1 = tf.Variable(
                    reader.get_tensor('im_stream/conv1_1/conv1_1_weights')
                ).initial_value.eval()
                self.w1_2 = tf.Variable(
                    reader.get_tensor('im_stream/conv1_2/conv1_2_weights')
                ).initial_value.eval()
                self.w2_1 = tf.Variable(
                    reader.get_tensor('im_stream/conv2_1/conv2_1_weights')
                ).initial_value.eval()
                self.w2_2 = tf.Variable(
                    reader.get_tensor('im_stream/conv2_2/conv2_2_weights')
                ).initial_value.eval()
                self.b1_1 = tf.Variable(
                    reader.get_tensor('im_stream/conv1_1/conv1_1_bias')
                ).initial_value.eval()
                self.b1_2 = tf.Variable(
                    reader.get_tensor('im_stream/conv1_2/conv1_2_bias')
                ).initial_value.eval()
                self.b2_1 = tf.Variable(
                    reader.get_tensor('im_stream/conv2_1/conv2_1_bias')
                ).initial_value.eval()
                self.b2_2 = tf.Variable(
                    reader.get_tensor('im_stream/conv2_2/conv2_2_bias')
                ).initial_value.eval()
                self.w_3 = tf.Variable(
                    reader.get_tensor(
                        'im_stream/fc3_weights')).initial_value.eval()
                self.b_3 = tf.Variable(reader.get_tensor(
                    'im_stream/fc3_bias')).initial_value.eval()
                self.w_41 = tf.Variable(
                    reader.get_tensor('merge_stream/fc4_input_1_weights')
                ).initial_value.eval()
                self.w_42 = tf.Variable(
                    reader.get_tensor('merge_stream/fc4_input_2_weights')
                ).initial_value.eval()
                self.b_4 = tf.Variable(
                    reader.get_tensor(
                        'merge_stream/fc4_bias')).initial_value.eval()
                self.w_5 = tf.Variable(
                    reader.get_tensor(
                        'merge_stream/fc5_weights')).initial_value.eval()
                self.b_5 = tf.Variable(
                    reader.get_tensor(
                        'merge_stream/fc5_bias')).initial_value.eval()
                self.w_p1 = tf.Variable(
                    reader.get_tensor(
                        'pose_stream/pc1_weights')).initial_value.eval()
                self.b_p1 = tf.Variable(
                    reader.get_tensor(
                        'pose_stream/pc1_bias')).initial_value.eval()
        else:
            if 'im' in full_var_names[0]:
                self.w1_1 = tf.Variable(reader.get_tensor(
                    full_var_names[1])).numpy()
                self.w1_2 = tf.Variable(reader.get_tensor(
                    full_var_names[3])).numpy()
                self.w2_1 = tf.Variable(reader.get_tensor(
                    full_var_names[5])).numpy()
                self.w2_2 = tf.Variable(reader.get_tensor(
                    full_var_names[7])).numpy()
                self.b1_1 = tf.Variable(reader.get_tensor(
                    full_var_names[0])).numpy()
                self.b1_2 = tf.Variable(reader.get_tensor(
                    full_var_names[2])).numpy()
                self.b2_1 = tf.Variable(reader.get_tensor(
                    full_var_names[4])).numpy()
                self.b2_2 = tf.Variable(reader.get_tensor(
                    full_var_names[6])).numpy()
                self.w_3 = tf.Variable(reader.get_tensor(
                    full_var_names[9])).numpy()
                self.b_3 = tf.Variable(reader.get_tensor(
                    full_var_names[8])).numpy()
                self.w_41 = tf.Variable(reader.get_tensor(
                    full_var_names[11])).numpy()
                self.w_42 = tf.Variable(reader.get_tensor(
                    full_var_names[12])).numpy()
                self.b_4 = tf.Variable(reader.get_tensor(
                    full_var_names[10])).numpy()
                self.w_5 = tf.Variable(reader.get_tensor(
                    full_var_names[14])).numpy()
                self.b_5 = tf.Variable(reader.get_tensor(
                    full_var_names[13])).numpy()
                self.w_p1 = tf.Variable(reader.get_tensor(
                    full_var_names[16])).numpy()
                self.b_p1 = tf.Variable(reader.get_tensor(
                    full_var_names[15])).numpy()
            else:
                self.w1_1 = tf.Variable(reader.get_tensor(
                    full_var_names[0])).numpy()
                self.w1_2 = tf.Variable(reader.get_tensor(
                    full_var_names[2])).numpy()
                self.w2_1 = tf.Variable(reader.get_tensor(
                    full_var_names[4])).numpy()
                self.w2_2 = tf.Variable(reader.get_tensor(
                    full_var_names[6])).numpy()
                self.b1_1 = tf.Variable(reader.get_tensor(
                    full_var_names[1])).numpy()
                self.b1_2 = tf.Variable(reader.get_tensor(
                    full_var_names[3])).numpy()
                self.b2_1 = tf.Variable(reader.get_tensor(
                    full_var_names[5])).numpy()
                self.b2_2 = tf.Variable(reader.get_tensor(
                    full_var_names[7])).numpy()
                self.w_3 = tf.Variable(reader.get_tensor(
                    full_var_names[8])).numpy()
                self.b_3 = tf.Variable(reader.get_tensor(
                    full_var_names[9])).numpy()
                self.w_41 = tf.Variable(reader.get_tensor(
                    full_var_names[10])).numpy()
                self.w_42 = tf.Variable(reader.get_tensor(
                    full_var_names[11])).numpy()
                self.b_4 = tf.Variable(reader.get_tensor(
                    full_var_names[12])).numpy()
                self.w_5 = tf.Variable(reader.get_tensor(
                    full_var_names[13])).numpy()
                self.b_5 = tf.Variable(reader.get_tensor(
                    full_var_names[14])).numpy()
                self.w_p1 = tf.Variable(reader.get_tensor(
                    full_var_names[15])).numpy()
                self.b_p1 = tf.Variable(reader.get_tensor(
                    full_var_names[16])).numpy()
        self.w_4 = np.hstack((self.w_41.T, self.w_42.T)).T
Example #24
0
 def get_variable_names(self):
   return [name for name, _ in list_variables(self._model_dir)]