ValueError: Error when checking input: expected keras_layer_input to have 4 dimensions, but got array with shape (10, 1) - numpy

Before this gets marked as duplicate, I already tried all of the the similar questions and most of them were not resolved, if they have an answer, it did not work with my problem. The original code has more than 10 samples.
Input: list of model input np.arrays. sample_train_emb1 has length = 2
Problem: model.fit() error ValueError: Error when checking input: expected keras_layer_input to have 4 dimensions, but got array with shape (10, 1)
Here is my plot_model image:
The model.fit() looks like this:
model.fit(
sample_train_emb1,
sample_y_train,
validation_data=(sample_valid_emb1, sample_y_valid),
epochs=epoch,
batch_size=batch_size,
verbose=1,
)
Thank you! Let me know if you need more details to help me solve this problem. It has many similar posts that remained unresolved so I thought it will help anybody who might face the same problem in the future.
What I've tried so far:
Swapping the two features.
Converting the image feature into a `TensorShape([Dimension(1),
Dimension(224), Dimension(224), Dimension(3)]) based on a similar question's answer

I eventually figured it out. Using the answer from this post.
sample_train_emb1[1] = np.array([x for x in sample_train_emb1[1]])
Hope this helps in the future to anyone.

Related

How can I "try out" Tensorflow functions in iPython and see the answers?

Sometimes I really just want to interactively experiment with things like softmax(), or sigmoid() just to get a sense of how they behave. I'm struggling to be able to see the answer. Maybe I need to rewrite everything in numpy, but I hope not.
Example:
v = tf.sigmoid(tf.convert_to_tensor([0.123, 0.345]))
Now I have v, but heck if I can figure out how to see the values inside it. How can it be done?
In case you are running Tensorflow 2.0 -
v = tf.sigmoid(tf.convert_to_tensor([0.123, 0.345]))
v.numpy()
The answer is -
array([0.5307113, 0.5854046], dtype=float32)
If you are running Tensorflow 1.0 -
with tf.Session() as sess:
print(v.eval())
It gives the following answer -
[0.5307113 0.5854046]

Most recent output in Colab notebook

I forgot to get the history histrory = model.fit( ...) trained for 2 hours and my cell look like this:
model.fit( ....)
Is there a variable (as in matlab's "ans"), for the most recent output?
I tried output, out, out[-1], etc. None of the work.
Any help or workarounds would be appreciated.
CS
The global dictionary Out holds all cell outputs. So, for example, if you executed the code in cell 20, Out[20] has the result.
You can also get the output of the most recent execution with the _ variable.

keras: zca whitening gets stuck with train_datagen.fit()

I am trying to use zca_whitening with the keras image processing option, but the calculation gets stuck and never ends. My part of the code causing the problem looks like this:
train_datagen = ImageDataGenerator(rotation_range=30, zca_whitening=True)
def read_pil_image(img_path, height, width):
with open(img_path, 'rb') as f:
return np.array(Image.open(f).convert('RGB').resize((width, height)))
def load_all_images(dataset_path, height, width, img_ext='jpg'):
return np.array([read_pil_image(str(p), height, width) for p in
Path(dataset_path).rglob("*."+img_ext)])
zca_dir = 'some/path/to_jpg_images/'
train_datagen.fit(load_all_images(zca_dir, height, width))
When I execute the above it gets stuck at train_datagen.fit(). There is no error message displayed. I found out if I just outcomment the zca_whitening and substitute it with samplewise_center=True, samplewise_std_normalization=True the code works just fine.
I thought first that maybe the directory zca_dir contains too many pictures, so I reduced them to 30. Still, same problem.
The problem appears on 2 different machines with 2 different keras versions. In this post there is the suggestion to reverse the dimension of my images from [80,80,3] to [3,80,80], but this doesn't change anything.
Do you know how I can solve this? Thanks

tf.train.shuffle_batch() ValueError: Cannot infer Tensor's rank: Tensor("PyFunc:0", dtype=uint8)

I am trying to feed my image data from my TFRecord files into tf.train.shuffle_batch(). I have a load_img_file() function that reads the TFRecord files, does preprocessing, and returns the images and one-hot labels in the format [[array of images, np.uint8 format], [array of labels, np.uint8 format]]. I made the op
load_img_file_op = tf.py_func(self.load_img_file, [], [np.uint8, np.uint8])
which converts that function into an op. I have verified that that op works by doing
data = tf.Session().run(load_img_file_op)
for n in range(50): #go through images
print data[1][n] #print one-hot label
self.image_set.display_img(data[0][n]) #display image
which successfully prints the one-hot labels and displays the corresponding images.
However, when I try to do something like
self.batch = tf.train.shuffle_batch(load_img_file_op, batch_size=self.batch_size, capacity=q_capacity, min_after_dequeue=10000)
I get the error
raise ValueError("Cannot infer Tensor's rank: %s" % tl[i])
ValueError: Cannot infer Tensor's rank: Tensor("PyFunc:0", dtype=uint8)"
I have tried many variations to try to match what the guide does:
Instead of self.batch =, I have tried example_batch, label_batch = (trying to get two values instead of one)
setting enqueue_many to True
having my load_image_file() function and load_img_file_op return two separate values: images and labels. And then inputting them like tf.train.shuffle_batch([images, labels],...)
returning/inputting just one image and label at a time into tf.train.shuffle_batch()
using tf.train.shuffle_batch_join()
Nothing seems to work, but I feel like I am following the format of the guide and various other tutorials I have seen. What am I doing wrong? I apologize if my mistake is stupid or trivial (searches for this error do not seem to return anything relevant to me). Thank you for your help and time!
The link in the comments helped a lot; thank you! (The answer is that you have to give the shape when using py_func.) Since I had to figure out a little bit more on top of that I will post the complete solution:
I had to make my function return two separate values so that they would be two different tensors and could be shaped separately:
return images, labels
Then, proceeding as in the question above, but shaping:
load_img_file_op = tf.py_func(self.load_img_file, [], [np.uint8, np.uint8]) # turn the function into an op
images, labels = load_img_file_op
images.set_shape([imgs_per_file, height * width])
labels.set_shape([imgs_per_file, num_classes])
self.batch = tf.train.shuffle_batch([images, labels], batch_size=self.batch_size, capacity=q_capacity, min_after_dequeue=1000, enqueue_many = True)
The enqueue_many is important so that the images will enter the queue individually.

TypeError: cannot convert key of dictionary on call to prediction?

I'm trying to do 10 test predictions similar to the code in the CNTK_103B_MNIST_FeedForwardNetwork tutorial, but I'm getting an error as shown below. What could be the problem with my code?
Code / Error Message
To answer my own question. I used the incorrect name of the CNTK input_variable in the model.
I changed this line of code:
predicted_label_prob = pred_basic_model.eval({input : x})
to
predicted_label_prob = pred_basic_model.eval({pred_basic_model.arguments[0] : x})
and now it works.