加载检查点并使用Tensorflow DNN评估单个图像 [英] Load checkpoint and evaluate single image with tensorflow DNN

查看:102
本文介绍了加载检查点并使用Tensorflow DNN评估单个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了在大学里进行研究,我正在研究牛津17花alexnet示例。该示例使用基于tensorflow的API tflearn。培训在我的GPU上运行良好,一段时间后达到了约97%的准确度。

For research at university I am examining the oxford 17 flowers alexnet example. The example uses the API tflearn based on tensorflow. Training is working very well on my GPU, reaching an accuracy of ~ 97% after a while.

不幸的是,在tflearn中评估单个图像仍无法正常工作,我本来可以使用model.predict(...)来预测每批我的所有数据,并遍历我的所有测试集并自己计算准确性。

Unfortunately evaluating single images isn't working yet in tflearn, I would have to use model.predict(...) to predict all my data per batch, and loop over all my test set and calculate accuracy by myself.

到目前为止我的培训代码:

My training code so far:

...
import image_loader
X, Y = image_loader.load_data(one_hot=True, shuffle=False)

X = X.reshape(244,244)

# Build network
network = input_data(shape=[None, 224, 224, 3])

network = conv_2d(network, 96, 11, strides=4, activation='relu')
network = max_pool_2d(network, 3, strides=2)
network = local_response_normalization(network)

network = conv_2d(network, 256, 5, activation='relu')
network = max_pool_2d(network, 3, strides=2)
network = local_response_normalization(network)

network = conv_2d(network, 384, 3, activation='relu')
network = conv_2d(network, 384, 3, activation='relu')
network = conv_2d(network, 256, 3, activation='relu')
network = max_pool_2d(network, 3, strides=2)
network = local_response_normalization(network)

network = fully_connected(network, 4096, activation='tanh')
network = dropout(network, 0.5)

network = fully_connected(network, 4096, activation='tanh')
network = dropout(network, 0.5)

network = fully_connected(network, 17, activation='softmax')
network = regression(network, optimizer='momentum',
                 loss='categorical_crossentropy',
                 learning_rate=0.01)

# Training
model = tflearn.DNN(network, checkpoint_path='model_ba',
                max_checkpoints=1, tensorboard_verbose=0)
model.fit(X, Y, n_epoch=3, validation_set=0.1, shuffle=True,
      show_metric=True, batch_size=32, snapshot_step=400,
      snapshot_epoch=False, run_id='ba_soccer_network')

代码正在保存一个检查点 model_ba,并以.meta文件的形式保存网络。
是否有可能加载保存的检查点并使用张量流评估单个图像?

The code is saving a checkpoint "model_ba" and also the network in form of a .meta-file. Is there a possibility of loading that saved checkpoint and evaluate a single image with tensorflow?

在此先感谢,
Arno

Thanks in advance, Arno

推荐答案

保存:
model.save('name.tflearn')

for save: model.save('name.tflearn')

对于加载:
model.load('name.tflearn')

for load: model.load('name.tflearn')

对于循环测试,只需加载模型并遵循以下代码

and for testing in loop just load the model and follow following code

files_path = '/your/test/images/directory/path'
img_files_path = os.path.join(files_path, '*.jpg')
img_files = sorted(glob(img_files_path))

for f in img_files:
    try:
        img = Image.open(f).convert('RGB')
        img = ImageOps.fit(img, ((64, 64)), Image.ANTIALIAS)

        img_arr = np.array(img)
        img_arr = img_arr.reshape(-1, 64, 64, 3).astype("float")

        pred = model.predict(img_arr)
        print(" %s" % pred[0])

    except:
        continue

这篇关于加载检查点并使用Tensorflow DNN评估单个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆