人脸识别角膜维数问题

时间:2018-12-23 16:55:56

标签: python python-3.x tensorflow machine-learning keras

我们正尝试使用keras进行图像识别,但遇到以下错误:ValueError: Error when checking input: expected conv2d_93_input to have 4 dimensions, but got array with shape (4999, 40)。因此,我们使用imread读取图像数据,然后将其放在数组中,但出于某些原因,keras想要一个第四维度。

这是我们读取文件的方式:

def generator(BatchSize):
text_file = open("/content/list_attr_celeba.txt", "r")
lines = text_file.readlines()
lines = lines[2:]
prew = 1
e = []
while True:
    for i in range(prew,prew+BatchSize):
        #print(i)
        lines[i] = lines[i].split()
        name = lines[i][0]
        lines[i] =  lines[i][1:]
        a = imread('/content/img_align_celeba/' + name)
        #b =  numpy.zeros(4,1)
        #print(a)
        e.append(numpy.array(a))            
        if i % BatchSize == 0 and i != 0:
            yield (numpy.array(lines[prew:i]),e)                
            e = []
            prew = i+1

这就是我们定义生成器和模型的方式

 gen = generator(5000)
 model = Sequential()
model.add(Conv2D(32, kernel_size=(5, 5), strides=(1, 1),
             activation='relu',
             input_shape=((170,140,3))))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Conv2D(64, (5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(1000, activation='relu'))
model.add(Dense(40, activation='sigmoid'))
model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = 
['accuracy'])

这就是我们适合的方式

model.fit_generator(gen, epochs=2, verbose=1,  max_queue_size=10, 
workers=1, use_multiprocessing=False, shuffle=False, initial_epoch=0, 
steps_per_epoch = 4)

1 个答案:

答案 0 :(得分:2)

将数据重塑为(4999, 40,1),例如添加尺寸为1的尺寸,conv2d期望(batch_size, x, y, filters)

a = numpy.array(a)
e.append(a.reshape((a.shape + (1,)))