貓狗大戰(4)測試模型
來自專欄 《人工智慧》
參考博客:https://github.com/kevin28520/My-TensorFlow-tutorials/blob/master/01%20cats%20vs%20dogs/input_data.py
tensorflow 實戰 貓狗大戰(一)訓練自己的數據 - CSDN博客
import osimport numpy as npimport tensorflow as tfimport input_dataimport modelimport matplotlib.pyplot as pltfrom PIL import Imagefrom sspicon import MsV1_0Lm20ChallengeRequestdef get_one_image(train): n = len(train) ind = np.random.randint(0,n) img_dir = train[ind] image = Image.open(img_dir) plt.imshow(image) image = image.resize([208,208]) image = np.array(image) return imagedef evaluate_one_image(): train_dir = J:\移動硬碟\數據集\train\ train,train_label = input_data.get_files(train_dir) image_array = get_one_image(train) with tf.Graph.as_default(): BATCH_SIZE = 1 N_CLASSES = 2 image = tf.cast(image_array,tf.float32) image = tf.image.per_image_standardization(image) image = tf.reshape(image,[1,208,208,3]) logit = model.inference(image,BATCH_SIZE,N_CLASSES) logit = tf.nn.softmax(logit) x = tf.placeholder(tf.float32,shape=[208,208,3]) logs_train_dir = J:\移動硬碟\數據集\logs\ saver = tf.train.Saver() with tf.Session() as sess: print(Reading checkpoints...) ckpt = tf.train.get_checkpoint_state(logs_train_dir) if ckpt and ckpt.model_checkpoint_path: global_step = ckpt.model_checkpoint_path.split(/)[-1].split(-)[-1] saver.restore(sess,ckpt.model_checkpoint_path) print(Loading success,global_step is %s % global_step) else: print(No checkpoint file found) prediction = sess.run(logit,feed_dict={x:image_array}) max_index = np.argmax(prediction) if max_index==0: print(This is a cat with possibility %.6f %prediction[:,0]) else: print(This is a dog with possibility %.6f %prediction[:,1])
推薦閱讀:
※張兵:這個模型告訴你,傳統行業該如何轉型互聯網
※荒野獵手——TFC路上王布力伽
※來自10年前的感動——SS系列領袖級眩暈
※求推薦幾款百元以下高性價比的MG國產高達模型?
※我很喜歡做軍模但是因為預算問題不能購買正式一點的上色怎麼辦?