강의영상

- (1/6): 아나콘다 가상환경 만들기, 파이토치 설치, 주피터랩 설치, conda install 과 pip install 의 차이

- (2/6): 이미지 분석을 위한 데이터셋 준비 및 정리

- (3/6): 학습 및 예측

- (4/6): 코랩설명 + 깃허브/블로그 (뒷부분은 화면전환 오류로 설명이 부실함)

- (5/6): 코랩설명 + 깃허브/블로그

- (6/6): 우리강아지 이미지를 활용한 예측, 과제설명

import

from fastai.vision.all import *

데이터저장, 데이터로더스 생성후 dls로 저장

path=untar_data(URLs.PETS)/'images'
files=get_image_files(path) # 이미지파일들의 이름을 모두 복붙하여 리스트를 만든뒤에 files.txt로 저장하는 과정으로 비유할 수 있음
files[2] # txt 파일의 3번째 목록
Path('/home/cgb4/.fastai/data/oxford-iiit-pet/images/leonberger_5.jpg')
def label_func(f):
    if f[0].isupper():
        return 'cat' 
    else: 
        return 'dog' 
label_func('asdf')
'dog'
dls=ImageDataLoaders.from_name_func(path,files,label_func,item_tfms=Resize(224)) 
dls.show_batch(max_n=16)

학습

learn=cnn_learner(dls,resnet34,metrics=error_rate)
Downloading: "https://download.pytorch.org/models/resnet34-b627a593.pth" to C:\Users\cgb/.cache\torch\hub\checkpoints\resnet34-b627a593.pth
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_29964/2234347239.py in <module>
----> 1 learn=cnn_learner(dls,resnet34,metrics=error_rate)

~\anaconda3\envs\bda2021\lib\site-packages\fastai\vision\learner.py in cnn_learner(dls, arch, normalize, n_out, pretrained, config, loss_func, opt_func, lr, splitter, cbs, metrics, path, model_dir, wd, wd_bn_bias, train_bn, moms, **kwargs)
    177     if n_out is None: n_out = get_c(dls)
    178     assert n_out, "`n_out` is not defined, and could not be inferred from data, set `dls.c` or pass `n_out`"
--> 179     model = create_cnn_model(arch, n_out, pretrained=pretrained, **kwargs)
    180 
    181     splitter=ifnone(splitter, meta['split'])

~\anaconda3\envs\bda2021\lib\site-packages\fastai\vision\learner.py in create_cnn_model(arch, n_out, pretrained, cut, n_in, init, custom_head, concat_pool, **kwargs)
    141     "Create custom convnet architecture"
    142     meta = model_meta.get(arch, _default_meta)
--> 143     body = create_body(arch, n_in, pretrained, ifnone(cut, meta['cut']))
    144     if custom_head is None:
    145         nf = num_features_model(nn.Sequential(*body.children()))

~\anaconda3\envs\bda2021\lib\site-packages\fastai\vision\learner.py in create_body(arch, n_in, pretrained, cut)
     63 def create_body(arch, n_in=3, pretrained=True, cut=None):
     64     "Cut off the body of a typically pretrained `arch` as determined by `cut`"
---> 65     model = arch(pretrained=pretrained)
     66     _update_first_layer(model, n_in, pretrained)
     67     #cut = ifnone(cut, cnn_config(arch)['cut'])

~\anaconda3\envs\bda2021\lib\site-packages\torchvision\models\resnet.py in resnet34(pretrained, progress, **kwargs)
    286         progress (bool): If True, displays a progress bar of the download to stderr
    287     """
--> 288     return _resnet('resnet34', BasicBlock, [3, 4, 6, 3], pretrained, progress,
    289                    **kwargs)
    290 

~\anaconda3\envs\bda2021\lib\site-packages\torchvision\models\resnet.py in _resnet(arch, block, layers, pretrained, progress, **kwargs)
    260     model = ResNet(block, layers, **kwargs)
    261     if pretrained:
--> 262         state_dict = load_state_dict_from_url(model_urls[arch],
    263                                               progress=progress)
    264         model.load_state_dict(state_dict)

~\anaconda3\envs\bda2021\lib\site-packages\torch\hub.py in load_state_dict_from_url(url, model_dir, map_location, progress, check_hash, file_name)
    551             r = HASH_REGEX.search(filename)  # r is Optional[Match[str]]
    552             hash_prefix = r.group(1) if r else None
--> 553         download_url_to_file(url, cached_file, hash_prefix, progress=progress)
    554 
    555     if _is_legacy_zip_format(cached_file):

~\anaconda3\envs\bda2021\lib\site-packages\torch\hub.py in download_url_to_file(url, dst, hash_prefix, progress)
    436         if hash_prefix is not None:
    437             sha256 = hashlib.sha256()
--> 438         with tqdm(total=file_size, disable=not progress,
    439                   unit='B', unit_scale=True, unit_divisor=1024) as pbar:
    440             while True:

~\anaconda3\envs\bda2021\lib\site-packages\tqdm\notebook.py in __init__(self, *args, **kwargs)
    240         unit_scale = 1 if self.unit_scale is True else self.unit_scale or 1
    241         total = self.total * unit_scale if self.total else self.total
--> 242         self.container = self.status_printer(self.fp, total, self.desc, self.ncols)
    243         self.container.pbar = proxy(self)
    244         self.displayed = False

~\anaconda3\envs\bda2021\lib\site-packages\tqdm\notebook.py in status_printer(_, total, desc, ncols)
    113         # Prepare IPython progress bar
    114         if IProgress is None:  # #187 #451 #558 #872
--> 115             raise ImportError(
    116                 "IProgress not found. Please update jupyter and ipywidgets."
    117                 " See https://ipywidgets.readthedocs.io/en/stable"

ImportError: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html

- 에러를 해결하기 위해서는 아래를 설치하면 된다.

!conda install -c conda-forge jupyterlab_widgets -y 
!conda install -c conda-forge ipywidgets -y 
!conda install -c conda-forge nodejs -y

- 위를 설치하고 커널을 재시작하면 정상적으로 모형이 만들어진다.

learn=cnn_learner(dls,resnet34,metrics=error_rate)
/home/cgb4/anaconda3/envs/bda2021/lib/python3.8/site-packages/torch/nn/functional.py:718: UserWarning: Named tensors and all their associated APIs are an experimental feature and subject to change. Please do not use them for anything important until they are released as stable. (Triggered internally at  /tmp/pip-req-build-pma2oi4d/c10/core/TensorImpl.h:1156.)
  return torch.max_pool2d(input, kernel_size, stride, padding, dilation, ceil_mode)
learn.fine_tune(1)
epoch train_loss valid_loss error_rate time
0 0.148441 0.018828 0.006766 00:12
epoch train_loss valid_loss error_rate time
0 0.040593 0.014769 0.002706 00:11

예측

learn.predict(files[0])
('dog', tensor(1), tensor([6.1421e-07, 1.0000e+00]))
learn.show_results()

오답분석

interp = Interpretation.from_learner(learn)
interp.plot_top_losses(16)

진짜 잘되는게 맞는건가?

PILImage.create('2021-09-06-cat1.png')
learn.predict(PILImage.create('2021-09-06-cat1.png'))
('cat', tensor(0), tensor([1.0000e+00, 1.7844e-07]))

- 헷갈리는 고양이 사진인데 잘 구분한다.

PILImage.create('2021-09-06-cat2.jpeg')
learn.predict(PILImage.create('2021-09-06-cat2.jpeg'))
('cat', tensor(0), tensor([9.9984e-01, 1.6283e-04]))
PILImage.create('2021-09-06-hani01.jpeg')
learn.predict(PILImage.create('2021-09-06-hani01.jpeg'))
('dog', tensor(1), tensor([5.0984e-04, 9.9949e-01]))
PILImage.create('2021-09-06-hani02.jpeg')
learn.predict(PILImage.create('2021-09-06-hani02.jpeg'))
('dog', tensor(1), tensor([7.1694e-06, 9.9999e-01]))
PILImage.create('2021-09-06-hani03.jpg')
learn.predict(PILImage.create('2021-09-06-hani03.jpg'))
('dog', tensor(1), tensor([4.5399e-04, 9.9955e-01]))

다음시간

  • 이미지 크롤링 --> 데이터셋트 --> A,B 구분하는 모델

숙제

  • 위의 사진들 이외에 사진들을 바탕으로 예측을 하는 모형구축.
  • 예측결과를 스샷으로 저장하여 제출 (이미지도 함께 스샷할것)

숙제참고자료

import PIL 
urls='https://t1.daumcdn.net/cfile/tistory/9925F03C5AD486B033'
urllib.request.urlretrieve(urls,'temp.png')
learn.predict(PILImage.create('temp.png'))
('dog', tensor(1), tensor([6.6117e-04, 9.9934e-01]))