在10行代码中使用Fast.AI细粒度服装分类
服装类别分类被认为是一种细粒度的图像分类任务,因为,大多数服装看起来非常相似。
数据集使用DeepFashion数据库
from fastai import *
from fastai.vision import *
path = Path("data/cloth_categories/")
data = ImageDataBunch.from_csv(path, csv_labels="train_labels.csv", ds_tfms=get_transforms(), size=224)
data.normalize(imagenet_stats)
learn = create_cnn(data, models.resnet34, metrics=accuracy)
learn.fit_one_cycle(8)
learn.save('stage-1_sz-150')
获取数据集:
http://mmlab.ie.cuhk.edu.hk/projects/DeepFashion/AttributePrediction.html
在此页面上,转到Google云端硬盘或百度硬盘链接,然后下载:
衣服图片(2.8 GB Zip文件)
类别注释(文本文件)
train/ Val /test(文本文件)
#好吧,让我们开始编码......
from fastai import *
from fastai.vision import *
数据导入
# Creating path object
path = Path("data/cloth_categories/")
# Creating data from CSV
data = ImageDataBunch.from_csv(path, csv_labels="labels.csv" ,ds_tfms=get_transforms(), size=150)
#Normalize dataset and augmentated image tranformation
data.normalize(imagenet_stats)
数据可视化
# show 8*8 grid of random images from our dataset
data.show_batch(rows=8, figsize=(14,12))
查看目标数据类
print(data.classes)
print (len(data.classes),data.c)
>>> ['Blouse', 'Blazer', 'Button-Down', 'Bomber', 'Anorak', 'Tee', 'Tank', 'Top', 'Sweater', 'Flannel', 'Hoodie', 'Cardigan', 'Jacket', 'Henley', 'Poncho', 'Jersey', 'Turtleneck', 'Parka', 'Peacoat', 'Halter', 'Skirt', 'Shorts', 'Jeans', 'Joggers', 'Sweatpants', 'Jeggings', 'Cutoffs', 'Sweatshorts', 'Leggings', 'Culottes', 'Chinos', 'Trunks', 'Sarong', 'Gauchos', 'Jodhpurs', 'Capris', 'Dress', 'Romper', 'Coat', 'Kimono', 'Jumpsuit', 'Robe', 'Caftan', 'Kaftan', 'Coverup', 'Onesie']
(46, 46)
最后使用Resnet-34进行训练
# create fast.ai vision Conv learner aka CNN from resnet34 architecture
# Please note, we are using metrics as top-1 Accuracy
learn = create_cnn(data, models.resnet34, metrics=accuracy)
# fit learner on data in 8 cycle
learn.fit_one_cycle(8)
# save our fitted model
learn.save('stage-1_arch-34_sz-150')
>>>
Total time: 49:55
epoch train loss valid loss accuracy
1 0.632115 0.474118 0.761742 (06:13)
2 0.548713 0.408220 0.783295 (06:12)
3 0.412822 0.369032 0.826267 (06:13)
4 0.350622 0.345870 0.841440 (06:14)
5 0.345819 0.308995 0.852246 (06:16)
6 0.261442 0.289868 0.877319 (06:16)
7 0.270456 0.276496 0.891509 (06:16)
8 0.264946 0.272343 0.921870 (06:12)
网友评论