Skip to content

Commit

Permalink
fixed cityscapes weights loading
Browse files Browse the repository at this point in the history
  • Loading branch information
bonlime committed Mar 19, 2019
1 parent ce09324 commit 6bcd9c6
Show file tree
Hide file tree
Showing 4 changed files with 653 additions and 475 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
models/
weights/
__pycache__
test.ipynb
test.ipynb
.ipynb_checkpoints
25 changes: 12 additions & 13 deletions .ipynb_checkpoints/model-checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
https://github.com/tensorflow/models/tree/master/research/deeplab
On Pascal VOC, original model gets to 84.56% mIOU
Now this model is only available for the TensorFlow backend,
due to its reliance on `SeparableConvolution` layers, but Theano will add
this layer soon.
MobileNetv2 backbone is based on this repo:
https://github.com/JonathanCMitchell/mobilenet_v2_keras
Expand Down Expand Up @@ -222,23 +218,23 @@ def _inverted_res_block(inputs, expansion, stride, alpha, filters, block_id, ski
return x


def Deeplabv3(weights='pascal_voc', input_tensor=None, input_shape=(512, 512, 3), classes=21, backbone='mobilenetv2', OS=16, alpha=1.):
def Deeplabv3(weights='pascal_voc', input_tensor=None, input_shape=(512, 512, 3), classes=21, backbone='mobilenetv2', OS=16, alpha=1., activation=None):
""" Instantiates the Deeplabv3+ architecture
Optionally loads weights pre-trained
on PASCAL VOC. This model is available for TensorFlow only,
and can only be used with inputs following the TensorFlow
data format `(width, height, channels)`.
on PASCAL VOC or Cityscapes. This model is available for TensorFlow only.
# Arguments
weights: one of 'pascal_voc' (pre-trained on pascal voc),
'cityscapes' (pre-trained on cityscape) or None (random initialization)
input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
to use as image input for the model.
input_shape: shape of input image. format HxWxC
PASCAL VOC model was trained on (512,512,3) images
classes: number of desired classes. If classes != 21,
last layer is initialized randomly
PASCAL VOC model was trained on (512,512,3) images. None is allowed as shape/width
classes: number of desired classes. PASCAL VOC has 21 classes, Cityscapes has 19 classes.
If number of classes not aligned with the weights used, last layer is initialized randomly
backbone: backbone to use. one of {'xception','mobilenetv2'}
activation: optional activation to add to the top of the network.
One of 'softmax', 'sigmoid' or None
OS: determines input_shape/feature_extractor_output ratio. One of {8,16}.
Used only for xception backbone.
alpha: controls the width of the MobileNetV2 network. This is known as the
Expand All @@ -249,7 +245,7 @@ def Deeplabv3(weights='pascal_voc', input_tensor=None, input_shape=(512, 512, 3)
of filters in each layer.
- If `alpha` = 1, default number of filters from the paper
are used at each layer.
Used only for mobilenetv2 backbone
Used only for mobilenetv2 backbone. Pretrained is only available for alpha=1.
# Returns
A Keras model instance.
Expand Down Expand Up @@ -434,7 +430,7 @@ def Deeplabv3(weights='pascal_voc', input_tensor=None, input_shape=(512, 512, 3)
depth_activation=True, epsilon=1e-5)

# you can use it with arbitary number of classes
if classes == 21:
if (weights == 'pascal_voc' and classes == 21) or (weights == 'cityscapes' and classes == 19):
last_layer_name = 'logits_semantic'
else:
last_layer_name = 'custom_logits_semantic'
Expand All @@ -451,6 +447,9 @@ def Deeplabv3(weights='pascal_voc', input_tensor=None, input_shape=(512, 512, 3)
else:
inputs = img_input

if activation in {'softmax','sigmoid'}:
x = tf.keras.layers.Activation(activation)(x)

model = Model(inputs, x, name='deeplabv3plus')

# load weights
Expand Down
Loading

0 comments on commit 6bcd9c6

Please sign in to comment.