示例#1
0
    def validation(self, batch_size):
        with tf.name_scope('LDNN'):
            with tf.device('/cpu:0'):
                valid_batch = read_validationset(self.SET['validation'], batch_size,self.MEAN,self.STD,self.LABEL_MODE)
                handle = tf.placeholder(tf.string, shape=[])
                iterator = tf.data.Iterator.from_string_handle(handle, valid_batch.output_types,
                                                               valid_batch.output_shapes)
                X, Y, seq = iterator.get_next()
            mask_zero_frames = tf.cast(tf.not_equal(Y, -1), tf.int32)
            mask_padding = tf.cast(tf.not_equal(Y, 0), tf.int32)
            mask_negative = tf.cast(tf.not_equal(Y, 2), tf.int32)
            # convert 2(-1) to 0
            Y = Y * mask_negative

            seq = tf.reshape(seq, [batch_size])  # original sequence length, only used for RNN
            valid_iterator = valid_batch.make_initializable_iterator()
            logits, update_op, reset_op = MultiRNN(X, batch_size, seq, self.NUM_CLASSES,
                                         self.NUM_LSTM, self.NUM_HIDDEN, self.OUTPUT_KEEP_PROB,
                                         self.NUM_MLP, self.NUM_NEURON, training=False)
            if self.LABEL_MODE == 'block':
                w = get_scenes_weight(-1, self.VAL_FOLD)# block-based
            else:
                w = get_scenes_weight_instant(self.SCENES, self.VAL_FOLD)
            with tf.variable_scope('loss'):
                loss_op = tf.nn.weighted_cross_entropy_with_logits(tf.cast(Y, tf.float32), logits, tf.constant(w,dtype=tf.float32))
                # number of frames without zero_frame
                counted_non_zeros = tf.cast(tf.reduce_sum(mask_zero_frames), tf.float32)
                # eliminate zero_frame loss
                loss_op = tf.reduce_sum(loss_op * tf.cast(mask_zero_frames, tf.float32)) / counted_non_zeros

            predicted = tf.to_int32(tf.sigmoid(logits) > self.OUTPUT_THRESHOLD)
            # mask padding, zero_frames part
            TP = tf.count_nonzero(predicted * Y * mask_zero_frames * mask_padding)
            TN = tf.count_nonzero((predicted - 1) * (Y - 1) * mask_zero_frames* mask_padding)
            FP = tf.count_nonzero(predicted * (Y - 1) * mask_zero_frames* mask_padding)
            FN = tf.count_nonzero((predicted - 1) * Y * mask_zero_frames* mask_padding)
            precision = TP / (TP + FP)
            recall = TP / (TP + FN)
            f1 = 2 * precision * recall / (precision + recall)
            # TPR = TP/(TP+FN)
            sensitivity = recall
            specificity = TN / (TN + FP)
            # New performance measurement:
            #  return [batch_size,1, performance(13 classes)]
            TP1 = tf.count_nonzero(predicted * Y * mask_zero_frames * mask_padding,axis=1)

            TN1 = tf.count_nonzero((predicted - 1) * (Y - 1) * mask_zero_frames* mask_padding,axis=1)

            FP1 = tf.count_nonzero(predicted * (Y - 1) * mask_zero_frames* mask_padding,axis=1)

            FN1 = tf.count_nonzero((predicted - 1) * Y * mask_zero_frames* mask_padding,axis=1)
            return valid_iterator, sensitivity, specificity, f1, reset_op,handle,TP1,TN1,FP1,FN1,loss_op
示例#2
0
    def train(self, batch_size):
        with tf.name_scope('LDNN'):
            with tf.device('/cpu:0'):
                # teosorflow error solution: Cannot create a tensor proto whose content is larger than 2GB\
                numpy_path = np.array(self.SET['train'])
                self.path_placeholder = tf.placeholder(numpy_path.dtype,
                                                       numpy_path.shape)
                train_batch = read_trainset(self.path_placeholder,
                                            self.BATCH_SIZE, self.MEAN,
                                            self.STD)
                handle = tf.placeholder(tf.string, shape=[])
                iterator = tf.data.Iterator.from_string_handle(
                    handle, train_batch.output_types,
                    train_batch.output_shapes)
                X, Y, seq = iterator.get_next()
            # get mask matrix
            mask_zero_frames = tf.cast(tf.not_equal(Y, -1), tf.int32)

            seq = tf.reshape(
                seq,
                [batch_size])  # original sequence length, only used for RNN

            train_iterator = train_batch.make_initializable_iterator()
            logits, update_op, _ = MultiRNN(X,
                                            batch_size,
                                            seq,
                                            self.NUM_CLASSES,
                                            self.NUM_LSTM,
                                            self.NUM_HIDDEN,
                                            self.OUTPUT_KEEP_PROB,
                                            self.NUM_MLP,
                                            self.NUM_NEURON,
                                            training=True)

            # Get weight for weighted cross entory
            w = get_scenes_weight(self.SCENES, self.VAL_FOLD)

            # Define loss and optimizer
            with tf.variable_scope('loss'):
                loss_op = tf.nn.weighted_cross_entropy_with_logits(
                    tf.cast(Y, tf.float32), logits, tf.constant(w))
                # number of frames without zero_frame
                counted_non_zeros = tf.cast(tf.reduce_sum(mask_zero_frames),
                                            tf.float32)
                # eliminate zero_frame loss
                loss_op = tf.reduce_sum(loss_op * tf.cast(
                    mask_zero_frames, tf.float32)) / counted_non_zeros
                # L2
                # for unreg in [tf_var.name for tf_var in tf.trainable_variables() if
                #               ("bias" in tf_var.name)]:
                #     print(unreg)
                l2 = self.LAMBDA_L2 * sum(
                    tf.nn.l2_loss(tf_var)
                    for tf_var in tf.trainable_variables()
                    if not ("bias" in tf_var.name))
                loss_op += l2
            with tf.variable_scope('optimize'):
                optimizer = tf.train.AdamOptimizer(
                    learning_rate=self.LEARNING_RATE)
                # train_op = optimizer.minimize(loss_op)
                gradients, variables = zip(
                    *optimizer.compute_gradients(loss_op))
                gradients, _ = tf.clip_by_global_norm(gradients,
                                                      self.MAX_GRAD_NORM)
                train_op = optimizer.apply_gradients(zip(gradients, variables))
            with tf.name_scope("train_accuracy"):
                # add a threshold to round the output to 0 or 1
                # logits is already being sigmoid
                predicted = tf.to_int32(
                    tf.sigmoid(logits) > self.OUTPUT_THRESHOLD)
                TP = tf.count_nonzero(predicted * Y * mask_zero_frames)
                # mask padding, zero_frame,
                TN = tf.count_nonzero(
                    (predicted - 1) * (Y - 1) * mask_zero_frames)
                FP = tf.count_nonzero(predicted * (Y - 1) * mask_zero_frames)
                FN = tf.count_nonzero((predicted - 1) * Y * mask_zero_frames)
                precision = TP / (TP + FP)
                recall = TP / (TP + FN)
                f1 = 2 * precision * recall / (precision + recall)
                # TPR = TP/(TP+FN)
                sensitivity = recall
                specificity = TN / (TN + FP)
            return train_iterator, loss_op, train_op, sensitivity, specificity, f1, update_op, handle, TP, TN, FP, FN
示例#3
0
    def validation(self, batch_size):
        with tf.name_scope('LDNN'):
            with tf.device('/cpu:0'):
                valid_batch = read_validationset(self.SET['validation'],
                                                 batch_size)
                handle = tf.placeholder(tf.string, shape=[])
                iterator = tf.data.Iterator.from_string_handle(
                    handle, valid_batch.output_types,
                    valid_batch.output_shapes)
                X, Y, seq = iterator.get_next()
            mask_zero_frames = tf.cast(tf.not_equal(Y, -1), tf.int32)
            mask_padding = tf.cast(tf.not_equal(Y, 0), tf.int32)
            mask_negative = tf.cast(tf.not_equal(Y, 2), tf.int32)
            # convert 2(-1) to 0
            Y = Y * mask_negative

            seq = tf.reshape(
                seq,
                [batch_size])  # original sequence length, only used for RNN
            valid_iterator = valid_batch.make_initializable_iterator()
            logits, update_op, reset_op = MultiRNN(X,
                                                   batch_size,
                                                   seq,
                                                   self.NUM_CLASSES,
                                                   self.NUM_LSTM,
                                                   self.NUM_HIDDEN,
                                                   self.OUTPUT_KEEP_PROB,
                                                   self.NUM_MLP,
                                                   self.NUM_NEURON,
                                                   training=False)

            predicted = tf.to_int32(tf.sigmoid(logits) > self.OUTPUT_THRESHOLD)
            # mask padding, zero_frames part
            TP = tf.count_nonzero(predicted * Y * mask_zero_frames *
                                  mask_padding)
            TN = tf.count_nonzero(
                (predicted - 1) * (Y - 1) * mask_zero_frames * mask_padding)
            FP = tf.count_nonzero(predicted * (Y - 1) * mask_zero_frames *
                                  mask_padding)
            FN = tf.count_nonzero(
                (predicted - 1) * Y * mask_zero_frames * mask_padding)
            precision = TP / (TP + FP)
            recall = TP / (TP + FN)
            f1 = 2 * precision * recall / (precision + recall)
            # TPR = TP/(TP+FN)
            sensitivity = recall
            specificity = TN / (TN + FP)
            # New performance measurement:
            #  return [batch_size,1, performance(13 classes)]
            TP1 = tf.count_nonzero(predicted * Y * mask_zero_frames *
                                   mask_padding,
                                   axis=1)

            TN1 = tf.count_nonzero(
                (predicted - 1) * (Y - 1) * mask_zero_frames * mask_padding,
                axis=1)

            FP1 = tf.count_nonzero(predicted * (Y - 1) * mask_zero_frames *
                                   mask_padding,
                                   axis=1)

            FN1 = tf.count_nonzero(
                (predicted - 1) * Y * mask_zero_frames * mask_padding, axis=1)
            return valid_iterator, sensitivity, specificity, f1, reset_op, handle, TP1, TN1, FP1, FN1