声明:版权所有,转载请联系作者并注明出处  http://blog.csdn.net/u013719780?viewmode=contents

博主简介:风雪夜归子(英文名:Allen),机器学习算法攻城狮,喜爱钻研Meachine Learning的黑科技,对Deep Learning和Artificial Intelligence充满兴趣,经常关注Kaggle数据挖掘竞赛平台,对数据、Machine Learning和Artificial Intelligence有兴趣的童鞋可以一起探讨哦,个人CSDN博客:http://blog.csdn.net/u013719780?viewmode=contents

很多童鞋可能知道,深度学习需要大量数据才能训练处一个较好的模型。但是,有时候我们很难获取大量数据,因为得到足够大样本量的特定领域的数据集并不是那么容易,这是否就意味着我们不能使用上深度学习这一黑科技啦?我很高兴的告诉大家,事实并非如此。迁移学习就可以帮助我们使用上深度学习这一高大上的技术。

何为迁移学习?迁移学习是指使用一个预训练的网络:比如 VGG16 。VGG16 是基于大量真实图像的 ImageNet 图像库预训练的网络。我们将学习的 VGG16 的权重迁移(transfer)到自己的卷积神经网络上作为网络的初始权重,然后微调(fine-tune)这些预训练的通用网络使它们能够识别出人的activities图像,从而提高对HAR的预测效果。

迁移学习究竟如何使用?各位童鞋直接看下面的代码吧~

'''
This script reuses pieces of code from the post:
"Building powerful image classification models using very little data"
from blog.keras.io
and from:
https://www.kaggle.com/tnhabc/state-farm-distracted-driver-detection/keras-sample
The training data can be downloaded at:
https://www.kaggle.com/c/state-farm-distracted-driver-detection/data
'''import os
import h5py
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from keras import optimizers
from keras.models import Sequential
from keras.layers import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.layers import Activation, Dropout, Flatten, Dense
from regularizers import EigenvalueRegularizer
from numpy.random import permutation
from keras.optimizers import SGD
import pandas as pd
import datetime
import glob
import cv2
import math
import pickle
from collections import OrderedDict
from keras import backend as K# Enter here the path to the model weights files:
weights_path = '/home/oswaldo/video_classification/competitionKaggle/vgg16_weights.h5'
# Enter here the path to the top-model weights files:
top_model_weights_path = '/home/oswaldo/video_classification/competitionKaggle/fc_model.h5'
# Enter here the path for storage of the whole model weights (VGG16+top classifier model):
whole_model_weights_path = '/home/oswaldo/video_classification/competitionKaggle/whole_model.h5'
# Enter here the name of the folder that contains the folders c0, c1,..., c9, with the training images belonging to classes 0 to 9:
train_data_dir = 'train'
# Enter here the name of the folder where is the test images (the data evalueted in the private leaderboard):
test_data_dir = 'test'test_images_path = 'test/test'# Enter here the features of the data set:
img_width, img_height = 224, 224
nb_train_samples = 22424
nb_test_samples = 79726
color_type_global = 3# You can set larger values here, according with the memory of your GPU:
batch_size = 32# Enter here the number of training epochs (with 80 epochs the model was positioned among
# the 29% best competitors in the private leaderboard of state-farm-distracted-driver-detection)
# According to our results, this model can achieve a better performance if trained along a larger
# number of epochs, due to the agressive regularization with Eigenvalue Decay that was adopted.
nb_epoch = 80#Enter here the path for the whole model (VGG16+top classifier model):
whole_model_weights_path = '/home/oswaldo/video_classification/competitionKaggle/whole_model.h5'# build the VGG16 network:
model = Sequential()
model.add(ZeroPadding2D((1, 1), input_shape=(3, img_width, img_height)))model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_2'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_2'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
In [15]:
model.output_shape

Out[15]:
(None, 512, 7, 7)

最后图像的输出维度是512x7x7。这个结果是怎么来的呢?下面我们详细的推导一下:

图像的输入维度    3x224x224

ZeroPadding2D    3x226x226

conv1_1                64x224x224

ZeroPadding2D    64x226x226

conv1_2                64x224x224

MaxPooling2D      64x112x112

ZeroPadding2D     64x114x114

conv2_1                128x112x112

ZeroPadding2D     128x114x114

conv2_2                128x112x112

MaxPooling2D      128x56x56

ZeroPadding2D     128x58x58

conv3_1                 256x56x56

ZeroPadding2D     256x58x58

conv3_2                 256x56x56

ZeroPadding2D     256x58x58

conv3_3                 256x56x56

MaxPooling2D       256x28x28

ZeroPadding2D      256x30x30

conv4_1                  512x28x28

ZeroPadding2D      512x30x30

conv4_2                  512x28x28

ZeroPadding2D      512x30x30

conv4_3                  512x28x28

MaxPooling2D        512x14x14

ZeroPadding2D 512x16x16

conv5_1         512x14x14

ZeroPadding2D 512x16x16

conv5_2         512x14x14

ZeroPadding2D 512x16x16

conv5_3         512x14x14

MaxPooling2D 512x7x7

所有模型最后的输出维度是512x7x7。

接下来的部分直接上代码

# loading the weights of the pre-trained VGG16:assert os.path.exists(weights_path), 'Model weights not found (see "weights_path" variable in script).'
f = h5py.File(weights_path)
for k in range(f.attrs['nb_layers']):if k >= len(model.layers):breakg = f['layer_{}'.format(k)]weights = [g['param_{}'.format(p)] for p in range(g.attrs['nb_params'])]model.layers[k].set_weights(weights)
f.close()
print('Model loaded.')# building a classifier model on top of the convolutional model:top_model = Sequential()
top_model.add(Flatten(input_shape=model.output_shape[1:]))
top_model.add(Dense(64, activation='relu', W_regularizer=EigenvalueRegularizer(10)))
top_model.add(Dense(10, activation='softmax', W_regularizer=EigenvalueRegularizer(10)))
top_model.load_weights(top_model_weights_path)# add the model on top of the convolutional base
model.add(top_model)# setting the first 15 layers to non-trainable (the original weights will not be updated)for layer in model.layers[:15]:layer.trainable = False# Compiling the model with a SGD/momentum optimizer:model.compile(loss = "categorical_crossentropy",optimizer=optimizers.SGD(lr=1e-6, momentum=0.9),metrics=['mean_squared_logarithmic_error', 'accuracy'])# Data augmentation:train_datagen = ImageDataGenerator(shear_range=0.3, zoom_range=0.3, rotation_range=0.3)
test_datagen = ImageDataGenerator()print('trainning')
train_generator = train_datagen.flow_from_directory(train_data_dir,target_size=(img_height, img_width),batch_size=32,class_mode='categorical')print('testing')
test_generator = test_datagen.flow_from_directory(test_data_dir,target_size=(img_height, img_width),batch_size=32,class_mode='categorical',shuffle=False)class_dictionary = train_generator.class_indices
sorted_class_dictionary = OrderedDict(sorted(class_dictionary.items()))
sorted_class_dictionary = sorted_class_dictionary.values()
print(sorted_class_dictionary)# Fine-tuning the model:
model.fit_generator(train_generator,samples_per_epoch=nb_train_samples,nb_epoch=nb_epoch,validation_data=train_generator,nb_val_samples=nb_train_samples)model.save_weights(whole_model_weights_path)aux = model.predict_generator(test_generator, nb_test_samples)
predictions = np.zeros((nb_test_samples, 10))# Rearranging the predictions:ord = [5, 0, 6, 2, 7, 9, 1, 4, 8, 3]for n in range(10):i = ord[n]print(i)print(aux[:, i])predictions[:, n] = aux[:, i]# Trick to improve the multi-class logarithmic loss (the evaluation metric of state-farm-distracted-driver-detection from Keras):predictions = 0.985 * predictions + 0.015def get_im(path, img_width, img_height, color_type=1):if color_type == 1:img = cv2.imread(path, 0)elif color_type == 3:img = cv2.imread(path)# Reduce sizeresized = cv2.resize(img, (img_height, img_width))return resizeddef load_test(img_width, img_height, color_type=1):print('Read test images')path = os.path.join(test_images_path, '*.jpg')files = glob.glob(path)X_test = []X_test_id = []total = 0thr = math.floor(len(files)/10)for fl in files:flbase = os.path.basename(fl)img = get_im(fl, img_width, img_height, color_type)X_test.append(img)X_test_id.append(flbase)total += 1if total % thr == 0:print('Read {} images from {}'.format(total, len(files)))return X_test, X_test_idX_test, test_id = load_test(img_width, img_height, color_type_global)def create_submission(predictions, test_id):result1 = pd.DataFrame(predictions, columns=['c0', 'c1', 'c2', 'c3','c4', 'c5', 'c6', 'c7','c8', 'c9'])result1.loc[:, 'img'] = pd.Series(test_id, index=result1.index)now = datetime.datetime.now()if not os.path.isdir('subm'):os.mkdir('subm')suffix = '_' + str(now.strftime("%Y-%m-%d-%H-%M"))sub_file = os.path.join('subm', 'submission_' + suffix + '.csv')result1.to_csv(sub_file, index=False)create_submission(predictions, test_id)

机器学习实验(五):用迁移学习方法基于keras建立卷积神经网络进行人体动作识别(HAR)相关推荐

  1. 空间注意力机制sam_一种基于注意力机制的神经网络的人体动作识别方法与流程...

    本发明属于计算机视觉领域,具体来说是一种基于注意力机制的神经网络的人体动作识别的方法. 背景技术: 人体动作识别,具有着非常广阔的应用前景,如人机交互,视频监控.视频理解等方面.按目前的主流方法,可主 ...

  2. 基于Keras的卷积神经网络(CNN)可视化

    基于Keras的卷积神经网络(CNN)可视化 标签(空格分隔): 深度学习 卷积神经网络可视化 本文整理自Deep Learning with Python,书本上完整的代码在 这里的5.4节,并陪有 ...

  3. 一种基于深度学习(卷积神经网络CNN)的人脸识别算法-含Matlab代码

    目录 一.引言 二.算法的基本思想 三.算法数学原理 3.1 权值共享 3.2 CNN结构 四.基于卷积神经网络的人脸识别算法-Matlab代码 五.Matlab源代码获取 一.引言 在工程应用中经常 ...

  4. cnn神经网络可以用于数据拟合吗_使用Keras搭建卷积神经网络进行手写识别的入门(包含代码解读)...

    本文是发在Medium上的一篇博客:<Handwritten Equation Solver using Convolutional Neural Network>.本文是原文的翻译.这篇 ...

  5. 基于步态能量图和CNN卷积神经网络的人体步态识别matlab仿真

    目录 1.算法描述 2.仿真效果预览 3.MATLAB核心程序 4.完整MATLAB 1.算法描述 步态能量图(Gait Engery Image, GEI)是步态检测中最非常常用的特征,提取方法简单 ...

  6. 基于Keras的卷积神经网络用于猫狗分类(进行了数据增强)+卷积层可视化

    接着我上一篇博客,https://blog.csdn.net/fanzonghao/article/details/81149153. 在上一篇基础上对数据集进行数据增强.函数如下: "&q ...

  7. 基于Keras的卷积神经网络用于猫狗分类(未进行数据增强)+卷积层可视化

    首先看数据集路径: cats和dogs存放的就是各种大小的猫狗图片. 读取数据集代码: import os import matplotlib.pyplot as plt ""&q ...

  8. 机器学习 实验五 垃圾分类

    机器学习 实验五 垃圾分类 一.实验环境 PC机,Python 二.代码 #%% import torch import torch.nn as nn import torch.nn.function ...

  9. 2022-05-08 基于卷积神经网络ResNet的车型识别(实验)

    人工智能应用--基于卷积神经网络ResNet的车型识别 一.实验目的 熟悉ResNet卷积神经网络 熟悉物体检测+识别的整体流程 二.实验内容与记录 在给定的6类车型图片数据库上,使用ResNet18 ...

最新文章

  1. 实现HTTP协议Get、Post和文件上传功能——使用libcurl接口实现
  2. Qt Creator使用版本控制系统
  3. 如何看待程序员内卷现象
  4. 手把手教你写个小程序定时器管理库
  5. 2017年云主机性能测评报告
  6. Maven使用assembly对多模块进行打包
  7. 谈java的导入(import)
  8. ggplot 非常难调的参数
  9. java.lang.OutOfMemoryError: PermGen space tomcat启动项目出错
  10. unity人物旋转移动代码_Unity实现人物旋转和移动效果
  11. 清明节偷偷训练“熊猫烧香”,结果我的电脑为熊猫“献身了”!
  12. 华为android界面强刷救砖教程,华为手机救砖教程 华为手机开不了机变砖自救
  13. WinRAR 去除广告弹窗,简单4步亲测有效!
  14. 简单个人网页设计 静态HTML动物主题网页 DW个人网站模板 简单宠物网页作品代码 个人网页制作 个人网页Dreamweaver设计与实现
  15. 百度图片爬虫【图片编码处理】
  16. CSS3 will-change提高页面动画等渲染性能
  17. 手把手教你扩展个人微信号(2)
  18. Python-OpenCV按住鼠标左键绘制图形
  19. 攻防世界misc——misc1
  20. php骑手轨迹_轨迹分析,如何解决车辆定位与道路的偏差?|斑马数智技术内参...

热门文章

  1. 2022起重机司机(限桥式起重机)操作证考试题库及答案
  2. aspire鹦鹉螺_【详评兔】Aspire Nautilus 2 鹦鹉螺2成品雾化器 | 经典传承
  3. 值得收藏的设计实用字体的8条规则
  4. 无锡考研——全日制和非全日制研究生的区别
  5. QT自定义控件--键盘输入框
  6. 哪种蓝牙耳机佩戴舒服?长久佩戴舒适的蓝牙耳机推荐
  7. vue 下拉框点击没有反应
  8. java的redis的作用_redis用途
  9. for循环与if判断及其复合使用
  10. android studio SQLScout插件查看sqlite数据库