ykamikawa / tf-keras-SegNet

SegNet including indices pooling for Semantic Segmentation with tensorflow and keras
178 stars 80 forks source link

Problems with MaxUnpooling2D native keras to tf.keras #12

Open ghost opened 5 years ago

ghost commented 5 years ago

Hey guys,

I am working on a project and try to use the SegNet architecture - and as I want to work on local host and on a distributed multi-server environment (got access to some GPUs) I want to work with Estimators.

But for this I have to switch from native keras

from keras.models import Model from keras.layers import Input from keras.layers.core import Activation, Reshape from keras.layers.convolutional import Convolution2D from keras.layers.normalization import BatchNormalization

to tensorflow-gpu 1.13.1 (pip3 install tensorflow-gpu) with tf.keras:

import tensorflow as tf
Convolution2D = tf.keras.layers.Convolution2D
BatchNormalization = tf.keras.layers.BatchNormalization
Activation = tf.keras.layers.Activation

Then I reduced the model and changed the output (as I have my labels in another format) to get my pipeline running at first:

def model(input_shape, n_labels, kernel=3, pool_size=(2, 2), output_mode="softmax"):

Convolution2D = tf.keras.layers.Convolution2D
BatchNormalization = tf.keras.layers.BatchNormalization
Activation = tf.keras.layers.Activation

# Encoder
inputs = tf.keras.layers.Input(shape=input_shape)

conv_1 = Convolution2D(64, (kernel, kernel), padding="same")(inputs)
conv_1 = BatchNormalization()(conv_1)
conv_1 = Activation("relu")(conv_1)

conv_2 = Convolution2D(64, (kernel, kernel), padding="same")(conv_1)
conv_2 = BatchNormalization()(conv_2)
conv_2 = Activation("relu")(conv_2)

# pool_1, mask_1 = MaxPoolingWithArgmax2D(pool_size)(conv_2)
# unpool_5 = MaxUnpooling2D(pool_size)([pool_1, mask_1])

conv_25 = Convolution2D(64, (kernel, kernel), padding="same")(conv_2)
conv_25 = BatchNormalization()(conv_25)
conv_25 = Activation("relu")(conv_25)

conv_26 = Convolution2D(n_labels, (1, 1), padding="valid")(conv_25)
conv_26 = BatchNormalization()(conv_26)
conv_26 = tf.keras.layers.Reshape(
    (input_shape[0], input_shape[1], n_labels),
    input_shape=(input_shape[0], input_shape[1], n_labels))(conv_26)

outputs = Activation(output_mode)(conv_26)
print("Building decoder done...")

model = tf.keras.models.Model(inputs=inputs, outputs=outputs, name="SegNet")

return model

This is working so far! Also with tf.keras instead of native keras!!

Now i want to check if upsampling with indices is working and therefore uncomment pool_1, mask_1 = MaxPoolingWithArgmax2D(pool_size)(conv_2) unpool_5 = MaxUnpooling2D(pool_size)([pool_1, mask_1])

and change input of conv_25 to unpool_5 conv_25 = Convolution2D(64, (kernel, kernel), padding="same")(unpool_5)

so this should downsample and upsample my tensors once.

But now it get following error in line of conv_25: "ValueError: The channel dimension of the inputs should be defined. Found None."

Can anybody please help me with this behavior!

Best regards,

Matt

ghost commented 5 years ago

Ok I fixed my problem with using "MaxPoolingWithArgmax2D" and "MaxUnpooling2D(Layer)" from this project: https://github.com/yselivonchyk/Tensorflow_WhatWhereAutoencoder

iamyixuan commented 4 years ago

Ok I fixed my problem with using "MaxPoolingWithArgmax2D" and "MaxUnpooling2D(Layer)" from this project: https://github.com/yselivonchyk/Tensorflow_WhatWhereAutoencoder

Thanks Matt, I ran into the same problem and then fixed it from that.

ghost commented 4 years ago

Thanks Matt, helps a lot