<jupyter_start><jupyter_text><jupyter_code>import warnings
warnings.filterwarnings('ignore')
import tensorflow as tf
tf.__version__
print('GPU Information -', tf.test.gpu_device_name(), '\n')
!nvidia-smi
from google.colab import drive
drive.mount('/content/drive')
!ls -l '/content/drive/My Drive/Colab Notebooks/datasets/Logo_Data.zip'
!unzip /content/drive/My\ Drive/Colab\ Notebooks/datasets/Logo_Data.zip
!ls -l
train_dir = 'train'
valid_dir = 'test'
test_dir = 'test'
from tensorflow.keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale =1./255)
valid_datagen = ImageDataGenerator(rescale =1./255)

train_generator = train_datagen.flow_from_directory(train_dir,
                                                    target_size = (75, 75),
                                                    batch_size = 8,
                                                    class_mode = 'categorical'
                                                    )
valid_generator = valid_datagen.flow_from_directory(valid_dir,
                                                    target_size = (75, 75),
                                                    batch_size = 8,
                                                    class_mode = 'categorical'
                                                    )
for data_batch, labels_batch in train_generator:
    print('배치 데이터 크기:', data_batch.shape)
    print('배치 데이터 크기:', labels_batch.shape)
    break
labels_batch
from tensorflow.keras import layers
from tensorflow.keras import models

model = models.Sequential()
model.add(layers.Conv2D(32,(3,3), activation= 'relu', input_shape = (75,75,3)))
model.add(layers.MaxPooling2D(2,2))
model.add(layers.Conv2D(32,(3,3), activation= 'relu'))
model.add(layers.MaxPooling2D(2,2))
model.add(layers.Conv2D(32,(3,3), activation= 'relu'))
model.add(layers.MaxPooling2D(2,2))
model.add(layers.Conv2D(32,(3,3), activation= 'relu'))


model.add(layers.Flatten())
model.add(layers.Dense(512, activation = 'relu'))
model.add(layers.Dense(27,activation= 'softmax'))
model.summary()
model.compile(loss = 'categorical_crossentropy',
              optimizer = 'adam',
              metrics = ['accuracy'])

%%time

T_model = model.fit(train_generator, steps_per_epoch= 5,
                    epochs = 60,
                    validation_data = valid_generator,
                    validation_steps = 50)

import matplotlib.pyplot as plt
epochs = range(1, len(T_model.history['loss']) + 1)
plt.figure(figsize = (9, 6))
plt.plot(epochs, T_model.history['loss'])
plt.title('Training & Validation Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend(['Training Loss'])
plt.grid()
plt.show()
import matplotlib.pyplot as plt
epochs = range(1, len(T_model.history['loss']) + 1)
plt.figure(figsize = (9, 6))
plt.plot(epochs, T_model.history['accuracy'])
plt.title('Training & Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend(['Training Accuracy'])
plt.grid()
plt.show()
test_datagen = ImageDataGenerator(rescale = 1./255)

test_generator = test_datagen.flow_from_directory(test_dir,
                                                  target_size = (75,75),
                                                  batch_size = 20,
                                                  class_mode = 'categorical')

loss , accuracy = model.evaluate(test_generator,
                                 steps =50)
print('Loss = {:.5f}'.format(loss))
print('Accuracy = {:.5f}'.format(accuracy))
model.save('/content/drive/My Drive/Colab Notebooks/models/logo.h5')
!ls -l /content/drive/My\ Drive/Colab\ Notebooks/models
from tensorflow.keras.models import load_model
model_small = load_model('/content/drive/My Drive/Colab Notebooks/models/logo.h5')
loss, accuracy = model_small.evaluate(test_generator,
steps = 50)
print('Loss = {:.5f}'.format(loss))
print('Accuracy = {:.5f}'.format(accuracy))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1310766098243828, 'fraction_numerical': 0.022906592008144565, 'mean_word_length': 2.5662431941923773, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 23, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '176562', 'license': 'MIT', 'n_tokens_mistral': 1279, 'n_tokens_neox': 1212, 'n_words': 218, 'path': 'CNN_Logo_GPU.ipynb', 'repo_name': 'BbangB/ML_basic', 'size': '3929'}