How to resize a (28,28) ndarray to a (28,28,1)? - keras

How can I resize a (28,28) image to a (28,28,1) in keras?
I want to resize it to a 3d image for working with keras.

Related

Input image with circular FOV into CNN

I am working on a project that I need to train a CNN with Computer Tomography (CT) images, which has a circular Field Of View (FOV). All voxels outside the FOV is meaningless.
To my knowledge, people would usually only feed image with rectangular dimension into CNN model. For similar issues with circular image shape, they usually just crop part of the image to form a rectangular dimension during image-preprocessing. By this method, some part of the image is unavoidably cropped out from the training. Is there any way I can feed image with circular FOV into the model without cropping out any feature from the image? I can't really find similar topics for this concern on the internet so I ask here. Thank you.

does is affect NN training accuracy if the color format of images is BGR not RGB?

I'm training Neural Network with ImageNet dataset and I noticed that images are in BGR color format when I read them using OpenCV function cv2.imread(), so does is affect training accuracy?, if yes then how can I change it to RGB in pytorch?
It will not affect your NN's accuracy, in general. However, if you are using a pre-trained CNN, then it likely expects RGB images as input, and will not do as well on BGR images initially and will have to re-learn its weights for BGR.
You can convert BGR to RGB using cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB).
You can also consider the following alternatives for reading images:
torchvision.io.read_image(path) (https://pytorch.org/vision/stable/io.html#image)
torch.from_numpy(np.array(PIL.Image.open(path)))
torchvision.transforms.functional.pil_to_tensor(PIL.Image.open(path)) (https://pytorch.org/vision/stable/transforms.html#torchvision.transforms.functional.pil_to_tensor)

How to resize images from my dataset(64,64) to fit VGG16(224,224)

VGG16 takes (224,224) size images as its input.
I have a .h5 file which i want to test in this model.
Problem is images in my dataset are (64,64).
My file is test_catvnoncat.h5
1.How to change my entire .h5 images to (224,224)
thank you for your answer.
You can use OpenCV's resize function:
resized = cv2.resize(img, (224, 224))
You can look at the doc here

How do I crop a Landsat image into smaller chunks for training and then predict on the original image

I am looking at using Landsat imagery to train a CNN for unsupervised pixel-wise semantic segmentation classification. That said, I have been unable to find a method that allows me to crop images from the larger Landsat image for training and then predict on the original image. Essentially here is what I am trying to do:
Original Landsat image (5,000 x 5,000 - this is an arbitrary size, not exactly sure of the actual dimensions off-hand) -> crop the image into (100 x 100) chunks -> train the model on these cropped images -> output a prediction for each pixel in the original (uncropped) image.
That said, I am not sure if I should predict on the cropped images and stitch them together after they are predicted or if I can predict on the original image.
Any clarification/code examples would be greatly appreciated. For reference, I use both pytorch and tensorflow.
Thank you!
Lance D
Borrowing from Ronneberger et al., what we have been doing is to split the input Landsat scene and corresponding ground truth mask into overlapping tiles. Take the original image and pad it by the overlap margin (we use reflection for the padding) then split into tiles. Here is a code snippet using scikit-image:
import skimage as sk
patches = sk.util.view_as_windows(image,
(self.tile_height+2*self.image_margin,
self.tile_width+2*self.image_margin,raster_value['channels']),
(self.tile_height,self.tile_width,raster_value['channels'])
I don't know what you are using for a loss function for unsupervised segmentation. In our case with supervised learning, we crop the final segmentation prediction to match the ground truth output shape. In the Ronneberger paper they relied on shrinkage due to the use of valid padding.
For predictions you would do the same (split into overlapping tiles) and stitch the result.

Bounding box augmentation with Keras ImageDataGenerator

I'm using Keras to train a model to detect objects in images and put a bounding box around them.
I want to use ImageDataGenerator to augment the images with shift/rotate/sclae/etc.
The ImageDataGenerator is building a transformation matrix and using it to transform the images.
My question is, after I get back the augmented image, how can I adjust the bounding box according to the augmentation?
I'd say that if the transformation matrix was returned from the ImageDataGenerator together with the augmented image it will be great. but it doesn't.
So how to do it correctly?
Is it worth opening an issue for Keras to add this functionality?

Resources