아래 내용은 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 함수
'교육동영상 > 02. pytorch: Deep Learning' 카테고리의 다른 글
15. In Depth: Gradient Descent (0) | 2021.01.13 |
---|---|
14. In-Depth: Loss Functions (0) | 2021.01.13 |
12. Stock Trading Project with Deep Reinforcement Learning (0) | 2021.01.06 |
10. Deep Reinforcement Learning (0) | 2021.01.04 |
10. GANs (0) | 2020.12.28 |