使用类标签和数据创建numpy数组

2024-04-20 06:28:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望实现一个类似于下面的代码,因为我正在一个新的、类似的数据集上尝试它。我需要的是调整它,以我有:2类和5个例子为每个。 如何以类似的方式将其放入numpy数组中?你知道吗

这是原始代码:

train_split_path = os.path.join(root_dir, 'splits', 'train.txt')

with open(train_split_path, 'r') as train_split:
      train_classes = [line.rstrip() for line in train_split.readlines()]

#number of classes
no_of_classes = len(train_classes)

#number of examples
num_examples = 20

#image width
img_width = 28

#image height
img_height = 28
channels = 1

train_dataset = np.zeros([no_of_classes, num_examples, img_height, img_width], dtype=np.float32)


for label, name in enumerate(train_classes):
    alphabet, character, rotation = name.split('/')
    rotation = float(rotation[3:])
    img_dir = os.path.join(root_dir, 'data', alphabet, character)
    img_files = sorted(glob.glob(os.path.join(img_dir, '*.png')))


    for index, img_file in enumerate(img_files):
        values = 1. - np.array(Image.open(img_file).rotate(rotation).resize((img_width, img_height)), np.float32, copy=False)
        train_dataset[label, index] = values

train_dataset.shape

结果是:

(4112, 20, 28, 28)

我没有类的txt文件,因为它们的类名是1和2。你知道吗

这是原始代码的链接:

https://github.com/sudharsan13296/Hands-On-Meta-Learning-With-Python/blob/master/03.%20Prototypical%20Networks%20and%20its%20Variants/3.3%20Omniglot%20Character%20set%20classification%20using%20Prototypical%20Network.ipynb

这就是我所尝试的:

arr = []


def filter_banks(wav, arr):

a = filt_banks(r'...\vowels\2.wav', arr)

no_of_classes = len(a)

#number of examples
num_examples = 5

#image width
width = 28

#image height
height = 28
channels = 3

train_dataset = np.zeros([no_of_classes, num_examples, height, width], dtype=np.float32)

for i in range(len(a)):
    train_dataset[no_of_classes]=np.array(i).resize([height,width])

我得到以下信息:

IndexError: index 2 is out of bounds for axis 0 with size 2

我怎样才能得到与前面所说的相似的结果?你知道吗


Tags: ofpathnoinimgfordirnp
1条回答
网友
1楼 · 发布于 2024-04-20 06:28:01

试试这个,除了那个np.零通过大小、高度和宽度的图像矩阵


for label in number_of_classes:
    for example in number_of_examples:
        data[label,example] = np.zeros((height,width))


相关问题 更多 >