Hello!
I am trying to train a deep learning model to identify wetlands in several thousand historic topographic maps. I am setting up the DL training session currently (breaking maps into "chips"-- code below). All of my files are properly named with .shp, and .tif extensions in their respective folders. I'm stumped. Any ideas would be welcome.
#Read in libraries/extensions
import arcpy
arcpy.CheckOutExtension("ImageAnalyst")
arcpy.env.overwriteOutput = True
from arcpy.ia import *
from arcpy import env
from arcpy.sa import *
import os
# Define input folders
in_fold = "C:/Users/jub718/Documents/GulfofMaine/Wetlands_ML/"
in_toposF = in_fold + "Raster/"
in_wetlandsF = in_fold + "Data_Layers/"
in_quadsF = in_fold + "Masks/"
chips_outF = in_fold + "/processing/goa_Chips/"
to_8bitF = in_fold + "/processing/goa_8bit/"
#create new directories
os.mkdir(chips_outF)
os.mkdir(to_8bitF)
#List all topo maps
arcpy.env.workspace = in_toposF
chips = arcpy.ListRasters()
print(chips)
#Make list and remove file extensions
chip_n = list()
for c in chips:
c1 = os.path.basename(c)
c2 = os.path.splitext(c1)[0]
chip_n.append(c2)
print(chip_n)
import arcpy
import os
from arcpy.ia import ExportTrainingDataForDeepLearning
for cr in chip_n:
# Set workspace
arcpy.env.workspace = in_toposF
# Set local variables and make folders
cr_parts = cr.split('_')
if len(cr_parts) > 3:
quadNm = f"{cr_parts[1]}_{cr_parts[3]}".replace(" ", "_")
else:
quadNm = cr.replace(" ", "_")
subdir = os.path.join(chips_outF, cr)
out_folder = os.path.join(in_fold, "processing", "goa_Chips")
# Ensure the output folder exists
if not os.path.exists(out_folder):
os.makedirs(out_folder)
# Read in topo
in_raster = os.path.join(in_toposF, f"{cr}.tif")
# Copy topo to 8-bit PNG
in_raster2 = arcpy.CopyRaster_management(in_raster, os.path.join(to_8bitF, f"{cr}.png"), "", "", 256, "NONE", "NONE", "8_BIT_UNSIGNED", "NONE", "NONE", "PNG", "NONE")
# Read in wetlands
in_wetlands = os.path.join(in_wetlandsF, f"{cr}.shp")
in_training = os.path.join(in_wetlandsF, f"{cr}.shp")
arcpy.env.workspace = in_toposF
# Define image chip parameters
image_chip_format = "PNG"
tile_size_x = 256
tile_size_y = 256
stride_x = 128
stride_y = 128
output_nofeature_tiles = "ONLY_TILES_WITH_FEATURES"
metadata_format = "Classified_Tiles"
start_index = 0
class_value_field = "value"
buffer_radius = 0
in_mask_polygons = os.path.join(in_quadsF, f"{cr}.shp")
rotation_angle = 0
reference_system = "MAP_SPACE"
processing_mode = "PROCESS_AS_MOSAICKED_IMAGE"
blacken_around_feature = "NO_BLACKEN"
crop_mode = "FIXED_SIZE"
in_instance_data = None
instance_class_value_field = "classvalue"
min_polygon_overlap_ratio = None
in_class_data = in_wetlands
# Validate feature classes
if not arcpy.Exists(in_class_data):
raise FileNotFoundError(f"Feature class {in_class_data} does not exist.")
if not arcpy.Exists(in_mask_polygons):
raise FileNotFoundError(f"Feature class {in_mask_polygons} does not exist.")
# Create image chips
ExportTrainingDataForDeepLearning(
in_raster, out_folder, in_class_data, image_chip_format, tile_size_x, tile_size_y, stride_x, stride_y,
output_nofeature_tiles, metadata_format, start_index, class_value_field, buffer_radius, in_mask_polygons,
rotation_angle, reference_system, processing_mode, blacken_around_feature, crop_mode, in_raster2,
in_instance_data, instance_class_value_field, min_polygon_overlap_ratio
)