{"cells":[{"metadata":{},"cell_type":"markdown","source":"quote from module site:\n> Auto-Keras is an open source software library for automated machine learning (AutoML). It is developed by DATA Lab at Texas A&M University and community contributors. The ultimate goal of AutoML is to provide easily accessible deep learning tools to domain experts with limited data science or machine learning background. Auto-Keras provides functions to automatically search for architecture and hyperparameters of deep learning models.\n\n\n\"domain experts with limited data science or machine learning background\" aren't the main public here on kaggle but the finding of automated search on different datasets may give some insights\n\n[https://autokeras.com/](https://autokeras.com/)"},{"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)\nimport os","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"duree_max=datetime.timedelta(hours=5,minutes=30)","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!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 --user git+https://github.com/jhfjhfj1/autokeras.git\n!pip install  -U --user tensorflow-gpu\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":"print(datetime.datetime.now()-date_depart)\ndf=pd.read_csv(\"../input/fashion-small/fashion_small/fashion_small/styles.csv\",error_bad_lines =False, warn_bad_lines=False)\ndf.info()\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.groupby(\"gender\").size()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.groupby([\"year\",\"season\"]).size()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.groupby(\"masterCategory\").size()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"articleType=df.groupby(\"articleType\").size().sort_values(ascending=False)\narticleType.name=\"articleType n\"\narticleType=articleType[articleType>300].reset_index(drop=False)\narticleType.index.name=\"ncat\"\narticleType=articleType.reset_index(drop=False)\narticleType","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!ls ../input -Rlh","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"categories=pd.merge(df,articleType)[[\"id\",\"ncat\"]]\n\ncategories.ncat.plot.hist()\ncategories[\"path\"]=categories.id.apply(lambda i:f\"../input/fashion-small/fashion_small/fashion_small/resized_images/{i}.jpg\")\ncategories['File Name'] =categories.id.apply(lambda i:f\"{i}.jpg\")\ncategories['Label']=categories.ncat\n\n\n\ncategories","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"categories[\"exists\"]= categories[\"path\"].apply(os.path.isfile)\n\ncategories=categories[categories.exists].sample(frac=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ncategories[['File Name','Label']].to_csv(\"categories.csv\" ,index=False)\nlval=int( len(categories)*0.2)+1\ntrain=categories.iloc[:-lval]\nval=categories.iloc[-lval:]\ntrain[['File Name','Label']].to_csv(\"train.csv\" ,index=False)\nval[['File Name','Label']].to_csv(\"val.csv\" ,index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"basepath=\"../input/fashion-small/fashion_small/fashion_small/resized_images/\"\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nx_test, y_test =ak.image.image_supervised.load_image_dataset(\"val.csv\",basepath)\nx_test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train, y_train=ak.image.image_supervised.load_image_dataset(\"train.csv\",basepath)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"82b632cc29b803a68ab20b101a7058143a42abed"},"cell_type":"code","source":"\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]\n#x_test=x_test[...,None]","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":"\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","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":"\nclf.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","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":{"trusted":true},"cell_type":"code","source":"print(datetime.datetime.now()-date_depart)","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":{"trusted":true},"cell_type":"code","source":"print(datetime.datetime.now()-date_depart)","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":{},"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_","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":"print(datetime.datetime.now()-date_depart)","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":"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":{"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}