<jupyter_start><jupyter_text>Challenge Kaggle 준비하기 * Kaggle 가입 : https://www.kaggle.com  * Competition : https://www.kaggle.com/c/aerial-cactus-identification<jupyter_code># This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load in 

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# Any results you write to the current directory are saved as output.
os.listdir('../input/aerial-cactus-identification/')
os.listdir('../input/aerial-cactus-identification/train/train')
train_dir = '../input/aerial-cactus-identification/train/train'
csv_path = '../input/aerial-cactus-identification/train.csv'

df = pd.read_csv(csv_path)
df.head()
filenames = df['id']
filenames.head()
file_paths = [os.path.join(train_dir, fname) for fname in filenames]
file_paths[:5]
train_df = pd.DataFrame(data={'id': file_paths, 'has_cactus': df['has_cactus']})
train_df.head()
train_df = train_df.astype(np.str)
sample_csv_path = '../input/aerial-cactus-identification/sample_submission.csv'
sample_df = pd.read_csv(sample_csv_path)
sample_df.head()
len(train_df)
train_df = train_df[:-500]
test_df = train_df[-500:]

len(train_df), len(test_df)
path = train_df['id'][0]<jupyter_output><empty_output><jupyter_text>Load Packages<jupyter_code>from tqdm import tqdm_notebook

import matplotlib.pyplot as plt
from PIL import Image

import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras.preprocessing.image import ImageDataGenerator
train_dir
test_dir = '../input/aerial-cactus-identification/test/test/'
os.listdir(test_dir)<jupyter_output><empty_output><jupyter_text>Data Explore<jupyter_code>train_df.head()
img_pil = Image.open(path)
image = np.array(img_pil)

image.shape
plt.imshow(image)
plt.show()<jupyter_output><empty_output><jupyter_text>Hyperparameter<jupyter_code>input_shape = (32, 32, 3)
batch_size = 32
num_classes = 2
num_epochs = 10

learning_rate = 0.01<jupyter_output><empty_output><jupyter_text>Model<jupyter_code>inputs = layers.Input(input_shape)
net = layers.Conv2D(64, (3, 3), padding='same')(inputs)
net = layers.Conv2D(64, (3, 3), padding='same')(net)
net = layers.Conv2D(64, (3, 3), padding='same')(net)
net = layers.BatchNormalization()(net)
net = layers.Activation('relu')(net)
net = layers.MaxPooling2D(pool_size=(2, 2))(net)

net = layers.Conv2D(128, (3, 3), padding='same')(net)
net = layers.Conv2D(128, (3, 3), padding='same')(net)
net = layers.Conv2D(128, (3, 3), padding='same')(net)
net = layers.BatchNormalization()(net)
net = layers.Activation('relu')(net)
net = layers.MaxPooling2D(pool_size=(2, 2))(net)
net = layers.Dropout(0.25)(net)

net = layers.Conv2D(256, (3, 3), padding='same')(net)
net = layers.Conv2D(256, (3, 3), padding='same')(net)
net = layers.Conv2D(256, (3, 3), padding='same')(net)
net = layers.BatchNormalization()(net)
net = layers.Activation('relu')(net)
net = layers.MaxPooling2D(pool_size=(2, 2))(net)
net = layers.Dropout(0.25)(net)

net = layers.Conv2D(512, (3, 3), padding='same')(net)
net = layers.Conv2D(512, (3, 3), padding='same')(net)
net = layers.Conv2D(512, (3, 3), padding='same')(net)
net = layers.BatchNormalization()(net)
net = layers.Activation('relu')(net)
net = layers.MaxPooling2D(pool_size=(2, 2))(net)
net = layers.Dropout(0.25)(net)

net = layers.Conv2D(512, (3, 3), padding='same')(net)
net = layers.Conv2D(512, (3, 3), padding='same')(net)
net = layers.Conv2D(512, (3, 3), padding='same')(net)
net = layers.BatchNormalization()(net)
net = layers.Activation('relu')(net)
net = layers.MaxPooling2D(pool_size=(2, 2))(net)
net = layers.Dropout(0.25)(net)

net = layers.Flatten()(net)
net = layers.Dense(512)(net)
net = layers.Activation('relu')(net)
net = layers.Dropout(0.5)(net)
net = layers.Dense(num_classes)(net)
net = layers.Activation('softmax')(net)

model = tf.keras.Model(inputs=inputs, outputs=net)
model.compile(loss='sparse_categorical_crossentropy',
              optimizer=tf.keras.optimizers.Adam(learning_rate),
              metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>Data Preprocess<jupyter_code>train_datagen = ImageDataGenerator(
    rescale=1./255.,
    width_shift_range=0.3,
    zoom_range=0.2,
    horizontal_flip=True
)

test_datagen = ImageDataGenerator(
    rescale=1./255.
)
train_generator = train_datagen.flow_from_dataframe(
    train_df,
    x_col='id',
    y_col='has_cactus',
    target_size=input_shape[:2],
    batch_size=batch_size,
    class_mode='sparse'
)

test_generator = test_datagen.flow_from_dataframe(
    test_df,
    x_col='id',
    y_col='has_cactus',
    target_size=input_shape[:2],
    batch_size=batch_size,
    class_mode='sparse'
)<jupyter_output><empty_output><jupyter_text>Train<jupyter_code>model.fit_generator(
    train_generator,
    steps_per_epoch=len(train_generator),
    epochs=num_epochs,
    validation_data=test_generator,
    validation_steps=len(test_generator)
)<jupyter_output><empty_output><jupyter_text>Evaluate<jupyter_code>test_dir
sample_df.head()
path = os.path.join(test_dir, sample_df['id'][0])
img_pil = Image.open(path)
image = np.array(img_pil)
image.shape
plt.imshow(image)
plt.show()
pred = model.predict(image[tf.newaxis, ...])
pred
pred = np.argmax(pred)
pred
preds = []

for fname in tqdm_notebook(sample_df['id']):
    path = os.path.join(test_dir, fname)

    img_pil = Image.open(path)
    image = np.array(img_pil)

    pred = model.predict(image[tf.newaxis, ...])
    pred = np.argmax(pred)
    preds.append(pred)

submission_df = pd.DataFrame(data={'id': sample_df['id'], 'has_cactus': preds})
submission_df.head()
submission_df.to_csv('samplesubmission.csv', index=False)
os.listdir()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15582941462623642, 'fraction_numerical': 0.025295929949732447, 'mean_word_length': 5.993197278911564, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 3, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '661478', 'license': 'MIT', 'n_tokens_mistral': 2432, 'n_tokens_neox': 2347, 'n_words': 407, 'path': 'DL_TF20/Part 22 - Challenge Kaggle.ipynb', 'repo_name': '46gom/DLND_Exercises', 'size': '6167'}