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
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
#Linear metadata model for testing purposes from comet_ml import Experiment import tensorflow as tf from DeepTreeAttention.trees import AttentionModel from DeepTreeAttention.models import metadata from DeepTreeAttention.callbacks import callbacks import pandas as pd model = AttentionModel(config="/home/b.weinstein/DeepTreeAttention/conf/tree_config.yml") model.create() #Log config experiment = Experiment(project_name="neontrees", workspace="bw4sz") experiment.log_parameters(model.config["train"]) experiment.log_parameters(model.config["evaluation"]) experiment.log_parameters(model.config["predict"]) experiment.add_tag("HSI") ##Train #Train see config.yml for tfrecords path with weighted classes in cross entropy model.read_data() class_weight = model.calc_class_weight() ##Train subnetwork experiment.log_parameter("Train subnetworks", True) with experiment.context_manager("HSI_spatial_subnetwork"): print("Train HSI spatial subnetwork") model.read_data(mode="HSI_submodel") model.train(submodel="spatial", sensor="hyperspectral",class_weight=[class_weight, class_weight, class_weight], experiment=experiment)
def mod(tmpdir): mod = AttentionModel(config="conf/tree_config.yml") train_dir = tmpdir.mkdir("train") predict_dir = tmpdir.mkdir("predict") label_file = "{}/label_file.csv".format(train_dir) #create a fake label file pd.DataFrame({ "taxonID": ["Ben", "Jon"], "label": [0, 1] }).to_csv(label_file) config = {} train_config = {} train_config["tfrecords"] = train_dir train_config["batch_size"] = 1 train_config["epochs"] = 1 train_config["steps"] = 1 train_config["gpus"] = 1 train_config["crop_size"] = 20 train_config["shuffle"] = True train_config["weighted_sum"] = False train_config["classes"] = 2 train_config["species_class_file"] = label_file #evaluation eval_config = {} eval_config["tfrecords"] = None eval_config["steps"] = 1 eval_config["ground_truth_path"] = "data/processed/test.shp" predict_config = {} predict_config["tfrecords"] = predict_dir config["train"] = train_config config["evaluation"] = eval_config config["predict"] = predict_config #Replace config for testing env for key, value in config.items(): for nested_key, nested_value in value.items(): mod.config[key][nested_key] = nested_value #Update the inits mod.RGB_size = mod.config["train"]["RGB"]["crop_size"] mod.HSI_size = mod.config["train"]["HSI"]["crop_size"] mod.HSI_channels = 369 mod.RGB_channels = 3 mod.extend_HSI_box = mod.config["train"]["HSI"]["extend_box"] mod.classes_file = label_file mod.train_shp = pd.DataFrame({ "taxonID": ["Jon", "Ben"], "siteID": [0, 1], "domainID": [0, 1], "plotID": [0, 1], "canopyPosition": ["a", "b"], "scientific": ["genus species", "genus species"] }) mod.train_shp.index = [2, 7] mod.sites = 23 mod.domains = 15 #Create a model with input sizes mod.create() return mod