I am working on CNN model for MNIST fashion dataset. I have created a successful CNN model. But I want to test the model for classification for another image that I downloaded from internet .
My all train and test set is of the shape (28, 28, 1). But now for the image I want to predict I resized it into (28,28) and it made it into one channel of RGB using
cv2.cvtColor(load_img_rz, cv2.COLOR_BGR2GRAY)
Now the shape of image is (28, 28). I tried to input it into the model and its shows error
ValueError: Input 0 of layer sequential_6 is incompatible with the layer: : expected min_ndim=4,
found ndim=3. Full shape received: (None, 28, 3)
I think shape is the issue. So how can I convert it into the shape(28,28,1) if that is the issue.
And does CNN work better in one channel RGB than 3 channel RGB?
A very useful command for me in Deep Learning is the expand_dims from numpy.
your_image.shape
>>> (28, 28)
your_new_array = np.expand_dims(your_image, axis=-1)
your_new_array.shape
>>> (28, 28, 1)
You can play around with the axis parameter to get a better feeling of what is going on here.
since you don't include your code, i'll assume that you have a problem with your input layer. So, you need to specified the number of unit and inpt dim into your input layer first:
model = Sequential()
model.add(Dense(X.shape[1], activation='something you desired', input_dim=X.shape[1]))
and so on
hard to understand what are you dealing with and what you want to achieve since you don't specified / share anything, not even the code.
Related
I'm following this Git-hub repository (Link: https://github.com/JJN123/Fall-Detection)
as per repository i run dstcae_c3d_main_train.py python file but got an error:
ValueError: Error when checking target: expected conv3d_7 to have shape (4, 64, 64, 1) but got array with shape (2, 64, 64, 1)
please helping me out, how to give an input to train model for 3D fall detection.
I want to be able to predict the Class of a Single Image from the Learner and i always get an Index Out of Bound Exception .
Here is the Code
data = ImageDataLoader.from_folder(path, train="Train", valid ="Valid",
ds_tfms=get_transforms(), size=(256,256), bs=32, num_workers=4)
//Model is a Sequential One
learn = Learner(data, model, loss_func = nn.CrossEntropyLoss(), metrics=accuracy)// The Model
learn.fit_one_cycle(100, lr_max=3e-3)
Img = //PIL Image Path
learn.predict(img)
The Model is able to Predict on ImageDataLoader but not on a Single Image .If anyone has any clue it would be much appreciated
Here is a Link to FastAi but didnt solve the issue
https://forums.fast.ai/t/how-to-use-learner-predict-list-index-out-of-range/81998/7
EDIT NOTE : I have tried to convert the Image to a tensor Flow but another error is given .Photo of the Error
I think that the problem is that data is a rank 4 tensor whereas Img is rank 3. In other words, it is missing the #points or batch dimension up front. In TF one can fix that with tf.expand_dims like so
img = tf.expand_dims(img, axis=0)
or fixing it when passing to the model
learn.predict(tf.expand_dims(img, axis=0))
You can also look at tf.newaxis (see the second code example here).
I'm trying to do post-training full 8-bit quantization of a Keras model to compile and deploy to EdgeTPU.
I have a trained Keras model saved as .h5 file, and am trying to go through the steps as specified here: https://coral.withgoogle.com/docs/edgetpu/models-intro/, for deployment to the Coral Dev Board.
I'm following these instructions for quantization: https://www.tensorflow.org/lite/performance/post_training_quantization#full_integer_quantization_of_weights_and_activations)
I’m trying to use the following code:
import tensorflow as tf
num_calibration_steps = 100
def representative_dataset_gen():
for _ in range(num_calibration_steps):
# Get sample input data as a numpy array in a method of your choosing.
yield [X_train_quant_conv]
converter = tf.compat.v1.lite.TFLiteConverter.from_keras_model_file('/tmp/classNN_simple.h5')
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
converter.representative_dataset = representative_dataset_gen
tflite_full_integer_quant_model = converter.convert()
where X_train_quant_conv is a subset of my training data converted to np.array and of type np.float32
When running this piece of code, I get the following error:
ValueError: Cannot set tensor: Dimension mismatch
I’ve tried changing the function representative_dataset_gen() in different ways, but every time I get a new error. I’m not sure how this function should be. I’m also in doubt of what value num_calibration_steps should have.
Any suggestions or working examples are very appreciated.
This question is very similar to this answered question: Convert Keras model to quantized Tensorflow Lite model that can be used on Edge TPU
You might want to look at my demo script for quantization, on github.
It's just a guess since I can't see what X_train_quant_conv really is, but in my working demo, I yield one image at a time (random data created on the fly, in my case) in representative_dataset_gen(). The image is stored as batch of size 1 (e.g., tensor shape is (1, 56, 56, 32) for my 52x52x32 image). There are 32 channels, though there would typically just be 3, for a color image. I think representative_dataset_gen() has to yield a list containing a tensor (or more than one?) for which the first dimension is of length 1.
image_shape = (56, 56, 32)
def representative_dataset_gen():
num_calibration_images = 10
for i in range(num_calibration_images):
image = tf.random.normal([1] + list(image_shape))
yield [image]
I am trying to load a grayscale image dataset(fashion-mnist) to MobileNet model to predict hand written numbers but according to this tutorial only RGB images can be loaded to the model. When I try to feed fashion-mnist samples, it gives me the following error
Error when checking input: expected keras_layer_13_input to have shape
(224, 224, 3) but got array with shape (224, 224, 1)
How to solve this problem ?
Probably pre-trained MobileNet is not suitable for this task. You have two different problems. Mobilenet is made for Imagenet images which are 224x224 images with 3 color channels, while MNIST dataset is 28x28 images with one color channel. You can repeat the color channel in RGB:
# data.shape [70000, 224, 224, 1] -> [70000, 224, 224, 3]
data = np.repeat(data, 3, -1)
But before that, you need to resize images. For example, you can use PIL for resizing images:
from PIL import Image
data = np.array([Image.fromarray(x).resize([224,224]) for x in data])
There are some small details here which you should figure out yourself. Such as dtype of the images if you have loaded from the dataset as numpy. You may need to convert numpy types to integers with np.uint8().
Mobilenet v2 needs RGB. You might also be able to use the convert function from PIL.
Try this:
from PIL import Image
x= Image.open(input_image).resize((96,96)).convert("RGB")
documentation is here: https://pillow.readthedocs.io/en/stable/reference/Image.html
try this, x = np.stack((x,)*3, axis=-1). Please refer to this link for more details: https://github.com/malnakli/ML/blob/master/tf_serving_keras_mobilenetv2/main.ipynb
I want to implement this paper in tensorflow. It's approach is to create two separate CNNs at first, and then concatenate them for finetuning (as you can see in figure 1a)).
My current situation is: I have two pre-trained and saved models, each of them fed with a data queue input (so, no feed_dict and no Dataset API), and now i am off for finetuning. I want to restore them from disk and somehow concatenate them, so that i can define an optimizer which optimizes both networks.
This is my current approach:
# Build data input
aug_img, is_img, it_img = finetuning_read_training_images(trainset_size=num_steps,
batch_size=batch_size,
cropped=cropped,
base_folder=base_folder)
# Load the graphs
tf.reset_default_graph()
print("Loading ECNN graph...")
ecnn_graph = tf.train.import_meta_graph(os.path.join(ecnn_path, "ecnn.meta"), clear_devices=True)
trained_target = tf.get_default_graph().get_tensor_by_name("E-CNN/ecnn_output/ecnn_output_convolution/BiasAdd:0")
augmented_icnn_input = tf.concat([is_img, trained_target], axis=2)
icnn_graph = tf.train.import_meta_graph(os.path.join(icnn_path, "icnn.meta"), clear_devices=True, input_map={"aug_img": augmented_icnn_input, "it_img": it_img, "is_img": is_img})
The data input function reads 3 batches. aug_img is the source image, which has reflections, e.g. when photographed through a glass panel, augmented with its edge map as a 4th color channel. The ECNN graph should predict the reflection-free edge map. Tensorflow should augment the plain source image, which is stored in the is_img variable with the predicted reflection-free edge map, which should happen in the lines beginning with trained_target and augmented_icnn_input. The augmented image is then fed to the ICNN graph which should create a reflection-free image then, so it is given the it_img which is the target image. It is fed the not-augmented source image again, only for tensorboard visualization.
But now i am unable to further proceed. I cannot concat the both tensors for creating the augmented_icnn_input because i get a ValueError: Tensor("E-CNN/ecnn_output/ecnn_output_convolution/BiasAdd:0", shape=(?, 224, 224, 1), dtype=float32) must be from the same graph as Tensor("batch:1", shape=(?, 224, 224, 3), dtype=float32).
Also i seem not to understand the input_map in combination with the data input queue correctly, since although i have had definied the aug_imgand other variables in the ICNN, and see them in tensorboard, i do not see them in the variables collection and therefore cannot map them.
So i would like to know: Is this the correct approach to combine two subgraphs in a bigger graph? How can i solve the problem that i am unable to concat the two tensors in augmented_icnn_input? And why is my input_map not working?