text_plain_output_1.png
from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau

from keras.layers import Conv2D, MaxPooling2D

from keras.layers import Dense, Dropout, Flatten, Activation

from keras.models import Sequential

from keras.preprocessing.image import ImageDataGenerator, load_img

from sklearn.model_selection import train_test_split

import matplotlib.pyplot as plt

import numpy as np

import os

import pandas as pd

data_dir = '/kaggle/input/lego-vs-unknown-cropped/VIA_dataset_cropped/train/'
data_dir_train_lego = '/kaggle/input//lego-vs-unknown-cropped/VIA_dataset_cropped/train/LEGO'
test_lego = ['LEGO/' + f for f in os.listdir(data_dir_train_lego)]
data_dir_test_Unknown = '/kaggle/input//lego-vs-unknown-cropped/VIA_dataset_cropped/train/Unknown'
test_Unknown = ['Unknown/' + f for f in os.listdir(data_dir_test_Unknown)]
lists = test_lego + test_Unknown
df = pd.DataFrame(lists, columns=['image'])
df['label'] = np.where(df['image'].str.contains('LEGO'), 'LEGO', 'Unknown')

sample = df.sample(2)
image = load_img(data_dir + sample.iloc[0]['image'])
image = load_img(data_dir + sample.iloc[1]['image'])

train_df, valid_df = train_test_split(df, test_size=0.2, random_state=42)

img_width, img_height = (204, 204)
batch_size = 64
num_classes = 2
input_shape = (img_width, img_height, 3)
EPOCHS = 10

train_datagen = ImageDataGenerator(rotation_range=45, shear_range=0.1, zoom_range=0.2, horizontal_flip=True, width_shift_range=0.1, height_shift_range=0.1, rescale=1.0 / 255)
train_generator = train_datagen.flow_from_dataframe(dataframe=train_df, directory=data_dir, x_col='image', y_col='label', target_size=(img_width, img_height), batch_size=batch_size, class_mode='categorical', color_mode='rgb')

validation_datagen = ImageDataGenerator(rescale=1.0 / 255)
validation_generator = validation_datagen.flow_from_dataframe(dataframe=valid_df, directory=data_dir, x_col='image', y_col='label', target_size=(img_width, img_height), batch_size=batch_size, class_mode='categorical', color_mode='rgb')

sample = train_df.sample(1, random_state=42)
sample_generator = train_datagen.flow_from_dataframe(dataframe=sample, directory=data_dir, x_col='image', y_col='label', target_size=(img_width, img_height), batch_size=batch_size, class_mode='categorical', color_mode='rgb')

for i in range(0, 15):
    for x, y in sample_generator:
        image = x[0]
        break
plt.tight_layout()

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='nadam', metrics=['accuracy'])

model.summary()

earlystopper = EarlyStopping(monitor='val_loss', min_delta=0.001, patience=15, mode='min', verbose=1)

checkpointer = ModelCheckpoint(filepath='/kaggle/working/models/model.{epoch:02d}-{val_loss:.6f}.hdf5', verbose=1, save_best_only=True, save_weights_only=True)

reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.3, patience=10, min_lr=0, verbose=1)

hist = model.fit_generator(train_generator, steps_per_epoch=np.ceil(len(train_df) / batch_size), epochs=EPOCHS, validation_data=validation_generator, validation_steps=np.ceil(len(valid_df) / batch_size), workers=8, max_queue_size=15, callbacks=[earlystopper, checkpointer, reduce_lr])

fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10,10))
ax1.plot(hist.history['loss'], color='b', label='Training loss')
ax1.plot(hist.history['val_loss'], color='r', label='Validation loss')
ax1.set_xticks(np.arange(1, EPOCHS, 1))
ax1.set_yticks(np.arange(0, 1, 0.1))

ax2.plot(hist.history['accuracy'], color='b', label='Training loss')
ax2.plot(hist.history['val_accuracy'], color='r', label='Validation loss')
ax2.set_xticks(np.arange(1, EPOCHS, 1))

legend = plt.legend(loc='best', shadow=True)
plt.tight_layout()
plt.show()

data_dir = '/kaggle/input/lego-vs-unknown-cropped/VIA_dataset_cropped/test/'
data_dir_test_lego = '/kaggle/input//lego-vs-unknown-cropped/VIA_dataset_cropped/test/LEGO'
test_lego = ['LEGO/' + f for f in os.listdir(data_dir_test_lego)]
data_dir_test_Unknown = '/kaggle/input//lego-vs-unknown-cropped/VIA_dataset_cropped/test/Unknown'
test_Unknown = ['Unknown/' + f for f in os.listdir(data_dir_test_Unknown)]
lists = test_lego + test_Unknown
df_test = pd.DataFrame(lists, columns=['image'])
df_test['label'] = np.where(df_test['image'].str.contains('LEGO'), 'LEGO', 'Unknown')

test_datagen = ImageDataGenerator(rescale=1.0 / 255)
test_generator = test_datagen.flow_from_directory(directory=data_dir, target_size=(img_width, img_height), color_mode='rgb', batch_size=batch_size, class_mode=None, shuffle=False)
test_size = df_test.shape[0]

df_test