pytorch/examples

Neural Style example: "OSError: [Errno 22] Invalid argument" after 2nd epoch during saving the model

vrtx-voiod opened this issue · 2 comments

I am running PyTorch on Windows 10 using:

  • Anaconda 3
  • Python: 3.9.1
  • PyTorch 1.7.1
  • numpy 1.14.5
  • scipy 1.5.3
  • Cuda Toolkit 11.0.3

executing the Train Model code of the Neural Style example, using the recommended COCO dataset, with following code:

<path_to_the_examples_folder>/neural_style/neural_style.py train --dataset <path_to_dataset>/train2014 --style-image <path_to_style_image>/styleimage_1280x720.jpg --save-model-dir <path_to_folder_for_saving_models> --epochs 2 --cuda 1

After finishing the 2nd epoch I get the following error message:

File "<path_to_the_examples_folder>\neural_style\neural_style.py", line 242, in <module>
    main()
  File "<path_to_the_examples_folder>\neural_style\neural_style.py", line 236, in main
    train(args)
  File "<path_to_the_examples_folder>\neural_style\neural_style.py", line 117, in train
    torch.save(transformer.state_dict(), save_model_path)
  File "<path_to_conda_env_lib>\site-packages\torch\serialization.py", line 369, in save
    with _open_file_like(f, 'wb') as opened_file:
File "<path_to_conda_env_lib>\site-packages\torch\serialization.py", line 230, in _open_file_like
   return _open_file(name_or_buffer, mode)
  File "<path_to_conda_env_lib>\site-packages\torch\serialization.py", line 211, in __init__
    super(_open_file, self).__init__(open(name, mode))

OSError: [Errno 22] Invalid argument: '<path_to_folder_for_saving_models>\\epoch_2_Fri_Jan__1_02:38:03_2021_100000.0_10000000000.0.model'

Do I have to give the model file a specific name?
Such as:

xyz.pth

Or is something else missing?

Paths within < > are placeholders, replace this with a valid path and allowed chars with paths.

thanks for this comment.. it helped me fix my issue. for windows i added another replace statement and at the top added system.

from platform import system

 if (system() == 'Windows'):
        save_model_filename = "epoch_" + str(args.epochs) + "_" + str(time.ctime()).replace(' ', '_').replace(':','_') + "_" + str(args.content_weight) + "_" + str(args.style_weight) + ".pth"
    else:
        save_model_filename = "epoch_" + str(args.epochs) + "_" + str(time.ctime()).replace(' ', '_') + "_" + str(args.content_weight) + "_" + str(args.style_weight) + ".model"