Exemplo n.º 1
0
def mod(tmpdir):
    mod = AttentionModel(config="conf/tree_config.yml")   
    mod.sites = 2
    mod.domains = 2    
    mod.RGB_channels = 3
    mod.HSI_channels = 3
    
    train_dir = tmpdir.mkdir("train")
    
    label_file = "{}/label_file.csv".format(train_dir)
    
    mod.config["train"]["tfrecords"] = train_dir
    mod.classes_file = "data/processed/species_class_labels.csv"
    
    domain = "D17"
    numeric_domain = domain_label_dict[domain]   

    site = "BART"    
    numeric_site = site_label_dict[site]
    
    created_records = mod.generate(shapefile=test_predictions, site=numeric_site, domain=numeric_domain, elevation=100/1000,
                                   HSI_sensor_path=test_sensor_tile,
                                   RGB_sensor_path=test_sensor_tile,
                                   train=True,
                                   chunk_size=2,
                                   savedir = mod.config["train"]["tfrecords"])
    
    #create a fake label file
    pd.DataFrame({"taxonID":["Ben","Jon"],"label":[0,1]}).to_csv(label_file)
    mod.classes_file = label_file
    
    mod.create()
    mod.ensemble(experiment=None)
        
    return mod
Exemplo n.º 2
0
def run(record):
    
    att = AttentionModel(config="/home/b.weinstein/DeepTreeAttention/conf/tree_config.yml")
    
    ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    site_classes_file = "{}/data/processed/site_class_labels.csv".format(ROOT)
    site_classdf  = pd.read_csv(site_classes_file)
    site_label_dict = site_classdf.set_index("siteID").label.to_dict()    
    
    domain_classes_file = "{}/data/processed/domain_class_labels.csv".format(ROOT)
    domain_classdf  = pd.read_csv(domain_classes_file)
    domain_label_dict = domain_classdf.set_index("domainID").label.to_dict()  
    
    species_classes_file = "{}/data/processed/species_class_labels.csv".format(ROOT)
    species_classdf  = pd.read_csv(species_classes_file)
    species_label_dict = species_classdf.set_index("taxonID").label.to_dict()
    
    rgb_pool = glob.glob(att.config["rgb_sensor_pool"], recursive=True)
    hyperspectral_pool = glob.glob(att.config["hyperspectral_sensor_pool"], recursive=True)
    
    #Convert h5 hyperspec
    renamed_record = record.replace("itc_predictions", "image")
    hyperspec_path = lookup_and_convert(shapefile=renamed_record, rgb_pool=rgb_pool, hyperspectral_pool=hyperspectral_pool, savedir=att.config["hyperspectral_tif_dir"])
    
    rgb_path = find_sensor_path(shapefile=renamed_record, lookup_pool=rgb_pool)
    
    #infer site
    site = site_from_path(renamed_record)
    numeric_site = site_label_dict[site] 
    
    domain = domain_from_path(renamed_record)
    numeric_domain = domain_label_dict[domain] 
    
    #infer elevation
    h5_path = find_sensor_path(shapefile=renamed_record, lookup_pool=hyperspectral_pool)    
    elevation = elevation_from_tile(h5_path)
    
    #Generate record when complete   
    df = pd.read_csv(record)
    
    # hot fix the heights for the moment.
    heights = np.repeat(10, df.shape[0])
    
    tfrecords = att.generate(
        csv_file=record,
        HSI_sensor_path=hyperspec_path,
        RGB_sensor_path=rgb_path,
        chunk_size=500,
        train=True,
        domain=numeric_domain,
        site=numeric_site,
        heights=heights,
        elevation=elevation,
        label_column="filtered_taxonID",
        species_label_dict=species_label_dict
    )
    
    return tfrecords
Exemplo n.º 3
0
def mod(tmpdir):
    mod = AttentionModel(config="conf/tree_config.yml")
    mod.sites = 2
    mod.domains = 2
    mod.RGB_channels = 3
    mod.HSI_channels = 3

    train_dir = tmpdir.mkdir("train")

    label_file = "{}/label_file.csv".format(train_dir)

    shp = gpd.read_file(test_predictions)
    mod.config["train"]["tfrecords"] = train_dir
    mod.classes_file = "data/processed/species_class_labels.csv"
    created_records = mod.generate(shapefile=test_predictions,
                                   site=0,
                                   domain=1,
                                   elevation=100,
                                   heights=np.random.random(shp.shape[0]),
                                   HSI_sensor_path=test_sensor_tile,
                                   RGB_sensor_path=test_sensor_tile,
                                   train=True,
                                   chunk_size=2)

    #create a fake label file
    pd.DataFrame({
        "taxonID": ["Ben", "Jon"],
        "label": [0, 1]
    }).to_csv(label_file)
    mod.classes_file = label_file

    mod.create()
    mod.ensemble(experiment=None, train=False)

    #turn ensemble model into a feature extractor of the 2nd to last layer.
    mod.ensemble_model = tfk.Model(
        mod.ensemble_model.inputs,
        mod.ensemble_model.get_layer("submodel_concat").output)

    return mod
Exemplo n.º 4
0
def run(record, savedir, raw_box_dir):
    """Take a plot of deepforest prediction (see prepare_field_data.py) and generate crops for training/evalution"""
    #Read record
    df = gpd.read_file(record)
    
    #get bounding boxes from the surrounding trees
    basename = os.path.basename(record)
    raw_boxes ="{}/{}".format(raw_box_dir, basename)
    
    att = AttentionModel(config="/home/b.weinstein/DeepTreeAttention/conf/tree_config.yml")
    ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    
    site_classes_file = "{}/data/processed/site_class_labels.csv".format(ROOT)
    site_classdf  = pd.read_csv(site_classes_file)
    site_label_dict = site_classdf.set_index("siteID").label.to_dict()    
    
    domain_classes_file = "{}/data/processed/domain_class_labels.csv".format(ROOT)
    domain_classdf  = pd.read_csv(domain_classes_file)
    domain_label_dict = domain_classdf.set_index("domainID").label.to_dict()  
    
    species_classes_file = "{}/data/processed/species_class_labels.csv".format(ROOT)
    species_classdf  = pd.read_csv(species_classes_file)
    species_label_dict = species_classdf.set_index("taxonID").label.to_dict()
    
    rgb_pool = glob.glob(att.config["rgb_sensor_pool"], recursive=True)
    hyperspectral_pool = glob.glob(att.config["hyperspectral_sensor_pool"], recursive=True)
    
    #Convert h5 hyperspec
    hyperspec_path = lookup_and_convert(bounds=df.total_bounds, rgb_pool=rgb_pool, hyperspectral_pool=hyperspectral_pool, savedir=att.config["hyperspectral_tif_dir"])
    rgb_path = find_sensor_path(bounds=df.total_bounds, lookup_pool=rgb_pool)
    
    #infer site, only 1 per plot.
    site = df.siteID.unique()[0]
    numeric_site = site_label_dict[site] 
    
    domain = df.domainID.unique()[0]
    numeric_domain = domain_label_dict[domain] 
    
    #infer elevation
    h5_path = find_sensor_path(bounds=df.total_bounds, lookup_pool=hyperspectral_pool)    
    elevation = elevation_from_tile(h5_path)
    
    att.ensemble_model = tf.keras.models.load_model("{}/Ensemble.h5".format(att.config["neighbors"]["model_dir"]), custom_objects={"WeightedSum":WeightedSum})    
    ensemble_model = tf.keras.Model(att.ensemble_model.inputs, att.ensemble_model.get_layer("ensemble_learn").output)
    
    #Generate record when complete   
    tfrecords = att.generate(
        shapefile=record,
        raw_boxes=raw_boxes,
        HSI_sensor_path=hyperspec_path,
        RGB_sensor_path=rgb_path,
        chunk_size=500,
        train=True,
        domain=numeric_domain,
        site=numeric_site,
        elevation=elevation,
        label_column="taxonID",
        species_label_dict=species_label_dict,
        ensemble_model=None,
        savedir=savedir
    )
    
    return tfrecords