Ejemplo n.º 1
0
    def inference(self, X, dirr, train_status=False, Not_Realtest=True):
        now = datetime.now().isoformat()[11:]
        print("------- Testing begin: {} -------\n".format(now), flush=True)
        if Not_Realtest:
            x_list, y_list, tnum = X.next_batch()
        else:
            x_list, tnum = X.next_batch(Not_Realtest=False)
            y_list = None
        rd = 0
        pdx = []
        yl = []
        with tf.Session() as sessb:
            # Initialize all global and local variables
            init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
            sessb.run(init_op)
            # Create a coordinator and run all QueueRunner objects
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(coord=coord, sess=sessb)

            while True:
                try:
                    if Not_Realtest:
                        x, y = sessb.run([x_list, y_list])
                        x = x.astype(np.uint8)
                    else:
                        x = sessb.run([x_list])
                        y = None
                        x = x[0].astype(np.uint8)
                    feed_dict = {self.x_in: x, self.is_train: train_status}
                    fetches = [self.pred, self.net, self.w]
                    pred, net, w = self.sesh.run(fetches, feed_dict)
                    if Not_Realtest:
                        ac.CAM(net, w, pred, x, y, dirr, 'Test', rd)
                    # else:
                    #     ac.CAM_R(net, w, pred, x, dirr, 'Test', rd)

                    if rd == 0:
                        pdx = pred
                        yl = y
                    else:
                        pdx = np.concatenate((pdx, pred), axis=0)
                        yl = np.concatenate((yl, y), axis=None)

                    rd += 1

                except tf.errors.OutOfRangeError:
                    # Stop the threads
                    coord.request_stop()

                    # Wait for threads to stop
                    coord.join(threads)
                    if Not_Realtest:
                        ac.metrics(pdx, yl, dirr, 'Test')
                    else:
                        ac.realout(pdx, dirr, 'Test')
                    sessb.close()
                    now = datetime.now().isoformat()[11:]
                    print("------- Testing end: {} -------\n".format(now), flush=True)
                    break
Ejemplo n.º 2
0
    def inference(self,
                  X,
                  dirr,
                  testset,
                  bs,
                  train_status=False,
                  Not_Realtest=True):
        now = datetime.now().isoformat()[11:]
        print("------- Testing begin: {} -------\n".format(now), flush=True)
        rd = 0
        pdx = []
        yl = []
        if Not_Realtest:
            itr, file, ph = X.data(train=False)
            next_element = itr.get_next()
            with tf.Session() as sessa:
                sessa.run(itr.initializer, feed_dict={ph: file})
                while True:
                    try:
                        x, y = sessa.run(next_element)
                        feed_dict = {self.x_in: x, self.is_train: train_status}
                        fetches = [self.pred, self.net, self.w]
                        pred, net, w = self.sesh.run(fetches, feed_dict)
                        # ac.CAM(net, w, pred, x, y, dirr, 'Test', bs, rd)
                        if rd == 0:
                            pdx = pred
                            yl = y
                        else:
                            pdx = np.concatenate((pdx, pred), axis=0)
                            yl = np.concatenate((yl, y), axis=0)
                        rd += 1
                    except tf.errors.OutOfRangeError:
                        ac.metrics(pdx, yl, dirr, 'Test', testset)
                        break
        else:
            itr, img, ph = X.data(Not_Realtest=False, train=False)
            next_element = itr.get_next()
            with tf.Session() as sessa:
                sessa.run(itr.initializer, feed_dict={ph: img})
                while True:
                    try:
                        x = sessa.run(next_element)
                        feed_dict = {self.x_in: x, self.is_train: train_status}
                        fetches = [self.pred, self.net, self.w]
                        pred, net, w = self.sesh.run(fetches, feed_dict)
                        # ac.CAM_R(net, w, pred, x, dirr, 'Test', bs, rd)
                        if rd == 0:
                            pdx = pred
                        else:
                            pdx = np.concatenate((pdx, pred), axis=0)
                        rd += 1
                    except tf.errors.OutOfRangeError:
                        ac.realout(pdx, dirr, 'Test')
                        break

        now = datetime.now().isoformat()[11:]
        print("------- Testing end: {} -------\n".format(now), flush=True)
Ejemplo n.º 3
0
    def inference(self,
                  X,
                  dirr,
                  testset=None,
                  pmd=None,
                  train_status=False,
                  Not_Realtest=True,
                  bs=None):
        now = datetime.now().isoformat()[11:]
        print("------- Testing begin: {} -------\n".format(now))
        rd = 0
        pdx = []
        yl = []
        if Not_Realtest:
            itr, file, ph = X.data(train=False)
            next_element = itr.get_next()
            with tf.Session() as sessa:
                sessa.run(itr.initializer, feed_dict={ph: file})
                while True:
                    try:
                        x, y = sessa.run(next_element)
                        feed_dict = {self.x_in: x, self.is_train: train_status}
                        fetches = [self.pred, self.net, self.w, self.logits]
                        pred, net, w, logits = self.sesh.run(
                            fetches, feed_dict)
                        # ac.CAM(net, w, pred, x, y, dirr, 'Test', bs, pmd, rd)
                        net = np.mean(net, axis=(1, 2))
                        if rd == 0:
                            pdx = pred
                            yl = y
                            netl = net
                            logitsl = logits
                        else:
                            pdx = np.concatenate((pdx, pred), axis=0)
                            yl = np.concatenate((yl, y), axis=0)
                            netl = np.concatenate((netl, net), axis=0)
                            logitsl = np.concatenate((logitsl, logits), axis=0)
                        rd += 1
                    except tf.errors.OutOfRangeError:
                        ac.metrics(pdx, yl, dirr, 'Test', pmd, testset)
                        ac.logit_agg(logitsl, testset, dirr, "Test")
                        ac.tSNE_prep(flatnet=netl,
                                     ori_test=testset,
                                     y=yl,
                                     pred=pdx,
                                     path=dirr,
                                     pmd=pmd)
                        break
        else:
            itr, file, ph = X.data(Not_Realtest=False, train=False)
            next_element = itr.get_next()
            with tf.Session() as sessa:
                sessa.run(itr.initializer, feed_dict={ph: file})
                while True:
                    try:
                        x = sessa.run(next_element)
                        feed_dict = {self.x_in: x, self.is_train: train_status}
                        fetches = [self.pred, self.net, self.w]
                        pred, net, w = self.sesh.run(fetches, feed_dict)
                        # ac.CAM_R(net, w, pred, x, dirr, 'Test', bs, rd)
                        if rd == 0:
                            pdx = pred
                        else:
                            pdx = np.concatenate((pdx, pred), axis=0)
                        rd += 1
                    except tf.errors.OutOfRangeError:
                        ac.realout(pdx, dirr, 'Test', pmd)
                        break

        now = datetime.now().isoformat()[11:]
        print("------- Testing end: {} -------\n".format(now), flush=True)