卷积神经网络

视频:

https://www.youtube.com/watch?v=hMIZ85t9r9A

https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg

文章(没看):

https://blog.csdn.net/qq_28168421/article/details/81611874

https://blog.csdn.net/weixin_42451919/article/details/81381294

用途:图片,语言等

  • 输入是图片,输出是区分图片的内容,例如鼻子和眼睛
  • 经层由很多神经元组成
  • 对图像的处理不是单个像素,而是一块块区域,有利于理解图像。(这个过程就叫卷积)
  • 图像是有高度的,黑白的是1,彩色的是3
  • 批量过滤器(例子是5*5*3)持续不断的在推图片上滚动,收集图片上的信息。产生边缘信息等
  • 卷积过程:扫描根据边缘信息可得到更高级的信息,例如区分图像内容,例如鼻子和眼睛
  • Pooling:池化就是对特征图进行特征压缩。(相反的说法:卷积过程长宽越来越小,为了防止丢失信息,采用pooling??) 
  • 经典的过程是:输入层(image)-> 卷积层(convolution)->池化层(Max Pooling)->卷积层(convolution)->池化层(Max Pooling)->全连接层->全连接层->分类

下面两段程序都是从minist取数据,然后用cnn来辨别0-9数字


from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.utils import np_utils
from keras.optimizers import RMSprop
from keras import backend as K

#image->convolution->pooling->convolution->pooling->full connected->full connected(hidden layer) ->classifier

def createData():
    # download the mnist to the path '~/.keras/datasets/' if it is the first time to be called
    # X_train is image information. X_train.shape = (60000, 28, 28). 
    # y_train is value array. Its count is 60000 and value scope is from 1 to 10.
    (X_train, y_train), (X_test, y_test) = mnist.load_data()

    #教程中没有提,但必须修改,需要判断channel的位置
    if K.image_data_format() == 'channels_first':
        X_train = X_train.reshape(-1, 1, 28, 28) / 255
        X_test = X_test.reshape(-1, 1, 28, 28) / 255
    else:
        X_train = X_train.reshape(-1, 28, 28, 1)
        X_test = X_test.reshape(-1, 28, 28, 1)       

    # y_train.shape = (60000, ) => (60000, 10); '10' means use 10 bits on behalf of a digit
    y_train = np_utils.to_categorical(y_train, num_classes=10) #10 bits里有一个值为1,其他为0,用这种形式代表一个分类
    y_test = np_utils.to_categorical(y_test, num_classes=10)
    return (X_train, y_train), (X_test, y_test)


# Step1. Create Data
(X_train, y_train), (X_test, y_test) = createData()

# Step2. Create Module
model = Sequential()  ## 定义cnn模型
model.add(Conv2D(32, (3,3), input_shape = (28, 28, 1), activation='relu')) ## 添加2D卷积层,32个过滤器,用3乘3过滤器,使用relu激活函数,指定输入图片尺寸
## ken 这里应该添加更多的层
model.add(MaxPooling2D(2,2)) ## 添加最大池化层
#model.add(Conv2D(64, (3,3), activation='relu',input_shape=(100,200,1))) ## 添加2D卷积层,32个过滤器,用3乘3过滤器,使用relu激活函数,指定输入图片尺寸
model.add(Conv2D(32, (3,3), activation='relu')) ## 添加2D卷积层,32个过滤器,用3乘3过滤器,使用relu激活函数,指定输入图片尺寸
model.add(MaxPooling2D(2,2)) ## 添加最大池化层
# drop out of max-pooling layer , drop out rate is 0.25 (0.5??)  防止过拟合
model.add(Dropout(0.25))
# flatten inputs from 2d to 1d
model.add(Flatten())
model.add(Dense(128, activation='relu')) #???
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))  ## 添加全连结层,使用softmax激活函数,输出5种分类的可能性

# Step3 激活训练模型compile,optimizing
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) ## 编译模型,rmsprop优化函数,分类交叉熵损失函数
    
# Step4 training
model.fit(X_train, y_train, batch_size=30, epochs=2)

# Step5 test
print('Testing -----------')
loss, accuracy = model.evaluate(X_test, y_test)
print('loss = ', loss, ' accuracy = ', accuracy)



'''Trains a simple convnet on the MNIST dataset.
Gets to 99.25% test accuracy after 12 epochs
(there is still a lot of margin for parameter tuning).
16 seconds per epoch on a GRID K520 GPU.
'''

from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K

batch_size = 30 #128
num_classes = 10
epochs = 12 #2

# input image dimensions
img_rows, img_cols = 28, 28

# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()

if K.image_data_format() == 'channels_first':
    x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
    x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
    input_shape = (1, img_rows, img_cols)
else:
    x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
    x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
    input_shape = (img_rows, img_cols, 1)

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
                 activation='relu',
                 input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])

model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

用cnn来判断猫和狗,这里只有两种分类,代码修改自 

https://www.kaggle.com/c/grasp-and-lift-eeg-detection/discussion/16479#latest-165271

这个网站有很多例子,输入“dog cat”

# Convolutional Neural Network

# Installing Theano
# pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git

# Installing Tensorflow
# pip install tensorflow

# Installing Keras
# pip install --upgrade keras

# const
imagePath = 'C:/Users/CCW/Desktop/dog and cat/'
trainPath = imagePath + 'training_set/'
testPath = imagePath + 'test_set/'


# Part 1 - Building the CNN

# Importing the Keras libraries and packages
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense

# Initialising the CNN
classifier = Sequential()

# Step 1 - Convolution
classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))

# Step 2 - Pooling
classifier.add(MaxPooling2D(pool_size = (2, 2)))

# Adding a second convolutional layer
classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))

# Step 3 - Flattening
classifier.add(Flatten())

# Step 4 - Full connection
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 1, activation = 'sigmoid'))

# Compiling the CNN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

# Part 2 - Fitting the CNN to the images

from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)

test_datagen = ImageDataGenerator(rescale = 1./255)

training_set = train_datagen.flow_from_directory(trainPath,
                                                 target_size = (64, 64),
                                                 batch_size = 32,
                                                 class_mode = 'binary')

test_set = test_datagen.flow_from_directory(testPath,
                                            target_size = (64, 64),
                                            batch_size = 32,
                                            class_mode = 'binary')

classifier.fit_generator(training_set,
                         steps_per_epoch = 1000, #8000
                         epochs = 4, #25
                         validation_data = test_set,
                         validation_steps = 250) #2000

classifier.save('catDog_model.h1')

##load model
#from keras.models import load_model
#classifier = load_model('catDog_model.h1')  ## 读取保存的模型

# Part 3 - Making new predictions
import numpy as np
from keras.preprocessing import image

images = ['dogs/dog.4007.jpg', '/cats/cat.4040.jpg', '/cats/cat.4016.jpg', 'dogs/dog.4040.jpg']
for img in images:
    test_image = image.load_img(testPath + img, target_size = (64, 64))
    test_image = image.img_to_array(test_image)
    test_image = np.expand_dims(test_image, axis = 0)
    result = classifier.predict(test_image)
    #training_set.class_indices # no use??
    if result[0][0] == 1:
        prediction = 'dog'
    else:
        prediction = 'cat'

最后是小车行驶程序,功能根据图像中的白条,从5个方向选一个。

from keras.models import Sequential
from keras import layers
from keras.layers import Dense, Dropout, Conv2D, MaxPooling2D, Flatten
import numpy
from keras import optimizers
from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt


#image->convolution->pooling->convolution->pooling->full connected->full connected(hidden layer) ->classifier

def defineModelAndGenerateModel():
    model = Sequential()  ## 定义cnn模型
    model.add(Conv2D(32, (3,3), activation='relu',input_shape=(100,200,1))) ## 添加2D卷积层,32个过滤器,用3乘3过滤器,使用relu激活函数,指定输入图片尺寸
    ## ken 这里应该添加更多的层
    model.add(MaxPooling2D(2,2)) ## 添加最大池化层
    model.add(Conv2D(32, (3,3), activation='relu')) ## 添加2D卷积层,32个过滤器,用3乘3过滤器,使用relu激活函数,指定输入图片尺寸
    model.add(MaxPooling2D(2,2)) ## 添加最大池化层

    # drop out of max-pooling layer , drop out rate is 0.25 (0.5??) 
    model.add(Dropout(0.25))
    # flatten inputs from 2d to 1d
    model.add(Flatten())
    model.add(Dense(6, activation='softmax'))  ## 添加全连结层,使用softmax激活函数,输出6种分类的可能性

    model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) ## 编译模型,rmsprop优化函数,分类交叉熵损失函数
    train_datagen = ImageDataGenerator(rescale=1./255) ## 从目录里提取图片

    train_generator = train_datagen.flow_from_directory(
        'c:/1', target_size=(100,200),
        batch_size=10,
        color_mode='grayscale',
        class_mode='categorical'
    )


    #todo:训练之前应该将一部分训练中的数据挑选为validation用,用keras的split方法。
    validation_generator= train_datagen.flow_from_directory(
        'c:/2', target_size=(100,200),
        batch_size=10,
        color_mode='grayscale',
        class_mode='categorical'
    )

    #调整参数
    history = model.fit_generator(  #开始训练模型
        train_generator,
        steps_per_epoch=100,
        epochs=10,
        validation_data= validation_generator,
        validation_steps=5)
    model.save('car_model_1.h6.1')  ## 保存训练的模型(*以后做预测会用到)
    return history

def showresult(history):  ## 生成模型衡量的结果
    plt.figure()
    acc = history.history['acc']
    val_acc = history.history['val_acc']
    loss = history.history['loss']
    val_loss = history.history['val_loss']
    epochs = range(1,len(acc)+1)
    plt.plot(epochs, acc, 'bo', label='Training acc')
    plt.plot(epochs, val_acc, 'b', label='Validation acc')
    plt.title('Training and validation accuracy')
    #plt.legend()
    plt.show()

if __name__ == "__main__":
    history= defineModelAndGenerateModel()
    showresult(history)
    

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值