Skip to content

Commit

Permalink
add cloud masking for S2 SR images
Browse files Browse the repository at this point in the history
  • Loading branch information
mjevans26 committed May 8, 2020
1 parent a5adc95 commit 25a3f9e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .spyproject/workspace.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ save_non_project_files = False

[main]
version = 0.1.0
recent_files = ['C:\\Users\\mevans\\OneDrive - Defenders of Wildlife\\repos\\Satellite_ComputerVision\\utils\\calibration.py', 'C:\\Users\\mevans\\OneDrive - Defenders of Wildlife\\repos\\Satellite_ComputerVision\\utils\\clouds.py', 'C:\\Users\\mevans\\OneDrive - Defenders of Wildlife\\repos\\Satellite_ComputerVision\\utils\\stats.py', 'C:\\Users\\mevans\\OneDrive - Defenders of Wildlife\\repos\\Satellite_ComputerVision\\utils\\processing.py']
recent_files = ['C:\\Users\\mevans\\OneDrive - Defenders of Wildlife\\repos\\Satellite_ComputerVision\\utils\\calibration.py', 'C:\\Users\\mevans\\OneDrive - Defenders of Wildlife\\repos\\Satellite_ComputerVision\\utils\\clouds.py', 'C:\\Users\\mevans\\OneDrive - Defenders of Wildlife\\repos\\Satellite_ComputerVision\\utils\\stats.py', 'C:\\Users\\mevans\\OneDrive - Defenders of Wildlife\\repos\\Satellite_ComputerVision\\utils\\processing.py', 'C:\\Users\\mevans\\OneDrive - Defenders of Wildlife\\repos\\Satellite_ComputerVision\\utils\\model.py']

17 changes: 17 additions & 0 deletions utils/clouds.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ def rescale(img, exp, thresholds):
#return img.subtract(thresholds[0]).divide(thresholds[1]-thresholds[0])
return img.expression(exp, {'img': img}).subtract(thresholds[0]).divide(thresholds[1] - thresholds[0])

def maskS2SR(img):
"""
Apply built in masks to Sentinel-2 surface reflectance imagery
Parameters:
img (ee.Image): Sentinel-2 level 2A surface reflectange image
Returns:
ee.Image: masked image
"""
scored = basicQA(img)
maskBand = img.select('SCL')
cloudMask = maskBand.neq(8).And(maskBand.neq(9))
waterMask = maskBand.neq(6)
cirrusMask = maskBand.neq(10)
snowMask = maskBand.neq(11)
darkMask = maskBand.neq(2).And(maskBand.neq(3))
return scored.updateMask(cloudMask.And(waterMask).And(cirrusMask).And(snowMask).And(darkMask))

def waterScore(img):
"""
Calculate a water likelihood score [0, 1]
Expand Down
32 changes: 16 additions & 16 deletions utils/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,26 @@ def get_model(depth, optim, mets):
Returns:
tf.keras.model: compiled U-Net model
"""
inputs = layers.Input(shape=[None, None, len(BANDS)]) # 256
encoder0_pool, encoder0 = encoder_block(inputs, 32) # 128
encoder1_pool, encoder1 = encoder_block(encoder0_pool, 64) # 64
encoder2_pool, encoder2 = encoder_block(encoder1_pool, 128) # 32
encoder3_pool, encoder3 = encoder_block(encoder2_pool, 256) # 16
encoder4_pool, encoder4 = encoder_block(encoder3_pool, 512) # 8
center = conv_block(encoder4_pool, 1024) # center
decoder4 = decoder_block(center, encoder4, 512) # 16
decoder3 = decoder_block(decoder4, encoder3, 256) # 32
decoder2 = decoder_block(decoder3, encoder2, 128) # 64
decoder1 = decoder_block(decoder2, encoder1, 64) # 128
decoder0 = decoder_block(decoder1, encoder0, 32) # 256
outputs = layers.Conv2D(1, (1, 1), activation='sigmoid')(decoder0)
inputs = layers.Input(shape=[None, None, len(BANDS)]) # 256
encoder0_pool, encoder0 = encoder_block(inputs, 32) # 128
encoder1_pool, encoder1 = encoder_block(encoder0_pool, 64) # 64
encoder2_pool, encoder2 = encoder_block(encoder1_pool, 128) # 32
encoder3_pool, encoder3 = encoder_block(encoder2_pool, 256) # 16
encoder4_pool, encoder4 = encoder_block(encoder3_pool, 512) # 8
center = conv_block(encoder4_pool, 1024) # center
decoder4 = decoder_block(center, encoder4, 512) # 16
decoder3 = decoder_block(decoder4, encoder3, 256) # 32
decoder2 = decoder_block(decoder3, encoder2, 128) # 64
decoder1 = decoder_block(decoder2, encoder1, 64) # 128
decoder0 = decoder_block(decoder1, encoder0, 32) # 256
outputs = layers.Conv2D(1, (1, 1), activation='sigmoid')(decoder0)

model = models.Model(inputs=[inputs], outputs=[outputs])
model = models.Model(inputs=[inputs], outputs=[outputs])

model.compile(
model.compile(
optimizer=optim,
loss = weighted_bce,
#loss=losses.get(LOSS),
metrics=[metrics.get(metric) for metric in mets])

return model
return model

0 comments on commit 25a3f9e

Please sign in to comment.