๐๐โ๐พ๐๐๐๐
- python -m venv venv
- venv/scripts/activate
- pip install -qr https://raw.githubusercontent.com/ultralytics/yolov5/master/requirements.txt
import torch
import cv2
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
img = cv2.imread('aa.jpeg')
print(img.shape)
results = model(img)
results.save()
result = results.pandas().xyxy[0].to_numpy()
result = [item for item in result if item[6]=='person']
print(result)
#
print(len(result))
# 5
tmp_img = cv2.imread('aa.jpeg')
for i in range(len(result)):
cv2.rectangle(tmp_img, (int(results.xyxy[0][i][0].item()), int(results.xyxy[0][i][1].item())),
(int(results.xyxy[0][i][2].item()), int(results.xyxy[0][i][3].item())),
(255,255,255))
cv2.imwrite('result1.png', tmp_img)
for i in range(len(result)):
cropped = tmp_img[int(result[i][1]):int(result[i][3]), # ymin:ymax
int(result[i][0]):int(result[i][2]) # xmin:xmax
]
i+=1
filename = 'people%i.png'%i # %i ๋ถ๋ถ์ ์ซ์๊ฐ ๋ค์ด๊ฐ
cv2.imwrite(filename, cropped)
์ถ๋ ฅ๊ฒฐ๊ณผ
(837, 1024, 3)
[
array([432.42706298828125, 155.5716552734375, 631.0242919921875, 804.8375854492188, 0.9132900238037109, 0, 'person'], dtype=object),
array([620.9993896484375, 154.4466552734375, 790.0960693359375, 806.7845458984375, 0.9080604910850525, 0, 'person'], dtype=object),
array([762.9304809570312, 127.33479309082031, 951.8087768554688, 813.9844970703125, 0.8943580985069275, 0, 'person'], dtype=object),
array([274.86883544921875, 174.7275390625, 457.47015380859375, 805.7642822265625, 0.8802736401557922, 0, 'person'], dtype=object),
array([98.14337158203125, 109.3960189819336, 310.7022399902344, 816.4161987304688, 0.8792229890823364, 0, 'person'], dtype=object)
]
5
git init
git remote add origin https://github.com/hyojine/1013test.git
git remote -v
โ
origin https://github.com/hyojine/1013test.git (fetch)
origin https://github.com/hyojine/1013test.git (push)
git add .
git commit -m'1013test'
Author identity unknown
*** Please tell me who you are.
Rungit config --global user.email "you@example.com"
git config --global user.name "Your Name"to set your account's default identity.
โ ๋๋ค ๋ณต์ฌํด์ ๋ฐ์ดํ ์์ ์์ฑํด์ ๋ฃ์ผ๋ฉด ๊นํ ์ฐ๊ฒฐํ๋ ์ฐฝ ๋จ๊ณ ๊น ์ฐ๊ฒฐ ์๋ฃ์ค์ค์ค์คใ ์ค์ค์น์ค์น
perceptron
Deep Feed Forward Network : RNN, LSTM์ผ๋ก ํ์
Deep Convolutional Network(DCN) : ์ด๋ฏธ์ง ์ฒ๋ฆฌ์ ํนํ๋จ
ํํฐ๋ฅผ ์ฌ๋ฌ๊ฐ ์ฌ์ฉํ๋ฉด ํฉ์ฑ๊ณฑ ์ ๊ฒฝ๋ง์ ์ฑ๋ฅ์ ๋์ผ ์ ์๋ค
์ด๋ฏธ์ง๋ 3์ฐจ์(๊ฐ๋ก, ์ธ๋ก, ์ฑ๋)
ex) ์
๋ ฅ์ด๋ฏธ์ง ํฌ๊ธฐ(10,10,3) - ํํฐ์ ํฌ๊ธฐ(4,4,3) - ํํฐ์ ๊ฐฏ์ 2 - ์ถ๋ ฅ(ํน์ฑ๋งต)์ ํฌ๊ธฐ(10,10,2)
โ ํํฐ์ ์์ ์์ํ(ํผ์ณ๋งต) ์ฑ๋ ์ ๋์ผํจ
ํฉ์ฑ๊ณฑ(convolution) ๋ ์ด์ด์ ์์ ์ฐ๊ฒฐ(fully connected=dense) ๋ ์ด์ด๋ฅผ ํจ๊ป ์ฌ์ฉ
ํฉ์ฑ๊ณฑ ๋ ์ด์ด(activator :Relu)์ ๊ฒฐ๊ณผ๊ฐ ํน์ฑ๋งต + pooling(=subsampling)์ ๋ฐ๋ณตํ๋ฉด์ ํฌ๊ธฐ๊ฐ ์์์ง๋ฉด์ ํต์ฌ์ ์ธ ํน์ฑ๋ค์ ์ถ์ถํด๋
pooling: ํน์ฑ๋งต์ ์ค์ํ ๋ถ๋ถ์ ์ถ์ถ, ์ ์ฅ
flatten layer : ๋ง์ง๋ง ํ๋ง ๋ ์ด์ด๋ฅผ ์ง๋๋ฉด FCL(Dense)์ ์ฐ๊ฒฐ๋์ด์ผํ๋๋ฐ ํ๋ง์ ํต๊ณผํ ํน์ฑ๋งต์ 2์ฐจ์, FCL์ 1์ฐจ์์ด๋ฏ๋ก ์ฐ์ฐํ๊ธฐ์ํด 2์ฐจ์ โ 1์ฐจ์์ผ๋ก ๋ฐ๊ฟ์ค๋ค(flattening)
flatten์ ๊ฑฐ์น ๋ ์ด์ด๋ flc์ ํ๋ ฌ๊ณฑ์ ์ ํ๊ณ ์ดํ ์์ ์ฐ๊ฒฐ๊ณ์ธต๊ณผ ํ์ฑํํจ์์ ๋ฐ๋ณต์ ํตํด ๋ ธ๋ ๊ฐฏ์๋ฅผ ์ค์ด๊ณ ๋ง์ง๋ง์ softmaxํ์ฑํ ํจ์๋ฅผ ํต๊ณผํด์ ๊ฒฐ๊ณผ๋ฌผ ์์ฑ
ex) ์์จ์ฃผํ ๋ฌผ์ฒด ์ธ์, ์์ธ ์ธ์(์ ์คํธ๋์ค), ํ์ง ๊ฐ์ (super-resolution), style transfer(์ฌ์ง์ ํํฐ ์ ์ฉํ๊ธฐ)
(์ง๋๋ฒ์ deep neural network MLP๋ก ํ๋)
https://colab.research.google.com/drive/1x2SRHEAdRqNHTMKvVn8oUSwF9KV9Vi4C?usp=sharing#scrollTo=I_strLH75R_x
๋ฐํ์Runtime - ๋ฐํ์ ์ ํ ๋ณ๊ฒฝChange runtime type - GPU ์ ํ(์ฐ์ฐ์๋ ๋๋ฆฌ๊ธฐ)
import os
os.environ['KAGGLE_USERNAME'] = 'username' # username
os.environ['KAGGLE_KEY'] = 'key' # key
!kaggle datasets download -d datamunge/sign-language-mnist
!unzip sign-language-mnist.zip
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense, Conv2D, MaxPooling2D, Flatten, Dropout
from tensorflow.keras.optimizers import Adam, SGD
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import OneHotEncoder
#๋ฐ์ดํฐ์
๋ก๋ํ๊ธฐ
train_df = pd.read_csv('sign_mnist_train.csv')
test_df = pd.read_csv('sign_mnist_test.csv')
# ๋ผ๋ฒจ ๋ถํฌ ํ์ธํ๊ธฐ(์ด 24๊ฐ)
plt.figure(figsize=(16, 10))
sns.countplot(train_df['label'])
plt.show()
# ์ ์ฒ๋ฆฌ
# ์
๋ ฅ๊ณผ ์ถ๋ ฅ ๋๋๊ธฐ
# ํ๋ จ ๋ฐ์ดํฐ์
train_df = train_df.astype(np.float32)
#32๋นํธ๋ก ๋ฐ๊พธ๋๊ฑด ํ์
x_train = train_df.drop(columns=['label'], axis=1).values
# ๋ผ๋ฒจ ๋นผ๊ณ ๋ ๋ชจ๋ x
x_train = x_train.reshape((-1, 28, 28, 1))
# (๋ฐฐ์น์ฌ์ด์ฆ, ์ด๋ฏธ์ง ๊ฐ๋ก, ์ธ๋ก, ๊ทธ๋ ์ด์ค์ผ์ผ)
# ์ด๋ฏธ์ง ๊ฐ๋ก, ์ธ๋ก, ์์๊น์งํด์ 3์ฐจ์
# -> xdata๋ฅผ (28,28,1) ํฌ๊ธฐ์ ์ด๋ฏธ์ง ํํ๋ก ๋ณํ
y_train = train_df[['label']].values
# ๊ฒ์ฆ ๋ฐ์ดํฐ์
test_df = test_df.astype(np.float32)
x_test = test_df.drop(columns=['label'], axis=1).values
x_test = x_test.reshape((-1, 28, 28, 1))
y_test = test_df[['label']].values
print(x_train.shape, y_train.shape) # (27455, 28, 28, 1) (27455, 1)
print(x_test.shape, y_test.shape) # (7172, 28, 28, 1) (7172, 1)
# ๋ฐ์ดํฐ ๋ฏธ๋ฆฌ๋ณด๊ธฐ
index = 1
plt.title(str(y_train[index]))
plt.imshow(x_train[index].reshape((28, 28)), cmap='gray')
plt.show()
# one-hot ์ธ์ฝ๋ฉํ๊ธฐ
encoder = OneHotEncoder()
y_train = encoder.fit_transform(y_train).toarray()
y_test = encoder.fit_transform(y_test).toarray()
print(y_train.shape) # (27455, 24)
# ์ผ๋ฐํํ๊ธฐ
# ์ด๋ฏธ์ง ๋ฐ์ดํฐ๋ฅผ 255๋ก ๋๋์ด 0-1 ์ฌ์ด์ ์์์ ๋ฐ์ดํฐ(floating point 32bit = float32)๋ก ๋ฐ๊พธ๊ธฐ
# ImageDataGenerator() ์ด์ฉ
train_image_datagen = ImageDataGenerator(
rescale=1./255, # ์ผ๋ฐํ
)
train_datagen = train_image_datagen.flow(
x=x_train,
y=y_train,
batch_size=256,
shuffle=True
)
# flow๋ฅผ ์ด์ฉํด ์์ฐจ์ ์ผ๋ก ๊ฐ์ ๋ฃ์ด์ค๋ค
test_image_datagen = ImageDataGenerator(
rescale=1./255
)
test_datagen = test_image_datagen.flow(
x=x_test,
y=y_test,
batch_size=256,
shuffle=False # ๋๋ค์ฑ์ ์์ฐ
)
index = 1
preview_img = train_datagen.__getitem__(0)[0][index]
# __getitem__
preview_label = train_datagen.__getitem__(0)[1][index]
plt.imshow(preview_img.reshape((28, 28)))
plt.title(str(preview_label))
plt.show()
input = Input(shape=(28, 28, 1))
# conv๋ pooling์ด๋ ํ์์ด ํ๋ธ๋ญ
hidden = Conv2D(filters=32, kernel_size=3, strides=1, padding='same', activation='relu')(input)
hidden = MaxPooling2D(pool_size=2, strides=2)(hidden)
hidden = Conv2D(filters=64, kernel_size=3, strides=1, padding='same', activation='relu')(hidden)
hidden = MaxPooling2D(pool_size=2, strides=2)(hidden)
hidden = Conv2D(filters=32, kernel_size=3, strides=1, padding='same', activation='relu')(hidden)
hidden = MaxPooling2D(pool_size=2, strides=2)(hidden)
# ๋ง์ง๋ง ํ๋ง์ ๊ฑฐ์น๊ณ ๋๋ฉด
# 1์ฐจ์์ผ๋ก ํ์ด์ค์ผ dense์ ์ฐ์ฐ ๊ฐ๋ฅ
hidden = Flatten()(hidden)
# ์ฌ๊ธฐ๋ถํด deep neural network๋ ๋์ผ(๋
ธ๋์, ํ์ฑํจ์)
hidden = Dense(512, activation='relu')(hidden)
hidden = Dropout(rate=0.3)(hidden)
# ๋
ธ๋์ 30%๋ฅผ ๋๋ค์ผ๋ก ๋บ๋ค
output = Dense(24, activation='softmax')(hidden)
model = Model(inputs=input, outputs=output)
model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=0.001), metrics=['acc'])
model.summary()
โ ํฌ๊ธฐ๋ ์ ์์ฌ์ผํ๋ฏ๋ก 7->3 ์ผ๋ก ์ค์ด๋ค๋ ๋๋จธ์ง๋ ๋ฒ๋ฆผ
# ํ์ต์ํค๊ธฐ
history = model.fit(
train_datagen,
validation_data=test_datagen, # ๊ฒ์ฆ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ด์ฃผ๋ฉด ํ epoch์ด ๋๋ ๋๋ง๋ค ์๋์ผ๋ก ๊ฒ์ฆ
epochs=20 # epochs ๋ณต์ํ์ผ๋ก ์ฐ๊ธฐ!
)
# ํ์ต๊ฒฐ๊ณผ ๊ทธ๋ํ
fig, axes = plt.subplots(1, 2, figsize=(20, 6))
axes[0].plot(history.history['loss'])
axes[0].plot(history.history['val_loss'])
axes[1].plot(history.history['acc'])
axes[1].plot(history.history['val_acc'])
โญ์ด๋ฏธ์ง ์ฆ๊ฐ ๊ธฐ๋ฒ data augmentation ์ด์ฉํด๋ณด๊ธฐโญ
#ImageDataGenerator๋ฅผ ์ด์ํด์ ์ด๋ฏธ์ง ์ฆ๊ฐ์ ์ฝ๊ฒ ํ ์ ์๋ค
train_image_datagen = ImageDataGenerator(
rescale=1./255, # ์ผ๋ฐํ(์๋ ์ด๊ฒ๋งํ์๋๋ฐ)
rotation_range=10, # ๋๋คํ๊ฒ ์ด๋ฏธ์ง๋ฅผ ํ์ (๋จ์: ๋, 0-180)
zoom_range=0.1, # ๋๋คํ๊ฒ ์ด๋ฏธ์ง ํ๋ (%)
width_shift_range=0.1, # ๋๋คํ๊ฒ ์ด๋ฏธ์ง๋ฅผ ์ํ์ผ๋ก ์ด๋ (%)
height_shift_range=0.1, # ๋๋คํ๊ฒ ์ด๋ฏธ์ง๋ฅผ ์์ง์ผ๋ก ์ด๋ (%)
)
train_datagen = train_image_datagen.flow(
x=x_train,
y=y_train,
batch_size=256,
shuffle=True
)
test_image_datagen = ImageDataGenerator(
rescale=1./255
)
# ๊ฒ์ฆ/ํ
์คํธ ๋ฐ์ดํฐ๋ augmentations์์ด๋ ๋๊ธฐ๋ ํ๊ณ ์์ผ๋ฉด ๊ฒ์ฆ๋๋ง๋ค ๊ฒฐ๊ณผ๊ฐ ๋ฌ๋ผ์ ธ์ ๊ฒฐ๊ณผ๊ฐ ๊ฐ๊ด์ฑ์ ์์
test_datagen = test_image_datagen.flow(
x=x_test,
y=y_test,
batch_size=256,
shuffle=False
)
index = 1
preview_img = train_datagen.__getitem__(0)[0][index]
preview_label = train_datagen.__getitem__(0)[1][index]
plt.imshow(preview_img.reshape((28, 28)))
plt.title(str(preview_label))
plt.show()
input = Input(shape=(28, 28, 1))
hidden = Conv2D(filters=32, kernel_size=3, strides=1, padding='same', activation='relu')(input)
hidden = MaxPooling2D(pool_size=2, strides=2)(hidden)
hidden = Conv2D(filters=64, kernel_size=3, strides=1, padding='same', activation='relu')(hidden)
hidden = MaxPooling2D(pool_size=2, strides=2)(hidden)
hidden = Conv2D(filters=32, kernel_size=3, strides=1, padding='same', activation='relu')(hidden)
hidden = MaxPooling2D(pool_size=2, strides=2)(hidden)
hidden = Flatten()(hidden)
hidden = Dense(512, activation='relu')(hidden)
hidden = Dropout(rate=0.3)(hidden)
output = Dense(24, activation='softmax')(hidden)
model = Model(inputs=input, outputs=output)
model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=0.001), metrics=['acc'])
model.summary()
history = model.fit(
train_datagen,
validation_data=test_datagen,
epochs=20
)
fig, axes = plt.subplots(1, 2, figsize=(20, 6))
axes[0].plot(history.history['loss'])
axes[0].plot(history.history['val_loss'])
axes[1].plot(history.history['acc'])
axes[1].plot(history.history['val_acc'])