2121import logging
2222import queue
2323import gc
24+ import math
2425
2526from datetime import datetime
2627
@@ -147,7 +148,8 @@ def _interpreter_runner(self, seg_idx: int):
147148 self .interpreter = None
148149 self .input_details = None
149150 self .output_details = None
150- self .rebalancing_lock .release ()
151+ if self .rebalancing_lock .locked ():
152+ self .rebalancing_lock .release ()
151153 return
152154
153155 start_inference_time = time .perf_counter_ns ()
@@ -466,7 +468,7 @@ def _watchdog(self):
466468 time .time () - self .watchdog_time > self .max_idle_secs_before_recycle :
467469 logging .warning ("No work in {} seconds, watchdog shutting down TPUs." .format (self .max_idle_secs_before_recycle ))
468470 self .runner_lock .acquire (timeout = MAX_WAIT_TIME )
469- if self .pipe :
471+ if self .pipe . first_name :
470472 self .pipe .delete ()
471473 self .runner_lock .release ()
472474 # Pipeline will reinitialize itself as needed
@@ -726,7 +728,7 @@ def _delete(self):
726728 def pipeline_ok (self ) -> bool :
727729 """ Check we have valid interpreters """
728730 with self .runner_lock :
729- return self .pipe and any (self .pipe .interpreters )
731+ return bool ( self .pipe and any (self .pipe .interpreters ) )
730732
731733 def process_image (self ,
732734 options :Options ,
@@ -1142,9 +1144,17 @@ def _resize_and_chop_tiles(self,
11421144 self .printed_shape_map [key ] = True
11431145
11441146 # Do chunking
1147+ # Image will not be an even fit in at least one dimension; space tiles appropriately.
11451148 tiles = []
1146- for x_off in range (0 , image .width - options .tile_overlap , int (ceil ((image .width - m_width )/ tiles_x ))):
1147- for y_off in range (0 , image .height - options .tile_overlap , int (ceil ((image .height - m_height )/ tiles_y ))):
1149+ step_x = 1
1150+ if tiles_x > 1 :
1151+ step_x = int (math .ceil ((image .width - m_width )/ (tiles_x - 1 )))
1152+ step_y = 1
1153+ if tiles_y > 1 :
1154+ step_y = int (math .ceil ((image .height - m_height )/ (tiles_y - 1 )))
1155+
1156+ for x_off in range (0 , max (image .width - m_width , 0 ) + tiles_x , step_x ):
1157+ for y_off in range (0 , max (image .height - m_height , 0 ) + tiles_y , step_y ):
11481158 # Adjust contrast on a per-chunk basis; we will likely be quantizing the image during scaling
11491159 image_chunk = ImageOps .autocontrast (image .crop ((x_off ,
11501160 y_off ,
@@ -1158,9 +1168,6 @@ def _resize_and_chop_tiles(self,
11581168
11591169 tiles .append ((cropped_arr .astype (self .input_details ['dtype' ]), resamp_info ))
11601170
1161- # Check again with various scales
1162- #Image.fromarray((tiles[-1][0] + 128).astype(np.uint8)).save("test.png")
1163-
11641171
11651172 return tiles
11661173
0 commit comments