{"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":"code","source":"!apt install zip","metadata":{"execution":{"iopub.status.busy":"2021-08-22T21:42:37.78822Z","iopub.execute_input":"2021-08-22T21:42:37.788482Z","iopub.status.idle":"2021-08-22T21:42:41.45323Z","shell.execute_reply.started":"2021-08-22T21:42:37.788435Z","shell.execute_reply":"2021-08-22T21:42:41.452542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Imports","metadata":{}},{"cell_type":"code","source":"import glob\nimport joblib\nimport numpy as np\nimport PIL\nfrom PIL import Image\nimport pydicom\nimport tqdm\nimport os\nimport cv2\nimport zipfile\nimport pandas as pd\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-22T21:42:46.974889Z","iopub.execute_input":"2021-08-22T21:42:46.975198Z","iopub.status.idle":"2021-08-22T21:42:47.672621Z","shell.execute_reply.started":"2021-08-22T21:42:46.975148Z","shell.execute_reply":"2021-08-22T21:42:47.671892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Get images paths","metadata":{}},{"cell_type":"code","source":"# Desired output size.\nRESIZED_WIDTH, RESIZED_HEIGHT = 224, 224\nEACH_WIDTH, EACH_HEIGHT = RESIZED_WIDTH // 2, RESIZED_HEIGHT // 2\n\nOUTPUT_FORMAT = \"jpg\"\n\nOUTPUT_DIR = \"data_argument_224_224\"","metadata":{"execution":{"iopub.status.busy":"2021-08-22T21:43:09.748312Z","iopub.execute_input":"2021-08-22T21:43:09.748646Z","iopub.status.idle":"2021-08-22T21:43:09.753494Z","shell.execute_reply.started":"2021-08-22T21:43:09.748592Z","shell.execute_reply":"2021-08-22T21:43:09.75244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_num2str = {0: 'powdery_mildew',\n                     1: 'scab',\n                     2: 'complex',\n                     3: 'frog_eye_leaf_spot',\n                     4: 'rust'}\n    \nlabel_str2num = {'powdery_mildew': 0,\n                 'scab': 1,\n                 'complex': 2,\n                 'frog_eye_leaf_spot': 3,\n                 'rust': 4}","metadata":{"execution":{"iopub.status.busy":"2021-08-22T21:43:13.831255Z","iopub.execute_input":"2021-08-22T21:43:13.831558Z","iopub.status.idle":"2021-08-22T21:43:13.836275Z","shell.execute_reply.started":"2021-08-22T21:43:13.831508Z","shell.execute_reply":"2021-08-22T21:43:13.835534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir = \"../input/plant-pathology-2021-fgvc8\"\n!ls {data_dir}","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2021-08-22T21:43:17.188647Z","iopub.execute_input":"2021-08-22T21:43:17.188989Z","iopub.status.idle":"2021-08-22T21:43:17.819989Z","shell.execute_reply.started":"2021-08-22T21:43:17.188931Z","shell.execute_reply":"2021-08-22T21:43:17.818997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = \"train_images\"\ntrain_paths = glob.glob(f\"{data_dir}/{train_dir}/*.jpg\")\n# test_dir = \"test_images\"\n# test_paths = glob.glob(f\"{data_dir}/{test_dir}/*.jpg\")\n# len(train_paths), len(test_paths)","metadata":{"execution":{"iopub.status.busy":"2021-08-22T21:43:20.783565Z","iopub.execute_input":"2021-08-22T21:43:20.783914Z","iopub.status.idle":"2021-08-22T21:43:21.932332Z","shell.execute_reply.started":"2021-08-22T21:43:20.783855Z","shell.execute_reply":"2021-08-22T21:43:21.931259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocess all data\n\nFirst declare a bunch of useful functions.","metadata":{}},{"cell_type":"code","source":"!mkdir -p {OUTPUT_DIR}/{train_dir}","metadata":{"execution":{"iopub.status.busy":"2021-08-22T21:43:26.411743Z","iopub.execute_input":"2021-08-22T21:43:26.412075Z","iopub.status.idle":"2021-08-22T21:43:27.053993Z","shell.execute_reply.started":"2021-08-22T21:43:26.412018Z","shell.execute_reply":"2021-08-22T21:43:27.052853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load converted images\n\nLet's test that everything is ok!","metadata":{}},{"cell_type":"code","source":"TRAIN_DF = pd.read_csv('../input/pp-csv/PP_one_hot.csv')\nTRAIN_DF","metadata":{"execution":{"iopub.status.busy":"2021-08-22T21:43:31.729268Z","iopub.execute_input":"2021-08-22T21:43:31.729599Z","iopub.status.idle":"2021-08-22T21:43:31.792414Z","shell.execute_reply.started":"2021-08-22T21:43:31.729545Z","shell.execute_reply":"2021-08-22T21:43:31.791705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"EXTRA_TRAIN = pd.DataFrame(columns=[\"image\", \"numerical labels\"], dtype=str)","metadata":{"execution":{"iopub.status.busy":"2021-08-22T21:43:35.629984Z","iopub.execute_input":"2021-08-22T21:43:35.630302Z","iopub.status.idle":"2021-08-22T21:43:35.639083Z","shell.execute_reply.started":"2021-08-22T21:43:35.630252Z","shell.execute_reply":"2021-08-22T21:43:35.638379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def concat_img(imgs):\n    if len(imgs) < 4:\n        return\n    output_shape = (EACH_WIDTH, EACH_HEIGHT)\n    imgs = [cv2.resize(img, output_shape) for img in imgs]\n    upper_img = cv2.vconcat([imgs[0], imgs[1]])\n    lower_img = cv2.vconcat([imgs[2], imgs[3]])\n    img = cv2.hconcat([upper_img, lower_img])\n    return img","metadata":{"execution":{"iopub.status.busy":"2021-08-22T21:43:37.671985Z","iopub.execute_input":"2021-08-22T21:43:37.672297Z","iopub.status.idle":"2021-08-22T21:43:37.678302Z","shell.execute_reply.started":"2021-08-22T21:43:37.672246Z","shell.execute_reply":"2021-08-22T21:43:37.677467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"length = len(TRAIN_DF)\nindexs = np.random.randint(0, length, size=4)\n\nfor count in range(1000):\n    imgs = []\n    new_num_labels = []\n    new_labels = []\n    indexs = np.random.randint(0, length, size=4)\n    for index in indexs:\n        filepath = train_paths[index]\n        img = cv2.imread(filepath)\n        imgs.append(img)\n        labels = TRAIN_DF.iloc[index, 1]\n        numerical_labels = TRAIN_DF.iloc[index, 2]\n        numerical_labels = numerical_labels.replace('[','')\n        numerical_labels = numerical_labels.replace(']','')\n        for disease in numerical_labels.split(','):\n            if not disease or int(disease) in new_num_labels:\n                continue\n            new_num_labels.append(int(disease))\n        new_labels = [label_num2str[label] for label in new_num_labels]\n    img_name = f\"{count}.jpg\"\n    new_img = concat_img(imgs)\n    new_label = ' '.join(new_labels)\n    new_num_label = str(new_num_labels)\n    new_file_path = f\"{OUTPUT_DIR}/{train_dir}/{img_name}\"\n    cv2.imwrite(new_file_path, new_img)\n    EXTRA_TRAIN = EXTRA_TRAIN.append({\"image\":img_name, \"labels\":new_label, \"numerical labels\":new_num_label}, ignore_index=True)","metadata":{"execution":{"iopub.status.busy":"2021-08-22T21:43:57.877173Z","iopub.execute_input":"2021-08-22T21:43:57.877483Z","iopub.status.idle":"2021-08-22T21:55:05.929815Z","shell.execute_reply.started":"2021-08-22T21:43:57.877432Z","shell.execute_reply":"2021-08-22T21:55:05.928864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_num = len(train_paths)\n# for i, train_path in enumerate(train_paths):\n#     filename = train_path.split(\"/\")[-1]\n#     img = Image.open(train_path)\n#     img = img.resize((RESIZED_WIDTH, RESIZED_HEIGHT))\n#     img.save(os.path.join(OUTPUT_DIR, train_dir, filename))\n#     print(f\"Successfully process {i}th image of {train_num} images\")\n\n# test_num = len(test_paths)\n# for i, test_path in enumerate(test_paths):\n#     filename = test_path.split(\"/\")[-1]\n#     img = Image.open(test_path)\n#     img = img.resize((RESIZED_WIDTH, RESIZED_HEIGHT))\n#     img.save(os.path.join(OUTPUT_DIR, test_dir, filename))\n#     print(f\"Successfully process {i}th image of {test_num} images\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"EXTRA_TRAIN.to_csv(f\"{OUTPUT_DIR}/extra_train.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2021-08-22T22:04:33.034164Z","iopub.execute_input":"2021-08-22T22:04:33.034487Z","iopub.status.idle":"2021-08-22T22:04:34.846151Z","shell.execute_reply.started":"2021-08-22T22:04:33.034435Z","shell.execute_reply":"2021-08-22T22:04:34.845404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!zip -r {OUTPUT_DIR}_resized.zip ./{OUTPUT_DIR}/*","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -r -f ./{OUTPUT_DIR}","metadata":{"execution":{"iopub.status.busy":"2021-07-27T02:25:03.260648Z","iopub.execute_input":"2021-07-27T02:25:03.260973Z","iopub.status.idle":"2021-07-27T02:25:03.898613Z","shell.execute_reply.started":"2021-07-27T02:25:03.26092Z","shell.execute_reply":"2021-07-27T02:25:03.897575Z"},"trusted":true},"execution_count":null,"outputs":[]}]}