11# coding=utf-8
2+ # Copyright 2018 The Tensor2Tensor Authors.
3+ #
24# Licensed under the Apache License, Version 2.0 (the "License");
35# you may not use this file except in compliance with the License.
46# You may obtain a copy of the License at
57#
6- # http://www.apache.org/licenses/LICENSE-2.0
8+ # http://www.apache.org/licenses/LICENSE-2.0
79#
810# Unless required by applicable law or agreed to in writing, software
911# distributed under the License is distributed on an "AS IS" BASIS,
1012# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1113# See the License for the specific language governing permissions and
1214# limitations under the License.
13-
1415"""Problem definitions for Allen Brain Atlas problems.
1516
1617Notes:
2829
2930from io import BytesIO
3031import math
31- import numpy as np
3232import os
33+
34+ import numpy as np
3335import requests
3436
3537from tensor2tensor .data_generators import generator_utils
3638from tensor2tensor .data_generators import image_utils
3739from tensor2tensor .data_generators import problem
3840from tensor2tensor .data_generators import text_encoder
39- from tensor2tensor .utils import registry
4041from tensor2tensor .utils import metrics
41-
42- from tensor2tensor .data_generators .allen_brain_utils import try_importing_pil_image
42+ from tensor2tensor .utils import registry
4343
4444import tensorflow as tf
4545
5252# the steps described here: http://help.brain-map.org/display/api,
5353# e.g. https://gist.github.com/cwbeitel/5dffe90eb561637e35cdf6aa4ee3e704
5454_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
7277
7378
7479def _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):
7782 Args:
7883 tmp_dir: str, the root path to which raw images were written, at the
7984 top level having meta/ and raw/ subdirs.
80- size: int, the size of sub-images to consider (`size`x`size`).
8185 case: bool, whether obtaining file paths for training (true) or eval
8286 (false).
8387 training_fraction: float, the fraction of the sub-image path list to
8488 consider as the basis for training examples.
8589
8690 Returns:
8791 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.
8896 """
8997
9098 paths = tf .gfile .Glob ("%s/*.jpg" % tmp_dir )
@@ -146,7 +154,7 @@ def maybe_download_image_dataset(image_ids, target_dir):
146154
147155 response .raise_for_status ()
148156
149- with open (tmp_destination , "w" ) as f :
157+ with tf . gfile . Open (tmp_destination , "w" ) as f :
150158 for block in response .iter_content (1024 ):
151159 f .write (block )
152160
@@ -159,7 +167,6 @@ def random_square_mask(shape, fraction):
159167 Args:
160168 shape: tuple, shape of the mask to create.
161169 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.
163170
164171 Returns:
165172 numpy.array: A numpy array storing the mask.
@@ -191,6 +198,8 @@ def _generator(tmp_dir, training, size=_BASE_EXAMPLE_IMAGE_SIZE,
191198 alternatively, evaluation), determining whether examples in tmp_dir
192199 prefixed with train or dev will be used.
193200 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.
194203
195204 Yields:
196205 A dictionary representing the images with the following fields:
@@ -207,7 +216,7 @@ def _generator(tmp_dir, training, size=_BASE_EXAMPLE_IMAGE_SIZE,
207216 case = training ,
208217 training_fraction = training_fraction )
209218
210- image_obj = try_importing_pil_image ()
219+ image_obj = PIL_Image ()
211220
212221 tf .logging .info ("Loaded case file paths (n=%s)" % len (image_files ))
213222 height = size
@@ -230,8 +239,7 @@ def _generator(tmp_dir, training, size=_BASE_EXAMPLE_IMAGE_SIZE,
230239 v_end = v_offset + size - 1
231240
232241 # 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
235243
236244 # Filter images that are likely background (not tissue).
237245 if np .amax (subimage ) < 230 :
0 commit comments