Bạn nào sành python nhờ chạy cái file mình upload và chỉ dẫn mình lỗi như hình sau với, cảm ơn nhiều
code
import numpy as np
from sklearn import linear_model # for logistic regression
from sklearn.metrics import accuracy_score # for evaluation
from scipy import misc # for loading image
np.random.seed(1) # for fixing random values
path = 'D:\\0China\\0Python3\\ARdatabase\\AR_warp_zip\\test2' # path to the database
train_ids = np.arange(1, 26)
#print("print train_ids",train_ids)
test_ids = np.arange(26, 50)
#print("test_ids", test_ids)
view_ids = np.hstack((np.arange(1, 8), np.arange(14, 21)))
#print("view ids is: ",view_ids)
D = 165*120 # original dimension
d = 500 # new dimension
# generate the projection matrix
ProjectionMatrix = np.random.randn(D, d)
#print("Pinrt ProjectionMatrix, I want to see", ProjectionMatrix)
def build_list_fn(pre, img_ids, view_ids):
"""
INPUT:
pre = 'M-' or 'W-'
img_ids: indexes of images
view_ids: indexes of views
OUTPUT:
a list of filenames
"""
list_fn = []
for im_id in img_ids:
for v_id in view_ids:
fn = path + pre + str(im_id).zfill(3) + '-' + \
str(v_id).zfill(2) + '.bmp'
list_fn.append(fn)
return list_fn
def rgb2gray(rgb):
# Y' = 0.299 R + 0.587 G + 0.114 B
return rgb[:,:,0]*.299 + rgb[:, :, 1]*.587 + rgb[:, :, 2]*.114
# feature extraction
def vectorize_img(filename):
# load image
rgb = misc.imread(filename)
# convert to gray scale
gray = rgb2gray(rgb)
# vectorization each row is a data point
im_vec = gray.reshape(1, D)
return im_vec
def build_data_matrix(img_ids, view_ids):
total_imgs = img_ids.shape[0]*view_ids.shape[0]*2
print("print example total_imgs", total_imgs)
X_full = np.zeros((total_imgs, D))
y = np.hstack((np.zeros((int(total_imgs/2), )), np.ones((int(total_imgs/2), ))))
#print("example printer", np.hstack((np.zeros((total_imgs/2)))))
list_fn_m = build_list_fn('M-', img_ids, view_ids)
list_fn_w = build_list_fn('W-', img_ids, view_ids)
list_fn = list_fn_m + list_fn_w
for i in range(len(list_fn)):
X_full[i, :] = vectorize_img(list_fn[i])
X = np.dot(X_full, ProjectionMatrix)
return (X, y)
(X_train_full, y_train) = build_data_matrix(train_ids, view_ids)
x_mean = X_train_full.mean(axis = 0)
x_var = X_train_full.var(axis = 0)
def feature_extraction(X):
return (X - x_mean)/x_var
X_train = feature_extraction(X_train_full)
X_train_full = None ## free this variable
(X_test_full, y_test) = build_data_matrix(test_ids, view_ids)
X_test = feature_extraction(X_test_full)
X_test_full = None
def rgb2gray(rgb):
# Y' = 0.299 R + 0.587 G + 0.114 B
return rgb[:,:,0]*.299 + rgb[:, :, 1]*.587 + rgb[:, :, 2]*.114
# feature extraction
def vectorize_img(filename):
# load image
rgb = misc.imread(filename)
# convert to gray scale
gray = rgb2gray(rgb)
# vectorization each row is a data point
im_vec = gray.reshape(1, D)
return im_vec
def build_data_matrix(img_ids, view_ids):
total_imgs = img_ids.shape[0]*view_ids.shape[0]*2
X_full = np.zeros((total_imgs, D))
y = np.hstack((np.zeros((total_imgs/2, )), np.ones((total_imgs/2, ))))
list_fn_m = build_list_fn('M-', img_ids, view_ids)
list_fn_w = build_list_fn('W-', img_ids, view_ids)
list_fn = list_fn_m + list_fn_w
for i in range(len(list_fn)):
X_full[i, :] = vectorize_img(list_fn[i])
X = np.dot(X_full, ProjectionMatrix)
return (X, y)
(X_train_full, y_train) = build_data_matrix(train_ids, view_ids)
x_mean = X_train_full.mean(axis = 0)
x_var = X_train_full.var(axis = 0)
def feature_extraction(X):
return (X - x_mean)/x_var
X_train = feature_extraction(X_train_full)
X_train_full = None ## free this variable
(X_test_full, y_test) = build_data_matrix(test_ids, view_ids)
X_test = feature_extraction(X_test_full)
X_test_full = None
logreg = linear_model.LogisticRegression(C=1e5) # just a big number
logreg.fit(X_train, y_train)
y_pred = logreg.predict(X_test)
print("Accuracy: %.2f %%" %(100*accuracy_score(y_test, y_pred)))