Recently in the race to do an image classification, as the first contact with deep learning Rookie, get started Keras. To tell the truth, in addition to the Keras tutorial, Chinese Blog Technical support is too poor. In the study of the big head ... Needless to say, record some of the small details of your study. In the Encounter generator.flow_from_directory (' Data/train ' ...) This function, you need to enter the path of the training picture. At this point the training image and the verification image are in one directory, so I need to reconstruct two files to put the training image and verify the image separately. To begin, I would like to use OPENCV to read each picture, and then re-write to the new folder based on whether the training image or the verification image. The From Shutil import copy2 function in Python was later found to place the picture directly under the specified path.
ImportOSImportRandomImportShutil fromShutilImportCopy2trainfiles= Os.listdir ('Data/train') Num_train=Len (trainfiles) index_list=Range (Num_train) random.shuffle (index_list) Num=0trainDir='Data/pre_train'Validdir='Data/pre_valid' forIinchIndex_list:filename= Os.path.join ('Data/train', Trainfiles[i])ifNum < num_train*0.8: Copy2 (FileName, Traindir)Else: Copy2 (fileName, validdir) num+ = 1
Simple idea: The original ' Data/train ' path under the name of the image read out, and then disrupt the order of all the pictures, the total number of images scrambled to the top 80% of the picture as training data into the new ' Data/pre_train ' directory, the rest of the picture put to ' data/pre_ Valid ' directory.
How Python tells the picture in a file is divided into two