欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

图像识别笔记

程序员文章站 2024-03-15 23:34:24
...

图像识别笔记

CNN

图像识别笔记


VGG16

图像识别笔记


ResNet

  • 包含两篇论文Deep Residual learning for image recognitiontraining very deep network

图像识别笔记
图像识别笔记

Vgg16代码

  • 数据是keras自带的CIFAR100小图像分类集,参考这里
  • 相关代码参考Vgg16Resnet
  • vgg16每次卷积前要padding,filter是3*3,所以ZeroPadding2D(padding=(1,1)),这个操作和在Conv2D中设置padding='same’一样
  • 每次卷积后要在channel的维度做 BatchNormalization
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 19 15:18:58 2020

@author: 86188
"""
'''
x_train:(50000, 32, 32, 3)

y_train.shape:(50000, 1)
'''
import tensorflow as tf
from keras.datasets import cifar10
from keras.models import Model
from keras.layers import Masking, ZeroPadding2D, Bidirectional,Dense, Input, Conv2D, Activation,Dropout,MaxPooling2D,BatchNormalization,Flatten
#from keras.preprocessing import sequence
from keras import metrics
from keras import backend as K
from keras.callbacks import Callback
#import matplotlib.pyplot as plt
from keras.callbacks import ReduceLROnPlateau
from keras.callbacks import EarlyStopping
from keras.utils.np_utils import to_categorical


‘’‘
数据是keras自带的
‘’‘
(x_train, y_train), (x_test, y_test) = cifar10.load_data() #

y_train = to_categorical(y_train, num_classes=10)
y_test = to_categorical(y_test, num_classes=10)

'''
Conv2D(nb_filter, kernel_size, padding=padding, strides=strides, activation='relu', name=conv_name,padding='same')(x)
data_forma="channels_last"(默认)
padding='same':默认pad到不改变图大小,padding='valid'没有padding
channels_last 对应输入尺寸为 (batch, height, width, channels),
channels_first 对应输入尺寸为 (batch, channels, height, width)
'''
def Conv2d_BN(x, nb_filter, kernel_size, strides=(1, 1), padding='same', name=None):
#    if name is not None:
#        bn_name = name + '_bn'
#        conv_name = name + '_conv'
#    else:
#        bn_name = None
#        conv_name = None
 
    x = Conv2D(nb_filter, kernel_size, padding=padding, strides=strides, activation='relu')(x)
    x = BatchNormalization(axis=3)(x)
    return x


def VGG_16(weights_path=None):


    pic_input = Input(shape=(32,32,3))
    x = ZeroPadding2D((1, 1))(pic_input)
    
    x=Conv2d_BN(x, nb_filter=64, kernel_size=(3,3), padding='valid')
    x=Conv2d_BN(x, nb_filter=64, kernel_size=(3,3), padding='same')
    x=MaxPooling2D((2,2),strides=(2,2))(x)


    x=Conv2d_BN(x, nb_filter=128, kernel_size=(3,3), padding='same')
    x=Conv2d_BN(x, nb_filter=128, kernel_size=(3,3), padding='same')
    x=MaxPooling2D((2,2),strides=(2,2))(x)
    
    x=Conv2d_BN(x, nb_filter=256, kernel_size=(3,3), padding='same')
    x=Conv2d_BN(x, nb_filter=256, kernel_size=(3,3), padding='same')
    x=Conv2d_BN(x, nb_filter=256, kernel_size=(3,3), padding='same')
    x=MaxPooling2D((2,2),strides=(2,2))(x)
 
    x=Conv2d_BN(x, nb_filter=512, kernel_size=(3,3), padding='same')
    x=Conv2d_BN(x, nb_filter=512, kernel_size=(3,3), padding='same')
    x=Conv2d_BN(x, nb_filter=512, kernel_size=(3,3), padding='same')
    x=MaxPooling2D((2,2),strides=(2,2))(x)
 
    x=Conv2d_BN(x, nb_filter=512, kernel_size=(3,3), padding='same')
    x=Conv2d_BN(x, nb_filter=512, kernel_size=(3,3), padding='same')
    x=Conv2d_BN(x, nb_filter=512, kernel_size=(3,3), padding='same')
    x=MaxPooling2D((2,2),strides=(2,2))(x)
    
    x=Flatten()(x)
    x=Dense(4096, activation='relu')(x)
    x=Dropout(0.5)(x)
    x=Dense(4096, activation='relu')(x)
    x=Dropout(0.5)(x) 
    x=Dense(10, activation='softmax')(x)

    model = Model(inputs=pic_input, outputs=x)
    model.summary()
 
    if weights_path:
        model.load_weights(weights_path)
 
    return model

reduce_lr = ReduceLROnPlateau(monitor='val_loss', patience=3,factor=0.1,mode='min')    
early_stopping = EarlyStopping(monitor='val_loss', patience=5, verbose=2, mode='min')
model=VGG_16()
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
hist1=model.fit(x_train, y_train, validation_split=0.1,epochs=10, batch_size=32,callbacks=[reduce_lr,early_stopping])



score=model.evaluate(x_test,y_test,batch_size=32,verbose=2)
model.save("vgg_try.h5")

#def VGG_16(weights_path=None):
#    model = Sequential()
#    model.add(ZeroPadding2D((1,1),input_shape=(32,32,3)))
#    model.add(Convolution2D(64, 3, 3, activation='relu'))
#    model.add(ZeroPadding2D((1,1)))
#    model.add(Convolution2D(64, 3, 3, activation='relu'))
#    model.add(MaxPooling2D((2,2), strides=(2,2)))
# 
#    model.add(ZeroPadding2D((1,1)))
#    model.add(Convolution2D(128, 3, 3, activation='relu'))
#    model.add(ZeroPadding2D((1,1)))
#    model.add(Convolution2D(128, 3, 3, activation='relu'))
#    model.add(MaxPooling2D((2,2), strides=(2,2)))
# 
#    model.add(ZeroPadding2D((1,1)))
#    model.add(Convolution2D(256, 3, 3, activation='relu'))
#    model.add(ZeroPadding2D((1,1)))
#    model.add(Convolution2D(256, 3, 3, activation='relu'))
#    model.add(ZeroPadding2D((1,1)))
#    model.add(Convolution2D(256, 3, 3, activation='relu'))
#    model.add(MaxPooling2D((2,2), strides=(2,2)))
# 
#    model.add(ZeroPadding2D((1,1)))
#    model.add(Convolution2D(512, 3, 3, activation='relu'))
#    model.add(ZeroPadding2D((1,1)))
#    model.add(Convolution2D(512, 3, 3, activation='relu'))
#    model.add(ZeroPadding2D((1,1)))
#    model.add(Convolution2D(512, 3, 3, activation='relu'))
#    model.add(MaxPooling2D((2,2), strides=(2,2)))
# 
#    model.add(ZeroPadding2D((1,1)))
#    model.add(Convolution2D(512, 3, 3, activation='relu'))
#    model.add(ZeroPadding2D((1,1)))
#    model.add(Convolution2D(512, 3, 3, activation='relu'))
#    model.add(ZeroPadding2D((1,1)))
#    model.add(Convolution2D(512, 3, 3, activation='relu'))
#    model.add(MaxPooling2D((2,2), strides=(2,2)))
# 
#    model.add(Flatten())
#    model.add(Dense(4096, activation='relu'))
#    model.add(Dropout(0.5))
#    model.add(Dense(4096, activation='relu'))
#    model.add(Dropout(0.5))
#    model.add(Dense(1000, activation='softmax'))
# 
#    if weights_path:
#        model.load_weights(weights_path)
# 
#    return model