def visual_results(self, dataset_type="TEST", images_index=3):
        image_w = self.config["INPUT_WIDTH"]
        image_h = self.config["INPUT_HEIGHT"]
        image_c = self.config["INPUT_CHANNELS"]

        with self.sess as sess:
            # Restore saved session
            saver = tf.train.Saver()
            saver.restore(self.sess,
                          os.path.join(self.saved_dir, 'model.ckpt-19'))

            _, _, prediction = cal_loss(logits=self.logits,
                                        labels=self.labels_pl)

            if (dataset_type == 'TRAIN'):
                test_type_path = self.config["TRAIN_FILE"]
                if type(images_index) == list:
                    indexes = images_index
                else:
                    indexes = random.sample(range(367), images_index)
            elif (dataset_type == 'VAL'):
                test_type_path = self.config["VAL_FILE"]
                if type(images_index) == list:
                    indexes = images_index
                else:
                    indexes = random.sample(range(101), images_index)
            elif (dataset_type == 'TEST'):
                test_type_path = self.config["TEST_FILE"]
                if type(images_index) == list:
                    indexes = images_index
                else:
                    indexes = random.sample(range(233), images_index)

            # Load images
            image_filename, label_filename = get_filename_list(
                test_type_path, self.config)
            images, labels = get_all_test_data(image_filename, label_filename)

            # Keep images subset of length images_index
            images = [images[i] for i in indexes]
            labels = [labels[i] for i in indexes]

            pred_tot = []

            for image_batch, label_batch in zip(images, labels):
                image_batch = np.reshape(image_batch,
                                         [1, image_h, image_w, image_c])
                label_batch = np.reshape(label_batch, [1, image_h, image_w, 1])
                fetches = [prediction]
                feed_dict = {
                    self.inputs_pl: image_batch,
                    self.labels_pl: label_batch,
                    self.batch_size_pl: 1
                }
                pred = sess.run(fetches=fetches, feed_dict=feed_dict)
                pred = np.reshape(pred, [image_h, image_w])
                pred_tot.append(pred)

            draw_plots(images, labels, pred_tot)
Exemplo n.º 2
0
    def train(self, max_steps=30000, batch_size=3):
        image_filename, label_filename = get_filename_list(
            self.train_file, self.config)
        val_image_filename, val_label_filename = get_filename_list(
            self.val_file, self.config)

        with self.graph.as_default():
            if self.images_tr is None:
                self.images_tr, self.labels_tr = dataset_inputs(
                    image_filename, label_filename, batch_size, self.config)
                self.images_val, self.labels_val = dataset_inputs(
                    val_image_filename, val_label_filename, batch_size,
                    self.config)

            loss, accuracy, prediction = cal_loss(logits=self.logits,
                                                  labels=self.labels_pl)
            #                                                  , number_class=self.num_classes)
            train, global_step = train_op(total_loss=loss, opt=self.opt)

            summary_op = tf.summary.merge_all()

            with self.sess.as_default():
                self.sess.run(tf.local_variables_initializer())
                self.sess.run(tf.global_variables_initializer())

                coord = tf.train.Coordinator()
                threads = tf.train.start_queue_runners(coord=coord)

                train_writer = tf.summary.FileWriter(self.tb_logs,
                                                     self.sess.graph)
                self.saver = tf.train.Saver()

                for step in range(max_steps):
                    image_batch, label_batch = self.sess.run(
                        [self.images_tr, self.labels_tr])
                    feed_dict = {
                        self.inputs_pl: image_batch,
                        self.labels_pl: label_batch,
                        self.batch_size_pl: batch_size
                    }

                    _, _loss, _accuracy, summary = self.sess.run(
                        [train, loss, accuracy, summary_op],
                        feed_dict=feed_dict)
                    self.train_loss.append(_loss)
                    self.train_accuracy.append(_accuracy)
                    print(
                        "Iteration {}: Train Loss{:6.3f}, Train Accu {:6.3f}".
                        format(step, self.train_loss[-1],
                               self.train_accuracy[-1]))

                    if step % 100 == 0:
                        train_writer.add_summary(summary, step)

                    if step % 1000 == 0:
                        print("start validating..")
                        _val_loss = []
                        _val_acc = []
                        for test_step in range(9):
                            image_batch_val, label_batch_val = self.sess.run(
                                [self.images_val, self.labels_val])
                            feed_dict_valid = {
                                self.inputs_pl: image_batch_val,
                                self.labels_pl: label_batch_val,
                                self.batch_size_pl: batch_size
                            }
                            # since we still using mini-batch, so in the batch norm we set phase_train to be
                            # true, and because we didin't run the trainop process, so it will not update
                            # the weight!
                            _loss, _acc, _val_pred = self.sess.run(
                                [loss, accuracy, self.logits], feed_dict_valid)
                            _val_loss.append(_loss)
                            _val_acc.append(_acc)

                        self.val_loss.append(np.mean(_val_loss))
                        self.val_acc.append(np.mean(_val_acc))

                        print("Val Loss {:6.3f}, Val Acc {:6.3f}".format(
                            self.val_loss[-1], self.val_acc[-1]))

                        self.saver.save(self.sess,
                                        os.path.join(self.saved_dir,
                                                     'model.ckpt'),
                                        global_step=self.model_version)
                        self.model_version = self.model_version + 1

                coord.request_stop()
                coord.join(threads)
Exemplo n.º 3
0
    def visual_results_external_image(self, images, FLAG_MAX_VOTE = False):

        #train_dir = "./saved_models/segnet_vgg_bayes/segnet_vgg_bayes_30000/model.ckpt-30000"
        #train_dir = "./saved_models/segnet_scratch/segnet_scratch_30000/model.ckpt-30000"


        i_width = 64
        i_height =64
        images = [misc.imresize(image, (i_height, i_width)) for image in images]

        image_w = self.config["INPUT_WIDTH"]
        image_h = self.config["INPUT_HEIGHT"]
        image_c = self.config["INPUT_CHANNELS"]
        train_dir = self.config["SAVE_MODEL_DIR"]
        FLAG_BAYES = self.config["BAYES"]

        with self.sess as sess:

            # Restore saved session
            saver = tf.train.Saver()
            saver.restore(sess, train_dir)

            _, _, prediction = cal_loss(logits=self.logits,
                                           labels=self.labels_pl)
            prob = tf.nn.softmax(self.logits,dim = -1)

            num_sample_generate = 30
            pred_tot = []
            var_tot = []

            labels = []
            for i in range(len(images)):
                labels.append(np.array([[1 for x in range(64)] for y in range(64)]))


            inference_time = []
            start_time = time.time()

            for image_batch, label_batch in zip(images,labels):
            #for image_batch in zip(images):

                image_batch = np.reshape(image_batch,[1,image_h,image_w,image_c])
                label_batch = np.reshape(label_batch,[1,image_h,image_w,1])

                if FLAG_BAYES is False:
                    fetches = [prediction]
                    feed_dict = {self.inputs_pl: image_batch,
                                 self.labels_pl: label_batch,
                                 self.is_training_pl: False,
                                 self.keep_prob_pl: 0.5,
                                 self.batch_size_pl: 1}
                    pred = sess.run(fetches = fetches, feed_dict = feed_dict)
                    pred = np.reshape(pred,[image_h,image_w])
                    var_one = []
                else:
                    feed_dict = {self.inputs_pl: image_batch,
                                 self.labels_pl: label_batch,
                                 self.is_training_pl: False,
                                 self.keep_prob_pl: 0.5,
                                 self.with_dropout_pl: True,
                                 self.batch_size_pl: 1}
                    prob_iter_tot = []
                    pred_iter_tot = []
                    for iter_step in range(num_sample_generate):
                        prob_iter_step = sess.run(fetches = [prob], feed_dict = feed_dict)
                        prob_iter_tot.append(prob_iter_step)
                        pred_iter_tot.append(np.reshape(np.argmax(prob_iter_step,axis = -1),[-1]))

                    if FLAG_MAX_VOTE is True:
                        prob_variance,pred = MAX_VOTE(pred_iter_tot,prob_iter_tot,self.config["NUM_CLASSES"])
                        #acc_per = np.mean(np.equal(pred,np.reshape(label_batch,[-1])))
                        var_one = var_calculate(pred,prob_variance)
                        pred = np.reshape(pred,[image_h,image_w])
                    else:
                        prob_mean = np.nanmean(prob_iter_tot,axis = 0)
                        prob_variance = np.var(prob_iter_tot, axis = 0)
                        pred = np.reshape(np.argmax(prob_mean,axis = -1),[-1]) #pred is the predicted label with the mean of generated samples
                        #THIS TIME I DIDN'T INCLUDE TAU
                        var_one = var_calculate(pred,prob_variance)
                        pred = np.reshape(pred,[image_h,image_w])


                pred_tot.append(pred)
                var_tot.append(var_one)
                inference_time.append(time.time() - start_time)
                start_time = time.time()

            try:
                draw_plots_bayes_external(images, pred_tot, var_tot)
                return pred_tot, var_tot, inference_time
            except:
                return pred_tot, var_tot, inference_time
Exemplo n.º 4
0
    def visual_results(self, dataset_type = "TEST", images_index = 3, FLAG_MAX_VOTE = False):

        image_w = self.config["INPUT_WIDTH"]
        image_h = self.config["INPUT_HEIGHT"]
        image_c = self.config["INPUT_CHANNELS"]
        train_dir = self.config["SAVE_MODEL_DIR"]
        FLAG_BAYES = self.config["BAYES"]
        print(FLAG_BAYES)
        with self.sess as sess:

            # Restore saved session
            saver = tf.train.Saver()
            saver.restore(sess, train_dir)

            _, _, prediction = cal_loss(logits=self.logits,
                                        labels=self.labels_pl,number_class=self.num_classes)
            prob = tf.nn.softmax(self.logits,dim = -1)

            if (dataset_type=='TRAIN'):
                test_type_path = self.config["TRAIN_FILE"]
                if type(images_index) == list:
                    indexes = images_index
                else:
                    '''CHANGE IT BACK'''
                    #indexes = random.sample(range(367),images_index)
                    indexes = random.sample(range(6),images_index)
                #indexes = [0,75,150,225,300]
            elif (dataset_type=='VAL'):
                test_type_path = self.config["VAL_FILE"]
                if type(images_index) == list:
                    indexes = images_index
                else:
                    #indexes = random.sample(range(101),images_index)
                    indexes = random.sample(range(10),images_index)
                #indexes = [0,25,50,75,100]
            elif (dataset_type=='TEST'):
                test_type_path = self.config["TEST_FILE"]
                if type(images_index) == list:
                    indexes = images_index
                else:
                    indexes = random.sample(range(5),images_index)
                    #indexes = random.sample(range(233),images_index)
                #indexes = [0,50,100,150,200]

            # Load images

            image_filename,label_filename = get_filename_list(test_type_path, self.config)
            images, labels = get_all_test_data(image_filename,label_filename)
            
            # Keep images subset of length images_index
            images = [images[i] for i in indexes]
            labels = [labels[i] for i in indexes]

            num_sample_generate = 30
            pred_tot = []
            var_tot = []
            print(image_c)
            for image_batch, label_batch in zip(images,labels):
                print(image_batch.shape)
                image_batch = np.reshape(image_batch,[1,image_h,image_w,image_c])
                label_batch = np.reshape(label_batch,[1,image_h,image_w,1])

                if FLAG_BAYES is False:
                    print("NON BAYES")
                    fetches = [prediction]
                    feed_dict = {self.inputs_pl: image_batch,
                                 self.labels_pl: label_batch,
                                 self.is_training_pl: False,
                                 self.keep_prob_pl: 0.5,
                                 self.batch_size_pl: 1}
                    pred = sess.run(fetches = fetches, feed_dict = feed_dict)
                    pred = np.reshape(pred,[image_h,image_w])
                    var_one = []
                else:
                    feed_dict = {self.inputs_pl: image_batch,
                                 self.labels_pl: label_batch,
                                 self.is_training_pl: False,
                                 self.keep_prob_pl: 0.5,
                                 self.with_dropout_pl: True,
                                 self.batch_size_pl: 1}
                    prob_iter_tot = []
                    pred_iter_tot = []
                    for iter_step in range(num_sample_generate):
                        prob_iter_step = sess.run(fetches = [prob], feed_dict = feed_dict)
                        prob_iter_tot.append(prob_iter_step)
                        pred_iter_tot.append(np.reshape(np.argmax(prob_iter_step,axis = -1),[-1]))

                    if FLAG_MAX_VOTE is True:
                        prob_variance,pred = MAX_VOTE(pred_iter_tot,prob_iter_tot,self.config["NUM_CLASSES"])
                        #acc_per = np.mean(np.equal(pred,np.reshape(label_batch,[-1])))
                        var_one = var_calculate(pred,prob_variance)
                        pred = np.reshape(pred,[image_h,image_w])
                    else:
                        prob_mean = np.nanmean(prob_iter_tot,axis = 0)
                        prob_variance = np.var(prob_iter_tot, axis = 0)
                        pred = np.reshape(np.argmax(prob_mean,axis = -1),[-1]) #pred is the predicted label with the mean of generated samples
                        #THIS TIME I DIDN'T INCLUDE TAU
                        var_one = var_calculate(pred,prob_variance)
                        pred = np.reshape(pred,[image_h,image_w])


                pred_tot.append(pred)
                var_tot.append(var_one)

            draw_plots_bayes(images, labels, pred_tot, var_tot)
        return (images,labels,pred_tot,var_tot)
Exemplo n.º 5
0
    def train(self, max_steps=30001, batch_size=3):
        # For train the bayes, the FLAG_OPT SHOULD BE SGD, BUT FOR TRAIN THE NORMAL SEGNET,
        # THE FLAG_OPT SHOULD BE ADAM!!!

        image_filename, label_filename = get_filename_list(self.train_file, self.config)
        val_image_filename, val_label_filename = get_filename_list(self.val_file, self.config)

        with self.graph.as_default():
            if self.images_tr is None:
                self.images_tr, self.labels_tr = dataset_inputs(image_filename, label_filename, batch_size, self.config)
                self.images_val, self.labels_val = dataset_inputs(val_image_filename, val_label_filename, batch_size,
                                                                  self.config)

            loss, accuracy, prediction = cal_loss(logits=self.logits, labels=self.labels_pl,
                                                     number_class=self.num_classes)
            train, global_step = train_op(total_loss=loss, opt=self.opt)

            summary_op = tf.summary.merge_all()

            with self.sess.as_default():
                self.sess.run(tf.local_variables_initializer())
                self.sess.run(tf.global_variables_initializer())

                coord = tf.train.Coordinator()
                threads = tf.train.start_queue_runners(coord=coord)
                # The queue runners basic reference:
                # https://www.tensorflow.org/versions/r0.12/how_tos/threading_and_queues
                train_writer = tf.summary.FileWriter(self.tb_logs, self.sess.graph)
                for step in range(max_steps):
                    print("OK")
                    image_batch, label_batch = self.sess.run([self.images_tr, self.labels_tr])
                    feed_dict = {self.inputs_pl: image_batch,
                                 self.labels_pl: label_batch,
                                 self.is_training_pl: True,
                                 self.keep_prob_pl: 0.5,
                                 self.with_dropout_pl: True,
                                 self.batch_size_pl: batch_size}

                    _, _loss, _accuracy, summary = self.sess.run([train, loss, accuracy, summary_op],
                                                                 feed_dict=feed_dict)
                    self.train_loss.append(_loss)
                    self.train_accuracy.append(_accuracy)
                    print("Iteration {}: Train Loss{:6.3f}, Train Accu {:6.3f}".format(step, self.train_loss[-1],
                                                                                       self.train_accuracy[-1]))

                    if step % 100 == 0:
                        conv_classifier = self.sess.run(self.logits, feed_dict=feed_dict)
                        print('per_class accuracy by logits in training time',
                              per_class_acc(conv_classifier, label_batch, self.num_classes))
                        # per_class_acc is a function from utils
                        train_writer.add_summary(summary, step)

                    if step % 1000 == 0:
                        print("start validating.......")
                        _val_loss = []
                        _val_acc = []
                        hist = np.zeros((self.num_classes, self.num_classes))
                        for test_step in range(int(20)):
                            fetches_valid = [loss, accuracy, self.logits]
                            image_batch_val, label_batch_val = self.sess.run([self.images_val, self.labels_val])
                            feed_dict_valid = {self.inputs_pl: image_batch_val,
                                               self.labels_pl: label_batch_val,
                                               self.is_training_pl: True,
                                               self.keep_prob_pl: 1.0,
                                               self.with_dropout_pl: False,
                                               self.batch_size_pl: batch_size}
                            # since we still using mini-batch, so in the batch norm we set phase_train to be
                            # true, and because we didin't run the trainop process, so it will not update
                            # the weight!
                            _loss, _acc, _val_pred = self.sess.run(fetches_valid, feed_dict_valid)
                            _val_loss.append(_loss)
                            _val_acc.append(_acc)
                            hist += get_hist(_val_pred, label_batch_val)

                        print_hist_summary(hist)

                        self.val_loss.append(np.mean(_val_loss))
                        self.val_acc.append(np.mean(_val_acc))

                        print(
                            "Iteration {}: Train Loss {:6.3f}, Train Acc {:6.3f}, Val Loss {:6.3f}, Val Acc {:6.3f}".format(
                                step, self.train_loss[-1], self.train_accuracy[-1], self.val_loss[-1],
                                self.val_acc[-1]))

                coord.request_stop()
                coord.join(threads)
Exemplo n.º 6
0
    def visual_results(self,
                       dataset_type="TEST",
                       images_index=3,
                       FLAG_MAX_VOTE=False):

        image_w = self.config["INPUT_WIDTH"]
        image_h = self.config["INPUT_HEIGHT"]
        image_c = self.config["INPUT_CHANNELS"]
        train_dir = self.config["SAVE_MODEL_DIR"]
        FLAG_BAYES = self.config["BAYES"]

        with self.sess as sess:

            # Restore saved session
            saver = tf.train.Saver()
            saver.restore(sess, train_dir)

            kernel = variable_with_weight_decay('weights',
                                                initializer=initialization(
                                                    1, 64),
                                                shape=[1, 1, 64, 3],
                                                wd=False)
            conv = tf.nn.conv2d(self.deconv1_3,
                                kernel, [1, 1, 1, 1],
                                padding='SAME')
            biases = variable_with_weight_decay('biases',
                                                tf.constant_initializer(0.0),
                                                shape=[3],
                                                wd=False)
            logits = tf.nn.bias_add(conv, biases, name="scope.name")
            #exit()
            sess.run(tf.global_variables_initializer())
            #sess.run(logits)
            _, _, prediction = cal_loss(logits=logits, labels=self.labels_pl)
            prob = tf.nn.softmax(logits, dim=-1)
            print(
                "==================================================================================="
            )
            print(prediction)
            #exit()
            if (dataset_type == 'TRAIN'):
                test_type_path = self.config["TRAIN_FILE"]
                if type(images_index) == list:
                    indexes = images_index
                else:
                    indexes = random.sample(range(367), images_index)
                #indexes = [0,75,150,225,300]
            elif (dataset_type == 'VAL'):
                test_type_path = self.config["VAL_FILE"]
                if type(images_index) == list:
                    indexes = images_index
                else:
                    indexes = random.sample(range(101), images_index)
                #indexes = [0,25,50,75,100]
            elif (dataset_type == 'TEST'):
                test_type_path = self.config["TEST_FILE"]
                if type(images_index) == list:
                    indexes = images_index
                else:
                    indexes = random.sample(range(233), images_index)
                #indexes = [0,50,100,150,200]

            # Load images
            image_filename, label_filename = get_filename_list(
                test_type_path, self.config)
            images, labels = get_all_test_data(image_filename, label_filename)

            # Keep images subset of length images_index
            images = [images[i] for i in indexes]
            labels = [labels[i] for i in indexes]

            num_sample_generate = 30
            pred_tot = []
            var_tot = []

            for image_batch, label_batch in zip(images, labels):

                image_batch = np.reshape(image_batch,
                                         [1, image_h, image_w, image_c])
                label_batch = np.reshape(label_batch, [1, image_h, image_w, 1])

                if FLAG_BAYES is False:
                    fetches = [prediction]
                    feed_dict = {
                        self.inputs_pl: image_batch,
                        self.labels_pl: label_batch,
                        self.is_training_pl: False,
                        self.keep_prob_pl: 0.5,
                        self.batch_size_pl: 1
                    }
                    pred = sess.run(fetches=fetches, feed_dict=feed_dict)
                    pred = np.reshape(pred, [image_h, image_w])
                    var_one = []
                else:
                    feed_dict = {
                        self.inputs_pl: image_batch,
                        self.labels_pl: label_batch,
                        self.is_training_pl: False,
                        self.keep_prob_pl: 0.5,
                        self.with_dropout_pl: True,
                        self.batch_size_pl: 1
                    }
                    prob_iter_tot = []
                    pred_iter_tot = []
                    for iter_step in range(num_sample_generate):
                        prob_iter_step = sess.run(fetches=[prob],
                                                  feed_dict=feed_dict)
                        prob_iter_tot.append(prob_iter_step)
                        pred_iter_tot.append(
                            np.reshape(np.argmax(prob_iter_step, axis=-1),
                                       [-1]))

                    if FLAG_MAX_VOTE is True:
                        prob_variance, pred = MAX_VOTE(
                            pred_iter_tot, prob_iter_tot,
                            self.config["NUM_CLASSES"])
                        #acc_per = np.mean(np.equal(pred,np.reshape(label_batch,[-1])))
                        var_one = var_calculate(pred, prob_variance)
                        pred = np.reshape(pred, [image_h, image_w])
                    else:
                        prob_mean = np.nanmean(prob_iter_tot, axis=0)
                        prob_variance = np.var(prob_iter_tot, axis=0)
                        pred = np.reshape(
                            np.argmax(prob_mean, axis=-1), [-1]
                        )  #pred is the predicted label with the mean of generated samples
                        #THIS TIME I DIDN'T INCLUDE TAU
                        var_one = var_calculate(pred, prob_variance)
                        pred = np.reshape(pred, [image_h, image_w])

                pred_tot.append(pred)
                var_tot.append(var_one)

            draw_plots_bayes(images, labels, pred_tot, var_tot)
Exemplo n.º 7
0
    def visual_results_external_image(self, images, model_file):

        images = [
            misc.imresize(image, (self.input_h, self.input_w))
            for image in images
        ]

        with tf.Session() as sess:

            # Restore saved session
            saver = tf.train.Saver()

            if model_file is None:
                saver.restore(sess,
                              tf.train.latest_checkpoint(FLAGS.runtime_dir))
            else:
                saver.restore(sess, os.path.join(FLAGS.runtime_dir,
                                                 model_file))

            _, _, prediction = cal_loss(logits=self.logits,
                                        labels=self.labels_pl,
                                        n_classes=self.n_classes)
            prob = tf.nn.softmax(self.logits, dim=-1)

            pred_tot = []
            var_tot = []

            labels = []
            for i in range(len(images)):
                labels.append(
                    np.array([[1 for x in range(self.input_w)]
                              for y in range(self.input_h)]))

            inference_time = []
            start_time = time.time()

            for image_batch, label_batch in zip(images, labels):
                image_batch = np.reshape(
                    image_batch, [1, self.input_h, self.input_w, self.input_c])
                label_batch = np.reshape(label_batch,
                                         [1, self.input_h, self.input_w, 1])

                fetches = [prediction]
                feed_dict = {
                    self.inputs_pl: image_batch,
                    self.labels_pl: label_batch,
                    self.is_training_pl: False,
                    self.keep_prob_pl: 0.5,
                    self.batch_size_pl: 1
                }
                pred = sess.run(fetches=fetches, feed_dict=feed_dict)
                pred = np.reshape(pred, [self.input_h, self.input_w])

                pred_tot.append(pred)
                inference_time.append(time.time() - start_time)
                start_time = time.time()

            try:
                draw_plots_bayes_external(images, pred_tot)
                return pred_tot, var_tot, inference_time
            except:
                return pred_tot, var_tot, inference_time
Exemplo n.º 8
0
    def visual_results(self,
                       dataset_type="TEST",
                       indices=None,
                       n_samples=3,
                       model_file=None):

        with tf.Session() as sess:

            # Restore saved session
            saver = tf.train.Saver()

            if model_file is None:
                saver.restore(sess,
                              tf.train.latest_checkpoint(FLAGS.runtime_dir))
            else:
                saver.restore(sess, os.path.join(FLAGS.runtime_dir,
                                                 model_file))

            _, _, prediction = cal_loss(logits=self.logits,
                                        labels=self.labels_pl,
                                        n_classes=self.n_classes)

            test_type_path = None
            if dataset_type == 'TRAIN':
                test_type_path = self.train_file
            elif dataset_type == 'VAL':
                test_type_path = self.val_file
            elif dataset_type == 'TEST':
                test_type_path = self.test_file

            # Load images
            image_filenames, label_filenames = get_filename_list(
                test_type_path)
            images, labels = get_all_test_data(image_filenames,
                                               label_filenames)

            if not indices:
                indices = random.sample(range(len(images)), n_samples)

            # Keep images subset of length images_index
            images = [images[i] for i in indices]
            labels = [labels[i] for i in indices]

            pred_tot = []

            for image_batch, label_batch in zip(images, labels):
                image_batch = np.reshape(
                    image_batch, [1, self.input_h, self.input_w, self.input_c])
                label_batch = np.reshape(label_batch,
                                         [1, self.input_h, self.input_w, 1])

                fetches = [prediction]
                feed_dict = {
                    self.inputs_pl: image_batch,
                    self.labels_pl: label_batch,
                    self.is_training_pl: False,
                    self.keep_prob_pl: 0.5,
                    self.batch_size_pl: 1
                }
                pred = sess.run(fetches=fetches, feed_dict=feed_dict)
                pred = np.reshape(pred, [self.input_h, self.input_w])
                pred_tot.append(pred)

            draw_plots_bayes(images, labels, pred_tot)
Exemplo n.º 9
0
    def train(self):
        image_filename, label_filename = get_filename_list(self.train_file)
        val_image_filename, val_label_filename = get_filename_list(
            self.val_file)

        if self.images_tr is None:
            self.images_tr, self.labels_tr = dataset_inputs(
                image_filename, label_filename, FLAGS.batch_size, self.input_w,
                self.input_h, self.input_c)
            self.images_val, self.labels_val = dataset_inputs(
                val_image_filename, val_label_filename, FLAGS.batch_size,
                self.input_w, self.input_h, self.input_c)

        loss, accuracy, predictions = cal_loss(logits=self.logits,
                                               labels=self.labels_pl,
                                               n_classes=self.n_classes)
        train, global_step = train_op(loss, FLAGS.learning_rate)

        tf.summary.scalar("global_step", global_step)
        tf.summary.scalar("total loss", loss)

        # Calculate total number of trainable parameters
        total_parameters = 0
        for variable in tf.trainable_variables():
            shape = variable.get_shape()
            variable_parameters = 1
            for dim in shape:
                variable_parameters *= dim.value
            total_parameters += variable_parameters
        print('Total Trainable Parameters: ', total_parameters)

        with tf.train.SingularMonitoredSession(
                # save/load model state
                checkpoint_dir=FLAGS.runtime_dir,
                hooks=[
                    tf.train.StopAtStepHook(last_step=FLAGS.n_epochs),
                    tf.train.CheckpointSaverHook(
                        checkpoint_dir=FLAGS.runtime_dir,
                        save_steps=FLAGS.checkpoint_frequency,
                        saver=tf.train.Saver()),
                    tf.train.SummarySaverHook(
                        save_steps=FLAGS.summary_frequency,
                        output_dir=FLAGS.runtime_dir,
                        scaffold=tf.train.Scaffold(
                            summary_op=tf.summary.merge_all()),
                    )
                ],
                config=tf.ConfigProto(log_device_placement=True)) as mon_sess:

            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(coord=coord, sess=mon_sess)

            while not mon_sess.should_stop():

                image_batch, label_batch = mon_sess.raw_session().run(
                    [self.images_tr, self.labels_tr])
                feed_dict = {
                    self.inputs_pl: image_batch,
                    self.labels_pl: label_batch,
                    self.is_training_pl: True,
                    self.keep_prob_pl: 0.5,
                    self.with_dropout_pl: True,
                    self.batch_size_pl: FLAGS.batch_size
                }

                step, _, training_loss, training_acc = mon_sess.run(
                    [global_step, train, loss, accuracy], feed_dict=feed_dict)

                print("Iteration {}: Train Loss{:9.6f}, Train Accu {:9.6f}".
                      format(step, training_loss, training_acc))

                # Check against validation set
                if step % FLAGS.validate_frequency == 0:
                    sampled_losses = []
                    sampled_accuracies = []

                    hist = np.zeros((self.n_classes, self.n_classes))

                    for test_step in range(int(20)):
                        fetches_valid = [loss, accuracy, self.logits]
                        image_batch_val, label_batch_val = mon_sess.raw_session(
                        ).run([self.images_val, self.labels_val])

                        feed_dict_valid = {
                            self.inputs_pl: image_batch_val,
                            self.labels_pl: label_batch_val,
                            self.is_training_pl: True,
                            self.keep_prob_pl: 1.0,
                            self.with_dropout_pl: False,
                            self.batch_size_pl: FLAGS.batch_size
                        }

                        validate_loss, validate_acc, predictions = mon_sess.raw_session(
                        ).run(fetches_valid, feed_dict_valid)
                        sampled_losses.append(validate_loss)
                        sampled_accuracies.append(validate_acc)
                        hist += get_hist(predictions, label_batch_val)

                    print_hist_summary(hist)

                    # Average loss and accuracy over n samples from validation set
                    avg_loss = np.mean(sampled_losses)
                    avg_acc = np.mean(sampled_accuracies)

                    print(
                        "Iteration {}: Avg Val Loss {:9.6f}, Avg Val Acc {:9.6f}"
                        .format(step, avg_loss, avg_acc))

                coord.request_stop()
                coord.join(threads)