Batch modify your own image dataset based on tensorflow (with code)

       There are many tutorials about Deeplearning on the Internet now, but the datasets of these tutorials are all ready, and the format names and so on have been sorted out. In particular, many introductory tutorials are Mnist datasets, which are already very complete. However, for Xiaobai who wants to make a dataset by himself, how to batch convert the pictures collected by himself into the format he needs, there are not many mentioned on the Internet. As a rookie, now I will share with you the method of making a dataset by myself. If there are any mistakes, please correct me.

       Take a Cat VS Dog binary classification as an example. First, separate the pictures you crawled from the Internet into two folders, and the folder names are classified objects (cat, dog).

Then by calling the function, the name of the picture is uniformly modified, respectively    cat0.jpg , cat1.jpg , cat2.jpg , cat3.jpg ......  

dog.0, dog.1, dog.2, dog.3, dog.4... Rename.

       step 1. Initial files and pictures


Two subfiles under the train file

           


Just climbed down the name of the picture under the cat file (generally, the names of the pictures that are directly crawled down on the Internet are not uniform and will be messy)


No gossip, just go to the code

import them  

def rename(file_dir,name):
    '''Rename the picture crawled down the web (for better viewing)'''
    '''The first parameter is the target file name and the second parameter is the name of the image'''
    i=0
    for file in os.listdir(file_dir):  
        '''Get all pictures under the path file'''    
        src = os.path.join(os.path.abspath(file_dir), file)
        '''The storage location of the modified image (target folder + the name of the new image)'''        
        dst = os.path.join(os.path.abspath(file_dir),  name+str(i) + '.jpg')
        os.rename(src, dst) #Rename the picture
        i=i+1     

file_dir='C:/Users/ASUS/Desktop/train' #Folder name under the target
rename(file_dir+'/cat','cat') #Get the path of the subfile under the target folder and rename it
rename(file_dir+'/dog','dog')


This is the name of the picture under the modified subfolder


 The purpose of modifying the image name is the first step in making your own data set. Next, the blogger will share with you how to use the modified image to make a tfrecord data set. The tfrecord data set can read the data better. Effective use of the computer's GPU, especially when the amount of data is large, can greatly improve the speed of data processing. If there are any deficiencies, please correct me, and hope to make progress together! laughing out loudlaughing out loud  

     

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324622081&siteId=291194637