I have to train binary semantic segmentation in Python(using Tenforflow, rasterio, geopandas, sh). And I have raster .tif images and vector .shp images which is mask. I know that I should divide each images into sub-images(tiles). But I need some help in coding part because even when I found code to generate patches for raster images I cannot apply it to the mask because it's vector file, not raster one. Here is multiple codes that I tried:
large_image_stack = tiff.imread('path_to_file.tif')
#large_mask_stack = tiff.imread('small_dataset_for_training/masks/12_training_mito_masks.tif')
for img in range(large_image_stack.shape[0]):
large_image = large_image_stack[img]
patches_img = patchify(large_image, (64, 64), step=64) #Step=256 for 256 patches means no overlap
patches_img = np.squeeze(patches_img)
for i in range(patches_img.shape[0]):
for j in range(patches_img.shape[1]):
single_patch_img = patches_img[i,j,:,:,:]
tiff.imwrite('patches/images/' + 'image_' + str(img) + '_' + str(i)+str(j)+ ".tif", single_patch_img)
By the way I had this error while running this : window_shape
is too large
Another code:
patch_size = 128
scaler = MinMaxScaler()
image_dataset = []
for path,subdirs,files in os.walk(tif_dir):
dirname = path.split(os.path.sep)[-1]
if dirname == 'train':
images = os.listdir(path)
images.sort()
print(images)
for i, image_name in enumerate(images):
if image_name.endswith(".tif"):
#Get image paths "print(path+"/"+image_name)"
image = cv2.imread(path+"/"+image_name,1)
SIZE_X = (image.shape[1]//patch_size)*patch_size
SIZE_Y = (image.shape[0]//patch_size)*patch_size
image = Image.fromarray(image)
image = image.crop((0,0,SIZE_X,SIZE_Y))
image = np.array(image)
patches_img = patchify(image, (patch_size, patch_size, 3), step=patch_size)
for i in range(patches_img.shape[0]):
for j in range(patches_img.shape[1]):
single_patch_img = patches_img[i,j,0,:,:]
single_patch_img = scaler.fit_transform(single_patch_img.reshape(-1, single_patch_img.shape[-1])).reshape(single_patch_img.shape)
image_dataset.append(single_patch_img)
My question is what can I do with both .tif raster and .shp vector images to train them?