# -*- coding: utf-8 -*-
"""
Created on Sat Apr 15 23:46:38 2017
@author: Aprameyo
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from scipy.misc import imresize
import os
import urllib
import tensorflow as tf
import cv2 as cv2
import numpy as np
import tflearn
from tflearn.data_utils import image_preloader
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.normalization import local_response_normalization
from tflearn.layers.estimator import regression
Tensor = np.zeros((112,92,280))
Tensor_V = np.zeros((112,92,120))
K = 0
for x in range(1, 41):
directory = "s%d"%x
os.chdir(directory)
for y in range(1,8):
image = "%d.pgm"%y
Tensor[:,:,K] = (cv2.imread(image,-1))
K = K+1
os.chdir("../")
#Tensor_Fin = tf.convert_to_tensor(Tensor, dtype=tf.float32)
T =0
Targets = np.zeros((280,40))
for x in range(0,40):
for y in range(0,7):
Targets[T,x] = 1
T = T+1
K1 = 0
for x in range(1, 41):
directory = "s%d"%x
os.chdir(directory)
for y in range(8,11):
image = "%d.pgm"%y
Tensor_V[:,:,K1] = (cv2.imread(image,-1))
K1 = K1+1
os.chdir("../")
#Tensor_Fin = tf.convert_to_tensor(Tensor, dtype=tf.float32)
T1 =0
Targets_V = np.zeros((120,40))
for x in range(0,40):
for y in range(0,3):
Targets_V[T1,x] = 1
T1 = T1+1
Tensor_Train = Tensor
Targets_Train = Targets
Tensor_Vali = Tensor_V
Targets_Vali = Targets_V
Tensor_Train = Tensor_Train.reshape([-1, 112, 92, 1])
Tensor_Vali = Tensor_Vali.reshape([-1, 112, 92, 1])
network = input_data(shape=[None, 112, 92, 1], name='input')
network = conv_2d(network, 32, 3, activation='relu', regularizer="L2")
network = max_pool_2d(network, 2)
network = local_response_normalization(network)
network = conv_2d(network, 64, 3, activation='relu', regularizer="L2")
network = max_pool_2d(network, 2)
network = local_response_normalization(network)
network = fully_connected(network, 128, activation='sigmoid')
network = dropout(network, 0.8)
network = fully_connected(network, 256, activation='sigmoid')
network = dropout(network, 0.8)
network = fully_connected(network, 40, activation='softmax')
network = regression(network, optimizer='adam', learning_rate=0.01,
loss='categorical_crossentropy', name='target')
# Training
model = tflearn.DNN(network, tensorboard_verbose=0)
model.fit({'input': Tensor_Train}, {'target': Targets_Train},n_epoch=20,
validation_set=({'input': Tensor_Vali}, {'target':Targets_Vali}),
snapshot_step=100, show_metric=True, run_id='convnet_mnist')
我通过将其保存在名为att_faces的目录中来实现此代码,该目录包含10个人的图像,并且有40个这样的人,训练集包括其中的7个,并且测试包括其中的3个。我无法弄清楚为什么准确性如此可恶。