spectrewolf8 commited on
Commit
b1ea76e
1 Parent(s): 4ff5e1d

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +229 -0
README.md ADDED
@@ -0,0 +1,229 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ library_name: transformers
4
+ tags:
5
+ - Aerial Image Segmentation
6
+ - Road Detection
7
+ - Semantic Segmentation
8
+ - U-Net-50
9
+ - Computer Vision
10
+ - Remote Sensing
11
+ - Urban Planning
12
+ - Geographic Information Systems (GIS)
13
+ - Deep Learning
14
+ datasets:
15
+ - balraj98/massachusetts-roads-dataset
16
+ ---
17
+
18
+ # Model Card for Model ID
19
+
20
+ This model card provides an overview of a computer vision model designed for aerial image road segmentation using the U-Net-50 architecture. The model is intended to accurately identify and segment road networks from aerial imagery, crucial for applications in mapping and autonomous driving.
21
+
22
+ ## Model Details
23
+
24
+ ### Model Description
25
+
26
+ - **Developed by:** [spectrewolf8](https://github.com/Spectrewolf8)
27
+ - **Model type:** Computer-Vision/Semantic-segmentation
28
+ - **License:** MIT
29
+
30
+ ### Model Sources
31
+
32
+ - **Repository:** https://github.com/Spectrewolf8/aerial-image-road-segmentation-xp
33
+
34
+ ## Uses
35
+
36
+ ### Direct Use
37
+
38
+ This model can be used to segment road networks from aerial images without additional fine-tuning. It is applicable in scenarios where detailed and accurate road mapping is required.
39
+
40
+ ### Downstream Use
41
+
42
+ When fine-tuned on additional datasets, this model can be adapted for other types of semantic segmentation tasks, potentially enhancing applications in various remote sensing domains.
43
+
44
+ ## How to Get Started with the Model
45
+
46
+ Use the code below to get started with the model.
47
+
48
+ ```python
49
+ # Import necessary classes
50
+ from tensorflow.keras.models import load_model
51
+ from tensorflow.python.keras import layers
52
+ from tensorflow.python.keras.models import Sequential
53
+
54
+ import random
55
+ import numpy as np
56
+ import matplotlib.pyplot as plt
57
+ from tensorflow.keras.preprocessing.image import ImageDataGenerator
58
+
59
+ seed=24
60
+ batch_size= 8
61
+
62
+ # Load images for dataset generators from respective dataset libraries. The images and masks are returned as NumPy arrays
63
+
64
+ # Images can be further resized by adding target_size=(150, 150) with any size for your network to flow_from_directory parameters
65
+ # Our images are already cropped to 256x256 so traget_size parameter can be ignored
66
+
67
+ def image_and_mask_generator(image_dir, label_dir):
68
+ img_data_gen_args = dict(rescale = 1/255.)
69
+ mask_data_gen_args = dict()
70
+
71
+ image_data_generator = ImageDataGenerator(**img_data_gen_args)
72
+ image_generator = image_data_generator.flow_from_directory(image_dir,
73
+ seed=seed,
74
+ batch_size=batch_size,
75
+ classes = ["."],
76
+ class_mode=None #Very important to set this otherwise it returns multiple numpy arrays thinking class mode is binary.
77
+ )
78
+
79
+ mask_data_generator = ImageDataGenerator(**mask_data_gen_args)
80
+ mask_generator = mask_data_generator.flow_from_directory(label_dir,
81
+ classes = ["."],
82
+ seed=seed,
83
+ batch_size=batch_size,
84
+ color_mode = 'grayscale', #Read masks in grayscale
85
+ class_mode=None
86
+ )
87
+ # print processed image paths for vanity
88
+ print(image_generator.filenames[0:5])
89
+ print(mask_generator.filenames[0:5])
90
+
91
+ generator = zip(image_generator, mask_generator)
92
+ return generator
93
+
94
+ # Method to calculate Intersection over Union Accuracy Coefficient
95
+ def iou_coef(y_true, y_pred, smooth=1e-6):
96
+ intersection = tensorflow.reduce_sum(y_true * y_pred)
97
+ union = tensorflow.reduce_sum(y_true) + tensorflow.reduce_sum(y_pred) - intersection
98
+
99
+ return (intersection + smooth) / (union + smooth)
100
+
101
+ # Method to calculate Dice Accuracy Coefficient
102
+ def dice_coef(y_true, y_pred, smooth=1e-6):
103
+ intersection = tensorflow.reduce_sum(y_true * y_pred)
104
+ total = tensorflow.reduce_sum(y_true) + tensorflow.reduce_sum(y_pred)
105
+
106
+ return (2. * intersection + smooth) / (total + smooth)
107
+
108
+ # Method to calculate Dice Loss
109
+ def soft_dice_loss(y_true, y_pred):
110
+ return 1-dice_coef(y_true, y_pred)
111
+
112
+ # Method to create generator
113
+ def create_generator(zipped):
114
+ for (img, mask) in zipped:
115
+ yield (img, mask)
116
+
117
+ model_path = "path"
118
+ u_net_model = load_model(model_path, custom_objects={'soft_dice_loss': soft_dice_loss, 'dice_coef': dice_coef, "iou_coef": iou_coef})
119
+
120
+ test_generator = create_generator(image_and_mask_generator(output_test_image_dir,output_test_label_dir))
121
+
122
+ # Assuming create_generator is defined and provides images for prediction
123
+ images, ground_truth_masks = next(test_generator)
124
+
125
+ # Make predictions
126
+ predictions = u_net_model.predict(images)
127
+
128
+ # Apply threshold to predictions
129
+ thresh_val = 0.8
130
+ prediction_threshold = (predictions > thresh_val).astype(np.uint8)
131
+
132
+ # Visualize results
133
+ num_samples = min(10, len(images)) # Use at most 10 samples or the total number of images available
134
+ f = plt.figure(figsize=(15, 25))
135
+ for i in range(num_samples):
136
+ ix = random.randint(0, len(images) - 1) # Ensure ix is within range
137
+
138
+ f.add_subplot(num_samples, 4, i * 4 + 1)
139
+ plt.imshow(images[ix])
140
+ plt.title("Image")
141
+ plt.axis('off')
142
+
143
+ f.add_subplot(num_samples, 4, i * 4 + 2)
144
+ plt.imshow(np.squeeze(ground_truth_masks[ix]))
145
+ plt.title("Ground Truth")
146
+ plt.axis('off')
147
+
148
+ f.add_subplot(num_samples, 4, i * 4 + 3)
149
+ plt.imshow(np.squeeze(predictions[ix]))
150
+ plt.title("Prediction")
151
+ plt.axis('off')
152
+
153
+ f.add_subplot(num_samples, 4, i * 4 + 4)
154
+ plt.imshow(np.squeeze(prediction_threshold[ix]))
155
+ plt.title(f"Thresholded at {thresh_val}")
156
+ plt.axis('off')
157
+
158
+ plt.show()
159
+
160
+ ```
161
+
162
+
163
+ ## Training Details
164
+
165
+ ### Training Data
166
+
167
+ The model was trained on the Massachusetts Roads Dataset, which includes high-resolution aerial images with corresponding road segmentation masks. The images were preprocessed by cropping into 256x256 patches and converting masks to binary format.
168
+
169
+ ### Training Procedure
170
+
171
+ #### Preprocessing
172
+
173
+ - Images were cropped into 256x256 patches to manage memory usage and improve training efficiency.
174
+ - Masks were binarized to create clear road/non-road classifications.
175
+
176
+ #### Training Hyperparameters
177
+
178
+ - **Training regime:** FP32 precision
179
+ - **Epochs:** 2
180
+ - **Batch Size:** 8
181
+ - **Learning Rate:** 0.0001
182
+
183
+ ## Evaluation
184
+
185
+ ### Testing Data, Factors & Metrics
186
+
187
+ #### Testing Data
188
+
189
+ The model was evaluated using a separate set of aerial images and their corresponding ground truth masks from the dataset.
190
+
191
+ #### Metrics
192
+
193
+ - **Intersection over Union (IoU):** Measures the overlap between predicted and actual road areas.
194
+ - **Dice Coefficient:** Evaluates the similarity between predicted and ground truth masks.
195
+
196
+ ### Results
197
+
198
+ The model achieved 71% accuracy in segmenting road networks from aerial images, with evaluation metrics indicating good performance in distinguishing road features from non-road areas.
199
+
200
+ #### Summary
201
+
202
+ The U-Net-50 model effectively segments road networks, demonstrating its potential for practical applications in urban planning and autonomous systems.
203
+ ## Technical Specifications
204
+
205
+ ### Model Architecture and Objective
206
+
207
+ - **Architecture:** U-Net-50
208
+ - **Objective:** Road segmentation in aerial images
209
+
210
+ ### Compute Infrastructure
211
+
212
+ #### Hardware
213
+
214
+ - **Type:** [Specify Hardware, e.g., NVIDIA Tesla V100]
215
+ - **Configuration:** [Specify Configuration]
216
+
217
+ #### Software
218
+
219
+ - **Framework:** TensorFlow 2.x
220
+ - **Dependencies:** Keras, OpenCV, tifffile
221
+
222
+ **BibTeX:**
223
+
224
+ @misc{aerial-image-road-segmentation-with-U-NET-xp,
225
+ author = {spectrewolf8},
226
+ title = {Aerial Image Road Segmentation Using U-Net-50},
227
+ year = {2024},
228
+ howpublished = {\url{https://github.com/Spectrewolf8/aerial-image-road-segmentation-xp}},
229
+ }