コード例 #1
0
def classify_trees(width, height, img_path):
    model = get_dilated_unet(input_shape=(2048, 2048, 3),
                             mode='cascade',
                             filters=56,
                             n_class=3)
    model.load_weights('models/trees_model_weights.hdf5')

    classified_image = np.zeros(
        ((height // 2048 + 1) * 2048, (width // 2048 + 1) * 2048, 3))
    cv2.imwrite('data/' + folder_name + '/images/trees.jpg', classified_image)
    for img in os.listdir(img_path):
        classified_image = cv2.imread('data/' + folder_name +
                                      '/images/trees.jpg')
        if img.endswith('.tif'):
            im = Image.open(img_path + '/' + img)
            x_train = np.array(im, 'f')
            x_train1 = model.predict(x_train)
            x = np.zeros((2048, 2048, 3))
            x[np.where(x_train1[0, :, :, 0] > 0.50)] = [255, 0, 0]
            x[np.where(x_train1[0, :, :, 1] > 0.50)] = [0, 255, 0]
            x[np.where(x_train1[0, :, :, 2] > 0.50)] = [0, 0, 0]
            stri = img[:-4].split('_')
            classified_image[int(stri[2]):int(stri[2]) + 2048,
                             int(stri[1]):int(stri[1]) + 2048, :3] = x
            cv2.imwrite('data/' + folder_name + '/images/trees.jpg',
                        classified_image)
            del classified_image
            os.remove(img_path + '/' + img)

    classified_image = cv2.imread('data/' + folder_name + '/images/trees.jpg')
    classified_image = classified_image[0:height, 0:width]
    cv2.imwrite('data/' + folder_name + '/images/trees.jpg', classified_image)
コード例 #2
0
def classify_roads(width, height, img_path, ortho):
    model = get_dilated_unet(input_shape=(2048, 2048, 3),
                             mode='cascade',
                             filters=56,
                             n_class=3)
    model.load_weights('models/roads_model_weights.hdf5')
    ds = gdal.Open('data/' + folder_name + '/' + ortho)
    geo_trans = ds.GetGeoTransform()

    classified_image = np.zeros(
        ((height // 2048 + 1) * 2048, (width // 2048 + 1) * 2048, 3))
    cv2.imwrite('data/' + folder_name + '/images/roads.jpg', classified_image)
    for img in os.listdir(img_path):
        classified_image = cv2.imread('data/' + folder_name +
                                      '/images/roads.jpg')
        if img.endswith('.tif'):
            im = Image.open(img_path + '/' + img)
            x_train = np.array(im, 'f')
            x_train1 = model.predict(x_train)
            x = np.zeros((2048, 2048, 3))
            x[np.where(x_train1[0, :, :, 0] > 0.50)] = [255, 0, 0]
            x[np.where(x_train1[0, :, :, 1] > 0.50)] = [0, 255, 0]
            x[np.where(x_train1[0, :, :, 2] > 0.50)] = [0, 0, 0]
            stri = img[:-4].split('_')
            classified_image[int(stri[2]):int(stri[2]) + 2048,
                             int(stri[1]):int(stri[1]) + 2048, :3] = x
            cv2.imwrite('data/' + folder_name + '/images/roads.jpg',
                        classified_image)
            del classified_image
            os.remove(img_path + '/' + img)

    classified_image = cv2.imread('data/' + folder_name + '/images/roads.jpg')
    classified_image = classified_image[0:height, 0:width]
    cv2.imwrite('data/' + folder_name + '/images/roads.jpg', classified_image)
    mask2poly = vector.mask.mask_to_poly_geojson(classified_image,
                                                 channel_scaling=[1, -1, -1],
                                                 bg_threshold=100,
                                                 simplify=False)
    out = vector.polygon.georegister_px_df(mask2poly,
                                           affine_obj=geo_trans,
                                           crs='epsg:32618')
    out.to_file('data/' + folder_name + '/jsons/roads.json', driver='GeoJSON')

    mask2poly = vector.mask.mask_to_poly_geojson(classified_image,
                                                 channel_scaling=[-1, 1, -1],
                                                 bg_threshold=100,
                                                 simplify=False)
    out = vector.polygon.georegister_px_df(mask2poly,
                                           affine_obj=geo_trans,
                                           crs='epsg:32618')
    out.to_file('data/' + folder_name + '/jsons/footpaths.json',
                driver='GeoJSON')
コード例 #3
0
            x_batch = np.array(x_batch, np.float32) / 255.
            y_batch = np.array(y_batch, np.float32) / 255.

            yield x_batch, y_batch


if __name__ == '__main__':

    df_train = pd.read_csv(
        'Kaggle-Carvana-Image-Masking-Challenge-master/input/train_masks.csv')
    ids_train = df_train['img'].map(lambda s: s.split('.')[0])

    ids_train, ids_valid = train_test_split(ids_train, test_size=0.1)

    model = get_dilated_unet(input_shape=(1024, 1024, 3),
                             mode='cascade',
                             filters=32,
                             n_class=1)

    callbacks = [
        EarlyStopping(monitor='val_dice_coef',
                      patience=10,
                      verbose=1,
                      min_delta=1e-4,
                      mode='max'),
        ReduceLROnPlateau(monitor='val_dice_coef',
                          factor=0.2,
                          patience=5,
                          verbose=1,
                          epsilon=1e-4,
                          mode='max'),
        ModelCheckpoint(monitor='val_dice_coef',
コード例 #4
0
            x_batch = np.array(x_batch, np.float32) / 255.
            y_batch = np.array(y_batch, np.float32) / 255.

            yield x_batch, y_batch


if __name__ == '__main__':

    df_train = pd.read_csv('input/train_masks.csv')
    ids_train = df_train['img'].map(lambda s: s.split('.')[0])

    ids_train, ids_valid = train_test_split(ids_train, test_size=0.1)
    #    lr=0.0001,
    model = get_dilated_unet(input_shape=(1280, 1280, 3),
                             mode='cascade',
                             filters=32,
                             lr=0.0001,
                             n_class=1)

    callbacks = [
        EarlyStopping(monitor='val_dice_coef',
                      patience=10,
                      verbose=1,
                      min_delta=1e-4,
                      mode='max'),
        ReduceLROnPlateau(monitor='val_dice_coef',
                          factor=0.2,
                          patience=5,
                          verbose=1,
                          epsilon=1e-4,
                          mode='max'),