I try to train model with images.
My model is based on :
_myModel = _tf.sequential();
_myModel.add(_tf.layers.conv2d({ inputShape: [96, 96, 1],....
Each image for train is loaded with...
let buffer = _fs.readFileSync(filePath);
let imageTensor = _tfnode.node.decodeImage( buffer, <channels>)
.resizeNearestNeighbor([96,96])
and the whole set of tensors are used to train the model:
_myModel.fit( trainData.images, trainData.labels, ...
Here are the differents errors with each value of "channels" :
No "channels" value (default is 0):
let imageTensor = _tfnode.node.decodeImage( buffer)...
_myModel.fit( trainData.images, trainData.labels, {...
err: Error: input expected a batch of elements where each example has shape [96,96,1] (i.e.,tensor shape [*,96,96,1]) but the input received an input with 7 examples, each with shape [96,96,4] (tensor shape [7,96,96,4])
With "channels" = 1 (B&W) :
let imageTensor = _tfnode.node.decodeImage( buffer, 1)...
_myModel.fit( trainData.images, trainData.labels, {...
err: Error: target expected a batch of elements where each example has shape [5] (i.e.,tensor shape [*,5]) but the target received an input with 7 examples, each with shape [7] (tensor shape [7,7])
With "channels" = 3 (RGB) :
let imageTensor = _tfnode.node.decodeImage( buffer, 3)...
_myModel.fit( trainData.images, trainData.labels, {....
err: Error: input expected a batch of elements where each example has shape [96,96,1] (i.e.,tensor shape [*,96,96,1]) but the input received an input with 7 examples, each with shape [96,96,3] (tensor shape [7,96,96,3])
So, how to match image tensor with model ?
Best regards
your model definition specifies what looks like grayscale single-channel input:
_myModel.add(_tf.layers.conv2d({ inputShape: [96, 96, 1],....
so you need to convert your imageTensor to that.
right now you've tried with RGBA (4-channels) and RGB (3-channels) - and neither are what model definition states.
something like
const rgb = [0.2989, 0.5870, 0.1140]; // factors for red/green/blue colors when converting to grayscale: https://www.mathworks.com/help/matlab/ref/rgb2gray.html
const [red, green, blue] = tf.split(imageTensor, 3, 3);
const redNorm = tf.mul(red, rgb[0]);
const greenNorm = tf.mul(green, rgb[1]);
const blueNorm = tf.mul(blue, rgb[2]);
const grayscale = tf.addN([redNorm, greenNorm, blueNorm]);
Related
I'm facing with this error properly and I could not see any exact solution or a solution formula for this error. My inputs are like (48x48) and that's not matching with the input shape of the resnet101. How can I edit my input to fit to the resnet101? You can see my code below, it probably helps you to understand my problem.
if __name__ == "__main__":
vid = cv2.VideoCapture(0)
emotions = []
while vid.isOpened():
image = cv2.imread("/home/berkay/Desktop/angry_man.jpg")
_, frame = vid.read()
# takes in a gray coloured filter of the frame
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# initializing the haarcascade face detector
faces = face_cascade.detectMultiScale(frame)
for (x,y,w,h) in faces:
# takes the region of interest of the face only in gray
roi_gray = gray[y:y+h, x:x+h]
resized = cv2.resize(roi_gray, (48, 48)) # resizes to 48x48 sized image
# predict the mood
img = img2tensor(resized)
prediction = predict(img)
In that point, I'm getting this error:
weight of size [64, 3, 7, 7], expected input[1, 1, 229, 229] to have 3 channels, but got 1 channels instead
How can I fix this? Thanks in advance
You can modify the input layer of resnet so that it would accept a single-channel tensors inputs using
In [1]: model = resnet101()
In [2]: model.conv1 = nn.Conv2d(1, 64, kernel_size=(2, 2))
In [3]: model(torch.rand(10, 1, 48, 48))
Out[3]:
tensor([[-0.5015, 0.6124, 0.1370, ..., 1.2181, -0.4707, 0.3285],
[-0.4776, 1.1027, 0.0161, ..., 0.6363, -0.4733, 0.6218],
[-0.3935, 0.8276, -0.0316, ..., 0.6853, -0.4735, 0.6424],
...,
[-0.2986, 1.1758, 0.0158, ..., 0.7422, -0.4422, 0.4792],
[-0.2668, 0.7884, -0.1205, ..., 1.1445, -0.6249, 0.6697],
[-0.2139, 1.0412, 0.2326, ..., 0.8332, -0.8744, 0.4827]],
grad_fn=<AddmmBackward0>)
(you will probably need to modify the kernel size accordingly too)
I am using StandardScaler() to standardize the inputs.
How can I convert prediction back to original data? I am using the following code, but it throws me an error.
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
#custom inputs for prediction after training
sample = pd.DataFrame({'salary': [1211], 'age': [30]})
sample = sc.transform(sample)
sample_predict = sc.inverse_transform(sample_predict)
print (sample_predict)
shape of X_test: (3000, 2)
shape of sample_predict: (1, 2)
Error:
X *= self.scale_
ValueError: non-broadcastable output operand with shape (1,1) doesn't match the broadcast shape (1,2)
def AHE(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
eq = clahe.apply(gray)
return eq
IMG_SIZE = (120,120)
batch_size = 8
epoch = 10
train_image_generator = ImageDataGenerator(rescale=1./119,rotation_range=30, horizontal_flip=0.5, preprocessing_function=AHE)
validation_image_generator = ImageDataGenerator(rescale=1./119)
test_image_generator = ImageDataGenerator(rescale=1./119,preprocessing_function=AHE)
train_data_gen = train_image_generator.flow_from_directory(batch_size=batch_size,
directory=train_dir,
shuffle=True,
target_size=IMG_SIZE,
)
val_data_gen = validation_image_generator.flow_from_directory(batch_size=batch_size,
directory=validate_dir,
shuffle=True,
target_size=IMG_SIZE,
)
test_data_gen = test_image_generator.flow_from_directory(batch_size=batch_size,
directory=test_dir,
shuffle=True,
target_size=IMG_SIZE,
)
sample_test_images, labels = next(test_data_gen)
print(labels[0:10])
sample_test_images.shape
labels.shape
Even Though I converted the image to gray scale I'm getting this ERROR:
OpenCV(4.1.2) /io/opencv/modules/imgproc/src/clahe.cpp:351: error: (-215:Assertion failed) _src.type() == CV_8UC1 || _src.type() == CV_16UC1 in function 'apply'
I ran into just the same problem, exactly. What is asserted is significant here:
(-215:Assertion failed) _src.type() == CV_8UC1 || _src.type() == CV_16UC1
This says that the apply function is expecting to receive specific types, either "CV_8UCI" or "CV_16UCI." These correspond to np.unit8 or np.uint16, respectively, so changing the type of the input array resolves the error.
Another issue that then came up was that the shape of the image array was no longer of the same shape as the input to the preprocessing_function. According to the Keras documentation, "The function should take one argument: one image (Numpy tensor with rank 3), and should output a Numpy tensor with the same shape." To resolve this issue, I reconverted the image back to RGB color (3 channels). (BGR is native to OpenCV, but I changed to convert to and from RGB.) I also converted the array back to np.float32 type.
So your function may be along these lines:
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
def AHE(img):
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
gray = gray.astype(np.uint16)
eq = clahe.apply(gray)
eq = cv2.cvtColor(eq, cv2.COLOR_GRAY2RBG)
eq = eq.astype(np.float32)
return eq
BTW, with this added preprocessing, the time for each epoch has about tripled (GPU on a Colab notebook). Working with classifying chest x-rays at the moment, I hope the enhancement is worth the overhead. ;)
i'm trying to preprocess set of images before passing it to the CNN model, and i had to convert it to greyscale because my model my model required to, the issue is when i save the converted image and load it , it works correctly without any errors because the shape is (244,244,3) but if i pass it directly to the model the shape of images be (None , 224,224) and it produced this error:
ValueError: Input 0 of layer sequential_5 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: (None, 224, 224)
the code is:
gray = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)
resized_image = cv2.resize(crop_img, (224, 224))
frames.append(resized_image)
cv2.imwrite("/Users/naimahalaqeel/Documents/graduation project/toArabicText/test2/%d.jpg" % count2, resized_image)
count2 = count2 +1
success,image = vidcap.read()
count+=1
afterPrecceseFrames = np.array(frames)
predicted_classes = model.predict_classes(afterPrecceseFrames)
I think you are using cv2.resize() to crop_img not gray. Is that the problem.
I've been trying to use tensorflow's tf.estimator, but I'm getting the following errors regarding the shape of input/output data.
ValueError: Dimension size must be evenly divisible by 9 but is 12 for
'linear/linear_model/x/Reshape' (op: 'Reshape') with input shapes:
[4,3], [2] and with input tensors computed as partial shapes: input[1]
= [?,9].
Here is the code:
data_size = 3
iterations = 10
learn_rate = 0.005
# generate test data
input = np.random.rand(data_size,3)
output = np.dot(input, [2, 3 ,7]) + 4
output = np.transpose([output])
feature_columns = [tf.feature_column.numeric_column("x", shape=(data_size, 3))]
estimator = tf.estimator.LinearRegressor(feature_columns=feature_columns)
input_fn = tf.estimator.inputs.numpy_input_fn({"x":input}, output, batch_size=4, num_epochs=None, shuffle=True)
estimator.train(input_fn=input_fn, steps=iterations)
input data shape is shape=(3, 3):
[[ 0.06525168 0.3171153 0.61675511]
[ 0.35166298 0.71816544 0.62770994]
[ 0.77846666 0.20930611 0.1710842 ]]
output data shape is shape=(3, 1)
[[ 9.399135 ]
[ 11.25179188]
[ 7.38244104]]
I have sense it is related to input data, output data and batch_size, because when input data changed to 1 row it works. When input data rows count equal to batch_size(data_size = 10 and batch_size=10) then it throws other error:
ValueError: Shapes (1, 1) and (10, 1) are incompatible
Any help with the errors would be much appreciated.