forked from cnheider/jord
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
394 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,337 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "initial_id", | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"\n", | ||
"\n", | ||
"from shapely.geometry import Point\n", | ||
"from torch.utils.data import Dataset\n", | ||
"\n", | ||
"class ShapelyFeatureDataset(Dataset):\n", | ||
"\n", | ||
" def __init__(self, features, transform=None):\n", | ||
" self.features = features\n", | ||
" self.transform = transform\n", | ||
"\n", | ||
" def __len__(self):\n", | ||
" return len(self.features)\n", | ||
"\n", | ||
" def __getitem__(self, idx):\n", | ||
" feature = self.features[idx]\n", | ||
"\n", | ||
" if self.transform:\n", | ||
" feature = self.transform(feature)\n", | ||
"\n", | ||
" raster = self.feature_to_raster(feature)\n", | ||
" return raster\n", | ||
"\n", | ||
" def feature_to_raster(self, feature):\n", | ||
" # Create a blank raster\n", | ||
" raster = np.zeros((256, 256), dtype=np.uint8)\n", | ||
" # Convert feature to raster\n", | ||
" coords = np.array(feature.exterior.coords)\n", | ||
" cv2.fillPoly(raster, [coords.astype(np.int32)], 255)\n", | ||
" return raster\n", | ||
"\n", | ||
"class RandomTransform:\n", | ||
"\n", | ||
" def __init__(self, scale_range=(0.5, 1.5), rotation_range=(-45, 45), noise_level=0.1):\n", | ||
" self.scale_range = scale_range\n", | ||
" self.rotation_range = rotation_range\n", | ||
" self.noise_level = noise_level\n", | ||
"\n", | ||
" def __call__(self, feature):\n", | ||
" # Random scale\n", | ||
" scale_factor = random.uniform(*self.scale_range)\n", | ||
" feature = scale(feature, xfact=scale_factor, yfact=scale_factor, origin='center')\n", | ||
"\n", | ||
" # Random rotation\n", | ||
" angle = random.uniform(*self.rotation_range)\n", | ||
" feature = rotate(feature, angle, origin='center')\n", | ||
"\n", | ||
" # Add noise\n", | ||
" noise = np.random.normal(0, self.noise_level, feature.exterior.coords.shape)\n", | ||
" noisy_coords = feature.exterior.coords + noise\n", | ||
" feature = shape({'type': 'Polygon', 'coordinates': [noisy_coords]})\n", | ||
"\n", | ||
" return feature\n", | ||
"\n", | ||
"# Example usage\n", | ||
"features = [Point(128, 128).buffer(50)] # Example Shapely features\n", | ||
"transform = RandomTransform()\n", | ||
"dataset = ShapelyFeatureDataset(features, transform=transform)\n", | ||
"\n", | ||
"# Get a sample\n", | ||
"sample = dataset[0]\n", | ||
"print(sample)" | ||
] | ||
}, | ||
{ | ||
"metadata": {}, | ||
"cell_type": "code", | ||
"outputs": [], | ||
"execution_count": null, | ||
"source": [ | ||
"\n", | ||
"from shapely.geometry import Point\n", | ||
"from torch.utils.data import Dataset\n", | ||
"\n", | ||
"class ShapelyFeatureDataset(Dataset):\n", | ||
"\n", | ||
" def __init__(self, features, transform=None, subset_size=5):\n", | ||
" self.features = features\n", | ||
" self.transform = transform\n", | ||
" self.subset_size = subset_size\n", | ||
"\n", | ||
" def __len__(self):\n", | ||
" return len(self.features)\n", | ||
"\n", | ||
" def __getitem__(self, idx):\n", | ||
" subset = self.sample_subset()\n", | ||
" rasters, masks = self.features_to_rasters(subset)\n", | ||
" return rasters, masks\n", | ||
"\n", | ||
" def sample_subset(self):\n", | ||
" return random.sample(self.features, self.subset_size)\n", | ||
"\n", | ||
" def features_to_rasters(self, features):\n", | ||
" rasters = []\n", | ||
" masks = []\n", | ||
" placed_features = []\n", | ||
" for feature in features:\n", | ||
" if self.transform:\n", | ||
" feature = self.transform(feature)\n", | ||
" feature = self.ensure_no_overlap(feature, placed_features)\n", | ||
" raster, mask = self.feature_to_raster(feature)\n", | ||
" rasters.append(raster)\n", | ||
" masks.append(mask)\n", | ||
" placed_features.append(feature)\n", | ||
" return rasters, masks\n", | ||
"\n", | ||
" def ensure_no_overlap(self, feature, placed_features):\n", | ||
" minx, miny, maxx, maxy = feature.bounds\n", | ||
" width, height = maxx - minx, maxy - miny\n", | ||
" while True:\n", | ||
" x_offset = random.randint(0, 256 - int(width))\n", | ||
" y_offset = random.randint(0, 256 - int(height))\n", | ||
" translated_feature = translate(feature, xoff=x_offset - minx, yoff=y_offset - miny)\n", | ||
" if not any(translated_feature.intersects(pf) for pf in placed_features):\n", | ||
" return translated_feature\n", | ||
"\n", | ||
" def feature_to_raster(self, feature):\n", | ||
" # Create a blank raster and mask\n", | ||
" raster = np.zeros((256, 256), dtype=np.uint8)\n", | ||
" mask = np.zeros((256, 256), dtype=np.uint8)\n", | ||
"\n", | ||
" # Convert feature to raster\n", | ||
" coords = np.array(feature.exterior.coords)\n", | ||
" cv2.fillPoly(raster, [coords.astype(np.int32)], 255)\n", | ||
" cv2.fillPoly(mask, [coords.astype(np.int32)], 1)\n", | ||
" return raster, mask\n", | ||
"\n", | ||
"class RandomTransform:\n", | ||
"\n", | ||
" def __init__(self, scale_range=(0.5, 1.5), rotation_range=(-45, 45), noise_level=0.1):\n", | ||
" self.scale_range = scale_range\n", | ||
" self.rotation_range = rotation_range\n", | ||
" self.noise_level = noise_level\n", | ||
"\n", | ||
" def __call__(self, feature):\n", | ||
" # Random scale\n", | ||
" scale_factor = random.uniform(*self.scale_range)\n", | ||
" feature = scale(feature, xfact=scale_factor, yfact=scale_factor, origin='center')\n", | ||
"\n", | ||
" # Random rotation\n", | ||
" angle = random.uniform(*self.rotation_range)\n", | ||
" feature = rotate(feature, angle, origin='center')\n", | ||
"\n", | ||
" # Add noise\n", | ||
" noise = np.random.normal(0, self.noise_level, feature.exterior.coords.shape)\n", | ||
" noisy_coords = feature.exterior.coords + noise\n", | ||
" feature = shape({'type': 'Polygon', 'coordinates': [noisy_coords]})\n", | ||
"\n", | ||
" return feature\n", | ||
"\n", | ||
"# Example usage\n", | ||
"features = [Point(128, 128).buffer(50) for _ in range(10)] # Example Shapely features\n", | ||
"transform = RandomTransform()\n", | ||
"dataset = ShapelyFeatureDataset(features, transform=transform, subset_size=5)\n", | ||
"\n", | ||
"# Get a sample\n", | ||
"rasters, masks = dataset[0]\n", | ||
"for i, (raster, mask) in enumerate(zip(rasters, masks)):\n", | ||
" print(f\"Raster {i}:\\n{raster}\\nMask {i}:\\n{mask}\")\n" | ||
], | ||
"id": "d3e8495b3d58b5bf" | ||
}, | ||
{ | ||
"metadata": {}, | ||
"cell_type": "code", | ||
"outputs": [], | ||
"execution_count": null, | ||
"source": [ | ||
"import random\n", | ||
"import cv2\n", | ||
"import numpy as np\n", | ||
"from shapely.affinity import rotate, scale, translate\n", | ||
"from shapely.geometry import Point, shape\n", | ||
"from torch.utils.data import Dataset\n", | ||
"\n", | ||
"class ShapelyFeatureDataset(Dataset):\n", | ||
"\n", | ||
" def __init__(self, categories, transform=None, subset_size=5):\n", | ||
" self.categories = categories\n", | ||
" self.transform = transform\n", | ||
" self.subset_size = subset_size\n", | ||
"\n", | ||
" def __len__(self):\n", | ||
" return sum(len(features) for features in self.categories.values())\n", | ||
"\n", | ||
" def __getitem__(self, idx):\n", | ||
" subset = self.sample_subset()\n", | ||
" rasters, masks = self.features_to_rasters(subset)\n", | ||
" return rasters, masks\n", | ||
"\n", | ||
" def sample_subset(self):\n", | ||
" subset = []\n", | ||
" for category, features in self.categories.items():\n", | ||
" subset.extend(random.sample(features, min(self.subset_size, len(features))))\n", | ||
" return subset\n", | ||
"\n", | ||
" def features_to_rasters(self, features):\n", | ||
" rasters = []\n", | ||
" masks = []\n", | ||
" placed_features = []\n", | ||
" for feature in features:\n", | ||
" if self.transform:\n", | ||
" feature = self.transform(feature)\n", | ||
" feature = self.ensure_no_overlap(feature, placed_features)\n", | ||
" raster, mask = self.feature_to_raster(feature)\n", | ||
" rasters.append(raster)\n", | ||
" masks.append(mask)\n", | ||
" placed_features.append(feature)\n", | ||
" return rasters, masks\n", | ||
"\n", | ||
" def ensure_no_overlap(self, feature, placed_features):\n", | ||
" minx, miny, maxx, maxy = feature.bounds\n", | ||
" width, height = maxx - minx, maxy - miny\n", | ||
" while True:\n", | ||
" x_offset = random.randint(0, 256 - int(width))\n", | ||
" y_offset = random.randint(0, 256 - int(height))\n", | ||
" translated_feature = translate(feature, xoff=x_offset - minx, yoff=y_offset - miny)\n", | ||
" if not any(translated_feature.intersects(pf) for pf in placed_features):\n", | ||
" return translated_feature\n", | ||
"\n", | ||
" def feature_to_raster(self, feature):\n", | ||
" # Create a blank raster and mask\n", | ||
" raster = np.zeros((256, 256), dtype=np.uint8)\n", | ||
" mask = np.zeros((256, 256), dtype=np.uint8)\n", | ||
"\n", | ||
" # Convert feature to raster\n", | ||
" coords = np.array(feature.exterior.coords)\n", | ||
" cv2.fillPoly(raster, [coords.astype(np.int32)], 255)\n", | ||
" cv2.fillPoly(mask, [coords.astype(np.int32)], 1)\n", | ||
" return raster, mask\n", | ||
"\n", | ||
"class RandomTransform:\n", | ||
"\n", | ||
" def __init__(self, scale_range=(0.5, 1.5), rotation_range=(-45, 45), noise_level=0.1):\n", | ||
" self.scale_range = scale_range\n", | ||
" self.rotation_range = rotation_range\n", | ||
" self.noise_level = noise_level\n", | ||
"\n", | ||
" def __call__(self, feature):\n", | ||
" # Random scale\n", | ||
" scale_factor = random.uniform(*self.scale_range)\n", | ||
" feature = scale(feature, xfact=scale_factor, yfact=scale_factor, origin='center')\n", | ||
"\n", | ||
" # Random rotation\n", | ||
" angle = random.uniform(*self.rotation_range)\n", | ||
" feature = rotate(feature, angle, origin='center')\n", | ||
"\n", | ||
" # Add noise\n", | ||
" noise = np.random.normal(0, self.noise_level, feature.exterior.coords.shape)\n", | ||
" noisy_coords = feature.exterior.coords + noise\n", | ||
" feature = shape({'type': 'Polygon', 'coordinates': [noisy_coords]})\n", | ||
"\n", | ||
" return feature\n", | ||
"\n", | ||
"# Example usage\n", | ||
"categories = {\n", | ||
" 'chairs': [Point(128, 128).buffer(50) for _ in range(3)],\n", | ||
" 'tables': [Point(64, 64).buffer(30) for _ in range(3)],\n", | ||
" 'carpets': [Point(192, 192).buffer(40) for _ in range(3)],\n", | ||
" 'plants': [Point(32, 32).buffer(20) for _ in range(3)],\n", | ||
" 'walls': [Point(160, 160).buffer(60) for _ in range(3)]\n", | ||
" }\n", | ||
"transform = RandomTransform()\n", | ||
"dataset = ShapelyFeatureDataset(categories, transform=transform, subset_size=2)\n", | ||
"\n", | ||
"# Get a sample\n", | ||
"rasters, masks = dataset[0]\n", | ||
"for i, (raster, mask) in enumerate(zip(rasters, masks)):\n", | ||
" print(f\"Raster {i}:\\n{raster}\\nMask {i}:\\n{mask}\")" | ||
], | ||
"id": "16b0d6871c86332d" | ||
}, | ||
{ | ||
"metadata": { | ||
"jupyter": { | ||
"is_executing": true | ||
} | ||
}, | ||
"cell_type": "code", | ||
"source": [ | ||
"from matplotlib import pyplot\n", | ||
"\n", | ||
"# Example usage\n", | ||
"features = [Point(128, 128).buffer(50) for _ in range(10)] # Example Shapely features\n", | ||
"transform = RandomTransform()\n", | ||
"dataset = ShapelyFeatureDataset(features, transform=transform, subset_size=5)\n", | ||
"\n", | ||
"# Get a sample\n", | ||
"rasters, masks = dataset[0]\n", | ||
"\n", | ||
"# Visualize the rasters\n", | ||
"fig, axes = pyplot.subplots(1, len(rasters), figsize=(15, 5))\n", | ||
"for i, raster in enumerate(rasters):\n", | ||
" axes[i].imshow(raster, cmap='gray')\n", | ||
" axes[i].set_title(f'Raster {i}')\n", | ||
" axes[i].axis('off')\n", | ||
"\n", | ||
"pyplot.show()\n" | ||
], | ||
"id": "f04530319e7b8596", | ||
"outputs": [], | ||
"execution_count": null | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 2 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython2", | ||
"version": "2.7.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
Oops, something went wrong.