{{ message }}

Instantly share code, notes, and snippets.

# takatakamanbou/00_Vision.md Secret

Last active May 7, 2019

# 視覚認知計算特論

## ex0624 (2017)

 0 0.7550326945263522 0.05 1.007767522485821 0.1 0.6361953553367321 0.15 0.6959238424062661 0.2 0.595485181368133 0.25 0.4831892240046355 0.3 0.159575705011379 0.35 0.186501916036992 0.4 0.1567768077057974 0.45 0.04182438372911931 0.5 -0.06982713339289053 0.55 0.08544102980639576 0.6 -0.1007904952533244 0.65 -0.1277469644723259 0.7 -0.02542025832150743 0.75 -0.411322875976739 0.8 0.01309389848625916 0.85 -0.1196592829342933 0.9 0.07144698650328071 0.95 -0.1983213666516035
 import numpy as np import leastsquares as ls dat = np.loadtxt('dat20170410.txt') N = dat.shape[0] print('# N =', N) x = dat[:, 0] D = 1 # D is the degree of the polynomial to be fitted X = np.zeros((N, D+1)) X[:, 0] = 1 for i in range(1, D+1): X[:, i] = x**i print(X) print('# X.shape =', X.shape) y = dat[:, 1] print(y) print('# y.shape =', y.shape) w = ls.solve(X, y) print(w)
 import numpy as np import leastsquares as ls import gendat190428 as gendat # if you have matplotlib, you can set the following variable to True. haveMatplotlib = False def makeDataMatrix(x, D): N = x.shape[0] X = np.zeros((N, D+1)) X[:, 0] = 1 for i in range(1, D+1): X[:, i] = x**i return X ndata = 30 sigma = 0.5 # learning (training) data datL = gendat.gendat(ndata, seed=0, sigma=sigma) # test data datT = gendat.gendat(ndata, seed=1, sigma=sigma) # fitting a D-th degree polynomial by least squares method D = 10 X = makeDataMatrix(datL[:, 0], D) y = datL[:, 1] print('# X.shape =', X.shape) print('# y.shape =', y.shape) w = ls.solve(X, y) print('# estimated coefficients = ', w) # mean squared error for the learning data y_est = np.dot(X, w) msqe = np.mean((y - y_est)**2) print('# mean squared error (L) = ', msqe) # mean squared error for the test data X = makeDataMatrix(datT[:, 0], D) y = datT[:, 1] y_est = np.dot(X, w) msqe = np.mean((y - y_est)**2) print('# mean squared error (T) = ', msqe) # true (noiseless) data & more... datTrue = gendat.gendat(1000) X = makeDataMatrix(datTrue[:, 0], D) y = datTrue[:, 1] y_est = np.dot(X, w) if haveMatplotlib: import matplotlib.pyplot as plt plt.xlim(-2, 3) plt.ylim(-5, 10) plt.plot(datTrue[:, 0], datTrue[:, 1], color="blue") # true data plt.scatter(datL[:, 0], datL[:, 1], color="red") # noisy data plt.plot(datTrue[:, 0], y_est, color="green") # estimated curve plt.show() else: # The graph can be plotted by using gnuplot as follows # gnuplot> plot [-2:3][-5:10] "true.txt" w l, "data.txt" w p, "estimated.txt" w l np.savetxt('true.txt', datTrue) A = np.hstack((datL, datT[:, 1, np.newaxis])) np.savetxt('data.txt', A) B = np.vstack((datTrue[:, 0], y_est)).T np.savetxt('estimated.txt', B)
 import numpy as np import image N = 231 # 総データ数 D = 64*64 # データの次元数 dat = np.empty((N, D)) # データ行列 for i in range(N): # img.shape は (64, 64) img = image.imread('face231/img%03d.png' % i) # それを 4096 次元ベクトルに reshape して dat[i, :] に代入 dat[i, :] = img.reshape(-1) print(dat.shape) # ラベル． 0 が人間女性，1 が人間男性，2 が猫 lab = np.loadtxt('face231/label.txt', dtype = int) print(lab) # 最初の131枚を学習用，残り100枚をテスト用とする datL = dat[:131, :] labL = lab[:131] datT = dat[131:, :] labT = lab[131:] print('# 学習データの数は', datL.shape[0]) print('# うち人間女性は', datL[labL == 0].shape[0]) print('# うち人間男性は', datL[labL == 1].shape[0]) print('# うち猫は', datL[labL == 2].shape[0]) img = np.mean(datL[labL == 2, :], axis = 0).reshape((64, 64)) image.imsave('hoge.png', img)
 from __future__ import absolute_import from __future__ import division from __future__ import print_function import numpy as np def gendat(n, seed = 0, nsig = 0.0): x = np.linspace(-1.5, 2.5, num = n) y = x*x*x - x*x - x + 1.0 np.random.seed(seed) y += nsig * np.random.randn(n) X = np.vstack((x, y)).T return X if __name__ == '__main__': n = 20 seed = 0 X = gendat(n, seed = seed, nsig = 0.5) np.savetxt('hoge.txt', X)
 import numpy as np def gendat(n, seed = 0, sigma = 0.0): x = np.linspace(-1.5, 2.5, num = n) y = x*x*x - x*x - x + 1.0 np.random.seed(seed) y += sigma * np.random.randn(n) X = np.vstack((x, y)).T return X if __name__ == '__main__': # if you have matplotlib, you can set the following variable to True. haveMatplotlib = False seed = 0 data_noisy = gendat(30, seed=seed, sigma=0.5) data_true = gendat(1000, seed=seed, sigma=0) if haveMatplotlib: import matplotlib.pyplot as plt plt.xlim(-2, 3) plt.ylim(-5, 10) plt.plot(data_true[:, 0], data_true[:, 1], color="blue") plt.scatter(data_noisy[:, 0], data_noisy[:, 1], color="red") plt.show() else: # The graph can be plotted by using gnuplot as follows # gnuplot> plot [-2:3][-5:10] "true.txt" w l, "data.txt" w p np.savetxt('true.txt', data_true) np.savetxt('data.txt', data_noisy)
 import scipy.misc as spmisc import numpy as np def imread(fn): img = spmisc.imread(fn) # spmisc.imread returns a 3-D or 2-D array with dtype = uint8 # so its type should be converted # converting to int (pixel vaules are in [0, 255]) #img2 = img.astype(np.int) # converting to float (pixel vaules are in [0, 1]) img2 = img.astype(np.float) / 255 return img2 def imsave(fn, img): # spmisc.imsave can handle all of the above dtypes (uint8, int, float) spmisc.imsave(fn, img) if __name__ == '__main__': img = imread('blackuni3.png') print(img.shape, img.dtype) hoge = 1.0 - img imsave('hoge.png', hoge) hoge2 = img[:, ::-1, :] imsave('hoge2.png', hoge2) hoge3 = (hoge + hoge2)/2 imsave('hoge3.png', hoge3) img = imread('blackuni3-gray.png') print(img.shape, img.dtype) hogeG = 1.0 - img imsave('hogeG.png', hogeG)
 import numpy as np ### solving the linear least squares problem (1) # # X.shape is assumed to be (N, D+1) # y.shape is assumed to be (N,) # def solve(X, y): A = np.dot(X.T, X) b = np.dot(X.T, y) # x is the solution of the equation Ax = b x = np.linalg.solve(A, b) return x ### solving the linear least squares problem (2) # def solve2(X, y): A = np.dot(X.T, X) b = np.dot(X.T, y) # rv[0] is the solution minimizing ||Ax - b||^2 rv = np.linalg.lstsq(A, b) return rv[0] if __name__ == '__main__': x = np.array([0, 4, 8, 20]) y = np.array([30, 31, 29, 28]) y -= y[0] N = x.shape[0] print('# N =', N) X = np.vstack((np.ones(N), x)).T print(X) print('# X.shape =', X.shape) print(y) print('# y.shape =', y.shape) w = solve(X, y) #w = solve2(X, y) print('# estimated parameters =', w) b, a = w # w[0] is b and w[1] is a print('# fitted equation is y =', a, '*x +', b) print(a * 36 + b + 30)
 # -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import numpy as np def getData(nclass, seed = None): assert nclass == 2 or nclass == 3 if seed != None: np.random.seed(seed) # 2次元の spherical な正規分布3つからデータを生成 X0 = 0.10 * np.random.randn(200, 2) + [ 0.3, 0.3 ] X1 = 0.10 * np.random.randn(200, 2) + [ 0.7, 0.6 ] X2 = 0.05 * np.random.randn(200, 2) + [ 0.3, 0.7 ] # それらのラベル用のarray lab0 = np.zeros(X0.shape[0], dtype = int) lab1 = np.zeros(X1.shape[0], dtype = int) + 1 lab2 = np.zeros(X2.shape[0], dtype = int) + 2 # X （入力データ）, label （クラスラベル）, t（教師信号） をつくる if nclass == 2: X = np.vstack((X0, X1)) label = np.hstack((lab0, lab1)) t = np.zeros(X.shape[0]) t[label == 1] = 1.0 else: X = np.vstack((X0, X1, X2)) label = np.hstack((lab0, lab1, lab2)) t = np.zeros((X.shape[0], nclass)) for ik in range(nclass): t[label == ik, ik] = 1.0 return X, label, t if __name__ == '__main__': import matplotlib import matplotlib.pyplot as plt K = 3 X, lab, t = getData(K) fig = plt.figure() plt.xlim(-0.2, 1.2) plt.ylim(-0.2, 1.2) ax = fig.add_subplot(1, 1, 1) ax.set_aspect(1) ax.scatter(X[lab == 0, 0], X[lab == 0, 1], color = 'red') ax.scatter(X[lab == 1, 0], X[lab == 1, 1], color = 'green') if K == 3: ax.scatter(X[lab == 2, 0], X[lab == 2, 1], color = 'blue') plt.show()