import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras import callbacks
from sklearn.model_selection import train_test_split, KFold, StratifiedKFold, cross_val_score
from sklearn.metrics import accuracy_score
from keras.utils import np_utils
from sklearn.linear_model import LogisticRegression
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier, KerasRegressor
from sklearn.preprocessing import LabelEncoder, StandardScaler
from tensorflow.keras.preprocessing.image import ImageDataGenerator
data_dir = '/content/drive/MyDrive/Colab Notebooks/Multi-class Weather Dataset'
filepaths, labels = [], []
for folder in os.listdir(data_dir):
folder_path = os.path.join(data_dir, folder)
for file in os.listdir(folder_path):
file_path = os.path.join(folder_path, file)
filepaths.append(file_path)
labels.append(folder)
df = pd.DataFrame({'filepaths': filepaths, 'labels': labels})
# train
train_df, dummy_df = train_test_split(df, train_size= 0.8, shuffle= True, random_state= 42)
# valid and test
valid_df, test_df = train_test_split(dummy_df, train_size= 0.5, shuffle= True, random_state= 42)
batch_size = 32
img_size = 224
channels = 3
img_shape = (img_size, img_size, channels)
train_gen = ImageDataGenerator()
test_gen = ImageDataGenerator()
train_gen = train_gen.flow_from_dataframe(train_df, x_col='filepaths', y_col='labels',
target_size=(img_size, img_size), class_mode='categorical',
batch_size=batch_size, shuffle=True)
valid_gen = test_gen.flow_from_dataframe(valid_df, x_col='filepaths', y_col='labels',
target_size=(img_size, img_size), class_mode='categorical',
batch_size=batch_size, shuffle=True)
ts_length = len(test_df)
test_batch_size = max(sorted([ts_length // n for n in range(1, ts_length + 1) if ts_length%n == 0 and ts_length/n <= 80]))
test_steps = ts_length // test_batch_size
test_gen = test_gen.flow_from_dataframe(test_df, x_col='filepaths', y_col='labels',
target_size=(img_size, img_size), class_mode='categorical',
batch_size=test_batch_size, shuffle=False)
there are 4 image classification types on my dataset, Create a baseline MLP classification model
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras import callbacks
from sklearn.model_selection import train_test_split, KFold, StratifiedKFold, cross_val_score
from sklearn.metrics import accuracy_score
from keras.utils import np_utils
from sklearn.linear_model import LogisticRegression
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier, KerasRegressor
from sklearn.preprocessing import LabelEncoder, StandardScaler
from tensorflow.keras.preprocessing.image import ImageDataGenerator
data_dir = '/content/drive/MyDrive/Colab Notebooks/Multi-class Weather Dataset'
filepaths, labels = [], []
for folder in os.listdir(data_dir):
folder_path = os.path.join(data_dir, folder)
for file in os.listdir(folder_path):
file_path = os.path.join(folder_path, file)
filepaths.append(file_path)
labels.append(folder)
df = pd.DataFrame({'filepaths': filepaths, 'labels': labels})
# train
train_df, dummy_df = train_test_split(df, train_size= 0.8, shuffle= True, random_state= 42)
# valid and test
valid_df, test_df = train_test_split(dummy_df, train_size= 0.5, shuffle= True, random_state= 42)
batch_size = 32
img_size = 224
channels = 3
img_shape = (img_size, img_size, channels)
train_gen = ImageDataGenerator()
test_gen = ImageDataGenerator()
train_gen = train_gen.flow_from_dataframe(train_df, x_col='filepaths', y_col='labels',
target_size=(img_size, img_size), class_mode='categorical',
batch_size=batch_size, shuffle=True)
valid_gen = test_gen.flow_from_dataframe(valid_df, x_col='filepaths', y_col='labels',
target_size=(img_size, img_size), class_mode='categorical',
batch_size=batch_size, shuffle=True)
ts_length = len(test_df)
test_batch_size = max(sorted([ts_length // n for n in range(1, ts_length + 1) if ts_length%n == 0 and ts_length/n <= 80]))
test_steps = ts_length // test_batch_size
test_gen = test_gen.flow_from_dataframe(test_df, x_col='filepaths', y_col='labels',
target_size=(img_size, img_size), class_mode='categorical',
batch_size=test_batch_size, shuffle=False)
there are 4 image classification types on my dataset, Create a baseline MLP classification model