Python tensorflow.python.keras.preprocessing.image.img_to_array() Examples
The following are 6
code examples of tensorflow.python.keras.preprocessing.image.img_to_array().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tensorflow.python.keras.preprocessing.image
, or try the search function
.
Example #1
Source File: train_TensorFlow.py From Neural-Style with MIT License | 7 votes |
def load_img(path_to_img): max_dim = 512 img = Image.open(path_to_img) img_size = max(img.size) scale = max_dim/img_size img = img.resize((round(img.size[0]*scale), round(img.size[1]*scale)), Image.ANTIALIAS) img = kp_image.img_to_array(img) # We need to broadcast the image array such that it has a batch dimension img = np.expand_dims(img, axis=0) # preprocess raw images to make it suitable to be used by VGG19 model out = tf.keras.applications.vgg19.preprocess_input(img) return tf.convert_to_tensor(out)
Example #2
Source File: idenprof.py From IdenProf with MIT License | 6 votes |
def run_inference(): model = ResNet50(input_shape=(224, 224, 3), num_classes=10) model.load_weights(MODEL_PATH) picture = os.path.join(execution_path, "Haitian-fireman.jpg") image_to_predict = image.load_img(picture, target_size=( 224, 224)) image_to_predict = image.img_to_array(image_to_predict, data_format="channels_last") image_to_predict = np.expand_dims(image_to_predict, axis=0) image_to_predict = preprocess_input(image_to_predict) prediction = model.predict(x=image_to_predict, steps=1) predictiondata = decode_predictions(prediction, top=int(5), model_json=JSON_PATH) for result in predictiondata: print(str(result[0]), " : ", str(result[1] * 100)) # run_inference()
Example #3
Source File: model_util.py From image-similarity with MIT License | 5 votes |
def preprocess_image(path): '''Process an image to numpy array. Args: path: the path of the image. Returns: Numpy array of the image. ''' img = process_image.load_img(path, target_size=(224, 224)) x = process_image.img_to_array(img) # x = np.expand_dims(x, axis=0) x = preprocess_input(x) return x
Example #4
Source File: tsne_grid.py From tsne-grid with MIT License | 5 votes |
def get_activations(model, img_collection): activations = [] for idx, img in enumerate(img_collection): if idx == to_plot: break; print("Processing image {}".format(idx+1)) img = img.resize((224, 224), Image.ANTIALIAS) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) activations.append(np.squeeze(model.predict(x))) return activations
Example #5
Source File: tsne_grid.py From tsne-grid with MIT License | 5 votes |
def save_tsne_grid(img_collection, X_2d, out_res, out_dim): grid = np.dstack(np.meshgrid(np.linspace(0, 1, out_dim), np.linspace(0, 1, out_dim))).reshape(-1, 2) cost_matrix = cdist(grid, X_2d, "sqeuclidean").astype(np.float32) cost_matrix = cost_matrix * (100000 / cost_matrix.max()) row_asses, col_asses, _ = lapjv(cost_matrix) grid_jv = grid[col_asses] out = np.ones((out_dim*out_res, out_dim*out_res, 3)) for pos, img in zip(grid_jv, img_collection[0:to_plot]): h_range = int(np.floor(pos[0]* (out_dim - 1) * out_res)) w_range = int(np.floor(pos[1]* (out_dim - 1) * out_res)) out[h_range:h_range + out_res, w_range:w_range + out_res] = image.img_to_array(img) im = image.array_to_img(out) im.save(out_dir + out_name, quality=100)
Example #6
Source File: data_preprocesser.py From BCNN-keras-clean with MIT License | 4 votes |
def _get_batches_of_transformed_samples(self, index_array): batch_x = np.zeros( (len(index_array),) + self.image_shape, dtype=floatx()) grayscale = self.color_mode == 'grayscale' # Build batch of image data for i, j in enumerate(index_array): fname = self.filenames[j] img = load_img( os.path.join(self.directory, fname), grayscale=grayscale, target_size=None, interpolation=self.interpolation) x = img_to_array(img, data_format=self.data_format) # Pillow images should be closed after `load_img`, but not PIL images. if hasattr(img, 'close'): img.close() x = self.image_data_generator.standardize(x) batch_x[i] = x # Optionally save augmented images to disk for debugging purposes if self.save_to_dir: for i, j in enumerate(index_array): img = array_to_img(batch_x[i], self.data_format, scale=True) fname = '{prefix}_{index}_{hash}.{format}'.format( prefix=self.save_prefix, index=j, hash=np.random.randint(1e7), format=self.save_format) img.save(os.path.join(self.save_to_dir, fname)) # Build batch of labels if self.class_mode == 'input': batch_y = batch_x.copy() elif self.class_mode == 'sparse': batch_y = self.classes[index_array] elif self.class_mode == 'binary': batch_y = self.classes[index_array].astype(floatx()) elif self.class_mode == 'categorical': batch_y = np.zeros( (len(batch_x), self.num_classes), dtype=floatx()) for i, label in enumerate(self.classes[index_array]): batch_y[i, label] = 1. else: return batch_x return batch_x, batch_y