Ejemplo n.º 1
0
def get_result(val_base_dir, json_sav_dir):
    '''
       val_base_dir : patch saved dir
       json_sav_dir : json file saved dir
    '''
    val_base_pth = val_base_dir
    sample_name = os.listdir(val_base_dir)

    x = tf.placeholder(tf.float32, [None, height, width, channel], name="inputs")
    is_training = tf.placeholder(tf.bool, name="is_train")

    resnet = ResNet()
    last_feature = resnet.model(x, is_training)
    before_prob = resnet.fc_layer(last_feature, 1, is_training)
    prob = tf.nn.sigmoid(before_prob)

    
    my_dict = dict()
    saver = tf.train.Saver()
    with tf.Session() as sess:
        saver.restore(sess, model_saved_path)

        cnt = 0
        for name in sample_name:
            pth_name = val_base_dir + "/" + name
            prob_list = []
            t1 = time()
            
            for fname in os.listdir(pth_name):
                img = cv.imread(pth_name + '/' + fname)
            
                img = dataAugmentation(img)  
                img = (img - [MEAN_B, MEAN_G, MEAN_R]) / [VAR_B, VAR_G, VAR_R]
                if img.shape[0] != 224 : img = cv.resize(img, (224, 224))

                img = np.expand_dims(img, axis=0)

                p = sess.run(prob, {x: img, is_training: is_train})                
                p = np.squeeze(p) 

                prob_list.append(float(p))                

            t2 = time()
    
            print("%d : predict all patch spend %.5f second" % (cnt, (t2 - t1)))
            print(len(prob_list))
            print(type(prob_list)) 
            my_dict[name] = prob_list
            cnt += 1

        with open(json_sav_dir + '/prob_TCGA.json', "w") as f:
            json.dump(my_dict, f)
    
    return
def get_result():
    sample_name = os.listdir(val_base_dir)

    x = tf.placeholder(tf.float32, [None, height, width, channel],
                       name="inputs")
    is_training = tf.placeholder(tf.bool, name="is_train")

    resnet = ResNet()
    last_feature = resnet.model(x, is_training)
    before_prob = resnet.fc_layer(last_feature, 1, is_training)
    # prob = tf.nn.softmax(before_prob, axis=1, name="prob")
    prob = tf.nn.sigmoid(before_prob)

    grad = tf.gradients(before_prob, last_feature)
    print(grad.get_shape().as_list())

    # y_pred = tf.argmax(prob, axis=1)
    y_pred = prob >= 0.5

    saver = tf.train.Saver()
    with tf.Session() as sess:
        saver.restore(sess, model_saved_path)

        t1 = time()
        for name in sample_name:
            pth_name = val_base_dir + "/" + name

            print(pth_name)

            pos_patch_num = 0
            patch_num = len(os.listdir(pth_name))

            for fname in os.listdir(pth_name):
                # print(pth_name + '/' + fname)

                img = cv.imread(pth_name + '/' + fname)
                img = dataAugmentation(img)
                img = (img - [MEAN_B, MEAN_G, MEAN_R]) / [VAR_B, VAR_G, VAR_R]
                if img.shape[0] != 224: img = cv.resize(img, (224, 224))
                img = np.expand_dims(img, axis=0)

                # print(img.shape)

                predict, p, g = sess.run([y_pred, prob, grad], {
                    x: img,
                    is_training: is_train
                })

                # print("predict : ", predict, "probability : ", p)
                # print("labels : ", (1 if fname[:16] in highTMB else 0))
                break
            break
Ejemplo n.º 3
0
def get_result():

    x = tf.placeholder(tf.float32, [None, height, width, channel],
                       name="inputs")
    is_training = tf.placeholder(tf.bool, name="is_train")

    vgg = VGG()
    out = vgg.model(x, is_training)
    prob = tf.nn.sigmoid(out)

    y_pred = tf.cast(tf.greater(prob, 0.5), tf.float32)

    saver = tf.train.Saver()
    with tf.Session() as sess:
        saver.restore(sess, model_saved_path)

        for dir_name in os.listdir(base_dir):

            test_dir = base_dir + '/' + dir_name
            file_name = os.listdir(test_dir)

            cur_positive_sample_count = 0
            cur_sample_count = len(file_name)

            for name in file_name:
                img = cv.imread(test_dir + '/' + name)

                img = dataAugmentation(img)
                img = (img - [MEAN_B, MEAN_G, MEAN_R]) / 255.0

                if img.shape[0] != 224:
                    img = cv.resize(img, (224, 224))

                img = np.expand_dims(img, axis=0)

                probability, predict = sess.run([prob, y_pred], {
                    x: img,
                    is_training: False
                })

                print("predict is : ", predict, "labels is : ",
                      [1 if name[:16] in highTMB else 0])

                cur_positive_sample_count += predict

            print("current positive sample count from predict : ",
                  cur_positive_sample_count)
            print("current sample count : ", cur_sample_count)
Ejemplo n.º 4
0
def get_result():
    sample_name = os.listdir(val_base_dir)

    x = tf.placeholder(tf.float32, [None, height, width, channel], name="inputs")
    is_training = tf.placeholder(tf.bool, name="is_train")

    resnet = ResNet()
    last_feature = resnet.model(x, is_training)
    before_prob = resnet.fc_layer(last_feature, 1, is_training)
    # prob = tf.nn.softmax(before_prob, axis=1, name="prob")
    prob = tf.nn.sigmoid(before_prob)

    # y_pred = tf.argmax(prob, axis=1)
    y_pred = prob >= 0.5

    saver = tf.train.Saver()
    with tf.Session() as sess:
        saver.restore(sess, model_saved_path)

        ratio = []
        ans = []
        lab = []
        avt = []

        t1 = time()
        pc = 0
        for name in sample_name:
            pth_name = val_base_dir + "/" + name
            
            print(pth_name)

            pos_patch_num = 0
            patch_num = len(os.listdir(pth_name))

            for fname in os.listdir(pth_name):
                # print(pth_name + '/' + fname)

                img = cv.imread(pth_name + '/' + fname)
                img = dataAugmentation(img)  
                img = (img - [MEAN_B, MEAN_G, MEAN_R]) / [VAR_B, VAR_G, VAR_R] 
                if img.shape[0] != 224 : img = cv.resize(img, (224, 224))
                img = np.expand_dims(img, axis=0)

                # print(img.shape)
 
                predict, p = sess.run([y_pred, prob], {x: img, is_training: is_train})
                
                # print("predict : ", predict, "probability : ", p)
                # print("labels : ", (1 if fname[:16] in highTMB else 0))

                pos_patch_num += int(predict)
                
                
            ratio.append(pos_patch_num / patch_num)
            ans.append((pos_patch_num / patch_num) >= 0.5)
            lab.append((1 if name[:16] in highTMB else 0))
            
            print("postive num : %d, patch num : %d, ratio : %.5f" % (pos_patch_num, patch_num, pos_patch_num / patch_num))
            print("predict : %d, labels : %d" % (int((pos_patch_num / patch_num) >= 0.5), int(1 if name[:16] in highTMB else 0)))
            print("Done")
            
  
        acc = np.mean(np.array(ans) == np.array(lab))

        print("acc  : ", acc)
        t2 = time()

        avt.append(t2 - t1)
        t = np.mean(np.array(avt))
        print("Average predict time : ", t)
Ejemplo n.º 5
0
def get_result():
    # sample_name = os.listdir(val_base_dir)

    x = tf.placeholder(tf.float32, [None, height, width, channel], name="inputs")
    is_training = tf.placeholder(tf.bool, name="is_train")

    vgg = VGG()
    out = vgg.model(x, is_training)
    prob = tf.nn.sigmoid(out)

    y_pred = tf.cast(tf.greater(prob, 0.5), tf.float32)

    saver = tf.train.Saver()
    with tf.Session() as sess:
        saver.restore(sess, model_saved_path)

        ava = []
        wc = 0       # whole count
        wp = 0       # whole positive count
        wn = 0       # whole negative count
        wpp = 0      # whole predict positive
        wpn = 0      # whole negative predict

        pssc = 0
        for sample_name in os.listdir(sample_pth): 
            t1 = time()
            pc = 0                                                         # 每个病例下的被预测为TMB高的癌灶区patch个数
            pos_patch_num = 0
            patch_num = len(os.listdir(sample_pth + '/' + sample_name))
            for name in os.listdir(sample_pth + '/' + sample_name):
                img_name = sample_pth + '/' + sample_name + "/" + name
            
                # print(pth_name)


                    # print(pth_name + '/' + fname)

                img = cv.imread(img_name) 
                img = dataAugmentation(img)
                img = (img - [B_MEAN, G_MEAN, R_MEAN]) / 255.0
                if img.shape[0] != 224 : 
                    img = cv.resize(img, (224, 224))

                img = np.expand_dims(img, axis=0)

                    # print(img.shape)
 
                predict, p = sess.run([y_pred, prob], {x: img, is_training: is_train})
                
                    # print("predict : ", predict, "probability : ", p)
                    # print("labels : ", (1 if fname[:16] in highTMB else 0))
                label = (1 if name[:16] in highTMB else 0)

                wc += 1
                wp += label
                wn += (1 - label)
                wpp += int((predict == 1) and (label == 1))
                wpn += int((predict == 0) and (label == 0)) 
                pos_patch_num += predict
                
                # a = input()
                
            
                # print("postive num : %d, patch num : %d, ratio : %.5f" % (pos_patch_num, patch_num, pos_patch_num / patch_num))
                # print("predict : %d, labels : %d" % (int((pos_patch_num / patch_num) >= 0.5), int(1 if name[:16] in highTMB else 0)))
                # print("Done")

            pc = int((pos_patch_num / patch_num) >= 0.5)
            
            t2 = time()
            case_predict_result = pc
            print("case predict : ", case_predict_result, "label is : ", int(1 if sample_name[:16] in highTMB else 0)) 
            ava.append(t2 - t1)

        t = np.mean(np.array(ava))
        print("average time is : ", t)
        print("recall : ", wpp / wp)
        print("acc for patch : ", (wpp + wpn) / wc)
def get_result():
    sample_name = os.listdir(val_base_dir)

    x = tf.placeholder(tf.float32, [None, height, width, channel], name="inputs")
    is_training = tf.placeholder(tf.bool, name="is_train")

    vgg = VGG()
    out = vgg.model(x, is_training)
    prob = tf.nn.sigmoid(out)

    y_pred = tf.greater(prob, 0.5)

    saver = tf.train.Saver()
    with tf.Session() as sess:
        saver.restore(sess, model_saved_path)

        ratio = []
        ans = []
        lab = []
        avt = []

        t1 = time()
        pc = 0
        for name in sample_name:
            pth_name = val_base_dir + "/" + name
            
            print(pth_name)

            pos_patch_num = 0
            patch_num = len(os.listdir(pth_name))

            for fname in os.listdir(pth_name):
                # print(pth_name + '/' + fname)

                img = cv.imread(pth_name + '/' + fname)
                img = dataAugmentation(img)  
                img = (img - [B_MEAN, G_MEAN, R_MEAN]) / 255.0
                img = cv.resize(img, (224, 224))
                img = np.expand_dims(img, axis=0)

                # print(img.shape)
 
                predict, p = sess.run([y_pred, prob], {x: img, is_training: is_train})
                
                # print("predict : ", predict, "probability : ", p)
                # print("labels : ", (1 if fname[:16] in highTMB else 0))

                pos_patch_num += int(predict)
                
                
            ratio.append(pos_patch_num / patch_num)
            ans.append((pos_patch_num / patch_num) >= 0.5)
            lab.append((1 if name[:16] in highTMB else 0))
            
            print("postive num : %d, patch num : %d, ratio : %.5f" % (pos_patch_num, patch_num, pos_patch_num / patch_num))
            print("predict : %d, labels : %d" % (int((pos_patch_num / patch_num) >= 0.5), int(1 if name[:16] in highTMB else 0)))
            print("Done")
            
            pc += int((pos_patch_num / patch_num) >= 0.5)
        
        if pc * 2 >= len(sample_name) :
            print("TMB value is High !")
        else:
            print("TMB value is Low !")
  
        acc = np.mean(np.array(ans) == np.array(lab))

        print("acc  : ", acc)
        t2 = time()

        avt.append(t2 - t1)
        t = np.mean(np.array(avt))
        print("Average predict time : ", t)