machine learning - Loading custom weights for tensorflow layers -


i trained dnn in theano due issues, switched tensorflow. converted weights theano tensorflow format. built same architecture in tensorflow in theano. how initialize weights of layers weight file have on disk. base architecture:

input_layer = keras.layers.inputlayer(input_shape=(224,224,3),input_tensor=features)  # conv block 1 conv1_1 = tf.layers.conv2d(inputs=input_layer,                             filters=64, kernel_size=[3,3],                             padding='same',                             activation=tf.nn.relu,                            name='conv1_1')  conv1_2 = tf.layers.conv2d(inputs=conv1_1,                             filters=64, kernel_size=[3,3],                             padding='same',                             activation=tf.nn.relu,                            name='conv1_2')  pool1 = tf.layers.max_pooling2d(inputs=conv1_2,                                 pool_size=(2,2),                                  strides=(2,2),                                  name='pool1')   # conv block 2 conv2_1 = tf.layers.conv2d(inputs=pool1,                             filters=128, kernel_size=[3,3],                             padding='same',                             activation=tf.nn.relu,                            name='conv2_1')  conv2_2 = tf.layers.conv2d(inputs=conv2_1,                             filters=128, kernel_size=[3,3],                             padding='same',                             activation=tf.nn.relu,                            name='conv2_2')  pool2 = tf.layers.max_pooling2d(inputs=conv2_2,                                 pool_size=(2,2),                                  strides=(2,2),                                  name='pool2')  # conv block 3 conv3_1 = tf.layers.conv2d(inputs=pool2,                             filters=256, kernel_size=[3,3],                             padding='same',                             activation=tf.nn.relu,                            name='conv3_1')  conv3_2 = tf.layers.conv2d(inputs=conv3_1,                             filters=256, kernel_size=[3,3],                             padding='same',                             activation=tf.nn.relu,                            name='conv3_2')  conv3_3 = tf.layers.conv2d(inputs=conv3_2,                             filters=256, kernel_size=[3,3],                             padding='same',                             activation=tf.nn.relu,                            name='conv3_3')  pool3 = tf.layers.max_pooling2d(inputs=conv3_3,                                 pool_size=(2,2),                                  strides=(2,2),                                  name='pool3') 

how load weights these layers weight file have on disk? please help

there many different ways accomplish this. i'd easiest way export weight (parameter) matrices , bias vectors arrays using np.savez

for instance, can build dictionary , add arrays

params = {} ...  params['fc1/weights'] = this_weight_matrix params['fc1/biases'] = this_bias_vector ...  np.savez('model_weights', **params) 

then, setup tensorflow graph; example connected layer wrapper function:

def fc_layer(input_tensor, n_output_units, name,              activation_fn=none, seed=none,              weight_params=none, bias_params=none):      tf.variable_scope(name):          if weight_params not none:             weights = tf.variable(weight_params, name='weights',                                   dtype=tf.float32)         else:             weights = tf.variable(tf.truncated_normal(                 shape=[input_tensor.get_shape().as_list()[-1], n_output_units],                     mean=0.0,                     stddev=0.1,                     dtype=tf.float32,                     seed=seed),                 name='weights',)          if bias_params not none:             biases = tf.variable(bias_params, name='biases',                                   dtype=tf.float32)          else:             biases = tf.variable(tf.zeros(shape=[n_output_units]),                                  name='biases',                                   dtype=tf.float32)          act = tf.matmul(input_tensor, weights) + biases          if activation_fn not none:             act = activation_fn(act)      return act 

next, load parameters saved disk python session:

param_dict = np.load('model_weigths.npz') 

then, when setup actual graph (using previous wrapper function), follows:

g = tf.graph() g.as_default():     fc1 = fc_layer(input_tensor=tf_x,                     n_output_units=n_hidden_1,                     name='fc1',                    weight_params=fixed_params['fc1/weights'],                     bias_params=fixed_params['fc1/biases'],                    activation_fn=tf.nn.relu) ...  

Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -