作业说明:CNN分类问题
根据实物照片进行分类,照片的命名规则是“类别_编号.jpg”,类别也是用数字表示,共有11类,Bread, Dairy product, Dessert, Egg, Fried food, Meat, Noodles/Pasta, Rice, Seafood, Soup, and Vegetable/Fruit;其实具体类别是什么我们可以不用管,只需要训练的时候输入数据,输出类别即可。
training 以及 validation 中的照片名名称格式為“类别_编号.jpg”,例如 3_100.jpg 即为类别 3 的照片(编号不重要)testing 中的照片名稱格式為 [编号].jpg
数据集需要提前下载下来,解压即可,范例中是从谷歌云下载的,需要翻墙,比较麻烦
参考:李宏毅机器学习第三次作业源码
导入需要的包
cv2需要额外安装,直接输入命令pip install opencv-python即可完成
1 | # Import需要的套件 |
读入数据
1 | #读取文件的函数 |
1 | workspace_dir = './food-11' |
Reading data
Size of training data = 9866
Size of validation data = 3430
Size of Testing data = 3347
数据集
在 Pytorch 中,我們可以利用 torch.utils.data 的 Dataset 及 DataLoader 來”包裝” data,使後續的 training 及 testing 更為方便
1 | #training时需要做数据增强 |
分批次训练,可以加快训练的速度
1 | batch_size = 128 |
模型构建
BatchNorm2d对数据进行归一化处理
激活函数为ReLu
1 | class Classifier(nn.Module): |
训练模型
使用trainingset训练,使用validationset寻找好的的参数
1 | model = Classifier().cuda() |
[001/030] 14.86 sec(s) Train Acc: 0.227549 Loss: 0.018395 | Val Acc: 0.243440 loss: 0.016740
[002/030] 14.73 sec(s) Train Acc: 0.340158 Loss: 0.014800 | Val Acc: 0.334111 loss: 0.016127
[003/030] 14.75 sec(s) Train Acc: 0.381512 Loss: 0.013810 | Val Acc: 0.313994 loss: 0.015460
[004/030] 14.77 sec(s) Train Acc: 0.431482 Loss: 0.012794 | Val Acc: 0.317493 loss: 0.015771
[005/030] 14.83 sec(s) Train Acc: 0.467160 Loss: 0.012125 | Val Acc: 0.441691 loss: 0.013532
[006/030] 14.82 sec(s) Train Acc: 0.501824 Loss: 0.011313 | Val Acc: 0.190962 loss: 0.029743
[007/030] 14.82 sec(s) Train Acc: 0.530002 Loss: 0.010675 | Val Acc: 0.351603 loss: 0.018797
[008/030] 14.82 sec(s) Train Acc: 0.550882 Loss: 0.010232 | Val Acc: 0.527697 loss: 0.010849
[009/030] 14.80 sec(s) Train Acc: 0.573383 Loss: 0.009685 | Val Acc: 0.407872 loss: 0.014792
[010/030] 14.84 sec(s) Train Acc: 0.582911 Loss: 0.009314 | Val Acc: 0.359767 loss: 0.019833
[011/030] 14.86 sec(s) Train Acc: 0.617981 Loss: 0.008696 | Val Acc: 0.557434 loss: 0.011074
[012/030] 14.84 sec(s) Train Acc: 0.625481 Loss: 0.008536 | Val Acc: 0.522449 loss: 0.011264
[013/030] 14.86 sec(s) Train Acc: 0.657004 Loss: 0.007838 | Val Acc: 0.580175 loss: 0.010005
[014/030] 14.80 sec(s) Train Acc: 0.674133 Loss: 0.007390 | Val Acc: 0.582799 loss: 0.009706
[015/030] 14.86 sec(s) Train Acc: 0.686600 Loss: 0.007146 | Val Acc: 0.541108 loss: 0.011018
[016/030] 14.83 sec(s) Train Acc: 0.696331 Loss: 0.006872 | Val Acc: 0.569971 loss: 0.010966
[017/030] 14.88 sec(s) Train Acc: 0.716197 Loss: 0.006412 | Val Acc: 0.595335 loss: 0.010484
[018/030] 14.88 sec(s) Train Acc: 0.719136 Loss: 0.006439 | Val Acc: 0.632945 loss: 0.009088
[019/030] 14.94 sec(s) Train Acc: 0.727042 Loss: 0.006230 | Val Acc: 0.532653 loss: 0.012839
[020/030] 14.90 sec(s) Train Acc: 0.736063 Loss: 0.005996 | Val Acc: 0.605831 loss: 0.010434
[021/030] 14.85 sec(s) Train Acc: 0.757754 Loss: 0.005454 | Val Acc: 0.578717 loss: 0.011531
[022/030] 14.88 sec(s) Train Acc: 0.766471 Loss: 0.005285 | Val Acc: 0.632070 loss: 0.009517
[023/030] 14.88 sec(s) Train Acc: 0.784715 Loss: 0.004788 | Val Acc: 0.662391 loss: 0.008359
[024/030] 14.83 sec(s) Train Acc: 0.804987 Loss: 0.004413 | Val Acc: 0.550729 loss: 0.013303
[025/030] 14.85 sec(s) Train Acc: 0.813602 Loss: 0.004159 | Val Acc: 0.634985 loss: 0.010451
[026/030] 14.85 sec(s) Train Acc: 0.821711 Loss: 0.004053 | Val Acc: 0.467055 loss: 0.017780
[027/030] 14.93 sec(s) Train Acc: 0.828198 Loss: 0.003934 | Val Acc: 0.579592 loss: 0.013445
[028/030] 14.85 sec(s) Train Acc: 0.823535 Loss: 0.004012 | Val Acc: 0.616910 loss: 0.011728
[029/030] 14.90 sec(s) Train Acc: 0.832455 Loss: 0.003813 | Val Acc: 0.628863 loss: 0.010894
[030/030] 14.83 sec(s) Train Acc: 0.837726 Loss: 0.003696 | Val Acc: 0.679300 loss: 0.009366
1 | train_val_x = np.concatenate((train_x, val_x), axis=0) |
1 | model_best = Classifier().cuda() |
[001/030] 16.94 sec(s) Train Acc: 0.253234 Loss: 0.016887
[002/030] 16.98 sec(s) Train Acc: 0.377708 Loss: 0.013877
[003/030] 16.97 sec(s) Train Acc: 0.443442 Loss: 0.012409
[004/030] 17.08 sec(s) Train Acc: 0.496916 Loss: 0.011322
[005/030] 17.02 sec(s) Train Acc: 0.539486 Loss: 0.010247
[006/030] 16.99 sec(s) Train Acc: 0.578896 Loss: 0.009436
[007/030] 17.08 sec(s) Train Acc: 0.608303 Loss: 0.008864
[008/030] 17.06 sec(s) Train Acc: 0.630039 Loss: 0.008328
[009/030] 17.02 sec(s) Train Acc: 0.656965 Loss: 0.007704
[010/030] 17.14 sec(s) Train Acc: 0.677873 Loss: 0.007283
[011/030] 17.18 sec(s) Train Acc: 0.699158 Loss: 0.006727
[012/030] 17.19 sec(s) Train Acc: 0.717735 Loss: 0.006340
[013/030] 17.19 sec(s) Train Acc: 0.745262 Loss: 0.005799
[014/030] 17.17 sec(s) Train Acc: 0.758574 Loss: 0.005436
[015/030] 17.14 sec(s) Train Acc: 0.765569 Loss: 0.005278
[016/030] 17.09 sec(s) Train Acc: 0.777828 Loss: 0.004947
[017/030] 17.18 sec(s) Train Acc: 0.795427 Loss: 0.004533
[018/030] 17.12 sec(s) Train Acc: 0.799188 Loss: 0.004513
[019/030] 17.12 sec(s) Train Acc: 0.825887 Loss: 0.003908
[020/030] 17.22 sec(s) Train Acc: 0.831980 Loss: 0.003822
[021/030] 17.12 sec(s) Train Acc: 0.840177 Loss: 0.003572
[022/030] 17.10 sec(s) Train Acc: 0.859582 Loss: 0.003178
[023/030] 17.21 sec(s) Train Acc: 0.868758 Loss: 0.002964
[024/030] 17.14 sec(s) Train Acc: 0.875150 Loss: 0.002801
[025/030] 17.16 sec(s) Train Acc: 0.888613 Loss: 0.002458
[026/030] 17.10 sec(s) Train Acc: 0.892449 Loss: 0.002349
[027/030] 17.05 sec(s) Train Acc: 0.905836 Loss: 0.002034
[028/030] 17.08 sec(s) Train Acc: 0.917043 Loss: 0.001871
[029/030] 17.18 sec(s) Train Acc: 0.914786 Loss: 0.001894
[030/030] 17.16 sec(s) Train Acc: 0.916291 Loss: 0.001863
测试
1 | test_set = ImgDataset(test_x, transform=test_transform) |
1 | model_best.eval() |
1 | with open("predict.csv", 'w') as f: |
1 |