๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
[Kaggle] CNN Architectures ๋ณธ๋ฌธ
220204 ์์ฑ
<๋ณธ ๋ธ๋ก๊ทธ๋ Kaggle ์ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค>
https://www.kaggle.com/shivamb/cnn-architectures-vgg-resnet-inception-tl
CNN Architectures : VGG, ResNet, Inception + TL
Explore and run machine learning code with Kaggle Notebooks | Using data from multiple data sources
www.kaggle.com
https://wooono.tistory.com/233
[DL] LeNet-5, AlexNet, VGG-16, ResNet, Inception Network
CNN ์ข ๋ฅ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค. Classic Networks LeNet-5 AlexNet VGG-16 ResNet Inception(GoogLeNet) Network ๋ค์ด๊ฐ๊ธฐ ์์, ์ ๋ ฅ ์ด๋ฏธ์ง ํฌ๊ธฐ๋ก๋ถํฐ ์ถ๋ ฅ ์ด๋ฏธ์ง ํฌ๊ธฐ๋ฅผ ์ถ๋ก ํ๋ ๊ณต์์ Convolution layer์ P..
wooono.tistory.com
1. CNN Architectures
- VGG16
: VGG16 ์ ๋ง์ ํ๋ผ๋ฏธํฐ๋ฅผ ๊ฐ์ง๋ฉฐ, ๋จ์ํจ
: Convolution Layer (filter = (3, 3), stride = 1, padding = same), Pooling Layer (filter = (2, 2), stride = 2)๋ฅผ ๋ฐ๋ณต์ ์ฌ์ฉ
: ์ด๋ฏธ์ง์ ์ฑ๋์ Convolution Layer๋ง๋ค 2๋ฐฐ์ฉ ์ฆ๊ฐํ๋ฉฐ, ์ด๋ฏธ์ง ํฌ๊ธฐ๋ Pooling Layer๋ง๋ค 2๋ฐฐ์ฉ ๊ฐ์
: ํน์ง
- Max Pooling์ ์ฌ์ฉ
- ํ์ฑํ ํจ์๋ก ReLU๋ฅผ ์ฌ์ฉ
- 16๊ฐ์ Layer๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ VGG-16
: ๋จ์
- Fully Connected Layer์์ parameter ์๊ฐ ๋ง์
- ์๋นํ memory cost, overfitting ๋ฌธ์ ๋ฅผ ์ผ๊ธฐ
from keras.layers import Input, Conv2D, MaxPooling2D
from keras.layers import Dense, Flatten
from keras.models import Model
_input = Input((224,224,1))
conv1 = Conv2D(filters=64, kernel_size=(3,3), padding="same", activation="relu")(_input)
conv2 = Conv2D(filters=64, kernel_size=(3,3), padding="same", activation="relu")(conv1)
pool1 = MaxPooling2D((2, 2))(conv2)
conv3 = Conv2D(filters=128, kernel_size=(3,3), padding="same", activation="relu")(pool1)
conv4 = Conv2D(filters=128, kernel_size=(3,3), padding="same", activation="relu")(conv3)
pool2 = MaxPooling2D((2, 2))(conv4)
conv5 = Conv2D(filters=256, kernel_size=(3,3), padding="same", activation="relu")(pool2)
conv6 = Conv2D(filters=256, kernel_size=(3,3), padding="same", activation="relu")(conv5)
conv7 = Conv2D(filters=256, kernel_size=(3,3), padding="same", activation="relu")(conv6)
pool3 = MaxPooling2D((2, 2))(conv7)
conv8 = Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu")(pool3)
conv9 = Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu")(conv8)
conv10 = Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu")(conv9)
pool4 = MaxPooling2D((2, 2))(conv10)
conv11 = Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu")(pool4)
conv12 = Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu")(conv11)
conv13 = Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu")(conv12)
pool5 = MaxPooling2D((2, 2))(conv13)
flat = Flatten()(pool5)
dense1 = Dense(4096, activation="relu")(flat)
dense2 = Dense(4096, activation="relu")(dense1)
output = Dense(1000, activation="softmax")(dense2)
vgg16_model = Model(inputs=_input, outputs=output)
from keras.applications.vgg16 import decode_predictions
from keras.applications.vgg16 import preprocess_input
from keras.preprocessing import image
import matplotlib.pyplot as plt
from PIL import Image
import seaborn as sns
import pandas as pd
import numpy as np
import os
img1 = "dogs-vs-cats-redux-kernels-edition/train/cat.11679.jpg"
img2 = "dogs-vs-cats-redux-kernels-edition/train/dog.2811.jpg"
img3 = "dogs-vs-cats-redux-kernels-edition/train/cat.11679.jpg"
img4 = "dogs-vs-cats-redux-kernels-edition/train/dog.2811.jpg"
imgs = [img1, img2, img3, img4]
def _load_image(img_path):
img = image.load_img(img_path, target_size=(224, 224))
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
img = preprocess_input(img)
return img
def _get_predictions(_model):
f, ax = plt.subplots(1, 4)
f.set_size_inches(80, 40)
for i in range(4):
ax[i].imshow(Image.open(imgs[i]).resize((200, 200), Image.ANTIALIAS))
plt.show()
f, axes = plt.subplots(1, 4)
f.set_size_inches(80, 20)
for i,img_path in enumerate(imgs):
img = _load_image(img_path)
preds = decode_predictions(_model.predict(img), top=3)[0]
b = sns.barplot(y=[c[1] for c in preds], x=[c[2] for c in preds], color="gray", ax=axes[i])
b.tick_params(labelsize=55)
f.tight_layout()
from keras.applications.vgg16 import VGG16
vgg16_weights = 'dogs-vs-cats-redux-kernels-edition/vgg16_weights_tf_dim_ordering_tf_kernels.h5'
vgg16_model = VGG16(weights=vgg16_weights)
_get_predictions(vgg16_model)
- VGG19
: VGG19๋ 19๊ฐ์ ๋ ์ด์ด ๊ฐ์ง
: ๊ฐ์ฅ ๋ง์ ํ๋ผ๋ฏธํฐ์ ์๋ฅผ ๊ฐ์ง๊ณ ์๊ณ ๊ธฐ๋ณธ์ ์ธ ํ์ VGG16์ ํน์ง๊ณผ ๋น์ทํ๋ฉฐ, Conv ๋ ์ด์ด๊ฐ 3๊ฐ ์ถ๊ฐ๋จ
from keras.applications.vgg19 import VGG19
vgg19_weights = 'dogs-vs-cats-redux-kernels-edition/vgg19_weights_tf_dim_ordering_tf_kernels.h5'
vgg19_model = VGG19(weights=vgg19_weights)
_get_predictions(vgg19_model)
- InceptionNet
1) 1 x 1 convolution
=> ๋ณผ๋ฅจ์ ํฌ๊ธฐ๋ ๊ทธ๋๋ก ๋์ฑ, ์ฑ๋์ ์ค์ด๊ธฐ
=> (1, 1, channel) convolutuion ์ฌ์ฉํด์ ์ฑ๋ ์ค์ด๊ธฐ
=> 1x1 convolution ์ฌ์ฉํ๋ฉด ์ฑ๋์ ์ ์ค์ด๊ณ , ์ ์งํ๊ณ , ๋๋ฆด ์ ์์
2) Inception Module
: input ๋ณผ๋ฅจ์ ๋ํด Convolution(1x1, 3x3, 5x5)๊ณผ Max-Pooling(3x3)์ ๊ฐ๊ฐ ์ํํด์ output์ ์์์ฌ๋ฆฌ๊ธฐ
: ํ๋ผ๋ฏธํฐ์ ํํฐ ์ฌ์ด์ฆ ์กฐํฉ์ ๋ชจ๋ ํ์ต
=> 1x1 Conv, 1x1 Conv -> 3x3 Conv, 1x1 Conv ->5x5 Conv, MAXPOOL -> 1x1 Conv
=> MAXPOOL layer ๋ค์ 1x1 Conv layer๊ฐ ์ค๋ ๊ฒ์ ์ ์
=> MAXPOOL์ channel ์๋ฅผ ๊ฐ์์ํฌ ์ ์์ด์, 1x1 Conv๋ฅผ ํตํด channel ์๋ฅผ ์ค์
=> layer์ 1x1 Convolution layer๋ฅผ ์ถ๊ฐํด bottlenect layer๋ฅผ ๊ตฌํํจ์ผ๋ก์จ, channel ์๋ฅผ ๊ฐ์, ์ฐ์ฐ๋์ ์ค์
( Conv ์ฐ์ฐ ์ค๊ฐ์ (1, 1) Conv ์ฐ์ฐ์ ์ถ๊ฐ๋ฅผ bottleneck layer(๋ณ๋ชฉ์ธต))
3) Inception (GoogleNet) Network
: Inception Module ์งํฉ
: ๋ชจ๋ธ ์ฌ์ด์๋ Max Pooling์ด ๋ผ์์ ธ ์์
: ๋ชจ๋ธ์ ๋ง์ง๋ง์๋ Fully connected layer๋ก ๊ฒฐ๊ณผ๊ฐ์ ์ถ๋ ฅ
: ์ค๊ฐ์ softmax layer๊ฐ ์ถ๊ฐ๋ก ๋ฌ๋ ค์๋๋ฐ, ์ด๋ ํ๋ผ๋ฏธํฐ๊ฐ ์ ์ ๋ฐ์ดํธ๋๋๋ก, output์ ์ฑ๋ฅ์ด ๋์์ง์๊ฒ ๋์์ค
: Regularization ํจ๊ณผ๋ฅผ ์ป์ ์ ์๊ณ , overfitting์ ๋ฐฉ์ง
- Resnet
: ๋ง์ดํฌ๋ก์ํํธ ๊ฐ๋ฐ, ๊น์ ์ ๊ฒฝ๋ง ( Vanishing Gradient, Exploding Gradient ๋ฌธ์ ๋ฐ์ )
: Residual block ๋์
: l+2๋ฒ์งธ ๋น์ ํ ํจ์ ์ ๋ ฅ๊ฐ์ l ๋ฒ์งธ ๋น์ ํ ํจ์ ์ถ๋ ฅ๊ฐ์ ๋ํด์ค ์ ์๋๋ก ์ง๋ฆ๊ธธ(shortcut)์ ํ๋ ๋ง๋ฆ
: ๊ธฐ๋ณธ์ ์ผ๋ก VGG19 ๊ตฌ์กฐ ๋ฐ๋ฆ
: ์ปจ๋ณผ๋ฃจ์ ์ธต๋ค ์ถ๊ฐํด์ ๊น๊ฒ ๋ง๋ค๊ณ , shoutcut ์ถ๊ฐ
: 34 ๊ฐ์ layer ์ ๊ฐ์ง 34-layer residual ๋คํธ์ํฌ์ shortcut ์ ์ธ ๋ฒ์ ์ธ 34-layer plain ๊ตฌ์กฐ
: ์ฒ์์ ์ ์ธํ๊ณ 3*3 convolution layer ๊ท ์ผ ์ฌ์ฉ
: ์ด๋ฏธ์ง ํฌ๊ธฐ๊ฐ ๋ฐ์ผ๋ก ์ค์ด๋ค๋ฉด, ์ฑ๋ ํฌ๊ธฐ 2๋ฐฐ๋ก ๋๋ฆผ
- XceptionNet
: Extreme version of Inception module
: Depthwise separable convolution ( ๊ฐ ์ฑ๋๋ณ๋ก conv ์ฐ์ฐํ๊ณ , ๊ทธ ๊ฒฐ๊ณผ์ 1x1 conv ์ฐ์ฐ ์ทจํจ ) ์์ ํจ
: channel, spatial convolution ์ depthwise separable convolution ์ผ๋ก ์๋ฒฝํ๊ฒ ๋ถ๋ฆฌํ์
2. Image Feature Extraction
vgg1616 = VGG16(weights="imagenet", include_top=False)
def _get_features(img_path):
img = image.load_img(img_path, target_size=(224, 224))
img_data = image.img_to_array(img)
img_data = np.expand_dims(img_data, axis=0)
img_data = preprocess_input(img_data)
resnet_features = vgg1616.predict(img_data)
return resnet_features
img_path = "dogs-vs-cats-redux-kernels-edition/train/dog.2811.jpg"
vgg16_features = _get_features(img_path)
features_representation_1 = vgg16_features.flatten()
features_representation_2 = vgg16_features.squeeze()
print ("Shape 1: ", features_representation_1.shape)
print ("Shape 2: ", features_representation_2.shape)
3. Transfer Learning
basepath = "dogs-vs-cats-redux-kernels-edition/train/"
class1 = os.listdir(basepath + "dog/")
class2 = os.listdir(basepath + "cat/")
data = {'dog': class1[:10],
'cat': class2[:10],
'test': [class1[11], class2[11]]}
features = {"dog" : [], "cat" : [], "test" : []}
testimgs = []
for label, val in data.items():
for k, each in enumerate(val):
if label == "test" and k == 0:
img_path = basepath + "/dog/" + each
testimgs.append(img_path)
elif label == "test" and k == 1:
img_path = basepath + "/cat/" + each
testimgs.append(img_path)
else:
img_path = basepath + label.title() + "/" + each
feats = _get_features(img_path)
features[label].append(feats.flatten())
dataset = pd.DataFrame()
for label, feats in features.items():
temp_df = pd.DataFrame(feats)
temp_df['label'] = label
dataset = dataset.append(temp_df, ignore_index=True)
dataset.head()
y = dataset[dataset.label != 'test'].label
X = dataset[dataset.label != 'test'].drop('label', axis=1)
from sklearn.feature_selection import VarianceThreshold
from sklearn.neural_network import MLPClassifier
from sklearn.pipeline import Pipeline
model = MLPClassifier(hidden_layer_sizes=(100, 10))
pipeline = Pipeline([('low_variance_filter', VarianceThreshold()), ('model', model)])
pipeline.fit(X, y)
print ("Model Trained on pre-trained features")
preds = pipeline.predict(features['test'])
f, ax = plt.subplots(1, 2)
for i in range(2):
ax[i].imshow(Image.open(testimgs[i]).resize((200, 200), Image.ANTIALIAS))
ax[i].text(10, 180, 'Predicted: %s' % preds[i], color='k', backgroundcolor='red', alpha=0.8)
plt.show()
dog, cat ํด๋๊ฐ ๋ฐ๋ก ์์ด์,,,, ๊ฐ์ธ์ ์ผ๋ก
91๊ฐ ์ฉ ์ฎ๊ฒจ์.... train ํ๋
๋ฐ์ดํฐ ์์ด ๋ถ์กฑํด์,, ๊ฐ ์์ธก์ ์๋ชป ํใท์ค ใ ใ กใ
๊ทธ๋ฆฌ๊ถ vgg ๋ง๊ณ ๋ค๋ฅธ ๋ชจ๋ธ๋ค์
ใท์๋ฌ๊ฐ ๋ฌ๋ฝ,,, why??
'๐ฉโ๐ป ์ปดํจํฐ ๊ตฌ์กฐ > Kaggle' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Kaggle]Pneumonia/Normal Classification(CNN) (0) | 2022.03.20 |
---|---|
[Kaggle]Super Image Resolution_๊ณ ํ์ง ์ด๋ฏธ์ง ๋ง๋ค๊ธฐ (0) | 2022.02.07 |
[Kaggle] HeartAttack ์์ธก (0) | 2022.01.31 |
[Kaggle] Chest X-Ray ํ์ ์ด๋ฏธ์ง ๋ถ๋ฅํ๊ธฐ (0) | 2022.01.29 |
[Kaggle]Breast Cancer Wisconsin (Diagnostic) Data Set_์ ๋ฐฉ์ ๋ถ๋ฅ (0) | 2022.01.28 |