Luecke commited on
Commit
6d70836
·
1 Parent(s): c82f030

current pipeline

Browse files
classification/classification_predict.py CHANGED
@@ -82,7 +82,7 @@ def classify(img_path):
82
  # Initialize your custom model
83
  model = CustomNet(num_ftrs, num_classes)
84
  # Load the trained model weights
85
- model.load_state_dict(torch.load('./fine_tuned:plant_classifier.pth'))
86
 
87
  # Predict the class probabilities
88
  class_probabilities = predict_single_image(image_path, model)
 
82
  # Initialize your custom model
83
  model = CustomNet(num_ftrs, num_classes)
84
  # Load the trained model weights
85
+ model.load_state_dict(torch.load('./classification/fine_tuned_plant_classifier.pth'))
86
 
87
  # Predict the class probabilities
88
  class_probabilities = predict_single_image(image_path, model)
detectree2model/predictions/predict.py CHANGED
@@ -53,15 +53,17 @@ def predict(tile_path, overlap_threshold, confidence_threshold, simplify_value,
53
  download_file(url=url, local_filename=trained_model)
54
 
55
  cfg = setup_cfg(update_model=trained_model)
56
- #cfg.MODEL.DEVICE = "cpu"
 
 
57
  predict_on_data(tile_path, predictor=DefaultPredictor(cfg))
58
 
59
  project_to_geojson(tile_path, tile_path + "predictions/", tile_path + "predictions_geo/")
60
  crowns = stitch_crowns(tile_path + "predictions_geo/", 1)
61
  clean = clean_crowns(crowns, overlap_threshold, confidence=confidence_threshold)
62
  clean = clean.set_geometry(clean.simplify(simplify_value))
63
- clean.to_file(store_path + "predicted_delineations.geojson")
64
 
65
- def run_detectree2(tif_input_path, tile_width=20, tile_height=20, tile_buffer=20, overlap_threshold=0.35, confidence_threshold=0.2, simplify_value=0.2, store_path='./train_outputs/'):
66
  tile_path = create_tiles(input_path=tif_input_path, tile_width=tile_width, tile_height=tile_height, tile_buffer=tile_buffer)
67
- predict(tile_path=tile_path, overlap_threshold=overlap_threshold, confidence_threshold=confidence_threshold, simplify_value=simplify_value, store_path=store_path)
 
53
  download_file(url=url, local_filename=trained_model)
54
 
55
  cfg = setup_cfg(update_model=trained_model)
56
+
57
+ # hash the following line if you have gpu support
58
+ cfg.MODEL.DEVICE = "cpu"
59
  predict_on_data(tile_path, predictor=DefaultPredictor(cfg))
60
 
61
  project_to_geojson(tile_path, tile_path + "predictions/", tile_path + "predictions_geo/")
62
  crowns = stitch_crowns(tile_path + "predictions_geo/", 1)
63
  clean = clean_crowns(crowns, overlap_threshold, confidence=confidence_threshold)
64
  clean = clean.set_geometry(clean.simplify(simplify_value))
65
+ clean.to_file(store_path + "detectree2_delin.geojson")
66
 
67
+ def run_detectree2(tif_input_path, store_path, tile_width=20, tile_height=20, tile_buffer=20, overlap_threshold=0.35, confidence_threshold=0.2, simplify_value=0.2):
68
  tile_path = create_tiles(input_path=tif_input_path, tile_width=tile_width, tile_height=tile_height, tile_buffer=tile_buffer)
69
+ predict(tile_path=tile_path, overlap_threshold=overlap_threshold, confidence_threshold=confidence_threshold, simplify_value=simplify_value, store_path=store_path)
main.py CHANGED
@@ -1,25 +1,77 @@
1
  from detectree2model.predictions.predict import run_detectree2
2
- from polygons_processing.postpprocess_detectree2 import postprocess, export_df_as_geojson
3
  from generate_tree_images.generate_tree_images import generate_tree_images
4
  from classification.classification_predict import classify
5
  import os
 
6
 
7
- if __name__=="__main__":
8
- tif_input = ""
9
- detectree2_output = run_detectree2(tif_input)
 
 
 
 
 
 
10
 
11
- processed_output_df = postprocess(detectree2_output)
12
- processed_geojson = '../polygons_processing/postprocessed_predictions'
13
 
14
- generate_tree_images(processed_geojson, tif_input)
15
- output_folder = '../generate_tree_images/tree_images'
 
 
 
16
 
17
- for file_name in os.listdir(output_folder):
18
- file_path = os.path.join(output_folder, file_name)
19
- probs = classify(file_path)
20
- #add probs to df and then convert to geojson again for final output
21
- processed_output_df['class_probs'] = probs
22
-
23
- final_output_path = 'result.geojson'
24
- export_df_as_geojson(processed_output_df, final_output_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
 
 
 
 
 
 
 
 
 
 
1
  from detectree2model.predictions.predict import run_detectree2
2
+ from polygons_processing.postpprocess_detectree2 import postprocess
3
  from generate_tree_images.generate_tree_images import generate_tree_images
4
  from classification.classification_predict import classify
5
  import os
6
+ import json
7
 
8
+ def row_to_feature(row):
9
+ feature = {
10
+ "id": row["id"],
11
+ "type": "Feature",
12
+ "properties": {"Confidence_score": row["Confidence_score"]},
13
+ "geometry": {"type": "Polygon", "coordinates": [row["coordinates"]]},
14
+ "species": row['species']
15
+ }
16
+ return feature
17
 
18
+ def export_geojson(df, filename):
19
+ features = [row_to_feature(row) for idx, row in df.iterrows()]
20
 
21
+ feature_collection = {
22
+ "type": "FeatureCollection",
23
+ "crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:EPSG::32720"}},
24
+ "features": features,
25
+ }
26
 
27
+ output_geojson = json.dumps(feature_collection)
28
+
29
+ with open(f"{filename}.geojson", "w") as f:
30
+ f.write(output_geojson)
31
+
32
+ print(f"GeoJSON data exported to '{filename}.geojson' file.")
33
+
34
+ """
35
+ tif_input: the file containing a tif that we are analyzing
36
+
37
+ tif_file_name: the file name of the tif input. tif_input is the folder in which the tif file lies
38
+ (detectree2 works with that) but generate_tree_images requires path including the file hence the file name is needed
39
+
40
+ output_directory: the directory were all in-between and final files are stored
41
+
42
+ generate_tree_images stores the cutout tree images in a separate folder
43
+ """
44
+
45
+ tif_input = "/Users/jonathanseele/ETH/Hackathons/EcoHackathon/WeCanopy/test/"
46
+ tif_file_name = "TreeCrownVectorDataset_761588_9673769_20_20_32720"
47
+
48
+ current_directory = os.getcwd()
49
+ output_directory = os.path.join(current_directory, "outputs")
50
+ if not os.path.exists(output_directory):
51
+ os.makedirs(output_directory)
52
+
53
+ run_detectree2(tif_input, store_path=output_directory)
54
+
55
+ processed_output_df = postprocess(output_directory + '/detectree2_delin.geojson')
56
+ processed_geojson = output_directory + '/processed_delin.geojson'
57
+
58
+ generate_tree_images(processed_geojson, tif_input)
59
+ output_folder = './tree_images'
60
+
61
+ all_top_3_list = [] # Initialize an empty list to accumulate all top_3 lists
62
+
63
+ for file_name in os.listdir(output_folder):
64
+ file_path = os.path.join(output_folder, file_name)
65
+ probs = classify(file_path)
66
+ top_3 = probs.head(3)
67
+ top_3_list = [[cls, prob] for cls, prob in top_3.items()]
68
 
69
+ # Accumulate the top_3_list for each file
70
+ all_top_3_list.append(top_3_list)
71
+
72
+ # Assign the accumulated top_3_list to the 'species' column of the dataframe
73
+ processed_output_df['species'] = all_top_3_list
74
+
75
+ final_output_path = 'result'
76
+ export_geojson(processed_output_df, final_output_path)
77
+
polygons_processing/postpprocess_detectree2.py CHANGED
@@ -351,6 +351,6 @@ def postprocess(prediction_geojson_path):
351
 
352
  df_res = process([df])
353
 
354
- export_df_as_geojson(df=df_res, filename="postprocessed_predictions")
355
 
356
  return df_res
 
351
 
352
  df_res = process([df])
353
 
354
+ export_df_as_geojson(df=df_res, filename="processed_delin")
355
 
356
  return df_res