我更改了张量流的cifar10网络代码(https://www.tensorflow.org/versions/r0.10/tutorials/deep_cnn/index.html),以使用我自己的数据库预测图像。
它正在工作,“评估”是预测图像类的功能。在此函数中,“image”是包含数据库的所有图像的列表,参数是包含数据库的一些信息的对象。问题是因为预测单个图像的时间随着时间的推移而增加,例如:预测第一批图像的时间是1秒,预测第二批图像的时间是5秒,预测时间的时间是第三批图像是20秒...我不知道它为什么会发生,因为所有批次都具有相同的大小。有人知道为什么会这样吗?
def evaluate(parameters, images):
saver = tf.train.Saver()
with tf.Session() as sess:
if os.path.isfile(parameters.DATA_DIR + "features/model.ckpt"):
saver.restore(sess, parameters.DATA_DIR + "features/model.ckpt")
else:
raise ValueError('No checkpoint file found')
len_ = (np.uint16(len(images)/parameters.BATCH_SIZE) + 1) * parameters.BATCH_SIZE
features_vector = np.zeros((len_,192))#192 is the number of features
cont=0
while(cont <= len(images)):
aux = []
for i in range(cont,cont+parameters.BATCH_SIZE):
if(i < len(images)):
aux.append(tf.image.per_image_whitening(tf.cast(images[i], tf.float32)))
else:
aux.append(tf.cast(images[-1], tf.float32))
aux = tf.reshape(aux, [parameters.BATCH_SIZE,parameters.IMAGE_SIZE1,parameters.IMAGE_SIZE2,parameters.NUM_CHANNELS])
features_vector[cont:cont+parameters.BATCH_SIZE,:] = np.array(sess.run(parameters.FEATURES_LAYER, feed_dict={parameters.X: sess.run(aux)})).reshape(parameters.BATCH_SIZE,192)#192 is the number of features
cont+=parameters.BATCH_SIZE
print(cont)
return features_vector[0:len(images),:]
class Parameters(object):
IMAGE_SIZE1 = 0
IMAGE_SIZE2 = 0
NUM_CHANNELS = 0
# Global constants describing the CIFAR-10 data set.
NUM_CLASSES = 0
NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN = 0
BATCH_SIZE = 0
DATA_DIR = ""
CLASSES = []
EXT = ""
PATH_TEST = ""
FEATURES_LAYER = []
X = []
LEARNING_RATE = 0
NUM_EPOCHS = 0
#Filling the parameters
def __init__(self, batch_size, data_dir, folders, ext, path_test,list_parameters):
self.BATCH_SIZE = batch_size
self.DATA_DIR = data_dir
self.EXT = ext
self.PATH_TEST = path_test
#Learning rate and num of epochs
if(list_parameters[0] != "" and list_parameters[1] != ""):
self.LEARNING_RATE = np.double(list_parameters[0])
self.NUM_EPOCHS = int(list_parameters[1])
#name of the classes
for i in folders:
self.CLASSES.append(i.split("/")[-2])
#number of examples per epochs for train
name_images = glob.glob(folders[0] + "*" + self.EXT)
self.NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN+= len(name_images)
#number of classes
self.NUM_CLASSES = len(self.CLASSES)
#Size and number of channels of the images
#name_images = glob.glob(folders[0] + "*" + self.EXT)
im = imread(name_images[0])
self.IMAGE_SIZE1 = im.shape[0]
self.IMAGE_SIZE2 = im.shape[0]
if(len(im.shape) > 2):
self.NUM_CHANNELS = 3
else:
self.NUM_CHANNELS = 1
self.X = tf.placeholder(tf.float32, [batch_size, self.IMAGE_SIZE1,self.IMAGE_SIZE2,self.NUM_CHANNELS])
self.FEATURES_LAYER = cnn_tensorFlow.inference_pred(self)