前往
大廳
主題

如何將自己的深度學習使用在安卓手機上

Polaris | 2020-10-26 21:37:59 | 巴幣 0 | 人氣 117

裡面的檔案都已經幫你將所需的檔案處理完畢,也有一個完整的實作方法,但如果要將自己的模型從0到轉換成.pb檔時,這時候必須先將自己的Tensorflow降至指定版本,目前最新只支援到1.13.1版本(Android studio中的build.gradle檔案也必須修改成相符合的版本名稱),使用指定版本做出模型之後必須轉換成凍結圖
tf.train.write_graph(frozen_graph, "some_directory", "my_model5.pb", as_text=False)

如上所示,然後要注意的點是,在製作模型完成後必須找出輸出跟輸入層的"正確名稱"以讓手機可以讀入你的照片來辨識並,
輸出辨識的結果
def freeze_session(session, keep_var_names=None, output_names=None, clear_devices=True):
    """
    Freezes the state of a session into a pruned computation graph.

    Creates a new computation graph where variable nodes are replaced by
    constants taking their current value in the session. The new graph will be
    pruned so subgraphs that are not necessary to compute the requested
    outputs are removed.
    @param session The TensorFlow session to be frozen.
    @param keep_var_names A list of variable names that should not be frozen,
                          or None to freeze all the variables in the graph.
    @param output_names Names of the relevant graph outputs.
    @param clear_devices Remove the device directives from the graph for better portability.
    @return The frozen graph definition.
    """
    graph = session.graph
    with graph.as_default():
        freeze_var_names = list(set(v.op.name for v in tf.global_variables()).difference(keep_var_names or []))
        output_names = output_names or []
        print(output_names)
        output_names += [v.op.name for v in tf.global_variables()]
        input_graph_def = graph.as_graph_def()
        if clear_devices:
            for node in input_graph_def.node:
                node.device = ""
        frozen_graph = tf.graph_util.convert_variables_to_constants(
            session, input_graph_def, output_names, freeze_var_names)
        return frozen_graph
如不知道相關輸入層輸出層的正確名稱可以使用以上代碼,丟入檔案即可輸出所有層的相關資料,
然後再 Android studio中將設定更改成你的模型的輸出入層的名稱,
然後必須準備一個txt檔案來寫入所有的類別的標籤(label),你有11類就寫11行,讓手機能順利抓到他所辨識的項目名稱,
最後將.pb檔案跟.txt檔案都丟入\app\src\main\assets的資料夾內就可以了,相關的如辨識所需輸入等細節就因人而異,
不過還有一點新版的 Android studio會自動更新gradle的樣子,不過好像不會衝突只是資料夾內會有兩個gradle檔案夾,

更多原始資料可閱讀

創作回應

更多創作