本文整理汇总了Python中keras.applications.xception.preprocess_input方法的典型用法代码示例。如果您正苦于以下问题:Python xception.preprocess_input方法的具体用法?Python xception.preprocess_input怎么用?Python xception.preprocess_input使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.applications.xception
的用法示例。
在下文中一共展示了xception.preprocess_input方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from keras.applications import xception [as 别名]
# 或者: from keras.applications.xception import preprocess_input [as 别名]
def main(args):
# create model
model = load_model(args.model)
# load class names
classes = []
with open(args.classes, 'r') as f:
classes = list(map(lambda x: x.strip(), f.readlines()))
# load an input image
img = image.load_img(args.image, target_size=(299, 299))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
# predict
pred = model.predict(x)[0]
result = [(classes[i], float(pred[i]) * 100.0) for i in range(len(pred))]
result.sort(reverse=True, key=lambda x: x[1])
for i in range(args.top_n):
(class_name, prob) = result[i]
print("Top %d ====================" % (i + 1))
print("Class name: %s" % (class_name))
print("Probability: %.2f%%" % (prob))
示例2: generate_from_paths_and_labels
# 需要导入模块: from keras.applications import xception [as 别名]
# 或者: from keras.applications.xception import preprocess_input [as 别名]
def generate_from_paths_and_labels(
input_paths, labels, batch_size, input_size=(299, 299)):
num_samples = len(input_paths)
while 1:
perm = np.random.permutation(num_samples)
input_paths = input_paths[perm]
labels = labels[perm]
for i in range(0, num_samples, batch_size):
inputs = list(map(
lambda x: image.load_img(x, target_size=input_size),
input_paths[i:i+batch_size]
))
inputs = np.array(list(map(
lambda x: image.img_to_array(x),
inputs
)))
inputs = preprocess_input(inputs)
yield (inputs, labels[i:i+batch_size])
示例3: test_spimage_converter_module
# 需要导入模块: from keras.applications import xception [as 别名]
# 或者: from keras.applications.xception import preprocess_input [as 别名]
def test_spimage_converter_module(self):
""" spimage converter module must preserve original image """
img_fpaths = glob(os.path.join(_getSampleJPEGDir(), '*.jpg'))
def exec_gfn_spimg_decode(spimg_dict, img_dtype):
gfn = gfac.buildSpImageConverter('BGR', img_dtype)
with IsolatedSession() as issn:
feeds, fetches = issn.importGraphFunction(gfn, prefix="")
feed_dict = dict(
(tnsr, spimg_dict[tfx.op_name(tnsr, issn.graph)]) for tnsr in feeds)
img_out = issn.run(fetches[0], feed_dict=feed_dict)
return img_out
def check_image_round_trip(img_arr):
spimg_dict = imageArrayToStruct(img_arr).asDict()
spimg_dict['data'] = bytes(spimg_dict['data'])
img_arr_out = exec_gfn_spimg_decode(
spimg_dict, imageTypeByOrdinal(spimg_dict['mode']).dtype)
self.assertTrue(np.all(img_arr_out == img_arr))
for fp in img_fpaths:
img = load_img(fp)
img_arr_byte = img_to_array(img).astype(np.uint8)
check_image_round_trip(img_arr_byte)
img_arr_float = img_to_array(img).astype(np.float32)
check_image_round_trip(img_arr_float)
img_arr_preproc = iv3.preprocess_input(img_to_array(img))
check_image_round_trip(img_arr_preproc)
示例4: test_bare_keras_module
# 需要导入模块: from keras.applications import xception [as 别名]
# 或者: from keras.applications.xception import preprocess_input [as 别名]
def test_bare_keras_module(self):
""" Keras GraphFunctions should give the same result as standard Keras models """
img_fpaths = glob(os.path.join(_getSampleJPEGDir(), '*.jpg'))
for model_gen, preproc_fn, target_size in [(InceptionV3, iv3.preprocess_input, model_sizes['InceptionV3']),
(Xception, xcpt.preprocess_input, model_sizes['Xception']),
(ResNet50, rsnt.preprocess_input, model_sizes['ResNet50'])]:
keras_model = model_gen(weights="imagenet")
_preproc_img_list = []
for fpath in img_fpaths:
img = load_img(fpath, target_size=target_size)
# WARNING: must apply expand dimensions first, or ResNet50 preprocessor fails
img_arr = np.expand_dims(img_to_array(img), axis=0)
_preproc_img_list.append(preproc_fn(img_arr))
imgs_input = np.vstack(_preproc_img_list)
preds_ref = keras_model.predict(imgs_input)
gfn_bare_keras = GraphFunction.fromKeras(keras_model)
with IsolatedSession(using_keras=True) as issn:
K.set_learning_phase(0)
feeds, fetches = issn.importGraphFunction(gfn_bare_keras)
preds_tgt = issn.run(fetches[0], {feeds[0]: imgs_input})
np.testing.assert_array_almost_equal(preds_tgt,
preds_ref,
decimal=self.featurizerCompareDigitsExact)
示例5: test_pipeline
# 需要导入模块: from keras.applications import xception [as 别名]
# 或者: from keras.applications.xception import preprocess_input [as 别名]
def test_pipeline(self):
""" Pipeline should provide correct function composition """
img_fpaths = glob(os.path.join(_getSampleJPEGDir(), '*.jpg'))
xcpt_model = Xception(weights="imagenet")
stages = [('spimage', gfac.buildSpImageConverter('BGR', 'float32')),
('xception', GraphFunction.fromKeras(xcpt_model))]
piped_model = GraphFunction.fromList(stages)
for fpath in img_fpaths:
target_size = model_sizes['Xception']
img = load_img(fpath, target_size=target_size)
img_arr = np.expand_dims(img_to_array(img), axis=0)
img_input = xcpt.preprocess_input(img_arr)
preds_ref = xcpt_model.predict(img_input)
spimg_input_dict = imageArrayToStruct(img_input).asDict()
spimg_input_dict['data'] = bytes(spimg_input_dict['data'])
with IsolatedSession() as issn:
# Need blank import scope name so that spimg fields match the input names
feeds, fetches = issn.importGraphFunction(piped_model, prefix="")
feed_dict = dict(
(tnsr, spimg_input_dict[tfx.op_name(tnsr, issn.graph)]) for tnsr in feeds)
preds_tgt = issn.run(fetches[0], feed_dict=feed_dict)
# Uncomment the line below to see the graph
# tfx.write_visualization_html(issn.graph,
# NamedTemporaryFile(prefix="gdef", suffix=".html").name)
np.testing.assert_array_almost_equal(preds_tgt,
preds_ref,
decimal=self.featurizerCompareDigitsExact)
示例6: preprocess
# 需要导入模块: from keras.applications import xception [as 别名]
# 或者: from keras.applications.xception import preprocess_input [as 别名]
def preprocess(self, inputImage):
# Keras expects RGB order
return inception_v3.preprocess_input(_reverseChannels(inputImage))
示例7: extract_VGG16
# 需要导入模块: from keras.applications import xception [as 别名]
# 或者: from keras.applications.xception import preprocess_input [as 别名]
def extract_VGG16(tensor):
from keras.applications.vgg16 import VGG16, preprocess_input
return VGG16(weights='imagenet', include_top=False).predict(preprocess_input(tensor))
示例8: extract_VGG19
# 需要导入模块: from keras.applications import xception [as 别名]
# 或者: from keras.applications.xception import preprocess_input [as 别名]
def extract_VGG19(tensor):
from keras.applications.vgg19 import VGG19, preprocess_input
return VGG19(weights='imagenet', include_top=False).predict(preprocess_input(tensor))
示例9: extract_Resnet50
# 需要导入模块: from keras.applications import xception [as 别名]
# 或者: from keras.applications.xception import preprocess_input [as 别名]
def extract_Resnet50(tensor):
from keras.applications.resnet50 import ResNet50, preprocess_input
return ResNet50(weights='imagenet', include_top=False).predict(preprocess_input(tensor))
示例10: extract_Xception
# 需要导入模块: from keras.applications import xception [as 别名]
# 或者: from keras.applications.xception import preprocess_input [as 别名]
def extract_Xception(tensor):
from keras.applications.xception import Xception, preprocess_input
return Xception(weights='imagenet', include_top=False).predict(preprocess_input(tensor))
示例11: extract_InceptionV3
# 需要导入模块: from keras.applications import xception [as 别名]
# 或者: from keras.applications.xception import preprocess_input [as 别名]
def extract_InceptionV3(tensor):
from keras.applications.inception_v3 import InceptionV3, preprocess_input
return InceptionV3(weights='imagenet', include_top=False).predict(preprocess_input(tensor))
示例12: val_pre_model
# 需要导入模块: from keras.applications import xception [as 别名]
# 或者: from keras.applications.xception import preprocess_input [as 别名]
def val_pre_model(source_path, folder, img_dim, architechture):
array_path = os.path.join(source_path, folder)
pre_model_path = os.path.join(source_path, 'pre_model')
shutil.rmtree(pre_model_path,ignore_errors=True)
os.makedirs(pre_model_path)
if architechture == 'resnet50':
popped, pre_model = get_resnet_pre_model(img_dim)
elif architechture == 'xception':
popped, pre_model = get_xception_pre_model(img_dim)
else:
popped, pre_model = get_inception_v3_pre_model(img_dim)
for (array, label, array_name, label_name) in tqdm(gen_array_from_dir(array_path)):
if architechture == 'resnet50':
array = resnet_preprocess_input(array[np.newaxis].astype(np.float32))
elif architechture == 'xception':
array = xception_preprocess_input(array[np.newaxis].astype(np.float32))
else:
array = inception_v3_preprocess_input(array[np.newaxis].astype(np.float32))
array_pre_model = np.squeeze(pre_model.predict(array, batch_size=1))
array_name = array_name.split('.')[0]
label_name = label_name.split('.')[0]
img_pre_model_path = os.path.join(pre_model_path, array_name)
label_pre_model_path = os.path.join(pre_model_path, label_name)
np.save(img_pre_model_path, array_pre_model)
np.save(label_pre_model_path, label)
示例13: multi_predict
# 需要导入模块: from keras.applications import xception [as 别名]
# 或者: from keras.applications.xception import preprocess_input [as 别名]
def multi_predict(aug_gen, models, architecture):
predicted = []
for img, _ in aug_gen:
if architecture == 'resnet50':
img = resnet_preprocess_input(img[np.newaxis].astype(np.float32))
elif architecture == 'xception':
img = xception_preprocess_input(img[np.newaxis].astype(np.float32))
else:
img = inception_v3_preprocess_input(img[np.newaxis].astype(np.float32))
for model in models:
predicted.append(model.predict(img))
predicted = np.array(predicted).sum(axis=0)
pred_list = list(predicted[0])
return predicted, pred_list
示例14: gen_minibatches
# 需要导入模块: from keras.applications import xception [as 别名]
# 或者: from keras.applications.xception import preprocess_input [as 别名]
def gen_minibatches(array_dir, array_names, batch_size, architecture, final = False):
array_names = list(array_names)
while True:
# in place shuffle
np.random.shuffle(array_names)
array_names_mb = array_names[:batch_size]
arrays = []
labels = []
for array_name in array_names_mb:
img_path = os.path.join(array_dir, array_name)
array = np.load(img_path)
if final:
if architecture == 'resnet50':
array = np.squeeze(resnet_preprocess_input(array[np.newaxis].astype(np.float32)))
elif architecture == 'xception':
array = np.squeeze(xception_preprocess_input(array[np.newaxis].astype(np.float32)))
else:
array = np.squeeze(inception_v3_preprocess_input(array[np.newaxis].astype(np.float32)))
arrays.append(array)
labels.append(np.load(img_path.replace('-img-','-label-')))
yield np.array(arrays), np.array(labels)