利用深度神经网络进行基本图像分类

网友投稿 232 2022-10-23


利用深度神经网络进行基本图像分类

利用深度神经网络进行基本图像分类

此代码在colab已经经过测试并且可以查看TensorBoard:地址

环境:Tensorflow-2.1.0、Python3.6、google colab

In[1]:

import ssl ssl._create_default_https_context = ssl._create_unverified_context# 不知道为什么在本地的jupyter notebook上无法导入fashion_mnist数据集(本地需要,google colab不需要)

In[2]:

import matplotlib as mplimport matplotlib.pyplot as plt%matplotlib inlineimport numpy as npimport sklearn  import pandas as pdimport osimport sysimport timeimport tensorflow as tffrom tensorflow import keras as kerasfrom sklearn.preprocessing import StandardScalerfor module in mpl,np,pd,sklearn,tf,keras:print(module.__name__,module.__version__)# 打印版本库

Out[1]:

matplotlib 3.1.2 numpy 1.18.1 pandas 0.25.3 sklearn 0.22.1 tensorflow 2.1.0 tensorflow_core.keras 2.2.4-tf

In[3]:

fashion_mnist=keras.datasets.fashion_mnist(x_train_all, y_train_all),(x_test, y_test)=fashion_mnist.load_data()# 导入fashion_mnist数据集

In[4]

x_valid,x_train=x_train_all[:5000],x_train_all[5000:]y_valid,y_train=y_train_all[:5000],y_train_all[5000:]# fashion_mnist有6w个数据,前5k个为验证集,其中x为图片,y为序列号print(x_valid.shape,y_valid.shape)print(x_train.shape,y_valid.shape)print(x_test.shape,y_test.shape)# 打印数据集的格式

Out[2]

(5000, 28, 28) (5000,) (55000, 28, 28) (5000,) (10000, 28, 28) (10000,)

In[5]:

print(np.max(x_train),np.min(x_train))# 打印数据集中的最大值和最小值

Out[3]:

255 0

In[6]:

# x= (x-u)/stdscaler=StandardScaler()# x_train:[None, 28, 28] -> [None, 784]x_train_scaled=scaler.fit_transform(x_train.astype(np.float32).reshape(-1,1)).reshape(-1,28,28) #会自动记录方差u和均值std,并进行归一化x_valid_scaled=scaler.transform(x_valid.astype(np.float32).reshape(-1,1)).reshape(-1,28,28) # 会根据自动记录的方差u和均值std进行归一化x_test_scaled=scaler.transform(x_test.astype(np.float32).reshape(-1,1)).reshape(-1,28,28) # 会根据自动记录的方差u和均值std进行归一化

Out[4]:

2.0231433 -0.8105136

In[7]:

def show_single_image(img_arr):plt.imshow(img_arr,cmap='binary')plt.show()# 打印单张图片show_single_image(x_train[0])

Out[5]:

In[8]:

def show_images(n_rows,n_cols,x_data,y_data,class_names):assert len(x_data)==len(y_data)assert n_cols*n_rows

Out[6]:

In[9]:

# 构建一个三层的神经网络# model=keras.models.Sequential()# model.add(keras.layers.Flatten(input_shape=[28,28])) # 展开成28*28的向量# model.add(keras.layers.Dense(300,activation='relu')) # 全链接层,层数300,激活函数relu# model.add(keras.layers.Dense(100,activation='relu')) # model.add(keras.layers.Dense(10,activation='softmax')) # 有10个输出# model=keras.models.Sequential([#     keras.layers.Flatten(input_shape=[28,28]),#     keras.layers.Dense(300,activation='relu'),#     keras.layers.Dense(100,activation='relu'),#     keras.layers.Dense(10,activation='softmax')# ])model=keras.models.Sequential()model.add(keras.layers.Flatten(input_shape=[28,28]))for _ in range(20):model.add(keras.layers.Dense(100,activation='selu'))# model.add(keras.layers.Dense(100,activation='relu'))# model.add(keras.layers.BatchNormalization())# 激活函数在前的后的批归一化# model.add(keras.layers.Dense(100))# model.add(keras.layers.BatchNormalization()) # model.add(keras.layers.Activation('relu'))# model.add(keras.layers.Dropout(rate=0.5))model.add(keras.layers.AlphaDropout(rate=0.5))# AlphaDropout与Dropout的区别:1、均值方差不变 2、归一化性质不变model.add(keras.layers.Dense(10,activation='softmax'))# relu: y=max(0,x)# softmax: 将向量变成概率分布 x=[x1,x2,x3]#          y=[e^x1/sum,e^x2/sum,e^x3/sum],sum=e^x1+e^x2+e^x3# reason for sparse: y->index y->one_hot->[]model.compile(loss='sparse_categorical_crossentropy',  optimizer='sgd',  metrics=['accuracy'])

tensorflow.keras.model.compile

In[10]:

model.layers

Out[7]:

[,  ,  ,  ,  ,  ,  ,  ,  ,  ,  ,  ,  ,  ,  ,  ,  ,  ,  ,  ,  ,  ,  ]

In[11]:

model.summary() #打印模型的结构

Out[8]:

Model: "sequential" _________________________________________________________________ Layer (type)                 Output Shape              Param #    ================================================================= flatten (Flatten)            (None, 784)               0          _________________________________________________________________ dense (Dense)                (None, 100)               78500      _________________________________________________________________ dense_1 (Dense)              (None, 100)               10100      _________________________________________________________________ dense_2 (Dense)              (None, 100)               10100      _________________________________________________________________ dense_3 (Dense)              (None, 100)               10100      _________________________________________________________________ dense_4 (Dense)              (None, 100)               10100      _________________________________________________________________ dense_5 (Dense)              (None, 100)               10100      _________________________________________________________________ dense_6 (Dense)              (None, 100)               10100      _________________________________________________________________ dense_7 (Dense)              (None, 100)               10100      _________________________________________________________________ dense_8 (Dense)              (None, 100)               10100      _________________________________________________________________ dense_9 (Dense)              (None, 100)               10100      _________________________________________________________________ dense_10 (Dense)             (None, 100)               10100      _________________________________________________________________ dense_11 (Dense)             (None, 100)               10100      _________________________________________________________________ dense_12 (Dense)             (None, 100)               10100      _________________________________________________________________ dense_13 (Dense)             (None, 100)               10100      _________________________________________________________________ dense_14 (Dense)             (None, 100)               10100      _________________________________________________________________ dense_15 (Dense)             (None, 100)               10100      _________________________________________________________________ dense_16 (Dense)             (None, 100)               10100      _________________________________________________________________ dense_17 (Dense)             (None, 100)               10100      _________________________________________________________________ dense_18 (Dense)             (None, 100)               10100      _________________________________________________________________ dense_19 (Dense)             (None, 100)               10100      _________________________________________________________________ alpha_dropout (AlphaDropout) (None, 100)               0          _________________________________________________________________ dense_20 (Dense)             (None, 10)                1010       ================================================================= Total params: 271,410 Trainable params: 271,410 Non-trainable params: 0 _________________________________________________________________

In[12]:

# 第一层 [None, 784] * w + b -> [None, 300] w.shape=[784,300], b=[300]

In[13]:

# TensorBoard,EarlyStopping,ModelCheckPointlogdir='./dnn-selu-dropout-callbacks' # 设置一个文件夹路径if not os.path.exists(logdir): # 若文件夹不存在,则创建文件夹os.mkdir(logdir)output_model_file=os.path.join(logdir,   'fashion_mnist_model.h5') #放入文件夹内,文件名fashion_mnist_model.h5callbacks=[keras.callbacks.TensorBoard(logdir),keras.callbacks.ModelCheckpoint(output_model_file,save_best_only=True), # 保存最好的模型,如果不是这个会默认保存最近的一个模型keras.callbacks.EarlyStopping(patience=5,min_delta=1e-3)]history=model.fit(x_train_scaled,y_train,epochs=10,  validation_data=(x_valid_scaled,y_valid),  callbacks=callbacks)

tensorflow.keras.model.fit

Out[9]:

Train on 55000 samples, validate on 5000 samples Epoch 1/10 55000/55000 [==============================] - 11s 195us/sample - loss: 0.6998 - accuracy: 0.7617 - val_loss: 0.5656 - val_accuracy: 0.8536 Epoch 2/10 55000/55000 [==============================] - 8s 144us/sample - loss: 0.4626 - accuracy: 0.8428 - val_loss: 0.6195 - val_accuracy: 0.8558 Epoch 3/10 55000/55000 [==============================] - 9s 162us/sample - loss: 0.4065 - accuracy: 0.8581 - val_loss: 0.5300 - val_accuracy: 0.8668 Epoch 4/10 55000/55000 [==============================] - 8s 142us/sample - loss: 0.3755 - accuracy: 0.8717 - val_loss: 0.6154 - val_accuracy: 0.8664 Epoch 5/10 55000/55000 [==============================] - 8s 152us/sample - loss: 0.3521 - accuracy: 0.8774 - val_loss: 0.5287 - val_accuracy: 0.8766 Epoch 6/10 55000/55000 [==============================] - 8s 153us/sample - loss: 0.3343 - accuracy: 0.8815 - val_loss: 0.4956 - val_accuracy: 0.8856 Epoch 7/10 55000/55000 [==============================] - 8s 143us/sample - loss: 0.3193 - accuracy: 0.8857 - val_loss: 0.5349 - val_accuracy: 0.8716 Epoch 8/10 55000/55000 [==============================] - 8s 144us/sample - loss: 0.3085 - accuracy: 0.8916 - val_loss: 0.5294 - val_accuracy: 0.8842 Epoch 9/10 55000/55000 [==============================] - 8s 145us/sample - loss: 0.2955 - accuracy: 0.8946 - val_loss: 0.4668 - val_accuracy: 0.8822 Epoch 10/10 55000/55000 [==============================] - 8s 152us/sample - loss: 0.2839 - accuracy: 0.8975 - val_loss: 0.4907 - val_accuracy: 0.8860

In[14]:

type(history)

Out[10]:

tensorflow.python.keras.callbacks.History

In[15]:

history.history# 打印每次学习的结果

Out[11]:

{'loss': [0.6997782779043371,   0.4625766654621471,   0.4064591264659708,   0.37553091496554286,   0.35207060631621967,   0.3343261710730466,   0.3193407494176518,   0.30854321794509887,   0.29552189791636035,   0.28390346760099583],  'accuracy': [0.76165456,   0.84276366,   0.85809094,   0.8717273,   0.87743634,   0.8814909,   0.8856909,   0.89156365,   0.89461815,   0.8974909],  'val_loss': [0.5656432386219501,   0.6194551657661795,   0.5299783393919468,   0.6153697894215584,   0.5287225459891837,   0.49561948866695166,   0.5348633618405322,   0.5294362505242228,   0.4668049536377192,   0.4907300000913441],  'val_accuracy': [0.8536,   0.8558,   0.8668,   0.8664,   0.8766,   0.8856,   0.8716,   0.8842,   0.8822,   0.886]}

In[16]

def plot_learning_curves(history):pd.DataFrame(history.history).plot(figsize=(8,5))plt.grid(True)plt.gca().set_ylim(0,1)plt.show()# 打印学习曲线plot_learning_curves(history)

Out[12]:

In[17]:

model.evaluate(x_test_scaled,y_test) # 在测试集上验证

tensorflow.keras.model.evaluate

Out[13]:

10000/10000 [==============================] - 1s 95us/sample - loss: 0.5799 - accuracy: 0.8741

Out[14]:

[0.5799049034297467, 0.8741]

汇总:

#!/usr/bin/env python# coding: utf-8# In[1]:import ssl ssl._create_default_https_context = ssl._create_unverified_context# 不知道为什么在本地的jupyter notebook上无法导入fashion_mnist数据集(本地需要,google colab不需要)# In[23]:import matplotlib as mplimport matplotlib.pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline')import numpy as npimport sklearn  import pandas as pdimport osimport sysimport timeimport tensorflow as tffrom tensorflow import keras as kerasfrom sklearn.preprocessing import StandardScalerfor module in mpl,np,pd,sklearn,tf,keras:print(module.__name__,module.__version__)# 打印版本库# In[3]:fashion_mnist=keras.datasets.fashion_mnist(x_train_all, y_train_all),(x_test, y_test)=fashion_mnist.load_data()# 导入fashion_mnist数据集# In[4]:x_valid,x_train=x_train_all[:5000],x_train_all[5000:]y_valid,y_train=y_train_all[:5000],y_train_all[5000:]# fashion_mnist有6w个数据,前5k个为验证集,其中x为图片,y为序列号print(x_valid.shape,y_valid.shape)print(x_train.shape,y_valid.shape)print(x_test.shape,y_test.shape)# 打印数据集的格式# In[5]:print(np.max(x_train),np.min(x_train))# 打印数据集中的最大值和最小值# In[6]:# x= (x-u)/stdscaler=StandardScaler()# x_train:[None, 28, 28] -> [None, 784]x_train_scaled=scaler.fit_transform(x_train.astype(np.float32).reshape(-1,1)).reshape(-1,28,28) #会自动记录方差u和均值std,并进行归一化x_valid_scaled=scaler.transform(x_valid.astype(np.float32).reshape(-1,1)).reshape(-1,28,28) # 会根据自动记录的方差u和均值std进行归一化x_test_scaled=scaler.transform(x_test.astype(np.float32).reshape(-1,1)).reshape(-1,28,28) # 会根据自动记录的方差u和均值std进行归一化# In[7]:def show_single_image(img_arr):plt.imshow(img_arr,cmap='binary')plt.show()# 打印单张图片show_single_image(x_train[0])# In[8]:def show_images(n_rows,n_cols,x_data,y_data,class_names):assert len(x_data)==len(y_data)assert n_cols*n_rowsindex y->one_hot->[]model.compile(loss='sparse_categorical_crossentropy',  optimizer='sgd',  metrics=['accuracy'])# In[10]:model.layers# In[11]:model.summary() #打印模型的结构# In[12]:# 第一层 [None, 784] * w + b -> [None, 300] w.shape=[784,300], b=[300]# In[15]:# TensorBoard,EarlyStopping,ModelCheckPointlogdir='./dnn-selu-dropout-callbacks' # 设置一个文件夹路径if not os.path.exists(logdir): # 若文件夹不存在,则创建文件夹os.mkdir(logdir)output_model_file=os.path.join(logdir,   'fashion_mnist_model.h5') #放入文件夹内,文件名fashion_mnist_model.h5callbacks=[keras.callbacks.TensorBoard(logdir),keras.callbacks.ModelCheckpoint(output_model_file,save_best_only=True), # 保存最好的模型,如果不是这个会默认保存最近的一个模型keras.callbacks.EarlyStopping(patience=5,min_delta=1e-3)]history=model.fit(x_train_scaled,y_train,epochs=10,  validation_data=(x_valid_scaled,y_valid),  callbacks=callbacks)# In[16]:type(history)# In[17]:history.history# 打印每次学习的结果# In[18]:def plot_learning_curves(history):pd.DataFrame(history.history).plot(figsize=(8,5))plt.grid(True)plt.gca().set_ylim(0,1)plt.show()# 打印学习曲线plot_learning_curves(history)# In[19]:model.evaluate(x_test_scaled,y_test) # 在测试集上验证


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java基础之创建虚拟机对象的过程详细总结
下一篇:全球TOP 3丨山石网科连续两年荣获Gartner网络防火墙“客户之选”称号
相关文章

 发表评论

暂时没有评论,来抢沙发吧~