{"cells":[{"metadata":{"trusted":true,"_uuid":"8c7847a411e5fdd1af68e812b57dc469a7366cd8"},"cell_type":"code","source":"import  datetime\ndate_depart=datetime.datetime.now()\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)","execution_count":null,"outputs":[]},{"metadata":{"id":"Dmh0PLreaS1f","colab_type":"code","outputId":"680d26fa-14cf-4f0f-b0b8-3f09b28e3a52","colab":{"base_uri":"https://localhost:8080/","height":508},"trusted":true,"_uuid":"1feed44c5709d1affcc7b6ad205d34c5c97f924e"},"cell_type":"code","source":"!pip install git+https://github.com/onnx/onnx-tensorflow.git\n!pip install keras2onnx \n\n!apt install -y graphviz \n\n\n!pip install -q  keras torch torchvision graphviz autokeras  onnxmltools\n!pip install -q    onnxmltools onnx-tf\n#!pip install --user autokeras \n!pip install git+https://github.com/jhfjhfj1/autokeras.git\nimport keras\nimport autokeras as ak\nimport onnxmltools","execution_count":null,"outputs":[]},{"metadata":{"id":"6zL3ExXfdUpD","colab_type":"code","outputId":"e20074d6-6690-482f-e13b-5930f5cced5d","colab":{"base_uri":"https://localhost:8080/","height":55},"trusted":true,"_uuid":"4729a6bc4c0a46f405ba16e5e4fab06f7c80373a"},"cell_type":"code","source":"(x_train, y_train), (x_test, y_test)=keras.datasets.mnist.load_data()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"df79585d4edb463a3e31a82d77f6917e8561b03f"},"cell_type":"code","source":"x_train= x_train[...,None]\nx_test=x_test[...,None]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"82b632cc29b803a68ab20b101a7058143a42abed"},"cell_type":"code","source":"train = pd.read_csv('../input/emnist-balanced-train.csv', header=None)\ntest = pd.read_csv('../input/emnist-balanced-test.csv', header=None)\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ntrain_data = train.iloc[:, 1:]\ntrain_labels = train.iloc[:, 0]\ntest_data = test.iloc[:, 1:]\ntest_labels = test.iloc[:, 0]\n\n","execution_count":null,"outputs":[]},{"metadata":{"id":"ZHjlBKflaxEw","colab_type":"code","outputId":"73fca99a-79aa-4435-9ed5-00e8761b189f","colab":{"base_uri":"https://localhost:8080/","height":36},"trusted":true,"_uuid":"6b5845445511fc0763d9db9dd4885757539425be"},"cell_type":"code","source":"\n\n\nclf = ak.ImageClassifier(verbose=True, augment=False)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"09934484308f9405716132511689c858c0851f7b"},"cell_type":"code","source":"time_limit=7*3600","execution_count":null,"outputs":[]},{"metadata":{"id":"vpwsqPCNo2BB","colab_type":"code","outputId":"b82d5f17-31ae-49ae-fdc6-6d0dd82b5780","colab":{"base_uri":"https://localhost:8080/","height":225},"trusted":true,"_uuid":"b2fdcc843036a69cf40915dfc8ca43970f90bd10"},"cell_type":"code","source":"clf.fit(x_train, y_train, time_limit=time_limit)\n","execution_count":null,"outputs":[]},{"metadata":{"id":"tDdhzH3_qDaG","colab_type":"code","colab":{},"trusted":true,"_uuid":"a027aa80058a56c6a7959d6fcca6abd17b3cf366"},"cell_type":"code","source":"clf.final_fit(x_train, y_train, x_test, y_test)","execution_count":null,"outputs":[]},{"metadata":{"id":"NZBwRxSFpdVS","colab_type":"code","colab":{},"trusted":true,"_uuid":"83abcf87ccc5f46b5bc19e53996a40a6e0701b48"},"cell_type":"code","source":"clf.evaluate(x_test, y_test)","execution_count":null,"outputs":[]},{"metadata":{"id":"bjiBHkX8pbUn","colab_type":"code","colab":{},"trusted":true,"_uuid":"63c5630af20755b37a035c5c856d552b165ba78e"},"cell_type":"code","source":"results = clf.predict(x_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"665c192761e6deb487c868bc231160cc29c5a33c"},"cell_type":"code","source":"model=clf.cnn.best_model #keras.models.load_model(\"model.h5\")\n","execution_count":null,"outputs":[]},{"metadata":{"id":"EgC9v6DQvYc_","colab_type":"code","colab":{},"trusted":true,"_uuid":"8db572ce6a4ef256d9fd44b60c98a45691274c17"},"cell_type":"code","source":"import IPython\nfrom graphviz import Digraph\ndot = Digraph(comment='autokeras model')\ngraph=clf.cnn.best_model\nfor index, node in enumerate(graph.node_list):\n    dot.node(str(index), str(node.shape))\n\nfor u in range(graph.n_nodes):\n    for v, layer_id in graph.adj_list[u]:\n      dot.edge(str(u), str(v), str(graph.layer_list[layer_id]))\ndot.render(filename='model.png',format='png')\ndot.render(filename='model.svg',format='svg')\nIPython.display.Image(filename='model.png')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fe3cb7ddeba13de5ae0f55104d4658587be30f66"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"id":"nOZW9w6arjJR","colab_type":"code","colab":{},"trusted":true,"_uuid":"9143f69a985f703520d573e59a3e9996f9c773a2"},"cell_type":"code","source":"import IPython\n\nkeras_model=clf.cnn.best_model.produce_keras_model()\n\nkeras.utils.plot_model(keras_model, show_shapes=True, to_file='model_keras_mnist.png')\nIPython.display.Image(filename='model_keras_mnist.png')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fe6bc314000c0a09d44ba5f8b41f952e4a2caabe"},"cell_type":"code","source":"keras_model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bfdcad3bb20750099835d434cfa01a1bdc6f8cfc"},"cell_type":"code","source":"keras_model.compile(\"adam\",\"mse\")\nkeras_model.save(\"model.h5\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"39e29a979c430d587f0fffab533da87a34218615"},"cell_type":"code","source":"#clf.export_keras_model(\"model.h5\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b8c3c84ff5cb4bb64106351936d3455d723d56e5"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"df6a0e74cb430f0a64e0df1b2f0f900d602b571e"},"cell_type":"code","source":"\nkeras_model=keras.models.load_model(\"model.h5\")\nkeras_model.compile(\"adam\",\"mse\")\nonnx_model = onnxmltools.convert_keras(keras_model, target_opset=7) \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n# Save as text\nonnxmltools.utils.save_text(onnx_model, 'model.json')\n\n# Save as protobuf\nonnxmltools.utils.save_model(onnx_model, 'model.onnx')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import keras2onnx\nonnx_model = keras2onnx.convert_keras(keras_model,\"autokeras nmist\")\n# Save as text\nonnxmltools.utils.save_text(onnx_model, 'model_keras2onnx.json')\n\n# Save as protobuf\nonnxmltools.utils.save_model(onnx_model, 'model_keras2onnx.onnx","execution_count":null,"outputs":[]}],"metadata":{"colab":{"name":"autokeras_mnist.ipynb","version":"0.3.2","provenance":[],"collapsed_sections":[]},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"accelerator":"TPU","language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}