本文整理汇总了Python中keras.applications.resnet50.preprocess_input方法的典型用法代码示例。如果您正苦于以下问题:Python resnet50.preprocess_input方法的具体用法?Python resnet50.preprocess_input怎么用?Python resnet50.preprocess_input使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.applications.resnet50
的用法示例。
在下文中一共展示了resnet50.preprocess_input方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 import preprocess_input [as 别名]
def main(self):
self.logger.info('Will load keras model')
model = ResNet50(weights='imagenet')
self.logger.info('Keras model loaded')
feature_list = []
img_path_list = []
for raw_file in self.inp.raw_files:
media_path = raw_file.path
file_list = os.listdir(media_path)
total = float(len(file_list))
for index, img_file in enumerate(file_list):
img_path = os.path.join(media_path, img_file)
img_path_list.append(img_path)
img = image.load_img(img_path, target_size=(224, 224))
x = keras_image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
# extract features
scores = model.predict(x)
sim_class = np.argmax(scores)
print('Scores {}\nSimClass: {}'.format(scores, sim_class))
self.outp.request_annos(img_path, img_sim_class=sim_class)
self.logger.info('Requested annotation for: {} (cluster: {})'.format(img_path, sim_class))
self.update_progress(index*100/total)
示例2: load_images_for_keras
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 import preprocess_input [as 别名]
def load_images_for_keras(self, img_path, target_size=(224, 224)):
features = []
filenames = sorted(os.listdir(img_path))
for filename in filenames:
img = image.load_img(os.path.join(img_path, filename), target_size=target_size)
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
img = preprocess_input(img)
feature = self.model.predict(img)
if img is not None:
features.append(feature)
return features
示例3: gen
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 import preprocess_input [as 别名]
def gen(session, data, labels, batch_size):
def _f():
start = 0
end = start + batch_size
n = data.shape[0]
while True:
X_batch = session.run(resize_op, {img_placeholder: data[start:end]})
X_batch = preprocess_input(X_batch)
y_batch = labels[start:end]
start += batch_size
end += batch_size
if start >= n:
start = 0
end = batch_size
print(start, end)
yield (X_batch, y_batch)
return _f
示例4: pred_data
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 import preprocess_input [as 别名]
def pred_data():
with open('./models/cat_dog.yaml') as yamlfile:
loaded_model_yaml = yamlfile.read()
model = model_from_yaml(loaded_model_yaml)
model.load_weights('./models/cat_dog.h5')
sgd = Adam(lr=0.0003)
model.compile(loss='categorical_crossentropy',optimizer=sgd, metrics=['accuracy'])
images = []
path='./data/test/'
for f in os.listdir(path):
img = image.load_img(path + f, target_size=image_size)
img_array = image.img_to_array(img)
x = np.expand_dims(img_array, axis=0)
x = preprocess_input(x)
result = model.predict_classes(x,verbose=0)
print(f,result[0])
示例5: predict
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 import preprocess_input [as 别名]
def predict(model, img, target_size, top_n=3):
"""Run model prediction on image
Args:
model: keras model
img: PIL format image
target_size: (w,h) tuple
top_n: # of top predictions to return
Returns:
list of predicted labels and their probabilities
"""
if img.size != target_size:
img = img.resize(target_size)
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
with graph.as_default():
preds = model.predict(x)
return decode_predictions(preds, top=top_n)[0]
示例6: pix2depth
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 import preprocess_input [as 别名]
def pix2depth(path, model):
model_name = 'p2d'
originalImage = cv2.imread(path)
loaded_model = model_list['pix2depth'][model]
file_name = model+'_'+path.split('/')[-1]
output_file = os.path.join(output_path,file_name)
if model =='CNN':
originalImage = cv2.resize(originalImage,(img_dim,img_dim))
x = preprocess_input(originalImage/1.)
elif model == 'CycleGAN':
test(path)
os.system('cp gautam/inf_results/imgs/fakeA_0_0.jpg %s' % output_file)
else:
originalImage = cv2.resize(originalImage,(256,256))
x = originalImage/255.
if not model == 'CycleGAN':
p1 = get_depth_map(x, loaded_model)
cv2.imwrite(output_file,p1)
return output_file
示例7: depth2pix
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 import preprocess_input [as 别名]
def depth2pix(path,model):
model_name = 'd2p'
originalImage = cv2.imread(path)
loaded_model = model_list['depth2pix'][model]
file_name = model+'_'+path.split('/')[-1]
output_file = os.path.join(output_path,file_name)
if model =='CNN':
img_dim = 256
originalImage = cv2.resize(originalImage,(img_dim,img_dim))
x = preprocess_input(originalImage/1.)
elif model == 'CycleGAN':
test_dep(path)
os.system('cp gautam/inf_results/imgs/fakeB_0_0.jpg %s' % output_file)
else:
originalImage = cv2.resize(originalImage,(256,256))
x = originalImage/255.
if not model == 'CycleGAN':
p1 = get_depth_map(x, loaded_model)
cv2.imwrite(output_file,p1)
return output_file
示例8: _extract
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 import preprocess_input [as 别名]
def _extract(fp, model):
# Load the image, setting the size to 224 x 224
img = image.load_img(fp, target_size=(224, 224))
# Convert the image to a numpy array, resize it (1, 2, 244, 244), and preprocess it
img_data = image.img_to_array(img)
img_data = np.expand_dims(img_data, axis=0)
img_data = preprocess_input(img_data)
# Extract the features
np_features = model.predict(img_data)[0]
# Convert from Numpy to a list of values
return np.char.mod('%f', np_features)
示例9: test_imagenet_preprocess_input
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 import preprocess_input [as 别名]
def test_imagenet_preprocess_input(self):
# compare our tf implementation to the np implementation in keras
image = np.zeros((256, 256, 3))
sess = tf.Session()
with sess.as_default():
x = tf.placeholder(tf.float32, shape=[256, 256, 3])
processed = keras_apps._imagenet_preprocess_input(x, (256, 256)),
sparkdl_preprocessed_input = sess.run(processed, {x: image})
keras_preprocessed_input = resnet50.preprocess_input(np.expand_dims(image, axis=0))
# NOTE: precision errors occur for decimal > 5
np.testing.assert_array_almost_equal(sparkdl_preprocessed_input, keras_preprocessed_input,
decimal=5)
示例10: test_spimage_converter_module
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 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)
示例11: test_bare_keras_module
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 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)
示例12: test_pipeline
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 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)
示例13: computeFeatures
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 import preprocess_input [as 别名]
def computeFeatures(self, video):
x = vgg16.preprocess_input(video)
features = self.model.predict(x)
return features
示例14: __fetch_nn_feature
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 import preprocess_input [as 别名]
def __fetch_nn_feature(batch_image, batch_file_name):
batch_image = np.concatenate(batch_image, axis=0)
x = preprocess_input(batch_image)
features = res50_model.predict(x)
features_reduce = features.squeeze()
for idx in range(len(batch_file_name)):
image_nn_feature_dict[batch_file_name[idx]] = features_reduce[idx]
# print(features_reduce)
print(features_reduce.shape)
示例15: main
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 import preprocess_input [as 别名]
def main(self):
self.logger.info('Will load keras model')
model = ResNet50(weights='imagenet')
self.logger.info('Keras model loaded')
feature_list = []
img_path_list = []
# Request only MIA annotations for annotations of first stage
# that have been annotated in current iteration cycle.
img_annos = list(filter(lambda x: x.iteration == self.iteration,
self.inp.img_annos))
total = len(img_annos)
for index, img_anno in enumerate(img_annos):
annos = img_anno.to_vec('anno.data')
if annos:
types = img_anno.to_vec('anno.dtype')
img = skimage.io.imread(self.get_abs_path(img_anno.img_path))
crops, anno_boxes = anno_helper.crop_boxes(annos, types,
img, context=0.01)
sim_classes = []
for crop in crops:
# img = image.load_img(img_path, target_size=(224, 224))
crop_img = image.img_to_array(image.array_to_img(crop, scale=False).resize((224,224)))
x = keras_image.img_to_array(crop_img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
# extract features
scores = model.predict(x)
sim_classes.append(np.argmax(scores))
self.outp.request_annos(img_anno.img_path,
annos=annos, anno_types=types, anno_sim_classes=sim_classes)
self.logger.info('Requested annotation for: {}\n{}\n{}'.format(img_anno.img_path, types, sim_classes))
self.update_progress(index*100/total)