ValueError while implementing sample code
See original GitHub issueModel:
Sample Code:
from smooth_tiled_predictions import predict_img_with_smooth_windowing
# CNN's receptive field's border size: size of patches
window_size = 32
# Amount of categories predicted per pixels.
nb_classes = 16
# Load an image. Convention is channel_last, such as having an input_img.shape of: (x, y, nb_channels), where nb_channels is of 3 for regular RGB images.
input_img = test
# Use the algorithm. The `pred_func` is passed and will process all the image 8-fold by tiling small patches with overlap, called once with all those image as a batch outer dimension.
# Note that model.predict(...) accepts a 4D tensor of shape (batch, x, y, nb_channels), such as a Keras model.
predictions_smooth = predict_img_with_smooth_windowing(
input_img,
window_size=window_size,
subdivisions=2, # Minimal amount of overlap for windowing. Must be an even number.
nb_classes=nb_classes,
pred_func=(
lambda img_batch_subdiv: encoder.predict(image_to_neural_input(img_batch_subdiv))
)
)
Error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
G:\Programs\anaconda3\envs\py35\lib\site-packages\numpy\lib\arraypad.py in _normalize_shape(ndarray, shape, cast_to_int)
1035 try:
-> 1036 shape_arr = np.broadcast_to(shape_arr, (ndims, 2))
1037 except ValueError:
G:\Programs\anaconda3\envs\py35\lib\site-packages\numpy\lib\stride_tricks.py in broadcast_to(array, shape, subok)
172 """
--> 173 return _broadcast_to(array, shape, subok=subok, readonly=True)
174
G:\Programs\anaconda3\envs\py35\lib\site-packages\numpy\lib\stride_tricks.py in _broadcast_to(array, shape, subok, readonly)
127 (array,), flags=['multi_index', 'refs_ok', 'zerosize_ok'] + extras,
--> 128 op_flags=[op_flag], itershape=shape, order='C').itviews[0]
129 result = _maybe_view_as_subclass(array, broadcast)
ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (3,2) and requested shape (4,2)
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-34-4fa28d1e9a96> in <module>()
17 nb_classes=nb_classes,
18 pred_func=(
---> 19 lambda img_batch_subdiv: encoder.predict(image_to_neural_input(img_batch_subdiv))
20 )
21 )
E:\GitHub\MTP\implementation\autoencoders\smooth_tiled_predictions.py in predict_img_with_smooth_windowing(input_img, window_size, subdivisions, nb_classes, pred_func)
226 http://blog.kaggle.com/2017/05/09/dstl-satellite-imagery-competition-3rd-place-winners-interview-vladimir-sergey/
227 """
--> 228 pad = _pad_img(input_img, window_size, subdivisions)
229 pads = _rotate_mirror_do(pad)
230
E:\GitHub\MTP\implementation\autoencoders\smooth_tiled_predictions.py in _pad_img(img, window_size, subdivisions)
77 aug = int(round(window_size * (1 - 1.0/subdivisions)))
78 more_borders = ((aug, aug), (aug, aug), (0, 0))
---> 79 ret = np.pad(img, pad_width=more_borders, mode='reflect')
80 # gc.collect()
81
G:\Programs\anaconda3\envs\py35\lib\site-packages\numpy\lib\arraypad.py in pad(array, pad_width, mode, **kwargs)
1299
1300 narray = np.array(array)
-> 1301 pad_width = _validate_lengths(narray, pad_width)
1302
1303 allowedkwargs = {
G:\Programs\anaconda3\envs\py35\lib\site-packages\numpy\lib\arraypad.py in _validate_lengths(narray, number_elements)
1078
1079 """
-> 1080 normshp = _normalize_shape(narray, number_elements)
1081 for i in normshp:
1082 chk = [1 if x is None else x for x in i]
G:\Programs\anaconda3\envs\py35\lib\site-packages\numpy\lib\arraypad.py in _normalize_shape(ndarray, shape, cast_to_int)
1037 except ValueError:
1038 fmt = "Unable to create correctly shaped tuple from %s"
-> 1039 raise ValueError(fmt % (shape,))
1040
1041 # Cast if necessary
ValueError: Unable to create correctly shaped tuple from ((16, 16), (16, 16), (0, 0))
Any help is appreciated. Apology if i am creating a trivial issue
Issue Analytics
- State:
- Created 6 years ago
- Comments:6
Top Results From Across the Web
Python ValueError Exception Handling Examples - DigitalOcean
Here is a simple example to handle ValueError exception using ... Our program can raise ValueError in int() and math.sqrt() functions.
Read more >How to Fix ValueError Exceptions in Python - Rollbar
The Python ValueError is an exception that occurs when a function receives an argument of the correct data type but an inappropriate value....
Read more >ValueError while implementing the train_test_split
I'm following through the Machine Learning tutorial on Kaggle and I have a ValueError despite following the tutorial line by line.
Read more >What is ValueError in Python? - Educative.io
ValueError in Python is raised when a user gives an invalid value to a function but is of a valid argument. It usually...
Read more >How to Avoid ValueError in Python with Examples - eduCBA
This is a guide to Python ValueError. Here we discuss an introduction, how does it work, how to avoid valueError in python with...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
On reading the code i realized that the issue is not of any serious concern. I was passing already windowed image (image of x*y*channels converted into n*window_size*window_size*channels) to the code. Instead we need to pass the complete image array x*y*channels. Also for my case I am using an autoencoder. So I am not doing any classification. My model instead outputs n*x*y*channels2. So I need to give channels2 as number of classes in the input argument. This may be useful for anybody working with autoencoders.
Thanks for the awesome piece of code
@git-ankur-shukla Thank you for responding!
My code…
And I get this stack-trace:
Also note that I made the changes suggested in this thread: https://github.com/Vooban/Smoothly-Blend-Image-Patches/issues/1