Skip to content

Commit 8e45390

Browse files
committed
Store tiles/patches in data/output and read the same as numpy
1 parent ff87f18 commit 8e45390

File tree

5 files changed

+71
-2
lines changed

5 files changed

+71
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
data/output/*
2+
__pycache__/*

maps.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import torch
77
import numpy as np
88
import random
9+
import dill
10+
911

1012
# Credit - Bahar
1113
class MapReader:
@@ -36,11 +38,12 @@ def get_char(self, layer):
3638
# Modifications - Ishaan
3739

3840
class MapsDataset(Dataset):
39-
def __init__(self, window_size, step_size, converter):
41+
def __init__(self, window_size, step_size, sample_group_size, converter):
4042
self.char_size = converter.char_size
4143
self.converter = converter
4244
self.window_size = window_size
4345
self.step_size = step_size
46+
self.sample_group_size = sample_group_size
4447
self.samples = []
4548
self.block_size = self.window_size[0] * self.window_size[1] - 1
4649

@@ -62,6 +65,25 @@ def add(self, mapReader):
6265
for y in range(self.window_size[1])]
6366
for x in range(self.window_size[0])])
6467

68+
#Generate image patches and write to data/output directory
69+
def generate_patches(self, mapReader, image_groups=3):
70+
img_group_number = 0
71+
for i in range(0, mapReader.size[0] - self.window_size[0] + 1, self.step_size):
72+
for j in range(0, mapReader.size[1] - self.window_size[1] + 1, self.step_size):
73+
self.samples.append([[
74+
(self.converter.get_char(mapReader.data[i + x][j + y]) / (len(self.converter.char_groups) - 1)) * -2 + 1
75+
for y in range(self.window_size[1])]
76+
for x in range(self.window_size[0])])
77+
78+
if len(self.samples)==self.sample_group_size:
79+
with open("data/output/SF_group"+str(img_group_number)+".dill", 'wb+') as f:
80+
dill.dump(self.samples, f)
81+
f.close()
82+
print("Image group {} saved in data/output/ !".format(img_group_number))
83+
self.samples.clear()
84+
img_group_number+=1
85+
86+
6587
def shuffle(self):
6688
random.shuffle(self.samples)
6789

read.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Script to read dill data objects as numpy arrays.
2+
import dill
3+
import numpy as np
4+
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+self.location+"_group"+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+self.location+"_group"+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+
reader = ImgGroupReader("data/output/")
34+
reader.load_group()
35+
data = reader.read_numpy()
36+
im = Image.fromarray(data[0])
37+
im.show()
38+
39+

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
matplotlib==3.5.3
1+
dill==0.3.5.1
22
numpy==1.23.3
33
Pillow==9.2.0
44
torch==1.12.1

run.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Run this script to generate data in /output directory.
2+
import maps
3+
4+
sfMap = maps.MapReader('data/input/sf_layered.txt')
5+
mapsDataset = maps.MapsDataset((32, 32), 2, 1280, maps.single_layer_converter) #Third parameter is the group size
6+
mapsDataset.generate_patches(sfMap) #This will generate dill files which contain the saved sample lists.

0 commit comments

Comments
 (0)