image_output_1.png
from PIL import Image

from PIL import Image

from keras.preprocessing.image import ImageDataGenerator

from sklearn.model_selection import train_test_split

import matplotlib.pyplot as plt

import os

import pandas as pd

import random # Randomly select a filename for viewing

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
import random
from keras.preprocessing.image import load_img
from PIL import Image
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, BatchNormalization
from keras.optimizers import RMSprop
from keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint
from keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
main_folder = '../input'
input_file_train = 'train/train'
input_file_test = 'test1/test1'

filenames = os.listdir(os.path.join(main_folder, input_file_train))
categories = []
for file in filenames:
    category = file.split('.')[0].lower()
    categories.append(category)
dataframe = pd.DataFrame({'filename': filenames, 'categories': categories})

sample = random.choice(filenames)
from PIL import Image
im = Image.open(os.path.join(main_folder, input_file_train, sample))
im.size

random_seed = 10
batch_size = 100
image_height, image_width, image_channels = (128, 128, 3)
train_df, val_df = train_test_split(dataframe, test_size=0.2, random_state=random_seed)
train_df.reset_index(inplace=True, drop=True)
val_df.reset_index(inplace=True, drop=True)

train_datagen = ImageDataGenerator(rotation_range=15, rescale=1.0 / 255, width_shift_range=0.2, height_shift_range=0.2, horizontal_flip=True, shear_range=0.2, zoom_range=0.2)
train_generator = train_datagen.flow_from_dataframe(dataframe=train_df, directory=os.path.join(main_folder, input_file_train), x_col='filename', y_col='categories', target_size=(image_height, image_width), class_mode='categorical', batch_size=batch_size)
val_datagen = ImageDataGenerator(rescale=1.0 / 255)
val_generator = val_datagen.flow_from_dataframe(dataframe=val_df, directory=os.path.join(main_folder, input_file_train), x_col='filename', y_col='categories', target_size=(image_height, image_width), class_mode='categorical', batch_size=batch_size)