Make your own NPY datasets with data formats similar to mnist data (+)---"deep learning"

Source: Internet
Author: User
Tags split

Code:

Import TensorFlow as TF import numpy as NP import OS from PIL import Image dir= "/home/jobs/pictures/test" def Getfilearr (dir): result_arr=[] label_list=[] map={} map_file_result={} map_file_label={} map_new={} Coun

        T_label=0 count=0 File_list=os.listdir (dir) for file in File_list:file_path=os.path.join (Dir,file) Label=file.split (".")
            [0].split ("_") [0] map[file]=label if label not in Label_list:label_list.append (label) Map_new[label]=count_label count_label=count_label+1 Img=image.open (file_path) result=np.
        Array ([]) R,g,b=img.split () R_arr=np.array (R). Reshape (12544) G_arr=np.array (g). Reshape (12544) B_arr=np.array (b). Reshape (12544) img_arr=np.concatenate ((R_arr,g_arr,b_arr)) Result=np.concatenate ((R Esult,img_arr)) #result =result.reshape ((3,112,112)) Result=result.reshape ((112,112,3)) result=result/255.0 map_file_result[file]=result result_arr.append (Result) count=count+1 for file in File_list:map_file_label[file]=map_new[map[file]] #map [file]=map_new[map[file]] ret_arr=[] for
        File in file_list:each_list=[] Label_one_zero=np.zeros (Count_label) Result=map_file_result[file] Label=map_file_label[file] label_one_zero[label]=1.0 #print (Label_one_zero) Each_list.appe nd (Result) each_list.append (Label_one_zero) ret_arr.append (each_list) os.makedirs ("/home/jobs/desktop/ 
    Data_self ") np.save ('/home/jobs/desktop/data_self/test_data.npy ', Ret_arr) return Ret_arr if __name__==" __main__ ": Ret_arr=getfilearr (dir)

The collection of pictures you use:

Generated NPY datasets:

In the attachment I will upload the corresponding file, you are interested in the download test, you can also use their own data set for testing, note that the format of the picture is 112*112*3, pay attention to the training before the adjustment, attention to the label should also do the corresponding treatment.
You can go to my Resources page to download the appropriate training picture set and the resulting NPY dataset.

Update:
Since the label in front of us is one_hot encoded according to the total number of tags in the statistics, so that the possible encoding is not the same as the encoding we want, we will usually have a mapping relationship map of class tags and numbers, which is used to convert tags to one_hot encoding:
The following code can show this relationship, if we have a map map of our own definition, you can assign a value at the bottom of the map_file_label[file] when you can configure Map_new[map[file]], we can be MAP_ New replaces the map mapping relationship we defined, then we can generate our own custom tags.

#-*-coding=utf-8-*-Import TensorFlow as TF import numpy as NP import OS from PIL import Image dir= "test_data_std/" def Getfilearr (dir): result_arr=[] label_list=[] map={} map_file_result={} map_file_label={} map_new={ } count_label=0 count=0 File_list=os.listdir (dir) for file in File_list:file_path=os.path.join (d Ir,file) Label=file.split (".")
            [0].split ("_") [0] map[file]=label if label not in Label_list:label_list.append (label) Map_new[label]=count_label count_label=count_label+1 Img=image.open (file_path) result=np. 
        Array ([]) R,g,b=img.split () R_arr=np.array (R). Reshape (224*224) G_arr=np.array (g). Reshape (224*224) B_arr=np.array (b). Reshape (224*224) img_arr=np.concatenate ((R_arr,g_arr,b_arr)) Result=np.concaten Ate ((Result,img_arr)) Result=result.reshape ((224,224,3)) result=result/255.0 Map_fiLe_result[file]=result result_arr.append (Result) count=count+1 for file in File_list:map_file _label[file]=map_new[map[file]] #map [file]=map_new[map[file]] ret_arr=[] for file in File_list:e Ach_list=[] Label_one_zero=np.zeros (Count_label) result=map_file_result[file] Label=map_file_label [File] #label_one_zero [label]=1.0 #print (Label_one_zero) each_list.append (Result) each_lis T.append (label) ret_arr.append (each_list) np.save (' Test_data.npy ', Ret_arr) return Ret_arr def To_categor Ial (y,n_classes): Y_std=np.zeros ([Len (y), n_classes]) for I in range (len (y)): y_std[i,y[i]]=1.0 return Y_std def load_data (train_dir,test_dir): Train_data=np.load (Train_dir) test_data=np.load (test_dir) X_train_no n,y_train_non=train_data[:,0],train_data[:,1] x_test_non,y_test_non=test_data[:,0],test_data[:,1] X_train=np.zer OS ([Len (X_train_non), 224,224,3]) X_test=np.zeros ([Len (X_test_non), 224,224,3]) for I in Range (len (X_train_non)): X_train[i,:,:,:]=x_ Train_non[i] for I in range (len (X_test_non)): X_test[i,:,:,:]=x_test_non[i] #y_train_non =y_train_non.tolis T () #y_test_non =y_test_non.tolist () y_train=to_categorial (y_train_non,1000) y_test=to_categorial (y_test_non,10 ) return (X_train,y_train), (x_test,y_test)

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.