VGGNet实现CIFAR-100图像识别-1(数据预处理,one-hot)

写在最前:这个是和用户id:weixin_55841621的一个合作项目,得到对方允许后写了个心得总结,未经授权不得转载或直接复制使用。初学者,对于一些问题的理解可能不是很到位,请多多指教或者一起讨论~

导入数据

这个是从keras直接导入数据,y标签是用0-99的数字表示,但是从cifar数据官方网站下载的数据中,meta文件包含了y标签的文字表示,即类的语义名字。

# Load data from keras (https://keras.io/api/datasets/cifar100/),
# this will download dataset from https://www.cs.toronto.edu/~kriz/cifar-100-python.tar.gz

(X, y), (X_test, y_test) = tf.keras.datasets.cifar100.load_data(label_mode="fine") # "fine": the labels of 100 subclasses
print(X.shape, y.shape, X_test.shape, y_test.shape) # The dataset contains 50,000 32x32 color training images and 10,000 test images

数据预处理

将y标签的shape从(50000, 1)改成(50000,),为了与张量(tensor)相匹配。

方法1

# Change the shape of y to (50000,)

y = tf.squeeze(y)
y_test = tf.squeeze(y_test) 

print(y.shape)
print(y_test.shape)

type(y)
type(y_test)

# 输出:
# (50000,)
# (10000,)
# tensorflow.python.framework.ops.EagerTensor

方法2

# Change the shape of y to (50000,)

y = y[:,0]
y_test = y_test[:,0]

print(y.shape)
print(y_test.shape)

type(y)
type(y_test)

# 输出:
# (50000,)
# (10000,)
# numpy.ndarray

可能会遇到的问题

两种方法的不同是他们最后生成的数据类型不同,如果使用方法1,在后面拆分训练集和验证集的时候会遇到如下错误:
TypeError: Only integers, slices (:), ellipsis (...), tf.newaxis (None) and scalar tf.int32/tf.int64 tensors are valid indices, got array([36688, 3668, 12964, …, 14020, 49037, 29856])

解决办法

  1. 使用方法2
  2. 先拆分训练集和验证集,再将y的shape从(50000,1)变成(50000,)
  3. 按照这个方法(from_tensor_slices和map),图片来自于B站视频在这里插入图片描述

Normalization和拆分训练集、验证集

# Normalization

X = X.astype('float32')/255.0 # tf.cast(X, dtype=tf.float32)/255.0
X_test = X_test.astype('float32')/255.0
# Split the data into training and validation set

X_train, X_valid, y_train, y_valid = train_test_split(X, y, shuffle=True, random_state=85, test_size=0.1)
print(X_train.shape, y_train.shape, X_valid.shape, y_valid.shape)

One-hot编码

关于one-hot我们有两种选择,

  1. one-hot编码 (softmax激活函数需要),加上softmax激活函数,加上’categorical_crossentropy’损失函数,VGGNet是采用了这种方法。代码如下:
# Change class labels to binary class matrix
y_train_cate = tf.keras.utils.to_categorical(y_train, 100)
print(y_train_cate.shape)

y_valid_cate = tf.keras.utils.to_categorical(y_valid, 100)
print(y_valid_cate.shape)

y_test_cate = tf.keras.utils.to_categorical(y_test, 100)
print(y_test_cate.shape)
  1. 不采用one-hot编码,这个时候我们y标签的shape是(50000,1),加上’SparseCategoricalCrossentropy’损失函数,设置参数from_logits=True,来应用softmax激活函数,另一篇博文AlexNet采用这种方法。

未完待续,接下来请看另一篇博文:VGGNet实现CIFAR-100图像识别-2(图像增强/ImageDataGenerator)

VGGNet实现CIFAR-100图像识别-2(图像增强/ImageDataGenerator)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值