当前位置:网站首页>Implementation of image recognition code based on VGg convolutional neural network

Implementation of image recognition code based on VGg convolutional neural network

2022-04-23 17:53:00 Stephen_ Tao

VGG Model is introduced

VGG(Oxford Visual Geometry Group) The model is 2014 year ILSVRC Second place in the competition ,, from Karen Simonyan and Andrew Zisserman Realization .VGG It's a convolutional neural network model , Is in AlexNet Improvements based on .
 Insert picture description here
TensorFLow Of keras Integrated in the library VGG16、VGG19 Model , You can print the structure of the model , Let's say VGG16 Take as an example to illustrate the model structure :

from tensorflow.python.keras.applications.vgg16 import VGG16

model = VGG16()
print(model.summary())

VGG Model print results

Model: "vgg16"
_________________________________________________________________
Layer (type)                 Output Shape              Param # 
=================================================================
input_1 (InputLayer)         [(None, 224, 224, 3)]     0         
_________________________________________________________________
block1_conv1 (Conv2D)        (None, 224, 224, 64)      1792      
_________________________________________________________________
block1_conv2 (Conv2D)        (None, 224, 224, 64)      36928     
_________________________________________________________________
block1_pool (MaxPooling2D)   (None, 112, 112, 64)      0         
_________________________________________________________________
block2_conv1 (Conv2D)        (None, 112, 112, 128)     73856     
_________________________________________________________________
block2_conv2 (Conv2D)        (None, 112, 112, 128)     147584    
_________________________________________________________________
block2_pool (MaxPooling2D)   (None, 56, 56, 128)       0         
_________________________________________________________________
block3_conv1 (Conv2D)        (None, 56, 56, 256)       295168    
_________________________________________________________________
block3_conv2 (Conv2D)        (None, 56, 56, 256)       590080    
_________________________________________________________________
block3_conv3 (Conv2D)        (None, 56, 56, 256)       590080    
_________________________________________________________________
block3_pool (MaxPooling2D)   (None, 28, 28, 256)       0         
_________________________________________________________________
block4_conv1 (Conv2D)        (None, 28, 28, 512)       1180160   
_________________________________________________________________
block4_conv2 (Conv2D)        (None, 28, 28, 512)       2359808   
_________________________________________________________________
block4_conv3 (Conv2D)        (None, 28, 28, 512)       2359808   
_________________________________________________________________
block4_pool (MaxPooling2D)   (None, 14, 14, 512)       0         
_________________________________________________________________
block5_conv1 (Conv2D)        (None, 14, 14, 512)       2359808   
_________________________________________________________________
block5_conv2 (Conv2D)        (None, 14, 14, 512)       2359808   
_________________________________________________________________
block5_conv3 (Conv2D)        (None, 14, 14, 512)       2359808   
_________________________________________________________________
block5_pool (MaxPooling2D)   (None, 7, 7, 512)         0         
_________________________________________________________________
flatten (Flatten)            (None, 25088)             0         
_________________________________________________________________
fc1 (Dense)                  (None, 4096)              102764544 
_________________________________________________________________
fc2 (Dense)                  (None, 4096)              16781312  
_________________________________________________________________
predictions (Dense)          (None, 1000)              4097000   
=================================================================
Total params: 138,357,544
Trainable params: 138,357,544
Non-trainable params: 0

be based on VGG Image recognition

This article found two pictures of animals from the Internet , One is husky , Always a Jaguar , adopt VGG Model the image
Class identification .
Siberian Husky :
 Insert picture description here
jaguar :
 Insert picture description here

Code implementation

because VGG The model is integrated , So just call the corresponding API Can realize the task of image recognition .

Husky identifies

from tensorflow.python.keras.applications.vgg16 import VGG16,decode_predictions,preprocess_input
from tensorflow.python.keras.preprocessing.image import load_img,img_to_array

model = VGG16()
#  Change the input picture to (224,224) Fixed size of 
image = load_img("./dog.jpeg",target_size=(224,224))

#  Convert the picture to 3 Dimension group 
image = img_to_array(image)

#  The picture becomes 4 dimension , Satisfy VGG Input requirements of the model 
image = image.reshape(1,image.shape[0],image.shape[1],image.shape[2])

#  Preprocess the input picture 
image = preprocess_input(image)

#  Predict the categories of pictures 
y_predict = model.predict(image)

#  Decode the prediction results 
label = decode_predictions(y_predict)
res = label[0][0]
print(" The forecast category is :%s, The probability of :(%.2f%%)",(res[1],res[2]*100))

Running results

 The forecast category is :Siberian_husky, The probability of :(51.18%)

Jaguar recognition

from tensorflow.python.keras.applications.vgg16 import VGG16,decode_predictions,preprocess_input
from tensorflow.python.keras.preprocessing.image import load_img,img_to_array

model = VGG16()
#  Change the input picture to (224,224) Fixed size of 
image = load_img("./leopard.jpg",target_size=(224,224))

#  Convert the picture to 3 Dimension group 
image = img_to_array(image)

#  The picture becomes 4 dimension , Satisfy VGG Input requirements of the model 
image = image.reshape(1,image.shape[0],image.shape[1],image.shape[2])

#  Preprocess the input picture 
image = preprocess_input(image)

#  Predict the categories of pictures 
y_predict = model.predict(image)

#  Decode the prediction results 
label = decode_predictions(y_predict)
res = label[0][0]
print(" The forecast category is :%s, The probability of :(%.2f%%)",(res[1],res[2]*100))

Running results

 The forecast category is :leopard, The probability of :(62.83%)

summary

This paper introduces VGG Model , And based on TensorFlow.Keras Integrated in API Set the VGG Model . The accuracy of the model in the image recognition task is verified by two animal images .

版权声明
本文为[Stephen_ Tao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230548468399.html