Coder Social home page Coder Social logo

Comments (12)

karolzak avatar karolzak commented on September 2, 2024 13
image = np.array(Image.open("test.jpg").resize((384, 384)))
print("shape: ", image.shape)
shape: (384, 384, 3)
pr_mask = model.predict(image).round()

Ok, I see your problem. When providing inputs for prediction you need to serve images in batches - if you want to fit just a single image you need to reshape it from (384, 384, 3) to (1, 384, 384, 3)

from keras-unet.

parcodepie avatar parcodepie commented on September 2, 2024 3

Indeed it was the problem.
Here is the new working code

image = np.array(Image.open("test.jpg").resize((384, 384)))
images_list = []
images_list.append(np.array(image))
x = np.asarray(images_list)
pr_mask = model.predict(x).round()

plt.imshow(
    pr_mask[0]
)
plt.show()

also I'm saving the image to the disk

data = Image.fromarray(np.reshape(pr_mask[0], (384, 384)) )
data = data.convert("L")
data.save('xxx.png')

the model needs more training but it's a good start

Thanks @karolzak for your time

from keras-unet.

karolzak avatar karolzak commented on September 2, 2024

Hi @parcodepie
Seems to me you made a mistake when resizing the test image. You used 348x348 instead of 384x384

from keras-unet.

parcodepie avatar parcodepie commented on September 2, 2024

Thanks @karolzak I edited it but it throws another error
Input 0 is incompatible with layer model: expected shape=(None, 384, 384, 3), found shape=(32, 384, 3)

from keras-unet.

karolzak avatar karolzak commented on September 2, 2024

Your input array still have a wrong shape. It expects a batch of arrays of size 384x384x3 while you're providing 32 arrays of size 384x3. Something is wrong with your code

from keras-unet.

parcodepie avatar parcodepie commented on September 2, 2024

I'm trying to test only one image what could went wrong?

here is the whole test code

import cv2
import numpy as np
from keras_net.keras_unet.utils import get_patches
from keras_unet.models import satellite_unet
from PIL import Image

model_file = 'model_satellite.h5'
input_shape = (384, 384, 3)

model = satellite_unet(
    input_shape
)

model.load_weights(model_file)
image = np.array(Image.open("test.jpg").resize((384, 384)))
print("shape: ", image.shape)
shape:  (384, 384, 3)
pr_mask = model.predict(image).round()

cv2.imshow(
    "mask: ", pr_mask
)

from keras-unet.

gordonhu608 avatar gordonhu608 commented on September 2, 2024

Thanks @karolzak I edited it but it throws another error
Input 0 is incompatible with layer model: expected shape=(None, 384, 384, 3), found shape=(32, 384, 3)

Hi, so how did u fix the issue when you are testing many images instead of just one?

from keras-unet.

hhhhhhhhhhhhhhhhho avatar hhhhhhhhhhhhhhhhho commented on September 2, 2024

hi, i have a same problem ,too

how fix that problem ?

Input 0 is incompatible with layer model: expected shape=(None, 64, 64, 3), found shape=(32, 64, 3) .. . .

from keras-unet.

karolzak avatar karolzak commented on September 2, 2024

Hello @hhhhhhhhhhhhhhhhho ! Great username :)

As it was mentioned begore you need to serve images as an array. The solution mentioned above will work:

image = np.array(Image.open("test.jpg").resize((384, 384)))
images_list = []
images_list.append(np.array(image))
x = np.asarray(images_list)
pr_mask = model.predict(x).round()

plt.imshow(
pr_mask[0]
)
plt.show()

from keras-unet.

Anne-Andresen avatar Anne-Andresen commented on September 2, 2024

Had the same problem solved it by adding an additional dimension
code before:

for i in range(8):
    img = io.imread(TEST_PATH + str(i) + '.png')
    print('shape: ', img.shape)
    pred = model.predict(img)
    prediction = np.squeeze(pred) > .5
    plt.imsave(os.path.join(save_path, str(i) + '_predicted.png'), prediction)

Code after:

for i in range(8):
    img = io.imread(TEST_PATH + str(i) + '.png')
    print('shape: ', img.shape)
    img = img[np.newaxis, :,  :, :]
    pred = model.predict(img)
    prediction = np.squeeze(pred) > .5
    plt.imsave(os.path.join(save_path, str(i) + '_predicted.png'), prediction)

from keras-unet.

tehreemnaqvi avatar tehreemnaqvi commented on September 2, 2024

@karolzak I have the same error:

ValueError: Input 0 is incompatible with layer model: expected shape=(None, 9, 4), found shape=(9, 4).

This is my original data shape and I'm trying to generate synthetic data from TimeGAN. When I run the model, got the above error.

from keras-unet.

karolzak avatar karolzak commented on September 2, 2024

@tehreemnaqvi Network is expecting batches of inputs of size (9,4). Please read through this thread. Your problem was already answered above.

from keras-unet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.