4

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

ArnoXf
  • 135
  • 10
  • Can you check if `network`/`model` have a method `save` or `write`? (Inspired from [here](http://stackoverflow.com/a/34547718/2436175)) – Antonio May 11 '16 at 10:31
  • Yes, there is indeed a `model.save()` that saves a ckpt and a meta file (Even there is also a method `model.load()` in this API, I need to load the saved ckpt and meta into a tensorflow code without using the tflearn API) – ArnoXf May 11 '16 at 10:52

1 Answers1

0

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