CS231n Convolutional Neural Networks for Visual Recognition------Scipy and MatplotlibTutorial
源鏈接為:http://cs231n.github.io/python-numpy-tutorial/。
這篇指導書是由Justin Johnson編寫的。
在這門課程中我們將使用Python語言完成所有變成任務!Python本身就是一種很棒的通用編程語言,但是在一些流行的庫幫助下(numpy,scipy,matplotlib)它已經成為科學計算的強大環境。
我們希望你們中的許多人都有一些Python和numpy的使用經驗; 對你們其他人來說,這個section將作為Python用于科學計算和使用的快速速成課程。
你們中的一些人可能已經掌握了Matlab的知識,在這種情況下我們也推薦使用numpy。
你也可以閱讀由Volodymyr Kuleshov和Isaac Caswell(CS 228)編寫的Notebook版筆記。
本教程使用的Python版本為Python3.
目錄
Scipy
Image operations
MATLAB files
Distance between points
Matplotlib
Plotting
Subplots
Images
原文共分為4部分,分別介紹了Python、Numpy、Scipy和Matplotlib的使用。本次翻譯為最后兩個部分:Scipy和Matplotlib的使用指導!
Scipy
Numpy提供了一個高性能的多維數組以及計算和操作這些數組的基本工具。Scipy(官方鏈接)以此為基礎,提供大量在numpy數組上運行的函數,適用于不同類型的科學和工程應用。熟悉Scipy最好的方法就是瀏覽官方文檔。我們將重點介紹一些對你可能有用的部分。
Image operations
Scipy提供了一些處理圖像的基本函數。例如,它有將圖像從磁盤讀取到numpy數組,將numpy數組作為圖像寫入磁盤以及調整圖像大小的功能。 這是一個展示這些功能的簡單示例:
from scipy.misc import imread, imsave, imresize# Read an JPEG image into a numpy array img = imread('assets/cat.jpg') print(img.dtype, img.shape) # Prints "uint8 (400, 248, 3)"# We can tint the image by scaling each of the color channels # by a different scalar constant. The image has shape (400, 248, 3); # we multiply it by the array [1, 0.95, 0.9] of shape (3,); # numpy broadcasting means that this leaves the red channel unchanged, # and multiplies the green and blue channels by 0.95 and 0.9 # respectively. img_tinted = img * [1, 0.95, 0.9]# Resize the tinted image to be 300 by 300 pixels. img_tinted = imresize(img_tinted, (300, 300))# Write the tinted image back to disk imsave('assets/cat_tinted.jpg', img_tinted)???
Left: The original image. Right: The tinted and resized image.
MATLAB files
scipy.io.loadmat和scipy.io.savemat函數允許你讀取和寫入MATLAB文件。你可以看這篇文檔了解更多。
Distance between points
SciPy定義了一些用于計算各組點之間距離的有用函數。
函數scipy.spatial.distance.pdist計算給定集合中所有點對之間的距離:
import numpy as np from scipy.spatial.distance import pdist, squareform# Create the following array where each row is a point in 2D space: # [[0 1] # [1 0] # [2 0]] x = np.array([[0, 1], [1, 0], [2, 0]]) print(x)# Compute the Euclidean distance between all rows of x. # d[i, j] is the Euclidean distance between x[i, :] and x[j, :], # and d is the following array: # [[ 0. 1.41421356 2.23606798] # [ 1.41421356 0. 1. ] # [ 2.23606798 1. 0. ]] d = squareform(pdist(x, 'euclidean')) print(d)你可以在這篇文檔里了解更多細節。另一個相似函數(scipy.spatial.distance.cdist)計算兩組點之間所有對之間的距離; 你可以在文檔中閱讀它。
Matplotlib
Matplotlib是一個畫圖函數,這部分主要介紹matplotlib.pyplot模塊,作用和MATLAB里的畫圖系統相似。
Plotting
在matplotlib里最重要的函數時plot,使用它可以繪制2D數據,這里有一個簡單例子:
import numpy as np import matplotlib.pyplot as plt# Compute the x and y coordinates for points on a sine curve x = np.arange(0, 3 * np.pi, 0.1) y = np.sin(x)# Plot the points using matplotlib plt.plot(x, y) plt.show() # You must call plt.show() to make graphics appear.只需一點額外工作,我們就可以輕松地一次繪制多條線,并添加標題,圖例和軸標簽:
import numpy as np import matplotlib.pyplot as plt# Compute the x and y coordinates for points on sine and cosine curves x = np.arange(0, 3 * np.pi, 0.1) y_sin = np.sin(x) y_cos = np.cos(x)# Plot the points using matplotlib plt.plot(x, y_sin) plt.plot(x, y_cos) plt.xlabel('x axis label') plt.ylabel('y axis label') plt.title('Sine and Cosine') plt.legend(['Sine', 'Cosine']) plt.show()你可以在這篇文檔里了解更多關于plot的信息。
Subplots
您可以使用子圖功能在同一圖中繪制不同的東西。 這是一個例子:
import numpy as np import matplotlib.pyplot as plt# Compute the x and y coordinates for points on sine and cosine curves x = np.arange(0, 3 * np.pi, 0.1) y_sin = np.sin(x) y_cos = np.cos(x)# Set up a subplot grid that has height 2 and width 1, # and set the first such subplot as active. plt.subplot(2, 1, 1)# Make the first plot plt.plot(x, y_sin) plt.title('Sine')# Set the second subplot as active, and make the second plot. plt.subplot(2, 1, 2) plt.plot(x, y_cos) plt.title('Cosine')# Show the figure. plt.show()你可以在這篇文檔里了解更多關于subplot的信息。
Images
你可以使用imshow函數來顯示圖片,這里有一個例子:
import numpy as np from scipy.misc import imread, imresize import matplotlib.pyplot as pltimg = imread('assets/cat.jpg') img_tinted = img * [1, 0.95, 0.9]# Show the original image plt.subplot(1, 2, 1) plt.imshow(img)# Show the tinted image plt.subplot(1, 2, 2)# A slight gotcha with imshow is that it might give strange results # if presented with data that is not uint8. To work around this, we # explicitly cast the image to uint8 before displaying it. plt.imshow(np.uint8(img_tinted)) plt.show()?
總結
以上是生活随笔為你收集整理的CS231n Convolutional Neural Networks for Visual Recognition------Scipy and MatplotlibTutorial的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电竞选手UZI简自豪官宣结婚 女方晒照透
- 下一篇: 小米笔记本Pro 2022来了:大师级屏