大多數(shù)模型都是黑盒,其內(nèi)部邏輯對(duì)最終用戶是隱藏的。為了鼓勵(lì)透明度,我們通過(guò)簡(jiǎn)單地將Interface類中的interpretation關(guān)鍵字設(shè)置為default,使得向模型添加解釋變得非常容易。這允許您的用戶了解輸入的哪些部分負(fù)責(zé)輸出。
1、Interpret解釋?
我們來(lái)一個(gè)圖片的分類器,帶一個(gè)Interpret解釋,這里將會(huì)下載人類可讀的ImageNet標(biāo)簽,是在站點(diǎn)https://git.io/JJkYN上面返回的標(biāo)簽,所以需要用到科學(xué)上網(wǎng)。
import requests
import tensorflow as tf
import gradio as gr
inception_net = tf.keras.applications.MobileNetV2() # 加載模型
# 下載人類可讀的ImageNet標(biāo)簽
response = requests.get("https://git.io/JJkYN")
labels = response.text.split("\n")
def classify_image(inp):
inp = inp.reshape((-1, 224, 224, 3))
inp = tf.keras.applications.mobilenet_v2.preprocess_input(inp)
prediction = inception_net.predict(inp).flatten()
return {labels[i]: float(prediction[i]) for i in range(1000)}
image = gr.Image(shape=(224, 224))
label = gr.Label(num_top_classes=3)
#demo = gr.Interface(fn=classify_image, inputs=image, outputs=label, interpretation="default")
demo = gr.Interface(fn=classify_image, inputs=image, outputs=label, interpretation="shap", num_shap=5)
demo.launch()
如下圖,輸入一張貓(猞猁)的圖片,然后右邊輸出3個(gè)概率從大到小排序的分類標(biāo)簽:
然后點(diǎn)擊Interpret,我們來(lái)看下效果,對(duì)重要部分進(jìn)行了遮罩突出顯示,也就是輸出重要性判別的輸入的地方做個(gè)解釋。
2、高亮顯示
適用于任何函數(shù),即使在內(nèi)部,模型是一個(gè)復(fù)雜的神經(jīng)網(wǎng)絡(luò)或其他黑盒子。如果使用Gradio的默認(rèn)解釋或形狀解釋,則輸出組件必須是Label。支持所有常用輸入組件。
下面是一個(gè)文本輸入的示例:
import gradio as gr
male_words, female_words = ["he", "his", "him"], ["she", "hers", "her"]
def gender_of_sentence(sentence):
male_count = len([word for word in sentence.split() if word.lower() in male_words])
female_count = len(
[word for word in sentence.split() if word.lower() in female_words]
)
total = max(male_count + female_count, 1)
return {"male": male_count / total, "female": female_count / total}
demo = gr.Interface(
fn=gender_of_sentence,
inputs=gr.Textbox(value="She went to his house to get her keys."),
outputs="label",
interpretation="default",
)
demo.launch()
將顯示男女比例,然后我們點(diǎn)擊Interpret,將會(huì)看到界面會(huì)自動(dòng)突出顯示文本(或圖像等)中重要部分。顏色的強(qiáng)度與輸入部分的重要性相對(duì)應(yīng)。降低類置信度的部分用藍(lán)色突出顯示。
?
3、常見(jiàn)錯(cuò)誤處理?
3.1、安裝tensorflow
我們安裝任何包,個(gè)人依然推薦加豆瓣鏡像
pip install -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com tensorflow
這里有個(gè)比較奇怪的問(wèn)題,最開(kāi)始我是這么安裝,也是一直以來(lái)的常見(jiàn)安裝方法:
pip install tensorflow -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
看到區(qū)別了嗎,就是將tensorflow放在了install后面,這樣的情況會(huì)出現(xiàn)下面這樣的錯(cuò)誤:
WARNING: Ignoring invalid distribution -sonschema (d:\anaconda3\envs\pygpu\lib\site-packages)
WARNING: Ignoring invalid distribution -sonschema (d:\anaconda3\envs\pygpu\lib\site-packages)
Collecting http://pypi.douban.com/simple/
? Downloading http://pypi.douban.com/simple/ (24.7 MB)
? ? ?---------------------------------------- 24.7/24.7 MB 11.9 MB/s eta 0:00:00
? ERROR: Cannot unpack file C:\Users\Tony\AppData\Local\Temp\pip-unpack-2d7n8372\simple.html (downloaded from C:\Users\Tony\AppData\Local\Temp\pip-req-build-5xqb8otu, content-type: text/html); cannot detect archive format
ERROR: Cannot determine archive format of C:\Users\Tony\AppData\Local\Temp\pip-req-build-5xqb8otu
WARNING: Ignoring invalid distribution -sonschema (d:\anaconda3\envs\pygpu\lib\site-packages)
WARNING: Ignoring invalid distribution -sonschema (d:\anaconda3\envs\pygpu\lib\site-packages)
WARNING: Ignoring invalid distribution -sonschema (d:\anaconda3\envs\pygpu\lib\site-packages)
翻譯過(guò)來(lái)的意思就是:無(wú)法解包文件,無(wú)法檢測(cè)存檔格式,無(wú)法確定歸檔格式。
在以前安裝tensorflow是在一個(gè)新的虛擬環(huán)境,沒(méi)有問(wèn)題,這個(gè)是在有MXNet的里面安裝的,出現(xiàn)上述錯(cuò)誤,然后試著將tensorflow放到最后面,沒(méi)有想到竟然成功安裝。
3.2、安裝scikit-image
其中點(diǎn)擊Interpret,需要安裝skimage
ModuleNotFoundError: No module named 'skimage'
同樣的方法安裝即可,只不過(guò)這里需要注意名稱?
pip install -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com scikit-image?
3.3、安裝shap
指定一些參數(shù),interpretation設(shè)置為shap,可以修改num_shap參數(shù),該參數(shù)控制精度和運(yùn)行時(shí)之間的權(quán)衡(增加該值通常會(huì)提高精度)。
demo = gr.Interface(fn=classify_image, inputs=image, outputs=label, interpretation="shap", num_shap=5)
在指定interpretation="shap"參數(shù)的時(shí)候,我們?nèi)绻麤](méi)有安裝shape,也將報(bào)shap不存在的錯(cuò)誤。
ModuleNotFoundError: No module named 'shap'?
pip install -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com shap
4、并行與串行
4.1、并行Parallel
Gradio可以讓你很容易地使用Gradio來(lái)混合界面。
Parallel允許你將兩個(gè)相似的模型(如果它們具有相同的輸入類型)并行放置以比較模型預(yù)測(cè)?
generator1 = gr.load("huggingface/gpt2")
generator2 = gr.load("huggingface/EleutherAI/gpt-neo-2.7B")
generator3 = gr.load("huggingface/EleutherAI/gpt-j-6B")
gr.Parallel(generator1, generator2, generator3).launch()
這樣就可以比較幾個(gè)模型的輸出效果。
4.2、串行Series
我們也可以使用Series將兩個(gè)模型串聯(lián)起來(lái),比如將第一個(gè)模型的輸出作為第二個(gè)模型的輸入,下面就是通過(guò)gpt2得到輸出的信息,然后這些輸出信息作為輸入,進(jìn)入t5-small模型,處理成德語(yǔ)進(jìn)行最終的輸出。?
generator = gr.load("huggingface/gpt2")
translator = gr.load("huggingface/t5-small")
gr.Series(generator, translator).launch()
?如圖,輸出的英文再翻譯成德語(yǔ):
有興趣的可以查閱其余章節(jié):文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-482480.html
Gradio的web界面演示與交互機(jī)器學(xué)習(xí)模型,安裝和使用《1》
Gradio的web界面演示與交互機(jī)器學(xué)習(xí)模型,主要特征《2》
Gradio的web界面演示與交互機(jī)器學(xué)習(xí)模型,分享應(yīng)用《3》
Gradio的web界面演示與交互機(jī)器學(xué)習(xí)模型,全局狀態(tài)與會(huì)話狀態(tài)《4》
Gradio的web界面演示與交互機(jī)器學(xué)習(xí)模型,接口自動(dòng)刷新或連續(xù)刷新數(shù)據(jù)流《5》文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-482480.html
到了這里,關(guān)于Gradio的web界面演示與交互機(jī)器學(xué)習(xí)模型,高級(jí)接口特征《6》的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!