I'm currently using Keras' image pre-processing functions to augment some training image data. As part of this I'm trying to visualise the augmentations which can be done by saving the images to a directory using the flow method from the ImageDataGenerator class:
https://keras.io/preprocessing/image/#flow
datagenerator.flow(image, batch_size=1, save_to_dir=args["imgdir"], save_prefix='aug',
save_format='png')
The problem is that the images I pass in are RGB and the images saved in the directory are BGR. The only transform that I'm doing is a rotation, why is it converting them to BGR? I can remedy the situation by converting the image to BGR before passing it to the generator flow method.
The generator itself is not producing BGR images - those remain in RGB format, they're just being converted when they're saved.
The mismatch in channels might be due to the libraries that you are using to load and store the images. Checking this would help you solve this problem.
Related
I am using tf.keras.preprocessing.image.load_image to load 3-channel images into a simple classification network. Based on the documentation for load_img, this method will return a 3-channel image even if the original image had a single channel (by duplicating the channels, I suppose).
Since it is possible for the user of my application to accidentally provide a single-channel image, this has the potential to fail silently with no way for the user to know why the predictions are bad.
I would therefore like to add a line to my code that makes sure that the original loaded image does indeed have 3 channels, and raise an error otherwise.
Is there a way to catch 1-channel images without loading them twice separately? Is there an alternative method to load_image that I can use for loading the images in a way that doesn't change the number of channels?
I have been trying to label my grayscale images for my CNN model. At this stage I aim for binary classification (Normal VS Fault conditions). I have distributed my images to two separate folders (Normal and Fault) and want to use the corresponding directories in my codes. However, I did not find a resource for labelling grayscale images using directories. Does any one know how we can label our grayscale images using directories. I can ask my question in this way as well, can we label our grayscale images using ImageDataGenerator function?
NOTE: I use following codes, but it converts my grayscale images to coloured ones.
train_path = 'the directory'
train_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input)
.flow_from_directory(directory=train_path, target_size=(64,64), classes=['Normal','fault'], batch_size=10)
Best regards.
Well, you can create a training dataframe having 2 columns image_path and label and then iterate through the different directories and save the directory name in label column and path of the image in image_path column like below
import pandas as pd
data = pd.DataFrame()
images = [ ]
label = [ ]
for i in os.listdir( normal_directory_path ):
images.append( normal_directory_path/i)
label.append( "Normal" )
# similarly for fault directory
Then shuffle the dataset and then you can use dataloader to load the dataset into batches and while loading open the image_path and extract the image array out of it and train the model.
VGG is an artificial neural network trained for images having three channels. If you take a glance at the official document (https://www.tensorflow.org/api_docs/python/tf/keras/applications/vgg16/preprocess_input), you will see that this preprocessing method accepts images having three channels, and preprocesses them (again from the document):
Returns
Preprocessed numpy.array or a tf.Tensor with type float32.
The images are converted from RGB to BGR, then each color channel is zero-centered with respect to the ImageNet dataset, without scaling.
So, if you are going to feed grayscale images to your model, you need to do the preprocessing yourself. To do so, you will need ImageNet dataset statistics like "mean" value, to zero center every pixel in your images. This mean will be also per channel, so just take the weighted mean of the three channels (w.r.t. Colorimetric (perceptual luminance-preserving) conversion to grayscale) to find one channel mean.
References: Colorimetric (perceptual luminance-preserving) conversion to grayscale -https://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale
I have a large images 5000x3500 and I want to divide it into small images 512x512 but without loosing the original image coordinates. The large images are annotated/labled that's why I want to keep the original coordinates and I will use the small images to train YOLO model. I am not sure if that called tiled or not. but is there any suggestion to do it using python or opencv-python?
I'm training a deep neural network to improve the quality of images. The images contain some specific types of noise that I want to reduce/remove by means of a deep learning model. In order to do so I'm using a huge dataset of similar clear high-res images with barely any noise, add the specific types of noise to the images and train the network on regenerating the original image (a custom autoencoder network). With one of the several noise types this works very well so far. Without going to far into the details, adding that particular type of noise was easy.
Now I need to add another noise type to the images, more precisely: chroma noise like in the following image (the bottom right one): link
How do I artificially generate and add chroma noise to an image in Python? I can use the full range of image processing packages, PIL, numpy, OpenCV, torchvision...
You need to convert the image to a colorspace such as HSV or CIE Lab. You then add noise to the chromacity channels (a, b in Lab, or H, S is HSV). Finally, convert back to RGB.
This colorspace conversion step is very common and most image toolkits should have that functionality.
I am using python with keras, and wish to use keras image pre processing, some of my images are rgb while some are grayscale, I need to read all of them and changing the dimension of the grayscale images to x,x,3 or disposing them, and would like to do it as a part of the .flow function the same way I could use color_mode with .flow_from_directory, than can be set to rgb and read all the images as rgb even when they are grayscale,
Is that possible?
You can't do it as part of .flow because that assumes you already have loaded an prepared your images into a 4D tensor. You can use the load_img function which the .flow_from_directory uses that actually consumes the color_mode argument:
img = load_img(os.path.join(self.directory, fname),
color_mode=self.color_mode,
target_size=self.target_size,
interpolation=self.interpolation)
This is from the flow_from_directory code. You can use this function to load your images and then call .flow.