本文整理匯總了Python中cntk.softmax方法的典型用法代碼示例。如果您正苦於以下問題:Python cntk.softmax方法的具體用法?Python cntk.softmax怎麽用?Python cntk.softmax使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類cntk
的用法示例。
在下文中一共展示了cntk.softmax方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: eval_single_image
# 需要導入模塊: import cntk [as 別名]
# 或者: from cntk import softmax [as 別名]
def eval_single_image(loaded_model, image_path, image_dims):
# Load and format image (resize, RGB -> BGR, CHW -> HWC)
try:
img = Image.open(image_path)
if image_path.endswith("png"):
temp = Image.new("RGB", img.size, (255, 255, 255))
temp.paste(img, img)
img = temp
resized = img.resize((image_dims[2], image_dims[1]), Image.ANTIALIAS)
bgr_image = np.asarray(resized, dtype=np.float32)[..., [2, 1, 0]]
hwc_format = np.ascontiguousarray(np.rollaxis(bgr_image, 2))
# Compute model output
arguments = {loaded_model.arguments[0]: [hwc_format]}
output = loaded_model.eval(arguments)
# Return softmax probabilities
sm = cntk.softmax(output[0])
return sm.eval()
except FileNotFoundError:
log.error("Could not open (skipping file): ", image_path)
return ["None"]
示例2: _softmax
# 需要導入模塊: import cntk [as 別名]
# 或者: from cntk import softmax [as 別名]
def _softmax(x, dim):
"""Computes softmax along a specified dim. Keras currently lacks this feature.
"""
if K.backend() == 'tensorflow':
import tensorflow as tf
return tf.nn.softmax(x, dim)
elif K.backend() is 'cntk':
import cntk
return cntk.softmax(x, dim)
elif K.backend() == 'theano':
# Theano cannot softmax along an arbitrary dim.
# So, we will shuffle `dim` to -1 and un-shuffle after softmax.
perm = np.arange(K.ndim(x))
perm[dim], perm[-1] = perm[-1], perm[dim]
x_perm = K.permute_dimensions(x, perm)
output = K.softmax(x_perm)
# Permute back
perm[dim], perm[-1] = perm[-1], perm[dim]
output = K.permute_dimensions(x, output)
return output
else:
raise ValueError("Backend '{}' not supported".format(K.backend()))
示例3: test_softmax
# 需要導入模塊: import cntk [as 別名]
# 或者: from cntk import softmax [as 別名]
def test_softmax():
assert_cntk_ngraph_isclose(C.softmax([[1, 1, 2, 3]]))
assert_cntk_ngraph_isclose(C.softmax([1, 1]))
assert_cntk_ngraph_isclose(C.softmax([[[1, 1], [3, 5]]], axis=-1))
# This test is failing, bug must be fixed:
# assert_cntk_ngraph_isclose(C.softmax([[[1, 1], [3, 5]]], axis=1))
示例4: softmax
# 需要導入模塊: import cntk [as 別名]
# 或者: from cntk import softmax [as 別名]
def softmax(x, axis=-1):
return C.softmax(x, axis=axis)
示例5: softmax
# 需要導入模塊: import cntk [as 別名]
# 或者: from cntk import softmax [as 別名]
def softmax(x):
return C.softmax(x)
示例6: eval_single_image
# 需要導入模塊: import cntk [as 別名]
# 或者: from cntk import softmax [as 別名]
def eval_single_image(loaded_model, image_path, image_dims):
# Load and format image (resize, RGB -> BGR, CHW -> HWC)
try:
img = Image.open(image_path)
if image_path.endswith("png"):
temp = Image.new("RGB", img.size, (255, 255, 255))
temp.paste(img, img)
img = temp
resized = img.resize((image_dims[2], image_dims[1]), Image.ANTIALIAS)
bgr_image = np.asarray(resized, dtype=np.float32)[..., [2, 1, 0]]
hwc_format = np.ascontiguousarray(np.rollaxis(bgr_image, 2))
# compute model output
arguments = {loaded_model.arguments[0]: [hwc_format]}
output = loaded_model.eval(arguments)
# return softmax probabilities
sm = cntk.softmax(output[0])
return sm.eval()
except FileNotFoundError:
print("Could not open (skipping file): ", image_path)
return ["None"]
# Evaluates an image set using the provided model
示例7: eval_single_image_imagenet
# 需要導入模塊: import cntk [as 別名]
# 或者: from cntk import softmax [as 別名]
def eval_single_image_imagenet(opt_model, loaded_model, image_path, image_dims):
img = Image.open(image_path)
if image_path.endswith("png"):
temp = Image.new("RGB", img.size, (255, 255, 255))
temp.paste(img, img)
img = temp
resized = img.resize((image_dims[2], image_dims[1]), Image.ANTIALIAS)
bgr_image = np.asarray(resized, dtype=np.float32)[..., [2, 1, 0]]
hwc_format = np.ascontiguousarray(np.rollaxis(bgr_image, 2))
if "VGG" in opt_model:
arguments = {loaded_model.arguments[0]: [hwc_format]}
output = loaded_model.eval(arguments)
sm = cntk.softmax(output[0])
return sm.eval()
elif "InceptionV3" in opt_model:
z = cntk.as_composite(loaded_model[0].owner)
output = z.eval({z.arguments[0]: [hwc_format]})
else:
z = cntk.as_composite(loaded_model[3].owner)
output = z.eval({z.arguments[0]: [hwc_format]})
predictions = np.squeeze(output)
return predictions