第6章
6.1 利用图像特征完成图像识别
6.1.1 加载训练数据集
cifar_train = data.get('cifar10-small', subset='train')
fig() + plot(cifar_train)
6.1.2 获取图片特征
hog = HOGExtractor()
hog_train = cifar_train.map(hog, on_field=0)
6.1.3 观察HOG特征
index = 10
hog_feature, label = hog_train[index]
fig() + plot(hog_feature, type='featvec')
index = 10
image, label = cifar_train[index]
fig() + plot(image, type='hog')
6.1.4 创建分类器并用支持向量机训练分类器
mlc = multi_class_classifier()
mlc.train(hog_train, alg=SVM())
6.1.5 在训练子集上测试模型
acc1 = mlc.accuracy(hog_train)
print('Cifar_train Accuracy: ', acc1)
6.1.6 加载测试数据集并获取图片特征
cifar_test = data.get('cifar10-small', subset='test')
fig() + plot(cifar_test)
hog_test = cifar_test.map(hog, on_field=0)
6.1.7 在测试子集上测试模型
acc2 = mlc.accuracy(hog_test)
print('Cifar_test Accuracy: ', acc2)
6.1.8 用分类器做预测
index = 10
image, label = cifar_test[index]
fig()+plot(image)
print('label:', label)
hog_feature = hog(image)
prediction = mlc.predict(hog_feature)
print('prediction: ', prediction)
6.2 利用深度神经网络完成图像分类
6.2.1 加载训练数据集
cifar_train = data.get('cifar10-small', subset='train')
fig() + plot(cifar_train)
6.2.2 获取数据的类别
label_names = cifar_train.meta['label_names']
print(label_names)
6.2.3 构建神经网络
backbone = ssnet(h=32, w=32, c=3)
net = NNClassifier(backbone, ncls=10)
6.2.4 模拟神经网络的训练
net.demo_train(cifar_train, epoch=5)
6.2.5 加载测试数据集并用神经网络做预测
cifar_test = data.get('cifar10-small', subset='test')
img, label = cifar_test[100]
fig() + plot(img)
label_name = label_names[label]
pred = net.predict(img)
pred_name = label_names[pred]
print("Label is {}, Prediction is {}.".format(label_name, pred_name))
6.3 提取照片中的人脸特征
6.3.1 获取数据集
face_photos = data.get('faces')
photo = face_photos[109]
fig() + plot(photo)
6.3.2 人脸检测与五官对准
photo_d = detect_faces(photo)
fig() + plot(photo_d)
6.3.3 人脸转正与剪裁
faces = crop_aligned_faces(photo_d)
fig(2, 1) + [plot(faces[0]), plot(faces[1])]
6.3.4 身份特征提取
feature1 = extract_face_feature(faces[0])
feature2 = extract_face_feature(faces[1])
6.4.5 身份特征比对
fig(2,1) + [plot(feature1, type='featvec'), plot(feature2, type='featvec')]
6.4 相册中的人脸聚类
6.4.1 获取数据集
ds = data.get('facefeat')
faces = ds.field(0)
features = ds.field(1)
6.4.2 模型创建与训练
model = KMeans(K=7)
model.train(features)
6.4.3 模型预测与效果展示
pred = model.predict_all(features)
fig() + plot(faces, pred, type='face_cluster')
6.4.4 K值的选择(手肘法)
Ks = [4, 5, 6, 7, 8]
losses = []
for k in Ks:
model = KMeans(K=k)
model.train(features, visual=False)
losses.append(model.loss)
fig() + plot(losses, Ks, type='elbow')
6.5 理解视频中的光流
6.5.1 获取视频数据集
ucf = data.get('ucf-small', subset='train')
video, label = ucf[1]
fig() + plot(video)
6.5.2 获取视频中的帧
frame0 = video[5]
frame1 = video[6]
fig(2,1)+[plot(frame0), plot(frame1)]
6.5.3 计算相邻帧的光流
flow = optical_flow(frame0, frame1)
6.5.4 使用向量可视化光流
frame_flow = image_add_flow(frame0, flow)
fig() + plot(frame_flow)
6.5.5 使用灰度图可视化光流
flow_x, flow_y = split_flow(flow)
fig(2, 1) + [plot(flow_x), plot(flow_y)]
6.5.6 提取光流视频
flowvideo = optical_flow_video(video)
flow0 = flowvideo[0]
video_arrow = video_add_flow(video, flowvideo)
fig() + plot(video_arrow)
6.6 利用光流直方图完成行为识别任务
6.6.1 加载视频数据集
trainset = data.get('ucf-small', subset='train')
video, label = trainset[10]
fig() + plot(video)
print(trainset.meta['label_names'][label])
6.6.2 计算光流直方图特征
flowvideo = optical_flow_video(video)
feat = hof(flowvideo)
fig() + plot(feat, type='featvec')
6.6.3 计算所有视频的特征
trainflow = trainset.map(optical_flow_video, on_field=0)
trainhof = trainflow.map(hof, on_field=0)
print(trainhof[10][0].shape)
6.6.4 训练分类器
mlc = multi_class_classifier()
mlc.train(trainhof, alg=SVM())
6.6.5 在训练集上测试分类器
acctrain = mlc.accuracy(trainhof)
print('Train Accuracy:', acctrain)
6.6.6 在测试集测试分类器
testset = data.get('ucf-small', subset='test')
testflow = testset.map(optical_flow_video, on_field=0)
testhof = testflow.map(hof, on_field=0)
acctest = mlc.accuracy(testhof)
print('Test Accuracy:', acctest)
6.7 用生成对抗网络生成明星图片
6.7.1 获取数据集
celeba = data.get('celeba-tiny')
fig() + plot(celeba)
6.7.2 构建生成对抗网络
gan_model = DCGAN()
6.7.3 GAN网络的训练
gan_model.demo_train(celeba, step=200)
6.7.4 生成图片并展示
image = gan_model.generate_tile(5, 5)
fig() + plot(image)
6.7.5 加载不同迭代轮数的预训练模型
gan_model.loadparam(1.5)
image = gan_model.generate_tile(5, 5)
fig() + plot(image)