I'm new to DeepLab, wanted to change the metric from "accuracy" to "MeanIoU" in model.compile() function, but I'm getting this ValueError:
Dimension 0 in both shapes must be equal, but are 262144 and 524288. From merging Shape 0 with other shapes. for '{{node confusion_matrix/stack_1}} = Pack[N=2, T=DT_INT64, axis=1 (confusion_matrix/control_dependency_2, confusion_matrix/control_dependency_3)' with input shapes: [262144], [524288].
I'm using DeepLabV3+ implementation in Tensorflow Keras library here is the link: deepLabV3+
I am using my own Dataset for training consisting of 4500 orthophotomaps with masks and using only 2 classes for the segmantation (buildings and the rest as a background).
IMAGE_SIZE = 256, BATCH_SIZE = 4, NUM_CLASSES = 2
Everything works fine, but I'd like to change the metric, since IoU would be much better, but for the love of god I cannot work it out what is wrong with the confusion matrix (I presume).
Firstly I tried using tf.metrics.MeanIoU(num_classes=2), but this gives confusion matrix error.
Secondly I tried implementing my own IoU metric before the model.compile() and using it there, here it is:
from keras import backend as K
def iou_coef(y_true, y_pred, smooth=1):
intersection = K.sum(K.abs(y_true * y_pred), axis = [1,2,3])
union = K.sum(y_true,[1,2,3]) + K.sum(y_pred,[1,2,3])-intersection
iou = K.mean((intersection + smooth) / (union + smooth), axis=0)
return iou
model.compile(
optimizer = keras.optimizer.Adam(learning_rate=0.001),
loss = loss,
metrics = [iou_coef]
)
Actually after using this implementation, training resolves, but then the metric gives almost random values outside of range (0,1), like -6.321 or 2.341, which isn't very helpful either.
I feel like I'm missing something basic here, any help would be appreciated!