87

I'm getting an error in WSL while trying to use junyanz/pytorch-CycleGAN-and-pix2pix.

I followed all the install steps successfully on Windows 10 x64 using the steps at PyTORCH on Windows 10: An instructional with screenshots, then Ubuntu for Windows with GitHub desktop How to Install and Use the Linux Bash Shell on Windows 10.

But I'm blocked at the latest steps.

It's where I trying to download or train the model in Windows. Using for example the Ubuntu Linux prompt, I navigate manually to the appropriate place and type a following command like the one below (I also tried in su mode)

bash pretrained_models/download_pix2pix_model.sh facades_label2photo

I get an immediate error, always the same, similar to these:

root@Azure:/mnt/c/Users/vincent/Downloads/vision/pytorch-CycleGAN-and-pix2pix# bash pretrained_models/download_pix2pix_model.sh facades_label2photo
pretrained_models/download_pix2pix_model.sh: line 2: $'\r': command not found
Note: available models are edges2shoes, sat2map, and facades_label2photo
pretrained_models/download_pix2pix_model.sh: line 4: $'\r': command not found
]pecified [facades_label2photo
pretrained_models/download_pix2pix_model.sh: line 6: $'\r': command not found
mkdir: cannot create directory ‘./checkpoints/facades_label2photo\r_pretrained\r’: No such file or directory
pretrained_models/download_pix2pix_model.sh: line 10: $'\r': command not found
WARNING: timestamping does nothing in combination with -O. See the manual
for details.

: No such file or directoryhphoto
pretrained_models/download_pix2pix_model.sh: line 12: $'\r': command not found
pretrained_models/download_pix2pix_model.sh: line 13: $'\r': command not found

Any idea?

Eliah Kagan
  • 117,780

6 Answers6

103

Inside WSL:

sudo apt-get install dos2unix

Then,

dos2unix [file]

Full documentation:

man dos2unix

Saved my day, hope it helps.

phuclv
  • 628
  • 1
  • 8
  • 38
Chaim Eliyah
  • 1,169
  • 1
  • 9
  • 21
19

If you are on Windows,

  1. Open your shell file on NotePad++
  2. Click on Edit on Top bar menu, then choose EOL Conversion --> Unix(LF)
  3. Now copy this file in your Linux system and it should run without these errors.
  • 3
    Worked like a charm! – Floating Sunfish Aug 14 '20 at 10:15
  • Perfect! I noticed bizzare behaviour when doing the conversion. Before this my simple if statement didn't see other file in the same folder (which was there). After doing your instruction if returned true while searching my second file. – marcin2x4 Jan 03 '21 at 18:39
  • Also using Find and Replace to replace \r with nothing worked for me, but this seems like a one-click version. – McKay G Aug 26 '21 at 19:35
17

steeldriver is correct that the problem is that you have files with Windows line endings and bash cannot run them. $'\r' is a representation of the carriage return character (CR) that is part of traditional DOS and Windows line endings (CR LF), but which is absent in traditional Unix-style line endings (LF).

As you say, you're typing the command to attempt to run the script in bash, but notice that the script is actually stored outside your Ubuntu (WSL) system, in your Windows download directory:

/mnt/c/Users/vincent/Downloads/vision/pytorch-CycleGAN-and-pix2pix

WSL paths that start with /mnt/c, where c may be any Windows drive letter, are paths that access files and directories outside the Ubuntu system. As a Windows path, that is:

C:\Users\vincent\Downloads\vision\pytorch-CycleGAN-and-pix2pix

That a file is stored in your Windows system outside the area where Ubuntu is installed does not guarantee that it uses Windows-style instead of Unix-style line endings. However, if you downloaded the files with Git in Windows, its default configuration is to give you Windows-style line endings.

The easiest way to fix the problem is really to just download the files you need inside the Ubuntu system from your bash prompt. I recommend you fully update the Ubuntu system, then install git in Ubuntu, as well as build-essential which provides useful tools that you need to compile most software from source code. For Python programs, you may not need build-essential; you can omit it if you like, but I suspect you'll end up needing it at some point.

sudo apt update && sudo apt upgrade && sudo apt install git build-essential

Then use the cd command to go to a directory where you would like to download the software. This should be a directory within your Ubuntu system. For example, it could be your Ubuntu home directory or somewhere inside there. Once there, clone the repository from GitHub. I did it inside the src directory that I made inside my home directory.

cd ~/src
git clone https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix.git

Of course, you will most likely have to install the dependencies first. To do that, just follow all the official instructions. Do all those steps within the Ubuntu system.

Eliah Kagan
  • 117,780
  • 1
    also you can do cat -v to see the line endings as "^M" in your unix commandline. When you have fixed it with the git settings you will see the "^M" gone. – atom88 Aug 20 '20 at 21:48
9

I was using VS code within my WSL environment to edit the bash script. Changing the setting from "CRLF" to "LF" on the bottom right corner fixed it for me.

VSCode window

DWD 3
  • 91
6

I ran into this issue and the solution was to configure git to checkout specific files in Linux line-ending way.

You can add a file to your git root directory name .gitattributes which configure specific attributes for git (see full documentation).

Add the lines

# Convert to LF line endings on checkout.
*.sh text eol=lf

To configure git to checkout .sh files with LF line ending even on Windows machines.

Ido Ran
  • 161
  • 1
  • 3
  • also you can do cat -v to see the line endings as "^M" in your unix commandline. When you have fixed it with the git settings you will see the "^M" gone. – atom88 Aug 20 '20 at 21:48
5

If you have vim handy, it'll fix you up quick.

vim $filename +"set ff=unix" +wq
flickerfly
  • 7,279