ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

tesorflow基本图像分类

2021-04-17 14:56:45  阅读:173  来源: 互联网

标签:plt 分类 predictions tesorflow label labels 图像 test array


#!/usr/bin/env python# coding: utf-8# In[1]:import numpy as npimport tensorflow as tfimport matplotlib.pyplot as pltfrom tensorflow import keras as keras# In[2]:fashion_mnist=keras.datasets.fashion_mnist(train_images, train_labels),(test_images, test_labels)=fashion_mnist.load_data()print(train_images.shape,train_labels.shape)# In[3]:class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',   'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']# In[4]:def show_single_images(image,label,class_names):plt.figure()plt.imshow(image)plt.xlabel(class_names[label])plt.colorbar()plt.grid(False)plt.show()show_single_images(train_images[0],train_labels[0],class_names)# In[5]:model=keras.models.Sequential()model.add(keras.layers.Flatten(input_shape=[28,28]))model.add(keras.layers.Dense(128,activation='relu'))model.add(keras.layers.Dense(10))# In[6]:model.compile(optimizer='adam',  loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),  metrics=['accuracy'])# In[7]:model.fit(train_images,train_labels,epochs=10)# In[8]:test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)print('\n Test accuracy',test_acc)# In[9]:probability_model=keras.Sequential([model,keras.layers.Softmax()])# In[10]:predictions=probability_model.predict(test_images)# In[11]:print(predictions[0])# In[12]:print(np.argmax(predictions[0]))# In[13]:print(test_labels[0])# In[14]:def plot_image(i, predictions_array, true_label, img):predictions_array, true_label, img=predictions_array[i],true_label[i],img[i]plt.grid(False)plt.xticks([])plt.yticks([])plt.imshow(img,cmap=plt.cm.binary)
    predicted_label=np.argmax(predictions_array)if(true_label==predicted_label):color='blue'else:color='red'
    plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],100*np.max(predictions_array),class_names[true_label]),color=color)# In[15]:def plot_value_array(i, predictions_array, true_label):predictions_array, true_label= predictions_array[i], true_label[i]plt.grid(False)plt.xticks(range(10))plt.yticks([])thisplot = plt.bar(range(10), predictions_array, color="#777777")plt.ylim(0,1)predicted_label=np.argmax(predictions_array)
    thisplot[predicted_label].set_color('red')thisplot[true_label].set_color('blue')# In[16]:i=0plt.figure(figsize=(6,3))plt.subplot(1,2,1)plot_image(i,predictions, test_labels,test_images)plt.subplot(1,2,2)plot_value_array(i,predictions,test_labels)plt.show()# In[17]:i=12plt.figure(figsize=(6,3))plt.subplot(1,2,1)plot_image(i,predictions, test_labels,test_images)plt.subplot(1,2,2)plot_value_array(i,predictions,test_labels)plt.show()# In[18]:num_rows=5num_cols=3num_image=num_cols*num_rows
plt.figure(figsize=(2*2*num_cols,2*num_rows))for i in range(num_image):plt.subplot(num_rows,2*num_cols,2*i+1)plot_image(i,predictions,test_labels,test_images)plt.subplot(num_rows,2*num_cols,2*i+2)plot_value_array(i,predictions,test_labels)plt.tight_layout()plt.show()# In[ ]:

标签:plt,分类,predictions,tesorflow,label,labels,图像,test,array
来源: https://blog.51cto.com/u_14189203/2713501

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有