感覺AI可能因?yàn)槭莻€(gè)新興的學(xué)科忽妒,所以特別迷;
keras, tensorflow, slim等各大模塊亂七八糟姜挺,博客內(nèi)容五花八門......
1)利用tensorflow的slim模塊復(fù)現(xiàn)vggnet實(shí)現(xiàn)圖像分類
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復(fù)現(xiàn)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強(qiáng)行入門好難呀齿税,繼續(xù)鉆研ing