Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

machine learning - How can I add zero-padding to make images in dataset of equal size?

I'm working with a dataset having a size of more than 30Gb, the problem is that the images(RGB) in the dataset aren't of the same dimension. As I'm implementing custom CNN I'll be required to give input_size for the first convolutional layer. Is there any way to add zero-padding generically. I've initially implemented a pre-trained model(ResNet-50) and used the following method

from tensorflow.keras.applications.resnet50 import preprocess_input 
ImageDataGenerator(preprocess_input ,validation_split=0.2)

This made my dataset compatible with the model, is there any similar way where I can add zero-padding on the dataset but for a custom CNN model.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Although zero-padding might not be an ideal solution for handling images of various sizes, you can zero-pad images in python by the following methods.

  1. Numpy method reference
padded_image = np.zeros(result_shape)
padded_image[:image.shape[0],:image.shape[1]] = image
  1. Tensorflow method documentation
padded_image = tf.image.pad_to_bounding_box(image, top_padding, left_padding, target_height, target_width)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...