question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Predict on images that are different size than training data

See original GitHub issue

From the steps in the ‘DeepPoseKit Step 3 - Train a model’ I have trained a model for locust pose mapping. However, the data used in the annotation set is 160x160. When following the steps in the ‘DeepPoseKit Step 4b - Predict on new data’ notebook, I get the following issue:

reader = VideoReader(HOME + '/Data/crop.mp4', batch_size=50, gray=True) predictions = model.predict(reader, verbose=1) reader.close()

`--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-11-d79f7abc10a7> in <module> 1 reader = VideoReader(HOME + ‘/Data/crop.mp4’, batch_size=50, gray=True) ----> 2 predictions = model.predict(reader, verbose=1) 3 reader.close()

~\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training.py in predict(self, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing) 907 max_queue_size=max_queue_size, 908 workers=workers, –> 909 use_multiprocessing=use_multiprocessing) 910 911 def reset_metrics(self):

~\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training_generator.py in predict(self, model, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing) 646 max_queue_size=max_queue_size, 647 workers=workers, –> 648 use_multiprocessing=use_multiprocessing) 649 650

~\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training_generator.py in model_iteration(model, data, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch, mode, batch_size, steps_name, **kwargs) 263 264 is_deferred = not model._is_compiled –> 265 batch_outs = batch_function(*batch_data) 266 if not isinstance(batch_outs, list): 267 batch_outs = [batch_outs]

~\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training_generator.py in predict_on_batch(x, y, sample_weights) 533 # 1, 2, or 3-tuples from generator 534 def predict_on_batch(x, y=None, sample_weights=None): # pylint: disable=unused-argument –> 535 return model.predict_on_batch(x) 536 537 f = predict_on_batch

~\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training.py in predict_on_batch(self, x) 1142 # Validate and standardize user data. 1143 inputs, _, _ = self._standardize_user_data( -> 1144 x, extract_tensors_from_dataset=True) 1145 # If self._distribution_strategy is True, then we are in a replica context 1146 # at this point.

~\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, batch_size, check_steps, steps_name, steps, validation_split, shuffle, extract_tensors_from_dataset) 2470 feed_input_shapes, 2471 check_batch_axis=False, # Don’t enforce the batch size. -> 2472 exception_prefix=‘input’) 2473 2474 # Get typespecs for the input data and sanitize it if necessary.

~\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix) 572 ': expected ’ + names[i] + ’ to have shape ’ + 573 str(shape) + ’ but got array with shape ’ + –> 574 str(data_shape)) 575 return data 576

ValueError: Error when checking input: expected input_1 to have shape (160, 160, 1) but got array with shape (512, 512, 1)`

I assume that this is because the model was trained on 160x160 pixel images, while I would like to predict on 512x512 images. I have tried to resize the densenet model with Keras using the following methods:

from kerassurgeon.operations import delete_layer, insert_layer my_input_tensor = Input(input_shape=(512, 512, 1)) from tfkerassurgeon import delete_layer, insert_layer model = delete_layer(model.layers[0]) model = insert_layer(model.layers[0], my_input_tensor) along with keras.add, model.layers, and model.inputs to no avail.

Is there an easy way to resize the model for inference/prediction? Or will I have to re-train the model on 512x512 images?

Thanks!

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jgravingcommented, Nov 15, 2019

Hi, currently the API only supports predicting on images that are the same size as the training images, but this will be changed in a coming update. However, this is not hard to do using the Keras API. Here is some code that should accomplish what you want:

from deepposekit.models import load_model
import tensorflow as tf

model = load_model("/path/to/saved/model.h5")
predict_model = model.predict_model
predict_model.layers.pop(0) # remove current input layer

inputs = tf.keras.layers.Input((512, 512, 1))
outputs = predict_model(inputs)
predict_model = tf.keras.Model(inputs, outputs)

x = np.random.randint(0, 255, (16, 512, 512, 1), dtype=np.uint8) 
prediction = predict_model.predict(x, verbose=True)
0reactions
jgravingcommented, Nov 18, 2019

Hi, I’m not sure exactly what you mean, but currently all of the images in the training set must be the same size.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Handle Images of Different Sizes in a Convolutional ...
Resize - Resize the variable-sized images to the same size image. We can easily implement this using tf.data input pipeline.
Read more >
Different input size for training and prediction in CNN for ...
In the paper they state that they use input images that are of size 448x448. Further they state that they crop random sub-images...
Read more >
Prediction on image with different size - python - Stack Overflow
Is is possible to run prediction on images with different sizes than the training data? I have trained a unet_learner on pictures of...
Read more >
CNN prediction on Large images - Development
Hi, I trained a CNN for doing image classification on (41, 41, 7) shape training images and the actual image size on which...
Read more >
The right way to segment large images by applying a trained ...
Divide images into patches of smaller sizes and then perform the prediction. ... Dataset info: Electron microscopy (EM) dataset from ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found