Пример #1
0
def seg_predict_image_task1(image_path, use_tta=False):
    '''只针对一张图片'''
    num_folds = 4
    # use_tta = False  # 如果为false,表示不采用取平均数方式
    y_pred = np.zeros(shape=(1, 224, 224))
    resize_image_np, W, H = get_resize_image_np(image_path, 224)
    if use_tta:
        # 采用取平均数的模式
        for k_fold in range(num_folds + 1):
            model_name = 'task1_vgg16'
            run_name = 'task1_vgg16_k%d_v0' % k_fold
            model = backbone('vgg16').segmentation_model(load_from=run_name)
            # model.load_weights("/home/zhangfan/workData/LinuxCode/pythonProject/ISIC2018/model_data/task1_vgg16_k0_v0/task1_vgg16_k0_v0.hdf5")
            y_pred += inv_sigmoid(
                model.predict_on_batch(resize_image_np))[:, :, :, 0]
        y_pred /= (num_folds + 1)
    else:
        # 只是用最后一组数据的权重
        run_name = 'task1_vgg16_k%d_v0' % num_folds
        model = backbone('vgg16').segmentation_model(load_from=run_name)
        y_pred += inv_sigmoid(model.predict_on_batch(resize_image_np))[:, :, :,
                                                                       0]
    y_pred = sigmoid(y_pred)
    current_pred = y_pred[0] * 255
    current_pred[current_pred > 128] = 255
    current_pred[current_pred <= 128] = 0
    # 将resized_pred传给Nodejs
    print({"image_np": current_pred, "width": W, "height": H})
Пример #2
0
def cls_predict_image(image_path,use_tta=False):
    '''识别一张图片'''
    num_folds =4

    use_tta = False

    y_pred = np.zeros(shape=(1,7))

    resize_image_np,W,H = get_resize_image_np(image_path,224)
    if use_tta:
        for k_fold in range(num_folds+1):
            run_name = "task3_inception_v3_k"+str(k_fold)+"_v0"
            model = backbone("inception_v3").classification_model(load_from=run_name)
            # 这里可以考虑使用多线程
            if use_tta:
                y_pred += inv_sigmoid(task3_tta_predict(model=model,img_arr=resize_image_np))
            else:
                y_pred += inv_sigmoid(model.predict_on_batch(resize_image_np))
    else:
        run_name = "task3_inception_v3_k" + str(num_folds) + "_v0"
        model = backbone("inception_v3").classification_model(load_from=run_name)
        y_pred += inv_sigmoid(model.predict_on_batch(resize_image_np))

    y_pred = y_pred / num_folds
    y_prob = softmax(y_pred)
    print({
        "result":y_prob
    })
Пример #3
0
def cls_predict_images(image_paths):
    '''识别一组图片'''
    num_folds = 5
    use_tta = False
    run_name = "task3_inception_v3_k" + str(num_folds) + "_v0"
    model = backbone("inception_v3").classification_model(load_from=run_name)
    for image_path in image_paths:
        y_pred = np.zeros(shape=(1,7))
        resize_image_np, W, H = get_resize_image_np(image_path,224)
        y_pred += inv_sigmoid(model.predict_on_batch(resize_image_np))
        y_prod = softmax(y_pred)
        print({
            "result": y_prod
        })
Пример #4
0
def seg_predict_images_task1(image_paths, use_tta=False):
    '''输入图片地址,预测一组图片'''
    num_folds = 4
    use_tta = False
    image_num = len(image_paths)  # 得到图片的数量
    if use_tta:
        pass
    else:
        y_pred = np.zeros(shape=(1, 224, 224))
        run_name = 'task1_vgg16_k%d_v0' % num_folds
        model = backbone('vgg16').segmentation_model(load_from=run_name)
        for image_path in image_paths:
            resize_image_np, W, H = get_resize_image_np(image_path, 224)
            y_pred += inv_sigmoid(
                model.predict_on_batch(resize_image_np))[:, :, :, 0]
            y_pred = sigmoid(y_pred)
            current_pred = y_pred[0] * 255
            current_pred[current_pred > 128] = 255
            current_pred[current_pred <= 128] = 0
            print({"image_np": current_pred, "width": W, "height": H})
Пример #5
0
 def seg_predict_images_task1(self,image_paths):
     results = []
     resize_images_np,images_size = get_resize_images_np(image_paths)
     images_num = len(images_size)
     y_pred = np.zeros(shape=(images_num,224,224))
     with self.graph.as_default():
         y_pred += inv_sigmoid(self.task1_model.predict_on_batch(resize_images_np))[:, :, :, 0]
     y_pred = sigmoid(y_pred)
     y_pred = task1_post_process(y_prediction=y_pred,threshold=0.5,gauss_sigma=2.)
     for index,image_path in enumerate(image_paths):
         image_dir,image_name,image_ext = get_fileDirectoryPath_fileName_fileExt(image_path)
         current_pred = y_pred[index]
         current_pred = current_pred * 255
         resized_pred = transform.resize(current_pred,
                                         output_shape=images_size[index],
                                         preserve_range=True,
                                         mode='reflect')
         resized_pred[resized_pred > 128] = 255
         resized_pred[resized_pred <= 128] = 0
         img = Image.fromarray(resized_pred.astype(np.uint8))
         image_path = os.path.join(task1_result_dir,image_name+"_result"+image_ext)
         img.save(image_path)
         results.append(image_path)
     return results
Пример #6
0
    image_names = image_names[:max_num_images]
    image_sizes = image_sizes[:max_num_images]

    y_pred = np.zeros(shape=(max_num_images, 224, 224))

    num_folds = 5

    print('Starting prediction for set %s with TTA set to %r with num_folds %d' % (pred_set, use_tta, num_folds))

    for k_fold in range(num_folds):
        print('Processing fold ', k_fold)
        model_name = 'task%d_%s' % (task_idx, backbone_name)
        run_name = 'task%d_%s_k%d_v%s' % (task_idx, backbone_name, k_fold, version)
        model = backbone(backbone_name).segmentation_model(load_from=run_name)
        if use_tta:
            y_pred += inv_sigmoid(task1_tta_predict(model=model, img_arr=images))[:, :, :, 0]
        else:
            y_pred += inv_sigmoid(model.predict(images))[:, :, :, 0]

    print('Done predicting -- now doing post-processing')

    y_pred = y_pred / num_folds
    y_pred = sigmoid(y_pred)

    y_pred = task1_post_process(y_prediction=y_pred, threshold=0.5, gauss_sigma=2.)

    output_dir = submission_dir + '/task1_' + pred_set
    mkdir_if_not_exist([output_dir])

    for i_image, i_name in enumerate(image_names):