def random_crop_generator(generator, crop_size=(128, 128)):
 if (type(crop_size) not in (tuple, list)):
  crop_size = [crop_size, crop_size]
 elif (len(crop_size) == 2):
  crop_size = list(crop_size)
 else:
  raise ValueError('invalid crop_size')
 for (data, seg) in generator:
  lb_x = np.random.randint(0, (data.shape[2] - crop_size[0]))
  lb_y = np.random.randint(0, (data.shape[3] - crop_size[1]))
  data = data[:, :, lb_x:(lb_x + crop_size[0]), lb_y:(lb_y + crop_size[1])]
  seg = seg[:, :, lb_x:(lb_x + crop_size[0]), lb_y:(lb_y + crop_size[1])]
  (yield (data, seg))

def random_crop_generator ( generator , crop_size = ( 128 , 128 ) ) : if ( type ( crop_size ) not in ( tuple , list ) ) : crop_size = [ crop_size , crop_size ] elif ( len ( crop_size ) == 2 ) : crop_size = list ( crop_size ) else : raise ValueError ( 'invalid crop_size' ) for ( data , seg ) in generator : lb_x = np . random . randint ( 0 , ( data . shape [ 2 ] - crop_size [ 0 ] ) ) lb_y = np . random . randint ( 0 , ( data . shape [ 3 ] - crop_size [ 1 ] ) ) data = data [ : , : , lb_x : ( lb_x + crop_size [ 0 ] ) , lb_y : ( lb_y + crop_size [ 1 ] ) ] seg = seg [ : , : , lb_x : ( lb_x + crop_size [ 0 ] ) , lb_y : ( lb_y + crop_size [ 1 ] ) ] ( yield ( data , seg ) )
yields a random crop of size crop_size .