parameters: (data, target_wd, target_ht, is_train, box)
return_statement: return data.reshape((1,) + data.shape)
docstring: Crop and normnalize an image nd array.
docstring_summary: Crop and normnalize an image nd array.
docstring_tokens: Crop and normnalize an image nd array .
function: def transform(data, target_wd, target_ht, is_train, box):
      """Crop and normnalize an image nd array."""
      if box is not None:
          x, y, w, h = box
          data = data[y:min(y+h, data.shape[0]), x:min(x+w, data.shape[1])]
  
      # Resize to target_wd * target_ht.
      data = mx.image.imresize(data, target_wd, target_ht)
  
      # Normalize in the same way as the pre-trained model.
      data = data.astype(np.float32) / 255.0
      data = (data - mx.nd.array([0.485, 0.456, 0.406])) / mx.nd.array([0.229, 0.224, 0.225])
  
      if is_train:
          if random.random() < 0.5:
              data = nd.flip(data, axis=1)
          data, _ = mx.image.random_crop(data, (224, 224))
      else:
          data, _ = mx.image.center_crop(data, (224, 224))
  
      # Transpose from (target_wd, target_ht, 3)
      # to (3, target_wd, target_ht).
      data = nd.transpose(data, (2, 0, 1))
  
      # If image is greyscale, repeat 3 times to get RGB image.
      if data.shape[0] == 1:
          data = nd.tile(data, (3, 1, 1))
      return data.reshape((1,) + data.shape)
function_tokens: def transform ( data , target_wd , target_ht , is_train , box ) : if box is not None : x , y , w , h = box data = data [ y : min ( y + h , data . shape [ 0 ] ) , x : min ( x + w , data . shape [ 1 ] ) ] # Resize to target_wd * target_ht. data = mx . image . imresize ( data , target_wd , target_ht ) # Normalize in the same way as the pre-trained model. data = data . astype ( np . float32 ) / 255.0 data = ( data - mx . nd . array ( [ 0.485 , 0.456 , 0.406 ] ) ) / mx . nd . array ( [ 0.229 , 0.224 , 0.225 ] ) if is_train : if random . random ( ) < 0.5 : data = nd . flip ( data , axis = 1 ) data , _ = mx . image . random_crop ( data , ( 224 , 224 ) ) else : data , _ = mx . image . center_crop ( data , ( 224 , 224 ) ) # Transpose from (target_wd, target_ht, 3) # to (3, target_wd, target_ht). data = nd . transpose ( data , ( 2 , 0 , 1 ) ) # If image is greyscale, repeat 3 times to get RGB image. if data . shape [ 0 ] == 1 : data = nd . tile ( data , ( 3 , 1 , 1 ) ) return data . reshape ( ( 1 , ) + data . shape )