Beispiel #1
0
    def test(self):
        print("test")
        sess = tf.InteractiveSession()
        sess.run(tf.global_variables_initializer())
        saver = tf.train.Saver()
        saver.restore(sess, self.config['Train']['file_model_save'])
        base_dir = "../../data_vote_man/MR_CT/test/model_24000/*ct_train_%s_image_A_T/"
        ps = [get_sample_generator(target_id=i) for i in [1016]]
        # sample_generator = get_sample_generator(target_id=1014)
        time.clock()

        n_test_itr = 2500
        for itr in range(n_test_itr):
            '''
            随机产生一个数组,包含0-n_classes,取期中n_way个类
            '''
            atlas_img_patches, atlas_labs, target_img_patch, target_lab, sim = \
                ps[np.random.randint(0, len(ps))].next_sample(is_test=False)
            # np.expand_dims(np.stack(data, axis=0), axis=4).astype(np.float32)

            index = np.random.randint(0, len(atlas_img_patches))
            feed_img_patch = np.expand_dims(np.stack([target_img_patch],
                                                     axis=0),
                                            axis=4).astype(np.float32)
            feed_atlas_patch = np.expand_dims(np.stack(
                [atlas_img_patches[index]], axis=0),
                                              axis=4).astype(np.float32)

            binary_sim = [(1 if i > 0.8 else 0) for i in sim]
            feed_sim_lab = np.stack(
                [[binary_sim[index], 1 - binary_sim[index]]],
                axis=0).astype(np.float32)
            testFeed = {
                self.ph_target_p: feed_img_patch,
                self.ph_atlas_p: feed_atlas_patch,
                self.ph_sim_lab: feed_sim_lab
            }

            loss, p_y = sess.run([self.ce_loss, self.p_y], feed_dict=testFeed)
            acc = 0.0
            # avg_acc=avg_acc+acc
            # print("step:"+str(step))

            # one_hot_lab,out,p_y=sess.run([self.one_hot_lab, self.out,self.p_y], feed_dict=trainFeed)
            # print(one_hot_lab)
            # print(p_y)
            print([sim[index], 1 - sim[index]])
            print(p_y)
            print("loss: %f " % (loss))

        # print(avg_acc/self.config['Train']['total_iterations'])
        sess.close()
Beispiel #2
0
    def train(self):
        # initialize all variables
        sess = tf.Session()
        sess.run(tf.global_variables_initializer())
        # saver to save model
        self.saver = tf.train.Saver()
        # summary writer
        self.writer = tf.summary.FileWriter(
            os.path.dirname(self.config['Train']['file_model_save']),
            sess.graph)
        # ckpt = tf.train.get_checkpoint_state(os.path.dirname(vote_config['Train']['file_model_save']))
        # if ckpt and ckpt.model_checkpoint_path:
        #     # saver.restore(sess,ckpt.model_checkpoint_path)
        #     saver.restore(sess, vote_config['Inference']['file_model_saved'] + str(2500))
        #     print("Model restored...")
        # else:
        #     print('No Model')
        #tensorflow 的调试
        #sess=tf_debug.LocalCLIDebugWrapperSession(sess)
        # sess.graph.finalize()#让graph保持不变,防止内存溢出
        # ps = [get_sample_generator(target_id=i) for i in [1013, 1014, 1015, 1016]]
        ps = [get_sample_generator(target_id=i) for i in [1015]]
        sample_count = 0
        self.need = 0
        avg_acc = 0.0
        for step in range(self.config['Train']['total_iterations']):
            # pass

            atlas_img_patches, atlas_labs, target_img_patch, target_lab, sim = ps[
                np.random.randint(0, len(ps))].next_sample(is_test=False)
            # np.expand_dims(np.stack(data, axis=0), axis=4).astype(np.float32)
            lab = [(True if i > 0.8 else False) for i in sim]
            index = self.__get_sample_index(lab)
            if index == None:
                continue

            sim = [(1 if i > 0.8 else 0) for i in sim]

            feed_img_patch = np.expand_dims(np.stack([target_img_patch],
                                                     axis=0),
                                            axis=4).astype(np.float32)
            feed_atlas_patch = np.expand_dims(np.stack(
                [atlas_img_patches[index]], axis=0),
                                              axis=4).astype(np.float32)
            feed_sim_lab = np.stack([[sim[index], 1 - sim[index]]],
                                    axis=0).astype(np.float32)
            trainFeed = {
                self.ph_target_p: feed_img_patch,
                self.ph_atlas_p: feed_atlas_patch,
                self.ph_sim_lab: feed_sim_lab
            }

            sess.run(self.train_op, feed_dict=trainFeed)
            loss, p_y = sess.run([self.ce_loss, self.p_y], feed_dict=trainFeed)
            acc = 0.0
            # avg_acc=avg_acc+acc
            # print("step:"+str(step))

            # one_hot_lab,out,p_y=sess.run([self.one_hot_lab, self.out,self.p_y], feed_dict=trainFeed)
            print([sim[index], 1 - sim[index]])
            if sim[index] > 1 - sim[index]:
                sample_count = sample_count + 1
            else:
                sample_count = sample_count - 1
            print("loss: %f " % (loss))
        # print(avg_acc/self.config['Train']['total_iterations'])
        save_path = self.saver.save(sess,
                                    self.config['Train']['file_model_save'],
                                    write_meta_graph=False)
        print("save model to" + save_path)
        print("pos %d", sample_count)
        sess.close()
Beispiel #3
0
    def test(self):
        print("test")
        sess = tf.InteractiveSession()
        sess.run(tf.global_variables_initializer())
        saver = tf.train.Saver()
        saver.restore(sess, self.args.model_dir[0])
        base_dir = self.args.test_atlas_target[0]
        # base_dir = self.args.train_atlas_target[0]
        # sample_generator = [get_sample_generator(base_dir, target_id=i, args=self.args) for i in [1017, 1018, 1019, 1020]]
        # for i in [1013, 1014, 1015, 1016]:
        for i in [1017, 1018, 1019, 1020]:
            # for i in [1017]:
            g = get_sample_generator(base_dir, target_id=i, args=self.args)

            predict_target = np.zeros([3, 96, 96, 96], dtype=np.uint16)
            gold_standard = g.target_lab
            for i in range(3):
                predict_target[i] = g.mv_predict_lab

                # sample_generator = get_sample_generator(target_id=1013)
            time.clock()
            acc = np.zeros([3], dtype=np.float32)
            all = [0.0, 0.0, 0.0]
            count = 0
            while True:
                '''
                随机产生一个数组,包含0-n_classes,取期中n_way个类
                '''

                atlas_patches, atlas_labs, target_patch, target_lab, sim = g.next_sample(
                    num_nonlocal_patch=self.n_support_sample, is_test=True)
                if atlas_patches is None:
                    break
                pos = g.pos

                target_patch_center = target_lab[self.patch_size[0] // 2,
                                                 self.patch_size[1] // 2,
                                                 self.patch_size[2] // 2]
                # atlas_patches=np.expand_dims(atlas_patches[0,:,:,:,:],axis=0)
                # atlas_labs=np.expand_dims(atlas_labs[0,:,:,:,:],axis=0)
                # sim=np.expand_dims(sim[0,:],axis=0)

                # MV的方法
                pred = self.acc_MV(target_lab, atlas_labs)

                acc[0] = acc[0] + (1 if pred == target_patch_center else 0)
                predict_target[0][pos[0], pos[1], pos[2]] = pred

                # NLVW的方法
                pred = self.acc_nlvw(atlas_patches, atlas_labs, target_patch,
                                     target_lab)
                acc[1] = acc[1] + (1 if pred == target_patch_center else 0)
                predict_target[1][pos[0], pos[1], pos[2]] = pred

                # Network
                pred, loss = self.acc_net(sess, atlas_labs, atlas_patches, sim,
                                          target_lab, target_patch)
                acc[2] = acc[2] + (1 if pred == target_patch_center else 0)
                predict_target[2][pos[0], pos[1], pos[2]] = pred

                count = count + 1
                # print(acc)
                if count % 100 == 0:
                    self.logger.info(acc / count)
                # self.logger.debug(ac1, ac2, ac3)
                #
                # mk_or_cleardir('../tmp/')
                # self.__write_out_target(target_patch, target_lab)
                # self.__write_out_atlas(atlas_patches[0], atlas_labs[0], sim[0])

            # print("testing time %s" % time.clock())
            self.logger.info("mv_acc_avg =%f" % (acc[0] / count))
            self.logger.info("nlvw_acc_avg =%f" % (acc[1] / count))
            self.logger.info("patch embbeding avg_acc=%f" % (acc[2] / count))
            self.logger.info("final result")
            for i in range(3):
                method_i_acc = dice_compute(gold_standard, predict_target[i])
                self.logger.info(method_i_acc)
                all[i] = all[i] + method_i_acc
        # self.logger.info("all result"+all[i]/4.0)
        self.logger.info(all / 4.0)
        sess.close()
Beispiel #4
0
    def visulize(self):
        sess = tf.InteractiveSession()
        sess.run(tf.global_variables_initializer())
        saver = tf.train.Saver()
        saver.restore(sess, self.args.model_dir[0])
        base_dir = self.args.test_atlas_target[0]
        sample_generator = [
            get_sample_generator(base_dir, target_id=i, args=self.args)
            for i in [1017, 1018, 1019, 1020]
        ]
        # sample_generator = [get_sample_generator(base_dir,target_id=i, args=self.args) for i in [1013]]

        time.clock()
        acc_avg = 0.0
        mv_acc_avg = 0.0
        for itr in range(n_train_itr):
            '''
            随机产生一个数组,包含0-n_classes,取期中n_way个类
            '''
            # atlas_patches, atlas_labs, target_patch, target_lab,sim = sample_generator[np.random.randint(0,len(sample_generator))].next_sample(
            #     num_nonlocal_patch=6, dupplicated=False)
            g = sample_generator[np.random.randint(0, len(sample_generator))]
            np_atlas_patches, np_atlas_labs, target_patch, target_lab, np_sim = g.next_sample(
                num_nonlocal_patch=self.n_support_sample, is_test=False)
            # check是否有问题
            atlas_patches, atlas_labs, sim = self.__slice_for_network(
                np_atlas_patches, np_atlas_labs, np_sim, 0)

            binary_sim = [(1 if i > self.thres else 0) for i in sim]
            if np.sum(binary_sim) == 0 or np.sum(binary_sim) == len(
                    binary_sim):
                continue
            # max_idx=np.argmax(sim)
            # min_idx=np.argmin(sim)
            support = np.zeros([self.n_support_sample, self.one_support] +
                               self.patch_size + [self.channal],
                               dtype=np.float32)
            query = np.zeros([self.n_query_sample, self.one_query] +
                             self.patch_size + [self.channal],
                             dtype=np.float32)
            y_n_labels = np.zeros(
                [self.n_query_sample, self.one_query, self.n_support_sample],
                np.uint8)

            for i, p in enumerate(atlas_patches):
                support[i, 0] = np.expand_dims(p, axis=4).astype(np.float32)
            # support[0, 0]=np.expand_dims(atlas_patches[max_idx],axis=4).astype(np.float32)
            # support[1, 0]=np.expand_dims(atlas_patches[min_idx],axis=4).astype(np.float32)

            query[0, 0] = np.expand_dims(target_patch,
                                         axis=4).astype(np.float32)

            y_n_labels[0, 0] = binary_sim
            # y_n_labels[0,0]=[1,0]

            # _, ls = sess.run([self.train_op, self.ce_loss],
            #                  feed_dict={self.x: support, self.q: query, self.y_n_hot: y_n_labels})
            y_n_hot, predict = sess.run([self.y_n_hot, self.prob],
                                        feed_dict={
                                            self.x: support,
                                            self.q: query,
                                            self.y_n_hot: y_n_labels
                                        })
            # self.accV2(target_lab,atlas_labs,predict)
            sim_str = ['{:.2f}'.format(x) for x in sim]
            self.logger.debug(sim_str)
            self.logger.debug(predict)
            mk_or_cleardir('../tmp/')
            self.__write_out_target(target_patch, target_lab)
            self.__write_out_atlas(np_atlas_patches[0], np_atlas_labs[0],
                                   np_sim[0])
            # if (epi + 1) %50 == 0:
            # self.logger.debug('[ episode {}/{}] => loss: {:.3f}'.format(itr + 1, n_train_itr, ls))
        # print("avg_acc=%f" % (acc_avg / n_train_itr))
        # print("avg_acc=%f" % (mv_acc_avg / n_train_itr))
        self.logger.info("training time %s" % time.clock())
        save_path = saver.save(sess,
                               self.args.model_dir[0],
                               write_meta_graph=False)
        self.logger.info("save model to" + save_path)
        sess.close()