我们可以使用pickle将训练好的数据集保存为xml格式吗?

时间:2019-01-23 09:34:36

标签: python-3.x

我们可以使用泡菜将自己训练有素的数据集以xml格式存储吗?

import numpy as np 
import random
import pickle
import matplotlib
gui_env = [i for i in matplotlib.rcsetup.interactive_bk]
non_gui_backends = matplotlib.rcsetup.non_interactive_bk
print ("Non Gui backends are:", non_gui_backends)
print ("Gui backends I will test for", gui_env)
for gui in gui_env:
    print ("testing", gui)
    try:
        matplotlib.use(gui,warn=False, force=True)
        from matplotlib import pyplot as plt
        print ("    ",gui, "Is Available")
        plt.plot([1.5,2.0,2.5])
        fig = plt.gcf()
        fig.suptitle(gui)
        print ("Using ..... ",matplotlib.get_backend())
    except:
        print ("    ",gui, "Not found")

import os
import cv2
from tqdm import tqdm

DATADIR = "Datasets/PetImages"

CATEGORIES = ["Dog", "Cat"]

training_data = []
IMG_SIZE = 50

def create_training_data():
    for category in CATEGORIES: 

        path = os.path.join(DATADIR,category) 
        class_num = CATEGORIES.index(category) 

        for img in tqdm(os.listdir(path)):  
            try:
                img_array = cv2.imread(os.path.join(path,img) ,cv2.IMREAD_GRAYSCALE) 
                new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
                training_data.append([new_array, class_num])  
            except Exception as e:  
                pass
    random.shuffle(training_data)
    X = []
    y = []

    for features,label in training_data:
        X.append(features)
        y.append(label)

    print(X[0].reshape(-1, IMG_SIZE, IMG_SIZE, 1))

    X = np.array(X).reshape(-1, IMG_SIZE, IMG_SIZE, 1)
    pickle_out = open("X.xml","wb")
    pickle.dump(X, pickle_out)
    pickle_out.close()

    pickle_out = open("y.xml","wb")
    pickle.dump(y, pickle_out)
    pickle_out.close()

create_training_data()

print(len(training_data))

0 个答案:

没有答案