可能看着不是那么明确,但是每一句,每个方法都查过之后还是收获满满。fighting------
def OHE_labels(Y_tr,N_classes):(一)功能:返回Y_labels这个数组
OHC = OneHotEncoder()
Y_ohc = OHC.fit(np.arange(N_classes).reshape(-1, 1))
##arange将这么些类生成一维数组,reshape将其转化为多维,此处将其重塑成?行,1列。-1表示待定
Y_labels = Y_ohc.transform(Y_tr.reshape(-1, 1)).toarray()
return Y_labels
training_file = 'TrafficSignData/train.p'
testing_file = 'TrafficSignData/test.p'
with open(training_file, mode='rb') as f:
train = pickle.load(f)
with open(testing_file, mode='rb') as f:
test = pickle.load(f)
X_train, y_train = train['features'], train['labels']
##X_train, y_train得到的是字典的键
X_test, y_test = test['features'], test['labels']
labels_train = OHE_labels(y_train,43)
##y_train的labels经过函数一变为43行1列的矩阵再变为数组:相当于转置吧!
labels_test = OHE_labels(y_test,43)
def check_OHE(cls,y):(二)功能:核实函数一的功能
check = np.linalg.norm(np.argmax(cls,axis=1)-y)
if check == 0:
print('One hot encoding correct, ok to proceed')
else:
print('One hot encoding doesnt match the output, check code!!!')
##检查上一步的one-hot码是否正确
n_train = len(y_train)
n_test = len(y_test)
image_shape = np.shape(X_train[1])
##数据集【1】那里是数字的类吧!!!
n_classes = len(np.unique(y_train))
##len:取出来唯一那个数放在容器里返回容器中的项数。
def plot_random_3C(n_row,n_col,X,y):(三)功能
plt.figure(figsize = (11,8))
##画一个图,宽11,高8
gs1 = gridspec.GridSpec(n_row,n_row)
##划分成多少行多少列
gs1.update(wspace=0.01, hspace=0.02)
# 设置轴之间的空隙
for i in range(n_row*n_col):
## i = i + 1 下标从0开始
ax1 = plt.subplot(gs1[i])
plt.axis('on')
ax1.set_xticklabels([])
ax1.set_yticklabels([])
ax1.set_aspect('equal')
ind_plot = np.random.randint(1,len(y))
##随机得到长度为y,最小为1的数组
plt.imshow(X[ind_plot])
plt.text(2,4,str(y[ind_plot]),
color='k',backgroundcolor='c')
plt.axis('off')
plt.show()
def plot_random_1C(n_row,n_col,X,y):
plt.figure(figsize = (11,8))
gs1 = gridspec.GridSpec(n_row,n_row)
gs1.update(wspace=0.01, hspace=0.02) # set the spacing between axes.
for i in range(n_row*n_col):
# i = i + 1 # grid spec indexes from 0
ax1 = plt.subplot(gs1[i])
plt.axis('on')
ax1.set_xticklabels([])
ax1.set_yticklabels([])
ax1.set_aspect('equal')
#plt.subplot(4,11,i+1)
ind_plot = np.random.randint(1,len(y))
plt.imshow(X[ind_plot],cmap='gray')
plt.text(2,4,str(y[ind_plot]),
color='k',backgroundcolor='c')
plt.axis('off')
plt.show()
def plot_random_preprocess(n_row,n_col,X,y):
plt.figure(figsize = (11,8))
gs1 = gridspec.GridSpec(n_row,n_row)
gs1.update(wspace=0.01, hspace=0.02) # set the spacing between axes.
for i in range(n_row*n_col):
# i = i + 1 # grid spec indexes from 0
ax1 = plt.subplot(gs1[i])
plt.axis('on')
ax1.set_xticklabels([])
ax1.set_yticklabels([])
ax1.set_aspect('equal')
#plt.subplot(4,11,i+1)
ind_plot = np.random.randint(1,len(y))
plt.imshow(pre_process_image(X[ind_plot]),cmap='gray')
plt.text(2,4,str(y[ind_plot]),
color='k',backgroundcolor='c')
plt.axis('off')
plt.show()
def pre_process_image(image):
#image = cv2.cvtColor(image, cv2.COLOR_BGR2YUV)只保留明亮度(Y)色度(U、V)
#image = image[:,:,0]
image[:,:,0] = cv2.equalizeHist(image[:,:,0])
image[:,:,1] = cv2.equalizeHist(image[:,:,1])
image[:,:,2] = cv2.equalizeHist(image[:,:,2])
##直方图均衡化
image = image/255.-.5
#image = cv2.resize(image, (img_resize,img_resize),interpolation = cv2.INTER_CUBIC)
return image
.......
中间有个问题:
plt.figure(figsize=(12,8)) plt.bar(range(43),height=data_pd_sorted["Occurance"])
在anaconda里可以实现,在pycharm实现不了,既不可以放在main函数里,也不能用print,改为下面的语句即可实现
height = data_pd_sorted["Occurance"]#先把要画的图定成一个常量 plt.figure(figsize=(12,8))#进行坐标轴建立 p1=plt.bar(range(43),height=data_pd_sorted["Occurance"])#用不用p1变量承接都可以 plt.show()#show才是可以让这个图画出来的关键方法
img_resize = 32 def transform_image(image,ang_range,shear_range,trans_range): ang_rot = np.random.uniform(ang_range)-ang_range/2 rows,cols,ch = image.shape Rot_M = cv2.getRotationMatrix2D((cols/2,rows/2),ang_rot,1) # Translation tr_x = trans_range*np.random.uniform()-trans_range/2 tr_y = trans_range*np.random.uniform()-trans_range/2 Trans_M = np.float32([[1,0,tr_x],[0,1,tr_y]]) # Shear pts1 = np.float32([[5,5],[20,5],[5,20]]) pt1 = 5+shear_range*np.random.uniform()-shear_range/2 pt2 = 20+shear_range*np.random.uniform()-shear_range/2 pts2 = np.float32([[pt1,5],[pt2,pt1],[5,pt2]]) shear_M = cv2.getAffineTransform(pts1,pts2) image = cv2.warpAffine(image,Rot_M,(cols,rows)) image = cv2.warpAffine(image,Trans_M,(cols,rows)) image = cv2.warpAffine(image,shear_M,(cols,rows)) image = pre_process_image(image) #image = cv2.cvtColor(image, cv2.COLOR_BGR2YUV) #image = image[:,:,0] #image = cv2.resize(image, (img_resize,img_resize),interpolation = cv2.INTER_CUBIC) return image def get_index_dict(y_train): # Returns indices of each label # Assumes that the labels are 0 to N-1 dict_indices = {} ind_all = np.arange(len(y_train)) for i in range(len(np.unique(y_train))): ind_i = ind_all[y_train == i] dict_indices[i] = ind_i #print(ind_i) return dict_indices def gen_transformed_data(X_train,y_train,N_classes,n_each,ang_range,shear_range,trans_range,randomize_Var): dict_indices = get_index_dict(y_train) n_class = len(np.unique(y_train)) X_arr = [] Y_arr = [] for i in range(n_class): len_i = len(dict_indices[i]) ind_rand = np.random.randint(0,len_i,n_each) ind_dict_class = dict_indices[i] for i_n in range(n_each): img_trf = transform_image(X_train[ind_dict_class[ind_rand[i_n]]], ang_range,shear_range,trans_range) X_arr.append(img_trf) Y_arr.append(i) X_arr = np.array(X_arr,dtype = np.float32()) Y_arr = np.array(Y_arr,dtype = np.float32()) if (randomize_Var == 1): len_arr = np.arange(len(Y_arr)) np.random.shuffle(len_arr) X_arr[len_arr] = X_arr Y_arr[len_arr] = Y_arr labels_arr = OHE_labels(Y_arr,43) return X_arr,Y_arr,labels_arr def gen_extra_data(X_train,y_train,N_classes,n_each,ang_range,shear_range,trans_range,randomize_Var): dict_indices = get_index_dict(y_train) n_class = len(np.unique(y_train)) X_arr = [] Y_arr = [] n_train = len(X_train) for i in range(n_train): #X_arr.append(transform_image(X_train[i],0,0,0)/255.) #Y_arr.append(y_train[i]) for i_n in range(n_each): img_trf = transform_image(X_train[i], ang_range,shear_range,trans_range) X_arr.append(img_trf) Y_arr.append(y_train[i]) X_arr = np.array(X_arr,dtype = np.float32()) Y_arr = np.array(Y_arr,dtype = np.float32()) if (randomize_Var == 1): len_arr = np.arange(len(Y_arr)) np.random.shuffle(len_arr) X_arr[len_arr] = X_arr Y_arr[len_arr] = Y_arr labels_arr = OHE_labels(Y_arr,43) return X_arr,Y_arr,labels_arr