本文整理汇总了Python中tensorflow.python.keras.preprocessing.image.img_to_array方法的典型用法代码示例。如果您正苦于以下问题:Python image.img_to_array方法的具体用法?Python image.img_to_array怎么用?Python image.img_to_array使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.python.keras.preprocessing.image
的用法示例。
在下文中一共展示了image.img_to_array方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_img
# 需要导入模块: from tensorflow.python.keras.preprocessing import image [as 别名]
# 或者: from tensorflow.python.keras.preprocessing.image import img_to_array [as 别名]
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)
示例2: run_inference
# 需要导入模块: from tensorflow.python.keras.preprocessing import image [as 别名]
# 或者: from tensorflow.python.keras.preprocessing.image import img_to_array [as 别名]
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()
示例3: preprocess_image
# 需要导入模块: from tensorflow.python.keras.preprocessing import image [as 别名]
# 或者: from tensorflow.python.keras.preprocessing.image import img_to_array [as 别名]
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
示例4: get_activations
# 需要导入模块: from tensorflow.python.keras.preprocessing import image [as 别名]
# 或者: from tensorflow.python.keras.preprocessing.image import img_to_array [as 别名]
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
示例5: save_tsne_grid
# 需要导入模块: from tensorflow.python.keras.preprocessing import image [as 别名]
# 或者: from tensorflow.python.keras.preprocessing.image import img_to_array [as 别名]
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)
示例6: _get_batches_of_transformed_samples
# 需要导入模块: from tensorflow.python.keras.preprocessing import image [as 别名]
# 或者: from tensorflow.python.keras.preprocessing.image import img_to_array [as 别名]
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