Coding libraries and Notebook samples
Python and Jupiter Notebooks
- Google Colab - is probably the simplest environment for jupiter notebooks, python, torch, etc.
However, it has its limitations. We still recommend that you install Anaconda on a local machine or a laptop.
All sample notebooks posted below can be easily run on Colab with minimal (suggested) modifications. You may also use Colab for many of your homeworks (not for all),
but posted assignments' files will assume Anaconda.
- Anaconda
- provides a collection of python libraries (Python 3.12) and "Jupiter" notebook environment on a local machine or laptop. Some tricks:
- If you already have an older Anaconda installed on a PC (Windows), we recommend that you update it to make sure we are on the same page with the versions of Python,
as well as other libraries and packages. We recommend that you first manually uninstall the old Anaconda
as cleanly as possible. In our experience, fresh new installs of Anaconda are easier to work with.
- We use ipympl backend (or "widgets") for the standard "matplotlib" library
(see a summary and the notebook examples below)
allowing interactive plots and images (with crop, save, rotate 3D plots, etc). This widget backend is also supported in Google colab. The corresponding "ipympl" package requires manual installation (see instructions). In case of Windows PC, it may be as simple as running the following command
inside the "Anaconda Prompt" (shoud be available after Anaconda is installed). Note that you have to restart Jupiter-Notebook after packages are installed or updated. On some PCs we observed that several related supporting packages in the default Anaconda installation may require a manual update, particualrly if you updated an older Anaconda.
If plots in your notebook output "Error displaying widget" or if the plots are static (e.g. no buttons for crop/save/etc or no interactivity in 3D plots) you can try to manually update "ipywidgets" package using the following Anaconda prompt commands:
-
pip uninstall ipywidgets
-
pip install ipywidgets
When done, run conda list
to check the versions of installed Anaconda packages. Package "jupyterlab-widgets" should be >3.0.x. In case its version is 1.0.0, you should manually update it, e.g. by running two more commands:
-
pip uninstall jupyterlab-widgets
-
pip install jupyterlab-widgets
- Insetad of the "Jupyterlab" program installed by Anaconda in Windows, we suggest that you open notebook files (.ipynb) using the "Jupyter-notebook" program,
that also comes with Anaconda. It may be convenient to change the default "start directory" for the jupiter-notebook, as follows. Find an exitsing shortcut to "Jupiter Notebook" on your PC (or create a new one), right-click on "Properties", inside "Target" replace "%USERPROFILE%/" with the full path to your prefered start directory, e.g. "E:\MyCourses\CS484\code".
- For debugging purposes, python code from your notebook can be compiled/run inside Visual Studio (VS) if you have it.
- Standard python libraries (included in Anaconda and Google Colab)
- numpy - array/matrix library for python. The NumPy (Numeric Python) package provides routines for manipulating (large) arrays/matrices containing numerical data. It allows to directly use operators from linear algebra (e.g. matrix summation, multiplication, etc) and much more. In many cases, numpy code can look exactly like linear-algebraic equations. In fact, if instead of using numpy operators/functions you write multi-nested for-loops in basic python that explicitly access elements of the matrices, your code will be very slow. We will penalize explicit use of nested-for-loops explicitly accessing matrix elements - this is an absolutely wrong way to work with numpy matrices. While in earlier assignemnts the use of nested for-loops is primarily a question of style, in later assignemnts such code will be prohibitively expensive - it will run in hours instead of seconds or minutes. Note that it takes some time and effort to get used to properly using numpy functions avoiding explicit (and expensive) access of matrix elements. There are many useful online tutorials, e.g. on basics (indexing, slicing, shapes, operations, broadcasting). In practice, it is often convenient to have one line of code include arrays of different shapes, which requires good understanding of broadcasting, e.g. see a brief tutorial on broadcasting. However, the best way to learn numpy is in the context of specific assignemnts. In trickier cases, we will provide some hints on what functions to use. In particular, we will frequently use various numpy's sublibraries/packages. Just to name a few of such useful libraries:
- linalg (basic linear algebra)
- random (random sampling, probability distributions)
- scipy - scientific and numerical tools for Python.
The SciPy (Scientific Python) package extends the functionality of NumPy with a substantial collection of useful algorithms,
like minimization, Fourier transformation, regression, and other applied mathematical techniques. See also
an intro to numpy and skipy and
specific commonly used sub-libraries of scipy:
- matplotlib - powerful plotting and visualization library. Commonly used sub-libraries are listed below:
- pyplot - tools for plotting functions, arrays, vector fields.
It can also be used to draw shapes (lines, ovals, rectangles, etc).
See the summary of pyplot functions.
- image - tools for image loading, rescaling and display.
See also a summary of image functions.
- skimage - image processing and analysis library with a large list of functions. It includes a few image samples.
- scikit-learn - machine learning library including clustering methods including K-means, spectral clustering, mean-shift, GMM, etc.
- Graph cut library: PyMaxflow, see github site
for documentation and examples. This library could be easily installed in Anaconda or Google Colab using
pip install PyMaxflow
.
- Deep Learning python libraries: Keras (tutorial), TensorFlow, and PyTorch (examples). Our plan is to use the latter.
The corresponding packages can be installed from Anaconda Prompt by running a "pip install" command specific to your hardware/software, as detailed
here. First, it is a good idea to install the latest driver for your graphics card and a version of cuda
compatible with the pytorch library (only some are, see details in the provided link). Note that Pytorch is "pre-installed" in Google Colab.
Dive into deep learning: examples of pytorch code for computer vision.
Jupiter Notebook samples for CS484/684