def load_weights(self, net_params): """ Function to load weights from the C++ implementation into TensorFlow Parameters ---------- net_params: Dictionary Dict with weights from the C++ implementation and its names """ for key in net_params.keys(): if key in self.weights.keys(): if key.startswith('conv'): net_params[key] = _utils.convert_conv1d_coreml_to_tf(net_params[key]) self.sess.run(_tf.assign(_tf.get_default_graph().get_tensor_by_name(key+":0"), net_params[key])) elif key.startswith('dense'): net_params[key] = _utils.convert_dense_coreml_to_tf(net_params[key]) self.sess.run(_tf.assign(_tf.get_default_graph().get_tensor_by_name(key+":0"), net_params[key] )) elif key in self.biases.keys(): self.sess.run(_tf.assign(_tf.get_default_graph().get_tensor_by_name(key+":0"), net_params[key])) h2h_i_bias = net_params['lstm_h2h_i_bias'] h2h_c_bias = net_params['lstm_h2h_c_bias'] h2h_f_bias = net_params['lstm_h2h_f_bias'] h2h_o_bias = net_params['lstm_h2h_o_bias'] lstm_bias = _utils.convert_lstm_bias_coreml_to_tf(h2h_i_bias, h2h_c_bias, h2h_f_bias, h2h_o_bias) self.sess.run(_tf.assign(_tf.get_default_graph().get_tensor_by_name('rnn/lstm_cell/bias:0'), lstm_bias))
def __init__( self, net_params, batch_size, num_features, num_classes, prediction_window, seq_len, seed, ): _utils.suppress_tensorflow_warnings() self.num_classes = num_classes self.batch_size = batch_size tf = _lazy_import_tensorflow() keras = tf.keras ############################################# # Define the Neural Network ############################################# inputs = keras.Input(shape=(prediction_window * seq_len, num_features)) # First dense layer dense = keras.layers.Conv1D( filters=CONV_H, kernel_size=(prediction_window), padding='same', strides=prediction_window, use_bias=True, activation='relu', ) cur_outputs = dense(inputs) # First dropout layer dropout = keras.layers.Dropout( rate=0.2, seed=seed, ) cur_outputs = dropout(cur_outputs) # LSTM layer lstm = keras.layers.LSTM( units=LSTM_H, return_sequences=True, use_bias=True, ) cur_outputs = lstm(cur_outputs) # Second dense layer dense2 = keras.layers.Dense(DENSE_H) cur_outputs = dense2(cur_outputs) # Batch norm layer batch_norm = keras.layers.BatchNormalization() cur_outputs = batch_norm(cur_outputs) # ReLU layer relu = keras.layers.ReLU() cur_outputs = relu(cur_outputs) # Final dropout layer dropout = keras.layers.Dropout(rate=0.5, seed=seed) cur_outputs = dropout(cur_outputs) # Final dense layer dense3 = keras.layers.Dense(num_classes, use_bias=False) cur_outputs = dense3(cur_outputs) # Softmax layer softmax = keras.layers.Softmax() cur_outputs = softmax(cur_outputs) self.model = keras.Model(inputs=inputs, outputs=cur_outputs) self.model.compile(loss=tf.losses.categorical_crossentropy, optimizer=keras.optimizers.Adam(learning_rate=1e-3), sample_weight_mode="temporal") ############################################# # Load the Weights of the Neural Network ############################################# for key in net_params.keys(): net_params[key] = _utils.convert_shared_float_array_to_numpy( net_params[key]) # Set weight for first dense layer l = self.model.layers[1] l.set_weights( (_utils.convert_conv1d_coreml_to_tf(net_params["conv_weight"]), net_params["conv_bias"])) # Set LSTM weights i2h, h2h, bias = [], [], [] for i in ('i', 'f', 'c', 'o'): i2h.append(eval('net_params["lstm_i2h_%s_weight"]' % i)) h2h.append(eval('net_params["lstm_h2h_%s_weight"]' % i)) bias.append(eval('net_params["lstm_h2h_%s_bias"]' % i)) i2h = _np.concatenate(i2h, axis=0) h2h = _np.concatenate(h2h, axis=0) bias = _np.concatenate(bias, axis=0) i2h = _np.swapaxes(i2h, 1, 0) h2h = _np.swapaxes(h2h, 1, 0) l = self.model.layers[3] l.set_weights((i2h, h2h, bias)) # Set weight for second dense layer l = self.model.layers[4] l.set_weights( (net_params['dense0_weight'].reshape(DENSE_H, LSTM_H).swapaxes(0, 1), net_params['dense0_bias'])) # Set batch Norm weights l = self.model.layers[5] l.set_weights( (net_params['bn_gamma'], net_params['bn_beta'], net_params['bn_running_mean'], net_params['bn_running_var'])) # Set weights for last dense layer l = self.model.layers[8] l.set_weights((net_params['dense1_weight'].reshape( (self.num_classes, DENSE_H)).swapaxes(0, 1), ))
def init_activity_classifier_graph( self, net_params, num_features, prediction_window, seed ): # Vars self.data = _tf.placeholder( _tf.float32, [None, prediction_window * self.seq_len, num_features] ) self.weight = _tf.placeholder(_tf.float32, [None, self.seq_len, 1]) self.target = _tf.placeholder(_tf.int32, [None, self.seq_len, 1]) self.is_training = _tf.placeholder(_tf.bool) # Reshaping weights reshaped_weight = _tf.reshape(self.weight, [self.batch_size, self.seq_len]) # One hot encoding target reshaped_target = _tf.reshape(self.target, [self.batch_size, self.seq_len]) one_hot_target = _tf.one_hot(reshaped_target, depth=self.num_classes, axis=-1) # Weights self.weights = { "conv_weight": _tf.Variable( _utils.convert_conv1d_coreml_to_tf(net_params["conv_weight"]), shape=[prediction_window, num_features, CONV_H], name="conv_weight" ), "dense0_weight": _tf.Variable( _utils.convert_dense_coreml_to_tf(net_params["dense0_weight"]), shape=[LSTM_H, DENSE_H], name="dense0_weight" ), "dense1_weight": _tf.Variable( _utils.convert_dense_coreml_to_tf(net_params["dense1_weight"]), shape=[DENSE_H, self.num_classes], name="dense1_weight" ), } # Biases self.biases = { "conv_bias": _tf.Variable( net_params["conv_bias"], shape=[CONV_H], name="conv_bias"), "dense0_bias": _tf.Variable( net_params["dense0_bias"], shape=[DENSE_H], name="dense0_bias"), } # Convolution conv = _tf.nn.conv1d( self.data, self.weights["conv_weight"], stride=prediction_window, padding="SAME", ) conv = _tf.nn.bias_add(conv, self.biases["conv_bias"]) conv = _tf.nn.relu(conv) dropout = _tf.layers.dropout( conv, rate=0.2, training=self.is_training, seed=seed ) # Long Short Term Memory lstm = self._get_lstm_weights_params(net_params) cells = _tf.nn.rnn_cell.LSTMCell( num_units=LSTM_H, reuse=_tf.AUTO_REUSE, forget_bias=0.0, initializer=_tf.initializers.constant(lstm, verify_shape=True), ) init_state = cells.zero_state(self.batch_size, _tf.float32) rnn_outputs, _ = _tf.nn.dynamic_rnn( cells, dropout, initial_state=init_state ) # Dense dense = _tf.reshape(rnn_outputs, (-1, LSTM_H)) dense = _tf.add( _tf.matmul(dense, self.weights["dense0_weight"]), self.biases["dense0_bias"] ) dense = _tf.layers.batch_normalization( inputs=dense, beta_initializer=_tf.initializers.constant( net_params["bn_beta"], verify_shape=True ), gamma_initializer=_tf.initializers.constant( net_params["bn_gamma"], verify_shape=True ), moving_mean_initializer=_tf.initializers.constant( net_params["bn_running_mean"], verify_shape=True ), moving_variance_initializer=_tf.initializers.constant( net_params["bn_running_var"], verify_shape=True ), training=self.is_training, ) dense = _tf.nn.relu(dense) dense = _tf.layers.dropout( dense, rate=0.5, training=self.is_training, seed=seed ) # Output out = _tf.matmul(dense, self.weights["dense1_weight"]) out = _tf.reshape(out, (-1, self.seq_len, self.num_classes)) self.probs = _tf.nn.softmax(out) # Weights seq_sum_weights = _tf.reduce_sum(reshaped_weight, axis=1) binary_seq_sum_weights = _tf.reduce_sum( _tf.cast(seq_sum_weights > 0, dtype=_tf.float32) ) # Loss loss = _tf.losses.softmax_cross_entropy( logits=out, onehot_labels=one_hot_target, weights=reshaped_weight, reduction=_tf.losses.Reduction.NONE, ) self.loss_per_seq = _tf.reduce_sum(loss, axis=1) / (seq_sum_weights + 1e-5) self.loss_op = _tf.reduce_sum(self.loss_per_seq) / ( binary_seq_sum_weights + 1e-5 ) # Optimizer update_ops = _tf.get_collection(_tf.GraphKeys.UPDATE_OPS) self.set_learning_rate(1e-3) train_op = self.optimizer.minimize(self.loss_op) self.train_op = _tf.group([train_op, update_ops]) # Initialize all variables self.sess.run(_tf.global_variables_initializer()) self.sess.run(_tf.local_variables_initializer()) self._load_lstm_biases(net_params)