dataset pre-processing

Add Padding to data/images:

x_train = np.pad(x_train, ((0,0),(2,2),(2,2),(0,0)), 'constant')
x_test = np.pad(x_test, ((0,0),(2,2),(2,2),(0,0)), 'constant')
print("Updated Image Shape: {}".format(x_train[0].shape))

Image Resizing:

import cv2
import numpy as np
from keras import backend as K
from keras.utils import np_utils
# Example 1
if K.image_dim_ordering() == 'th':
   X_train = np.array([cv2.resize(img.transpose(1,2,0), (img_rows,img_cols)).transpose(2,0,1) for img in X_train[:nb_train_samples,:,:,:]])
   X_valid = np.array([cv2.resize(img.transpose(1,2,0), (img_rows,img_cols)).transpose(2,0,1) for img in X_valid[:nb_valid_samples,:,:,:]])
   X_train = np.array([cv2.resize(img, (img_rows,img_cols)) for img in X_train[:nb_train_samples,:,:,:]])
   X_valid = np.array([cv2.resize(img, (img_rows,img_cols)) for img in X_valid[:nb_valid_samples,:,:,:]])

# Example 2
x_train=tf.keras.backend.resize_images(x_test, 224, 224, 'channels_last', interpolation='nearest')

# Example 3
x_train=tf.image.resize_images(x_train, [224,224], align_corners=False, preserve_aspect_ratio=False, name=None)

# Example 4
nb_train_samples = 50000 # 5000 training samples
nb_test_samples = 10000 # 10000 test samples
img_rows, img_cols = 224, 224
if K.image_dim_ordering() == 'th':
  x_train = np.array([cv2.resize(img.transpose(1,2,0), (img_rows,img_cols)).transpose(2,0,1) for img in x_train[:nb_train_samples,:,:,:]])
  x_test = np.array([cv2.resize(img.transpose(1,2,0), (img_rows,img_cols)).transpose(2,0,1) for img in x_test[:nb_test_samples,:,:,:]])


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.