{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nos.listdir('../input')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install keras_efficientnets","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from PIL import Image, ImageOps\nimport gc\n\nimport pandas as pd\nimport numpy as np\n\nimport keras\nfrom keras.models import load_model\n\nfrom keras_efficientnets import EfficientNetB5, preprocess_input","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"IMG_SIZE = (456, 456)\nBATCH_SIZE = 32","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from PIL import ImageOps\n\nclass TestGenerator(keras.utils.Sequence):\n    \n    def __init__(self, x, mirror=True):\n        self.x = x\n        self.mirror = mirror\n        \n    def __len__(self):\n        return int(np.ceil(len(self.x)/BATCH_SIZE))\n    \n    def _transform(self, path):\n        image = Image.open('../input/3rd-ml-month-car-image-cropping-dataset/test_crop/' + path).convert(\"RGB\").resize(IMG_SIZE)\n        if self.mirror:\n            image = ImageOps.mirror(image)\n        image = preprocess_input(np.array(image))\n        return image\n    \n    def __getitem__(self, index):\n        x = np.array(list(map(self._transform, self.x[index*BATCH_SIZE:(index+1)*BATCH_SIZE])))\n        return  x","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test = pd.read_csv('../input/2019-3rd-ml-month-with-kakr/test.csv')['img_file'].values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def predict_one(x, file):\n    test_preds = np.zeros((len(x), 196))\n    model = load_model(file)\n    for mirror in [False, True]:\n        test_generator = TestGenerator(x, mirror=mirror)\n        test_preds += model.predict_generator(\n            test_generator,\n            verbose=2,\n        )\n    return test_preds\n\ndef predict_oof(x, files, weights=None):\n    if weights is None:\n        weights = [1]*len(files)\n    test_preds = np.zeros((len(x), 196))\n    for file, weight in zip(files, weights):\n        test_preds += predict_one(x, file)*weight\n    return test_preds","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"files = [\n    '../input/car-classification-models/eff0.h5',\n    '../input/car-classification-models/eff1.h5',\n    '../input/car-classification-models/eff2.h5',\n    '../input/car-classification-models/eff3.h5',\n    '../input/car-classification-models/eff4.h5',\n    \n    '../input/car-classification-models/eff5.h5', # 0.9484\n    '../input/car-classification-models/eff6.h5', # 0.95~~\n    '../input/car-classification-models/eff7.h5', # 0.9461\n    '../input/car-classification-models/eff8.h5', # 0.9448\n    '../input/car-classification-models/eff9.h5', # 0.9487\n]\nweights = None","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_preds = predict_oof(test, files, weights)\n    \npred = test_preds.argmax(axis=1)+1\n\nsubmission = pd.DataFrame({'img_file': test, 'class': pred})\nsubmission.to_csv('submission.csv', index=False)\nsubmission.head()","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.7.3"}},"nbformat":4,"nbformat_minor":1}