数据集
百度云链接:
https://pan.baidu.com/s/1V9nAAWsDrQG7P9KLd1SGyA
提取码:asdf
训练好的模型免费地址
csdn
cnn垃圾分类的神经网络模型-机器学习文档类资源-CSDN下载
github
内容为对测试集中图片数据进行分类,测试文件夹中有5113张图片
数据类别一共40类
每行输出id号(第一列数据)+’\t’+数字类别(不要改变测试集顺序)
示例如下:
fimg_4016.jpg hazardous_waste_dry_battery
fimg_4018.jpg hazardous_waste_dry_battery
fimg_4019.jpg hazardous_waste_dry_battery
conda install pytorch torchvision cpuonly -c pytorch
!pip install torch==1.1.0 torchvision==0.3.0 -f https://download.pytorch.org/whl/torch_stable.html
!pip install pylint
!pip install glob2
!pip install pyyaml
!pip install future
!pip install fastprogress
!pip install ipywidgets #这些是其他可能用到的库
#导入库 由于是在平台上跑的,有一些自带的库,丘比特上面要自行安装
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
import torchvision.datasets as dset
import torchvision.transforms as transforms
import torch.optim as optim
import torchvision.models as models
import PIL.Image as Image
import os
from PIL import Image
import torch
import torchvision.transforms as transforms
image_size = (224,224) #准备对图像转化为tensor形式
data_transform=transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.Resize(image_size),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
])
最重要的一步,下载训练好的模型到指定地点,这里没改名字直接用猫狗的传到github上了,可以直接下载使用
!wget -O /ilab/model1.pkl https://github.com/longsongline/voice-svm/raw/main/catvsdog.pkl
model = torch.load('/ilab/model1.pkl') #加载模型
model.eval() #模型唤醒
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
四十类垃圾类型,最后做返回处理
waste_types = ['hazardous_waste_dry_battery',
'hazardous_waste_expired_drugs',
'hazardous_waste_ointment',
'kitchen_waste_bone',
'kitchen_waste_eggshell',
'kitchen_waste_fish_bone',
'kitchen_waste_fruit_peel',
'kitchen_waste_meal',
'kitchen_waste_pulp',
'kitchen_waste_tea',
'kitchen_waste_vegetable',
'other_garbage_bamboo_chopsticks',
'other_garbage_cigarette',
'other_garbage_fast_food_box',
'other_garbage_flowerpot',
'other_garbage_soiled_plastic',
'other_garbage_toothpick',
'recyclables_anvil',
'recyclables_bag',
'recyclables_bottle',
'recyclables_can',
'recyclables_cardboard',
'recyclables_cosmetic_bottles',
'recyclables_drink_bottle',
'recyclables_edible_oil_barrel',
'recyclables_glass_cup',
'recyclables_metal_food_cans',
'recyclables_old_clothes',
'recyclables_paper_bags',
'recyclables_pillow',
'recyclables_plastic_bowl',
'recyclables_plastic_hanger',
'recyclables_plug_wire',
'recyclables_plush_toys',
'recyclables_pot',
'recyclables_powerbank',
'recyclables_seasoning_bottle',
'recyclables_shampoo_bottle',
'recyclables_shoes',
'recyclables_toys']
waste_types[2]
#!ls /ilab/datasets/local/lajifenlei/test
import pandas as pd
import os
import csv
# 将id读取
path_test = '/ilab/datasets/local/lajifenlei/test'
# 训练出来垃圾类别
dirs = os.listdir(path_test)
p = []
a = 1
pa = []
for img in range(0,len(dirs),2): #这个可能比较难理解,因为我最后他不能单个预测,所以我两 ## 两一组,形成[2,3,224,224]的可预测shape
if(img%10 == 0):
print(img)
img_pil = Image.open('/ilab/datasets/local/lajifenlei/test/'+ dirs[img]).convert('RGB') # 0~255
img_pil1 = Image.open('/ilab/datasets/local/lajifenlei/test/'+ dirs[img+1]).convert('RGB')
img_tensor = data_transform(img_pil)
img_tensor1 = data_transform(img_pil1)
ha = torch.stack((img_tensor, img_tensor1), 0)
out = model(ha)
pa.append(dirs[img] + '\t' + waste_types[int(out.argmax(dim=1)[0])])
pa.append(dirs[img+1] + '\t' + waste_types[int(out.argmax(dim=1)[1])])
# 输出submission文件
print(pa)
最后直接预测成功
事实上由于已经训练好了,这个实验不需要trian
test的数据你们可以自行从train里选取建立
或者看我的另一篇完整搭建模型训练的文章
预测结果pa
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/73590.html