-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRiskmapDataset.py
72 lines (55 loc) · 2.28 KB
/
RiskmapDataset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
import numpy
from torch.utils.data import Dataset
from PIL import Image
class RiskMap(Dataset):
def __init__(self, **kwargs):
super().__init__()
self.filenames = self.getFileNames()
self.labels = dict()
self.labels["file_names"] = self.filenames
self._length = len(self.filenames)
def __len__(self):
return self._length
def __getitem__(self, i):
RISKMAP_PATH_171819 = "./data/171819RM/"
RISKMAP_PATH_202122 = "./data/202122RM/"
CON_IMG_MAP_256 = "./data/ConditionImageMap256/"
CON_IMG_SATELLITE_256 = "./data/ConditionImageSatellite256/"
example = dict()
example["file_name"] = self.labels["file_names"][i]
fn = example["file_name"] + ".npy"
RM171819 = numpy.load(RISKMAP_PATH_171819 + fn)
RM171819 = numpy.expand_dims(RM171819, axis=0).astype(numpy.float32)
example["riskmap_171819"] = RM171819
fn = example["file_name"] + ".npy"
RM202122 = numpy.load(RISKMAP_PATH_202122 + fn)
RM202122 = numpy.expand_dims(RM202122, axis=0).astype(numpy.float32)
example["image"] = RM202122
example["cond_map"] = self.preprocess_image(CON_IMG_MAP_256 + example["file_name"] + ".png")
example["cond_satellite"] = self.preprocess_image(CON_IMG_SATELLITE_256 + example["file_name"] + ".png")
return example
def preprocess_image(self, image_path):
image = Image.open(image_path)
if not image.mode == "RGB":
image = image.convert("RGB")
image = numpy.array(image).astype(numpy.uint8)
image = (image / 127.5 - 1.0).astype(numpy.float32) # 将像素都放缩到0-1之间
return image
def getFileNames(self):
return []
class RiskMapTrain(RiskMap):
def getFileNames(self):
with open("./data/train_files.txt", "r") as f:
relpaths = f.read().splitlines()
return relpaths
class RiskMapValidation(RiskMap):
def getFileNames(self):
with open("./data/val_files.txt", "r") as f:
relpaths = f.read().splitlines()
return relpaths
class RiskMapTest(RiskMap):
def getFileNames(self):
with open("./data/test_files.txt", "r") as f:
relpaths = f.read().splitlines()
return relpaths