1
1
# coding=utf-8
2
+ # Copyright 2018 The Tensor2Tensor Authors.
3
+ #
2
4
# Licensed under the Apache License, Version 2.0 (the "License");
3
5
# you may not use this file except in compliance with the License.
4
6
# You may obtain a copy of the License at
5
7
#
6
- # http://www.apache.org/licenses/LICENSE-2.0
8
+ # http://www.apache.org/licenses/LICENSE-2.0
7
9
#
8
10
# Unless required by applicable law or agreed to in writing, software
9
11
# distributed under the License is distributed on an "AS IS" BASIS,
10
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
13
# See the License for the specific language governing permissions and
12
14
# limitations under the License.
13
-
14
15
"""Problem definitions for Allen Brain Atlas problems.
15
16
16
17
Notes:
28
29
29
30
from io import BytesIO
30
31
import math
31
- import numpy as np
32
32
import os
33
+
34
+ import numpy as np
33
35
import requests
34
36
35
37
from tensor2tensor .data_generators import generator_utils
36
38
from tensor2tensor .data_generators import image_utils
37
39
from tensor2tensor .data_generators import problem
38
40
from tensor2tensor .data_generators import text_encoder
39
- from tensor2tensor .utils import registry
40
41
from tensor2tensor .utils import metrics
41
-
42
- from tensor2tensor .data_generators .allen_brain_utils import try_importing_pil_image
42
+ from tensor2tensor .utils import registry
43
43
44
44
import tensorflow as tf
45
45
52
52
# the steps described here: http://help.brain-map.org/display/api,
53
53
# e.g. https://gist.github.com/cwbeitel/5dffe90eb561637e35cdf6aa4ee3e704
54
54
_IMAGE_IDS = [
55
- '74887117' , '71894997' , '69443979' , '79853548' , '101371232' , '77857182' ,
56
- '70446772' , '68994990' , '69141561' , '70942310' , '70942316' , '68298378' ,
57
- '69690156' , '74364867' , '77874134' , '75925043' , '73854431' , '69206601' ,
58
- '71771457' , '101311379' , '74777533' , '70960269' , '71604493' , '102216720' ,
59
- '74776437' , '75488723' , '79815814' , '77857132' , '77857138' , '74952778' ,
60
- '69068486' , '648167' , '75703410' , '74486118' , '77857098' , '637407' ,
61
- '67849516' , '69785503' , '71547630' , '69068504' , '69184074' , '74853078' ,
62
- '74890694' , '74890698' , '75488687' , '71138602' , '71652378' , '68079764' ,
63
- '70619061' , '68280153' , '73527042' , '69764608' , '68399025' , '244297' ,
64
- '69902658' , '68234159' , '71495521' , '74488395' , '73923026' , '68280155' ,
65
- '75488747' , '69589140' , '71342189' , '75119214' , '79455452' , '71774294' ,
66
- '74364957' , '68031779' , '71389422' , '67937572' , '69912671' , '73854471' ,
67
- '75008183' , '101371376' , '75703290' , '69533924' , '79853544' , '77343882' ,
68
- '74887133' , '332587' , '69758622' , '69618413' , '77929999' , '244293' ,
69
- '334792' , '75825136' , '75008103' , '70196678' , '71883965' , '74486130' ,
70
- '74693566' , '76107119' , '76043858' , '70252433' , '68928364' , '74806345' ,
71
- '67848661' , '75900326' , '71773690' , '75008171' ]
55
+ "74887117" , "71894997" , "69443979" , "79853548" , "101371232" , "77857182" ,
56
+ "70446772" , "68994990" , "69141561" , "70942310" , "70942316" , "68298378" ,
57
+ "69690156" , "74364867" , "77874134" , "75925043" , "73854431" , "69206601" ,
58
+ "71771457" , "101311379" , "74777533" , "70960269" , "71604493" , "102216720" ,
59
+ "74776437" , "75488723" , "79815814" , "77857132" , "77857138" , "74952778" ,
60
+ "69068486" , "648167" , "75703410" , "74486118" , "77857098" , "637407" ,
61
+ "67849516" , "69785503" , "71547630" , "69068504" , "69184074" , "74853078" ,
62
+ "74890694" , "74890698" , "75488687" , "71138602" , "71652378" , "68079764" ,
63
+ "70619061" , "68280153" , "73527042" , "69764608" , "68399025" , "244297" ,
64
+ "69902658" , "68234159" , "71495521" , "74488395" , "73923026" , "68280155" ,
65
+ "75488747" , "69589140" , "71342189" , "75119214" , "79455452" , "71774294" ,
66
+ "74364957" , "68031779" , "71389422" , "67937572" , "69912671" , "73854471" ,
67
+ "75008183" , "101371376" , "75703290" , "69533924" , "79853544" , "77343882" ,
68
+ "74887133" , "332587" , "69758622" , "69618413" , "77929999" , "244293" ,
69
+ "334792" , "75825136" , "75008103" , "70196678" , "71883965" , "74486130" ,
70
+ "74693566" , "76107119" , "76043858" , "70252433" , "68928364" , "74806345" ,
71
+ "67848661" , "75900326" , "71773690" , "75008171" ]
72
+
73
+
74
+ def PIL_Image (): # pylint: disable=invalid-name
75
+ from PIL import Image # pylint: disable=g-import-not-at-top
76
+ return Image
72
77
73
78
74
79
def _get_case_file_paths (tmp_dir , case , training_fraction = 0.95 ):
@@ -77,14 +82,17 @@ def _get_case_file_paths(tmp_dir, case, training_fraction=0.95):
77
82
Args:
78
83
tmp_dir: str, the root path to which raw images were written, at the
79
84
top level having meta/ and raw/ subdirs.
80
- size: int, the size of sub-images to consider (`size`x`size`).
81
85
case: bool, whether obtaining file paths for training (true) or eval
82
86
(false).
83
87
training_fraction: float, the fraction of the sub-image path list to
84
88
consider as the basis for training examples.
85
89
86
90
Returns:
87
91
list: A list of file paths.
92
+
93
+ Raises:
94
+ ValueError: if images not found in tmp_dir, or if training_fraction would
95
+ leave no examples for eval.
88
96
"""
89
97
90
98
paths = tf .gfile .Glob ("%s/*.jpg" % tmp_dir )
@@ -146,7 +154,7 @@ def maybe_download_image_dataset(image_ids, target_dir):
146
154
147
155
response .raise_for_status ()
148
156
149
- with open (tmp_destination , "w" ) as f :
157
+ with tf . gfile . Open (tmp_destination , "w" ) as f :
150
158
for block in response .iter_content (1024 ):
151
159
f .write (block )
152
160
@@ -159,7 +167,6 @@ def random_square_mask(shape, fraction):
159
167
Args:
160
168
shape: tuple, shape of the mask to create.
161
169
fraction: float, fraction of the mask area to populate with `mask_scalar`.
162
- mask_scalar: float, the scalar to apply to the otherwise 1-valued mask.
163
170
164
171
Returns:
165
172
numpy.array: A numpy array storing the mask.
@@ -191,6 +198,8 @@ def _generator(tmp_dir, training, size=_BASE_EXAMPLE_IMAGE_SIZE,
191
198
alternatively, evaluation), determining whether examples in tmp_dir
192
199
prefixed with train or dev will be used.
193
200
size: int, the image size to add to the example annotation.
201
+ training_fraction: float, the fraction of the sub-image path list to
202
+ consider as the basis for training examples.
194
203
195
204
Yields:
196
205
A dictionary representing the images with the following fields:
@@ -207,7 +216,7 @@ def _generator(tmp_dir, training, size=_BASE_EXAMPLE_IMAGE_SIZE,
207
216
case = training ,
208
217
training_fraction = training_fraction )
209
218
210
- image_obj = try_importing_pil_image ()
219
+ image_obj = PIL_Image ()
211
220
212
221
tf .logging .info ("Loaded case file paths (n=%s)" % len (image_files ))
213
222
height = size
@@ -230,8 +239,7 @@ def _generator(tmp_dir, training, size=_BASE_EXAMPLE_IMAGE_SIZE,
230
239
v_end = v_offset + size - 1
231
240
232
241
# Extract a sub-image tile.
233
- # pylint: disable=invalid-sequence-index
234
- subimage = np .uint8 (img [h_offset :h_end , v_offset :v_end ])
242
+ subimage = np .uint8 (img [h_offset :h_end , v_offset :v_end ]) # pylint: disable=invalid-sequence-index
235
243
236
244
# Filter images that are likely background (not tissue).
237
245
if np .amax (subimage ) < 230 :
0 commit comments