吴恩达团队 Tensorflow课程学习笔记 5-----多分类问题

最后一节学习多分类的案例-----剪刀,石头,布分类

1. 数据相关

⑴目录设置

每个子目录下都有三种类别: 石头、布、剪刀 .
在这里插入图片描述

⑵数据集

这里有一个新的数据集----包含约3000张图像,它们都是使用具有多种模型的CGI产生的(有男性的,女性的,还有许多不同肤色的),它们都是以白色为背景,分辨率为300×300,下面给出了一些示例.
在这里插入图片描述
数据集链接
上面给出的数据集链接里包含了训练集,验证集,以及测试集.

⑶获取数据

①有两个zip文件----训练集和测试集

!wget --no-check-certificate \
    https://storage.googleapis.com/laurencemoroney-blog.appspot.com/rps.zip \
    -O /tmp/rps.zip
  
!wget --no-check-certificate \
    https://storage.googleapis.com/laurencemoroney-blog.appspot.com/rps-test-set.zip \
    -O /tmp/rps-test-set.zip

②压缩至temp目录中

import os
import zipfile

local_zip = '/tmp/rps.zip'
zip_ref = zipfile.ZipFile(local_zip, 'r')
zip_ref.extractall('/tmp/')
zip_ref.close()

local_zip = '/tmp/rps-test-set.zip'
zip_ref = zipfile.ZipFile(local_zip, 'r')
zip_ref.extractall('/tmp/')
zip_ref.close()

③查看其中的文件

rock_dir = os.path.join('/tmp/rps/rock')
paper_dir = os.path.join('/tmp/rps/paper')
scissors_dir = os.path.join('/tmp/rps/scissors')

print('total training rock images:', len(os.listdir(rock_dir)))
print('total training paper images:', len(os.listdir(paper_dir)))
print('total training scissors images:', len(os.listdir(scissors_dir)))

rock_files = os.listdir(rock_dir)
print(rock_files[:10])

paper_files = os.listdir(paper_dir)
print(paper_files[:10])

scissors_files = os.listdir(scissors_dir)
print(scissors_files[:10])

输出结果包含每个类别的数量,一些文件名
在这里插入图片描述

④绘制一些文件

%matplotlib inline

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

pic_index = 2

next_rock = [os.path.join(rock_dir, fname) 
                for fname in rock_files[pic_index-2:pic_index]]
next_paper = [os.path.join(paper_dir, fname) 
                for fname in paper_files[pic_index-2:pic_index]]
next_scissors = [os.path.join(scissors_dir, fname) 
                for fname in scissors_files[pic_index-2:pic_index]]

for i, img_path in enumerate(next_rock+next_paper+next_scissors):
  #print(img_path)
  img = mpimg.imread(img_path)
  plt.imshow(img)
  plt.axis('Off')
  plt.show()

图像展示
在这里插入图片描述

⑷ 训练数据准备

虽然图像是300×300,设置图像生成器提供150×150方差. 它将即时调整图像的大小并增强训练目录中的图像.

train_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
        train_dir,
        target_size=(150,150),
        batch_size=128,
        class_mode='categorical') #多个类别

2. 模型相关

⑴建立模型

model = tf.keras.models.Sequential([
   
    tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(150, 150, 3)),
    tf.keras.layers.MaxPooling2D(2, 2),
    # The second convolution
    tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    # The third convolution
    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(512, activation='relu'),
    tf.keras.layers.Dense(3, activation='softmax') #三分类,softmax将所有的值转换为概率
])

⑵模型编译

model.compile(loss = 'categorical_crossentropy', #绝对交叉熵
              optimizer=RMSprop(lr=0.001), 
              metrics=['acc'])

⑶结果展示

①epochs=100

在这里插入图片描述

②epochs=25

在这里插入图片描述

⑷模型测试

import numpy as np
from google.colab import files
from keras.preprocessing import image

uploaded = files.upload()

for fn in uploaded.keys():
 
  # predicting images
  path = fn
  img = image.load_img(path, target_size=(150, 150))
  x = image.img_to_array(img)
  x = np.expand_dims(x, axis=0)

  images = np.vstack([x])
  classes = model.predict(images, batch_size=10)
  print(fn)
  print(classes)

某张图像的预测输出形式如下,测试结果中概率顺序依次对应为布、石头、剪刀(按字母顺序排序).
在这里插入图片描述
考虑这样一只手,通过神经网络最后的三个神经元,然后softmax输出三种类型的概率值(总和为1),类别判为概率最大值所对应的类别.
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值