Load checkpoint and evaluate single image with tensorflow DNN

ぐ巨炮叔叔 提交于 2019-12-10 20:01:22

问题


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.

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')

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?

Thanks in advance, Arno


回答1:


for save: model.save('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


来源:https://stackoverflow.com/questions/37058236/load-checkpoint-and-evaluate-single-image-with-tensorflow-dnn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!