Tensorflow - keras - Shape errors with 'strided_slice' (used resized MNIST dataset)










1














I am going to make some GAN-Model Tester using tf.keras with MNIST hand-writed digits dataset. Because my model is going to be used in 128x128 images, I resized MNIST dataset to 128x128x1. but, the program makes some errors, that I never seen.



(x_train, _), (_, _) = mnist.load_data()
x_train = (x_train.astype(np.float32) - 127.5) / 127.5
x_train = tf.image.resize_images(x_train, [128, 128])

idx = np.random.randint(0, x_train.shape[0], batch_size) # picks some data, count is batch_size=32.
imgs = x_train[idx] # This line made errors


The last line made two errors:




tensorflow.python.framework.errors_impl.InvalidArgumentError: Shape must be rank 1 but is rank 2 for 'strided_slice_1' (op: 'StridedSlice') with input shapes: [60000,128,128,1], [1,32], [1,32], [1].




and,




ValueError: Shape must be rank 1 but is rank 2 for 'strided_slice_1' (op: 'StridedSlice') with input shapes: [60000,128,128,1], [1,32], [1,32], [1].




I think the number '32' means batch_size(=32).



I tried to find about this error, but I couldn't find like this error.



I don't have any ideas to solve this problems (because I started to use keras a week ago, before I used pytorch).










share|improve this question























  • what is (1,32), 32 isn't the batch size? Please print(idx).
    – Geeocode
    Nov 12 '18 at 13:51










  • idx is the batch_size-sized list with random integer values. it is used at picking up 32 random MNIST data. example: [35904 26046 21148 28562 56414 31930 44948 53721 2118 30226 6292 57618 39161 46354 9053 46421 44620 36931 8266 48759 42023 40365 33027 13278 57609 3804 57904 4377 3517 30322 16445 47825]
    – Xsolute Lim
    Nov 12 '18 at 14:23










  • I think your idx if you printed it would be of shape (1,32) from the error message, that's why I asked you to print it.
    – Geeocode
    Nov 12 '18 at 15:24










  • The result of print(idx) is the example above, so I printed idx.shape: (32,).
    – Xsolute Lim
    Nov 12 '18 at 22:40










  • you wrote: imgs = x_train[idx] # This line made errors, not print(idx)
    – Geeocode
    Nov 12 '18 at 22:58















1














I am going to make some GAN-Model Tester using tf.keras with MNIST hand-writed digits dataset. Because my model is going to be used in 128x128 images, I resized MNIST dataset to 128x128x1. but, the program makes some errors, that I never seen.



(x_train, _), (_, _) = mnist.load_data()
x_train = (x_train.astype(np.float32) - 127.5) / 127.5
x_train = tf.image.resize_images(x_train, [128, 128])

idx = np.random.randint(0, x_train.shape[0], batch_size) # picks some data, count is batch_size=32.
imgs = x_train[idx] # This line made errors


The last line made two errors:




tensorflow.python.framework.errors_impl.InvalidArgumentError: Shape must be rank 1 but is rank 2 for 'strided_slice_1' (op: 'StridedSlice') with input shapes: [60000,128,128,1], [1,32], [1,32], [1].




and,




ValueError: Shape must be rank 1 but is rank 2 for 'strided_slice_1' (op: 'StridedSlice') with input shapes: [60000,128,128,1], [1,32], [1,32], [1].




I think the number '32' means batch_size(=32).



I tried to find about this error, but I couldn't find like this error.



I don't have any ideas to solve this problems (because I started to use keras a week ago, before I used pytorch).










share|improve this question























  • what is (1,32), 32 isn't the batch size? Please print(idx).
    – Geeocode
    Nov 12 '18 at 13:51










  • idx is the batch_size-sized list with random integer values. it is used at picking up 32 random MNIST data. example: [35904 26046 21148 28562 56414 31930 44948 53721 2118 30226 6292 57618 39161 46354 9053 46421 44620 36931 8266 48759 42023 40365 33027 13278 57609 3804 57904 4377 3517 30322 16445 47825]
    – Xsolute Lim
    Nov 12 '18 at 14:23










  • I think your idx if you printed it would be of shape (1,32) from the error message, that's why I asked you to print it.
    – Geeocode
    Nov 12 '18 at 15:24










  • The result of print(idx) is the example above, so I printed idx.shape: (32,).
    – Xsolute Lim
    Nov 12 '18 at 22:40










  • you wrote: imgs = x_train[idx] # This line made errors, not print(idx)
    – Geeocode
    Nov 12 '18 at 22:58













1












1








1


1





I am going to make some GAN-Model Tester using tf.keras with MNIST hand-writed digits dataset. Because my model is going to be used in 128x128 images, I resized MNIST dataset to 128x128x1. but, the program makes some errors, that I never seen.



(x_train, _), (_, _) = mnist.load_data()
x_train = (x_train.astype(np.float32) - 127.5) / 127.5
x_train = tf.image.resize_images(x_train, [128, 128])

idx = np.random.randint(0, x_train.shape[0], batch_size) # picks some data, count is batch_size=32.
imgs = x_train[idx] # This line made errors


The last line made two errors:




tensorflow.python.framework.errors_impl.InvalidArgumentError: Shape must be rank 1 but is rank 2 for 'strided_slice_1' (op: 'StridedSlice') with input shapes: [60000,128,128,1], [1,32], [1,32], [1].




and,




ValueError: Shape must be rank 1 but is rank 2 for 'strided_slice_1' (op: 'StridedSlice') with input shapes: [60000,128,128,1], [1,32], [1,32], [1].




I think the number '32' means batch_size(=32).



I tried to find about this error, but I couldn't find like this error.



I don't have any ideas to solve this problems (because I started to use keras a week ago, before I used pytorch).










share|improve this question















I am going to make some GAN-Model Tester using tf.keras with MNIST hand-writed digits dataset. Because my model is going to be used in 128x128 images, I resized MNIST dataset to 128x128x1. but, the program makes some errors, that I never seen.



(x_train, _), (_, _) = mnist.load_data()
x_train = (x_train.astype(np.float32) - 127.5) / 127.5
x_train = tf.image.resize_images(x_train, [128, 128])

idx = np.random.randint(0, x_train.shape[0], batch_size) # picks some data, count is batch_size=32.
imgs = x_train[idx] # This line made errors


The last line made two errors:




tensorflow.python.framework.errors_impl.InvalidArgumentError: Shape must be rank 1 but is rank 2 for 'strided_slice_1' (op: 'StridedSlice') with input shapes: [60000,128,128,1], [1,32], [1,32], [1].




and,




ValueError: Shape must be rank 1 but is rank 2 for 'strided_slice_1' (op: 'StridedSlice') with input shapes: [60000,128,128,1], [1,32], [1,32], [1].




I think the number '32' means batch_size(=32).



I tried to find about this error, but I couldn't find like this error.



I don't have any ideas to solve this problems (because I started to use keras a week ago, before I used pytorch).







python tensorflow keras neural-network generative-adversarial-network






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 1:00









Geeocode

2,2461820




2,2461820










asked Nov 12 '18 at 13:28









Xsolute LimXsolute Lim

86




86











  • what is (1,32), 32 isn't the batch size? Please print(idx).
    – Geeocode
    Nov 12 '18 at 13:51










  • idx is the batch_size-sized list with random integer values. it is used at picking up 32 random MNIST data. example: [35904 26046 21148 28562 56414 31930 44948 53721 2118 30226 6292 57618 39161 46354 9053 46421 44620 36931 8266 48759 42023 40365 33027 13278 57609 3804 57904 4377 3517 30322 16445 47825]
    – Xsolute Lim
    Nov 12 '18 at 14:23










  • I think your idx if you printed it would be of shape (1,32) from the error message, that's why I asked you to print it.
    – Geeocode
    Nov 12 '18 at 15:24










  • The result of print(idx) is the example above, so I printed idx.shape: (32,).
    – Xsolute Lim
    Nov 12 '18 at 22:40










  • you wrote: imgs = x_train[idx] # This line made errors, not print(idx)
    – Geeocode
    Nov 12 '18 at 22:58
















  • what is (1,32), 32 isn't the batch size? Please print(idx).
    – Geeocode
    Nov 12 '18 at 13:51










  • idx is the batch_size-sized list with random integer values. it is used at picking up 32 random MNIST data. example: [35904 26046 21148 28562 56414 31930 44948 53721 2118 30226 6292 57618 39161 46354 9053 46421 44620 36931 8266 48759 42023 40365 33027 13278 57609 3804 57904 4377 3517 30322 16445 47825]
    – Xsolute Lim
    Nov 12 '18 at 14:23










  • I think your idx if you printed it would be of shape (1,32) from the error message, that's why I asked you to print it.
    – Geeocode
    Nov 12 '18 at 15:24










  • The result of print(idx) is the example above, so I printed idx.shape: (32,).
    – Xsolute Lim
    Nov 12 '18 at 22:40










  • you wrote: imgs = x_train[idx] # This line made errors, not print(idx)
    – Geeocode
    Nov 12 '18 at 22:58















what is (1,32), 32 isn't the batch size? Please print(idx).
– Geeocode
Nov 12 '18 at 13:51




what is (1,32), 32 isn't the batch size? Please print(idx).
– Geeocode
Nov 12 '18 at 13:51












idx is the batch_size-sized list with random integer values. it is used at picking up 32 random MNIST data. example: [35904 26046 21148 28562 56414 31930 44948 53721 2118 30226 6292 57618 39161 46354 9053 46421 44620 36931 8266 48759 42023 40365 33027 13278 57609 3804 57904 4377 3517 30322 16445 47825]
– Xsolute Lim
Nov 12 '18 at 14:23




idx is the batch_size-sized list with random integer values. it is used at picking up 32 random MNIST data. example: [35904 26046 21148 28562 56414 31930 44948 53721 2118 30226 6292 57618 39161 46354 9053 46421 44620 36931 8266 48759 42023 40365 33027 13278 57609 3804 57904 4377 3517 30322 16445 47825]
– Xsolute Lim
Nov 12 '18 at 14:23












I think your idx if you printed it would be of shape (1,32) from the error message, that's why I asked you to print it.
– Geeocode
Nov 12 '18 at 15:24




I think your idx if you printed it would be of shape (1,32) from the error message, that's why I asked you to print it.
– Geeocode
Nov 12 '18 at 15:24












The result of print(idx) is the example above, so I printed idx.shape: (32,).
– Xsolute Lim
Nov 12 '18 at 22:40




The result of print(idx) is the example above, so I printed idx.shape: (32,).
– Xsolute Lim
Nov 12 '18 at 22:40












you wrote: imgs = x_train[idx] # This line made errors, not print(idx)
– Geeocode
Nov 12 '18 at 22:58




you wrote: imgs = x_train[idx] # This line made errors, not print(idx)
– Geeocode
Nov 12 '18 at 22:58












1 Answer
1






active

oldest

votes


















0














You have more issue with your code above, but the primer cause of error is that tensorflow doesn't support the numpy type advanced slicing.
Actually the error message is because tensorflow try to align your input array in his strided-slices:



An example for the strided-slices:




foo[5:,:,:3] on a 7x8x9 tensor is equivalent to foo[5:7,0:8,0:3].
foo[::-1] reverses a tensor with shape 8.




Unfortunately only basic type indexing is available in Tensorflow currently. Advanced type indexing is under development.



The secondary problem, that your resizing was not proper.
Tensorflow assumes a 3D or 4D input. You tried to pass a 2D image to `tf.image.resize_images(), which doesn't return the required new image dimensions. So we have to reshape the raw images like this:



x_train = x_train.reshape((-1, x_train.shape[1], x_train.shape[1], 1))


only then we can pass them to:



`x_train = tf.image.resize_images(x_train, [128, 128])


It will return then the proper dimensions:



print(x_train.shape)


Out:



(60000, 128, 128, 1)


So summarizing the whole solution, currently you can do it as follows:



import numpy as np
import tensorflow as tf

batch_size = 32

mnist = tf.keras.datasets.mnist

(x_train, _), (_, _) = mnist.load_data()

x_train = x_train.reshape((-1, x_train.shape[1], x_train.shape[1], 1))
x_train = (x_train.astype(np.float32) - 127.5) / 127.5
x_train = tf.image.resize_images(x_train, [128, 128])

idx = np.random.randint(0, x_train.shape[0], batch_size)

imgs = [x_train[i,:,:,:] for i in idx]


which is a pretty messy "solution".



Other, actually a real solution with rearrange the original code we can achieve what we aimed to as a workaround to the tensorflow indexing issue:



import numpy as np
import tensorflow as tf

batch_size = 32

mnist = tf.keras.datasets.mnist

(x_train, _), (_, _) = mnist.load_data()

x_train = (x_train.astype(np.float32) - 127.5) / 127.5
idx = np.random.randint(0, x_train.shape[0], batch_size)
x_train = x_train[idx]

x_train = x_train.reshape((-1, x_train.shape[1], x_train.shape[1], 1))
x_train = tf.image.resize_images(x_train, [128, 128])

print(x_train.shape)


Out:



(32, 128, 128, 1)


That's it!



Alternatively instead of tf.image.resize_images() you can use an additional image tools like skimage.transform.resize() from scikit-image which returns numpy array type data.






share|improve this answer






















  • I didn't know about the development of tensorflow. I got the idea to fix these problems! Thanks a lot!
    – Xsolute Lim
    Nov 13 '18 at 0:35










  • @XsoluteLim See my workaround.
    – Geeocode
    Nov 13 '18 at 0:40










  • Your code worked successfully. Now I only have to fix my original codes' structure. thanks for helping me.
    – Xsolute Lim
    Nov 13 '18 at 0:59










  • @XsoluteLim You're welcome!
    – Geeocode
    Nov 13 '18 at 1:00










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263216%2ftensorflow-keras-shape-errors-with-strided-slice-used-resized-mnist-datas%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You have more issue with your code above, but the primer cause of error is that tensorflow doesn't support the numpy type advanced slicing.
Actually the error message is because tensorflow try to align your input array in his strided-slices:



An example for the strided-slices:




foo[5:,:,:3] on a 7x8x9 tensor is equivalent to foo[5:7,0:8,0:3].
foo[::-1] reverses a tensor with shape 8.




Unfortunately only basic type indexing is available in Tensorflow currently. Advanced type indexing is under development.



The secondary problem, that your resizing was not proper.
Tensorflow assumes a 3D or 4D input. You tried to pass a 2D image to `tf.image.resize_images(), which doesn't return the required new image dimensions. So we have to reshape the raw images like this:



x_train = x_train.reshape((-1, x_train.shape[1], x_train.shape[1], 1))


only then we can pass them to:



`x_train = tf.image.resize_images(x_train, [128, 128])


It will return then the proper dimensions:



print(x_train.shape)


Out:



(60000, 128, 128, 1)


So summarizing the whole solution, currently you can do it as follows:



import numpy as np
import tensorflow as tf

batch_size = 32

mnist = tf.keras.datasets.mnist

(x_train, _), (_, _) = mnist.load_data()

x_train = x_train.reshape((-1, x_train.shape[1], x_train.shape[1], 1))
x_train = (x_train.astype(np.float32) - 127.5) / 127.5
x_train = tf.image.resize_images(x_train, [128, 128])

idx = np.random.randint(0, x_train.shape[0], batch_size)

imgs = [x_train[i,:,:,:] for i in idx]


which is a pretty messy "solution".



Other, actually a real solution with rearrange the original code we can achieve what we aimed to as a workaround to the tensorflow indexing issue:



import numpy as np
import tensorflow as tf

batch_size = 32

mnist = tf.keras.datasets.mnist

(x_train, _), (_, _) = mnist.load_data()

x_train = (x_train.astype(np.float32) - 127.5) / 127.5
idx = np.random.randint(0, x_train.shape[0], batch_size)
x_train = x_train[idx]

x_train = x_train.reshape((-1, x_train.shape[1], x_train.shape[1], 1))
x_train = tf.image.resize_images(x_train, [128, 128])

print(x_train.shape)


Out:



(32, 128, 128, 1)


That's it!



Alternatively instead of tf.image.resize_images() you can use an additional image tools like skimage.transform.resize() from scikit-image which returns numpy array type data.






share|improve this answer






















  • I didn't know about the development of tensorflow. I got the idea to fix these problems! Thanks a lot!
    – Xsolute Lim
    Nov 13 '18 at 0:35










  • @XsoluteLim See my workaround.
    – Geeocode
    Nov 13 '18 at 0:40










  • Your code worked successfully. Now I only have to fix my original codes' structure. thanks for helping me.
    – Xsolute Lim
    Nov 13 '18 at 0:59










  • @XsoluteLim You're welcome!
    – Geeocode
    Nov 13 '18 at 1:00















0














You have more issue with your code above, but the primer cause of error is that tensorflow doesn't support the numpy type advanced slicing.
Actually the error message is because tensorflow try to align your input array in his strided-slices:



An example for the strided-slices:




foo[5:,:,:3] on a 7x8x9 tensor is equivalent to foo[5:7,0:8,0:3].
foo[::-1] reverses a tensor with shape 8.




Unfortunately only basic type indexing is available in Tensorflow currently. Advanced type indexing is under development.



The secondary problem, that your resizing was not proper.
Tensorflow assumes a 3D or 4D input. You tried to pass a 2D image to `tf.image.resize_images(), which doesn't return the required new image dimensions. So we have to reshape the raw images like this:



x_train = x_train.reshape((-1, x_train.shape[1], x_train.shape[1], 1))


only then we can pass them to:



`x_train = tf.image.resize_images(x_train, [128, 128])


It will return then the proper dimensions:



print(x_train.shape)


Out:



(60000, 128, 128, 1)


So summarizing the whole solution, currently you can do it as follows:



import numpy as np
import tensorflow as tf

batch_size = 32

mnist = tf.keras.datasets.mnist

(x_train, _), (_, _) = mnist.load_data()

x_train = x_train.reshape((-1, x_train.shape[1], x_train.shape[1], 1))
x_train = (x_train.astype(np.float32) - 127.5) / 127.5
x_train = tf.image.resize_images(x_train, [128, 128])

idx = np.random.randint(0, x_train.shape[0], batch_size)

imgs = [x_train[i,:,:,:] for i in idx]


which is a pretty messy "solution".



Other, actually a real solution with rearrange the original code we can achieve what we aimed to as a workaround to the tensorflow indexing issue:



import numpy as np
import tensorflow as tf

batch_size = 32

mnist = tf.keras.datasets.mnist

(x_train, _), (_, _) = mnist.load_data()

x_train = (x_train.astype(np.float32) - 127.5) / 127.5
idx = np.random.randint(0, x_train.shape[0], batch_size)
x_train = x_train[idx]

x_train = x_train.reshape((-1, x_train.shape[1], x_train.shape[1], 1))
x_train = tf.image.resize_images(x_train, [128, 128])

print(x_train.shape)


Out:



(32, 128, 128, 1)


That's it!



Alternatively instead of tf.image.resize_images() you can use an additional image tools like skimage.transform.resize() from scikit-image which returns numpy array type data.






share|improve this answer






















  • I didn't know about the development of tensorflow. I got the idea to fix these problems! Thanks a lot!
    – Xsolute Lim
    Nov 13 '18 at 0:35










  • @XsoluteLim See my workaround.
    – Geeocode
    Nov 13 '18 at 0:40










  • Your code worked successfully. Now I only have to fix my original codes' structure. thanks for helping me.
    – Xsolute Lim
    Nov 13 '18 at 0:59










  • @XsoluteLim You're welcome!
    – Geeocode
    Nov 13 '18 at 1:00













0












0








0






You have more issue with your code above, but the primer cause of error is that tensorflow doesn't support the numpy type advanced slicing.
Actually the error message is because tensorflow try to align your input array in his strided-slices:



An example for the strided-slices:




foo[5:,:,:3] on a 7x8x9 tensor is equivalent to foo[5:7,0:8,0:3].
foo[::-1] reverses a tensor with shape 8.




Unfortunately only basic type indexing is available in Tensorflow currently. Advanced type indexing is under development.



The secondary problem, that your resizing was not proper.
Tensorflow assumes a 3D or 4D input. You tried to pass a 2D image to `tf.image.resize_images(), which doesn't return the required new image dimensions. So we have to reshape the raw images like this:



x_train = x_train.reshape((-1, x_train.shape[1], x_train.shape[1], 1))


only then we can pass them to:



`x_train = tf.image.resize_images(x_train, [128, 128])


It will return then the proper dimensions:



print(x_train.shape)


Out:



(60000, 128, 128, 1)


So summarizing the whole solution, currently you can do it as follows:



import numpy as np
import tensorflow as tf

batch_size = 32

mnist = tf.keras.datasets.mnist

(x_train, _), (_, _) = mnist.load_data()

x_train = x_train.reshape((-1, x_train.shape[1], x_train.shape[1], 1))
x_train = (x_train.astype(np.float32) - 127.5) / 127.5
x_train = tf.image.resize_images(x_train, [128, 128])

idx = np.random.randint(0, x_train.shape[0], batch_size)

imgs = [x_train[i,:,:,:] for i in idx]


which is a pretty messy "solution".



Other, actually a real solution with rearrange the original code we can achieve what we aimed to as a workaround to the tensorflow indexing issue:



import numpy as np
import tensorflow as tf

batch_size = 32

mnist = tf.keras.datasets.mnist

(x_train, _), (_, _) = mnist.load_data()

x_train = (x_train.astype(np.float32) - 127.5) / 127.5
idx = np.random.randint(0, x_train.shape[0], batch_size)
x_train = x_train[idx]

x_train = x_train.reshape((-1, x_train.shape[1], x_train.shape[1], 1))
x_train = tf.image.resize_images(x_train, [128, 128])

print(x_train.shape)


Out:



(32, 128, 128, 1)


That's it!



Alternatively instead of tf.image.resize_images() you can use an additional image tools like skimage.transform.resize() from scikit-image which returns numpy array type data.






share|improve this answer














You have more issue with your code above, but the primer cause of error is that tensorflow doesn't support the numpy type advanced slicing.
Actually the error message is because tensorflow try to align your input array in his strided-slices:



An example for the strided-slices:




foo[5:,:,:3] on a 7x8x9 tensor is equivalent to foo[5:7,0:8,0:3].
foo[::-1] reverses a tensor with shape 8.




Unfortunately only basic type indexing is available in Tensorflow currently. Advanced type indexing is under development.



The secondary problem, that your resizing was not proper.
Tensorflow assumes a 3D or 4D input. You tried to pass a 2D image to `tf.image.resize_images(), which doesn't return the required new image dimensions. So we have to reshape the raw images like this:



x_train = x_train.reshape((-1, x_train.shape[1], x_train.shape[1], 1))


only then we can pass them to:



`x_train = tf.image.resize_images(x_train, [128, 128])


It will return then the proper dimensions:



print(x_train.shape)


Out:



(60000, 128, 128, 1)


So summarizing the whole solution, currently you can do it as follows:



import numpy as np
import tensorflow as tf

batch_size = 32

mnist = tf.keras.datasets.mnist

(x_train, _), (_, _) = mnist.load_data()

x_train = x_train.reshape((-1, x_train.shape[1], x_train.shape[1], 1))
x_train = (x_train.astype(np.float32) - 127.5) / 127.5
x_train = tf.image.resize_images(x_train, [128, 128])

idx = np.random.randint(0, x_train.shape[0], batch_size)

imgs = [x_train[i,:,:,:] for i in idx]


which is a pretty messy "solution".



Other, actually a real solution with rearrange the original code we can achieve what we aimed to as a workaround to the tensorflow indexing issue:



import numpy as np
import tensorflow as tf

batch_size = 32

mnist = tf.keras.datasets.mnist

(x_train, _), (_, _) = mnist.load_data()

x_train = (x_train.astype(np.float32) - 127.5) / 127.5
idx = np.random.randint(0, x_train.shape[0], batch_size)
x_train = x_train[idx]

x_train = x_train.reshape((-1, x_train.shape[1], x_train.shape[1], 1))
x_train = tf.image.resize_images(x_train, [128, 128])

print(x_train.shape)


Out:



(32, 128, 128, 1)


That's it!



Alternatively instead of tf.image.resize_images() you can use an additional image tools like skimage.transform.resize() from scikit-image which returns numpy array type data.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 0:54

























answered Nov 13 '18 at 0:11









GeeocodeGeeocode

2,2461820




2,2461820











  • I didn't know about the development of tensorflow. I got the idea to fix these problems! Thanks a lot!
    – Xsolute Lim
    Nov 13 '18 at 0:35










  • @XsoluteLim See my workaround.
    – Geeocode
    Nov 13 '18 at 0:40










  • Your code worked successfully. Now I only have to fix my original codes' structure. thanks for helping me.
    – Xsolute Lim
    Nov 13 '18 at 0:59










  • @XsoluteLim You're welcome!
    – Geeocode
    Nov 13 '18 at 1:00
















  • I didn't know about the development of tensorflow. I got the idea to fix these problems! Thanks a lot!
    – Xsolute Lim
    Nov 13 '18 at 0:35










  • @XsoluteLim See my workaround.
    – Geeocode
    Nov 13 '18 at 0:40










  • Your code worked successfully. Now I only have to fix my original codes' structure. thanks for helping me.
    – Xsolute Lim
    Nov 13 '18 at 0:59










  • @XsoluteLim You're welcome!
    – Geeocode
    Nov 13 '18 at 1:00















I didn't know about the development of tensorflow. I got the idea to fix these problems! Thanks a lot!
– Xsolute Lim
Nov 13 '18 at 0:35




I didn't know about the development of tensorflow. I got the idea to fix these problems! Thanks a lot!
– Xsolute Lim
Nov 13 '18 at 0:35












@XsoluteLim See my workaround.
– Geeocode
Nov 13 '18 at 0:40




@XsoluteLim See my workaround.
– Geeocode
Nov 13 '18 at 0:40












Your code worked successfully. Now I only have to fix my original codes' structure. thanks for helping me.
– Xsolute Lim
Nov 13 '18 at 0:59




Your code worked successfully. Now I only have to fix my original codes' structure. thanks for helping me.
– Xsolute Lim
Nov 13 '18 at 0:59












@XsoluteLim You're welcome!
– Geeocode
Nov 13 '18 at 1:00




@XsoluteLim You're welcome!
– Geeocode
Nov 13 '18 at 1:00

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263216%2ftensorflow-keras-shape-errors-with-strided-slice-used-resized-mnist-datas%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto