image_output_1.png
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')

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')

plt.figure(figsize=(12, 12))
for i in range(0, 15):
    plt.subplot(5, 3, i + 1)
    for x, y in sample_generator:
        image = x[0]
        plt.imshow(image)
        break
plt.tight_layout()
plt.show()