tensorflow, expected conv2d_input to have 4 dimensions

  • Last Update :
  • Techknowledgy :
Image_Size is: 50 x50

import tensorflow as tf
import numpy as np
import pickle
from tensorflow.keras.models
import Sequential
from tensorflow.keras.layers
import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D

pickle_ind = open("x.pickle", "rb")
x = pickle.load(pickle_ind)
x = np.array(x, dtype = float)
# x = x / 255.0

pickle_ind = open("y.pickle", "rb")
y = pickle.load(pickle_ind)

n_batch = len(x)

model = Sequential()
model.add(Conv2D(32, (3, 3), activation = 'relu', input_shape = (50, 50, 1)))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation = 'relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation = 'relu'))

model.summary()

model.compile(optimizer = 'adam',
   loss = 'binary_crossentropy',
   metrics = ['accuracy'])

model.fit(x, y, epochs = 20, batch_size = n_batch)

Suggestion : 2

ValueError: Error when checking input: anycodings_tensorflow expected conv2d_input to have 4 anycodings_tensorflow dimensions, but got array with shape (1, 1),Rather return a image as 3D np array anycodings_python then create a batch of images for anycodings_python prediction. ,JS: How to change image orientation from horizontal to vertical that were fetched from an API with radio buttons,What I expected get new feature into my anycodings_tensorflow library:

My details:

RTX 2080
Tensorflow 1.13 .1
Cuda 10.0

My full code:

import os
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"

import tensorflow as tf
import numpy as np
import pickle
import cv2
from tensorflow.keras.models
import Sequential
from tensorflow.keras.layers
import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D
from tensorflow
import keras

IMG_SIZE = 50

def prepare(file):
   img_array = cv2.imread(file, cv2.IMREAD_GRAYSCALE)
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))

predictdata = tf.reshape(new_array, (1, 50, 50))
predictdata = np.expand_dims(predictdata, -1)
return predictdata

pickle_ind = open("x.pickle", "rb")
x = pickle.load(pickle_ind)
x = np.array(x, dtype = float)
x = np.expand_dims(x, -1)

pickle_ind = open("y.pickle", "rb")
y = pickle.load(pickle_ind)

n_batch = len(x)

model = Sequential()
model.add(Conv2D(32, (3, 3), activation = 'relu', input_shape = (50, 50, 1)))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation = 'relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation = 'relu'))
model.add(Flatten())
model.add(Dense(1, activation = 'softmax'))

model.summary()

model.compile(optimizer = 'adam',
   loss = 'binary_crossentropy',
   metrics = ['accuracy'])

model.fit(x, y, epochs = 1, batch_size = n_batch)
prediction = model.predict([prepare('demo1.jpg')], batch_size = n_batch, steps = 1, verbose = 1)

print(prediction)

Do below changes:

def prepare(file):
   img_array = cv2.imread(file, cv2.IMREAD_GRAYSCALE)
return np.expand_dims(cv2.resize(img_array, (IMG_SIZE, IMG_SIZE)), -1)

model.fit(x, y, epochs = 1, batch_size = n_batch)
model.predict(np.array([prepare("demo1.jpg")]), batch_size = n_batch, steps = 1, verbose = 1)

Suggestion : 3

Last updated 2022-07-14 UTC.

Computes a 2-D convolution given input and 4-D filters tensors.

tf.nn.conv2d(
   input,
   filters,
   strides,
   padding,
   data_format = 'NHWC',
   dilations = None,
   name = None
)

In detail, with the default NHWC format,

output[b, i, j, k] =
   sum_ {
      di,
      dj,
      q
   }
input[b, strides[1] * i + di, strides[2] * j + dj, q] *
   filter[di, dj, q, k]

Usage Example:

x_in = np.array([[
[[2], [1], [2], [0], [1]],
[[1], [3], [2], [2], [3]],
[[1], [1], [3], [3], [0]],
[[2], [2], [0], [1], [1]],
[[0], [0], [3], [1], [2]], ]])
kernel_in = np.array([
[ [[2, 0.1]], [[3, 0.2]] ],
[ [[0, 0.3]],[[1, 0.4]] ], ])
x = tf.constant(x_in, dtype=tf.float32)
kernel = tf.constant(kernel_in, dtype=tf.float32)
tf.nn.conv2d(x, kernel, strides=[1, 1, 1, 1], padding='VALID')
<tf.Tensor: shape=(1, 4, 4, 2), dtype=float32, numpy=..., dtype=float32)>

Suggestion : 4

ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (595, 10083),Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (28708, 1),Tensorflow/keras error: ValueError: Error when checking input: expected lstm_input to have 3 dimensions, but got array with shape (4012, 42),Error when checking input: expected lstm_132_input to have 3 dimensions, but got array with shape (23, 1, 3, 1)

You can create some test data like this:

x_train = np.random.random((4, 32, 32, 3))

# then
try out the model prediction

model.predict(x_train)