Coder Social home page Coder Social logo

demo.py about keras-retinanet HOT 7 CLOSED

fizyr avatar fizyr commented on July 24, 2024
demo.py

from keras-retinanet.

Comments (7)

hgaiser avatar hgaiser commented on July 24, 2024

Not a demo.py like the one in py-faster-rcnn, but you can look at this notebook for an example on how to visualize the results from Pascal VOC.

ps. if you want you can make a PR for one.

from keras-retinanet.

kaishijeng avatar kaishijeng commented on July 24, 2024

from keras-retinanet.

hgaiser avatar hgaiser commented on July 24, 2024

You need to let keras know where some of the custom objects are, by calling model = keras.models.load_model("model.h5", custom_objects=keras_retinanet.custom_objects).

However, as of writing this model loading is broken. The workaround is to create an identical model and call models.load_weights("model.h5", by_name=True). Or alternatively load the model definition first and then load weights. The rest can be seen in the notebook.

from keras-retinanet.

kaishijeng avatar kaishijeng commented on July 24, 2024

It works with models.load_weights("model.h5", by_name=True).
Now I am facing a different issue which use mode. predict with one input image. My code is below:

print('Creating model, this may take a second...')
model = create_model()
model.load_weights("snapshots/resnet50_voc_final.h5", by_name=True)
image = cv2.imread(sys.argv[1])
image -= min(image.flatten())
image /= max(image.flatten())
image *= 255
image = image.astype(np.uint8)
start = time.time()
predictions, detections = model.predict(image)
print("processing time: ", time.time() - start)

im = image.copy()
predicted_labels = np.argmax(detections[0, :, 4:], axis=1)
scores = np.max(detections[0, :, 4:], axis=1)

for idx, (label, score) in enumerate(zip(predicted_labels, scores)):
     if label == 0:
        continue
     b = detections[0, idx, :4].astype(int)
     cv2.rectangle(im, (b[0], b[1]), (b[2], b[3]), (0, 255, 0), 3)
     caption = "{} {:.3f}".format(class_names[label], score)
     cv2.putText(im, caption, (b[0], b[1] - 10), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 0, 0), 3)
     cv2.putText(im, caption, (b[0], b[1] - 10), cv2.FONT_HERSHEY_PLAIN, 1.5, (255, 255, 255), 2)
plt.figure(figsize=(15, 15))
plt.axis('off')
plt.imshow(im)
plt.show()

But I got the following errors:

Traceback (most recent call last):
File "./examples/demo-pascal.py", line 65, in
predictions, detections = model.predict(image)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1695, in predict
check_batch_axis=False)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 132, in _standardize_input_data
str(array.shape))
ValueError: Error when checking : expected input_1 to have 4 dimensions, but got array with shape (1288, 2048, 3)

from keras-retinanet.

hgaiser avatar hgaiser commented on July 24, 2024

You don't preprocess your image properly. It should firstly have 4 dimension ((batch, height, width, channels)). In addition the imagenet mean should be subtracted and the image should be rescaled by 1/255. For an example on how to preprocess an image you can look here : https://github.com/delftrobotics/keras-retinanet/blob/master/keras_retinanet/preprocessing/coco.py#L107

Perhaps I'll add a demo.py which follows this use case, but for now you could try replicating the preprocessing steps from the data generator.

PS. It's not so much that preprocessing has to be done this way, but it does have to be consistent with what you used during training.

from keras-retinanet.

kaishijeng avatar kaishijeng commented on July 24, 2024

Do you still have a plan to add demo.py?

Thanks,

from keras-retinanet.

hgaiser avatar hgaiser commented on July 24, 2024

Not really no. If you make a nice one we can add it to the repository.

from keras-retinanet.

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.