Black image when image is normalized - python

Hi I'm using the following procedure to normalize images by following the tutorial on the TF 2.3 website:
from tensorflow.keras import layers
normalization_layer = tf.keras.layers.experimental.preprocessing.Rescaling(1./255)
normalized_ds = train_ds.map(lambda x, y: (normalization_layer(x), y))
image_batch, labels_batch = next(iter(normalized_ds))
first_image = image_batch[0]
# Notice the pixels values are now in `[0,1]`.
print(np.min(first_image), np.max(first_image))
But after testing this for training my vision model doesn't converge. It works fine if I don't use the normalization layer but I'm afraid I'm hurting its performance by using non-normalized images. Also, after trying to render the normalized images I see only black images.
I use imshow to show the images.
I don't really understand why the images are black when I use imshow() and why my model doesn't work with the normalized images

Related

CNN cropped image loss

I would like to train a CNN only on a cropped part of the CNN-output. This mean that the input image has a resolution of w x h. The output image processed by my model is also w x h. The loss should then be computed by comparing the crop of the output image at the center (w/2 x h/2) and the label.
Is PyTorch taking care of the cropping or will my model not learn properly because of the cropping?
I am aware that training a CNN with a variable input size is possible; however, I am not sure if the weights will be adjusted correctly since my loss operates on a different resolution than my model's output.
Thanks!

Predicting a class of a an image from google images(bag) using a model that is trained using fashion mnist dataset

I am trying to do Image Recognition in Python with TensorFlow and Keras.I'm only beginning with keras and machine learning. I have trained the model using fashion MNIST dataset. I am now trying to predict this model by using an external image from google images. I am using an image of a bag. Please see below
I understand I need to load this new image, force it to be grayscale format, and force the size to be 28×28 pixels as this is how my training images are while training the model. grayscale and 28 * 28.
Hence, I followed some blogs and used below code to the same.
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
img_path = 'data/bag2.jpg'
img = image.load_img(img_path,grayscale=True,target_size=(28, 28))
img_tensor = image.img_to_array(img)
img_tensor = numpy.expand_dims(img_tensor, axis=0)
img_tensor /= 255.
pyplot.imshow(img_tensor[0])
pyplot.show()
print(img_tensor.shape)
The output of the above code is as below
Why the background is yellow and image is not gray? Is this correct? Based on what I understand , the background should be black and image should be gray.
while I trying to predict this image using below code, I get output as zero
pred = model.predict(img_tensor.reshape(-1,28, 28, 1))
print(pred.argmax())
Thanks in advance.
The above error worked by using below code
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
img_path = 'data/bag5.jpg'
img = image.load_img(img_path,color_mode='grayscale',target_size=(28, 28))
img_tensor = image.img_to_array(img)
img_tensor = numpy.expand_dims(img_tensor, axis=0)
img_tensor /= 255.
pyplot.imshow(img_tensor[0], cmap='gray')
pyplot.show()
print(img_tensor.shape)
When you are plotting with pyplot.imshow(), if you mention cmap='gray' then you can see grayscale images. In above code yellow background is default behaviour of imshow function.
Now if you use above mentioned solution and don't get correct results then try to get similar image as that of the dataset. Fashion MNIST dataset has images - 28x28 pixels, grayscale i.e. with black background and cloth item at foreground. The image you read
img = image.load_img(img_path,grayscale=True,target_size=(28, 28))
is grayscale but with white background. So you can use -
img = ImageOps.invert(img)
Now try to plot this with cmap='gray' and do prediction. If your model is trained with a reasonable accuracy you'll get correct results, almost for many images.

How to detect the trained images in the neural network?

I have used Convolutional Neural Network(CNN) to train the legends in the maps. These legends are circles, ovals, diamonds, crosses, and squares. The neural network (inspired from the code https://towardsdatascience.com/from-raw-images-to-real-time-predictions-with-deep-learning-ddbbda1be0e4) works well in my case. The input images are individual cropped pictures of legends like input trained image and the output I want is to predict these legends in the maps like input maps. My neural network can now classify the images and predict whether they are squares or circles. For example, when I provided this image diamondinput as input, the output is diamond.
from keras.models import model_from_json
import numpy as np
EMOTIONS_LIST = ["circle","cross","diamond","oval","square"]
def predict_emotion(img):
preds = model.predict(img)
return EMOTIONS_LIST[np.argmax(preds)]
import cv2
import numpy as np
from google.colab.patches import cv2_imshow
import keras
#model = keras.models.load_model('/content/drive/MyDrive/model_weights_updated.h5')
fr=cv2.imread('/content/drive/MyDrive/Images/train/diamond/Copy of 0076.tif')
gray_fr = cv2.cvtColor(fr, cv2.COLOR_BGR2GRAY)
roi = cv2.resize(gray_fr, (48, 48))
pred = predict_emotion(roi[np.newaxis, :, :, np.newaxis])
print(pred)
Output for the program:
[[1.7809551e-06 2.4862277e-07 9.9999583e-01 2.1272169e-06 8.9550163e-09]], diamond
How can I make the neural network to predict these legends in the map and all other legends in the map like this outputmap?.
With the network you got, it would be possible to just split each map into a grid and then perform classification for each segment in the grid, but there are many problems with such an approach.
A better solution for you would be to use a neural network that does semantic segmentation. This way, your network regresses a likelihood-map for each of your shapes on the map. With this likelihood-map, you will know how many you have of each class and where they are.
To do so, you can start with the following MaskRCNN implementation.
https://github.com/matterport/Mask_RCNN

How to generate accuracy from a saved model of Keras?

I already trained my Keras model in .h5. My model use 6 classes and it able to classify all the classes by using images. The model able to output the name of the class that it successfully classified. However, I want to generate accuracy when testing the model with an image input by user. I already searching everywhere but still there are no answer for this problem.
model = load_model('prototype-tl2-80-20.h5')
classes = { 1:'Kacip Fatimah',
2:'Mempisang',
3:'Misai Adam',
4:'Pandan Serapat',
5:'Tapak Sulaiman',
6:'Tongkat Ali'}
image = Image.open(file_path)
image = image.resize((224,224))
image = numpy.expand_dims(image, axis=0)
image = numpy.array(image)
pred = model.predict_classes([image])[0]
sign = classes[pred+1]
print(sign)
to predict an image using a trained model you have to be careful to make sure the image is processed exactly as the training images were processed. The image should be the same size (height,width) as the training images and have the same number of color bands example 'rgb' or 'grayscale'. Make sure color bands are in the same order as used in training. Next you must apply the same preprocessing to the image. For example if your training images were scaled to be between 0 and 1 then you need to rescale your test image with image=image/255. After that than do
pred = model.predict(image)
index=np.argmax(pred)
class=classes[index]
print (index, class)

CNN batch with images of different size

I restored a pre-trained model for face detection which takes a single image at a time and returns bounding boxes. How can I make it take a batch of images if these images have different sizes?
You can use tf.image.resize_images method to achieve this. According to docs tf.image.resize_images:
Resize images to size using the specified method.
Resized images will be distorted if their original aspect ratio is not
the same as size. To avoid distortions see
tf.image.resize_image_with_pad.
How to use it?
import tensorflow as tf
from tensorflow.python.keras.models import Model
x = Input(shape=(None, None, 3), name='image_input')
resize_x = tf.image.resize_images(x, [32,32])
vgg_model = load_vgg()(resize_x)
model = Model(inputs=x, outputs=vgg_model.output)
model.compile(...)
model.predict(...)

Categories