-
Notifications
You must be signed in to change notification settings - Fork 66
/
make_hdf5_synthetic_circles_and_boxes.ipynb
170 lines (170 loc) · 5.36 KB
/
make_hdf5_synthetic_circles_and_boxes.ipynb
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"dataname=\"synthetic\"\n",
"\n",
"patch_size=256 #size of the tiles to put into DB\n",
"data_size=[10000,100]\n",
"balance=.5\n",
"classes=[0,1] #what classes we expect to have in the data, in this case data without boxes and data with boxes\n",
"\n",
"max_circles=10\n",
"max_squares=1\n",
"diameter_min=10\n",
"diameter_max=50\n",
"\n",
"phases=[\"train\",\"val\"]"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"random seed (note down for reproducibility): 1750130440882827496\n"
]
}
],
"source": [
"import random\n",
"import tables\n",
"import sys\n",
"\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"from PIL import Image, ImageDraw\n",
"\n",
"\n",
"seed = random.randrange(sys.maxsize) #get a random seed so that we can reproducibly do the cross validation setup\n",
"random.seed(seed) # set the seed\n",
"print(f\"random seed (note down for reproducibility): {seed}\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"img_dtype = tables.UInt8Atom() # dtype in which the images will be saved, this indicates that images will be saved as unsigned int 8 bit, i.e., [0,255]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"train\n",
"val\n",
"done\n"
]
}
],
"source": [
"%matplotlib inline\n",
"storage={} #holder for future pytables\n",
"\n",
"block_shape=np.array((patch_size,patch_size)) #block shape specifies what we'll be saving into the pytable array, here we assume that masks are 1d and images are 3d\n",
"\n",
"filters=tables.Filters(complevel=6, complib='zlib') #we can also specify filters, such as compression, to improve storage speed\n",
"\n",
"\n",
"for phase,nimgs in zip(phases,data_size): #now for each of the phases, we'll loop through the files\n",
" print(phase)\n",
" \n",
" totals=np.zeros(2) # we can to keep counts of all the classes in for in particular training, since we \n",
"\n",
" hdf5_file = tables.open_file(f\"./{dataname}_{phase}.pytable\", mode='w') #open the respective pytable\n",
"\n",
"\n",
" storage[\"imgs\"]= hdf5_file.create_earray(hdf5_file.root, \"imgs\", img_dtype, \n",
" shape=np.append([0],block_shape), \n",
" chunkshape=np.append([1],block_shape),\n",
" filters=filters)\n",
" storage[\"labels\"]= hdf5_file.create_earray(hdf5_file.root, \"labels\", img_dtype, \n",
" shape=[0], \n",
" chunkshape=[1],\n",
" filters=filters)\n",
"\n",
" \n",
" for filei in range(nimgs): #now for each of the files\n",
" img=np.zeros((patch_size,patch_size))\n",
" img = Image.fromarray(img)\n",
" draw= ImageDraw.Draw(img)\n",
" \n",
" for i in range(np.random.randint(0,high=max_circles)):\n",
" d=np.random.randint(diameter_min,diameter_max)\n",
" ul=np.random.randint(diameter_min,patch_size-diameter_max,2)\n",
" draw.ellipse(list(np.append(ul,ul+d)),fill=255)\n",
" \n",
"\n",
" label=np.random.random()>balance\n",
" if label:\n",
" for i in range(np.random.randint(1,high=max_squares+1)):\n",
" d=np.random.randint(diameter_min,diameter_max)\n",
" ul=np.random.randint(diameter_min,patch_size-diameter_max,2)\n",
" draw.rectangle(list(np.append(ul,ul+d)),fill=255)\n",
" totals[1]+=1\n",
" else:\n",
" totals[0]+=1\n",
" #add square\n",
" \n",
" del draw \n",
"\n",
" storage[\"imgs\"].append(np.array(img)[None,::])\n",
" storage[\"labels\"].append([np.uint8(label)]) #add the filename to the storage array\n",
" \n",
" #lastely, we should store the number of pixels\n",
" npixels=hdf5_file.create_carray(hdf5_file.root, 'classsizes', tables.Atom.from_dtype(totals.dtype), totals.shape)\n",
" npixels[:]=totals\n",
" hdf5_file.close()\n",
" \n",
"print(\"done\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}