{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"Training notebook [fgvc8_fastai_training1](https://www.kaggle.com/teykaihong/fgvc8-fastai)","metadata":{}},{"cell_type":"code","source":"from fastai.vision.all import *\n\nclass CFG:\n    models = ['../input/trained-models1/alexnet.pkl', '../input/trained-models1/resnet50.pkl']\n    test_images_path = Path('../input/plant-pathology-2021-fgvc8/test_images')\n    threshs_path = Path('../input/trained-models1/thresholds.h5')\n    classes = ['complex', 'frog_eye_leaf_spot', 'healthy', 'powdery_mildew', 'rust', 'scab']\n    tta_n = 3\n    \ndef get_x(x):\n    return CFG.test_images_path/x['image']\n\ndef get_y(y):\n    return y['labels'].split(' ')\n\ndef get_threshs(path):\n    import h5py\n    threshs = []\n    \n    with h5py.File(path, 'r') as h:\n        threshs.append(np.array(h['thresholds']))\n        \n    return (threshs[0])\n\ndef to_labels(preds, threshs):\n    labels = []\n    for pred in preds:\n        names = []\n        for idx, ret in enumerate((pred.tolist()>threshs).astype(int)):\n            if ret:\n                names.append(CFG.classes[idx])\n        labels.append(' '.join(names))\n    \n    for n_pred, label in enumerate(labels):\n        if label == '':\n            min_id = 0\n            global_min = 2\n            for cls_id, val in enumerate(abs(preds[n_pred].tolist() - threshs)):\n                local_min = val\n                if local_min < global_min:\n                    min_id = cls_id\n                    global_min = local_min\n                    \n            labels[n_pred] = CFG.classes[min_id]\n            \n    return labels\n    \ndef csv_formation(img_path):\n    \n    image_names = os.listdir(img_path)\n    df = pd.DataFrame({'image': image_names})\n    \n    return df\n\ndef submit(df, labels):\n    df['labels'] = labels\n    df.to_csv('./submission.csv', index=False)\n    display(df)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = 0\ntest_df = csv_formation(CFG.test_images_path)\nthreshs = get_threshs(CFG.threshs_path)\n\nfor m in CFG.models:\n    learner = load_learner(m, cpu=False).to_fp32()\n    test_dl = learner.dls.test_dl(test_df)\n    preds, _ = learner.tta(dl=test_dl, n=CFG.tta_n)\n    predictions += preds\n\npredictions /= len(CFG.models)\nlabels = to_labels(predictions, threshs)\nsubmit(test_df, labels)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}