-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upgrade to tensorflow2 #3
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5fb8337
Upgrade to tensorflow2
atorch 8a42f6c
Add pyyaml requirement
atorch 881cc2b
Implement class weights for pixels using temporal sample weights
atorch cc0618d
Generator returns a triple now (X, y, weights)
atorch bb50eeb
Separate name for pixels_reshaped final layer used only when training…
atorch 916d09c
Turn epochs and steps per epoch back up
atorch 7fd0450
Move prediction out of fit_model.py
atorch 80861a0
Wider buffer for roads
atorch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
Fiona==1.8.6 | ||
Keras==2.2.4 | ||
PyYAML==5.3.1 | ||
Shapely==1.6.4.post2 | ||
black==19.3b0 | ||
matplotlib==3.0.3 | ||
numpy==1.16.3 | ||
pyproj==2.1.3 | ||
rasterio==1.0.22 | ||
scikit-learn==0.21.1 | ||
tensorflow==1.13.1 | ||
tensorflow==2.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
from functools import partial | ||
|
||
import keras | ||
from keras import backend as K | ||
from keras.models import Model | ||
from keras.layers import ( | ||
from tensorflow.keras import losses, optimizers | ||
from tensorflow.keras import backend as K | ||
from tensorflow.keras.models import Model | ||
from tensorflow.keras.layers import ( | ||
BatchNormalization, | ||
Conv2D, | ||
Dense, | ||
|
@@ -12,6 +12,7 @@ | |
GlobalAveragePooling2D, | ||
Input, | ||
MaxPooling2D, | ||
Reshape, | ||
UpSampling2D, | ||
concatenate, | ||
) | ||
|
@@ -21,6 +22,7 @@ | |
HAS_BUILDINGS, | ||
HAS_ROADS, | ||
PIXELS, | ||
PIXELS_RESHAPED, | ||
IS_MAJORITY_FOREST, | ||
MODAL_LAND_COVER, | ||
) | ||
|
@@ -68,29 +70,9 @@ def add_upsampling_block(input_layer, block_index, downsampling_conv2_layers): | |
return BatchNormalization()(conv2) | ||
|
||
|
||
def get_masked_categorical_crossentropy(cdl_indices_to_mask): | ||
def masked_categorical_crossentropy(y_true, y_pred): | ||
def get_keras_model(image_shape, label_encoder, include_reshape=True): | ||
|
||
# Used for pixel classifications: mask pixels whose class is in cdl_indices_to_mask | ||
# Note: shapes are (batch, image_shape, image_shape, n_classes) | ||
# TODO Speed this up, test it | ||
|
||
mask = K.ones_like(y_true[:, :, :, 0]) | ||
|
||
for cdl_index in cdl_indices_to_mask: | ||
cdl_index_indicators = K.cast( | ||
K.equal(y_true[:, :, :, cdl_index], 1), "float32" | ||
) | ||
mask -= cdl_index_indicators | ||
|
||
return K.categorical_crossentropy(y_true, y_pred) * mask | ||
|
||
return masked_categorical_crossentropy | ||
|
||
|
||
def get_keras_model(image_shape, label_encoder): | ||
|
||
# Note: model is fully convolutional, so image width and height can be arbitrary | ||
# Note: the model is fully convolutional: the input image width and height can be arbitrary | ||
input_layer = Input(shape=(None, None, image_shape[2])) | ||
|
||
# Note: Keep track of conv2 layers so that they can be connected to the upsampling blocks | ||
|
@@ -122,10 +104,23 @@ def get_keras_model(image_shape, label_encoder): | |
current_last_layer, index, downsampling_conv2_layers | ||
) | ||
|
||
output_pixels = Conv2D(n_classes, 1, activation="softmax", name=PIXELS)( | ||
pixels_final_conv = Conv2D(n_classes, 1, activation="softmax", name=PIXELS)( | ||
current_last_layer | ||
) | ||
|
||
if include_reshape: | ||
|
||
# Note: we are reshaping so that we can use weights with sample_weight_mode temporal when training | ||
# See https://github.com/keras-team/keras/issues/3653#issuecomment-557844450 | ||
# The model is **not** fully convolutional when include_reshape is True | ||
output_pixels = Reshape((image_shape[0] * image_shape[1], n_classes), name=PIXELS_RESHAPED)(pixels_final_conv) | ||
output_pixels_name = PIXELS_RESHAPED | ||
|
||
else: | ||
|
||
output_pixels = pixels_final_conv | ||
output_pixels_name = PIXELS | ||
|
||
model = Model( | ||
inputs=input_layer, | ||
outputs=[ | ||
|
@@ -139,21 +134,25 @@ def get_keras_model(image_shape, label_encoder): | |
|
||
print(model.summary()) | ||
|
||
nadam = keras.optimizers.Nadam() | ||
nadam = optimizers.Nadam() | ||
|
||
cdl_indices_to_mask = label_encoder.transform(CDL_CLASSES_TO_MASK) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Explain the idea behind masking in the readme: CDL developed contains roads, buildings, and (at 30 meter resolution) many NAIP pixels that are neither roads nor buildings, hence the idea of masking out CDL developed when computing loss. |
||
masked_categorical_crossentropy = get_masked_categorical_crossentropy( | ||
cdl_indices_to_mask | ||
) | ||
|
||
model.compile( | ||
optimizer=nadam, | ||
loss={ | ||
HAS_BUILDINGS: keras.losses.binary_crossentropy, | ||
HAS_ROADS: keras.losses.binary_crossentropy, | ||
IS_MAJORITY_FOREST: keras.losses.binary_crossentropy, | ||
MODAL_LAND_COVER: keras.losses.categorical_crossentropy, | ||
PIXELS: masked_categorical_crossentropy, | ||
HAS_BUILDINGS: losses.binary_crossentropy, | ||
HAS_ROADS: losses.binary_crossentropy, | ||
IS_MAJORITY_FOREST: losses.binary_crossentropy, | ||
MODAL_LAND_COVER: losses.categorical_crossentropy, | ||
output_pixels_name: losses.categorical_crossentropy, | ||
}, | ||
sample_weight_mode={ | ||
HAS_BUILDINGS: None, | ||
HAS_ROADS: None, | ||
IS_MAJORITY_FOREST: None, | ||
MODAL_LAND_COVER: None, | ||
output_pixels_name: "temporal", | ||
}, | ||
metrics=["accuracy"], | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got errors here when running fit_model.py on this branch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I replaced this with temporal sample weights (based on class): see keras-team/keras#3653 (comment)