original_docstring: Given a model and an image of an entire chess board (found in data/raw/ folder),
      this method predicts what piece (if any) is on each square
docstring: Given a model and an image of an entire chess board (found in data/raw/ folder),
  this method predicts what piece (if any) is on each square
docstring_tokens: Given a model and an image of an entire chess board ( found in data / raw / folder ) this method predicts what piece ( if any ) is on each square
code: def predict():
      """Given a model and an image of an entire chess board (found in data/raw/ folder),
      this method predicts what piece (if any) is on each square"""
      # parsing
      print("Predicting squares...")
      parser = argparse.ArgumentParser(description="Evaluation arguments")
      parser.add_argument("load_model_from", default="")
      parser.add_argument("image", default="")
      args = parser.parse_args(sys.argv[1:])
  
      # model loading
      model = ChessPiecePredictor(25, 12, 1, 128, 4)
      model.load_state_dict(torch.load(args.load_model_from))
      model.eval()
  
      # data loading and preparing
      image = Image.open(args.image)
      image = image.convert("L")  # convert to grayscale
  
      squares = crop(np.array(image))
      squares = torch.tensor(
          np.array(squares)
      )  # convert to np.array() for speed. If not, we get a user warning
      squares = transforms.ConvertImageDtype(torch.float)(squares)
  
      # convert from (64, image_size, image_size), models expects the extra dimension
      if model.image_size:
          squares = transforms.Resize((model.image_size, model.image_size))(squares)
          squares = squares.view(64, 1, model.image_size, model.image_size)
      else:
          squares = squares.view(64, 1, 50, 50)
  
      with torch.no_grad():
          prediction = model(squares)
  
      # show results
      classes = ["_", "b", "k", "n", "p", "q", "r", "B", "K", "N", "P", "Q", "R"]
  
      _, prediction_indices = torch.max(prediction, dim=1)
  
      board = ""
      i = 0
      for index in prediction_indices:
          print(classes[index], end="")
          board += str(classes[index])
          i += 1
          if i % 8 == 0:
              print()
              board += "\n"
  
      # the last char is a newline, so [:-1] removes that
      print("fen:", board_to_fen(board[:-1]))
code_tokens: def predict ( ) : print ( "Predicting squares..." ) parser = argparse . ArgumentParser ( description = "Evaluation arguments" ) parser . add_argument ( "load_model_from" , default = "" ) parser . add_argument ( "image" , default = "" ) args = parser . parse_args ( sys . argv [ 1 : ] ) model = ChessPiecePredictor ( 25 , 12 , 1 , 128 , 4 ) model . load_state_dict ( torch . load ( args . load_model_from ) ) model . eval ( ) image = Image . open ( args . image ) image = image . convert ( "L" ) squares = crop ( np . array ( image ) ) squares = torch . tensor ( np . array ( squares ) ) squares = transforms . ConvertImageDtype ( torch . float ) ( squares ) if model . image_size : squares = transforms . Resize ( ( model . image_size , model . image_size ) ) ( squares ) squares = squares . view ( 64 , 1 , model . image_size , model . image_size ) else : squares = squares . view ( 64 , 1 , 50 , 50 ) with torch . no_grad ( ) : prediction = model ( squares ) classes = [ "_" , "b" , "k" , "n" , "p" , "q" , "r" , "B" , "K" , "N" , "P" , "Q" , "R" ] _ , prediction_indices = torch . max ( prediction , dim = 1 ) board = "" i = 0 for index in prediction_indices : print ( classes [ index ] , end = "" ) board += str ( classes [ index ] ) i += 1 if i % 8 == 0 : print ( ) board += "\n" print ( "fen:" , board_to_fen ( board [ : - 1 ] ) )
short_docstring: Given a model and an image of an entire chess board (found in data/raw/ folder),
  this method predicts what piece (if any) is on each square
short_docstring_tokens: Given a model and an image of an entire chess board ( found in data / raw / folder ) this method predicts what piece ( if any ) is on each square
comment: """Given a model and an image of an entire chess board (found in data/raw/ folder),
      this method predicts what piece (if any) is on each square"""$SEP$# parsing$SEP$# model loading$SEP$# data loading and preparing$SEP$# convert to grayscale$SEP$# convert to np.array() for speed. If not, we get a user warning$SEP$# convert from (64, image_size, image_size), models expects the extra dimension$SEP$# show results$SEP$# the last char is a newline, so [:-1] removes that