dongjoaquin commited on
Commit
a6cbf0a
·
verified ·
1 Parent(s): 6d55688

Upload loaders.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. loaders.py +836 -0
loaders.py ADDED
@@ -0,0 +1,836 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import print_function, division
2
+ import os
3
+ import torch
4
+ import pandas as pd
5
+ from skimage import io, transform
6
+ import numpy as np
7
+ import matplotlib.pyplot as plt
8
+ from torch.utils.data import Dataset, DataLoader
9
+ from torchvision import transforms, utils, datasets, models
10
+ import warnings
11
+ warnings.filterwarnings("ignore")
12
+
13
+
14
+ #dir_gainDPM="gain/DPM/",
15
+ #dir_gainDPMcars="gain/carsDPM/",
16
+ #dir_gainIRT2="gain/IRT2/",
17
+ #dir_gainIRT2cars="gain/carsIRT2/",
18
+ #dir_buildings="png/",
19
+ #dir_antenna= ,
20
+
21
+
22
+ class RadioUNet_c(Dataset):
23
+ """RadioMapSeer Loader for accurate buildings and no measurements (RadioUNet_c)"""
24
+ def __init__(self,maps_inds=np.zeros(1), phase="train",
25
+ ind1=0,ind2=0,
26
+ dir_dataset="RadioMapSeer/",
27
+ numTx=80,
28
+ thresh=0.2,
29
+ simulation="DPM",
30
+ carsSimul="no",
31
+ carsInput="no",
32
+ IRT2maxW=1,
33
+ cityMap="complete",
34
+ missing=1,
35
+ transform= transforms.ToTensor()):
36
+ """
37
+ Args:
38
+ maps_inds: optional shuffled sequence of the maps. Leave it as maps_inds=0 (default) for the standart split.
39
+ phase:"train", "val", "test", "custom". If "train", "val" or "test", uses a standard split.
40
+ "custom" means that the loader will read maps ind1 to ind2 from the list maps_inds.
41
+ ind1,ind2: First and last indices from maps_inds to define the maps of the loader, in case phase="custom".
42
+ dir_dataset: directory of the RadioMapSeer dataset.
43
+ numTx: Number of transmitters per map. Default and maximal value of numTx = 80.
44
+ thresh: Pathlos threshold between 0 and 1. Defaoult is the noise floor 0.2.
45
+ simulation:"DPM", "IRT2", "rand". Default= "DPM"
46
+ carsSimul:"no", "yes". Use simulation with or without cars. Default="no".
47
+ carsInput:"no", "yes". Take inputs with or without cars channel. Default="no".
48
+ IRT2maxW: in case of "rand" simulation, the maximal weight IRT2 can take. Default=1.
49
+ cityMap: "complete", "missing", "rand". Use the full city, or input map with missing buildings "rand" means that there is
50
+ a random number of missing buildings.
51
+ missing: 1 to 4. in case of input map with missing buildings, and not "rand", the number of missing buildings. Default=1.
52
+ transform: Transform to apply on the images of the loader. Default= transforms.ToTensor())
53
+
54
+ Output:
55
+ inputs: The RadioUNet inputs.
56
+ image_gain
57
+
58
+ """
59
+
60
+
61
+
62
+ #self.phase=phase
63
+
64
+ if maps_inds.size==1:
65
+ self.maps_inds=np.arange(0,700,1,dtype=np.int16)
66
+ #Determenistic "random" shuffle of the maps:
67
+ np.random.seed(42)
68
+ np.random.shuffle(self.maps_inds)
69
+ else:
70
+ self.maps_inds=maps_inds
71
+
72
+ if phase=="train":
73
+ self.ind1=0
74
+ self.ind2=500
75
+ elif phase=="val":
76
+ self.ind1=501
77
+ self.ind2=600
78
+ elif phase=="test":
79
+ self.ind1=601
80
+ self.ind2=699
81
+ else: # custom range
82
+ self.ind1=ind1
83
+ self.ind2=ind2
84
+
85
+ self.dir_dataset = dir_dataset
86
+ self.numTx= numTx
87
+ self.thresh=thresh
88
+
89
+ self.simulation=simulation
90
+ self.carsSimul=carsSimul
91
+ self.carsInput=carsInput
92
+ if simulation=="DPM" :
93
+ if carsSimul=="no":
94
+ self.dir_gain=self.dir_dataset+"gain/DPM/"
95
+ else:
96
+ self.dir_gain=self.dir_dataset+"gain/carsDPM/"
97
+ elif simulation=="IRT2":
98
+ if carsSimul=="no":
99
+ self.dir_gain=self.dir_dataset+"gain/IRT2/"
100
+ else:
101
+ self.dir_gain=self.dir_dataset+"gain/carsIRT2/"
102
+ elif simulation=="rand":
103
+ if carsSimul=="no":
104
+ self.dir_gainDPM=self.dir_dataset+"gain/DPM/"
105
+ self.dir_gainIRT2=self.dir_dataset+"gain/IRT2/"
106
+ else:
107
+ self.dir_gainDPM=self.dir_dataset+"gain/carsDPM/"
108
+ self.dir_gainIRT2=self.dir_dataset+"gain/carsIRT2/"
109
+
110
+ self.IRT2maxW=IRT2maxW
111
+
112
+ self.cityMap=cityMap
113
+ self.missing=missing
114
+ if cityMap=="complete":
115
+ self.dir_buildings=self.dir_dataset+"png/buildings_complete/"
116
+ else:
117
+ self.dir_buildings = self.dir_dataset+"png/buildings_missing" # a random index will be concatenated in the code
118
+ #else: #missing==number
119
+ # self.dir_buildings = self.dir_dataset+ "png/buildings_missing"+str(missing)+"/"
120
+
121
+
122
+ self.transform= transform
123
+
124
+ self.dir_Tx = self.dir_dataset+ "png/antennas/"
125
+ #later check if reading the JSON file and creating antenna images on the fly is faster
126
+ if carsInput!="no":
127
+ self.dir_cars = self.dir_dataset+ "png/cars/"
128
+
129
+ self.height = 256
130
+ self.width = 256
131
+
132
+
133
+ def __len__(self):
134
+ return (self.ind2-self.ind1+1)*self.numTx
135
+
136
+ def __getitem__(self, idx):
137
+
138
+ idxr=np.floor(idx/self.numTx).astype(int)
139
+ idxc=idx-idxr*self.numTx
140
+ dataset_map_ind=self.maps_inds[idxr+self.ind1]+1
141
+ #names of files that depend only on the map:
142
+ name1 = str(dataset_map_ind) + ".png"
143
+ #names of files that depend on the map and the Tx:
144
+ name2 = str(dataset_map_ind) + "_" + str(idxc) + ".png"
145
+
146
+ #Load buildings:
147
+ if self.cityMap == "complete":
148
+ img_name_buildings = os.path.join(self.dir_buildings, name1)
149
+ else:
150
+ if self.cityMap == "rand":
151
+ self.missing=np.random.randint(low=1, high=5)
152
+ version=np.random.randint(low=1, high=7)
153
+ img_name_buildings = os.path.join(self.dir_buildings+str(self.missing)+"/"+str(version)+"/", name1)
154
+ str(self.missing)
155
+ image_buildings = np.asarray(io.imread(img_name_buildings))
156
+
157
+ #Load Tx (transmitter):
158
+ img_name_Tx = os.path.join(self.dir_Tx, name2)
159
+ image_Tx = np.asarray(io.imread(img_name_Tx))
160
+
161
+ #Load radio map:
162
+ if self.simulation!="rand":
163
+ img_name_gain = os.path.join(self.dir_gain, name2)
164
+ image_gain = np.expand_dims(np.asarray(io.imread(img_name_gain)),axis=2)/255
165
+ else: #random weighted average of DPM and IRT2
166
+ img_name_gainDPM = os.path.join(self.dir_gainDPM, name2)
167
+ img_name_gainIRT2 = os.path.join(self.dir_gainIRT2, name2)
168
+ #image_gainDPM = np.expand_dims(np.asarray(io.imread(img_name_gainDPM)),axis=2)/255
169
+ #image_gainIRT2 = np.expand_dims(np.asarray(io.imread(img_name_gainIRT2)),axis=2)/255
170
+ w=np.random.uniform(0,self.IRT2maxW) # IRT2 weight of random average
171
+ image_gain= w*np.expand_dims(np.asarray(io.imread(img_name_gainIRT2)),axis=2)/256 \
172
+ + (1-w)*np.expand_dims(np.asarray(io.imread(img_name_gainDPM)),axis=2)/256
173
+
174
+ #pathloss threshold transform
175
+ if self.thresh>0:
176
+ mask = image_gain < self.thresh
177
+ image_gain[mask]=self.thresh
178
+ image_gain=image_gain-self.thresh*np.ones(np.shape(image_gain))
179
+ image_gain=image_gain/(1-self.thresh)
180
+
181
+
182
+ #inputs to radioUNet
183
+ if self.carsInput=="no":
184
+ inputs=np.stack([image_buildings, image_Tx], axis=2)
185
+ #The fact that the buildings and antenna are normalized 256 and not 1 promotes convergence,
186
+ #so we can use the same learning rate as RadioUNets
187
+ else: #cars
188
+ #Normalization, so all settings can have the same learning rate
189
+ image_buildings=image_buildings/256
190
+ image_Tx=image_Tx/256
191
+ img_name_cars = os.path.join(self.dir_cars, name1)
192
+ image_cars = np.asarray(io.imread(img_name_cars))/256
193
+ inputs=np.stack([image_buildings, image_Tx, image_cars], axis=2)
194
+ #note that ToTensor moves the channel from the last asix to the first!
195
+
196
+
197
+ if self.transform:
198
+ inputs = self.transform(inputs).type(torch.float32)
199
+ image_gain = self.transform(image_gain).type(torch.float32)
200
+ #note that ToTensor moves the channel from the last asix to the first!
201
+
202
+
203
+ return [inputs, image_gain]
204
+
205
+
206
+
207
+
208
+
209
+ class RadioUNet_c_sprseIRT4(Dataset):
210
+ """RadioMapSeer Loader for accurate buildings and no measurements (RadioUNet_c)"""
211
+ def __init__(self,maps_inds=np.zeros(1), phase="train",
212
+ ind1=0,ind2=0,
213
+ dir_dataset="RadioMapSeer/",
214
+ numTx=2,
215
+ thresh=0.2,
216
+ simulation="IRT4",
217
+ carsSimul="no",
218
+ carsInput="no",
219
+ cityMap="complete",
220
+ missing=1,
221
+ num_samples=300,
222
+ transform= transforms.ToTensor()):
223
+ """
224
+ Args:
225
+ maps_inds: optional shuffled sequence of the maps. Leave it as maps_inds=0 (default) for the standart split.
226
+ phase:"train", "val", "test", "custom". If "train", "val" or "test", uses a standard split.
227
+ "custom" means that the loader will read maps ind1 to ind2 from the list maps_inds.
228
+ ind1,ind2: First and last indices from maps_inds to define the maps of the loader, in case phase="custom".
229
+ dir_dataset: directory of the RadioMapSeer dataset.
230
+ numTx: Number of transmitters per map. Default = 2. Note that IRT4 works only with numTx<=2.
231
+ thresh: Pathlos threshold between 0 and 1. Defaoult is the noise floor 0.2.
232
+ simulation: default="IRT4", with an option to "DPM", "IRT2".
233
+ carsSimul:"no", "yes". Use simulation with or without cars. Default="no".
234
+ carsInput:"no", "yes". Take inputs with or without cars channel. Default="no".
235
+ cityMap: "complete", "missing", "rand". Use the full city, or input map with missing buildings "rand" means that there is
236
+ a random number of missing buildings.
237
+ missing: 1 to 4. in case of input map with missing buildings, and not "rand", the number of missing buildings. Default=1.
238
+ num_samples: number of samples in the sparse IRT4 radio map. Default=300.
239
+ transform: Transform to apply on the images of the loader. Default= transforms.ToTensor())
240
+
241
+ Output:
242
+
243
+ """
244
+ if maps_inds.size==1:
245
+ self.maps_inds=np.arange(0,700,1,dtype=np.int16)
246
+ #Determenistic "random" shuffle of the maps:
247
+ np.random.seed(42)
248
+ np.random.shuffle(self.maps_inds)
249
+ else:
250
+ self.maps_inds=maps_inds
251
+
252
+ if phase=="train":
253
+ self.ind1=0
254
+ self.ind2=500
255
+ elif phase=="val":
256
+ self.ind1=501
257
+ self.ind2=600
258
+ elif phase=="test":
259
+ self.ind1=601
260
+ self.ind2=699
261
+ else: # custom range
262
+ self.ind1=ind1
263
+ self.ind2=ind2
264
+
265
+ self.dir_dataset = dir_dataset
266
+ self.numTx= numTx
267
+ self.thresh=thresh
268
+
269
+ self.simulation=simulation
270
+ self.carsSimul=carsSimul
271
+ self.carsInput=carsInput
272
+ if simulation=="IRT4":
273
+ if carsSimul=="no":
274
+ self.dir_gain=self.dir_dataset+"gain/IRT4/"
275
+ else:
276
+ self.dir_gain=self.dir_dataset+"gain/carsIRT4/"
277
+
278
+ elif simulation=="DPM" :
279
+ if carsSimul=="no":
280
+ self.dir_gain=self.dir_dataset+"gain/DPM/"
281
+ else:
282
+ self.dir_gain=self.dir_dataset+"gain/carsDPM/"
283
+ elif simulation=="IRT2":
284
+ if carsSimul=="no":
285
+ self.dir_gain=self.dir_dataset+"gain/IRT2/"
286
+ else:
287
+ self.dir_gain=self.dir_dataset+"gain/carsIRT2/"
288
+
289
+
290
+ self.cityMap=cityMap
291
+ self.missing=missing
292
+ if cityMap=="complete":
293
+ self.dir_buildings=self.dir_dataset+"png/buildings_complete/"
294
+ else:
295
+ self.dir_buildings = self.dir_dataset+"png/buildings_missing" # a random index will be concatenated in the code
296
+ #else: #missing==number
297
+ # self.dir_buildings = self.dir_dataset+ "png/buildings_missing"+str(missing)+"/"
298
+
299
+
300
+ self.transform= transform
301
+
302
+ self.num_samples=num_samples
303
+
304
+ self.dir_Tx = self.dir_dataset+ "png/antennas/"
305
+ #later check if reading the JSON file and creating antenna images on the fly is faster
306
+ if carsInput!="no":
307
+ self.dir_cars = self.dir_dataset+ "png/cars/"
308
+
309
+ self.height = 256
310
+ self.width = 256
311
+
312
+
313
+
314
+
315
+
316
+ def __len__(self):
317
+ return (self.ind2-self.ind1+1)*self.numTx
318
+
319
+ def __getitem__(self, idx):
320
+
321
+ idxr=np.floor(idx/self.numTx).astype(int)
322
+ idxc=idx-idxr*self.numTx
323
+ dataset_map_ind=self.maps_inds[idxr+self.ind1]+1
324
+ #names of files that depend only on the map:
325
+ name1 = str(dataset_map_ind) + ".png"
326
+ #names of files that depend on the map and the Tx:
327
+ name2 = str(dataset_map_ind) + "_" + str(idxc) + ".png"
328
+
329
+ #Load buildings:
330
+ if self.cityMap == "complete":
331
+ img_name_buildings = os.path.join(self.dir_buildings, name1)
332
+ else:
333
+ if self.cityMap == "rand":
334
+ self.missing=np.random.randint(low=1, high=5)
335
+ version=np.random.randint(low=1, high=7)
336
+ img_name_buildings = os.path.join(self.dir_buildings+str(self.missing)+"/"+str(version)+"/", name1)
337
+ str(self.missing)
338
+ image_buildings = np.asarray(io.imread(img_name_buildings))
339
+
340
+ #Load Tx (transmitter):
341
+ img_name_Tx = os.path.join(self.dir_Tx, name2)
342
+ image_Tx = np.asarray(io.imread(img_name_Tx))
343
+
344
+ #Load radio map:
345
+ if self.simulation!="rand":
346
+ img_name_gain = os.path.join(self.dir_gain, name2)
347
+ image_gain = np.expand_dims(np.asarray(io.imread(img_name_gain)),axis=2)/256
348
+ else: #random weighted average of DPM and IRT2
349
+ img_name_gainDPM = os.path.join(self.dir_gainDPM, name2)
350
+ img_name_gainIRT2 = os.path.join(self.dir_gainIRT2, name2)
351
+ #image_gainDPM = np.expand_dims(np.asarray(io.imread(img_name_gainDPM)),axis=2)/255
352
+ #image_gainIRT2 = np.expand_dims(np.asarray(io.imread(img_name_gainIRT2)),axis=2)/255
353
+ w=np.random.uniform(0,self.IRT2maxW) # IRT2 weight of random average
354
+ image_gain= w*np.expand_dims(np.asarray(io.imread(img_name_gainIRT2)),axis=2)/256 \
355
+ + (1-w)*np.expand_dims(np.asarray(io.imread(img_name_gainDPM)),axis=2)/256
356
+
357
+ #pathloss threshold transform
358
+ if self.thresh>0:
359
+ mask = image_gain < self.thresh
360
+ image_gain[mask]=self.thresh
361
+ image_gain=image_gain-self.thresh*np.ones(np.shape(image_gain))
362
+ image_gain=image_gain/(1-self.thresh)
363
+
364
+ #Saprse IRT4 samples, determenistic and fixed samples per map
365
+ image_samples = np.zeros((self.width,self.height))
366
+ seed_map=np.sum(image_buildings) # Each map has its fixed samples, independent of the transmitter location.
367
+ np.random.seed(seed_map)
368
+ x_samples=np.random.randint(0, 255, size=self.num_samples)
369
+ y_samples=np.random.randint(0, 255, size=self.num_samples)
370
+ image_samples[x_samples,y_samples]= 1
371
+
372
+ #inputs to radioUNet
373
+ if self.carsInput=="no":
374
+ inputs=np.stack([image_buildings, image_Tx], axis=2)
375
+ #The fact that the buildings and antenna are normalized 256 and not 1 promotes convergence,
376
+ #so we can use the same learning rate as RadioUNets
377
+ else: #cars
378
+ #Normalization, so all settings can have the same learning rate
379
+ image_buildings=image_buildings/256
380
+ image_Tx=image_Tx/256
381
+ img_name_cars = os.path.join(self.dir_cars, name1)
382
+ image_cars = np.asarray(io.imread(img_name_cars))/256
383
+ inputs=np.stack([image_buildings, image_Tx, image_cars], axis=2)
384
+ #note that ToTensor moves the channel from the last asix to the first!
385
+
386
+
387
+
388
+
389
+ if self.transform:
390
+ inputs = self.transform(inputs).type(torch.float32)
391
+ image_gain = self.transform(image_gain).type(torch.float32)
392
+ image_samples = self.transform(image_samples).type(torch.float32)
393
+
394
+
395
+ return [inputs, image_gain, image_samples]
396
+
397
+
398
+
399
+
400
+
401
+
402
+
403
+ class RadioUNet_s(Dataset):
404
+ """RadioMapSeer Loader for accurate buildings and no measurements (RadioUNet_c)"""
405
+ def __init__(self,maps_inds=np.zeros(1), phase="train",
406
+ ind1=0,ind2=0,
407
+ dir_dataset="RadioMapSeer/",
408
+ numTx=80,
409
+ thresh=0.2,
410
+ simulation="DPM",
411
+ carsSimul="no",
412
+ carsInput="no",
413
+ IRT2maxW=1,
414
+ cityMap="complete",
415
+ missing=1,
416
+ fix_samples=0,
417
+ num_samples_low= 10,
418
+ num_samples_high= 300,
419
+ transform= transforms.ToTensor()):
420
+ """
421
+ Args:
422
+ maps_inds: optional shuffled sequence of the maps. Leave it as maps_inds=0 (default) for the standart split.
423
+ phase:"train", "val", "test", "custom". If "train", "val" or "test", uses a standard split.
424
+ "custom" means that the loader will read maps ind1 to ind2 from the list maps_inds.
425
+ ind1,ind2: First and last indices from maps_inds to define the maps of the loader, in case phase="custom".
426
+ dir_dataset: directory of the RadioMapSeer dataset.
427
+ numTx: Number of transmitters per map. Default and maximal value of numTx = 80.
428
+ thresh: Pathlos threshold between 0 and 1. Defaoult is the noise floor 0.2.
429
+ simulation:"DPM", "IRT2", "rand". Default= "DPM"
430
+ carsSimul:"no", "yes". Use simulation with or without cars. Default="no".
431
+ carsInput:"no", "yes". Take inputs with or without cars channel. Default="no".
432
+ IRT2maxW: in case of "rand" simulation, the maximal weight IRT2 can take. Default=1.
433
+ cityMap: "complete", "missing", "rand". Use the full city, or input map with missing buildings "rand" means that there is
434
+ a random number of missing buildings.
435
+ missing: 1 to 4. in case of input map with missing buildings, and not "rand", the number of missing buildings. Default=1.
436
+ fix_samples: fixed or a random number of samples. If zero, fixed, else, fix_samples is the number of samples. Default = 0.
437
+ num_samples_low: if random number of samples, this is the minimum number of samples. Default = 10.
438
+ num_samples_high: if random number of samples, this is the maximal number of samples. Default = 300.
439
+ transform: Transform to apply on the images of the loader. Default= transforms.ToTensor())
440
+
441
+ Output:
442
+ inputs: The RadioUNet inputs.
443
+ image_gain
444
+
445
+ """
446
+
447
+
448
+
449
+ #self.phase=phase
450
+
451
+ if maps_inds.size==1:
452
+ self.maps_inds=np.arange(0,700,1,dtype=np.int16)
453
+ #Determenistic "random" shuffle of the maps:
454
+ np.random.seed(42)
455
+ np.random.shuffle(self.maps_inds)
456
+ else:
457
+ self.maps_inds=maps_inds
458
+
459
+ if phase=="train":
460
+ self.ind1=0
461
+ self.ind2=500
462
+ elif phase=="val":
463
+ self.ind1=501
464
+ self.ind2=600
465
+ elif phase=="test":
466
+ self.ind1=601
467
+ self.ind2=699
468
+ else: # custom range
469
+ self.ind1=ind1
470
+ self.ind2=ind2
471
+
472
+ self.dir_dataset = dir_dataset
473
+ self.numTx= numTx
474
+ self.thresh=thresh
475
+
476
+ self.simulation=simulation
477
+ self.carsSimul=carsSimul
478
+ self.carsInput=carsInput
479
+ if simulation=="DPM" :
480
+ if carsSimul=="no":
481
+ self.dir_gain=self.dir_dataset+"gain/DPM/"
482
+ else:
483
+ self.dir_gain=self.dir_dataset+"gain/carsDPM/"
484
+ elif simulation=="IRT2":
485
+ if carsSimul=="no":
486
+ self.dir_gain=self.dir_dataset+"gain/IRT2/"
487
+ else:
488
+ self.dir_gain=self.dir_dataset+"gain/carsIRT2/"
489
+ elif simulation=="rand":
490
+ if carsSimul=="no":
491
+ self.dir_gainDPM=self.dir_dataset+"gain/DPM/"
492
+ self.dir_gainIRT2=self.dir_dataset+"gain/IRT2/"
493
+ else:
494
+ self.dir_gainDPM=self.dir_dataset+"gain/carsDPM/"
495
+ self.dir_gainIRT2=self.dir_dataset+"gain/carsIRT2/"
496
+
497
+ self.IRT2maxW=IRT2maxW
498
+
499
+ self.cityMap=cityMap
500
+ self.missing=missing
501
+ if cityMap=="complete":
502
+ self.dir_buildings=self.dir_dataset+"png/buildings_complete/"
503
+ else:
504
+ self.dir_buildings = self.dir_dataset+"png/buildings_missing" # a random index will be concatenated in the code
505
+ #else: #missing==number
506
+ # self.dir_buildings = self.dir_dataset+ "png/buildings_missing"+str(missing)+"/"
507
+
508
+
509
+ self.fix_samples= fix_samples
510
+ self.num_samples_low= num_samples_low
511
+ self.num_samples_high= num_samples_high
512
+
513
+ self.transform= transform
514
+
515
+ self.dir_Tx = self.dir_dataset+ "png/antennas/"
516
+ #later check if reading the JSON file and creating antenna images on the fly is faster
517
+ if carsInput!="no":
518
+ self.dir_cars = self.dir_dataset+ "png/cars/"
519
+
520
+ self.height = 256
521
+ self.width = 256
522
+
523
+
524
+ def __len__(self):
525
+ return (self.ind2-self.ind1+1)*self.numTx
526
+
527
+ def __getitem__(self, idx):
528
+
529
+ idxr=np.floor(idx/self.numTx).astype(int)
530
+ idxc=idx-idxr*self.numTx
531
+ dataset_map_ind=self.maps_inds[idxr+self.ind1]+1
532
+ #names of files that depend only on the map:
533
+ name1 = str(dataset_map_ind) + ".png"
534
+ #names of files that depend on the map and the Tx:
535
+ name2 = str(dataset_map_ind) + "_" + str(idxc) + ".png"
536
+
537
+ #Load buildings:
538
+ if self.cityMap == "complete":
539
+ img_name_buildings = os.path.join(self.dir_buildings, name1)
540
+ else:
541
+ if self.cityMap == "rand":
542
+ self.missing=np.random.randint(low=1, high=5)
543
+ version=np.random.randint(low=1, high=7)
544
+ img_name_buildings = os.path.join(self.dir_buildings+str(self.missing)+"/"+str(version)+"/", name1)
545
+ str(self.missing)
546
+ image_buildings = np.asarray(io.imread(img_name_buildings))/256
547
+
548
+ #Load Tx (transmitter):
549
+ img_name_Tx = os.path.join(self.dir_Tx, name2)
550
+ image_Tx = np.asarray(io.imread(img_name_Tx))/256
551
+
552
+ #Load radio map:
553
+ if self.simulation!="rand":
554
+ img_name_gain = os.path.join(self.dir_gain, name2)
555
+ image_gain = np.expand_dims(np.asarray(io.imread(img_name_gain)),axis=2)/256
556
+ else: #random weighted average of DPM and IRT2
557
+ img_name_gainDPM = os.path.join(self.dir_gainDPM, name2)
558
+ img_name_gainIRT2 = os.path.join(self.dir_gainIRT2, name2)
559
+ #image_gainDPM = np.expand_dims(np.asarray(io.imread(img_name_gainDPM)),axis=2)/255
560
+ #image_gainIRT2 = np.expand_dims(np.asarray(io.imread(img_name_gainIRT2)),axis=2)/255
561
+ w=np.random.uniform(0,self.IRT2maxW) # IRT2 weight of random average
562
+ image_gain= w*np.expand_dims(np.asarray(io.imread(img_name_gainIRT2)),axis=2)/256 \
563
+ + (1-w)*np.expand_dims(np.asarray(io.imread(img_name_gainDPM)),axis=2)/256
564
+
565
+ #pathloss threshold transform
566
+ if self.thresh>0:
567
+ mask = image_gain < self.thresh
568
+ image_gain[mask]=self.thresh
569
+ image_gain=image_gain-self.thresh*np.ones(np.shape(image_gain))
570
+ image_gain=image_gain/(1-self.thresh)
571
+
572
+ image_gain=image_gain*256 # we use this normalization so all RadioUNet methods can have the same learning rate.
573
+ # Namely, the loss of RadioUNet_s is 256 the loss of RadioUNet_c
574
+ # Important: when evaluating the accuracy, remember to devide the errors by 256!
575
+
576
+ #input measurements
577
+ image_samples = np.zeros((256,256))
578
+ if self.fix_samples==0:
579
+ num_samples=np.random.randint(self.num_samples_low, self.num_samples_high, size=1)
580
+ else:
581
+ num_samples=np.floor(self.fix_samples).astype(int)
582
+ x_samples=np.random.randint(0, 255, size=num_samples)
583
+ y_samples=np.random.randint(0, 255, size=num_samples)
584
+ image_samples[x_samples,y_samples]= image_gain[x_samples,y_samples,0]
585
+
586
+ #inputs to radioUNet
587
+ if self.carsInput=="no":
588
+ inputs=np.stack([image_buildings, image_Tx, image_samples], axis=2)
589
+ #The fact that the buildings and antenna are normalized 256 and not 1 promotes convergence,
590
+ #so we can use the same learning rate as RadioUNets
591
+ else: #cars
592
+ #Normalization, so all settings can have the same learning rate
593
+ img_name_cars = os.path.join(self.dir_cars, name1)
594
+ image_cars = np.asarray(io.imread(img_name_cars))/256
595
+ inputs=np.stack([image_buildings, image_Tx, image_samples, image_cars], axis=2)
596
+ #note that ToTensor moves the channel from the last asix to the first!
597
+
598
+
599
+
600
+ if self.transform:
601
+ inputs = self.transform(inputs).type(torch.float32)
602
+ image_gain = self.transform(image_gain).type(torch.float32)
603
+ #note that ToTensor moves the channel from the last asix to the first!
604
+
605
+
606
+ return [inputs, image_gain]
607
+
608
+
609
+
610
+
611
+
612
+ class RadioUNet_s_sprseIRT4(Dataset):
613
+ """RadioMapSeer Loader for accurate buildings and no measurements (RadioUNet_c)"""
614
+ def __init__(self,maps_inds=np.zeros(1), phase="train",
615
+ ind1=0,ind2=0,
616
+ dir_dataset="RadioMapSeer/",
617
+ numTx=2,
618
+ thresh=0.2,
619
+ simulation="IRT4",
620
+ carsSimul="no",
621
+ carsInput="no",
622
+ cityMap="complete",
623
+ missing=1,
624
+ data_samples=300,
625
+ fix_samples=0,
626
+ num_samples_low= 10,
627
+ num_samples_high= 299,
628
+ transform= transforms.ToTensor()):
629
+ """
630
+ Args:
631
+ maps_inds: optional shuffled sequence of the maps. Leave it as maps_inds=0 (default) for the standart split.
632
+ phase:"train", "val", "test", "custom". If "train", "val" or "test", uses a standard split.
633
+ "custom" means that the loader will read maps ind1 to ind2 from the list maps_inds.
634
+ ind1,ind2: First and last indices from maps_inds to define the maps of the loader, in case phase="custom".
635
+ dir_dataset: directory of the RadioMapSeer dataset.
636
+ numTx: Number of transmitters per map. Default = 2. Note that IRT4 works only with numTx<=2.
637
+ thresh: Pathlos threshold between 0 and 1. Defaoult is the noise floor 0.2.
638
+ simulation: default="IRT4", with an option to "DPM", "IRT2".
639
+ carsSimul:"no", "yes". Use simulation with or without cars. Default="no".
640
+ carsInput:"no", "yes". Take inputs with or without cars channel. Default="no".
641
+ cityMap: "complete", "missing", "rand". Use the full city, or input map with missing buildings "rand" means that there is
642
+ a random number of missing buildings.
643
+ missing: 1 to 4. in case of input map with missing buildings, and not "rand", the number of missing buildings. Default=1.
644
+ data_samples: number of samples in the sparse IRT4 radio map. Default=300. All input samples are taken from the data_samples
645
+ fix_samples: fixed or a random number of samples. If zero, fixed, else, fix_samples is the number of samples. Default = 0.
646
+ num_samples_low: if random number of samples, this is the minimum number of samples. Default = 10.
647
+ num_samples_high: if random number of samples, this is the maximal number of samples. Default = 300.
648
+ transform: Transform to apply on the images of the loader. Default= transforms.ToTensor())
649
+
650
+ Output:
651
+
652
+ """
653
+ if maps_inds.size==1:
654
+ self.maps_inds=np.arange(0,700,1,dtype=np.int16)
655
+ #Determenistic "random" shuffle of the maps:
656
+ np.random.seed(42)
657
+ np.random.shuffle(self.maps_inds)
658
+ else:
659
+ self.maps_inds=maps_inds
660
+
661
+ if phase=="train":
662
+ self.ind1=0
663
+ self.ind2=500
664
+ elif phase=="val":
665
+ self.ind1=501
666
+ self.ind2=600
667
+ elif phase=="test":
668
+ self.ind1=601
669
+ self.ind2=699
670
+ else: # custom range
671
+ self.ind1=ind1
672
+ self.ind2=ind2
673
+
674
+ self.dir_dataset = dir_dataset
675
+ self.numTx= numTx
676
+ self.thresh=thresh
677
+
678
+ self.simulation=simulation
679
+ self.carsSimul=carsSimul
680
+ self.carsInput=carsInput
681
+ if simulation=="IRT4":
682
+ if carsSimul=="no":
683
+ self.dir_gain=self.dir_dataset+"gain/IRT4/"
684
+ else:
685
+ self.dir_gain=self.dir_dataset+"gain/carsIRT4/"
686
+
687
+ elif simulation=="DPM" :
688
+ if carsSimul=="no":
689
+ self.dir_gain=self.dir_dataset+"gain/DPM/"
690
+ else:
691
+ self.dir_gain=self.dir_dataset+"gain/carsDPM/"
692
+ elif simulation=="IRT2":
693
+ if carsSimul=="no":
694
+ self.dir_gain=self.dir_dataset+"gain/IRT2/"
695
+ else:
696
+ self.dir_gain=self.dir_dataset+"gain/carsIRT2/"
697
+
698
+
699
+ self.cityMap=cityMap
700
+ self.missing=missing
701
+ if cityMap=="complete":
702
+ self.dir_buildings=self.dir_dataset+"png/buildings_complete/"
703
+ else:
704
+ self.dir_buildings = self.dir_dataset+"png/buildings_missing" # a random index will be concatenated in the code
705
+ #else: #missing==number
706
+ # self.dir_buildings = self.dir_dataset+ "png/buildings_missing"+str(missing)+"/"
707
+
708
+
709
+ self.data_samples=data_samples
710
+ self.fix_samples= fix_samples
711
+ self.num_samples_low= num_samples_low
712
+ self.num_samples_high= num_samples_high
713
+
714
+ self.transform= transform
715
+
716
+
717
+ self.dir_Tx = self.dir_dataset+ "png/antennas/"
718
+ #later check if reading the JSON file and creating antenna images on the fly is faster
719
+ if carsInput!="no":
720
+ self.dir_cars = self.dir_dataset+ "png/cars/"
721
+
722
+ self.height = 256
723
+ self.width = 256
724
+
725
+
726
+
727
+
728
+
729
+ def __len__(self):
730
+ return (self.ind2-self.ind1+1)*self.numTx
731
+
732
+ def __getitem__(self, idx):
733
+
734
+ idxr=np.floor(idx/self.numTx).astype(int)
735
+ idxc=idx-idxr*self.numTx
736
+ dataset_map_ind=self.maps_inds[idxr+self.ind1]+1
737
+ #names of files that depend only on the map:
738
+ name1 = str(dataset_map_ind) + ".png"
739
+ #names of files that depend on the map and the Tx:
740
+ name2 = str(dataset_map_ind) + "_" + str(idxc) + ".png"
741
+
742
+ #Load buildings:
743
+ if self.cityMap == "complete":
744
+ img_name_buildings = os.path.join(self.dir_buildings, name1)
745
+ else:
746
+ if self.cityMap == "rand":
747
+ self.missing=np.random.randint(low=1, high=5)
748
+ version=np.random.randint(low=1, high=7)
749
+ img_name_buildings = os.path.join(self.dir_buildings+str(self.missing)+"/"+str(version)+"/", name1)
750
+ str(self.missing)
751
+ image_buildings = np.asarray(io.imread(img_name_buildings)) #Will be normalized later, after random seed is computed from it
752
+
753
+ #Load Tx (transmitter):
754
+ img_name_Tx = os.path.join(self.dir_Tx, name2)
755
+ image_Tx = np.asarray(io.imread(img_name_Tx))/256
756
+
757
+ #Load radio map:
758
+ if self.simulation!="rand":
759
+ img_name_gain = os.path.join(self.dir_gain, name2)
760
+ image_gain = np.expand_dims(np.asarray(io.imread(img_name_gain)),axis=2)/256
761
+ else: #random weighted average of DPM and IRT2
762
+ img_name_gainDPM = os.path.join(self.dir_gainDPM, name2)
763
+ img_name_gainIRT2 = os.path.join(self.dir_gainIRT2, name2)
764
+ #image_gainDPM = np.expand_dims(np.asarray(io.imread(img_name_gainDPM)),axis=2)/255
765
+ #image_gainIRT2 = np.expand_dims(np.asarray(io.imread(img_name_gainIRT2)),axis=2)/255
766
+ w=np.random.uniform(0,self.IRT2maxW) # IRT2 weight of random average
767
+ image_gain= w*np.expand_dims(np.asarray(io.imread(img_name_gainIRT2)),axis=2)/256 \
768
+ + (1-w)*np.expand_dims(np.asarray(io.imread(img_name_gainDPM)),axis=2)/256
769
+
770
+ #pathloss threshold transform
771
+ if self.thresh>0:
772
+ mask = image_gain < self.thresh
773
+ image_gain[mask]=self.thresh
774
+ image_gain=image_gain-self.thresh*np.ones(np.shape(image_gain))
775
+ image_gain=image_gain/(1-self.thresh)
776
+
777
+ image_gain=image_gain*256 # we use this normalization so all RadioUNet methods can have the same learning rate.
778
+ # Namely, the loss of RadioUNet_s is 256 the loss of RadioUNet_c
779
+ # Important: when evaluating the accuracy, remember to devide the errors by 256!
780
+
781
+ #Saprse IRT4 samples, determenistic and fixed samples per map
782
+ sparse_samples = np.zeros((self.width,self.height))
783
+ seed_map=np.sum(image_buildings) # Each map has its fixed samples, independent of the transmitter location.
784
+ np.random.seed(seed_map)
785
+ x_samples=np.random.randint(0, 255, size=self.data_samples)
786
+ y_samples=np.random.randint(0, 255, size=self.data_samples)
787
+ sparse_samples[x_samples,y_samples]= 1
788
+
789
+ #input samples from the sparse gain samples
790
+ input_samples = np.zeros((256,256))
791
+ if self.fix_samples==0:
792
+ num_in_samples=np.random.randint(self.num_samples_low, self.num_samples_high, size=1)
793
+ else:
794
+ num_in_samples=np.floor(self.fix_samples).astype(int)
795
+
796
+ data_inds=range(self.data_samples)
797
+ input_inds=np.random.permutation(data_inds)[0:num_in_samples[0]]
798
+ x_samples_in=x_samples[input_inds]
799
+ y_samples_in=y_samples[input_inds]
800
+ input_samples[x_samples_in,y_samples_in]= image_gain[x_samples_in,y_samples_in,0]
801
+
802
+ #normalize image_buildings, after random seed computed from it as an int
803
+ image_buildings=image_buildings/256
804
+
805
+ #inputs to radioUNet
806
+ if self.carsInput=="no":
807
+ inputs=np.stack([image_buildings, image_Tx, input_samples], axis=2)
808
+ #The fact that the buildings and antenna are normalized 256 and not 1 promotes convergence,
809
+ #so we can use the same learning rate as RadioUNets
810
+ else: #cars
811
+ #Normalization, so all settings can have the same learning rate
812
+ img_name_cars = os.path.join(self.dir_cars, name1)
813
+ image_cars = np.asarray(io.imread(img_name_cars))/256
814
+ inputs=np.stack([image_buildings, image_Tx, input_samples, image_cars], axis=2)
815
+ #note that ToTensor moves the channel from the last asix to the first!
816
+
817
+
818
+
819
+
820
+ if self.transform:
821
+ inputs = self.transform(inputs).type(torch.float32)
822
+ image_gain = self.transform(image_gain).type(torch.float32)
823
+ sparse_samples = self.transform(sparse_samples).type(torch.float32)
824
+
825
+
826
+
827
+ return [inputs, image_gain, sparse_samples]
828
+
829
+
830
+
831
+
832
+
833
+
834
+
835
+
836
+