File size: 6,428 Bytes
87895a7 a3a6f78 87895a7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
import os
import torch
import random
import numpy as np
import torchvision
import matplotlib.pyplot as plt
import torchvision.transforms as transforms
import shutil
import time
import xml.etree.ElementTree as et
import pickle
from tqdm import tqdm
from PIL import Image
from torchvision import models
from torch.utils.data import DataLoader
from torchvision.datasets import ImageFolder
BATCH_SIZE = 32
use_gpu = torch.cuda.is_available()
device = 'cuda' if use_gpu else 'cpu'
print('Connected device:', device)
musk = 'https://drive.google.com/uc?export=download&id=1BOuq35QzO1YtKQkYGfj_vtBj3Ps5xyBN'
gates ='https://drive.google.com/uc?export=download&id=1jgHQF_NMpH9uMTvic9rGnURu_8UOGdiz'
bezos = 'https://drive.google.com/uc?export=download&id=1n5UaLL-TAkjIeBbTNcn-Czkp_A3Eslhj'
zuker = 'https://drive.google.com/uc?export=download&id=1ncPmYTg6EPHlUFdcjl_bXTbtWRLv2DXy'
jobs = 'https://drive.google.com/uc?export=download&id=1TX3hiRyvSYiYVZUFrbAhN3Jpp9cd0Q9s'
# Метки
face_lst=[
["Bill Gates",'people/gates500.jpg'],
["Jeff Besoz",'people/bezos500.jpg'],
["Mark Zuckerberg", 'people/zuckerberg500.jpg'],
["Steve Jobs",'people/jobs500.jpg']
]
# Датасет скачивается во время выполнения программы
import wget
from zipfile import ZipFile
# os.mkdir('data')
# url = 'https://drive.google.com/uc?export=download&id=120xqh0mYtYZ1Qh7vr-XFzjPbSKivLJjA'
# file_name = wget.download(url, 'data/')
#
# with ZipFile(file_name, 'r') as zip_file:
# zip_file.extractall()
#
# link_lst = [musk, gates, bezos, zuker, jobs]
# for link in link_lst:
# wget.download(link, 'data/')
# Датасет для тренировки
train_dataset = ImageFolder(
root='people/train'
)
# Датасет для проверки
valid_dataset = ImageFolder(
root='people/valid'
)
# augmentations (ухудшение качество чтобы не было переобучения)
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
train_dataset.transform = transforms.Compose([
transforms.Resize([70, 70]),
transforms.RandomHorizontalFlip(),
transforms.RandomAutocontrast(),
transforms.RandomEqualize(),
transforms.ToTensor(),
normalize
])
valid_dataset.transform = transforms.Compose([
transforms.Resize([70, 70]),
transforms.ToTensor(),
normalize
])
# Размер каждого пакета при обучение
# Training data loaders.
train_loader = DataLoader(
train_dataset, batch_size=BATCH_SIZE,
shuffle=True
)
# Размер каждого пакета при проверке
# Validation data loaders.
valid_loader = DataLoader(
valid_dataset, batch_size=BATCH_SIZE,
shuffle=False
)
# Указание на используемую модель
def google(): # pretrained=True для tensorflow
model = models.googlenet(weights=models.GoogLeNet_Weights.IMAGENET1K_V1)
# Добавление линейного (выходного) слоя на основании которого идет дообучение
model.fc = torch.nn.Linear(1024, len(train_dataset.classes))
for param in model.parameters():
param.requires_grad = True
# Заморозка весов т.к. при переобучении модели они должны быть постоянны, а меняться будет только последний слой
model.inception3a.requires_grad = False
model.inception3b.requires_grad = False
model.inception4a.requires_grad = False
model.inception4b.requires_grad = False
model.inception4c.requires_grad = False
model.inception4d.requires_grad = False
model.inception4e.requires_grad = False
return model
# Функция обучения модели. Epoch - количество итераций обучения (прогонов по нейросети)
def train(model, optimizer, train_loader, val_loader, epoch=10):
loss_train, acc_train = [], []
loss_valid, acc_valid = [], []
# tqdm - прогресс бар
for epoch in tqdm(range(epoch)):
# Ошибки
losses, equals = [], []
torch.set_grad_enabled(True)
# Train. Обучение. В цикле проходится по картинкам и оптимизируются потери
model.train()
for i, (image, target) in enumerate(train_loader):
image = image.to(device)
target = target.to(device)
output = model(image)
loss = criterion(output,target)
losses.append(loss.item())
equals.extend(
[x.item() for x in torch.argmax(output, 1) == target])
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Метрики отображающие резултитаты обучения модели
loss_train.append(np.mean(losses))
acc_train.append(np.mean(equals))
losses, equals = [], []
torch.set_grad_enabled(False)
# Validate. Оценка качества обучения
model.eval()
for i , (image, target) in enumerate(valid_loader):
image = image.to(device)
target = target.to(device)
output = model(image)
loss = criterion(output,target)
losses.append(loss.item())
equals.extend(
[y.item() for y in torch.argmax(output, 1) == target])
loss_valid.append(np.mean(losses))
acc_valid.append(np.mean(equals))
return loss_train, acc_train, loss_valid, acc_valid
criterion = torch.nn.CrossEntropyLoss()
criterion = criterion.to(device)
model = google() # здесь можете заменить на VGG
print('Model: GoogLeNet\n')
# оптимайзер - отвечает за поиск и подбор оптимальных весов
optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)
model = model.to(device)
loss_train, acc_train, loss_valid, acc_valid = train(
model, optimizer, train_loader, valid_loader, 30)
print('acc_train:', acc_train, '\nacc_valid:', acc_valid)
# Сохранение модели в текущую рабочую директорию
pkl_filename = "model/pickle_model.pkl"
with open(pkl_filename, 'wb') as file:
pickle.dump(model, file)
|