Intro

这是深度学习第4课。

在本课程结束时,您将能够使用迁移学习为您的自定义目标构建高度准确的计算机视觉模型,即使您的数据相对较少。

Lesson

[1]

from IPython.display import YouTubeVideo
YouTubeVideo('mPFq5KMxKVw', width=800, height=450)

Sample Code

Specify Model

[2]

from tensorflow.python.keras.applications import ResNet50
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Flatten, GlobalAveragePooling2Dnum_classes = 2
resnet_weights_path = '../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'my_new_model = Sequential()
my_new_model.add(ResNet50(include_top=False, pooling='avg', weights=resnet_weights_path))
my_new_model.add(Dense(num_classes, activation='softmax'))# Say not to train first layer (ResNet) model. It is already trained
my_new_model.layers[0].trainable = False

Compile Model

[3]

my_new_model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])

Fit Model

[4]

from tensorflow.python.keras.applications.resnet50 import preprocess_input
from tensorflow.python.keras.preprocessing.image import ImageDataGeneratorimage_size = 224
data_generator = ImageDataGenerator(preprocessing_function=preprocess_input)train_generator = data_generator.flow_from_directory('../input/urban-and-rural-photos/rural_and_urban_photos/train',target_size=(image_size, image_size),batch_size=24,class_mode='categorical')validation_generator = data_generator.flow_from_directory('../input/urban-and-rural-photos/rural_and_urban_photos/val',target_size=(image_size, image_size),class_mode='categorical')my_new_model.fit_generator(train_generator,steps_per_epoch=3,validation_data=validation_generator,validation_steps=1)
Found 72 images belonging to 2 classes.
Found 20 images belonging to 2 classes.
Epoch 1/1
3/3 [==============================] - 29s 10s/step - loss: 0.5130 - acc: 0.8056 - val_loss: 0.3568 - val_acc: 0.9000<tensorflow.python.keras._impl.keras.callbacks.History at 0x7f9f5bc56a20>

Note on Results:

在此阶段,打印出的验证准确度可能比训练准确度更好。 这一开始可能令人费解。
之所以出现这种情况,是因为随着网络的改进,在多个点计算训练精度(卷积中的数字正在更新以使模型更准确)。 当模型看到第一个训练图像时,网络是不准确的,因为权重还没有被训练/改进。 
在模型完成所有数据后计算验证损失和准确度度量。 因此,在计算这些分数时,网络已经过全面训练。
这在实践中不是一个严重的问题,我们不会担心它。

Your Turn

写下您自己的内核来进行迁移学习。

Exercise:Using Transfer Learning

Exercise Introduction

拍摄我们深度学习视频的摄像师提到了一个我们可以通过深度学习解决的令人沮丧的问题。

他提供扫描照片和幻灯片的服务,以数字方式存储它们。他使用的机器能够快速扫描许多照片。但是根据原始照片的方向,许多图像都是横向数字化的。他目前花了很多时间寻找哪些照片需要侧向旋转,所以他可以修复它们。

如果这个过程可以自动化,那将节省他很多时间。在本练习中,您将构建一个模型,用于区分哪些照片是横向的,哪些照片是竖向的。

如果您打算在商业上销售此服务,则可以使用大型数据集来训练模型。但即使是一个小数据集,我们也会取得巨大成功。我们将使用一个小型关于狗的图片数据集,其中一半是横向旋转。

指定和编译模型看起来与您看到的示例相同。但是您需要进行一些更改以适应模型。

1)Specify the Model

由于这是您第一次,您将无法从头开始创建。

我们已经填写了您需要的大部分代码,但是将一些关键部分留空了。

在下面的代码中填写(标有____)。 然后取消注释这些行并运行单元格。

[1]

from tensorflow.python.keras.applications import ResNet50
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Flatten, GlobalAveragePooling2D# num_classes is the number of categories your model chooses between for each prediction
# num_classes = ____
resnet_weights_path = '../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'# my_new_model = Sequential()
# my_new_model.add(ResNet50(include_top=False, pooling='avg', weights=resnet_weights_path))
# my_new_model.add(Dense(num_classes, activation='softmax'))# The value below is either True or False.  If you choose the wrong answer, your modeling results
# won't be very good.  Recall whether the first layer should be trained/changed or not.
# my_new_model.layers[0].trainable = ____

2)Compile the Model

我们再次提供了大部分代码,并留下了一个非常重要的部分。 填写空白(标有____)。 然后取消注释该行代码并运行单元格。

[2]

# We are calling the compile command for some python object.
# Which python object is being compiled? Fill in the answer so the compile command works.
# ____.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])

3)Fit Model

您的训练数据位于../input/dogs-gone-sideways/images/train目录中。 验证数据在../input/dogs-gone-sideways/images/val中。 设置train_generator和validation_generator时使用该信息。

您有220张训练数据图像和217张验证数据。 对于训练生成器,选择批量大小为10。在fit_generator调用中找出steps_per_epoch的相应值? 它与示例中的不同。

填写所有空白(再次标记为____)。 然后取消注释每一行并运行代码单元格。 观察您的模型训练权重并提高准确度。

【3】

from tensorflow.python.keras.applications.resnet50 import preprocess_input
from tensorflow.python.keras.preprocessing.image import ImageDataGeneratorimage_size = 224
data_generator = ImageDataGenerator(preprocess_input)#train_generator = data_generator.flow_from_directory(
#        directory = ____,
#        target_size=(image_size, image_size),
#        batch_size=____,
#        class_mode='categorical')#validation_generator = data_generator.flow_from_directory(
#        directory = ____,
#        target_size=(image_size, image_size),
#        class_mode='categorical')#my_new_model.fit_generator(
#        train_generator,
#        steps_per_epoch=____,
#        validation_data=____,
#        validation_steps=1)

您能从结果中判断出您的模型在验证数据中的正确时间吗?

在下一步中,我们将看看我们是否可以改进。

Keep Going

继续学习数据增强。 这是改进模型的一种巧妙而简单的方法。 然后,您将数据增强应用于此自动图像旋转问题。

4.Transfer Learning相关推荐

  1. 迁移学习(Transfer learning)、重用预训练图层、预训练模型库

    迁移学习(Transfer learning).重用预训练图层.预训练模型库 目录 迁移学习(Transfer learning).重用预训练图层.预训练模型库 迁移学

  2. 基于Keras Application和Densenet迁移学习(transfer learning)的乳腺癌图像分类模型(良性、恶性)

    基于Keras Application和Densenet迁移学习(transfer learning)的乳腺癌图像分类模型(良性.恶性) 概论: 美国癌症学会官方期刊发表<2018年全球癌症统计 ...

  3. Domain adaptation:连接机器学习(Machine Learning)与迁移学习(Transfer Learning)

    domain adaptation(域适配)是一个连接机器学习(machine learning)与迁移学习(transfer learning)的新领域.这一问题的提出在于从原始问题(对应一个 so ...

  4. 深度学习不得不会的迁移学习Transfer Learning

    http://blog.itpub.net/29829936/viewspace-2641919/ 2019-04-18 10:04:53 目录 一.概述 二.什么是迁移学习? 2.1 模型的训练与预 ...

  5. 吴恩达深度学习笔记(67)-迁移学习(Transfer learning)

    https://www.toutiao.com/a6644868806923518471/ 2019-01-11 07:36:41 迁移学习(Transfer learning) 深度学习中,最强大的 ...

  6. 迁移学习简介(transfer learning)

    迁移学习简介(transfer learning) https://blog.csdn.net/qq_33414271/article/details/78756366 一.什么是迁移学习? 迁移学习 ...

  7. 【深度学习】一文看懂 (Transfer Learning)迁移学习(pytorch实现)

    前言 你会发现聪明人都喜欢"偷懒", 因为这样的偷懒能帮我们节省大量的时间, 提高效率. 还有一种偷懒是 "站在巨人的肩膀上". 不仅能看得更远, 还能看到更多 ...

  8. AI入门:Transfer Learning(迁移学习)

    迁移学习是一种机器学习方法,就是把为任务 A 开发的模型作为初始点,重新使用在为任务 B 开发模型的过程中 Pokemon Dataset 通过网络上收集宝可梦的图片,制作图像分类数据集.我收集了5种 ...

  9. 李宏毅机器学习课程-Transfer Learning

    深度学习 -> 强化学习 ->迁移学习(杨强教授报告) 李宏毅机器学习课程-Transfer Learning 迁移学习-吴恩达 freeze 待处理的 理解深层神经网络中的迁移学习及Te ...

最新文章

  1. vue click事件冒泡,默认行为
  2. MySql cmd下的学习笔记 —— 有关建立数据库的操作(连接Mysql,建立数据库,删除数据库等等)...
  3. 原来C语言还可以这样实现“泛型编程”!
  4. SEO原创文章制作器
  5. 我的高质量软件发布心得
  6. 5 分钟全面掌握 Python 装饰器
  7. oracle 11g空表不能exp导出问题解决方案
  8. CSDN使用富文本编辑器为所发布的文章生成右侧目录
  9. Journey Of Code组组员贡献率
  10. android类之间的关系,Android 中Activity,Window和View之间的关系
  11. web.xml中配置DispatcherServlet前端控制器和CharacterEncodingFilter字符过滤器后web-app标签显红报错
  12. GIT插件EGIT使用手册
  13. 思科常用配置实例的常用操作项
  14. 简单记录 Part1.3
  15. 一名菜鸟程序员的跳槽经历以及其所感所想(二)
  16. XR806与鸿蒙,简化构建环境流程
  17. 龙迅LT86102SXE 是 Lontium 的第 4 代 2 端口 HDMI/DVI 分配器
  18. exception java_总结java的exception
  19. 微信公众号开发移动端应用
  20. 【LTspice】007 Analysis Command 仿真命令

热门文章

  1. NOI-砝码称重v2 多重背包 生成函数
  2. tensorflow 指定cpu 但是还会运行 gpu_PyTorch VS TensorFlow 谁最强?这是标星 15000+ Transformers 库的运行结果...
  3. mysql分库一致性_分库分表带来的完整性和一致性问题
  4. C#中变量(成员变量、局部变量、全局变量)的作用域
  5. android监听器作用,android - 监听器和接收器(Android)有什么区别?
  6. 图片自动翻转css代码,用css实现图片翻转(示例代码)
  7. 微型计算机在温室管理中的应用初探,文献综述-测控051-陈杰.doc
  8. micropython lcd触摸屏显示中文_基于Micropython的天气显示 进程帖
  9. golang中文文档_【译】Go 语言源码贡献官方指导文档
  10. Asterisk realtime 之SIP用户动态写入mysql 数据库