Search code examples
pythonnumpytensorflowkerasscikit-image

How to run model.predict() on a 5 dimensional array in Keras?


I've trained a binary classification model that takes a (128x128x3) image and then gives a binary value of 0 or 1. I then want to take a larger image, say (nxmx3), and apply a windowing function and have the model run a prediction on each window.

I used skimage.util.view_as_windows to convert a (1024x1024x3) image, into a (897,897,128,128,3) numpy array. I now want to run each (i, j, 128,128,3) window through my model, and then place it in the same location. In the end, I'd like a (897,897) array containing the probability of that class existing.

The way I'm doing it now requires a for-loop that takes nearly 1-2 minutes to run through, while slowing down as the list containing the model predictions gets larger.

Is there a way to vectorize this process? Perhaps flattening the numpy array, running model.predict() on it, and then creating a 2d-array with the same previous dimensions?


Solution

  • You can use fully convolutional networks which uses sliding window to predict output and it's not dependent on the input shape. replace your fully connected layers with convolutional layers with same output_shape and train it on (128x128x3) datasets.

    if you predict on 1024x1024 input image the network predict one label for each 128x128 regions.