Browse Source

[Feature/Fix] Add data split tool and fix cls PATH

geoyee 3 years ago
parent
commit
33d360445b
4 changed files with 60 additions and 7 deletions
  1. 1 1
      paddlers/models/__init__.py
  2. 3 4
      paddlers/models/ppcls/__init__.py
  3. 2 2
      tools/geojson2mask.py
  4. 54 0
      tools/spliter.py

+ 1 - 1
paddlers/models/__init__.py

@@ -12,4 +12,4 @@
 # See the License for the specific language governing permissions and
 # See the License for the specific language governing permissions and
 # limitations under the License.
 # limitations under the License.
 
 
-from . import ppcd, ppcls, ppdet, ppseg
+from . import ppcd, ppcls, ppdet, ppseg

+ 3 - 4
paddlers/models/ppcls/__init__.py

@@ -12,13 +12,12 @@
 # See the License for the specific language governing permissions and
 # See the License for the specific language governing permissions and
 # limitations under the License.
 # limitations under the License.
 
 
-# TODO: add ppcls module
 import sys
 import sys
-sys.path.append("paddlers/models")
+import os.path as osp
+sys.path.insert(0, osp.dirname(osp.dirname(osp.realpath(__file__))))
 
 
 from . import optimizer
 from . import optimizer
-
 from .arch import *
 from .arch import *
 from .optimizer import *
 from .optimizer import *
 from .data import *
 from .data import *
-from .utils import *
+from .utils import *

+ 2 - 2
tools/geojson2mask.py

@@ -65,7 +65,7 @@ def _read_geojson(json_path):
         return annotations, sizes
         return annotations, sizes
 
 
 
 
-def convertData(raw_folder, end_folder):
+def convert_data(raw_folder, end_folder):
     print("-- Initializing --")
     print("-- Initializing --")
     img_folder = osp.join(raw_folder, "images")
     img_folder = osp.join(raw_folder, "images")
     save_img_folder = osp.join(end_folder, "img")
     save_img_folder = osp.join(end_folder, "img")
@@ -105,4 +105,4 @@ parser.add_argument("--save_folder", type=str, required=True, \
 
 
 if __name__ == "__main__":
 if __name__ == "__main__":
     args = parser.parse_args()
     args = parser.parse_args()
-    convertData(args.raw_folder, args.save_folder)
+    convert_data(args.raw_folder, args.save_folder)

+ 54 - 0
tools/spliter.py

@@ -0,0 +1,54 @@
+# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import sys
+import os.path as osp
+sys.path.insert(0, osp.abspath(".."))  # add workspace
+
+import os
+import argparse
+from math import ceil
+from PIL import Image
+from paddlers.datasets.raster import Raster
+
+
+def split_data(image_path, block_size, save_folder):
+    if not osp.exists(save_folder):
+        os.makedirs(save_folder)
+    image_name = image_path.replace("\\", "/").split("/")[-1].split(".")[0]
+    raster = Raster(image_path, to_uint8=True)
+    rows = ceil(raster.height / block_size)
+    cols = ceil(raster.width / block_size)
+    total_number = int(rows * cols)
+    for r in range(rows):
+        for c in range(cols):
+            loc_start = (c * block_size, r * block_size)
+            title = Image.fromarray(raster.getArray(loc_start, (block_size, block_size)))
+            save_path = osp.join(save_folder, (image_name + "_" + str(r) + "_" + str(c) + ".png"))
+            title.save(save_path, "PNG")
+            print("-- {:d}/{:d} --".format(int(r * cols + c + 1), total_number))
+
+
+parser = argparse.ArgumentParser(description="input parameters")
+parser.add_argument("--image_path", type=str, required=True, \
+                    help="The path of big image data.")
+parser.add_argument("--block_size", type=int, default=512, \
+                    help="The size of image block.")
+parser.add_argument("--save_folder", type=str, default="output", \
+                    help="The folder path to save the results, `output` is the default.")
+
+
+if __name__ == "__main__":
+    args = parser.parse_args()
+    split_data(args.image_path, args.block_size, args.save_folder)