def _get_batches_of_transformed_samples(self, index_array): batch_x = np.zeros(shape=(len(index_array),) + self.target_size + (3,)) batch_y = np.zeros(shape=(len(index_array),) + self.target_size + (self.num_classes,)) for i, idx in enumerate(index_array): image, label = load_image(self.images_list[idx]), load_image(self.labels_list[idx]) if self.image_data_generator.random_crop: image, label = random_crop(image, label, self.target_size) else: image, label = resize_image(image, label, self.target_size) if np.random.uniform(0., 1.) < self.data_aug_rate: if np.random.randint(2): image, label = random_vertical_flip(image, label, self.image_data_generator.vertical_flip) if np.random.randint(2): image, label = random_horizontal_flip(image, label, self.image_data_generator.horizontal_flip) if np.random.randint(2): image, label = random_brightness(image, label, self.image_data_generator.brightness_range) if np.random.randint(2): image, label = random_rotation(image, label, self.image_data_generator.rotation_range) if np.random.randint(2): image, label = random_channel_shift(image, label, self.image_data_generator.channel_shift_range) if np.random.randint(2): image, label = random_zoom(image, label, self.image_data_generator.zoom_range) image = imagenet_utils.preprocess_input(image.astype('float32'), data_format='channels_last', mode='torch') label = one_hot(label, self.num_classes) batch_x[i], batch_y[i] = image, label return batch_x, batch_y
def create_features(dataset, pre_model): x_scratch = [] i = 0 # loop over the images print("loop started") for imagePath in dataset: print("image", i, "\n") # load the input image and image is resized to 224x224 pixels # 299x299 for inceptionV3 image = load_img(imagePath, target_size=(224, 224)) image = img_to_array(image) # preprocess the image by (1) expanding the dimensions and # (2) subtracting the mean RGB pixel intensity from the # ImageNet dataset image = np.expand_dims(image, axis=0) image = imagenet_utils.preprocess_input(image) # add the image to the batch x_scratch.append(image) i += 1 print("for loop done") x = np.vstack(x_scratch) features = pre_model.predict(x, batch_size=32) # features_flatten = features.reshape((features.shape[0], 7 * 7 * 512)) return x, features
def _get_X_and_names(self, model: CnnModel, list_fams, num_samples, property_alias: str) -> Tuple[np.ndarray, List[str]]: """ Used to get all the features from a given set along with their names :param list_fams: the list of families :param num_samples: the number of samples :param property_alias: the property to be looked :return: a Tuple containing a Numpy array with the features and a List containing the name of the images """ channels, width, height = model.input_shape X_train = np.zeros((num_samples, width, height, channels)) cnt = 0 samples_names = [] print("Processing images ...") for i in range(len(list_fams)): print('current fam: ', i) for index, img_file in enumerate( self._fetch_all_images(join(list_fams[i], property_alias))): img = image.load_img(img_file, target_size=(width, height)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) X_train[cnt] = x cnt += 1 index = img_file.find(self.frame_delimiter) samples_names.append(img_file[0:index]) return X_train, samples_names
def preprocess(x, pre_process_method): if pre_process_method not in dic_mine_to_keras.keys(): raise ValueError("mode {} doesn't supported. Expected values: {}".format(pre_process_method, dic_mine_to_keras.keys())) if isinstance(x, np.ndarray): t = deepcopy(x) else: t = x return preprocess_input(x=t, mode=dic_mine_to_keras[pre_process_method], data_format='channels_last')
def preprocess_input(x, **kwargs): """Preprocesses a numpy array encoding a batch of images. # Arguments x: a 4D numpy array consists of RGB values within [0, 255]. # Returns Preprocessed array. """ return imagenet_utils.preprocess_input(x, mode='tf', **kwargs)
def preprocess_input(x): """Preprocesses a numpy array encoding a batch of images. # Arguments x: a 4D numpy array consists of RGB values within [0, 255]. # Returns Input array scaled to [-1.,1.] """ return imagenet_utils.preprocess_input(x, mode='tf')
def preprocess_input_keras(x, **kwargs): """Preprocesses a numpy array encoding a batch of images. # Arguments x: a 4D numpy array consists of RGB values within [0, 255]. data_format: data format of the image tensor. # Returns Preprocessed array. """ return preprocess_input(x, mode='torch', **kwargs)
def test_preprocess_input_symbolic(): # Test image batch x = np.random.uniform(0, 255, (2, 10, 10, 3)) inputs = layers.Input(shape=x.shape[1:]) outputs = layers.Lambda(utils.preprocess_input, output_shape=x.shape[1:])(inputs) model = models.Model(inputs, outputs) assert model.predict(x).shape == x.shape outputs1 = layers.Lambda( lambda x: utils.preprocess_input(x, 'channels_last'), output_shape=x.shape[1:])(inputs) model1 = models.Model(inputs, outputs1) out1 = model1.predict(x) x2 = np.transpose(x, (0, 3, 1, 2)) inputs2 = layers.Input(shape=x2.shape[1:]) outputs2 = layers.Lambda( lambda x: utils.preprocess_input(x, 'channels_first'), output_shape=x2.shape[1:])(inputs2) model2 = models.Model(inputs2, outputs2) out2 = model2.predict(x2) assert_allclose(out1, out2.transpose(0, 2, 3, 1)) # Test single image x = np.random.uniform(0, 255, (10, 10, 3)) inputs = layers.Input(shape=x.shape) outputs = layers.Lambda(utils.preprocess_input, output_shape=x.shape)(inputs) model = models.Model(inputs, outputs) assert model.predict(x[np.newaxis])[0].shape == x.shape outputs1 = layers.Lambda( lambda x: utils.preprocess_input(x, 'channels_last'), output_shape=x.shape)(inputs) model1 = models.Model(inputs, outputs1) out1 = model1.predict(x[np.newaxis])[0] x2 = np.transpose(x, (2, 0, 1)) inputs2 = layers.Input(shape=x2.shape) outputs2 = layers.Lambda( lambda x: utils.preprocess_input(x, 'channels_first'), output_shape=x2.shape)(inputs2) model2 = models.Model(inputs2, outputs2) out2 = model2.predict(x2[np.newaxis])[0] assert_allclose(out1, out2.transpose(1, 2, 0))
def preprocess_input(rgb_values, data_format=None): """Preprocesses a numpy array encoding a batch of images. # Arguments rgb_values: a 3D or 4D numpy array consists of RGB values within [0, 255]. data_format: data format of the image tensor. # Returns Preprocessed array. """ return imagenet_utils.preprocess_input(rgb_values, data_format, mode='torch')
def read_multi_image(image_paths): list_images = [] for (j, imagePath) in enumerate(image_paths): print("reading image " + str(j)) img = load_img(imagePath, target_size=(224, 224)) img = img_to_array(img) img = np.expand_dims(img, 0) img = imagenet_utils.preprocess_input(img) list_images.append(img) list_images = np.vstack(list_images) return list_images
def prepare_image(image, target): # image mode should be "RGB" if image.mode != "RGB": image = image.convert("RGB") # resize for model image = image.resize(target) image = img_to_array(image) image = np.expand_dims(image, axis=0) image = imagenet_utils.preprocess_input(image) # return it return image
def _get_batches_of_transformed_samples(self, index_array): batch_x = [] batch_y = [] for batch_index, image_index in enumerate(index_array): id = self.image_ids[image_index] img_name = self.image_name_template.format(id=id) path = os.path.join(self.images_dir, img_name) image = np.array(img_to_array(load_img(path)), "uint8") mask_name = self.mask_template.format(id=id) mask_path = os.path.join(self.masks_dir, mask_name) mask = cv2.imread(mask_path, cv2.IMREAD_COLOR) label = cv2.imread( os.path.join(self.labels_dir, self.label_template.format(id=id)), cv2.IMREAD_UNCHANGED) if args.use_full_masks: mask[..., 0] = (label > 0) * 255 if self.crop_shape is not None: crop_mask, crop_image, crop_label = self.augment_and_crop_mask_image( mask, image, label, id, self.crop_shape) data = self.random_transformer( image=np.array(crop_image, "uint8"), mask=np.array(crop_mask, "uint8")) crop_image, crop_mask = data['image'], data['mask'] if len(np.shape(crop_mask)) == 2: crop_mask = np.expand_dims(crop_mask, -1) crop_mask = self.transform_mask(crop_mask, crop_image) batch_x.append(crop_image) batch_y.append(crop_mask) else: x0, x1, y0, y1 = 0, 0, 0, 0 if (image.shape[1] % 32) != 0: x0 = int((32 - image.shape[1] % 32) / 2) x1 = (32 - image.shape[1] % 32) - x0 if (image.shape[0] % 32) != 0: y0 = int((32 - image.shape[0] % 32) / 2) y1 = (32 - image.shape[0] % 32) - y0 image = np.pad(image, ((y0, y1), (x0, x1), (0, 0)), 'reflect') mask = np.pad(mask, ((y0, y1), (x0, x1), (0, 0)), 'reflect') batch_x.append(image) mask = self.transform_mask(mask, image) batch_y.append(mask) batch_x = np.array(batch_x, dtype="float32") batch_y = np.array(batch_y, dtype="float32") if self.preprocessing_function: batch_x = imagenet_utils.preprocess_input( batch_x, mode=self.preprocessing_function) return self.transform_batch_x(batch_x), self.transform_batch_y(batch_y)
def __init__(self, config): """Constructor. """ self.config = config idxs = [] with open(self.config['path'] + '/seq_ref_clean.txt', 'r', encoding="latin-1") as f: for l, line in enumerate(f): if l >= 4: idxs.append([int(j) for j in line.split(' ')]) idxs[-1][1] = 4371 X = self.config['pad_value'] * np.ones( (len(idxs), np.max(np.diff(np.array(idxs), axis=1)) + 1, self.config['size'], self.config['size'], 3), dtype=self.config['dtype']) y = np.zeros((len(idxs), ), dtype='int32') for i, idx in enumerate(idxs): for j, q in enumerate(range(idx[0], idx[1] + 1)): img = imread(self.config['path'] + '/img_clean/img' + '{:04d}'.format(q) + '.jpg') img = imresize(img, (self.config['size'], self.config['size'])) X[i, j, :, :, :] = img y[i] = i X /= 255. # zca if self.config['zca_epsilon'] is not None: self.ZCA = ZCAWhitening(epsilon=self.config['zca_epsilon']) X = self.ZCA.fit_transform(X) if self.config['imagenet_preprocessing'] is True: X = np.pad(X, ((0, 0), (0, 0), (0, 32 - self.config['size']), (0, 32 - self.config['size']), (0, 0)), 'constant') for i in range(len(X)): for t in range(len(X[0])): X[i, t] = imresize(X[i, t], (32, 32)) X = preprocess_input(X, data_format='channels_last') Y = to_categorical(y, len(X)) self.X = X self.Y = Y print(self.X.shape, self.Y.shape)
def __getitem__(self, idx): batch_start = idx * self.config['batch_size'] batch_end = np.min( [batch_start + self.config['batch_size'], len(self.videos)]) metas = self.metas[batch_start:batch_end] lens = [v[0] for v in metas] X = [] for v, video in enumerate(self.videos[batch_start:batch_end]): frame_start = np.random.randint(lens[v] - self.config['frames']) frame_end = np.min([frame_start + self.config['frames'], lens[v]]) frames = self.build_frames(video, metas[v], frame_start=frame_start, frame_end=frame_end) X.append(frames) X = np.array(X).astype(self.config['dtype']) if self.config['zca_epsilon'] is not None: if self.precomputing is True: X = self.zca.partial_fit_transform(X) else: if self.train_val_test in [ 'train', 'train_labeled' ] and 'size_crop' in self.config.keys(): X = self.zca.partial_fit_transform(X) else: X = self.zca.transform(X) if self.config['imagenet_preprocessing'] is True: if X.shape[-1] == 1: X = np.repeat(X, 3, axis=-1) X = np.pad(X, ((0, 0), (0, 0), (0, 32 - self.config['size']), (0, 32 - self.config['size']), (0, 0)), 'constant') for i in range(len(X)): for t in range(len(X[0])): X[i, t] = imresize(X[i, t], (32, 32)) X = preprocess_input(X, data_format='channels_last') if self.classes is None: Y = X else: Y = [] for v, video in enumerate(self.videos[batch_start:batch_end]): y = self.classes.index(video.split('/')[0]) Y.append(y) Y = to_categorical(Y, len(self.classes)) return (X, Y)
def __init__(self, config): """Constructor. """ self.config = config X = np.zeros((100, 72, self.config['size'], self.config['size'], 3), dtype=self.config['dtype']) y = np.zeros((100, ), dtype='int32') for i in range(len(X)): for j, t in enumerate(range(0, 360, 5)): img = imread(self.config['path'] + '/obj' + str(i + 1) + '__' + str(t) + '.png') img = imresize(img, (self.config['size'], self.config['size'])) X[i, j, :, :, :] = img y[i] = i X /= 255. # zca if self.config['zca_epsilon'] is not None: self.ZCA = ZCAWhitening(epsilon=self.config['zca_epsilon']) X = self.ZCA.fit_transform(X) if self.config['imagenet_preprocessing'] is True: X = np.pad(X, ((0, 0), (0, 0), (0, 32 - self.config['size']), (0, 32 - self.config['size']), (0, 0)), 'constant') for i in range(len(X)): for t in range(len(X[0])): X[i, t] = imresize(X[i, t], (32, 32)) X = preprocess_input(X, data_format='channels_last') Y = to_categorical(y, len(X)) self.X = X self.Y = Y print(self.X.shape, self.Y.shape)
maxpool = model.get_layer(name='block5_pool') shape = maxpool.output_shape[1:] dense = model.get_layer(name='fc1') layer_utils.convert_dense_weights_data_format( dense, shape, 'channels_first') if K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') return model if __name__ == '__main__': model = VGG19(include_top=True, weights='imagenet') img_path = 'cat.jpg' img = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) print('Input image shape:', x.shape) preds = model.predict(x) print('Predicted:', decode_predictions(preds))
def preprocess_input(x, **kwargs): return imagenet_utils.preprocess_input(x, mode='torch', **kwargs)
if args.color_csv is None: csv_file = os.path.join('CamVid', 'class_dict.csv') else: csv_file = args.csv_file _, color_values = get_colored_info(csv_file) for i, name in enumerate(image_names): sys.stdout.write('\rRunning test image %d / %d' % (i + 1, len(image_names))) sys.stdout.flush() image = cv2.resize(load_image(name), dsize=(args.crop_width, args.crop_height)) image = imagenet_utils.preprocess_input(image.astype(np.float32), data_format='channels_last', mode='torch') # image processing if np.ndim(image) == 3: image = np.expand_dims(image, axis=0) assert np.ndim(image) == 4 # get the prediction prediction = net.predict(image) if np.ndim(prediction) == 4: prediction = np.squeeze(prediction, axis=0) # decode one-hot prediction = decode_one_hot(prediction)
def preprocess_input(*args, **kwargs): return imagenet_utils.preprocess_input(*args, **kwargs)
def preprocess_input(*args, **kwargs): kwargs['backend'] = backend return utils.preprocess_input(*args, **kwargs)
# load data and transform gray images to rgb images images = np.stack( [gray2rgb(io.imread(image_dir + '/' + fn)) for fn in file_names], axis=0) gt_maps = np.stack([io.imread(gt_dir + '/' + fn) for fn in file_names], axis=0) # Zero padding the input images pad_widths = [(0, 0), (8, 8), (12, 12), (0, 0)] if dataset == 'cyc2_1488x1512' else [(0, 0), (0, 0), (8, 8), (0, 0)] images_pad = np.pad(images, pad_widths, mode='reflect') ## Image preprocessing print('Preprocessing ...') preprocess_input = cm.get_preprocessing(backbone) ## preprocessing function images_pad = preprocess_input(images_pad) # will scale pixels between 0 and 1 and then will normalize each channel with respect to the ImageNet dataset print('Preprocessing done !') ## Load the trained model model = tf.keras.models.load_model(model_folder + '/ready_model.h5') ## Label map prediction pr_masks = model.predict(images_pad, batch_size=1) ## probability maps [N(num of images) x H x W x C(class)] for 0: G1, 1: S, 2: G2, 3: background/M phase # crop images back pr_masks = pr_masks[:, pad_widths[1][0]:-pad_widths[1][1], pad_widths[2][ 0]:-pad_widths[2][ 1], :] if dataset == 'cyc2_1488x1512' else pr_masks[:, :, pad_widths[ 2][0]:-pad_widths[2][1], :] pr_masks_ = np.zeros(pr_masks.shape, dtype=np.float32)
from keras.applications.resnet50 import ResNet50 from keras.preprocessing import image from keras_applications.imagenet_utils import preprocess_input from keras.applications.resnet50 import preprocess_input, decode_predictions import numpy as np model = ResNet50(weights='imagenet') img_path = '/Users/mosampatel/Documents/mosam.jpeg' img = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) # decode the results into a list of tuples (class, description, probability) # (one such list for each sample in the batch) print('Predicted:', decode_predictions(preds, top=3)[0])
def __init__(self, config): """Constructor. """ self.config = config (X_train, y_train), (X_test, y_test) = cifar10.load_data() X_train = X_train.reshape(-1, 32, 32, 3) X_test = X_test.reshape(-1, 32, 32, 3) X_train = X_train.astype(self.config['dtype']) X_test = X_test.astype(self.config['dtype']) N = ((32 - self.config['size']) // self.config['stride']) + 1 X_train_scan = np.zeros((X_train.shape[0], N**2, self.config['size'], self.config['size'], 3)) X_test_scan = np.zeros((X_test.shape[0], N**2, self.config['size'], self.config['size'], 3)) for i in range(len(X_train)): for t1 in range(N): for t2 in (range(N) if (np.mod(t1, 2) == 0) else range(N - 1, -1, -1)): X_train_scan[i, t1 * N + (t2 if (np.mod(t1, 2) == 0) else (N - 1 - t2)), :, :, :] = X_train[i, ( t2 * self.config['stride']):( t2 * self.config['stride'] + self.config['size']), ( t1 * self.config['stride']):( t1 * self.config['stride'] + self.config['size']), :] for i in range(len(X_test)): for t1 in range(N): for t2 in (range(N) if (np.mod(t1, 2) == 0) else range(N - 1, -1, -1)): X_test_scan[i, t1 * N + (t2 if (np.mod(t1, 2) == 0) else (N - 1 - t2)), :, :, :] = X_test[ i, (t2 * self.config['stride']):( t2 * self.config['stride'] + self.config['size']), (t1 * self.config['stride']):( t1 * self.config['stride'] + self.config['size']), :] X_train_scan /= 255. X_test_scan /= 255. # zca if self.config['zca_epsilon'] is not None: self.ZCA = ZCAWhitening(epsilon=self.config['zca_epsilon']) X_train_scan = self.ZCA.fit_transform(X_train_scan) X_test_scan = self.ZCA.transform(X_test_scan) if self.config['imagenet_preprocessing'] is True: X_train_scan = np.pad(X_train_scan, ((0, 0), (0, 0), (0, 32 - self.config['size']), (0, 32 - self.config['size']), (0, 0)), 'constant') X_test_scan = np.pad(X_test_scan, ((0, 0), (0, 0), (0, 32 - self.config['size']), (0, 32 - self.config['size']), (0, 0)), 'constant') for i in range(len(X_train_scan)): for t in range(len(X_train_scan[0])): X_train_scan[i, t] = imresize(X_train_scan[i, t], (32, 32)) for i in range(len(X_test_scan)): for t in range(len(X_test_scan[0])): X_test_scan[i, t] = imresize(X_test_scan[i, t], (32, 32)) X_train_scan = preprocess_input(X_train_scan, data_format='channels_last') X_test_scan = preprocess_input(X_test_scan, data_format='channels_last') nb_classes = 10 Y_train = to_categorical(y_train, nb_classes) Y_test = to_categorical(y_test, nb_classes) self.X_train = X_train_scan self.Y_train = Y_train self.X_test = X_test_scan self.Y_test = Y_test print(self.X_train.shape, self.Y_train.shape, self.X_test.shape, self.Y_test.shape)
def __init__(self, config): """Constructor. """ self.config = config (X_train, y_train), (X_test, y_test) = mnist.load_data() X_train = X_train.reshape(-1, 28, 28, 1) X_test = X_test.reshape(-1, 28, 28, 1) X_train = X_train.astype(self.config['dtype']) X_test = X_test.astype(self.config['dtype']) X_train_rot = np.zeros((X_train.shape[0], self.config['rotations'], self.config['size'], self.config['size'], 1)) X_test_rot = np.zeros((X_test.shape[0], self.config['rotations'], self.config['size'], self.config['size'], 1)) for i in range(len(X_train)): img = Image.fromarray(np.reshape(X_train[i, :, :, :], (28, 28))) img = img.convert('L') phase = 360. * np.random.rand() for t in range(self.config['rotations']): _img = img.rotate(phase + t * 360. / self.config['rotations'], Image.BILINEAR).resize((28, 28), Image.BILINEAR).getdata() _img = np.reshape(np.array(_img, dtype=np.uint8), (28, 28, 1))[:self.config['size'], :self.config['size'], :] X_train_rot[i, t, :, :, :] = _img.astype(dtype=self.config['dtype']) for i in range(len(X_test)): img = Image.fromarray(np.reshape(X_test[i, :, :, :], (28, 28))) img = img.convert('L') phase = 360. * np.random.rand() for t in range(self.config['rotations']): _img = img.rotate(phase + t * 360. / self.config['rotations'], Image.BILINEAR).resize((28, 28), Image.BILINEAR).getdata() _img = np.reshape(np.array(_img, dtype=np.uint8), (28, 28, 1))[:self.config['size'], :self.config['size'], :] X_test_rot[i, t, :, :, :] = _img.astype(dtype=self.config['dtype']) X_train_rot /= 255. X_test_rot /= 255. # zca if self.config['zca_epsilon'] is not None: self.ZCA = ZCAWhitening(epsilon=self.config['zca_epsilon']) X_train_rot = self.ZCA.fit_transform(X_train_rot) X_test_rot = self.ZCA.transform(X_test_rot) if self.config['imagenet_preprocessing'] is True: X_train_rot = np.pad(X_train_rot, ((0, 0), (0, 0), (0, 32 - self.config['size']), (0, 32 - self.config['size']), (0, 0)), 'constant') X_test_rot = np.pad(X_test_rot, ((0, 0), (0, 0), (0, 32 - self.config['size']), (0, 32 - self.config['size']), (0, 0)), 'constant') X_train_rot = np.repeat(X_train_rot, 3, axis=-1) X_test_rot = np.repeat(X_test_rot, 3, axis=-1) for i in range(len(X_train_rot)): for t in range(len(X_train_rot[0])): X_train_rot[i, t] = imresize(X_train_rot[i, t], (32, 32)) for i in range(len(X_test_rot)): for t in range(len(X_test_rot[0])): X_test_rot[i, t] = imresize(X_test_rot[i, t], (32, 32)) X_train_rot = preprocess_input(X_train_rot, data_format='channels_last') X_test_rot = preprocess_input(X_test_rot, data_format='channels_last') nb_classes = 10 Y_train = to_categorical(y_train, nb_classes) Y_test = to_categorical(y_test, nb_classes) self.X_train = X_train_rot self.Y_train = Y_train self.X_test = X_test_rot self.Y_test = Y_test print(self.X_train.shape, self.Y_train.shape, self.X_test.shape, self.Y_test.shape)
def preprocessing_function_caffe(x): return preprocess_input(x, data_format='channels_last', mode='caffe')
def preprocess_inputs(x): return preprocess_input(x, mode=args.preprocessing_function)
def test_preprocess_input(): # Test image batch with float and int image input x = np.random.uniform(0, 255, (2, 10, 10, 3)) xint = x.astype('int32') assert utils.preprocess_input(x).shape == x.shape assert utils.preprocess_input(xint).shape == xint.shape out1 = utils.preprocess_input(x, 'channels_last') out1int = utils.preprocess_input(xint, 'channels_last') out2 = utils.preprocess_input(np.transpose(x, (0, 3, 1, 2)), 'channels_first') out2int = utils.preprocess_input(np.transpose(xint, (0, 3, 1, 2)), 'channels_first') assert_allclose(out1, out2.transpose(0, 2, 3, 1)) assert_allclose(out1int, out2int.transpose(0, 2, 3, 1)) # Test single image x = np.random.uniform(0, 255, (10, 10, 3)) xint = x.astype('int32') assert utils.preprocess_input(x).shape == x.shape assert utils.preprocess_input(xint).shape == xint.shape out1 = utils.preprocess_input(x, 'channels_last') out1int = utils.preprocess_input(xint, 'channels_last') out2 = utils.preprocess_input(np.transpose(x, (2, 0, 1)), 'channels_first') out2int = utils.preprocess_input(np.transpose(xint, (2, 0, 1)), 'channels_first') assert_allclose(out1, out2.transpose(1, 2, 0)) assert_allclose(out1int, out2int.transpose(1, 2, 0)) # Test that writing over the input data works predictably for mode in ['torch', 'tf']: x = np.random.uniform(0, 255, (2, 10, 10, 3)) xint = x.astype('int') x2 = utils.preprocess_input(x, mode=mode) xint2 = utils.preprocess_input(xint) assert_allclose(x, x2) assert xint.astype('float').max() != xint2.max() # Caffe mode works differently from the others x = np.random.uniform(0, 255, (2, 10, 10, 3)) xint = x.astype('int') x2 = utils.preprocess_input(x, data_format='channels_last', mode='caffe') xint2 = utils.preprocess_input(xint) assert_allclose(x, x2[..., ::-1]) assert xint.astype('float').max() != xint2.max()