原文:Deep Learning 2: Part 1 Lesson 1
作者:Hiromi Suenaga
shift+enter
来运行单元格(你可以按住shift
并多次按enter
键来继续下拉单元格),或者你可以单击顶部的“运行”按钮。单元格可以包含代码,文本,图片,视频等。%reload_ext autoreload
%autoreload 2
%matplotlib inline
# This file contains all the main external libs we'll use
from fastai.imports import *
from fastai.transforms import *
from fastai.conv_learner import *
from fastai.model import *
from fastai.dataset import *
from fastai.sgdr import *
from fastai.plots import *
PATH = "data/dogscats/"
sz=224
先看图片 [15:39]
!ls {PATH}
models sample test1 tmp train valid
!
表明使用 bash(shell)而不是 python!ls {PATH}valid
cats dogs
files = !ls {PATH}valid/cats | head files
['cat.10016.jpg', 'cat.1001.jpg', 'cat.10026.jpg', 'cat.10048.jpg', 'cat.10050.jpg', 'cat.10064.jpg', 'cat.10071.jpg', 'cat.10091.jpg', 'cat.10103.jpg', 'cat.10104.jpg']
dogs
或cats
)。img = plt.imread(f' {PATH} valid/cats/ {files[0]} ') plt.imshow(img);
f'{PATH}valid/cats/{files[0]}'
- 这是一个 Python 3.6 格式化字符串,可以方便地格式化字符串。img.shape
(198, 179, 3)
img[:4,:4]
array([[[ 29, 20, 23], [ 31, 22, 25], [ 34, 25, 28], [ 37, 28, 31]],
[[ 60, 51, 54], [ 58, 49, 52], [ 56, 47, 50], [ 55, 46, 49]],
[[ 93, 84, 87], [ 89, 80, 83], [ 85, 76, 79], [ 81, 72, 75]],
[[104, 95, 98], [103, 94, 97], [102, 93, 96], [102, 93, 96]]], dtype=uint8)
img
是一个三维数组(又名 3 维张量)[29, 20, 23]
)表示 0 到 255 之间的红绿蓝像素值让我们训练一个模型 [20:21]
以下是训练模型所需的三行代码:
data = ImageClassifierData.from_paths(PATH, tfms=tfms_from_model(resnet34, sz))
learn = ConvLearner.pretrained(resnet34, data, precompute= True )
learn.fit (0.01, 3)
[ 0. 0.04955 0.02605 0.98975] [ 1. 0.03977 0.02916 0.99219] [ 2. 0.03372 0.02929 0.98975]
1.
)是迭代数。