def polygons2mask_layer(height, width, polygon, image_id, i):
    """

    :param height:
    :param width:
    :param polygons:
    :return:
    """

    x_max, y_min = extra_functions._get_xmax_ymin(image_id)
    x_scaler, y_scaler = extra_functions.get_scalers(height, width, x_max,
                                                     y_min)

    polygons = shapely.affinity.scale(polygon,
                                      xfact=x_scaler,
                                      yfact=y_scaler,
                                      origin=(0, 0, 0))
    img_mask = np.zeros((height, width), np.uint8)

    if not polygons:
        return img_mask

    int_coords = lambda x: np.array(x).round().astype(np.int32)
    exteriors = [int_coords(poly.exterior.coords) for poly in polygons]
    interiors = [
        int_coords(pi.coords) for poly in polygons for pi in poly.interiors
    ]

    cv2.fillPoly(img_mask, exteriors, 11 - i)
    cv2.fillPoly(img_mask, interiors, 0)

    return img_mask
def predict_poly(model, threashold1, threashold2, result, first_class):
    predicted_mask = extra_functions.make_prediction_cropped(
        model,
        image,
        initial_size=(112, 112),
        final_size=(112 - 32, 112 - 32),
        num_masks=num_mask_channels,
        num_channels=num_channels)

    image_v = np.flipud(image)
    predicted_mask_v = extra_functions.make_prediction_cropped(
        model,
        image_v,
        initial_size=(112, 112),
        final_size=(112 - 32, 112 - 32),
        num_masks=2,
        num_channels=num_channels)

    image_h = np.fliplr(image)
    predicted_mask_h = extra_functions.make_prediction_cropped(
        model,
        image_h,
        initial_size=(112, 112),
        final_size=(112 - 32, 112 - 32),
        num_masks=2,
        num_channels=num_channels)

    image_s = np.rot90(image)
    predicted_mask_s = extra_functions.make_prediction_cropped(
        model,
        image_s,
        initial_size=(112, 112),
        final_size=(112 - 32, 112 - 32),
        num_masks=2,
        num_channels=num_channels)

    new_mask = np.power(
        predicted_mask * np.flipud(predicted_mask_v) *
        np.fliplr(predicted_mask_h) * np.rot90(predicted_mask_s, 3), 0.25)

    x_scaler, y_scaler = extra_functions.get_scalers(H, W, x_max, y_min)

    mask_channel = first_class
    result += [(image_id, mask_channel + 1,
                mask2poly(new_mask[:, :, 0], threashold1, x_scaler, y_scaler))]
    mask_channel = first_class + 1
    result += [(image_id, mask_channel + 1,
                mask2poly(new_mask[:, :, 1], threashold2, x_scaler, y_scaler))]
    return result
        num_masks=1,
        num_channels=num_channels)

    image_s = image.swapaxes(1, 2)
    predicted_mask_s = extra_functions.make_prediction_cropped(
        model,
        image_s,
        initial_size=(112, 112),
        final_size=(112 - 32, 112 - 32),
        num_masks=1,
        num_channels=num_channels)

    new_mask = np.power(
        predicted_mask * flip_axis(predicted_mask_v, 1) *
        flip_axis(predicted_mask_h, 2) * predicted_mask_s.swapaxes(1, 2), 0.25)

    x_scaler, y_scaler = extra_functions.get_scalers(H, W, x_max, y_min)

    mask_channel = 0
    result += [(image_id, mask_channel + 1,
                mask2poly(new_mask, threashold, x_scaler, y_scaler))]

submission = pd.DataFrame(result,
                          columns=['ImageId', 'ClassType', 'MultipolygonWKT'])

sample = sample.drop('MultipolygonWKT', 1)
submission = sample.merge(submission, on=['ImageId', 'ClassType'],
                          how='left').fillna('MULTIPOLYGON EMPTY')

submission.to_csv('temp_building_3.csv', index=False)
                                      yfact=1.0 / y_scaler,
                                      origin=(0, 0, 0))
    return shapely.wkt.dumps(polygons)


result = []
for image_id in tqdm(test_ids):
    rgb = tiff.imread('../data/three_band/{}.tif'.format(image_id))
    _, height, width = rgb.shape
    rgb = np.rollaxis(rgb, 0, 3)
    m = tiff.imread('../data/sixteen_band/{}_M.tif'.format(image_id))
    # get our index
    CCCI = CCCI_index(m, rgb)

    x_max, y_min = extra_functions._get_xmax_ymin(image_id)
    x_scaler, y_scaler = extra_functions.get_scalers(height, width, x_max,
                                                     y_min)

    # you can look on histogram and pick your favorite threshold value(0.11 is my best)
    predicted_mask = (CCCI > 0.11).astype(np.float32)

    if predicted_mask.sum() > 680000:
        result += [(image_id, 8, 'MULTIPOLYGON EMPTY')]
    else:
        result += [(image_id, 8, mask2poly(predicted_mask, x_scaler,
                                           y_scaler))]

submission = pd.DataFrame(result,
                          columns=['ImageId', 'ClassType', 'MultipolygonWKT'])

sample = sample.drop('MultipolygonWKT', 1)
submission = sample.merge(submission, on=['ImageId', 'ClassType'],