So You're Ready to Graduate to Part 2?

In the spirit of giving back to the forums, I decided to put together how I got set up for part 2 of the course after finishing part 1. This is as of early October, so keep in mind that things could change as newer versions of packages are released and default installs change.

My goal is to get you setup for the Lesson 8 notebook. I haven’t finished running Lesson 8 myself so I may update some things as I work through the remaining bits of code. I have not had time to go beyond Lesson 8 so there are most certainly issues lurking in Lesson 9 and beyond that I won’t address.

The first thing to note is that I am using an AWS p2 instance. I don’t have the time or courage to build my own box as Jeremy recommends early in the Lesson 8 video lecture. So this post will likely be most useful for someone who is still using AWS resources to consume the material.

The second thing to note is that Jeremy gets us most of the way there with his post Py3 and tensorflow setup. So you should definitely look at that post in addition to this one. Another thing to note is that littered throughout the Part 2 forums, there are threads and posts within threads that point out and often solve these issues. It is my attempt to compile all of these issues in one place.

The final thing to note is that the challenge in simply following Jeremy’s instructions is that he was using different versions of tensorflow and keras than what will be installed if you follow his instructions. So another perfectly valid way of going about this is installing the older versions of these packages and following his directions exactly.

Without further ado, let’s start listing the steps.

Step:

  1. SSH into your existing instance from Part 1 and run the following two lines in the shell to update linux libraries
    sudo apt update sudo apt upgrade
  2. Download and install Anaconda’s Python 3.6 distribution by running the following four commands:
    cd
    cd downloads
    wget https://repo.continuum.io/archive/Anaconda3-4.3.0-Linux-x86_64.sh
    bash Anaconda3-4.3.0-Linux-x86_64.sh -b
  3. Replace
    export PATH="/home/ubuntu/anaconda2/bin:$PATH"
    with
    export PATH="/home/ubuntu/anaconda3/bin:$PATH"
    at the bottom of the .bashrc file in your home directory by executing the following two lines:
    cd
    vim .bashrc
    Recall that you edit a file with vim by pressing ‘i’ to enter insert mode. And then to write and quit press the escape key followed by ‘:wq’
  4. Next reboot your instance for good measure by running the following:
    sudo shutdown -r now
  5. SSH back into your instance
  6. So far we have just followed Jeremy’s instructions exactly. Here is where we start to deviate. We need to install cuDNN version 6 to pair with tensorflow 1.3. tensorflow 1.3 is the latest release and it is not backwards compatible with cuDNN version 5. So go into your ‘downloads’ folder and delete the ‘cuda’ folder if it exists.
    cd downloads
    rm -r cuda. If prompted if you really want to delete this folder answer yes by typing:
    y
  7. Now install CUDA toolkit 8.0 and cuDNN version 6 by running the following in order

CUDA_REPO_PKG="cuda-repo-ubuntu1604_8.0.61-1_amd64.deb"
wget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/${CUDA_REPO_PKG}
sudo dpkg -i ${CUDA_REPO_PKG}
sudo apt-get update
sudo apt-get -y install cuda
CUDNN_TAR_FILE="cudnn-8.0-linux-x64-v6.0.tgz"
wget http://developer.download.nvidia.com/compute/redist/cudnn/v6.0/${CUDNN_TAR_FILE}
tar -xzvf ${CUDNN_TAR_FILE}
sudo cp -P cuda/include/cudnn.h /usr/local/cuda-8.0/include
sudo cp -P cuda/lib64/libcudnn* /usr/local/cuda-8.0/lib64/
sudo chmod a+r /usr/local/cuda-8.0/lib64/libcudnn*
export PATH=/usr/local/cuda-8.0/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

  1. Install tensorflow (as of the time this post was made, this will install tensorflow 1.3)
    pip install tensorflow-gpu
  2. Install the latest version of keras (as of the time this post was made, this will install keras 2.0.8)
    pip install git+git://github.com/fchollet/keras.git
  3. Install other packages that will be needed sooner or later:

conda install bcolz
pip install xgboost
pip install gensim
pip install keras-tqdm
pip install jupyterthemes
pip install --upgrade jupyterthemes
conda install -c conda-forge jupyter_contrib_nbextensions
conda install -c conda-forge jupyter_nbextensions_configurator
sudo apt-get install ffmpeg
jupyter nbextension enable --py widgetsnbextension

  1. Configure keras to play nice with tensorflow by editing the keras.json file by running the command:

echo ‘{
“image_dim_ordering”: “tf”,
“epsilon”: 1e-07,
“floatx”: “float32”,
“backend”: “tensorflow”
}’ > ~/.keras/keras.json

  1. Test your installs of tensorflow and keras by starting python from the shell and then importing the packages:
    python
    import tensorflow as tf
    import keras
    If all worked as hoped, press Ctrl+d to exit python

  2. Optionally, create a new jupyter notebook password as instructed by Jeremy at the bottom of his post here.

  3. Install the course 2 notebooks and python scripts from GitHub. These next few commands assume you have a folder called ‘courses’ from course 1 that has a subfolder called ‘deeplearning1’ and you want to keep that folder untouched.
    mkdir course2
    cd course2
    git clone https://github.com/fastai/courses.git
    mv deeplearning2 courses/
    cd..
    rm -r course2

  4. Change the vgg16_avg.py file to be compatible with keras 2 via vim.
    cd courses/deeplearning2
    vim vgg16_avg.py
    Replace instances of 'Convolution2D with ‘Conv2D’: :%s/Convolution2D/Conv2D/gc
    Replace instances of ‘border_mode’ with ‘padding’ :%s/border_mode/padding/gc
    Replace filter size integers with tuples: :%s/3, 3/(3, 3)/gc
    Go into insert mode by pressing 'i’
    Replace:

Input_shape = obtain_input_shape(input_shape,
default_size=224,
min_size=48,
dim_ordering=K.image_dim_ordering(),
include_top=include_top)
with:
Input_shape = obtain_input_shape(input_shape,
default_size=224,
min_size=48,
data_format=K.image_dim_ordering(),
require_flatten=include_top)

  1. Change the utils2.py file to be compatible with the latest versions of the deep learning packages:
    vim utils2.py
    press ‘i’ to enter insert mode
    Replace:
    from keras import initializations
    with:
    from keras import initializers
    And comment out (or delete) the following line in the limit_mem() function by placing a ‘#’ in front:
    K.get_session().close()
    Press escape and then ‘:wq’ to write and then quit.

  2. Make the directories for and then download the datasets that Jeremy has made available for Lesson 8.
    mkdir data
    cd data
    mkdir datasets
    cd datasets
    mkdir imagenet
    cd imagenet
    wget http://files.fast.ai/data/imagenet-sample-train.tar.gz
    wget http://files.fast.ai/data/trn_resized_288.tar.gz
    wget http://files.fast.ai/data/trn_resized_72.tar.gz

  3. Unzip the tar files:
    tar -zxf imagenet-sample-train.tar.gz
    tar -zxf trn_resized_288.tar.gz
    tar -zxf trn_resized_72.tar.gz

  4. Return to the deeplearning2 folder:
    cd ../../..

  5. Optionally download some pictures from the internet for style transfer exercise. For instance, here’s how you could download and rename a picture of Van Gogh’s Starry Night painting:
    cd data
    wget https://media.overstockart.com/optimized/cache/data/product_images/VG2614-1000x1000.jpg
    mv VG2614-1000x1000.jpg starry_night.jpg
    cd ..

  6. Optionally, change the style of jupyter notebook to match Jeremy’s with the following command:
    jt -t grade3

  7. Launch jupyter notebook:
    jupyter notebook

  8. Open up a web browser with the ip address of your instance on port :8888 like you did for Part 1 of the course. Enter your password.

  9. Open up the neural-style.ipynb

  10. Run the first two code cells and ensure no errors. I get a deprecation warning for sklearn/cross_validaiton.
    In the third block of code replace Jeremy’s paths with the following (assuming you followed the same directory structure):
    path = 'data/datasets/imagenet/'
    if not os.path.exists(os.path.join(path,'results')): os.mkdir(os.path.join(path,'results'))

  11. Anywhere in the notebook you see the following:
    metrics.mse(...)
    Replace it with:
    K.mean(metrics.mse(...))

OK, that’s all I got for now. I hope this helps.

Cheers,
Patrick

10 Likes

thanks for sharing! it’s very helpful, I would actually go to install the old versions to save the trouble of different keras versions.