如何使用字典值拆分机器学习数据,然后使用它来测试/训练您的模型

时间:2021-06-19 22:03:10

标签: python machine-learning

所以我使用这里的 devangar 字符集:https://www.kaggle.com/ashokpant/devanagari-character-dataset

但我只是在测试和训练辅音... 到目前为止,这是我的代码:

在我的第一个文件中:

#from :https://www.youtube.com/watch?v=j-3vuBynnOE

#importing the libraries to see the actual images
import numpy as np 
import matplotlib.pyplot as plt 
import os
import cv2

#the kaggle download had three folders(consonants, vowels and numbers), looking at the vowels number

DATADIR = 'C:/Users/binju/Downloads/dev/Images/Images'
CATEGORIES = ['character_01_ka', 'character_02_kha',  'character_03_ga',  'character_04_gha',  'character_05_kna', 'character_06_cha','character_07_chha', 'character_08_ja', 'character_09_jha', 'character_10_yna' ,'character_11_taamatar', 'character_12_thaa',  'character_13_daa',  'character_14_dhaa', 'character_15_adna','character_16_tabala','character_17_tha', 'character_18_da',  'character_19_dha', 'character_20_na', 'character_21_pa','character_22_pha', 'character_23_ba', 'character_24_bha','character_25_ma', 'character_26_yaw', 'character_27_ra','character_28_la', 'character_29_waw', 'character_30_motosaw','character_31_petchiryakha','character_32_patalosaw',  'character_33_ha', 'character_34_chhya', 'character_35_tra', 'character_36_gya']

def getTrainingData():
    result = { "images": [], "labelIndexes": [] }
    for category in CATEGORIES:
        print("Getting data for category: ", category)
        for imgName in os.listdir(DATADIR + "/" + category):
            result["images"].append(cv2.imread(DATADIR + "/" + category + "/" + imgName, cv2.IMREAD_GRAYSCALE))
            result["labelIndexes"].append(CATEGORIES.index(category))
    return result

trainingData = getTrainingData()
print("Loaded all images!")

import pickle
print("Dumping images into pickle file")
outputFile = open("trainingData.pickle", "wb")
pickle.dump(trainingData, outputFile)
outputFile.close()

---------然后在我的第二个文件中:------------------------------ ---------------------

import pickle
inputFile = open("trainingData.pickle", "rb")
trainingData = pickle.load(inputFile)
inputFile.close()

print(len(trainingData))
for key in trainingData.keys():
    print(key)

for key in trainingData.keys():
    print(len(trainingData[key]))

#the thing is inside the dictionary, 'labelIndexes' and 'images'
#'images' are the pixel values #images should be pixel values
#'labelIndexes are the categories that it is in' #label Indexes should be the other value
"""
for i in trainingData.keys() :
    print(i, trainingData[i])
"""

for i in range(0,2):
    print(trainingData.get("labelIndexes"))

字典中的值包含图像的类别和像素值,但我不确定接下来要做什么...我想知道是否有人知道如何操作字典以便我可以拆分图像(具有各自的类别),但我不确定...

0 个答案:

没有答案
相关问题