tesorflow基本图像分类

#!/usr/bin/env python
# coding: utf-8

# In[1]:


import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from 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=0
plt.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=12
plt.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=5
num_cols=3
num_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[ ]:





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Gowi_fly

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值