How Python tells the picture in a file is divided into two

Source: Internet
Author: User
Tags keras

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.