我设法为这段代码添加了扩充,但我不知道如何以及从哪里开始。数据集只有 200 张图像,所以我需要增强。所以我需要至少 5 次增强来制作至少 1000 张图像。
这是代码:
from keras.preprocessing.image import ImageDataGenerator
aug = ImageDataGenerator(rescale=1./255,
shear_range=0.2,
horizontal_flip=True,
rotation_range=45,
vertical_flip=True,
fill_mode='nearest',
height_shift_range=0.5,
width_shift_range=0.5
)
import os
base_dir ='/content/drive/My Drive/RESEARCH CENTER/Dataset'
train_dir = os.path.join(base_dir, 'train')
test_dir = os.path.join(base_dir, 'test')
train_normal_dir = os.path.join(train_dir, 'normal')
train_hem_dir = os.path.join(train_dir, 'hemorrhage')
test_normal_dir = os.path.join(test_dir, 'normal')
test_hem_dir = os.path.join(test_dir, 'hemorrhage')
print("total training normal brain images:", len(os.listdir(train_normal_dir)))
print("total training hemorrhage brain images:", len(os.listdir(train_hem_dir)))
print("total test normal brain images:", len(os.listdir(test_normal_dir)))
print("total test hemorrhage brain images:", len(os.listdir(test_hem_dir)))
import cv2
import numpy as np
# Gather data train
train_data = [] # disimpan dalam bentuk array
train_label = []
for r, d, f in os.walk(train_dir):
for file in f:
if ".png" in file:
imagePath = os.path.join(r, file)
image = cv2.imread(imagePath)
image = cv2.resize(image, (224,224))
color_mode="grayscale"
label = imagePath.split(os.path.sep)[-2]
train_data.append(image)
train_label.append(label)
train_data = np.array(train_data)
train_label = np.array(train_label)
val_data = []
val_label = []
for r, d, f in os.walk(test_dir):
for file in f:
if ".png" in file:
imagePath = os.path.join(r, file)
image = cv2.imread(imagePath)
image = cv2.resize(image, (224,224))
color_mode="grayscale"
label = imagePath.split(os.path.sep)[-2]
val_label.append(label)
val_data.append(image)
val_data = np.array(val_data)
val_label = np.array(val_label)
print("Train Data = ", train_data.shape)
print("Train Label = ", train_label.shape)
print("Validation Data = ", val_data.shape)
print("Validation Label = ", val_label.shape)
from sklearn.preprocessing import LabelEncoder
print("Data sebelum di-normalisasi ", train_data[0][0][0])
x_train = train_data.astype('float32') / 255.0
x_val = val_data.astype('float32') / 255.0
print("Data setelah di-normalisasi ", x_train[0][0][0])
from sklearn.preprocessing import LabelEncoder
print("Label sebelum di-encoder ", train_label[50:110])
lb = LabelEncoder()
y_train = lb.fit_transform(train_label)
y_val = lb.fit_transform(val_label)
print("Label setelah di-encoder", y_train[50:110])
print("Class Label encoder", lb.classes_)
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import InputLayer, Dense, Conv2D, MaxPool2D, GlobalMaxPool2D, GlobalAvgPool2D, Flatten, Dropout
model = Sequential()
model.add(InputLayer(input_shape=[224,224,3]))
model.add(Conv2D(filters=16, kernel_size=3, strides=1, padding='same', activation='relu'))
model.add(MaxPool2D(pool_size=2, padding='same'))
model.add(Conv2D(filters=32, kernel_size=3, strides=1, padding='same', activation='relu'))
model.add(MaxPool2D(pool_size=2, padding='same'))
model.add(Conv2D(filters=64, kernel_size=3, strides=1, padding='same', activation='relu'))
model.add(MaxPool2D(pool_size=2, padding='same'))
#model.add(Dropout(0.2))
model.add(GlobalMaxPool2D())
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
from tensorflow.keras.optimizers import Adam
model.compile(optimizer=Adam(lr=0.001),
loss='binary_crossentropy',
metrics=['acc'])
H = model.fit(x_train, y_train, batch_size=20, epochs=15, validation_data=(x_val, y_val))
有人可以帮我添加增强功能吗?增强功能会在这段代码中起作用吗?