コード例 #1
0
    def test_procedure(self, test_data, distribution_op, inputX, inputY, mode):
        confusion_matrics = np.zeros([self.num_class, self.num_class], dtype="int")

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

            matrix_row, matrix_col = self.sess.run(distribution_op, feed_dict={inputX: _testImg,
                                                                               inputY: _testLab,
                                                                               self.is_training: False})
            for m, n in zip(matrix_row, matrix_col):
                confusion_matrics[m][n] += 1

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

        CoGAN_utils.save2file(log0, self.ckptDir, self.model)
        CoGAN_utils.save2file(log1, self.ckptDir, self.model)
        CoGAN_utils.save2file(log2, self.ckptDir, self.model)
        CoGAN_utils.save2file(log3, self.ckptDir, self.model)
        CoGAN_utils.save2file(log4, self.ckptDir, self.model)
コード例 #2
0
 def saveConfiguration(self):
     CoGAN_utils.save2file('epoch : %d' % self.eps, self.ckptDir, self.model)
     CoGAN_utils.save2file('restore epoch : %d' % self.res_eps, self.ckptDir, self.model)
     CoGAN_utils.save2file('model : %s' % self.model, self.ckptDir, self.model)
     CoGAN_utils.save2file('learning rate : %g' % self.lr, self.ckptDir, self.model)
     CoGAN_utils.save2file('batch size : %d' % self.bs, self.ckptDir, self.model)
     CoGAN_utils.save2file('image height : %d' % self.img_h, self.ckptDir, self.model)
     CoGAN_utils.save2file('image width : %d' % self.img_w, self.ckptDir, self.model)
     CoGAN_utils.save2file('num class : %d' % self.num_class, self.ckptDir, self.model)
     CoGAN_utils.save2file('train phase : %s' % self.train_phase, self.ckptDir, self.model)
     CoGAN_utils.save2file('step : %d' % self.step, self.ckptDir, self.model)
コード例 #3
0
    def train(self):
        self.sess.run(tf.global_variables_initializer())

        self.itr_epoch = len(self.source_training_data[0]) // self.bs

        source_training_acc = 0.0
        source_training_loss = 0.0

        for e in range(1, self.eps + 1):
            for itr in range(self.itr_epoch):
                feed_dict_train, feed_dict_eval = self.getBatchData()
                _ = self.sess.run(self.train_op, feed_dict=feed_dict_train)

                _training_accuracy, _training_loss = self.sess.run([self.accuracy_source, self.loss],
                                                                   feed_dict=feed_dict_eval)

                source_training_acc += _training_accuracy
                source_training_loss += _training_loss

            summary = self.sess.run(self.merged, feed_dict=feed_dict_eval)

            source_training_acc = float(source_training_acc / self.itr_epoch)
            source_training_loss = float(source_training_loss / self.itr_epoch)

            source_validation_acc, source_validation_loss = self.validation_procedure(
                validation_data=self.source_validation_data, distribution_op=self.distribution_source,
                loss_op=self.loss, inputX=self.x_source, inputY=self.y_source)

            log1 = "Epoch: [%d], Domain: Source, Training Accuracy: [%g], Validation Accuracy: [%g], " \
                   "Training Loss: [%g], Validation Loss: [%g], Time: [%s]" % (
                       e, source_training_acc, source_validation_acc, source_training_loss, source_validation_loss,
                       time.ctime(time.time()))

            self.plt_epoch.append(e)
            self.plt_training_accuracy.append(source_training_acc)
            self.plt_training_loss.append(source_training_loss)
            self.plt_validation_accuracy.append(source_validation_acc)
            self.plt_validation_loss.append(source_validation_loss)

            CoGAN_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)

            CoGAN_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)

            CoGAN_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.source_test_data, distribution_op=self.distribution_source,
                                inputX=self.x_source,
                                inputY=self.y_source, mode='source')

            source_training_acc = 0.0
            source_training_loss = 0.0
コード例 #4
0
    def train(self):
        print('Initialize parameters')
        self.sess.run(tf.global_variables_initializer())
        print('Global variables initialization finished')

        print('Reload parameters')
        dict_var = {}
        for i in self.src_var:
            for j in self.tar_var:
                if i.name[i.name.find('/') + 1:] in j.name[j.name.find('/') +
                                                           1:]:
                    dict_var[i.name[:-2]] = j

        self.src_encoder_reloadSaver = tf.train.Saver(var_list=self.src_var)
        self.tar_encoder_reloadSaver = tf.train.Saver(var_list=dict_var)
        self.classifier_reloadSaver = tf.train.Saver(var_list=self.cla_var)

        self.src_encoder_reloadSaver.restore(self.sess, self.reloadPath)
        self.tar_encoder_reloadSaver.restore(self.sess, self.reloadPath)
        self.classifier_reloadSaver.restore(self.sess, self.reloadPath)
        print(
            'source encoder, target encoder and classifier have been successfully reloaded !'
        )

        self.itr_epoch = len(self.source_training_data[0]) // self.bs

        source_training_acc = 0.0
        source_training_loss = 0.0

        for e in range(1, self.eps + 1):
            for itr in range(self.itr_epoch):
                feed_dict_train, feed_dict_eval = self.getBatchData()
                _ = self.sess.run(self.d_train_op, feed_dict=feed_dict_train)
                _ = self.sess.run(self.g_train_op, feed_dict=feed_dict_train)

                _training_accuracy, _training_loss = self.sess.run(
                    [self.accuracy_source, self.supervised_loss],
                    feed_dict=feed_dict_eval)

                source_training_acc += _training_accuracy
                source_training_loss += _training_loss

            summary = self.sess.run(self.merged, feed_dict=feed_dict_eval)

            source_training_acc = float(source_training_acc / self.itr_epoch)
            source_training_loss = float(source_training_loss / self.itr_epoch)

            source_validation_acc, source_validation_loss = self.validation_procedure(
                validation_data=self.source_validation_data,
                distribution_op=self.distribution_source,
                loss_op=self.supervised_loss,
                inputX=self.x_source,
                inputY=self.y_source)

            log1 = "Epoch: [%d], Domain: Source, Training Accuracy: [%g], Validation Accuracy: [%g], " \
                   "Training Loss: [%g], Validation Loss: [%g], Time: [%s]" % (
                       e, source_training_acc, source_validation_acc, source_training_loss, source_validation_loss,
                       time.ctime(time.time()))

            self.plt_epoch.append(e)
            self.plt_training_accuracy.append(source_training_acc)
            self.plt_training_loss.append(source_training_loss)
            self.plt_validation_accuracy.append(source_validation_acc)
            self.plt_validation_loss.append(source_validation_loss)

            CoGAN_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)

            CoGAN_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)

            CoGAN_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.source_test_data,
                                distribution_op=self.distribution_source,
                                inputX=self.x_source,
                                inputY=self.y_source,
                                mode='source')
            self.test_procedure(self.target_test_data,
                                distribution_op=self.distribution_target,
                                inputX=self.x_target,
                                inputY=self.y_target,
                                mode='target')

            source_training_acc = 0.0
            source_training_loss = 0.0