[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
1.数据集
pytorch有个快速构造数据集的方法
但是你的目录结构必须是
image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir,
x),data_transforms[x])for x in ['train', 'val']}
首先for循环将['train',val]分别赋给x,
x =train/val。 os.path.join(data_dir,x)将文件dataset和train和val拼接起来。
hymenoptera_data/train 和hymenoptera_data/val
然后data_transforms根据组装的数据预处理来处理文件。
3.生成image_datasets对象,根据类别访问 image_datasets是个list
4.组装数据
batch_size=4,一次取4个图片
shuffle=True, 将数据打乱,随机选取
num_workers=4 ,使用4个线程
4.dataloaders[]字典会同时返回 每张图片的张量数据和标签
5.返回数据集中trian和val的数据长度
6.返回分类标签
7.迭代数据集
8.更具索引返回分类名称
其中0代表ants,1代表bees
9.预测输出结果
outputs=model(inputs)会输出最后的分类概率,比如有7个分类就会输出4个1x7的向量
_,preds=torch.max(outputs,1),前面输出的是最大概率的值,后面preds表示最大概率的索引,
如果是批量大小是4,那么preds就是一个1x4的向量,其中的值代表索引,label也是一个1x4的向量,值代表类别得索引。
10.统计交叉熵损失和准确率
损失值要乘以批量得大小,最后除以分类得数据长度,得到平均损失率
如果preds=labels.data就返回true,否则返回false。统计true得值用sum计数。最后除以train数据集得长度,分别得到trian和val得准确率。