对于使用已经训练好的模型,比如VGG,RESNET等,keras都自带了一个keras.applications.imagenet_utils.decode_predictions的方法,有很多限制:

def decode_predictions(preds, top=5):
 """Decodes the prediction of an ImageNet model.

 # Arguments
 preds: Numpy tensor encoding a batch of predictions.
 top: Integer, how many top-guesses to return.

 # Returns
 A list of lists of top class prediction tuples
 `(class_name, class_description, score)`.
 One list of tuples per sample in batch input.

 # Raises
 ValueError: In case of invalid shape of the `pred` array
  (must be 2D).
 """
 global CLASS_INDEX
 if len(preds.shape) != 2 or preds.shape[1] != 1000:
 raise ValueError('`decode_predictions` expects '
    'a batch of predictions '
    '(i.e. a 2D array of shape (samples, 1000)). '
    'Found array with shape: ' + str(preds.shape))
 if CLASS_INDEX is None:
 fpath = get_file('imagenet_class_index.json',
    CLASS_INDEX_PATH,
    cache_subdir='models',
    file_hash='c2c37ea517e94d9795004a39431a14cb')
 with open(fpath) as f:
  CLASS_INDEX = json.load(f)
 results = []
 for pred in preds:
 top_indices = pred.argsort()[-top:][::-1]
 result = [tuple(CLASS_INDEX[str(i)]) + (pred[i],) for i in top_indices]
 result.sort(key=lambda x: x[2], reverse=True)
 results.append(result)
 return results

把重要的东西挖出来,然后自己敲,这样就OK了,下例以MNIST数据集为例:

import keras
from keras.models import Sequential
from keras.layers import Dense
import numpy as np
import tflearn
import tflearn.datasets.mnist as mnist

def decode_predictions_custom(preds, top=5):
 CLASS_CUSTOM = ["0","1","2","3","4","5","6","7","8","9"]
 results = []
 for pred in preds:
 top_indices = pred.argsort()[-top:][::-1]
 result = [tuple(CLASS_CUSTOM[i]) + (pred[i]*100,) for i in top_indices]
 results.append(result)
 return results

x_train, y_train, x_test, y_test = mnist.load_data(one_hot=True)

model = Sequential()
model.add(Dense(units=64, activation='relu', input_dim=784))
model.add(Dense(units=10, activation='softmax'))
model.compile(loss='categorical_crossentropy',
  optimizer='sgd',
  metrics=['accuracy'])
model.fit(x_train, y_train, epochs=10, batch_size=128)
# score = model.evaluate(x_test, y_test, batch_size=128)
# print(score)
preds = model.predict(x_test[0:1,:])
p = decode_predictions_custom(preds)
for (i,(label,prob)) in enumerate(p[0]):
 print("{}. {}: {:.2f}%".format(i+1, label,prob)) 
# 1. 7: 99.43%
# 2. 9: 0.24%
# 3. 3: 0.23%
# 4. 0: 0.05%
# 5. 2: 0.03%

补充知识:keras简单的去噪自编码器代码和各种类型自编码器代码

我就废话不多说了,大家还是直接看代码吧~

start = time()
 
from keras.models import Sequential
from keras.layers import Dense, Dropout,Input
from keras.layers import Embedding
from keras.layers import Conv1D, GlobalAveragePooling1D, MaxPooling1D
from keras import layers
from keras.models import Model
 
# Parameters for denoising autoencoder
nb_visible = 120
nb_hidden = 64
batch_size = 16
# Build autoencoder model
input_img = Input(shape=(nb_visible,))
 
encoded = Dense(nb_hidden, activation='relu')(input_img)
decoded = Dense(nb_visible, activation='sigmoid')(encoded)
 
autoencoder = Model(input=input_img, output=decoded)
autoencoder.compile(loss='mean_squared_error',optimizer='adam',metrics=['mae'])
autoencoder.summary()
 
# Train
### 加一个early_stooping
import keras 
 
early_stopping = keras.callbacks.EarlyStopping(
  monitor='val_loss',
  min_delta=0.0001,
  patience=5, 
  verbose=0, 
  mode='auto'
)
autoencoder.fit(X_train_np, y_train_np, nb_epoch=50, batch_size=batch_size , shuffle=True,
        callbacks = [early_stopping],verbose = 1,validation_data=(X_test_np, y_test_np))
# Evaluate
evaluation = autoencoder.evaluate(X_test_np, y_test_np, batch_size=batch_size , verbose=1)
print('val_loss: %.6f, val_mean_absolute_error: %.6f' % (evaluation[0], evaluation[1]))
 
end = time()
print('耗时:'+str((end-start)/60))

keras各种自编码代码

以上这篇keras topN显示,自编写代码案例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。