示例#1
0
    def test_procedure(self):
        confusion_matrics = np.zeros([self.num_class, self.num_class],
                                     dtype="int")
        test_loss = 0.0

        tst_batch_num = int(np.ceil(self.test_data[0].shape[0] / self.bs))
        for step in range(tst_batch_num):
            _testImg = self.test_data[0][step * self.bs:step * self.bs +
                                         self.bs]
            _testLab = self.test_data[1][step * self.bs:step * self.bs +
                                         self.bs]

            [matrix_row, matrix_col
             ], tmp_loss = self.sess.run([self.distribution, self.loss],
                                         feed_dict={
                                             self.x: _testImg,
                                             self.y: _testLab,
                                             self.is_training: False
                                         })
            for m, n in zip(matrix_row, matrix_col):
                confusion_matrics[m][n] += 1

            test_loss += tmp_loss

        test_accuracy = float(
            np.sum([confusion_matrics[q][q] for q in range(self.num_class)
                    ])) / float(np.sum(confusion_matrics))
        test_loss = test_loss / tst_batch_num
        detail_test_accuracy = [
            confusion_matrics[i][i] / np.sum(confusion_matrics[i])
            for i in range(self.num_class)
        ]
        log1 = "Test Accuracy : %g" % test_accuracy
        log0 = "Test Loss : %g" % test_loss
        log2 = np.array(confusion_matrics.tolist())
        log3 = ''
        for j in range(self.num_class):
            log3 += 'category %s test accuracy : %g\n' % (
                utils.pulmonary_category[j], detail_test_accuracy[j])
        log4 = 'F_Value : %g' % self.f_value(confusion_matrics)

        utils.save2file(log1, self.ckptDir, self.model)
        utils.save2file(log0, self.ckptDir, self.model)
        utils.save2file(log2, self.ckptDir, self.model)
        utils.save2file(log3, self.ckptDir, self.model)
        utils.save2file(log4, self.ckptDir, self.model)

        return test_accuracy, test_loss
示例#2
0
    def train(self):
        self.epoch_plt = []
        self.training_accuracy_plt = []
        self.test_accuracy_plt = []
        self.training_loss_plt = []
        self.test_loss_plt = []

        self.sess.run(tf.global_variables_initializer())
        self.train_itr = len(self.training_data[0]) // self.bs

        self.best_tst_accuracy = []
        self.best_tst_loss = []

        for e in range(1, self.eps + 1):
            _tr_img, _tr_lab = CIFAR_INIT.shuffle_data(self.training_data[0],
                                                       self.training_data[1])

            training_acc = 0.0
            training_loss = 0.0

            for itr in range(self.train_itr):
                _tr_img_batch, _tr_lab_batch = CIFAR_INIT.next_batch(
                    _tr_img, _tr_lab, self.bs, itr)
                _train_accuracy, _train_loss, _ = self.sess.run(
                    [self.accuracy, self.loss, self.train_op],
                    feed_dict={
                        self.x: _tr_img_batch,
                        self.y: _tr_lab_batch,
                        self.is_training: True
                    })
                training_acc += _train_accuracy
                training_loss += _train_loss

            summary = self.sess.run(self.merged,
                                    feed_dict={
                                        self.x: _tr_img_batch,
                                        self.y: _tr_lab_batch,
                                        self.is_training: False
                                    })

            training_acc = float(training_acc / self.train_itr)
            training_loss = float(training_loss / self.train_itr)

            utils.plot_acc_loss(self.ckptDir, self.eps, self.epoch_plt,
                                self.training_accuracy_plt,
                                self.training_loss_plt, self.test_accuracy_plt,
                                self.test_loss_plt)

            self.saver.save(self.sess,
                            self.ckptDir + self.model + '-' + str(e) + '.ckpt')

            self.epoch_plt.append(e)
            self.training_accuracy_plt.append(training_acc)
            self.training_loss_plt.append(training_loss)

            test_acc, test_loss = self.test_procedure()
            self.best_tst_accuracy.append(test_acc)
            self.best_tst_loss.append(test_loss)
            self.test_accuracy_plt.append(test_acc)
            self.test_loss_plt.append(test_loss)

            log1 = "Epoch: [%d], Training Accuracy: [%g], Test Accuracy: [%g], Loss Training: [%g] " \
                   "Loss Test: [%g], Time: [%s]" % \
                   (e, training_acc, test_acc, training_loss, test_loss, time.ctime(time.time()))

            utils.save2file(log1, self.ckptDir, self.model)

            self.writer.add_summary(summary, e)
        self.best_val_index = self.best_tst_accuracy.index(
            max(self.best_tst_accuracy))
        log2 = 'Highest Test Accuracy : [%g], Epoch : [%g]' % (
            self.best_tst_accuracy[self.best_val_index],
            self.best_val_index + 1)
        utils.save2file(log2, self.ckptDir, self.model)

        self.best_val_index_loss = self.best_tst_loss.index(
            min(self.best_tst_loss))
        log3 = 'Lowest Test Loss : [%g], Epoch : [%g]' % (self.best_tst_loss[
            self.best_val_index_loss], self.best_val_index_loss + 1)
        utils.save2file(log3, self.ckptDir, self.model)
示例#3
0
 def saveConfiguration(self):
     utils.save2file('epoch : %d' % self.eps, self.ckptDir, self.model)
     utils.save2file('restore epoch : %d' % self.res_eps, self.ckptDir,
                     self.model)
     utils.save2file('model : %s' % self.model, self.ckptDir, self.model)
     utils.save2file('ksize : %d' % self.k, self.ckptDir, self.model)
     utils.save2file('out channel 1 : %d' % self.oc1, self.ckptDir,
                     self.model)
     utils.save2file('out channel 2 : %d' % self.oc2, self.ckptDir,
                     self.model)
     utils.save2file('out channel 3 : %d' % self.oc3, self.ckptDir,
                     self.model)
     utils.save2file('learning rate : %g' % self.lr, self.ckptDir,
                     self.model)
     utils.save2file('weight decay : %g' % self.wd, self.ckptDir,
                     self.model)
     utils.save2file('batch size : %d' % self.bs, self.ckptDir, self.model)
     utils.save2file('image height : %d' % self.img_h, self.ckptDir,
                     self.model)
     utils.save2file('image width : %d' % self.img_w, self.ckptDir,
                     self.model)
     utils.save2file('num class : %d' % self.num_class, self.ckptDir,
                     self.model)
     utils.save2file('train phase : %s' % self.train_phase, self.ckptDir,
                     self.model)
    def train(self):
        print('Start to run in mode [Supervied Learning in Source Domain]')
        self.sess.run(tf.global_variables_initializer())
        self.train_itr = len(self.training_data[0]) // self.bs

        self.best_val_accuracy = []
        self.best_val_loss = []

        for e in range(1, self.eps + 1):
            _tr_img, _tr_lab = DA_init.shuffle_data(self.training_data[0],
                                                    self.training_data[1])

            training_acc = 0.0
            training_loss = 0.0

            for itr in range(self.train_itr):
                _tr_img_batch, _tr_lab_batch = DA_init.next_batch(
                    _tr_img, _tr_lab, self.bs, itr)
                _train_accuracy, _train_loss, _ = self.sess.run(
                    [self.accuracy, self.loss, self.train_op],
                    feed_dict={
                        self.x: _tr_img_batch,
                        self.y: _tr_lab_batch,
                        self.is_training: True
                    })
                training_acc += _train_accuracy
                training_loss += _train_loss

            summary = self.sess.run(self.merged,
                                    feed_dict={
                                        self.x: _tr_img_batch,
                                        self.y: _tr_lab_batch,
                                        self.is_training: False
                                    })

            training_acc = float(training_acc / self.train_itr)
            training_loss = float(training_loss / self.train_itr)

            validation_acc, validation_loss = self.validation_procedure()
            self.best_val_accuracy.append(validation_acc)
            self.best_val_loss.append(validation_loss)

            log1 = "Epoch: [%d], Training Accuracy: [%g], Validation Accuracy: [%g], Loss Training: [%g] " \
                   "Loss_validation: [%g], Time: [%s]" % \
                   (e, training_acc, validation_acc, training_loss, validation_loss, time.ctime(time.time()))

            self.plt_epoch.append(e)
            self.plt_training_accuracy.append(training_acc)
            self.plt_training_loss.append(training_loss)
            self.plt_validation_accuracy.append(validation_acc)
            self.plt_validation_loss.append(validation_loss)

            utils.plotAccuracy(x=self.plt_epoch,
                               y1=self.plt_training_accuracy,
                               y2=self.plt_validation_accuracy,
                               figName=self.model,
                               line1Name='training',
                               line2Name='validation',
                               savePath=self.ckptDir)

            utils.plotLoss(x=self.plt_epoch,
                           y1=self.plt_training_loss,
                           y2=self.plt_validation_loss,
                           figName=self.model,
                           line1Name='training',
                           line2Name='validation',
                           savePath=self.ckptDir)

            utils.save2file(log1, self.ckptDir, self.model)

            self.writer.add_summary(summary, e)

            self.saver.save(self.sess,
                            self.ckptDir + self.model + '-' + str(e))

            self.test_procedure()

        self.best_val_index = self.best_val_accuracy.index(
            max(self.best_val_accuracy))
        log2 = 'Highest Validation Accuracy : [%g], Epoch : [%g]' % (
            self.best_val_accuracy[self.best_val_index],
            self.best_val_index + 1)
        utils.save2file(log2, self.ckptDir, self.model)

        self.best_val_index_loss = self.best_val_loss.index(
            min(self.best_val_loss))
        log3 = 'Lowest Validation Loss : [%g], Epoch : [%g]' % (
            self.best_val_loss[self.best_val_index_loss],
            self.best_val_index_loss + 1)
        utils.save2file(log3, self.ckptDir, self.model)
示例#5
0
                     isPairData)
        runName = "%s_%s_d%d_w%d_ml%d_%s" % (
            dataset, modelName, dim, max_words, maxlen,
            datetime.now().strftime("%m-%d-%Y_%H-%M-%S"))

    elif modelName == "frage":
        run = FRAGE(dim, emb_dim, max_words, maxlen, embedding_layer,
                    class_num, isPairData, weight, weight2, modelMode)
        runName = "%s_%s_m%d_%s_d%d_w%d_ml%d_w%.3f_w2%.3f_pp%.3f_%s" % (
            dataset, modelName, modelMode,
            discMode, dim, max_words, maxlen, weight, weight2, pop_percent,
            datetime.now().strftime("%m-%d-%Y_%H-%M-%S"))
        run.init(x_train, discMode, isPairData, pop_percent)
        print("ready")

    save2file(path + "out/%s.res" % runName, runName)

    minMSE = 9999999
    maxACC = -999999

    for epoch in range(epochs):
        output = run.train(x_train, y_train, epoch, batch_size, isPairData)
        t2 = time()
        save2file(path + "out/%s.out" % runName, output)

        # Eval
        val_res = run.model.test_on_batch(x_val, y_val)
        test_res = run.model.test_on_batch(x_test, y_test)
        output = "Val acc: %f, Test acc: %f" % (val_res[1], test_res[1])
        save2file(path + "out/%s.res" % runName, output)