AWA(Animals with Attributes)

AWA(Animals with Attributes)

用于零样本学习的数据集,50个动物类别,85个属性

下载链接如下:

原数据集:http://cvml.ist.ac.at/AwA2/AwA2-data.zip

文件介绍:

JPEGImages:动物图片,子文件夹名为动物名称

licenses:是些用处不大的文件

class.txt:动物列表,顺序很重要!!!

predicates.txt:属性列表,顺序很重要!!!

predicate-matrix-binary.txt:每行对应每个动物的属性,每行对应的动物类型参考class.txt文件,每列对应的属性参考predicates.txt文件,二值属性

predicate-matrix-binary.txt:每行对应每个动物的属性,每行对应的动物类型参考class.txt文件,每列对应的属性参考predicates.txt文件,连续属性,用于后续的unseen样本的分类,值为每个属性的置信度,可能在实际应用中需要归一化

testclasses.txt:用于测试的类别

trainclasses.txt:用于训练的类别

在实际的使用中,原数据集中提供的数据可能不是很方便使用,可自行将txt文件转为csv文件更加方便使用,如下:

 

利用VGG19(在ImageNet数据集上预训练过)对该数据集进行特征提取,选取全连接层的倒数第二层输出作为特征,维度为4096维。

源码如下:

# -*- coding: utf-8 -*-
from keras.applications.vgg19 import VGG19
from keras.preprocessing import image
from keras.applications.vgg19 import preprocess_input
from keras.models import Model
import os
import numpy as np
import scipy.io as sio
import csv
 
 
##获取文件路径列表dir+filename
def get_files():
    attribute_path = r"C:\Users\ZhouFengtao\OneDrive\Program\balabalaba\data\attribute.csv"
    path = r"D:\AwA2-data\Animals_with_Attributes2\JPEGImages"
    class_list = []
    class_num = {}
    class_att = {}
    image_path = []
    image_att = None
 
    attribute_file = csv.reader(open(attribute_path))
    # get attribute
    print('get attribute')
    for row in attribute_file:
        att = [float(x) for x in row[1:]]
        class_att[row[0]] = att
        class_num[row[0]] = 0
 
    for root, dirs, files in os.walk(path):
        for name in files:
            animal = str.split(name, '_')[0]
            if animal not  in class_list:
                class_list.append(animal)
            class_num[animal] = class_num[animal] + 1
            if image_att is None:
                image_att = class_att[animal]
            else:
                image_att = np.row_stack((image_att, class_att[animal]))
            image_path.append(os.path.join(root, name))
    return class_list, class_num, class_att, image_path
 
 
if __name__ == '__main__':
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '0'
    os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
    os.environ["CUDA_VISIBLE_DEVICES"] = '0'
 
    base_model = VGG19(weights='imagenet', include_top=True)
    model_fc1 = Model(inputs=base_model.input, outputs=base_model.get_layer('fc1').output)  # fc1 layer
 
    class_list, class_num, class_att, image_path = get_files()  ##获取图片路径列表
    print(class_list)
    print(class_num)
    print(class_att)
 
    fc1s = None
    atts = None
 
    for i, path in enumerate(image_path):
        print('number ' + str(i) + ' path: ' + path)
        try:
            img = image.load_img(path, target_size=(224, 224))
            x = image.img_to_array(img)
            x = np.expand_dims(x, axis=0)
            x = preprocess_input(x)
            fc1 = model_fc1.predict(x)
            key = str.split(str.split(path, '\\')[-1], '_')[0]
            att = class_att[key]
            if fc1s is None:
                fc1s = fc1
                atts = att
            else:
                fc1s = np.row_stack((fc1s, fc1))
                atts = np.row_stack((atts, att))
        except IOError:
            print('########################Image Error###########################')
    # save extracted feature
    sio.savemat('vgg19_feature', {'fc1': fc1s, 'atts': atts})
    fw = open("animals_num.txt", 'w+')
    fw.write(str(class_num))  # 把字典转化为str
    fw.close()
    fw = open("animals_sequence.txt", 'w')
    fw.write(str(class_list))  # 把list转化为str
    fw.close()

 

你可能感兴趣的:(CV)