主要是图片处理的几个函数
第一个get_files(file_dir, ratio): 函数,输入获取文件的路径(注意根据函数的写法,路径中包含不同类别的文件夹),验证集所占比例
# step1:返回的四个值,分别是训练集的图片路径数组,训练集图片所属类别的数组;验证集图片路径数组,验证集图片所属类别的数组 def get_files(file_dir, ratio): for file in os.listdir(file_dir + '/speed_detecter'): speed_detecter.append(file_dir + '/speed_detecter' + '/' + file) label_speed_detecter.append(0) for file in os.listdir(file_dir + '/non_speed_detecter'): non_speed_detecter.append(file_dir + '/non_speed_detecter' + '/' + file) label_non_speed_detecter.append(1) # step2:对生成的图片路径和标签List做打乱处理把cat和dog合起来组成一个list(img和lab) image_list = np.hstack((speed_detecter, non_speed_detecter)) label_list = np.hstack((label_speed_detecter, label_non_speed_detecter)) # 利用shuffle打乱顺序 temp = np.array([image_list, label_list]) temp = temp.transpose() np.random.shuffle(temp) # 从打乱的temp中再取出list(img和lab) # image_list = list(temp[:, 0]) # label_list = list(temp[:, 1]) # label_list = [int(i) for i in label_list] # return image_list, label_list # 将所有的img和lab转换成list all_image_list = list(temp[:, 0]) all_label_list = list(temp[:, 1]) # 将所得List分为两部分,一部分用来训练tra,一部分用来测试val # ratio是测试集的比例 n_sample = len(all_label_list) n_val = int(math.ceil(n_sample * ratio)) # 测试样本数 n_train = n_sample - n_val # 训练样本数 tra_images = all_image_list[0:n_train] tra_labels = all_label_list[0:n_train] tra_labels = [int(float(i)) for i in tra_labels] val_images = all_image_list[n_train:-1] val_labels = all_label_list[n_train:-1] val_labels = [int(float(i)) for i in val_labels] return tra_images, tra_labels, val_images, val_labels
调用函数中输入参数打印信息,输出结果如下:
tra_images
['E:\\CNN\\TestImage/speed_detecter/20170929112354600_28.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930070459340_8.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170928124932890_10.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170929094427000_10.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170901083002860_24.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901081855100_19.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901071453220_28.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930155245700_5.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170929124613890_10.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901071457740_2.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901071122500_22.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901072854740_3.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170929103025880_28.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170929092841720_29.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170901081854380_18.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901093732500_22.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170929162542880_1.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901081850220_16.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901071121620_21.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930084735340_6.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170929154112600_16.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930170945340_18.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930084730700_2.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901093725100_18.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170929081344720_22.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930103332860_1.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170929114754360_11.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930084732220_5.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901093855500_24.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170928084203410_13.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901090458100_29.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170929112220480_22.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170901080553620_11.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901093726100_19.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901080558620_14.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901090504100_5.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901090018620_28.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901082957980_21.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901071452380_27.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170929154112880_17.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930085419340_12.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170901090502620_2.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170929104304240_4.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901072855860_4.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901071127380_25.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930092936990_2.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901090928500_10.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901081850980_17.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930091554580_21.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170929094424880_8.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170929092614600_23.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901072856740_5.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930084731460_4.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930171648100_25.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930101823460_25.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901090831380_6.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901080552740_10.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901091607860_16.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930114031340_17.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901071451620_26.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170901072859980_6.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901083003740_25.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930072424980_14.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901090833380_8.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901090503380_3.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170929085100010_2.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170929123804880_5.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170901093730500_21.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901071126380_24.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930093103100_7.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901071456860_1.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901093726980_20.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170929115729480_17.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930124601710_19.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901082958860_22.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901090931860_12.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901080557740_13.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901080556740_12.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170901071456100_29.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170929164023240_7.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170928091310410_19.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170929070832720_18.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930154652700_29.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901081849220_15.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170929090400480_14.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901090017220_27.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930124606980_22.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170929085720890_9.jpg']
tra_labels
[0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0]
val_images
['E:\\CNN\\TestImage/speed_detecter/20170929121738600_29.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901090503380_4.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930104234460_10.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930080206710_21.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901090458860_30.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901091606500_15.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901093854500_23.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930120934340_8.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901091609220_17.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170901071125620_23.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930120225860_2.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170929124615890_12.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170929120151240_22.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901082959620_23.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901072900860_7.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170929094425880_9.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170929111236720_17.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170928123715010_3.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901090927500_9.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901071456860_30.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170901090932620_13.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901090459620_1.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930115759460_26.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930124605100_20.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901083004740_26.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930070211700_1.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930080204860_19.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901072901740_8.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170929110056480_10.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170929101147240_24.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930081030340_28.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930121947220_15.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170929113210720_5.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170929101145360_22.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901090832380_7.jpg', 'E:\\CNN\\TestImage/speed_detecter/20170930084730580_1.jpg', 'E:\\CNN\\TestImage/non_speed_detecter/20170901081855980_20.jpg']
val_labels
[0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1]
由结果看出,输出为用数组保存的将读取的文件分为测试集与验证集路径,labels为图片所属类别