728x90
반응형
import os
import numpy as np
import seaborn as sns
import pandas as pd
import tensorflow as tf
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from tensorflow.python import keras
from tensorflow.python.keras.models import Sequential
from keras.layers import Dense, Conv2D, Activation, MaxPool2D, Flatten, Dropout, BatchNormalization
from keras.optimizers import RMSprop,Adam
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import visualkeras
from keras.utils import plot_model
import math
from keras.optimizers import RMSprop
Img_shape = 28
Num_classes = 10
test_size = 0.25
random_state = 1234
No_epochs = 100
Batch_size = 128
train_dataset = pd.read_csv("C:/Users/user/Desktop/sehyun/archive/fashion-mnist_train.csv")
test_dataset = pd.read_csv("C:/Users/user/Desktop/sehyun/archive/fashion-mnist_test.csv")
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
def data_preprocessing(raw):
label = tf.keras.utils.to_categorical(raw.label, 10)
num_images = raw.shape[0]
x_as_array = raw.values[:,1:]
x_shaped_array = x_as_array.reshape(num_images, 28, 28, 1)
image = x_shaped_array / 255
return image, label
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=test_size, random_state=random_state)
# We are using sequential model which is linear stack of layers. The Sequential model is initialized first and then using add method we add rest of the layers
model = tf.keras.Sequential()
# First layer, which has a 2D Convolutional layer with kernel size as 3x3 and Max pooling operation
model.add(Conv2D(32, (3,3), padding='same', input_shape=(28,28, 1)))
model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))
# Second layer, which has a 2D Convolutional layer with kernel size as 3x3 & ReLU activation and Max pooling operation
model.add(Conv2D(64, (3,3), padding='same', activation=tf.nn.relu))
model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))
# Fully connected layer with ReLU activation function
model.add(Flatten())
model.add(Dense(128, activation=tf.nn.relu))
# Output layer with softmax activation function
model.add(Dense(10, activation=tf.nn.softmax))
model.compile(optimizer='adam',
loss=tf.keras.losses.categorical_crossentropy,
metrics=['accuracy'])
train_model = model.fit(X_train, y_train,
batch_size=32,
epochs=10,
verbose=1,
validation_data=(X_val, y_val))
score = model.evaluate(X_test, y_test, steps=math.ceil(10000/32))
# checking the test loss and test accuracy
print('Test loss:', score[0])
print('Test accuracy:', score[1])
labels = {0 : "T-shirt/top", 1: "Trouser", 2: "Pullover", 3: "Dress", 4: "Coat",
5: "Sandal", 6: "Shirt", 7: "Sneaker", 8: "Bag", 9: "Ankle Boot"}
y_pred = model.predict(X_test)
X_test__ = X_test.reshape(X_test.shape[0], 28, 28)
fig, axis = plt.subplots(4, 4, figsize=(12, 14))
for i, ax in enumerate(axis.flat):
ax.imshow(X_test__[i], cmap='binary')
ax.set(title = f"Real Class is {labels[y_test[i].argmax()]}\nPredict Class is {labels[y_pred[i].argmax()]}");
728x90
반응형
'코드' 카테고리의 다른 글
[python] 이미지 증강 코드 data augmentaion (0) | 2023.03.20 |
---|---|
[python] Fashion mnist mlp (0) | 2023.02.24 |
[python] 말과 사람 구분하기 horse or human (0) | 2023.02.24 |
[python] keras NeRF (0) | 2023.02.24 |
[python] opencv로 태극기 그리기 (1) | 2023.02.24 |