# This code is only here to check that your internet is enabled. It doesn't do anything else.
# Here's a help thread on getting your phone number verified: https://www.kaggle.com/product-feedback/135367
import socket,warnings
try:
socket.setdefaulttimeout(1)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect(('1.1.1.1', 53))
except socket.error as ex: raise Exception("STOP: No internet. Click '>|' in top right and set 'Internet' switch to on")
clothe_types = 'shirt', 'pants', 'dress', 'socks'
path = Path('clothes')
if not path.exists():
path.mkdir()
for o in clothe_types:
dest = (path/o)
dest.mkdir(exist_ok = True)
results = search_images_ddg(f'{o}')
download_images(dest, urls=results)
fns = get_image_files(path)
fns
failed = verify_images(fns)
failed
failed.map(Path.unlink)
clothe = DataBlock(
blocks = (ImageBlock, CategoryBlock),
get_items = get_image_files,
splitter = RandomSplitter(valid_pct = 0.2, seed = 0),
get_y = parent_label,
item_tfms = Resize(128))
clothe = clothe.new(
item_tfms = RandomResizedCrop(224, min_scale = 0.5),
batch_tfms = aug_transforms())
dls = clothe.dataloaders(path)
Apply a fastai vision model to the data
See article by Jeremy Howard about which model is best for image classification
List of Pytorch image models
%%capture
!pip install timm
import timm
timm.list_models('convnext*')
Select a model to use for training
learn = vision_learner(dls, resnet34, metrics = error_rate)
learn.fine_tune(4)
interp = ClassificationInterpretation.from_learner(learn)
interp.plot_confusion_matrix()
interp.plot_top_losses(10, nrows = 2, figsize = (17,4))
cleaner = ImageClassifierCleaner(learn)
cleaner
for idx in cleaner.delete(): cleaner.fns[idx].unlink()
for idx, cat in cleaner.change(): shutil.move(str(cleaner.fns[idx]), path/cat)
!sudo apt-get install git-lfs
!git lfs install
from huggingface_hub import notebook_login
notebook_login()
from huggingface_hub import push_to_hub_fastai
repo_id = "tompham97/clothe-classifier"
push_to_hub_fastai(learner = learn, repo_id = repo_id)