반응형

아래 내용은 Udemy에서 Pytorch: Deep Learning and Artificial Intelligence를 보고 정리한 내용이다.

tensorflow install:

sudo pip install tensorflow

 

download anaconda

 

pip install --upgrade pandas theano

 

pip install tensorflow

pip install keras

 

NVIDIA GPU

CUDA

CuDNN

 

at least four giga

 

 

pip install tensorflow

pip install tensorflow-gpu

 

tf.test.is_gpu_available()

 

cpu/gpu torch

pip install torch

 

pip install keras

 

 

How to Code Yourself

Techniques <-> practice

 

fit(X,y)

prediction(X)

 

which parts did I get right?

which parts did I get Wrong?

 

jupyter notebook

 

deeplearningcourses.com/catalog

 

Deep Learning Courses - Master Neural Networks, Machine Learning, and Data Science in Python, Theano, TensorFlow, and Numpy

Deep Learning: Advanced Computer Vision (GANs, SSD, +More!)

deeplearningcourses.com

 

text

images

 

vanishing gradients 어디서 생기는지 ?

 

RNN -> text

 

regression 

classification

 

ensumble machine learning

random forest and a boost

 

 

반응형
반응형

아래 내용은 Udemy에서 Pytorch: Deep Learning and Artificial Intelligence를 보고 정리한 내용이다.

Gradient Descent

backbone of deep learning 

 

k-means clustering

hidden markov models

matric factorization

 

Big learning rate 

small learning rate

Stochastic Gradient Descent

optimizer = 'sgd'

 

stochastic

 

Momentum

SGD

움직이는 것 

physics momentum

zigzag

momentum이 없으면 엄청 zigzig가 많이 있다. 

momentum이 있으면 빨리 내리온다.

 

 

Variable and Adaptive Learning Rates

momentum is nice: huge performance gains, almost no work. 0.9 is usually fine.

learning rate scheduling

#1. step decay

exponentail decay 

 

leaning rate: 

너무 크게 해도 안좋고 너무 작게 해도 안좋다.

too slow -> increase learning rate

be careful! may hit a plateau temporarily

 

AdaGrad:Adaptive Learning Rete Techniques

everything is element-wise

each scalar parameter and its learning rate is updated independently of the others

It has been observed that AdaGrad decreases learning rate too aggressively

 

RMSProp

Introduced by Geoff Hinton+ team

since cache is growing too fast, let's decrease it on each update:

 

어떤것이 맞는지도 알 수 가 없다.

major packages have implemented both

Tensorflow initializers cache = 1

Kears initializzers cache = 0

 

AdaGrad:

at every batch:

cache+= gradient * 2

param = param- learning_rate * gradient/ sqrt(cache_ epsilon)

 

RMSProp

At every batch:

cache = decay * cache + (1-decay) * gradient ** 2

param = param- learning_rate * gradient/ sqrt(cache_ epsilon)

 

epsilon = 10 ** -8, 10 ** -9, 10 ** -10 ,etc ..., decay = 0.9, 0.99, 0.999, 0.9999, etc...

 

Adam optimizer

go-to default thes days

"RMSprop with momentum"

exponentailly-Smoothed Averages

 

RMS = "root mean square"

 

Adam:

m and v

 

반응형
반응형

아래 내용은 Udemy에서 Pytorch: Deep Learning and Artificial Intelligence를 보고 정리한 내용이다.

Mean Squared Error

MSE :probabilistic perspective

helps prepare us for the cross-entropy loss

Error = Cost = Loss = Objective

 

Why is it Squared?

positive 값과 negative값이 있을 수 있기 때문에 

 

MAE 

 

MLE: Maximum Likelihood Estimation(MLE)

eg.: we model the heights of the students in our class as a Gaussian distribution (bell curve)

 

the "best" estimate for μ

minimizing is the sam as maximizing negative

 

Binary Cross Entropy

binary classification

Regression: MES, Gaussian distrivution

Binary Classification: Binary Cross-Entropy , Bernoulli distribution

 

Categorical Cross Entropy

반응형
반응형

아래 내용은 Udemy에서 Pytorch: Deep Learning and Artificial Intelligence를 보고 정리한 내용이다.

VIP : Uncertainy Estimation

 

classification & regression

 

regression 

binary classification

multiple classification

 

positive

 

The neural network

2 outputs nodes, 

 

custom loss: mse

mean squared error

 

negative log-likelihood

criterion

 

pytorch uncertainy estimation

def generate_batch(batch_size = 32):
  x = np.random.random(batch_size) * 10 - 5
  sd = 0.05 + 0.1 * ( x+5)
  y = np.cos(x ) -0.3 * x + np.random.randn(batch_size) * sd
  return x, y

x ,y = generate_batch(1024)
plt.scatter(x, y , alpha=0.5)

모델 만들기

class Model(nn.Module):
  def __init__(self):
    super().__init__()

    self.ann1 = nn.Sequential(nn.Linear(1,10), nn.Tanh(), nn.Linear(10,1))
    self.ann2 = nn.Sequential(nn.Linear(1,10), nn.Tanh(), nn.Linear(10,1))
  def forward(self, inputs):
    return self.ann1(inputs), self.ann2(inputs)

loss function 만둘기

def criterion(outputs, targets):
  mu = outputs[0]
  v = torch.exp(outputs[1])

  c = torch.log(torch.sqrt(2 * np.pi * v))
  f = 0.5 / v* (targets- mu) **2
  nu1 = torch.mean(c+f)
  return nu1

학습하기

model = Model()
optimizer = torch.optim.Adam(model.parameters())

n_epochs = 5000
batch_size = 128
losses = np.zeros(n_epochs)
for i in range(n_epochs):
  x, y = generate_batch(batch_size)
  inputs = torch.from_numpy(x).float()
  targets = torch.from_numpy(y).float()

  inputs, targets = inputs.view(-1, 1), targets.view(-1, 1)
  optimizer.zero_grad()
  outputs = model(inputs)
  loss = criterion(outputs, targets)
  losses[i] = loss.item()

  if i % 1000 == 0:
    print(i, losses[i])
  
  loss.backward()
  optimizer.step()
x, y = generate_batch(1024)
plt.scatter(x,y , alpha=0.5)

inputs = torch.from_numpy(x).float()
targets = torch.from_numpy(y).float()

inputs, targets = inputs.view(-1, 1), targets.view(-1, 1)

with torch.no_grad():
  outputs = model(inputs)
  yhat = outputs[0].numpy().flatten()
  sd = np.exp(outputs[1].numpy().flatten() / 2)
idx = np.argsort(x) 
plt.plot(x[idx], yhat[idx], linewidth = 3, color='red')
plt.fill_between(x[idx], yhat[idx]- sd[idx], yhat[idx]+ sd[idx], color='red' ,alpha = 0.5)
plt.show()

 

 

Facial Recognition Section Introduction

siamese network

binary classification

"match" or "no match"

 

Making Pairs

"N choose 2" pairs

 

Facial Recognition

one time opportunity

 

A first approach

image classification?

build cnn => 101 classes non - students

한명 나가고 다시 들어왔을 때 어떻게 해야 하는지 ?

백만명 할때는 어떻게 해야 하는지?

 

Binary Classification:

image -> cnn -> match or not

 

Siamese Network

2 CNN -> not classify soft max가 없다 -> feature vector

different anger, glasses, ect. hen f(x1) should be close to f(X2)

if we have different people , then f(x1) should be far from f(X2)

 

face embeddings

contrastive loss

choose a threshold

< 0.75 predict "match"

> 0.75 predict " no match"

 

Siamese Networks Code Outline

basic image classifier 

set up a losss

minimize the loss

 

Not a normal image classifier

x = an image

y = the class that the image belongs to

Not exactly what we need for siamese networks

label 등이 없다.

 

Given 

image를 준다. 

id를 설정한다.

label이 아니다.  우리는 그냥 match vs. non-match만 한다.

 

dataset

pairs를 만들기 

 

Siamese Networks Loading in the data

files = glob('yalefaces/subject*')
np.random.shuffle(files)
N = len(files)
N
H, w = 60,80
def load_img(filepath):
  img = image.img_to_array(image.load_img(filepath, target_size=[H, w])).astype('uint8')
  return img
img = load_img(np.random.choice(files))
plt.imshow(img)
plt.show()
def to_grayscale(img):
  return img.mean(axis = -1)
shape = (N, H, w)
images = np.zeros(shape)
for i, f in enumerate(files):
  img = to_grayscale(load_img(f)) / 255.0
  images[i] = img


label 하기

labels = np.zeros(N)
for i, f in enumerate(files):
  filename = f.rsplit('/',1)[-1]
  subject_num = filename.split('.', 1)[0]
  idx = int(subject_num.replace('subject',''))-1
  labels[i] = idx

Counter하기

label_count = Counter(labels)
label_count
count_so_far = {}
train_idx = 0
test_idx = 0 
for img, label in zip(images, labels):
  count_so_far[label] = count_so_far.get(label, 0 ) + 1
  if count_so_far[label] > 3:
    train_images[train_idx ] = img
    train_labels[train_idx] = label
    train_idx += 1
  else:
    test_images[test_idx] = img
    test_labels[test_idx] = label
    test_idx += 1

 

What do we want?

train_label2idx = {}
test_label2idx = {}

for i, label in enumerate(train_labels):
  if label not in train_label2idx:
    train_label2idx[label] = [i]
  else:
    train_label2idx[label].append(i)

for i, label in enumerate(test_labels):
  if label not in test_label2idx:
    test_label2idx[label] = [i]
  else:
    test_label2idx[label].append(i)
train_positives = []
train_negatives = []
test_positives = []
test_negatives = []

for label, indices in train_label2idx.items():
  other_indices = set(range(n_train)) - set(indices)
  for i , idx1 in enumerate(indices):
    for idx2 in indices[i+1:]:
      train_positives.append((idx1, idx2))
    for idx2 in other_indices:
      train_negatives.append((idx1, idx2))

for label, indices in test_label2idx.items():
  other_indices = set(range(n_test)) - set(indices)
  for i , idx1 in enumerate(indices):
    for idx2 in indices[i+1:]:
      test_positives.append((idx1, idx2))
    for idx2 in other_indices:
      test_negatives.append((idx1, idx2))

subject1 : A,B

subject2: C,D

positives[(A,B),(C,D)]

negatives[(A,C),(A,D),(B,C),(B,D)]

 

모델 정의

 

loss 함수

 

반응형
반응형

아래 내용은 Udemy에서 Pytorch: Deep Learning and Artificial Intelligence를 보고 정리한 내용이다.

Reinforcement Learning

 

machine learning to the stock market

supervised learning 은 prediction 만 하고 , but you must still take the action

RNNs rather than RL

 

Actions = buy / sell/hold

state = stock prices/ # shares owned / amount of cash i have

reward = some function of protfolio value gained/ lost

 

build environment

 

state will consider of 3parts:

1. how many shares of each stock i own

2. current price of each stock

3. how much cash we have(uninvested)

 

action 

buy /sell / hold(do nothing)

3가지 주석 고려할 경우 3^3 => 27가능하다.

eg. sell , sell, sell

 

 

pop(0) => index 0

 

states(NxD array)

actions(N array)

rewards(N array)

Next states(N x D array)

Done flas(N array)

 

train and test

 

 

 

 

environment

agent

 

google colab에서 하면 local 보다 너무 늦어서 다른데서 하는 것이 좋다.

import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F

from datetime import datetime
import itertools
import argparse
import re
import os
import pickle

from sklearn.preprocessing import StandardScaler

 

get_data()

 

class ReplyBUffer:
  def __init__(self, obs_dim, act_dim, size):
    self.obs1_buf = np.zeros([size, obs_dim], dtype = np.float32)
    self.obs2_buf = np.zeros([size, obs_dim], dtype = np.float32)
    self.acts_buf = np.zeros(size, dtype=np.uint8)
    self.rew_buf = np.zeros(size, dtype=np.float32)
    self.done_buf = np.zeros(size, dtype = np.unit8)
    self.ptr, self.size, self.max_size = 0,0, size
  
  def store(self, obs, act, rew, next_obs, done):
    self.obs1_buf[self.ptr] = obs
    self.obs2_buf[self.ptr] = next_obs
    self.acts_buf[self.ptr] = act 
    self.rew_buf[self.ptr] = rew
    self.done_buf[self.ptr] = done
    self.ptr = (self.ptr+1) % self.max_size
    self.size = min(self.size +1, self.max_size)
  
  def sample_batch(self, batch_size = 32):
    idx = np.random.randint(0, self.size, size = batch_size)
    return dict(s = self.obs1_buf[idx], s2 = self.obs2_buf[idx], a = self.acts_buf[idx], r = self.rew_buf[idx], d  = self.done_buf[idx] )

def get_scaler(env):
  states = []
  for _ in range(env.n_step):
    action = np.random.choice(env.action_space)
    state, reward, done = env.step(action)
    states.append(state)
    if done:
      break
  scaler = StandardScaler()
  scaler.fit(states)
  return scaler

def maybe_make_dir(directory):
  if not os.path.exists(directory):
    os.makedirs(directory)
class MLP(nn.Module):
  def __init__(self, n_inputs, n_action, n_hidden_layers = 1, hidden_dim = 32):
    super(MLP, self)._init__()

    M = n_inputs
    self.layers = []
    for _ in range(n_hidden_layers):
      layer = nn.Linear(M, hidden_dim)
      M = hidden_dim
      self.layers.append(layer)
      self.layers.append(nn.ReLU())

    self.layers.append(nn.Linear(M, n_action))
    self.layers = nn.Sequential(*self.layers)

  def forward(self, x):
    return self.layers(x)
  
  def save_weights(self, path):
    torch.save(self.state_dict(), path)
  
  def load_weights(self, path):
    self.load_state_dict(torch.load(path))

  def predict(model, np_states):
    with torch.no_grad():
      inputs = torch.from_numpy(np_states.astype(np.float32))
      output = model(inputs)
      return output.numpy()
    
  def train_one_step(model, criterion, optimizer, inputs, targets):
    inputs = torch.from_numpy(inputs.astype(np.float32))
    targets = torch.from_numpy(targets.astype(np.float32))

    optimizer.zero_grad()

    outputs = model(inputs)
    loss = criterion(outputs, targets)

    loss.backward()
    optimizer.step()
class MultiStockEnv:
  def __init__(self, data, initial_investment = 20000):
    self.stock_price_history = data
    self.n_step, self.n_stack = self.stock_price_history.shape

    self.initial_investment = initial_investment
    self.cur_step = None
    self.stock_owned = None
    self.stock_price = None
    self.cash_in_hand = None
    
    self.action_space = np.arange(3 ** self.n_stock)

    self.action_list = list(map(list, itertools.product([0,1,2], repeat=self.n_stock)))
    self.state_dim = self.n_stock * 2+1
    self.reset()

  def reset(self):
    self.cur_step = 0
    self.stock_owned = np.zeros(self.n_stock)
    self.stock_price = self.stock_price_history[self.cur_step]
    self.cash_in_hand = self.initial_investment
    return self._get_obs()

  def step(self, action):
    assert action in self.action_space

    prev_val = self._get_val()

    self.cur_step+= 1
    self.stock_price = self.stock_price_history[self.cur_step]

    self._trade(action)

    cur_val = self._get_val()

    reward = cur_val = prev_val
    done = self.cur_step == self.n_step -1
    info = {'cur_val': cur_val}
    return self._get_obs(), reward, done, info

  def _get_obs(self):
    obs = np.empty(self.state_dim)
    obs[:self.n_stock] = self.stock_owned
    obs[self.n_stock: 2 * self.n_stock] = self.stock_price
    obs[-1] = self.cash_in_hand
    return obs

  def _get_val(self):
    return self.stock_owned.dot(self.stock_price) + self.cash_in_hand
    
  def _trade(self, action):
    action_vec = self.action_list[action]
    sell_index = []
    buy_index = []
    for i, a in enumerate(action_vec):
      if a == 0:
        sell_index.append(i)
      elif a == 2:
        buy_index.append(i)
    if sell_index:
      for i in sell_index:
        self.cash_in_hand += self.stock_price[i] + self.stock_owned[i]
        self.stock_owned[i] = 0
    if buy_index:
      can_buy = True
      while can_buy:
        for i in buy_index:
          if self.cash_in_hand > self.stock_price[i]:
            self.stock_owned[i] += 1
            self.cash_in_hand -= self.stock_price[i]
          else:
            can_buy = False
class DQNAgent(object):
  def __init__(self, state_size, action_size):
    self.state_size = state_size
    self.action_size = action_size
    self.memory = ReplyBUffer(state_size, action_size, size = 500) 
    self.gamma = 0.5
    self.epsilon = 1.0
    self.epsilon_min = 0.01
    self.epsilon_decay = 0.995
    self.model = MLP(state_size, action_size)

    self.criterion = nn.MSELoss()
    self.optimizer = torch.optim.Adam(self.model.parameters())

  def update_replay_memory(self,state, action, reward, next_state, done):
    self.memory.store(state, action, reward, next_state, done)
  def act(self, state):
    if np.random.rand() <= self.epsilon:
      return np.random.choice(self.action_size)
    act_values = predict(self.model, state)
    return np.argmax(act_values[0])
  def replay(self, batch_size = 32):
    if self.momory_size < batch_size:
      return

    minibatch = self.memory.sample_batch(batch_size)
    states = minibatch['s']
    actions = minibatch['a']
    rewards = minibatch['r']
    next_states = minibatch['s2']
    done = minibatch['d']

    target = rewards + (1-done)* self.gamma * np.amax(self.model.predict(next_states), axis = 1)

    target_full = predict(self.model, states)
    target_fill[np.arange(batch_size), actions ]  = target

    train_one_step(self.model, self.criterion, self.optimizer, states, target_full)

    if self.epsilon > self.epsilon_min:
      self.epsilon += self.epsilon_decay
  def load(self, name):
    self.model.load_weights(name)
  def save(self, name):
    self.model.save_weights(name)
  def play_one_episode(agent, env, is_train):
    state = env.reset()
    state = scaler.transform([state])
    done = False

    while not done:
      action = agent.act(state)
      next_state, reward, done, info = env.step(action)
      next_state = scaler.transform([next_state])
      if is_train == 'train':
        agent.update_replay_memory(state, action, reward, next_state, done)
        agent.replay(batch_size)
      state = next_state
    return info['cur_val']

 

반응형

'교육동영상 > 02. pytorch: Deep Learning' 카테고리의 다른 글

14. In-Depth: Loss Functions  (0) 2021.01.13
13. VIP  (0) 2021.01.08
10. Deep Reinforcement Learning  (0) 2021.01.04
10. GANs  (0) 2020.12.28
09. Transfer Learning  (0) 2020.12.23
반응형

아래 내용은 Udemy에서 Pytorch: Deep Learning and Artificial Intelligence를 보고 정리한 내용이다.

Reinforcement Learning 강화학습

supervised/unsupervised learning

 

image classifier just function

 

supervised Learning is like a static function

image를 입력하면 결과가 나온다.

 

 

Reinforcement Learning : loop

loop와 비슷하다.

we want to achieve a goal

future plan

 

agent

 

Tic-tac-Toe

environment: the computer program that implements the tic-tac-toe game

game.start()

game.move()

game.get_sate()

game.is_over() ->누가 이겼는지 computer agent

    O
X O  
O   X

 

episode 

 

agent, environment, episode

states, actions, rewards

state = for each location

action = where to place the next X/ O

Reward = a number, received at each step of the game

 

Example : Maze

 

breakout: states actions rewards

states:  what we observe in the environment, but can be derived from current and past observations

actions: just the different buttons/ inputs on the joystick or control pad

spaces:  state spaces

 

 

 

 

states actions rewards Policies

states: discrete or continuous

 

policies: 

policy as a function or mapping

 

the markov assumption

1. 예를 들어 내일 날씨는 rainy, sunny ,or cloudy 로 예측한다.

the markov assumption는 that tomorrow's weather depends only on today's weather, but not on any earlier day

어제 날씨와 관련만 되여있다.

2. 다음단어를 예측할 때 

 

Bellman equation

prediction vs. control.

 

반응형

'교육동영상 > 02. pytorch: Deep Learning' 카테고리의 다른 글

13. VIP  (0) 2021.01.08
12. Stock Trading Project with Deep Reinforcement Learning  (0) 2021.01.06
10. GANs  (0) 2020.12.28
09. Transfer Learning  (0) 2020.12.23
08. Recommender System  (0) 2020.12.21
반응형

아래 내용은 Udemy에서 Pytorch: Deep Learning and Artificial Intelligence를 보고 정리한 내용이다.

GANs Theory

CNNs and RNNs are just neural networks with shared weights

이전의 모델과 비교가 된다.

Gans is a system 

has two neural networks: generator and discriminator

 

위조범죄자 : 상점에 가서 가짜 돈을 사용하여 물건을 구매하려고 한다. 

shop owner: 진짜인지 가짜인지 판단한다.

 

GAN the loss function

2개가 필요하다. discriminator  generator

discriminator 은 classify real and fake images - binary classification

the correct loss is the binary cross entropy

 

Generator loss

Feeze the discriminator layers so only the generator is trained

real = 1 , fake = 0

 

Latent space

Generator is like the opposite of a feature transformer/embedding

image -> vecotr vs vecotr -> image

 

GAN pseudocode

#1. load in the data x, y = get_mnist()

#2. create networks

D = nn.Sequential(....)

G = nn.Sequential(....)

 

#3. loss and optimizer

crit = nn.BCEWithLogitsLoss()

optim_D = Adam(D.parameters())

optim_G = Adam(G.parameters())

 

#4. Get outputs and loss

D_out = [D(real), D(fake)] //then find loss, etc.

G_out = D(G(noise)) //combined_model(noise)

 

#4. gradient descent loop

for epoch in epochs:

  #train discriminator

  real_images = sample from x

  fake_images = g.predict(noise)

  d.train_on_batch(real_images, ones)

  d.train_on_batch(fake_images, zeros)

 

  #train generator

  combined_model.train_on_batch(noise, ones)

 

 

GAN code preparation

Generator is hard but discriminator is easy

 

Discriminator pytorch

D= nn.Sequential(
    nn.Linear(784, 512),
    nn.LeakyReLU(0.2),
    nn.Linear(512,256),
    nn.LeakyReLU(0.2),
    nn.Linear(256, 1)
)

 

Generator pytorch

latent_dim = 100
G = nn.Sequential(
    nn.Linear(latent_dim, 256),
    nn.LeakyReLU(0.2),
    nn.BatchNorm1d(256, momentum=0.7),
    nn.Linear(256,512),
    nn.LeakyReLU(0.2),
    nn.BatchNorm1d(512, momentum=0.7),
    nn.Linear(512,1024),
    nn.LeakyReLU(0.2),
    nn.BatchNorm1d(1024, momentum=0.7),
    nn.Linear(1024, 784),
    nn.Tanh()
)

 

 

 

 

GANs optimizer and loss pytorch

criterion = nn.BCEWithLogitsLoss()
d_optimizer = torch.optim.Adam(D.parameters(), lr = 0.0002, betas=(0.5, 0.999))
g_optimizer = torch.optim.Adam(G.parameters(), lr = 0.0002, betas=(0.5, 0.999))
# 0, 1로 return
def scale_image(img):
  out = (img+1) / 2
  return out
ones_ = torch.ones(batch_size, 1).to(device)
zeros_ = torch.zeros(batch_size, 1).to(device)

d_losses = []
g_losses = []

for epoch in range(200):
  for inputs, _ in data_loader:
    n = inputs.size(0)
    inputs = inputs.reshape(n, 784).to(device)

    ones = ones_[:n]
    zeros = zeros_[:n]

    ###train discriminator
    real_outputs = D(inputs)
    d_loss_real = criterion(real_outputs, ones)

    #fake images
    noise = torch.randn(n, latent_dim).to(device)
    fake_images = G(noise)
    fake_outputs = D(fake_images)
    d_loss_fake = criterion(fake_outputs, zeros)

    #gradient descent
    d_loss = 0.5 * (d_loss_real + d_loss_fake)
    d_optimizer.zero_grad()
    g_optimizer.zero_grad()
    d_loss.backward()
    d_optimizer.step()

    #train generator
    for _ in range(2):
      noise = torch.randn(n, latent_dim).to(device)
      fake_images = G(noise)
      fake_outputs = D(fake_images)

      g_loss = criterion(fake_outputs, ones)

      d_optimizer.zero_grad()
      g_optimizer.zero_grad()
      g_loss.backward()
      g_optimizer.step()

    d_losses.append(d_loss.item())
    g_losses.append(g_loss.item())

  print(f"Epoch: {epoch} , d_loss : {d_loss.item()} ,g_loss:{g_loss.item()}")

  fake_images = fake_images.reshape(-1, 1, 28,28)
  save_image(scale_image(fake_images) , f"gan_images/{epoch+1}.png")

 

반응형

'교육동영상 > 02. pytorch: Deep Learning' 카테고리의 다른 글

12. Stock Trading Project with Deep Reinforcement Learning  (0) 2021.01.06
10. Deep Reinforcement Learning  (0) 2021.01.04
09. Transfer Learning  (0) 2020.12.23
08. Recommender System  (0) 2020.12.21
07. NLP  (0) 2020.12.16
반응형

아래 내용은 Udemy에서 Pytorch: Deep Learning and Artificial Intelligence를 보고 정리한 내용이다.

Transfer Learning

Basic Conceps

Recall: Features are hierarchical!

computer vision

ImageNet - Large-scale image dataset

학습 하기 전에것은 좀 힘들다.

 

Training on ImageNet

 

2-part CNN

Feature transformer("body")

ANN classifier("head")

 

"head"를 바꾸고 새로운 해드를 추가 한다.

logistic regression or ANN 등  가능하다.

 

Freeze the "body"

train only the head (much fuster)

transfer learning 의 장점:

Don't need a lot of data to build a state-of-the-art model

with transfer learniing, this work has been done for us- the earlier features were already trained on lots of data(ImageNet)

Small dataset + a lot less weights helps us train fast

 

Some pre-trained Models(VGG, ResNet, Inception, MobieNet)

VGG -Visual Geometry Group

VGG16, VGG19

cnn 과 다르지 않고 그냥 bigger.

finally 3 fully-connected 

 

 

ResNet 

a cnn with branches(one branch is the identity function, so the other learns the residual)

ResNet50, ResNet101. ResNet152

ResNetv2-50, ResNetv2-101. ResNetv2-152

ResNetXt

layer에 따라 다르다.

 

Inception

similiar to resnet

Multiple convolutions in parallel branches

filter sizes (1x1, 3x3, 5x5, etc)

어떤것 선택할 지는 데이터 등에 따라 달라서 해야 한다.

 

MobileNet

Lightweight: makes a tradeoff between speed and accuracy

meant for less powerful machines(mobile, embedded)

 

Large Datasets pytorch

다양한 datasets

jpg, png

 

train_dataset = datasets.ImageFolder(

 'data/train',

 transform= train_transform

)

 

Approches to Transfer learning

2가지 부분

2-part CNN

Imagine the computation in 2 parts;

part 1: z = f(x) # pre-trained CNN - slow

part 2: y_hat = softmax(Wx+b) # logistic regression - fast

for epoch in epochs:

  shuffle(batches)

  for x, y in batches:

    z = vgg_body(x)

    y_hat = softmax(z.dot(w) + b)

   gw = grad(error(y, y_hat) , w)

   gb = grad(error(y, y_hat) , b)

   update w and b using gw and gb

 

vgg_body  -> 이것은 학습이 안된다.

 

 

All data is the same

z = f(x) # use cnn to precompute all z's at once

turn(z,y) into tabular dataset

fit(z,y) to logistic regression , never have to look at X again!

 

how can we use data augmentation

 

 

두가지 접근

1. use data augmentation with ImageFolder Dataset + DataLoader

  loop

2. precompute Z without data augmentation

  (Z,y)

 

 

장점과 단점

  with data augmentation without data augmentation
Speed slow(must pass through entire CNN) fast(only need to pass through 1 dense layer)
generalization/
accuracy
possibly better for generalization possibly worse for generalization

 

 

 

Transfer learning pytorch code 

pytorch transfer learning with data augmentation

binary classifier

 

normalizes mean and std are standardized for imagenet

train_dataset = datasets.ImageFolder(
 'data/train',
 transform= train_transform
)

test_dataset = datasets.ImageFolder(
 'data/test',
 transform= test_transform
)

pytorch vgg16 model pre-trained

model = models.vgg16(pretrained=True)

#freeze vgg weights
for param in model.parameters():
  param.requires_grad = False

binary classification

model.classifier = nn.Linear(n_features, 2)

pytorch transfer learning without data augmentation

transform= transforms.Compose([
transforms.Resize(size = 256),
transforms.CenterCrop(size = 224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406] , [0.229, 0.224, 0.225])
])

build the model

vgg = models.vgg16(pretrained=True)
class VGGFeatures(nn.Module):
  def __init__(self, vgg):
    super(VGGFeatures, self).__init__()
    self.vgg = vgg
  def forward(self, x):
    out = vgg.features(x)
    out = vgg.avgpool(out)
    out = out.view(out.size(0), -1) #flatten
    return out
vggf = VGGFeatures(vgg)
out = vggf(torch.rand(1,3, 224, 224))
out.shape

 

반응형

'교육동영상 > 02. pytorch: Deep Learning' 카테고리의 다른 글

10. Deep Reinforcement Learning  (0) 2021.01.04
10. GANs  (0) 2020.12.28
08. Recommender System  (0) 2020.12.21
07. NLP  (0) 2020.12.16
06-2. Recurrent Neural Networks, Time Series, and Sequence Data  (0) 2020.12.14

+ Recent posts