This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
cs501r_f2018:lab1 [2018/09/03 21:05] wingated created |
cs501r_f2018:lab1 [2021/06/30 23:42] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
====Objective:==== | ====Objective:==== | ||
- | Get started with anaconda, python, ipython notebooks, and pandas. Begin producing simple visualizations of data and images. | + | Get started with colab and python. Begin producing simple visualizations of data and images. |
---- | ---- | ||
====Deliverable:==== | ====Deliverable:==== | ||
- | For this lab, you will submit an ipython notebook. This notebook will have two parts: | + | For this lab, you will submit an ipython notebook via learningsuite. This notebook will have two parts: |
**Part 1:** Your notebook should generate a random image. We will run this | **Part 1:** Your notebook should generate a random image. We will run this | ||
Line 19: | Line 19: | ||
[[http://playground.tensorflow.org/|Tensorflow playground]] | [[http://playground.tensorflow.org/|Tensorflow playground]] | ||
+ | |||
+ | [[http://colab.research.google.com/|Google colab]] | ||
+ | |||
+ | [[https://colab.research.google.com/drive/1TzaPS3jvRadN-URLbQ9nD1ZNoZktfNRy|A colab notebook teaching you how to use colab notebooks]] | ||
+ | |||
+ | [[https://sites.google.com/site/artml2018/tutorials|Various colab tutorials]] | ||
---- | ---- | ||
Line 33: | Line 39: | ||
====Description:==== | ====Description:==== | ||
- | Throughout this class, we will be using a combination of ipython | + | Throughout this class, we will be using Google's colab environment to develop and test our deep neural networks. This consists of ipython |
- | notebooks, Tensorflow and the anaconda python distribution. For this lab, you | + | notebooks and a standardized python distribution. For this lab, you |
- | must install anaconda, and write a simple python program (using | + | must create a google account (or use one you already have), start up colab, and write a simple python program in it. |
- | ipython notebooks). | + | |
As described above, the notebook should do two things: | As described above, the notebook should do two things: | ||
Line 47: | Line 52: | ||
type, such as random lines, and can be created in a loop). We won't | type, such as random lines, and can be created in a loop). We won't | ||
count the number of elements; this is just to encourage you to create | count the number of elements; this is just to encourage you to create | ||
- | random images with moderate complexity. | + | random images with moderate complexity (ie, you can't just generate randomly colored pixels, as in the example below). |
For part 2, you should visit the Tensorflow playground (see link above), and play with different settings. Most of it will be unfamiliar, but don't worry -- you can't break it! | For part 2, you should visit the Tensorflow playground (see link above), and play with different settings. Most of it will be unfamiliar, but don't worry -- you can't break it! | ||
- | Once you have a working classifier, take a screenshot. Then use your ipython notebook to display that image in-line. | + | Once you have a working classifier, take a screenshot. Then use your colab notebook to display that image in-line. |
---- | ---- | ||
- | ====Installing anaconda:==== | + | ====Starter code:==== |
- | + | ||
- | http://docs.continuum.io/anaconda/install | + | |
- | + | ||
- | To generate images, check out PIL. | + | |
- | + | ||
- | To generate random numbers, check out the [[http://docs.scipy.org/doc/numpy-1.10.0/reference/routines.random.html|numpy.random]] module. | + | |
- | + | ||
- | To create a new notebook, run: | + | |
- | + | ||
- | ''jupyter-notebook'' | + | |
- | + | ||
- | This should start an ipython kernel in the background, set up a | + | |
- | webserver, and point your browser to the webserver. In the | + | |
- | upper-right corner, you will see a "new" menu; under that menu you | + | |
- | should see "Notebook" and "Python 2". This will create a new | + | |
- | notebook. | + | |
- | + | ||
- | **Note:** When you turn in your notebook, you should turn in the ''.ipynb'' file. Do not take a screen shot, or turn in an HTML page. | + | |
Here's some starter code to help you generate an image. The ''nbimage'' function will display the image inline in the notebook: | Here's some starter code to help you generate an image. The ''nbimage'' function will display the image inline in the notebook: | ||
<code python> | <code python> | ||
- | import cairo | + | import IPython.display |
- | import numpy | + | import PIL.Image |
+ | import numpy as np | ||
# A simple function to display an image in an ipython notebook | # A simple function to display an image in an ipython notebook | ||
def nbimage( data ): | def nbimage( data ): | ||
- | from IPython.display import display, Image | + | IPython.display.display(PIL.Image.fromarray(data)) |
- | from PIL.Image import fromarray | + | |
- | from StringIO import StringIO | + | |
- | s = StringIO() | ||
- | fromarray( data ).save( s, 'png' ) | ||
- | display( Image( s.getvalue() ) ) | ||
- | WIDTH = 512 | + | # create an image consisting of random colors |
- | HEIGHT = 288 | + | |
- | # this is a numpy buffer to hold the image data | + | data = np.random.rand(512,512,3) # a 512x512 image, with 3 color channels (R,G,B) |
- | data = numpy.zeros( (HEIGHT,WIDTH,4), dtype=numpy.uint8 ) | + | |
+ | # by default, rand creates floating point numbers between [0,1]. We need to convert that to 8-bit bytes between [0,255] | ||
+ | data = (255*data).astype('uint8') | ||
+ | |||
+ | # display it! | ||
+ | nbimage( data ) | ||
- | # this creates a cairo context based on the numpy buffer | ||
- | ims = cairo.ImageSurface.create_for_data( data, cairo.FORMAT_ARGB32, WIDTH, HEIGHT ) | ||
- | cr = cairo.Context( ims ) | ||
- | # draw a blue line | ||
- | cr.set_source_rgba( 1.0, 0.0, 0.0, 1.0 ) | ||
- | cr.set_line_width( 2.0 ) | ||
- | cr.move_to( 0.0, 0.0 ) | ||
- | cr.line_to( 100.0, 100.0 ) | ||
- | cr.stroke() | ||
- | # display the image | ||
- | nbimage( data ) | ||
</code> | </code> | ||
Line 126: | Line 102: | ||
plt.tight_layout | plt.tight_layout | ||
- | </code> | ||
- | |||
- | Also note that to get plots to show up inline, you may have to add the magic incantation **in the first cell**: | ||
- | |||
- | <code python> | ||
- | |||
- | %matplotlib inline | ||
- | |||
- | import matplotlib | ||
- | import numpy as np | ||
- | import matplotlib.pyplot as plt | ||
- | |||
- | </code> | ||
- | |||
- | Here is a full setup for cairo in linux. | ||
- | <code> | ||
- | sudo apt-get install libcairo2-dev | ||
- | git clone https://github.com/pygobject/pycairo.git | ||
- | cd pycairo/ | ||
- | python setup.py build | ||
- | python setup.py install | ||
- | </code> | ||
- | |||
- | Here is a setup for cairo in mac. | ||
- | <code> | ||
- | brew install cairo --use-clang | ||
- | brew install py2cairo | ||
</code> | </code> | ||