import tensorflow as tf
import numpy as np
import os
#%%获取数据集路径中的图片(文件名路径)和标签列表
def get_files(file_dir):
'''
Args:
file_dir: file directory (包含图片的文件夹的路径)
Returns:
list of images and labels (images_list为文件名(图片路径)列表,不是文件(图片))
'''
ys = [] # 存放cats图片路径的列表
label_ys = [] # 存放cats标签的列表
ns = []
label_ns = []
for file in os.listdir(file_dir): # os.listdir():列出路径下所有文件名(也就是图片文件夹下的所有图片的路径)
name = file.split(sep='.') # string.split():文件名根据'.'分割成一个列表,可以把关键词sep去掉
if name[0]=='y':
ys.append(file_dir + file) # 拼成图片的完整路径并存放在cats列表中
label_ys.append(0) # 用0代表cat的标签并存放在label_cats列表中
else:
ns.append(file_dir + file)
label_ns.append(1)
print('There are %d ys\nThere are %d ns' %(len(ys), len(ns)))
# 总结:在numpy中array中分行列,一般列表不分(只是序列而已)。
image_list = np.hstack((ys, ns)) # np.hstack():按列堆叠数组 (就是把猫狗列表堆成一行形成一个列表)(一行)
label_list = np.hstack((label_ys, label_ns))
temp = np.array([image_list, label_list]) # 创建numpy数组(把两个list放到一个list中),以便使用numpy函数(两行)
temp = temp.transpose() # numpy数组转置(把两个list中对应位置中的元素形成一个个list元素)(两列)
np.random.shuffle(temp) # 随机打乱列表(后面生成batch中就不需要打乱了),在numpy中做shuffle比较快
image_list = list(temp[:, 0]) # 取第一列形成图片路径列表
label_list = list(temp[:, 1])
label_list = [i for i in label_list] # 遍历标签列表使里面数据类型变成整数
return image_list, label_list # 返回包含图像路径的列表和对应标签的列表(0-cat,1-dog)
#%%从队列中读取数据生成batch
def get_batch(image, label, image_W, image_H, batch_size, capacity):
'''
Args:
image: list type (image_list)
label: list type (label_list)
image_W: image width
image_H: image height
batch_size: batch size
capacity: the maximum elements in queue
Returns:(tensor就是多维数组)
image_batch: 4D tensor [batch_size, width, height, 3], dtype=tf.float32
label_batch: 1D tensor [batch_size], dtype=tf.int32
'''
image = tf.cast(image, tf.string) # 把python中list数据类型转换成tensorflow能够识别的tensor数据类型
label = tf.cast(label, tf.int32) # tf.cast():转换数据类型
# make an input queue (tensorflow提供几个函数用来生成队列)
input_queue = tf.train.slice_input_producer([image, label]) # tf.train.slice_input_producer()生成一个队列,然后把image和label放入一个list中送入其中。(分开才用slice)
# 从队列中取元素 # 可以添加参数shuffle=True来打乱样本进入队列的顺序(里面有个可选参数num_epoch,不选的化可以无限进出队列,不只是一个epoch)
label = input_queue[1]
image_contents = tf.read_file(input_queue[0]) # tf.read_file():读取图片
image = tf.image.decode_jpeg(image_contents, channels=3) # 解码器(jpeg,jpg)解码得到图片的矩阵形式,如果格式不一样需要再送入队列前转换成统一图片格式
######################################
# data argumentation should go to here # 可以提高模型精度 (要多去官网查API函数(具体功能),就跟matlab一样,用的多并且去官网查文档(不知道在哪个模块,搜索关键词)就熟悉了,不用就会忘)
# image = tf.random_crop(image, [24, 24, 3]) # randomly crop the image size to 24 x 24
# image = tf.image.random_flip_left_right(image)
# image = tf.image.random_brightness(image, max_delta=63)
# image = tf.image.random_contrast(image,lower=0.2,upper=1.8)
######################################
# image = tf.image.resize_image_with_crop_or_pad(image, image_W, image_H) #图像进行裁剪和填充(在中心进行,大的裁剪,小的扩充),保持大小一致
image = tf.image.convert_image_dtype(image, dtype=tf.float32) # 解码后是整型,转换时要先转化为实数型再转为实数型,这样导致精度缺失。建议先转化为实数型。
image = tf.image.resize_images(image, [image_W, image_H], method=0)
# if you want to test the generated batches of images, you might want to comment the following line.
# 如果想看到正常的图片,请注释掉122行(标准化)和 137行(image_batch = tf.cast(image_batch, tf.float32))
# 训练时不要注释掉!
image = tf.image.per_image_standardization(image) # 将所有图片进行标准化(减去均值,除以方差)(不是归一化),加速神经网络的训练。
# 生成批次
image_batch, label_batch = tf.train.batch([image, label], # 按照给定的tensor顺序,把batch_size个tensor推送到文件队列(内存)(不是文件名队列),作为训练一个batch的数据,等待tensor出队执行计算
batch_size= batch_size, # 批次数
num_threads= 64, # 执行tensor入队操作的线程数量,可以设置使用多个线程同时并行执行,提高运行效率,但也不是数量越多越好
capacity = capacity) # 队列中最大容量
#you can also use shuffle_batch
#image_batch, label_batch = tf.train.shuffle_batch([image,label], # tf.train.shuffle_batch()与tf.train.batch()的区别就是打乱顺序,但是之前生成列表时已经打乱。
# batch_size=BATCH_SIZE, # tf.train.slice_input_producer(shuffle=True)送入队列时也可以打乱。
# num_threads=64,
# capacity=CAPACITY,
# min_after_dequeue=CAPACITY-1)
# label_batch = tf.reshape(label_batch, [batch_size]) # 改变label_batch形状,变成batch_size行的tensor
image_batch = tf.cast(image_batch, tf.float32) # 把解码处理过的图片转换成tensorflow指定输入数据类型tf.float32。
# ONE-HOT
n_classes = 2
label_batch = tf.one_hot(label_batch, depth= n_classes)
label_batch = tf.cast(label_batch, dtype=tf.int32)
label_batch = tf.reshape(label_batch, [batch_size, n_classes])
return image_batch, label_batch # 这两个都是tensor,不是列表,进入文件队列(内存)(不是文件名队列)。
#%% TEST
# To test the generated batches of images
# When training the model, DO comment the following codes
#import matplotlib.pyplot as plt # 库就是指完成一定功能的代码的集合,在python中是包和模块的形式。tensorflow是开�
没有合适的资源?快使用搜索试试~ 我知道了~
VGG.zip_VGG16 分类_VGG16迁移学习_vgg_分类问题_迁移学习 分类
共4个文件
py:4个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
1 下载量 7 浏览量
2022-09-23
04:43:56
上传
评论
收藏 11KB ZIP 举报
温馨提示
VGG16分类网络,可以用于分类问题,采用了迁移学习
资源详情
资源评论
资源推荐
收起资源包目录
VGG.zip (4个子文件)
VGG.py 5KB
input_data.py 11KB
tools.py 9KB
training_and_val.py 8KB
共 4 条
- 1
朱moyimi
- 粉丝: 61
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0