{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"markdown","source":"## 자동차 모델 분류\nDamien Park  \n2019.08.12"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport json\nimport math\nimport tqdm\nimport glob\nfrom IPython.display import FileLink, FileLinks\n\nimport sklearn.preprocessing\n\nfrom PIL import Image as img\n\nimport matplotlib.pyplot as plt\n\nimport keras\nfrom keras.preprocessing.image import ImageDataGenerator","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# train\ntrain_df = pd.read_csv(\"../input/2019-3rd-ml-month-with-kakr/train.csv\", engine=\"python\")\ntrain_df[\"class\"] = train_df[\"class\"].astype(\"str\")\n\n# class(extract automaker)\nclass_df = pd.read_csv(\"../input/2019-3rd-ml-month-with-kakr/class.csv\")\nclass_df[\"id\"] = class_df[\"id\"].astype(\"str\")\nclass_df[\"automaker\"] = class_df[\"name\"].str.split(n=1, expand=True)[0]\n\ntrain_df = train_df.merge(class_df, how=\"inner\", left_on=\"class\", right_on=\"id\")\ntrain_df.drop(columns=\"id\", inplace=True)\ndisplay(train_df.head())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# test\ntest_df = pd.read_csv(\"../input/2019-3rd-ml-month-with-kakr/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# hyperparameter\nbatch_size = 32\nimg_size = (256, 256)\nchannels = (3, )\nmodel_type = \"xception\"\nif model_type == \"xception\":\n    preprocess_inputs = keras.applications.xception.preprocess_input\nelif model_type == \"Resnet\":\n    preprocess_inputs = keras.applications.resnet50.preprocess_input","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_datagen = ImageDataGenerator(horizontal_flip=True,\n                                   vertical_flip=True, \n                                   zoom_range=.1, preprocessing_function=preprocess_inputs)\n\ntrain_generator = train_datagen.flow_from_dataframe(dataframe=train_df, directory=\"../input/train\", \n                                                    x_col=\"img_file\", y_col=\"class\", \n                                                    class_mode=\"categorical\", color_mode=\"rgb\", \n                                                    batch_size=batch_size, target_size=img_size)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# model.reset_states()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"if model_type == \"xception\":\n    xception = keras.applications.xception.Xception(include_top=False, weights='imagenet', input_tensor=None, input_shape=img_size+channels, pooling=None, classes=196)\n    model = keras.models.Sequential()\n\n    model.add(xception)\n    model.add(keras.layers.GlobalAveragePooling2D())\n    model.add(keras.layers.Dropout(rate=.3))\n\n    model.add(keras.layers.Dense(5, activation='sigmoid'))\n\n    model.add(keras.layers.Dense(units=len(train_generator.class_indices), activation=\"softmax\", kernel_initializer='he_normal'))\n\n    model.compile(optimizer=\"RMSprop\", loss=\"categorical_crossentropy\", metrics=[\"acc\"])\n    \nelif model_type == \"Resnet\":\n    Resnet = keras.applications.resnet50.ResNet50(include_top=False, input_shape=img_size+channels)\n    model = keras.models.Sequential()\n\n    model.add(Resnet)\n    model.add(keras.layers.GlobalAveragePooling2D())\n    model.add(keras.layers.Dropout(rate=.3))\n\n    model.add(keras.layers.Dense(5, activation='sigmoid'))\n\n    model.add(keras.layers.Dense(units=len(train_generator.class_indices), activation=\"softmax\", kernel_initializer='he_normal'))\n\n    model.compile(optimizer=\"RMSprop\", loss=\"categorical_crossentropy\", metrics=[\"acc\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(model_type)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\nmodel.fit_generator(train_generator, epochs=200, steps_per_epoch=300, verbose=0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.plot(model.history.history[\"acc\"])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"---"},{"metadata":{"trusted":true},"cell_type":"code","source":"test_datagen = ImageDataGenerator(horizontal_flip=False,\n                                  vertical_flip=False, \n                                  zoom_range=.0, preprocessing_function=preprocess_inputs)\n\ntest_generator = test_datagen.flow_from_dataframe(dataframe=test_df, directory=\"../input/test\", \n                                                  x_col=\"img_file\", y_col=None,\n                                                  class_mode=None, color_mode=\"rgb\", \n                                                  batch_size=batch_size, target_size=img_size)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\nresult = model.predict_generator(generator=test_generator, steps=math.ceil(len(test_df)/batch_size), verbose=0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result = np.argmax(result, axis=1)\ntemp = pd.DataFrame({\"index\" : test_generator.index_array, \"class\" : result})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"label = train_generator.class_indices\nlabel = pd.DataFrame.from_dict(label, orient='index').reset_index().rename(columns={0:\"class\"})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result = temp.merge(label, left_on=\"class\", right_on=\"class\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = test_df.merge(result, left_index=True, right_on=\"index_x\")[[\"img_file\", \"class\"]]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"The end of Notebook"}],"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":1}