Skip to content

Commit 3390835

Browse files
committed
new reader
1 parent 4ab7d7f commit 3390835

File tree

6 files changed

+70
-45
lines changed

6 files changed

+70
-45
lines changed

ChangeLog.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Oct 14, 2022
2+
1. generate_patches has a new parameter for output directory
3+
2. default output directory now has a meaningful structure with patch sizes and step sizes
4+
3. Refactored ImageGroupReader

src/__init__.py

Whitespace-only changes.

src/lib/ImageGroupReader.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import logging
2+
import dill
3+
import numpy as np
4+
import os
5+
import logging
6+
7+
class ImageGroupReader:
8+
def __init__(self, directory_path, location='SF'):
9+
self.data = []
10+
self.dir_path = directory_path
11+
self.location = location
12+
13+
def getGroupPath(self, groupNo):
14+
return os.path.join(self.dir_path, f"{groupNo}.dill")
15+
16+
def load_group(self, groupNo=0):
17+
with open(self.getGroupPath(groupNo), 'rb') as d:
18+
return dill.load(d)
19+
20+
21+
def groupAsImg(self, group):
22+
"""Assumes the raw data is standardized. So, we project it back into PIL RGB image format
23+
24+
Returns:
25+
_type_: _description_
26+
"""
27+
# print(data[0])
28+
data = np.asarray(group)
29+
data = np.clip((data + 1) / 2, 0, 1) * 255
30+
data = data.astype(np.uint8)
31+
return data
32+
33+
def asImg(self, patch):
34+
data = np.clip((patch + 1) / 2, 0, 1) * 255
35+
data = data.astype(np.uint8)
36+
return data

src/lib/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .ImageGroupReader import ImageGroupReader

src/maps.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def generate_patches(self, mapReader, image_groups=3, outDirectory=None):
9494
mapReader.standardize(converter=self.converter)
9595

9696
if outDirectory is None:
97-
outDirectory = os.path.join(self.outputDir, mapReader.mapName, f"{self.window_size[0]}x{self.window_size[1]}", f"{self.step_size}x{self.step_size}")
97+
outDirectory = os.path.join(self.outputDir, mapReader.mapName, f"{self.window_size[0]}x{self.window_size[1]}", f"stride-{self.step_size}")
9898
os.makedirs(outDirectory, exist_ok=True)
9999

100100
img_group_number = 0
@@ -118,21 +118,21 @@ def generate_patches(self, mapReader, image_groups=3, outDirectory=None):
118118
def extractSample(self, mapReader, topLeft):
119119
i = topLeft[0]
120120
j = topLeft[1]
121-
# sample = [
122-
# [
123-
# (self.converter.get_char(mapReader.data[i + x][j + y]) / (len(self.converter.char_groups) - 1)) * -2 + 1 # TODO this conversion should be done once in the original data instead of patches.
124-
# for y in range(self.window_size[1])
125-
# ]
126-
# for x in range(self.window_size[0])
127-
# ]
128121
sample = [
129122
[
130-
mapReader.data[i + x][j + y]
123+
(self.converter.get_char(mapReader.data[i + x][j + y]) / (len(self.converter.char_groups) - 1)) * -2 + 1 # TODO this conversion should be done once in the original data instead of patches.
131124
for y in range(self.window_size[1])
132125
]
133126
for x in range(self.window_size[0])
134127
]
135-
return sample
128+
# sample = [
129+
# [
130+
# mapReader.data[i + x][j + y]
131+
# for y in range(self.window_size[1])
132+
# ]
133+
# for x in range(self.window_size[0])
134+
# ]
135+
return np.asarray(sample)
136136

137137

138138
def shuffle(self):

src/read.py

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,25 @@
22
import dill
33
import numpy as np
44
from PIL import Image
5-
import torch
6-
7-
8-
class ImgGroupReader:
9-
def __init__(self, directory_path, location='SF'):
10-
self.data = []
11-
self.dir_path = directory_path
12-
self.location = location
13-
14-
def load_group(self, group=0):
15-
if self.dir_path[-1]=='/':
16-
with open(self.dir_path+str(group)+".dill", 'rb') as d:
17-
self.data = dill.load(d)
18-
d.close()
19-
else:
20-
self.dir_path = self.dir_path + "/"
21-
with open(self.dir_path+str(group)+".dill", 'rb') as d:
22-
self.data = dill.load(d)
23-
d.close()
24-
25-
def read_numpy(self):
26-
if(self.data == []): return print("Image group file not loaded! Call load_image_group() first.")
27-
else:
28-
self.data = np.array(self.data)
29-
self.data = np.clip((self.data+1)/2, 0, 1)*255
30-
self.data = self.data.astype(np.uint8)
31-
return self.data
32-
33-
sf_groups = 117
34-
for i in range(sf_groups):
35-
reader = ImgGroupReader("../data/output/city_patches/dill/washington")
36-
reader.load_group(group=i)
37-
data = reader.read_numpy()
38-
im = Image.fromarray(data[0])
39-
im.save('../data/output/city_patches/png/washington/'+str(i)+'.png')
5+
import os
6+
7+
from lib import ImageGroupReader
8+
9+
dillFolder = "../data/output/SF_Layered/32x32/stride-10"
10+
nGroups = 0
11+
# Iterate directory
12+
for path in os.listdir(dillFolder):
13+
# check if current path is a file
14+
if os.path.isfile(os.path.join(dillFolder, path)) and path.endswith(".dill"):
15+
nGroups += 1
16+
17+
for i in range(nGroups):
18+
reader = ImageGroupReader(dillFolder)
19+
data = reader.load_group(groupNo=i)
20+
patchImgArray = reader.asImg(data[0])
21+
im = Image.fromarray(patchImgArray)
22+
path = os.path.join(dillFolder, f"{i}.png")
23+
im.save(path)
4024

4125

4226

0 commit comments

Comments
 (0)