application_vnd.jupyter.stderr_output_1.png
from keras.layers import Conv2D, MaxPooling2D, Dense, Input, Activation, Dropout, GlobalAveragePooling2D, \

from keras.models import Model

from keras.optimizers import Adam

from keras.utils import np_utils

from sklearn.model_selection import train_test_split

import matplotlib

import numpy as np # linear algebra

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

import sys

df = pd.read_csv('../input/aerial-cactus-identification/train.csv')

train = pd.read_csv('../input/cactus-images-csv/dataset/train.csv')
test = pd.read_csv('../input/cactus-images-csv/dataset/test.csv')

train = train.drop('Unnamed: 0', axis=1)
test = test.drop('Unnamed: 0', axis=1)

import keras
from keras.models import Model
from keras.layers import Conv2D, MaxPooling2D, Dense, Input, Activation, Dropout, GlobalAveragePooling2D, BatchNormalization, concatenate, AveragePooling2D
from keras.optimizers import Adam
def conv_layer(conv_x, filters):
    conv_x = BatchNormalization()(conv_x)
    conv_x = Activation('relu')(conv_x)
    conv_x = Conv2D(filters, (3, 3), kernel_initializer='he_uniform', padding='same', use_bias=False)(conv_x)
    conv_x = Dropout(0.2)(conv_x)
    return conv_x
def dense_block(block_x, filters, growth_rate, layers_in_block):
    for i in range(layers_in_block):
        each_layer = conv_layer(block_x, growth_rate)
        block_x = concatenate([block_x, each_layer], axis=-1)
        filters += growth_rate
    return (block_x, filters)
def transition_block(trans_x, tran_filters):
    trans_x = BatchNormalization()(trans_x)
    trans_x = Activation('relu')(trans_x)
    trans_x = Conv2D(tran_filters, (1, 1), kernel_initializer='he_uniform', padding='same', use_bias=False)(trans_x)
    trans_x = AveragePooling2D((2, 2), strides=(2, 2))(trans_x)
    return (trans_x, tran_filters)
def dense_net(filters, growth_rate, classes, dense_block_size, layers_in_block):
    input_img = Input(shape=(32, 32, 3))
    x = Conv2D(24, (3, 3), kernel_initializer='he_uniform', padding='same', use_bias=False)(input_img)
    dense_x = BatchNormalization()(x)
    dense_x = Activation('relu')(x)
    dense_x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(dense_x)
    for block in range(dense_block_size - 1):
        dense_x, filters = dense_block(dense_x, filters, growth_rate, layers_in_block)
        dense_x, filters = transition_block(dense_x, filters)
    dense_x, filters = dense_block(dense_x, filters, growth_rate, layers_in_block)
    dense_x = BatchNormalization()(dense_x)
    dense_x = Activation('relu')(dense_x)
    dense_x = GlobalAveragePooling2D()(dense_x)
    output = Dense(classes, activation='softmax')(dense_x)
    return Model(input_img, output)

from keras.utils import np_utils
from sklearn.model_selection import train_test_split
images = train.drop('label', axis=1)
images = np.asarray(images)
images = images.reshape(images.shape[0], 32, 32, 3)
label = train['label']
X_train, X_test, y_train, y_test = train_test_split(images, label.values, test_size=0.33)
Cat_test_y = np_utils.to_categorical(y_test)
y_train = np_utils.to_categorical(y_train)

dense_block_size = 3
layers_in_block = 4
growth_rate = 12
classes = 2
model = dense_net(growth_rate * 2, growth_rate, classes, dense_block_size, layers_in_block)
model.summary()
batch_size = 32
epochs = 10
optimizer = Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])
history = model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, shuffle=True, validation_data=(X_test, Cat_test_y))

import sys
import matplotlib
print('Generating plots...')
sys.stdout.flush()
matplotlib.use('Agg')
matplotlib.pyplot.style.use('ggplot')
matplotlib.pyplot.figure()
N = epochs
matplotlib.pyplot.plot(np.arange(0, N), history.history['loss'], label='train_loss')
matplotlib.pyplot.plot(np.arange(0, N), history.history['val_loss'], label='val_loss')
matplotlib.pyplot.plot(np.arange(0, N), history.history['acc'], label='train_acc')
matplotlib.pyplot.plot(np.arange(0, N), history.history['val_acc'], label='val_acc')
matplotlib.pyplot.title('Cactus Image Classification')
matplotlib.pyplot.xlabel('Epoch #')
matplotlib.pyplot.ylabel('Loss/Accuracy')
matplotlib.pyplot.legend(loc='lower left')
matplotlib.pyplot.savefig('plot.png')