顔認識の具体例(李智恩)
35908 ワード
プロジェクト環境:python 3.6
一、プロジェクト構造
二、データセット準備
データセットの準備は2ステップに分けられます。写真を取得します。 .人の顔を抽出する。
1、画像を取得する
まず爬虫類を利用して、百度のピクチャーの上から大量にピクチャーをダウンロードすることができて、しかしデータ集の使うキーワードをダウンロードしてと後で任務のキーワードを識別しないでくださいに注意して、さもなくばピクチャーが重なるならば、“認識がとても正確です”の錯覚を生みます。下のプログラムは爬虫類の部分で、name.txtファイルの中で検索するキーワードを書いて、すぐ使えます。
各フォルダ内は下図の形式です。
IUフォルダ内の画像は以下の通りです。
フォルダ内のファイルの名前は、以下のプログラムを利用して順番に名前を変更できます。
人の顔を引き出すには、一人の顔認識庫face_を使う必要があります。レcognitionライブラリface_recognitionライブラリのダウンロード手順を参照してください。
https://www.jb51.net/article/209870.htm
主に三つのステップがあります。直接にアナコンダのコマンドラインインターフェースでコピーして使用できます。 pip install CMake-ihttps://pypi.douban.com/simple pip install dlib==19.7.0-ihttps://pypi.douban.com/simple pip install face uレcognition-ihttps://pypi.douban.com/simple 試してみましたが、確かに使えます。
下記の関数を使うと、画像に対応する顔が得られます。戻り値は顔写真です。
二つのフォルダの内部構造は全く同じです。
三、ネットワークモデル
1、画像処理
画像を112に切り取ります。×92サイズで、RGB画像を使用しています。(ここではグレースケール画像を使ってみましたが、効果がよくないようです。あきらめました。)画像を正規化しています。
1.get_face.py
全体としては新人の興味作ですが、GPUに限られては機能が複雑すぎてネットが使えなくなり、最後に認識される効果がよくないので、読者が興味があれば、ネットを変えて、データセットを改善して、識別性能を向上させてみてください。より多くの関系者の颜の认识内容は、私たちの前の文章を検索したり、下の関连した文章を読み続けてください。これからも応援してください。
一、プロジェクト構造
二、データセット準備
データセットの準備は2ステップに分けられます。
1、画像を取得する
まず爬虫類を利用して、百度のピクチャーの上から大量にピクチャーをダウンロードすることができて、しかしデータ集の使うキーワードをダウンロードしてと後で任務のキーワードを識別しないでくださいに注意して、さもなくばピクチャーが重なるならば、“認識がとても正確です”の錯覚を生みます。下のプログラムは爬虫類の部分で、name.txtファイルの中で検索するキーワードを書いて、すぐ使えます。
# , name + ‘ '
#############################################################################################
if GET_PIC == 1:
headers = {
'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
'Connection': 'keep-alive',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0',
'Upgrade-Insecure-Requests': '1'
}
A = requests.Session()
A.headers = headers
tm = int(input(' '))
numPicture = tm
line_list = []
with open('./name.txt', encoding='utf-8') as file:
line_list = [k.strip() for k in file.readlines()] # strip()
for word in line_list:
url = 'https://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=' + word + '&pn='
tot = Find(url, A)
Recommend = recommend(url) #
print(' %s %d ' % (word, tot))
file = word + ' '
y = os.path.exists(file)
if y == 1:
print(' , ')
else:
os.mkdir(file)
t = 0
tmp = url
while t < numPicture:
try:
url = tmp + str(t)
# result = requests.get(url, timeout=10)
#
result = A.get(url, timeout=10, allow_redirects=False)
print(url)
except error.HTTPError as e:
print(' , ')
t = t + 60
else:
dowmloadPicture(result.text, word)
t = t + 60
numPicture = numPicture + tm
print(' , ')
#############################################################################################
画像をダウンロードする時は区分に注意して、IUの写真を一つのフォルダの下に置いて、Otherのは別のフォルダの下に置きます。トレーニングセットとテストセットは全部そうです。下図のように:各フォルダ内は下図の形式です。
IUフォルダ内の画像は以下の通りです。
フォルダ内のファイルの名前は、以下のプログラムを利用して順番に名前を変更できます。
import os
raw_train_root_1 = 'E:/Table/ /find_iu/data/raw/train/IU/'
raw_train_root_2 = 'E:/Table/ /find_iu/data/raw/train/Other/'
raw_test_root_1 = 'E:/Table/ /find_iu/data/raw/test/IU/'
raw_test_root_2 = 'E:/Table/ /find_iu/data/raw/test/Other/'
raw_roots = [raw_train_root_1, raw_train_root_2, raw_test_root_1, raw_test_root_2]
for path in raw_roots:
# ,
fileList = os.listdir(path)
n = 0
for i in fileList:
# ( + )
oldname = path + os.sep + fileList[n] # os.sep
#
newname = path + os.sep + str(n) + '.JPG'
os.rename(oldname, newname) # os rename
print(oldname, '======>', newname)
n += 1
2.顔を引き出す人の顔を引き出すには、一人の顔認識庫face_を使う必要があります。レcognitionライブラリface_recognitionライブラリのダウンロード手順を参照してください。
https://www.jb51.net/article/209870.htm
主に三つのステップがあります。直接にアナコンダのコマンドラインインターフェースでコピーして使用できます。
下記の関数を使うと、画像に対応する顔が得られます。戻り値は顔写真です。
#
#############################################################################################
def find_face(path):
# Load the jpg file into a numpy array
image = face_recognition.load_image_file(path)
# Find all the faces in the image using the default HOG-based model.
# This method is fairly accurate, but not as accurate as the CNN model and not GPU accelerated.
# See also: find_faces_in_picture_cnn.py
face_locations = face_recognition.face_locations(image) # model="cnn"
if len(face_locations) == 0:
return None
else:
for face_location in face_locations:
# Print the location of each face in this image
top, right, bottom, left = face_location
# You can access the actual face itself like this:
face_image = image[top:bottom, left:right]
pil_image = Image.fromarray(face_image)
return pil_image
#############################################################################################
データセットを操作すると、処理後の顔画像が得られます。人物図トレーニングではなく、人の顔を取り出してから訓練するのは、人物画像の中の妨害要素が多すぎること、そしてテスト後に認識の効果が非常に悪いことを考慮して、人の顔を抽出するコーナーに参加するからです。データセットの操作コードは以下の通りです。
# raw ,
#############################################################################################
if __name__ == '__main__': #
raw_train_root_1 = 'E:/Table/ /find_iu/data/raw/train/IU/'
raw_train_root_2 = 'E:/Table/ /find_iu/data/raw/train/Other/'
raw_test_root_1 = 'E:/Table/ /find_iu/data/raw/test/IU/'
raw_test_root_2 = 'E:/Table/ /find_iu/data/raw/test/Other/'
raw_roots = [raw_train_root_1, raw_train_root_2, raw_test_root_1, raw_test_root_2]
img_raw_train_1 = os.listdir(raw_train_root_1)
img_raw_train_2 = os.listdir(raw_train_root_2)
img_raw_test_1 = os.listdir(raw_test_root_1)
img_raw_test_2 = os.listdir(raw_test_root_2)
img_raws = [img_raw_train_1, img_raw_train_2, img_raw_test_1, img_raw_test_2]
new_path_train_1 = 'E:/Table/ /find_iu/data/processed/train/IU/'
new_path_train_2 = 'E:/Table/ /find_iu/data/processed/train/Other/'
new_path_test_1 = 'E:/Table/ /find_iu/data/processed/test/IU/'
new_path_test_2 = 'E:/Table/ /find_iu/data/processed/test/Other/'
new_paths = [new_path_train_1, new_path_train_2, new_path_test_1, new_path_test_2]
for raw_root, img_raw, new_path in zip(raw_roots, img_raws, new_paths):
n = 0
for i in range(len(img_raw)):
try:
img = Image.open(raw_root + img_raw[i])
except:
print('a file error, continue')
continue
else:
img_train = find_face(raw_root + img_raw[i])
if img_train == None:
continue
else:
# img_train.save(new_path + '%d.JPG'%n)
# print(raw_root + img_raw[i])
n += 1
print(' %d , %d ' % (len(img_raw), n))
#############################################################################################
処理前の画像データは全部rawフォルダに保存されています。処理後はprocessedフォルダに保存されています。二つのフォルダの内部構造は全く同じです。
三、ネットワークモデル
1、画像処理
画像を112に切り取ります。×92サイズで、RGB画像を使用しています。(ここではグレースケール画像を使ってみましたが、効果がよくないようです。あきらめました。)画像を正規化しています。
data_transform = transforms.Compose([
# transforms.Grayscale(num_output_channels=1), # num_output_channels 1
transforms.Resize(112),
transforms.CenterCrop((112, 92)), # 112*92
transforms.ToTensor(),
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
# transforms.Normalize(mean=0.5, std=0.5)
])
双晶神経ネットワーク(Siamese Network)を使用します。
class SiameNetwork(nn.Module):
def __init__(self):
super(SiameNetwork, self).__init__()
# input: h=112, w=92
self.conv1 = torch.nn.Sequential(
torch.nn.Conv2d(in_channels=3, #
out_channels=16, # 16 3*3
kernel_size=3, #
stride=2, # , 1 ,2 (h/2)*(w/2)
padding=1), # , ,kernel_size//2
torch.nn.BatchNorm2d(16), # , 16
torch.nn.ReLU() #
) # output: h=56, w=46
self.conv2 = torch.nn.Sequential(
torch.nn.Conv2d(16, 32, 3, 2, 1),
torch.nn.BatchNorm2d(32),
torch.nn.ReLU()
) # output: h=28, w=23
self.conv3 = torch.nn.Sequential(
torch.nn.Conv2d(32, 64, 3, 2, 1),
torch.nn.BatchNorm2d(64),
torch.nn.ReLU()
) # output: h=14, w=12
self.conv4 = torch.nn.Sequential(
torch.nn.Conv2d(64, 64, 2, 2, 0),
torch.nn.BatchNorm2d(64),
torch.nn.ReLU()
) # output: h=7, w=6
self.mlp1 = torch.nn.Linear(7 * 6 * 64, 100) # conv4 , (size - kernal + 2*padding)/stride + 1
self.mlp2 = torch.nn.Linear(100, 10)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x = self.conv4(x)
x = self.mlp1(x.view(x.size(0), -1)) # view
x = self.mlp2(x)
return x
四、具体的なコード1.get_face.py
from PIL import Image
import face_recognition
import os
#
#############################################################################################
def find_face(path):
# Load the jpg file into a numpy array
image = face_recognition.load_image_file(path)
# Find all the faces in the image using the default HOG-based model.
# This method is fairly accurate, but not as accurate as the CNN model and not GPU accelerated.
# See also: find_faces_in_picture_cnn.py
face_locations = face_recognition.face_locations(image) # model="cnn"
if len(face_locations) == 0:
return None
else:
for face_location in face_locations:
# Print the location of each face in this image
top, right, bottom, left = face_location
# You can access the actual face itself like this:
face_image = image[top:bottom, left:right]
pil_image = Image.fromarray(face_image)
return pil_image
#############################################################################################
# raw ,
#############################################################################################
if __name__ == '__main__': #
raw_train_root_1 = 'E:/Table/ /find_iu/data/raw/train/IU/'
raw_train_root_2 = 'E:/Table/ /find_iu/data/raw/train/Other/'
raw_test_root_1 = 'E:/Table/ /find_iu/data/raw/test/IU/'
raw_test_root_2 = 'E:/Table/ /find_iu/data/raw/test/Other/'
raw_roots = [raw_train_root_1, raw_train_root_2, raw_test_root_1, raw_test_root_2]
img_raw_train_1 = os.listdir(raw_train_root_1)
img_raw_train_2 = os.listdir(raw_train_root_2)
img_raw_test_1 = os.listdir(raw_test_root_1)
img_raw_test_2 = os.listdir(raw_test_root_2)
img_raws = [img_raw_train_1, img_raw_train_2, img_raw_test_1, img_raw_test_2]
new_path_train_1 = 'E:/Table/ /find_iu/data/processed/train/IU/'
new_path_train_2 = 'E:/Table/ /find_iu/data/processed/train/Other/'
new_path_test_1 = 'E:/Table/ /find_iu/data/processed/test/IU/'
new_path_test_2 = 'E:/Table/ /find_iu/data/processed/test/Other/'
new_paths = [new_path_train_1, new_path_train_2, new_path_test_1, new_path_test_2]
for raw_root, img_raw, new_path in zip(raw_roots, img_raws, new_paths):
n = 0
for i in range(len(img_raw)):
try:
img = Image.open(raw_root + img_raw[i])
except:
print('a file error, continue')
continue
else:
img_train = find_face(raw_root + img_raw[i])
if img_train == None:
continue
else:
# img_train.save(new_path + '%d.JPG'%n)
# print(raw_root + img_raw[i])
n += 1
print(' %d , %d ' % (len(img_raw), n))
#############################################################################################
2.find_iu.py
import torch
import torchvision
import torch.nn as nn
from torch.autograd import Variable
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
import cv2 #opencv ,
import numpy as np
import os
from utils import draw_result
from network import SiameNetwork
from get_face import find_face
if __name__ == '__main__': #
#
#############################################################################################
path = 'E:/Table/ /find_iu/result/' #
epochs = 20 #
BATCH_SIZE = 16 #
NUM_WORKERS = 0
#############################################################################################
#
#############################################################################################
data_transform = transforms.Compose([
# transforms.Grayscale(num_output_channels=1), # num_output_channels 1
transforms.Resize(112),
transforms.CenterCrop((112, 92)), # 112*92
transforms.ToTensor(),
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
# transforms.Normalize(mean=0.5, std=0.5)
])
train_dataset = datasets.ImageFolder(root = r'E:/Table/ /find_iu/data/processed/train',
transform = data_transform)
test_dataset = datasets.ImageFolder(root = r'E:/Table/ /find_iu/data/processed/test',
transform = data_transform)
train_loader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=NUM_WORKERS)
test_loader = DataLoader(test_dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=NUM_WORKERS)
image, labels = next(iter(train_loader)) #
img = torchvision.utils.make_grid(image, nrow = 10)
img = img.numpy().transpose(1, 2, 0)
cv2.imshow('img', img) #
cv2.waitKey(0) #
print("data ready!")
#############################################################################################
# 、
#############################################################################################
device = torch.device('cuda')
model = SiameNetwork().to(device)
cost = torch.nn.CrossEntropyLoss() # ,
optimizer = torch.optim.Adam(model.parameters(), lr=0.0008, weight_decay=0.001) #Adam
print("device ready!")
#############################################################################################
# , epochs
#############################################################################################
draw_epoch = [] #
draw_loss = [] # ,
draw_train_acc = [] # ,
draw_val_loss = [] # ,
draw_val_acc = [] # ,
for epoch in range(epochs):
#
sum_loss = 0.0
sum_val_loss = 0.0
train_correct = 0
test_correct = 0
for data in train_loader:
inputs,labels = data
inputs,labels = Variable(inputs).cuda(),Variable(labels).cuda()
optimizer.zero_grad() # batch
outputs = model(inputs)
loss = cost(outputs, labels)
loss.backward() #
optimizer.step()
_, id = torch.max(outputs.data, 1)
sum_loss += loss.data
train_correct += torch.sum(id == labels.data)
for data in test_loader: #
inputs,labels = data
inputs,labels = Variable(inputs).cuda(),Variable(labels).cuda()
outputs = model(inputs)
val_loss = cost(outputs, labels)
_,id = torch.max(outputs.data, 1)
sum_val_loss += val_loss.data
test_correct += torch.sum(id == labels.data)
print('[%d,%d] train loss:%.03f train acc:%.03f%%'
%(epoch + 1, epochs, sum_loss / len(train_loader), (100 * train_correct / len(train_dataset))))
print(' val loss:%.03f val acc:%.03f%%'
%(sum_val_loss / len(test_loader), (100 * test_correct / len(test_dataset))))
draw_epoch.append(epoch+1) #
draw_loss.append(sum_loss / len(train_loader))
draw_train_acc.append(100 * train_correct / len(train_dataset))
draw_val_loss.append(sum_val_loss / len(test_loader))
draw_val_acc.append(100 * test_correct / len(test_dataset))
np.savetxt('%s/train_loss.txt'%(path), draw_loss, fmt="%.3f") #
np.savetxt('%s/train_acc.txt'%(path), draw_train_acc, fmt="%.3f") #
np.savetxt('%s/val_loss.txt'%(path), draw_val_loss, fmt="%.3f") #
np.savetxt('%s/val_acc.txt'%(path), draw_val_acc, fmt="%.3f") #
print("train ready!")
#############################################################################################
#
#############################################################################################
draw_result(draw_epoch, path) #
print("draw ready!")
#############################################################################################
#
#############################################################################################
torch.save(model.state_dict(), "parameter.pkl") #save
print("save ready!")
#############################################################################################
3.spider_iu.py
import re
import requests
from urllib import error
from bs4 import BeautifulSoup
import os
import torch
from torch.autograd import Variable
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
from network import SiameNetwork
from utils import cv_imread
import cv2
from PIL import Image
import shutil
from get_face import find_face
#
#############################################################################################
GET_PIC = 0 # 1 ,0
GET_FACE = 0
GET_IU = 1
#############################################################################################
num = 0
numPicture = 0
file = ''
List = []
#
#############################################################################################
def Find(url, A):
global List
print(' , .....')
t = 0
i = 1
s = 0
while t < 1000:
Url = url + str(t)
try:
#
Result = A.get(Url, timeout=7, allow_redirects=False)
except BaseException:
t = t + 60
continue
else:
result = Result.text
pic_url = re.findall('"objURL":"(.*?)",', result, re.S) # url
s += len(pic_url)
if len(pic_url) == 0:
break
else:
List.append(pic_url)
t = t + 60
return s
def recommend(url):
Re = []
try:
html = requests.get(url, allow_redirects=False)
except error.HTTPError as e:
return
else:
html.encoding = 'utf-8'
bsObj = BeautifulSoup(html.text, 'html.parser')
div = bsObj.find('div', id='topRS')
if div is not None:
listA = div.findAll('a')
for i in listA:
if i is not None:
Re.append(i.get_text())
return Re
def dowmloadPicture(html, keyword):
global num
# t =0
pic_url = re.findall('"objURL":"(.*?)",', html, re.S) # url
print(' :' + keyword + ' , ...')
for each in pic_url:
print(' ' + str(num + 1) + ' , :' + str(each))
try:
if each is not None:
pic = requests.get(each, timeout=7)
else:
continue
except BaseException:
print(' , ')
continue
else:
string = file + r'\\' + keyword + '_' + str(num) + '.jpg'
fp = open(string, 'wb')
fp.write(pic.content)
fp.close()
num += 1
if num >= numPicture:
return
#############################################################################################
if __name__ == '__main__': #
# , name + ‘ '
#############################################################################################
if GET_PIC == 1:
headers = {
'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
'Connection': 'keep-alive',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0',
'Upgrade-Insecure-Requests': '1'
}
A = requests.Session()
A.headers = headers
tm = int(input(' '))
numPicture = tm
line_list = []
with open('./name.txt', encoding='utf-8') as file:
line_list = [k.strip() for k in file.readlines()] # strip()
for word in line_list:
url = 'https://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=' + word + '&pn='
tot = Find(url, A)
Recommend = recommend(url) #
print(' %s %d ' % (word, tot))
file = word + ' '
y = os.path.exists(file)
if y == 1:
print(' , ')
else:
os.mkdir(file)
t = 0
tmp = url
while t < numPicture:
try:
url = tmp + str(t)
# result = requests.get(url, timeout=10)
#
result = A.get(url, timeout=10, allow_redirects=False)
print(url)
except error.HTTPError as e:
print(' , ')
t = t + 60
else:
dowmloadPicture(result.text, word)
t = t + 60
numPicture = numPicture + tm
print(' , ')
#############################################################################################
# raw , , file+' ' ‘ '
############################################################################################
if GET_FACE == 1:
if GET_PIC == 0:
file = ' '
raw_root = 'E:/Table/ /find_iu/'+ file + '/'
img_raw = os.listdir(raw_root)
new_path = 'E:/Table/ /find_iu/ /'
n = 0
for i in range(len(img_raw)):
try:
img = Image.open(raw_root + img_raw[i])
except:
print('a file error, continue')
continue
else:
img_train = find_face(raw_root + img_raw[i])
if img_train == None:
continue
else:
img_train.save(new_path + '%d.JPG' % n)
print(raw_root + img_raw[i])
n += 1
print(' %d , %d ' % (len(img_raw), n))
print(' , IU')
#############################################################################################
# , ' ‘ IU 'IU_pic‘
#############################################################################################
if GET_IU == 1:
data_transform = transforms.Compose([
# transforms.Grayscale(num_output_channels=1), # num_output_channels 1
transforms.Resize(112),
transforms.CenterCrop((112, 92)), # 112*92
transforms.ToTensor(),
transforms.Normalize(mean=[0.5, 0.5, 0.5],std=[0.5, 0.5, 0.5])
# transforms.Normalize(mean=0.5, std=0.5)
])
device = torch.device('cuda')
model = SiameNetwork().to(device)
model.load_state_dict(torch.load('parameter.pkl')) # load
model.eval()
judge_root = 'E:/Table/ /find_iu/ /'
img_judge = os.listdir(judge_root)
new_path = 'E:/Table/ /find_iu/IU_pic/'
result = []
n = 0
for i in range(len(img_judge)):
try:
img = Image.open(judge_root + img_judge[i])
except:
print('a file error, continue')
continue
else:
img = img.convert('RGB')
print(judge_root + img_judge[i])
input = data_transform(img)
input = input.unsqueeze(0) # input [C,H,W], B
# , img [1,C,H,W]
input = Variable(input.cuda())
output = model(input) #
_, id = torch.max(output.data, 1) # 0 IU,1
if id.item() == 0:
shutil.copy(judge_root + img_judge[i], new_path)
n += 1
print('/n %d , %d IU '%(len(img_judge), n))
#############################################################################################
4.file_deal.py
import os
raw_train_root_1 = 'E:/Table/ /find_iu/data/raw/train/IU/'
raw_train_root_2 = 'E:/Table/ /find_iu/data/raw/train/Other/'
raw_test_root_1 = 'E:/Table/ /find_iu/data/raw/test/IU/'
raw_test_root_2 = 'E:/Table/ /find_iu/data/raw/test/Other/'
raw_roots = [raw_train_root_1, raw_train_root_2, raw_test_root_1, raw_test_root_2]
for path in raw_roots:
# ,
fileList = os.listdir(path)
n = 0
for i in fileList:
# ( + )
oldname = path + os.sep + fileList[n] # os.sep
#
newname = path + os.sep + str(n) + '.JPG'
os.rename(oldname, newname) # os rename
print(oldname, '======>', newname)
n += 1
5.network.py
import torch
import torch.nn as nn
class SiameNetwork(nn.Module):
def __init__(self):
super(SiameNetwork, self).__init__()
# input: h=112, w=92
self.conv1 = torch.nn.Sequential(
torch.nn.Conv2d(in_channels=3, #
out_channels=16, # 16 3*3
kernel_size=3, #
stride=2, # , 1 ,2 (h/2)*(w/2)
padding=1), # , ,kernel_size//2
torch.nn.BatchNorm2d(16), # , 16
torch.nn.ReLU() #
) # output: h=56, w=46
self.conv2 = torch.nn.Sequential(
torch.nn.Conv2d(16, 32, 3, 2, 1),
torch.nn.BatchNorm2d(32),
torch.nn.ReLU()
) # output: h=28, w=23
self.conv3 = torch.nn.Sequential(
torch.nn.Conv2d(32, 64, 3, 2, 1),
torch.nn.BatchNorm2d(64),
torch.nn.ReLU()
) # output: h=14, w=12
self.conv4 = torch.nn.Sequential(
torch.nn.Conv2d(64, 64, 2, 2, 0),
torch.nn.BatchNorm2d(64),
torch.nn.ReLU()
) # output: h=7, w=6
self.mlp1 = torch.nn.Linear(7 * 6 * 64, 100) # conv4 , (size - kernal + 2*padding)/stride + 1
self.mlp2 = torch.nn.Linear(100, 10)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x = self.conv4(x)
x = self.mlp1(x.view(x.size(0), -1)) # view
x = self.mlp2(x)
return x
6.utils.py
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import cv2
# 、 、
#############################################################################################
def draw_result(draw_epoch, path):
show_loss = np.loadtxt('%s/train_loss.txt' % (path)) # txt ,
show_train_acc = np.loadtxt('%s/train_acc.txt' % (path)) #
show_val_loss = np.loadtxt('%s/val_loss.txt' % (path)) # txt ,
show_val_acc = np.loadtxt('%s/val_acc.txt' % (path)) #
mpl.rc('font',family='Times New Roman', weight='semibold', size=9) # matplotlib
font1 = {'weight' : 'semibold', 'size' : 11} #
fig = plt.figure(figsize = (7,5)) #figsize `
ax1 = fig.add_subplot(2, 2, 1) # ax1
ax1.plot(draw_epoch, show_loss,color = 'red', label = u'AdaPID', linewidth =1.0)
ax1.legend() #
ax1.set_title('Training Loss', font1)
ax1.set_xlabel(u'Epoch', font1)
ax2 = fig.add_subplot(2, 2, 2)
ax2.plot(draw_epoch, show_val_loss,color = 'red', label = u'Adam', linewidth =1.0)
ax2.legend() #
ax2.set_title('Validation Loss', font1)
ax2.set_xlabel(u'Epoch', font1)
ax3 = fig.add_subplot(2, 2, 3)
ax3.plot(draw_epoch, show_train_acc,color = 'red', label = u'Adam', linewidth =1.0)
ax3.legend() #
ax3.set_title('Training Accuracy', font1)
ax3.set_xlabel(u'Epoch', font1)
ax4 = fig.add_subplot(2, 2, 4)
ax4.plot(draw_epoch, show_val_acc,color = 'red', label = u'Adam', linewidth =1.0)
ax4.legend() #
ax4.set_title('Validation Accuracy', font1)
ax4.set_xlabel(u'Epoch', font1)
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=0.45) # hspace
plt.savefig('%s/show_curve.jpg' % (path), dpi=300)
#############################################################################################
# cv.imread
#############################################################################################
def cv_imread(filePath):
# , , mat
cv_img = cv2.imdecode(np.fromfile(filePath, dtype=np.uint8), -1)
# imdecode rgb, opencv , bgr,
# cv_img=cv2.cvtColor(cv_img,cv2.COLOR_RGB2BGR)
return cv_img
#############################################################################################
締め括りをつける全体としては新人の興味作ですが、GPUに限られては機能が複雑すぎてネットが使えなくなり、最後に認識される効果がよくないので、読者が興味があれば、ネットを変えて、データセットを改善して、識別性能を向上させてみてください。より多くの関系者の颜の认识内容は、私たちの前の文章を検索したり、下の関连した文章を読み続けてください。これからも応援してください。