VGGNet分类······Keras重建模型
感觉AI可能因为是个新兴的学科,所以特别迷;
keras, tensorflow, slim等各大模块乱七八糟,博客内容五花八门......
1)利用tensorflow的slim模块复现vggnet实现图像分类
import tensorflow as tf
import tensorflow.contrib.slim as slim
import tensorflow.contrib.slim.nets as nets
import sys
import numpy as np
sys.path.append('D:/models-master/research/slim')
from preprocessing import vgg_preprocessing
from nets import vgg
tf.reset_default_graph()
image_size = vgg.vgg_16.default_image_size
image=tf.read_file('1.jpeg')
jpeg = tf.image.decode_jpeg(image, channels=3)
processed_image = vgg_preprocessing.preprocess_image(jpeg,
image_size,
image_size,
is_training=False)
processed_images = tf.expand_dims(processed_image, 0)
with slim.arg_scope(vgg.vgg_arg_scope()):
logits, _ = vgg.vgg_16(processed_images,
num_classes=1000,
is_training=False)
probabilities = tf.nn.softmax(logits)
np_image = tf.placeholder(tf.float32, [None,224,224,3])
init_fn = slim.assign_from_checkpoint_fn('./vgg_16.ckpt',slim.get_model_variables('vgg_16'))
with tf.Session(config=tf.ConfigProto(device_count={'gpu':0})) as sess:
init_fn(sess)
np_image, network_input, probabilities = sess.run([jpeg, processed_images,probabilities])
print(np.max(probabilities))
2)利用keras复现vggnet
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.vgg16 import preprocess_input
from keras.applications.vgg16 import decode_predictions
from keras.applications.vgg16 import VGG16
# load the model
model = VGG16()
# load an image from file
image = load_img('mug.jpg', target_size=(224, 224))
# convert the image pixels to a numpy array
image = img_to_array(image)
# reshape data for the model
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
# prepare the image for the VGG model
image = preprocess_input(image)
# predict the probability across all output classes
yhat = model.predict(image)
# convert the probabilities to class labels
label = decode_predictions(yhat)
# retrieve the most likely result, e.g. highest probability
label = label[0][0]
# print the classification
print('%s (%.2f%%)' % (label[1], label[2]*100))
感觉不懂tensorflow强行入门好难呀,继续钻研ing