TFLearn获得预测的百分比

时间:2017-10-03 12:12:59

标签: python tensorflow tflearn

这个代码可以预测来自自定义训练的cnn(TFLearn)的图像数据。

# -*- coding: utf-8 -*-
from __future__ import division, print_function, absolute_import

import tflearn
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.estimator import regression
from tflearn.data_preprocessing import ImagePreprocessing
from tflearn.data_augmentation import ImageAugmentation
import scipy
import numpy as np
import imageio

# Same network definition as before
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()
img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_rotation(max_angle=25.)
img_aug.add_random_blur(sigma_max=3.)

network = input_data(shape=[None, 256, 256, 3],
                     data_preprocessing=img_prep,
                     data_augmentation=img_aug)
network = conv_2d(network, 32, 3, activation='relu')
network = max_pool_2d(network, 2)
network = conv_2d(network, 64, 3, activation='relu')
network = conv_2d(network, 64, 3, activation='relu')
network = max_pool_2d(network, 2)
network = fully_connected(network, 512, activation='relu')
network = dropout(network, 0.5)
network = fully_connected(network, 2, activation='softmax')
network = regression(network, optimizer='adam',
                     loss='categorical_crossentropy',
                     learning_rate=0.001)

model = tflearn.DNN(network, tensorboard_verbose=0, checkpoint_path='solar_panels_classifier.tfl.ckpt')
model.load("solar_panels_train.tfl.ckpt-500")

# Load the image file
img = imageio.imread("data/predict/1202211031011131131.jpg")
img.dtype

img = img.astype(float)

# Predict
prediction = model.predict([img])

print(prediction)

# gives the output [[ 0.  1.]] 

如何获得每个班级/标签的预测百分比?

我找到了model.evaluate(img,?LABELS)的文档,但我没有让这个标签起作用: - (

我希望有人可以帮助我。

0 个答案:

没有答案