def data_augmentation():
    src_dir = 'D:/Mywork/image_coord_regenerate/newest_img_coord/different_type/'
    one_samples = glob.glob(src_dir + 'two/' + "*.png")
    for record_number, record_item in enumerate(one_samples):

        cube_image = base_dicom_process.load_cube_img(record_item, 8, 8, 64)
        cube_image1 = numpy.fliplr(cube_image)
        save_cube_img(target_path=src_dir + 'two/' + str(record_number) +
                      '_2_0.png',
                      cube_img=cube_image1,
                      cols=8,
                      rows=8)
        cube_image2 = numpy.flipud(cube_image)
        save_cube_img(target_path=src_dir + 'two/' + str(record_number) +
                      '_2_1.png',
                      cube_img=cube_image2,
                      cols=8,
                      rows=8)
        cube_image3 = cube_image[:, :, ::-1]
        save_cube_img(target_path=src_dir + 'two/' + str(record_number) +
                      '_2_2.png',
                      cube_img=cube_image3,
                      cols=8,
                      rows=8)
        cube_image4 = cube_image[:, ::-1, :]
        save_cube_img(target_path=src_dir + 'two/' + str(record_number) +
                      '_2_3.png',
                      cube_img=cube_image4,
                      cols=8,
                      rows=8)
def predict_cubes(model_path, cube_dir):

    # 开始计时
    sw = base_settings.Stopwatch.start_new()
    # 导入模型
    model = step4_2_train_typedetector.get_net(input_shape=(CUBE_SIZE,
                                                            CUBE_SIZE,
                                                            CUBE_SIZE, 1),
                                               load_weight_path=model_path)

    cube_image = base_dicom_process.load_cube_img(cube_dir, 8, 8, 64)
    current_cube_size = cube_image.shape[0]  # 64
    indent_x = (current_cube_size - CROP_SIZE) / 2  # 16
    indent_y = (current_cube_size - CROP_SIZE) / 2  # 16
    indent_z = (current_cube_size - CROP_SIZE) / 2  # 16
    wiggle_indent = 0
    wiggle = current_cube_size - CROP_SIZE - 1  # 31
    if wiggle > (CROP_SIZE / 2):
        wiggle_indent = CROP_SIZE / 4  # 8
        wiggle = current_cube_size - CROP_SIZE - CROP_SIZE / 2 - 1  # 15
    indent_x = wiggle_indent + random.randint(0, wiggle)
    indent_y = wiggle_indent + random.randint(0, wiggle)
    indent_z = wiggle_indent + random.randint(0, wiggle)

    indent_x = int(indent_x)
    indent_y = int(indent_y)
    indent_z = int(indent_z)
    # 在64*64*64的立方体中,随机裁剪出32*32*32的小立方体(这里好像不太随机,小立方体像素范围是(8~54)
    cube_image = cube_image[indent_z:indent_z + CROP_SIZE,
                            indent_y:indent_y + CROP_SIZE,
                            indent_x:indent_x + CROP_SIZE]
    # print('cube image shape is :', cube_image.shape)
    img_prep = prepare_image_for_net3D(cube_image)
    # batch_list.append(img_prep)
    # 模型预测函数,该篇核心
    p = model.predict(img_prep)
    print('预测结果数:', len(p), p.shape)
    print('*********************************')
    print(p)
    print(p[0].tolist())
    print('the predict lung cancer type is : ',
          p[0].tolist().index(max(p[0].tolist())))
    # 测试花费时间
    print("Done in : ", sw.get_elapsed_seconds(), " seconds")
Beispiel #3
0
def get_mean_pixels():
    pixel_value = 0
    print("Get train/holdout files.")
    src_dir = 'D:/Mywork/image_coord_regenerate/dataset/train/'
    one_samples = glob.glob(src_dir+'one/' + "*.png")
    two_samples = glob.glob(src_dir+'two/' + "*.png")
    three_samples = glob.glob(src_dir+'three/' + "*.png")
    four_samples = glob.glob(src_dir+'four/' + "*.png")
    five_samples = glob.glob(src_dir+'zero/' + "*.png")
    # sample_count = [len(one_samples), len(two_samples), len(three_samples), len(four_samples), len(five_samples)]
    # print('sample count is:', sample_count)
    record_item = one_samples+two_samples+three_samples+four_samples+five_samples
    random.shuffle(record_item)
    for item in record_item:
        cube_image = base_dicom_process.load_cube_img(item, 8, 8, 64)
        pixel_value += cube_image.mean()
    print(len(record_item))
    print('pixel_value sum is:', pixel_value)
    print('avg pixel value is:', pixel_value/len(record_item))
def data_generator(batch_size, record_list, train_set):

    batch_idx = 0
    while True:
        img_list = []
        # class_list 是“肺癌类型”的标签集合[1,2,3,4, 5]
        class_list = []
        CROP_SIZE = CUBE_SIZE  # 32

        # 对每张图片进行遍历
        for record_idx, record_item in enumerate(record_list):

            class_label = int(record_item[1])
            # cube_image : 64*64*64
            cube_image = base_dicom_process.load_cube_img(
                record_item[0], 8, 8, 64)

            current_cube_size = cube_image.shape[0]  # 64
            indent_x = (current_cube_size - CROP_SIZE) / 2  # 16
            indent_y = (current_cube_size - CROP_SIZE) / 2  # 16
            indent_z = (current_cube_size - CROP_SIZE) / 2  # 16
            wiggle_indent = 0
            wiggle = current_cube_size - CROP_SIZE - 1  # 31
            if wiggle > (CROP_SIZE / 2):
                wiggle_indent = CROP_SIZE / 4  # 8
                wiggle = current_cube_size - CROP_SIZE - CROP_SIZE / 2 - 1  # 15
            if train_set:
                indent_x = wiggle_indent + random.randint(0, wiggle)
                indent_y = wiggle_indent + random.randint(0, wiggle)
                indent_z = wiggle_indent + random.randint(0, wiggle)

            indent_x = int(indent_x)
            indent_y = int(indent_y)
            indent_z = int(indent_z)
            # 在64*64*64的立方体中,随机裁剪出32*32*32的小立方体(这里好像不太随机,小立方体像素范围是(8~54)
            cube_image = cube_image[indent_z:indent_z + CROP_SIZE,
                                    indent_y:indent_y + CROP_SIZE,
                                    indent_x:indent_x + CROP_SIZE]
            assert cube_image.shape == (CUBE_SIZE, CUBE_SIZE, CUBE_SIZE)

            if train_set:  # 以下为四种随机翻转方式
                if random.randint(0, 100) > 50:
                    cube_image = numpy.fliplr(cube_image)
                if random.randint(0, 100) > 50:
                    cube_image = numpy.flipud(cube_image)
                if random.randint(0, 100) > 50:
                    cube_image = cube_image[:, :, ::-1]
                if random.randint(0, 100) > 50:
                    cube_image = cube_image[:, ::-1, :]

            # cube_image.mean() 计算三维矩阵所有数的平均数,结果是一个数
            # means.append(cube_image.mean())
            # cube_image 是 32*32 *32
            # img3d 为 1*32*32*32*1
            img3d = prepare_image_for_net3D(cube_image)
            img_list.append(img3d)
            class_list.append(class_label)

            batch_idx += 1
            if batch_idx >= batch_size:
                x = numpy.vstack(img_list)
                # print('x shape:', x.shape)
                # print(class_list)
                y_class = numpy.vstack(class_list)
                # 将标签转换为分类的 one-hot 编码
                one_hot_labels = utils.to_categorical(y_class, num_classes=6)
                # print(one_hot_labels)
                # yield 是返回部分,详见python生成器解释
                yield x, {"out_class": one_hot_labels}
                img_list = []
                class_list = []
                batch_idx = 0