Touch to PyTorch ISL Lab Seminar Hansol Kang : From basic to vanilla GAN
Contents October 9, 2018 Setup Install Development Tools Example What is PyTorch? PyTorch Deep Learning Framework Tensor Datasets Neural Nets Learning Applications
What is PyTorch? October 9, 2018 2 an open source machine learning library for Python. It is primarily developed by Facebook's artificial-intelligence research group. • PyTorch Vs.
What is PyTorch? October 9, 2018 3 Written in : Python Interface : PythonNov. 2010 Written in : C++ Interface : Python, MATLAB, C++Dec. 2013 Written in : C, Lua Interface : C, LuaJul. 2014 Written in : Python Interface : Python, RMar. 2015 Written in : C++, Python, CUDA Interface : Python, C/C++, Java, Go, R, JuliaNov. 2015 Written in : Interface : Python, C++Apr. 2017 Written in : Python, C, CUDA Interface : PythonOct. 2016 ** DL4J(Java) Chainer(Python) MXNet(C++, Python, Julia, MATLAB, JavaScript, Go, R, Scala, Perl) CNTK(Python, C++), TF Learn(Python) TF-Slim(Python) Etc. Recommend to choose these framework • Deep Learning Framework “What is this? Gum? It's GAN.”, pp. 21-22 Cf.
Setup October 9, 2018 4 • Install https://www.anaconda.com/download/
Setup October 9, 2018 5 • Install https://pytorch.org/ C++ : Preview, Linux only
Setup October 9, 2018 6 • Install conda create -n PyTorch python=3.6 activate PyTorch conda install pytorch cuda90 -c pytorch pip install torchvision o conda create는 환경 생성하는 명령어. PyTorch 뿐만 아니라 Tensorflow 같은 다른 딥러닝 프레임워크를 사용한다거나 하위 파이썬 버전도 사용해야하는 경우 환경마다 설정해주면, 디펜던시가 꼬이지 않음. o -n 환경명, python=파이썬버전 입력. 환경설정 리스트는 conda env list를 입력하면 확인 가능. o activate는 해당 환경을 활성화 시키는 명령어. 반대로 환경을 빠져나오는 명령어는 deactivate. o PyTorch를 설치하는 명령어는 conda install pytorch cuda90 -c pytorch.. o torchvision은 딥러닝 학습에 많이 사용되는 데이터셋, 네트워크 구조, 이미지 변환과 같은 기능을 제공하므로 설치하는 것을 권장.
Setup October 9, 2018 7 • Development Tools : Cell based execution : Intellisense : Intellisense : Management of python env. : GitHub : AI tool package : Startup file : Intellisense : Cell based execution : Management of python env. : Extension program : Insane extension program : Intellisense : Environment setting
Setup October 9, 2018 8 • Development Tools Personal opinion : >
Setup October 9, 2018 9 • Development Tools – Visual Studio File-New-Project
Setup October 9, 2018 10 • Development Tools – Visual Studio File-New-Project Python 도구 설치
Setup October 9, 2018 11 • Development Tools – Visual Studio File-New-Project Python 도구 설치 Python Application
Setup October 9, 2018 12 • Development Tools – Visual Studio File-New-Project Python 도구 설치 Python Application Python 환경 추가
Setup October 9, 2018 13 • Development Tools – Visual Studio File-New-Project Python 도구 설치 Python Application Python 환경 추가 코드 작성
Setup October 9, 2018 14 • Development Tools – Visual Studio File-New-Project Python 도구 설치 Python Application Python 환경 추가 코드 작성
Setup October 9, 2018 15 • Development Tools – Visual Studio File-New-Project Python 도구 설치 Python Application Python 환경 추가 코드 작성 실행 : 시작 파일로 설정-> Ctrl+F5
Setup October 9, 2018 16 • Development Tools – PyCharm
Setup October 9, 2018 17 • Development Tools – PyCharm
Setup October 9, 2018 18 • Development Tools – PyCharm
Setup October 9, 2018 19 • Development Tools – PyCharm
Setup October 9, 2018 20 • Development Tools – PyCharm
Setup October 9, 2018 21 • Development Tools – PyCharm File-New Project
Setup October 9, 2018 22 • Development Tools – PyCharm File-New Project 코드 작성
Setup October 9, 2018 23 • Development Tools – PyCharm File-New Project 코드 작성 실행 : Ctrl+F5 “Too easy, but important things”, pp. 42-43 Cf. TODO
Example October 9, 2018 24 • Tensor array Tensor reshape view(reshape) linspace linspace ones, zeros ones, zeros random.randn, random.rand randn, rand from_numpy numpy to, cuda
Example October 9, 2018 25 • Tensor import torch as tc #Tensor a = tc.Tensor([2.5, 4]) b = tc.Tensor([[1, 2.5], [2.5, 6]]) #reshape c = b.reshape(1, 4) d = b.reshape(1, -1) Tensor a : tensor([ 2.5000, 4.0000]) Size of a : torch.Size([2]) Tensor b : tensor([[ 1.0000, 2.5000], [ 2.5000, 6.0000]]) Size of b : torch.Size([2, 2]) Tensor c : tensor([[ 1.0000, 2.5000, 2.5000, 6.0000]]) Size of c : torch.Size([1, 4]) Tensor d : tensor([[ 1.0000, 2.5000, 2.5000, 6.0000]]) Size of d : torch.Size([1, 4])
Example October 9, 2018 26 • Tensor Start End 0 #ones, zeros real = tc.ones(2, 2) fake = tc.zeros(2, 2) 1 #linspace x = tc.linspace(-1, 1, 9) Tensor x : tensor([-1.0000, -0.7500, -0.5000, -0.2500, 0.0000, 0.2500, 0.5000, 0.7500, 1.0000]) Size of x : torch.Size([9]) Tensor real : tensor([[ 1., 1.], [ 1., 1.]]) Size of real : torch.Size([2, 2]) Tensor fake : tensor([[ 0., 0.], [ 0., 0.]]) Size of fake : torch.Size([2, 2])
Example October 9, 2018 27 • Tensor 0 1 0 1 #randn, rand z1 = tc.randn(2, 3) z2 = tc.rand(2, 3) Tensor z1 : tensor([[-0.0750, -1.0555, -0.0706], [-0.1946, -0.8593, -0.2238]]) Size of z1 : torch.Size([2, 3]) Tensor z2 : tensor([[ 0.8204, 0.3505, 0.1034], [ 0.5318, 0.9543, 0.8781]]) Size of z2 : torch.Size([2, 3]) #to, cuda z_cuda = z1.cuda() device = tc.device('cuda' if tc.cuda.is_available() else 'cpu') z_device = z1.to(device) Tensor z_cuda : tensor([[-0.0750, -1.0555, -0.0706], [-0.1946, -0.8593, -0.2238]], device='cuda:0') Tensor z_device : tensor([[-0.0750, -1.0555, -0.0706], [-0.1946, -0.8593, -0.2238]], device='cuda:0')
Example October 9, 2018 28 • Tensor #from_numpy, numpy import numpy as np a = np.array([3.5, 4]) b = tc.from_numpy(a) c = b.numpy() array a : [3.5 4. ] Tensor b : tensor([ 3.5000, 4.0000], dtype=torch.float64) array c : [3.5 4. ]
Example October 9, 2018 29 • Datasets The following datasets are available: MNIST Fashion-MNIST EMNIST COCO LSUN ImageFolder DatasetFolder Imagenet-12 CIFAR STL10 SVHN PhotoTour : 바로 사용 가능. : 추가적인 과정 필요. : Custom dataset에 활용. MNIST Fashion-MNIST CIFAR LSUN CelebA K-pop(Custom)
Example October 9, 2018 30 • Datasets – MNIST trans  Compose()is used when there are multiple transform options. Here, ToTensor() and Normalize(mean, std) are used.  ToTensor () changes the PIL Image to a tensor. torchvision dataset The default type is PIL Image.  Normalize (mean, std) transforms the range of the image. Here, the value of [0, 1] is adjusted to [-1, 1]. ((value-mean) / std) trans = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) datasets = tv.datasets.MNIST(root='./MNIST', train=True, download=True, transform=trans) dataloader = tc.utils.data.DataLoader(datasets=datasets, batch_size=100, shuffle=True) import torch as tc import torchvision as tv import torchvision.transforms as transforms
Example October 9, 2018 31 • Datasets – MNIST dataset  root : This is the path to store (MNIST data). Folders are automatically created with the specified name.  train : Set the data to be used for the train.  transform : Transform the data according to the transform option set previously.  download : Download (MINST data). (If you downloaded it once, it will not do it again.) trans = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) datasets = tv.datasets.MNIST(root='./MNIST', train=True, download=True, transform=trans) dataloader = tc.utils.data.DataLoader(datasets=datasets, batch_size=100, shuffle=True) import torch as tc import torchvision as tv import torchvision.transforms as transforms
Example October 9, 2018 32 • Datasets – MNIST dataloader  dataset : Set the dataset to load.  batch_size : Set the batch size.  shuffle : Shuffle the data and load it. trans = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) datasets = tv.datasets.MNIST(root='./MNIST', train=True, download=True, transform=trans) dataloader = tc.utils.data.DataLoader(datasets=datasets, batch_size=100, shuffle=True) import torch as tc import torchvision as tv import torchvision.transforms as transforms
Example October 9, 2018 33 • Datasets – LSUN https://github.com/fyu/lsun
Example October 9, 2018 34 • Datasets – LSUN Run download.py def list_categories(tag): url = 'http://lsun.cs.princeton.edu/htbin/list.cgi?tag=' + tag f = urlopen(url) return json.loads(f.read()) If you are using Python 3.0 or later, modify the code from urllib2.urlopen (url) to urlopen (url). download.py -c bedroom bedroom 0 bridge 1 church_outdoor 2 classroom 3 conference_room 4 dining_room 5 kitchen 6 living_room 7 restaurant 8 tower 9
Example October 9, 2018 35 • Datasets – LSUN trans = transforms.Compose([transforms.Resize((64,64)) ,transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) datasets = tv.datasets.LSUN('.', classes=['bedroom_train'], transform=trans) dataloader = tc.utils.data.DataLoader(datasets=datasets, batch_size=100, shuffle=True) trans  Resize() is used to resize the image. datasets  root : Root directory for the database files.  classes : One of {‘train’, ‘val’, ‘test’} or a list of categories to load. e,g. [‘bedroom_train’, ‘church_train’].
Example October 9, 2018 36 • Datasets – ImageFolder(CelebA) http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html
Example October 9, 2018 37 • Datasets – ImageFolder(CelebA) http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html 압축을 풀면 jpg 형태로 파일들이 저장된 것을 확인할 수 있음.
Example October 9, 2018 38 • Datasets – ImageFolder(CelebA) trans = transforms.Compose([transforms.Resize((64,64)) ,transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) datasets = tv.datasets.ImageFolder('./img_align_celeba', trans) dataloader = tc.utils.data.DataLoader(datasets=datasets, batch_size=100, shuffle=True) datasets  root : Root directory for the database files.  transform : Transform the data according to the transform option set previously.
H Example October 9, 2018 39 • Neural Nets Sequence baseClass base model = tc.nn.Sequential( tc.nn.Linear(D_in, H), tc.nn.ReLU(), tc.nn.Linear(H, D_out), tc.nn.Sigmoid() ) class Model(tc.nn.Module): def __init__(self): super(Model, self).__init__() self.linear1 = tc.nn.Linear(D_in, H), self.linear2 = tc.nn.Linear(H, D_out) def forward(self, input): x = tc.nn.functional.relu(self.linear1(input)) x = tc.nn.functional.sigmoid(self.linear2(x)) return x D_in D_out Data manipulation
Example October 9, 2018 40 • Neural Nets Class base class Model(tc.nn.Module): def __init__(self): super(Model, self).__init__() self.linear1 = tc.nn.Linear(D_in, H), self.linear2 = tc.nn.Linear(H, D_out) def forward(self, input): x = tc.nn.functional.relu(self.linear1(input)) x = tc.nn.functional.sigmoid(self.linear2(x)) return x tc.nn.Module functions Model functions 생성자 부모 클래스 초기화 Propagation Cf. Diamond problem A B C D Class D __init__() Class B __init__() Class A __init__() Class C __init__() Class A __init__()
Example October 9, 2018 41 • Learning Back Propagation (Learning) Loss Optimizer loss_func = tc.nn.MSELoss() opt = tc.optim.Adam(model.parameters(), lr=0.01 ) for ep in range(epoch_sz): for step, (images, labels) in enumerate(dataloader): opt.zero_grad() images = images.to(device) labels = labels.to(device) output = model(images) loss = loss_func(output) loss.backward() opt.step()
Example October 9, 2018 42 • Learning loss_func = tc.nn.MSELoss() opt = tc.optim.Adam(model.parameters(), lr=0.01 ) for ep in range(epoch_sz): for step, (images, labels) in enumerate(dataloader): opt.zero_grad() images = images.to(device) labels = labels.to(device) output = model(images) loss = loss_func(output) loss.backward() opt.step() Loss 선언 Optimizer 선언 (업데이트 하려는 parameter) 정해진 epoch 만큼 수행 앞서 설정한 dataloader에 따라 image(data)와 label(_) 불러옴 Gradient 초기화. Loss 구함 Back propagation(Gradient 구함) 정해진 optimizer에 따라 parameter 업데이트 trans = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) datasets = tv.datasets.MNIST(root='./MNIST', train=True, download=True, transform=trans) dataloader = tc.utils.data.DataLoader(datasets=datasets, batch_size=100, shuffle=True) Cf.
Example October 9, 2018 43 • Learning • L1Loss • MSELoss • CrossEntropyLoss • NLLLoss • PoissonNLLLoss • KLDivLoss • BCELoss • BCEWithLogitsLoss • MarginRankingLoss • HingeEmbeddingLoss • SmoothL1Loss • SoftMarginLoss • MultiLabelSoftMarginLoss • CosineEmbeddingLoss • MultiMarginLoss • TripleMarginLoss • Adadelta • Adagrad • Adam • SparseAdam • Adamax • ASGD • LBFGS • RMSprop • Rprop • SGD Loss Optimizer * Bold체는 주로 사용하는 함수들.
Example • Applications –Vanilla GAN 2018-10-09 44 https://github.com/messy-snail/GAN_PyTorch “What is this? Gum? It's GAN.” Cf.
Example • Applications –Vanilla GAN 2018-10-09 45 Sequence baseClass base Neural Nets
Example • Applications –Vanilla GAN 2018-10-09 46
Example • Applications –Vanilla GAN 2018-10-09 47 Images(batch=100) … 28 28 100 Reshape(28*28*1->784) 7 8 4 7 8 4… 7 8 4 100 1 0 0 Noise(z) Label 1 0 Real Fake
Example • Applications –Vanilla GAN 2018-10-09 48 1 0 0 Noise(z) Label 1 0 Real Fake G (z) Generate D(Fakes) D(Images) Discriminate Loss Real Loss Fake Loss Reshape(28*28*1->784) 7 8 4 7 8 4… 7 8 4 100
Example • Applications –Vanilla GAN 2018-10-09 49 D(Fakes) D(Images) Discriminate Loss Real Loss Fake Total Loss Loss Update
Example • Applications –Vanilla GAN 2018-10-09 50 1 0 0 Noise(z) Label 1 Real G (z) Generate D(Fakes) Discriminate Loss g Loss Update
Future Work October 9, 2018 51 Paper Review Vanilla GAN DCGAN InfoGAN Unrolled GAN Wasserstein GAN LS GAN BEGAN Pix2Pix Cycle GAN Proposed Model SpyGAN Tools Document Programming PyTorch Python executable & UI Mathematical Study Linear algebra Probability and statistics Information theory Others Level Processor Ice Propagation Maybe next seminar?
52 &

PyTorch 튜토리얼 (Touch to PyTorch)

  • 1.
    Touch to PyTorch ISLLab Seminar Hansol Kang : From basic to vanilla GAN
  • 2.
    Contents October 9, 2018 Setup Install DevelopmentTools Example What is PyTorch? PyTorch Deep Learning Framework Tensor Datasets Neural Nets Learning Applications
  • 3.
    What is PyTorch? October9, 2018 2 an open source machine learning library for Python. It is primarily developed by Facebook's artificial-intelligence research group. • PyTorch Vs.
  • 4.
    What is PyTorch? October9, 2018 3 Written in : Python Interface : PythonNov. 2010 Written in : C++ Interface : Python, MATLAB, C++Dec. 2013 Written in : C, Lua Interface : C, LuaJul. 2014 Written in : Python Interface : Python, RMar. 2015 Written in : C++, Python, CUDA Interface : Python, C/C++, Java, Go, R, JuliaNov. 2015 Written in : Interface : Python, C++Apr. 2017 Written in : Python, C, CUDA Interface : PythonOct. 2016 ** DL4J(Java) Chainer(Python) MXNet(C++, Python, Julia, MATLAB, JavaScript, Go, R, Scala, Perl) CNTK(Python, C++), TF Learn(Python) TF-Slim(Python) Etc. Recommend to choose these framework • Deep Learning Framework “What is this? Gum? It's GAN.”, pp. 21-22 Cf.
  • 5.
    Setup October 9, 2018 4 •Install https://www.anaconda.com/download/
  • 6.
    Setup October 9, 2018 5 •Install https://pytorch.org/ C++ : Preview, Linux only
  • 7.
    Setup October 9, 2018 6 •Install conda create -n PyTorch python=3.6 activate PyTorch conda install pytorch cuda90 -c pytorch pip install torchvision o conda create는 환경 생성하는 명령어. PyTorch 뿐만 아니라 Tensorflow 같은 다른 딥러닝 프레임워크를 사용한다거나 하위 파이썬 버전도 사용해야하는 경우 환경마다 설정해주면, 디펜던시가 꼬이지 않음. o -n 환경명, python=파이썬버전 입력. 환경설정 리스트는 conda env list를 입력하면 확인 가능. o activate는 해당 환경을 활성화 시키는 명령어. 반대로 환경을 빠져나오는 명령어는 deactivate. o PyTorch를 설치하는 명령어는 conda install pytorch cuda90 -c pytorch.. o torchvision은 딥러닝 학습에 많이 사용되는 데이터셋, 네트워크 구조, 이미지 변환과 같은 기능을 제공하므로 설치하는 것을 권장.
  • 8.
    Setup October 9, 2018 7 •Development Tools : Cell based execution : Intellisense : Intellisense : Management of python env. : GitHub : AI tool package : Startup file : Intellisense : Cell based execution : Management of python env. : Extension program : Insane extension program : Intellisense : Environment setting
  • 9.
    Setup October 9, 2018 8 •Development Tools Personal opinion : >
  • 10.
    Setup October 9, 2018 9 •Development Tools – Visual Studio File-New-Project
  • 11.
    Setup October 9, 2018 10 •Development Tools – Visual Studio File-New-Project Python 도구 설치
  • 12.
    Setup October 9, 2018 11 •Development Tools – Visual Studio File-New-Project Python 도구 설치 Python Application
  • 13.
    Setup October 9, 2018 12 •Development Tools – Visual Studio File-New-Project Python 도구 설치 Python Application Python 환경 추가
  • 14.
    Setup October 9, 2018 13 •Development Tools – Visual Studio File-New-Project Python 도구 설치 Python Application Python 환경 추가 코드 작성
  • 15.
    Setup October 9, 2018 14 •Development Tools – Visual Studio File-New-Project Python 도구 설치 Python Application Python 환경 추가 코드 작성
  • 16.
    Setup October 9, 2018 15 •Development Tools – Visual Studio File-New-Project Python 도구 설치 Python Application Python 환경 추가 코드 작성 실행 : 시작 파일로 설정-> Ctrl+F5
  • 17.
    Setup October 9, 2018 16 •Development Tools – PyCharm
  • 18.
    Setup October 9, 2018 17 •Development Tools – PyCharm
  • 19.
    Setup October 9, 2018 18 •Development Tools – PyCharm
  • 20.
    Setup October 9, 2018 19 •Development Tools – PyCharm
  • 21.
    Setup October 9, 2018 20 •Development Tools – PyCharm
  • 22.
    Setup October 9, 2018 21 •Development Tools – PyCharm File-New Project
  • 23.
    Setup October 9, 2018 22 •Development Tools – PyCharm File-New Project 코드 작성
  • 24.
    Setup October 9, 2018 23 •Development Tools – PyCharm File-New Project 코드 작성 실행 : Ctrl+F5 “Too easy, but important things”, pp. 42-43 Cf. TODO
  • 25.
    Example October 9, 2018 24 •Tensor array Tensor reshape view(reshape) linspace linspace ones, zeros ones, zeros random.randn, random.rand randn, rand from_numpy numpy to, cuda
  • 26.
    Example October 9, 2018 25 •Tensor import torch as tc #Tensor a = tc.Tensor([2.5, 4]) b = tc.Tensor([[1, 2.5], [2.5, 6]]) #reshape c = b.reshape(1, 4) d = b.reshape(1, -1) Tensor a : tensor([ 2.5000, 4.0000]) Size of a : torch.Size([2]) Tensor b : tensor([[ 1.0000, 2.5000], [ 2.5000, 6.0000]]) Size of b : torch.Size([2, 2]) Tensor c : tensor([[ 1.0000, 2.5000, 2.5000, 6.0000]]) Size of c : torch.Size([1, 4]) Tensor d : tensor([[ 1.0000, 2.5000, 2.5000, 6.0000]]) Size of d : torch.Size([1, 4])
  • 27.
    Example October 9, 2018 26 •Tensor Start End 0 #ones, zeros real = tc.ones(2, 2) fake = tc.zeros(2, 2) 1 #linspace x = tc.linspace(-1, 1, 9) Tensor x : tensor([-1.0000, -0.7500, -0.5000, -0.2500, 0.0000, 0.2500, 0.5000, 0.7500, 1.0000]) Size of x : torch.Size([9]) Tensor real : tensor([[ 1., 1.], [ 1., 1.]]) Size of real : torch.Size([2, 2]) Tensor fake : tensor([[ 0., 0.], [ 0., 0.]]) Size of fake : torch.Size([2, 2])
  • 28.
    Example October 9, 2018 27 •Tensor 0 1 0 1 #randn, rand z1 = tc.randn(2, 3) z2 = tc.rand(2, 3) Tensor z1 : tensor([[-0.0750, -1.0555, -0.0706], [-0.1946, -0.8593, -0.2238]]) Size of z1 : torch.Size([2, 3]) Tensor z2 : tensor([[ 0.8204, 0.3505, 0.1034], [ 0.5318, 0.9543, 0.8781]]) Size of z2 : torch.Size([2, 3]) #to, cuda z_cuda = z1.cuda() device = tc.device('cuda' if tc.cuda.is_available() else 'cpu') z_device = z1.to(device) Tensor z_cuda : tensor([[-0.0750, -1.0555, -0.0706], [-0.1946, -0.8593, -0.2238]], device='cuda:0') Tensor z_device : tensor([[-0.0750, -1.0555, -0.0706], [-0.1946, -0.8593, -0.2238]], device='cuda:0')
  • 29.
    Example October 9, 2018 28 •Tensor #from_numpy, numpy import numpy as np a = np.array([3.5, 4]) b = tc.from_numpy(a) c = b.numpy() array a : [3.5 4. ] Tensor b : tensor([ 3.5000, 4.0000], dtype=torch.float64) array c : [3.5 4. ]
  • 30.
    Example October 9, 2018 29 •Datasets The following datasets are available: MNIST Fashion-MNIST EMNIST COCO LSUN ImageFolder DatasetFolder Imagenet-12 CIFAR STL10 SVHN PhotoTour : 바로 사용 가능. : 추가적인 과정 필요. : Custom dataset에 활용. MNIST Fashion-MNIST CIFAR LSUN CelebA K-pop(Custom)
  • 31.
    Example October 9, 2018 30 •Datasets – MNIST trans  Compose()is used when there are multiple transform options. Here, ToTensor() and Normalize(mean, std) are used.  ToTensor () changes the PIL Image to a tensor. torchvision dataset The default type is PIL Image.  Normalize (mean, std) transforms the range of the image. Here, the value of [0, 1] is adjusted to [-1, 1]. ((value-mean) / std) trans = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) datasets = tv.datasets.MNIST(root='./MNIST', train=True, download=True, transform=trans) dataloader = tc.utils.data.DataLoader(datasets=datasets, batch_size=100, shuffle=True) import torch as tc import torchvision as tv import torchvision.transforms as transforms
  • 32.
    Example October 9, 2018 31 •Datasets – MNIST dataset  root : This is the path to store (MNIST data). Folders are automatically created with the specified name.  train : Set the data to be used for the train.  transform : Transform the data according to the transform option set previously.  download : Download (MINST data). (If you downloaded it once, it will not do it again.) trans = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) datasets = tv.datasets.MNIST(root='./MNIST', train=True, download=True, transform=trans) dataloader = tc.utils.data.DataLoader(datasets=datasets, batch_size=100, shuffle=True) import torch as tc import torchvision as tv import torchvision.transforms as transforms
  • 33.
    Example October 9, 2018 32 •Datasets – MNIST dataloader  dataset : Set the dataset to load.  batch_size : Set the batch size.  shuffle : Shuffle the data and load it. trans = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) datasets = tv.datasets.MNIST(root='./MNIST', train=True, download=True, transform=trans) dataloader = tc.utils.data.DataLoader(datasets=datasets, batch_size=100, shuffle=True) import torch as tc import torchvision as tv import torchvision.transforms as transforms
  • 34.
    Example October 9, 2018 33 •Datasets – LSUN https://github.com/fyu/lsun
  • 35.
    Example October 9, 2018 34 •Datasets – LSUN Run download.py def list_categories(tag): url = 'http://lsun.cs.princeton.edu/htbin/list.cgi?tag=' + tag f = urlopen(url) return json.loads(f.read()) If you are using Python 3.0 or later, modify the code from urllib2.urlopen (url) to urlopen (url). download.py -c bedroom bedroom 0 bridge 1 church_outdoor 2 classroom 3 conference_room 4 dining_room 5 kitchen 6 living_room 7 restaurant 8 tower 9
  • 36.
    Example October 9, 2018 35 •Datasets – LSUN trans = transforms.Compose([transforms.Resize((64,64)) ,transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) datasets = tv.datasets.LSUN('.', classes=['bedroom_train'], transform=trans) dataloader = tc.utils.data.DataLoader(datasets=datasets, batch_size=100, shuffle=True) trans  Resize() is used to resize the image. datasets  root : Root directory for the database files.  classes : One of {‘train’, ‘val’, ‘test’} or a list of categories to load. e,g. [‘bedroom_train’, ‘church_train’].
  • 37.
    Example October 9, 2018 36 •Datasets – ImageFolder(CelebA) http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html
  • 38.
    Example October 9, 2018 37 •Datasets – ImageFolder(CelebA) http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html 압축을 풀면 jpg 형태로 파일들이 저장된 것을 확인할 수 있음.
  • 39.
    Example October 9, 2018 38 •Datasets – ImageFolder(CelebA) trans = transforms.Compose([transforms.Resize((64,64)) ,transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) datasets = tv.datasets.ImageFolder('./img_align_celeba', trans) dataloader = tc.utils.data.DataLoader(datasets=datasets, batch_size=100, shuffle=True) datasets  root : Root directory for the database files.  transform : Transform the data according to the transform option set previously.
  • 40.
    H Example October 9, 2018 39 •Neural Nets Sequence baseClass base model = tc.nn.Sequential( tc.nn.Linear(D_in, H), tc.nn.ReLU(), tc.nn.Linear(H, D_out), tc.nn.Sigmoid() ) class Model(tc.nn.Module): def __init__(self): super(Model, self).__init__() self.linear1 = tc.nn.Linear(D_in, H), self.linear2 = tc.nn.Linear(H, D_out) def forward(self, input): x = tc.nn.functional.relu(self.linear1(input)) x = tc.nn.functional.sigmoid(self.linear2(x)) return x D_in D_out Data manipulation
  • 41.
    Example October 9, 2018 40 •Neural Nets Class base class Model(tc.nn.Module): def __init__(self): super(Model, self).__init__() self.linear1 = tc.nn.Linear(D_in, H), self.linear2 = tc.nn.Linear(H, D_out) def forward(self, input): x = tc.nn.functional.relu(self.linear1(input)) x = tc.nn.functional.sigmoid(self.linear2(x)) return x tc.nn.Module functions Model functions 생성자 부모 클래스 초기화 Propagation Cf. Diamond problem A B C D Class D __init__() Class B __init__() Class A __init__() Class C __init__() Class A __init__()
  • 42.
    Example October 9, 2018 41 •Learning Back Propagation (Learning) Loss Optimizer loss_func = tc.nn.MSELoss() opt = tc.optim.Adam(model.parameters(), lr=0.01 ) for ep in range(epoch_sz): for step, (images, labels) in enumerate(dataloader): opt.zero_grad() images = images.to(device) labels = labels.to(device) output = model(images) loss = loss_func(output) loss.backward() opt.step()
  • 43.
    Example October 9, 2018 42 •Learning loss_func = tc.nn.MSELoss() opt = tc.optim.Adam(model.parameters(), lr=0.01 ) for ep in range(epoch_sz): for step, (images, labels) in enumerate(dataloader): opt.zero_grad() images = images.to(device) labels = labels.to(device) output = model(images) loss = loss_func(output) loss.backward() opt.step() Loss 선언 Optimizer 선언 (업데이트 하려는 parameter) 정해진 epoch 만큼 수행 앞서 설정한 dataloader에 따라 image(data)와 label(_) 불러옴 Gradient 초기화. Loss 구함 Back propagation(Gradient 구함) 정해진 optimizer에 따라 parameter 업데이트 trans = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) datasets = tv.datasets.MNIST(root='./MNIST', train=True, download=True, transform=trans) dataloader = tc.utils.data.DataLoader(datasets=datasets, batch_size=100, shuffle=True) Cf.
  • 44.
    Example October 9, 2018 43 •Learning • L1Loss • MSELoss • CrossEntropyLoss • NLLLoss • PoissonNLLLoss • KLDivLoss • BCELoss • BCEWithLogitsLoss • MarginRankingLoss • HingeEmbeddingLoss • SmoothL1Loss • SoftMarginLoss • MultiLabelSoftMarginLoss • CosineEmbeddingLoss • MultiMarginLoss • TripleMarginLoss • Adadelta • Adagrad • Adam • SparseAdam • Adamax • ASGD • LBFGS • RMSprop • Rprop • SGD Loss Optimizer * Bold체는 주로 사용하는 함수들.
  • 45.
    Example • Applications –VanillaGAN 2018-10-09 44 https://github.com/messy-snail/GAN_PyTorch “What is this? Gum? It's GAN.” Cf.
  • 46.
    Example • Applications –VanillaGAN 2018-10-09 45 Sequence baseClass base Neural Nets
  • 47.
  • 48.
    Example • Applications –VanillaGAN 2018-10-09 47 Images(batch=100) … 28 28 100 Reshape(28*28*1->784) 7 8 4 7 8 4… 7 8 4 100 1 0 0 Noise(z) Label 1 0 Real Fake
  • 49.
    Example • Applications –VanillaGAN 2018-10-09 48 1 0 0 Noise(z) Label 1 0 Real Fake G (z) Generate D(Fakes) D(Images) Discriminate Loss Real Loss Fake Loss Reshape(28*28*1->784) 7 8 4 7 8 4… 7 8 4 100
  • 50.
    Example • Applications –VanillaGAN 2018-10-09 49 D(Fakes) D(Images) Discriminate Loss Real Loss Fake Total Loss Loss Update
  • 51.
    Example • Applications –VanillaGAN 2018-10-09 50 1 0 0 Noise(z) Label 1 Real G (z) Generate D(Fakes) Discriminate Loss g Loss Update
  • 52.
    Future Work October 9,2018 51 Paper Review Vanilla GAN DCGAN InfoGAN Unrolled GAN Wasserstein GAN LS GAN BEGAN Pix2Pix Cycle GAN Proposed Model SpyGAN Tools Document Programming PyTorch Python executable & UI Mathematical Study Linear algebra Probability and statistics Information theory Others Level Processor Ice Propagation Maybe next seminar?
  • 53.