728x90
728x90
숫자맞추기는 이미 했던 거니까!
import tensorflow as tf
import numpy as np
import pandas as pd
from keras.datasets.mnist import load_data
from keras.models import Sequential, Model
from keras.layers import Dense, Input ,Flatten, Dropout, Conv2D, MaxPooling2D, GlobalAveragePooling2D
from keras.utils import plot_model, to_categorical
from keras.regularizers import l2
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
tf.debugging.set_log_device_placement (False)
train_dir = "/kaggle/input/2023-sejong-ai-challenge-pre/train"
test_dir = "/kaggle/input/2023-sejong-ai-challenge-pre/test"
train_ds = tf.keras.utils.image_dataset_from_directory(
train_dir,
validation_split=0.2,
subset="training",
seed=123,
image_size=(28, 28),
batch_size=16)
val_ds = tf.keras.utils.image_dataset_from_directory(
train_dir,
validation_split=0.2,
subset="validation",
seed=123,
image_size=(28, 28),
batch_size=16)
test_ds = tf.keras.utils.image_dataset_from_directory(
test_dir,
image_size=(28,28),
batch_size=16,
shuffle= False)
def normalize(image, label):
return image / 255, tf.one_hot(label, 10)
train_ds = train_ds.map(normalize)
test_ds = test_ds.map(normalize)
val_ds = val_ds.map(normalize)
input = Input(shape = (28,28,3))
x = Conv2D(256, (3,3), activation = 'relu', padding='same')(input)
x = MaxPooling2D()(x)
x = Conv2D(256, (3,3), activation = 'relu', padding='same')(x)
x = MaxPooling2D()(x)
x = Conv2D(256, (3,3), activation = 'relu', padding='same')(x)
x = MaxPooling2D()(x)
x = GlobalAveragePooling2D()(x)
x = Dense(256, activation = 'relu')(x)
x = Dense(128, activation = 'relu')(x)
x = Dense(64, activation = 'relu')(x)
output = Dense(10, activation = 'softmax')(x)
model = Model(inputs = input, outputs = output)
model.compile(optimizer = tf.keras.optimizers.Adam(learning_rate = 0.001), loss = 'categorical_crossentropy', metrics = ['acc'])
hist = model.fit(train_ds, epochs = 10, validation_data = val_ds)
요렇게 돌리면 학습 잘 됩니다.
plt.subplot(1,2,1)
plt.plot(hist.history['loss'], label = 'trian loss')
plt.plot(hist.history['val_loss'], label = 'val loss')
plt.legend()
plt.subplot(1,2,2)
plt.plot(hist.history['acc'], label = 'trian acc')
plt.plot(hist.history['val_acc'], label = 'val acc')
plt.legend()
이걸로 validation확인 가능!
validation이랑 tarin의 loss가 많이 차이나면 오버피팅!
predicted_classes = tf.argmax(out, axis=1).numpy()
submit.iloc[:, 1] = predicted_classes
new_submit.to_csv('submission1_1.csv', index=False)
이렇게 제출하면 끝입니당
728x90
'인공지능 > 공부' 카테고리의 다른 글
드디어 첫 수상 (33) | 2023.11.27 |
---|---|
AI 챌린지 본선 (25) | 2023.11.24 |
FCN quiz (0) | 2023.11.16 |
FCN - tensorflow (0) | 2023.11.15 |
1 vs all classification (0) | 2023.11.15 |