{"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","outputId":"680d26fa-14cf-4f0f-b0b8-3f09b28e3a52","trusted":true,"_uuid":"1feed44c5709d1affcc7b6ad205d34c5c97f924e"},"cell_type":"code","source":"!pip install git+https://github.com/onnx/onnx-tensorflow.git\n!pip install keras2onnx \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":{"trusted":true},"cell_type":"code","source":"!ls -Rlh ../input","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df=pd.read_csv(\"../input/train.csv\")\ndf[\"File Name\"]=df[\"file_name\"]\ndf[\"Label\"]=df[\"category_id\"]\ndf.sample(n=5000)[[\"File Name\",\"Label\"]].to_csv(\"train.csv\", index=False)\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train,y_train=ak.image.image_supervised.load_image_dataset(\"train.csv\",\"../input/train_images\")#, parallel=False)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"82b632cc29b803a68ab20b101a7058143a42abed"},"cell_type":"code","source":"duree_max=datetime.timedelta(hours=4,minutes=30)\ndate_limite= date_depart+duree_max\nduree_max.total_seconds()","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":{"id":"ZHjlBKflaxEw","outputId":"73fca99a-79aa-4435-9ed5-00e8761b189f","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":"\ndelai=date_limite-datetime.datetime.now()\n\ntime_limit=delai.total_seconds()\n\nprint(delai)\ntime_limit","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(datetime.datetime.now()-date_depart)","execution_count":null,"outputs":[]},{"metadata":{"id":"vpwsqPCNo2BB","outputId":"b82d5f17-31ae-49ae-fdc6-6d0dd82b5780","trusted":true,"_uuid":"b2fdcc843036a69cf40915dfc8ca43970f90bd10"},"cell_type":"code","source":"clf.fit(x_train, y_train, time_limit=time_limit)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(datetime.datetime.now()-date_depart)","execution_count":null,"outputs":[]},{"metadata":{"id":"tDdhzH3_qDaG","trusted":true,"_uuid":"a027aa80058a56c6a7959d6fcca6abd17b3cf366"},"cell_type":"code","source":"clf.final_fit(x_train, y_train, x_test, y_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(datetime.datetime.now()-date_depart)","execution_count":null,"outputs":[]},{"metadata":{"id":"NZBwRxSFpdVS","trusted":true,"_uuid":"83abcf87ccc5f46b5bc19e53996a40a6e0701b48"},"cell_type":"code","source":"clf.evaluate(x_test, y_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(datetime.datetime.now()-date_depart)","execution_count":null,"outputs":[]},{"metadata":{"id":"bjiBHkX8pbUn","trusted":true,"_uuid":"63c5630af20755b37a035c5c856d552b165ba78e"},"cell_type":"code","source":"results = clf.predict(x_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(datetime.datetime.now()-date_depart)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":""},{"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_","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","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":"print(datetime.datetime.now()-date_depart)","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":"# 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":"print(datetime.datetime.now()-date_depart)","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":{"trusted":true},"cell_type":"code","source":"print(datetime.datetime.now()-date_depart)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}