如何在深度学习中为多个类创建多维NumPy数组?

问题描述

我正在跟踪暹罗网络example以进行人脸识别,它仅使用两个类别,每个类别每个 3张图片(即共6张图片) 。我如何修改以下代码以读取并标记 6个图像类别,每个类别每个 10张图像(即总共60张图像)?

sub_dir_list = os.listdir( dir_path )
images = list()
labels = list()
for i in range( len( sub_dir_list ) ):
    label = i
    image_names = os.listdir( os.path.join(dir_path,sub_dir_list[i]) )
    for image_path in image_names:
        path = os.path.join(dir_path,sub_dir_list[i],image_path )
        try :
            image = Image.open(path)
            resize_image = image.resize((dimen,dimen))
            array_ = list()
            for x in range(dimen):
                sub_array = list()
                for y in range(dimen):
                    sub_array.append(resize_image.load()[x,y])
                array_.append(sub_array)
            image_data = np.array(array_)
            image = np.array(np.reshape(image_data,(dimen,dimen,3))) / 255
            images.append(image)
            labels.append(label)
        except:
            print( 'WARNING : File {} Could not be processed.'.format( path ) )

images = np.array( images )

samples_1 = list()
samples_2 = list()
labels = list()
for i in range( 6 ) :
    for j in range( 6 ) :
        samples_1.append( images[i] )
        samples_2.append( images[j] )
        if i < 3 :
            if j < 3 :
                labels.append( 1 )
            else:
                labels.append( 0 )
        else :
            if j > 2 :
                labels.append( 1 )
            else:
                labels.append( 0 )

X1 = np.array( samples_1  )
X2 = np.array( samples_2 )
Y = np.array( labels )

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)