ftp给网站做备份,优秀室内设计案例,注册一个公司需要哪些手续,wordpress用七牛网络思想#xff1a; 1、一张原始图片被resize到(224,224,3)#xff1b; 2、使用步长为4x4#xff0c;大小为11的卷积核对图像进行卷积#xff0c;输出的特征层为96层#xff0c; 输出的shape为(55,55,96)#xff1b; 3、使用步长为2的最大池化层进行池化#xff0c;此时…网络思想 1、一张原始图片被resize到(224,224,3) 2、使用步长为4x4大小为11的卷积核对图像进行卷积输出的特征层为96层 输出的shape为(55,55,96) 3、使用步长为2的最大池化层进行池化此时输出的shape为(27,27,96) 4、使用步长为1x1大小为5的卷积核对图像进行卷积输出的特征层为256层 输出的shape为(27,27,256) 5、使用步长为2的最大池化层进行池化此时输出的shape为(13,13,256) 6、使用步长为1x1大小为3的卷积核对图像进行卷积输出的特征层为384层 输出的shape为(13,13,384) 7、使用步长为1x1大小为3的卷积核对图像进行卷积输出的特征层为384层 输出的shape为(13,13,384) 8、使用步长为1x1大小为3的卷积核对图像进行卷积输出的特征层为256层 输出的shape为(13,13,256) 9、使用步长为2的最大池化层进行池化此时输出的shape为(6,6,256) 10、两个全连接层最后输出为1000类
细节部分举例
第一层 第一层输入数据为原始图像的2242243的图像这个图像被111133代表 深度例如RGB的3通道的卷积核进行卷积运算卷积核对原始图像的每次 卷积都会生成一个新的像素。 卷积核的步长为4个像素朝着横向和纵向这两个方向进行卷积。 由此会生成新的像素 第一层有96个卷积核所以就会形成555596个像素层。 pool池化层这些像素层还需要经过pool运算池化运算的处理池化运 算的尺度由预先设定为33运算的步长为2则池化后的图像的尺寸为 55-3/2127。即经过池化处理过的规模为2727*96.
代码实现
网络主体部分AlexNet.py
from keras.models import Sequential
from keras.layers import Dense,Activation,Conv2D,MaxPooling2D,Flatten,Dropout,BatchNormalization
from keras.datasets import mnist
from keras.utils import np_utils
from keras.optimizers import Adam# 注意为了加快收敛我将每个卷积层的filter减半全连接层减为1024
def AlexNet(input_shape(224,224,3),output_shape2):# AlexNetmodel Sequential()# 使用步长为4x4大小为11的卷积核对图像进行卷积输出的特征层为96层输出的shape为(55,55,96)# 所建模型后输出为48特征层model.add(Conv2D(filters48, kernel_size(11,11),strides(4,4),paddingvalid,input_shapeinput_shape,activationrelu))model.add(BatchNormalization())# 使用步长为2的最大池化层进行池化此时输出的shape为(27,27,96)# 所建模型后输出为48特征层model.add(MaxPooling2D(pool_size(3,3), strides(2,2), paddingvalid))# 使用步长为1x1大小为5的卷积核对图像进行卷积输出的特征层为256层输出的shape为(27,27,256)# 所建模型后输出为128特征层model.add(Conv2D(filters128, kernel_size(5,5), strides(1,1), paddingsame,activationrelu))model.add(BatchNormalization())# 使用步长为2的最大池化层进行池化此时输出的shape为(13,13,256)# 所建模型后输出为128特征层model.add(MaxPooling2D(pool_size(3,3),strides(2,2),paddingvalid))# 使用步长为1x1大小为3的卷积核对图像进行卷积输出的特征层为384层输出的shape为(13,13,384)# 所建模型后输出为192特征层model.add(Conv2D(filters192, kernel_size(3,3),strides(1,1), paddingsame, activationrelu)) # 使用步长为1x1大小为3的卷积核对图像进行卷积输出的特征层为384层输出的shape为(13,13,384)# 所建模型后输出为192特征层model.add(Conv2D(filters192, kernel_size(3,3), strides(1,1), paddingsame, activationrelu))# 使用步长为1x1大小为3的卷积核对图像进行卷积输出的特征层为256层输出的shape为(13,13,256)# 所建模型后输出为128特征层model.add(Conv2D(filters128, kernel_size(3,3), strides(1,1), paddingsame, activationrelu))# 使用步长为2的最大池化层进行池化此时输出的shape为(6,6,256)# 所建模型后输出为128特征层model.add(MaxPooling2D(pool_size(3,3), strides(2,2), paddingvalid))# 两个全连接层最后输出为1000类,这里改为2类猫和狗# 缩减为1024model.add(Flatten())model.add(Dense(1024, activationrelu))model.add(Dropout(0.25))model.add(Dense(1024, activationrelu))model.add(Dropout(0.25))model.add(Dense(output_shape, activationsoftmax))return model
图像预处理部分utils.py
import matplotlib.image as mpimg
import numpy as np
import cv2
import tensorflow as tf
from tensorflow.python.ops import array_opsdef load_image(path):# 读取图片rgbimg mpimg.imread(path)# 将图片修剪成中心的正方形short_edge min(img.shape[:2])yy int((img.shape[0] - short_edge) / 2)xx int((img.shape[1] - short_edge) / 2)crop_img img[yy: yy short_edge, xx: xx short_edge]return crop_imgdef resize_image(image, size):with tf.name_scope(resize_image):images []for i in image:i cv2.resize(i, size)images.append(i)images np.array(images)return imagesdef print_answer(argmax):with open(./data/model/index_word.txt,r,encodingutf-8) as f:synset [l.split(;)[1][:-1] for l in f.readlines()]# print(synset[argmax])return synset[argmax]
训练部分train.py
from keras.callbacks import TensorBoard, ModelCheckpoint, ReduceLROnPlateau, EarlyStopping
from keras.utils import np_utils
from keras.optimizers import Adam
from model.AlexNet import AlexNet
import numpy as np
import utils
import cv2
from keras import backend as K
#K.set_image_dim_ordering(tf)
K.image_data_format() channels_firstdef generate_arrays_from_file(lines,batch_size):# 获取总长度n len(lines)i 0while 1:X_train []Y_train []# 获取一个batch_size大小的数据for b in range(batch_size):if i0:np.random.shuffle(lines)name lines[i].split(;)[0]# 从文件中读取图像img cv2.imread(r.\data\image\train / name)img cv2.cvtColor(img,cv2.COLOR_BGR2RGB)img img/255X_train.append(img)Y_train.append(lines[i].split(;)[1])# 读完一个周期后重新开始i (i1) % n# 处理图像X_train utils.resize_image(X_train,(224,224))X_train X_train.reshape(-1,224,224,3)Y_train np_utils.to_categorical(np.array(Y_train),num_classes 2) yield (X_train, Y_train)if __name__ __main__:# 模型保存的位置log_dir ./logs/# 打开数据集的txtwith open(r.\data\dataset.txt,r) as f:lines f.readlines()# 打乱行这个txt主要用于帮助读取数据来训练# 打乱的数据更有利于训练np.random.seed(10101)np.random.shuffle(lines)np.random.seed(None)# 90%用于训练10%用于估计。num_val int(len(lines)*0.1)num_train len(lines) - num_val# 建立AlexNet模型model AlexNet()# 保存的方式3代保存一次checkpoint_period1 ModelCheckpoint(log_dir ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5,monitoracc, save_weights_onlyFalse, save_best_onlyTrue, period3)# 学习率下降的方式acc三次不下降就下降学习率继续训练reduce_lr ReduceLROnPlateau(monitoracc, factor0.5, patience3, verbose1)# 是否需要早停当val_loss一直不下降的时候意味着模型基本训练完毕可以停止early_stopping EarlyStopping(monitorval_loss, min_delta0, patience10, verbose1)# 交叉熵model.compile(loss categorical_crossentropy,optimizer Adam(lr1e-3),metrics [accuracy])# 一次的训练集大小batch_size 128print(Train on {} samples, val on {} samples, with batch size {}..format(num_train, num_val, batch_size))# 开始训练model.fit_generator(generate_arrays_from_file(lines[:num_train], batch_size),steps_per_epochmax(1, num_train//batch_size),validation_datagenerate_arrays_from_file(lines[num_train:], batch_size),validation_stepsmax(1, num_val//batch_size),epochs50,initial_epoch0,callbacks[checkpoint_period1, reduce_lr])model.save_weights(log_dirlast1.h5)#保存模型
预测部分predict.py
import numpy as np
import utils
import cv2
from keras import backend as K
from model.AlexNet import AlexNet# K.set_image_dim_ordering(tf)
K.image_data_format() channels_firstif __name__ __main__:model AlexNet()model.load_weights(./logs/last1.h5)img cv2.imread(./test2.jpg)img_RGB cv2.cvtColor(img,cv2.COLOR_BGR2RGB)img_nor img_RGB/255img_nor np.expand_dims(img_nor,axis 0)img_resize utils.resize_image(img_nor,(224,224))#utils.print_answer(np.argmax(model.predict(img)))print(the answer is: ,utils.print_answer(np.argmax(model.predict(img_resize))))cv2.imshow(ooo,img)cv2.waitKey(0)