Keras ImageDataGenerator.flow vs ImageDataGenerator.flow_from_directory не дает одинаковых результатов - PullRequest
0 голосов
/ 09 мая 2020

Я пытаюсь обучить модель VGG16 и сравнивать результаты между процессом непосредственно из памяти и процессом в пакете для моих образов поездов.

Во-первых, я проверил, что в this пост скажите, что поезд из партии занимает больше времени, чем обучение по памяти.

Но меня беспокоит, что у меня не было таких же результатов в производительности модели машинного обучения. Обучение по памяти дало мне около 80% точности, а обучение в пакетном режиме дало мне около 72% точности.

Как это возможно? Что-то я делаю не так с данными предварительной обработки?

Заранее спасибо

Код для обучения из памяти:

# import the necessary packages
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications import VGG16, VGG19
from tensorflow.keras.layers import AveragePooling2D
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Input
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.utils import to_categorical
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
import pandas as pd
from imutils import paths
import matplotlib.pyplot as plt
import numpy as np
import argparse
import time
import cv2
import os
import tensorflow as tf
try:
    tf.config.experimental.set_memory_growth(tf.config.experimental.list_physical_devices('GPU')[0],True)
except:
    print("Memory already set to growth")


dataset="data_in/Valencia/train/"



# initialize the initial learning rate, number of epochs to train for,
# and batch size
INIT_LR = 1e-3
EPOCHS = 25
BS = 32
num_px=256


# grab the list of images in our dataset directory, then initialize
# the list of data (i.e., images) and class images
print("[INFO] loading images...")
train = pd.read_csv("data_in/Valencia/train_split.txt", sep=' ', header=None, names=['id','Image','Class']).drop('id',axis=1)
data = []
labels = train.Class.to_list()

# loop over the image paths
for i in range(train.shape[0]):
  print(str(i)+" from "+str(train.shape[0]))
  imagePath=dataset+train["Image"][i]
  image = cv2.imread(imagePath, cv2.IMREAD_GRAYSCALE)
  image = cv2.resize(image, (num_px, num_px)).astype(np.float16)
  image = np.dstack([image] * 3)

  # update the data and labels lists, respectively
  data.append(image)

print("End: preprocessing images...")

# convert the data and labels to NumPy arrays while scaling the pixel
# intensities to the range [0, 255]
print("Start: rescaling images...")
data = np.array(data) / 255.0
labels = np.array(labels)
print("End: rescaling images...")

# perform one-hot encoding on the labels
lb = LabelBinarizer()
labels = lb.fit_transform(labels)
labels = to_categorical(labels)


# initialize the training data augmentation object
trainAug = ImageDataGenerator(rotation_range=15,fill_mode="nearest")

# load the VGG16 network, ensuring the head FC layer sets are left
# off
print("Start: Downloading VGG16...")
baseModel = VGG16(weights="imagenet", include_top=False, input_tensor=Input(shape=(num_px, num_px, 3)))
print("End: Downloading VGG16...")


# construct the head of the model that will be placed on top of the
# the base model
headModel = baseModel.output
headModel = AveragePooling2D(pool_size=(4, 4))(headModel)
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(128, activation="relu")(headModel)
headModel = Dropout(0.2)(headModel)
headModel = Dense(2, activation="softmax")(headModel)

# place the head FC model on top of the base model (this will become
# the actual model we will train)
model = Model(inputs=baseModel.input, outputs=headModel)

# loop over all layers in the base model and freeze them so they will
# *not* be updated during the first training process
for layer in baseModel.layers:
    layer.trainable = False

# compile our model
print("[INFO] compiling model...")
opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
model.compile(loss="binary_crossentropy", optimizer=opt,
    metrics=["accuracy"])

# train the head of the network
print("[INFO] training head...")
H = model.fit_generator(
    trainAug.flow(data, labels, batch_size=BS),
    steps_per_epoch=len(data) // BS,
    epochs=EPOCHS)

Код для пакетного обучения

from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications import VGG16, VGG19
from tensorflow.keras.layers import AveragePooling2D
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Input
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.utils import to_categorical
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
import pandas as pd
from imutils import paths
import matplotlib.pyplot as plt
import numpy as np
import argparse
import time
import cv2
import os
import tensorflow as tf
try:
    tf.config.experimental.set_memory_growth(tf.config.experimental.list_physical_devices('GPU')[0],True)
except:
    print("Memory already set to growth")

# initialize the initial learning rate, number of epochs to train for,
# and batch size
INIT_LR = 1e-3
EPOCHS = 25
BS = 32
num_px=256

import numpy as np

def gray_to_rgb(image):
    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    image = cv2.resize(image, (256, 256)).astype(np.float16)
    image = np.dstack([image] * 3)
    return image


train_datagen = ImageDataGenerator(preprocessing_function=gray_to_rgb,rotation_range=15,fill_mode="nearest",rescale=1 / 255.0)


train_generator=train_datagen.flow_from_directory('./data_in/Valencia/train_splitted/', # this is where you specify the path to the main data folder
                                                 #target_size=(num_px,num_px),
                                                 color_mode='rgb',
                                                 batch_size=BS,
                                                 class_mode='categorical',
                                                 shuffle=True)


train_generator.next()


# load the VGG16 network, ensuring the head FC layer sets are left
# off
baseModel = VGG16(weights="imagenet", include_top=False, input_tensor=Input(shape=(num_px, num_px, 3)))



# construct the head of the model that will be placed on top of the
# the base model
headModel = baseModel.output
headModel = AveragePooling2D(pool_size=(4, 4))(headModel)
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(128, activation="relu")(headModel)
headModel = Dropout(0.2)(headModel)
headModel = Dense(2, activation="softmax")(headModel)

# place the head FC model on top of the base model (this will become
# the actual model we will train)
model = Model(inputs=baseModel.input, outputs=headModel)

# loop over all layers in the base model and freeze them so they will
# *not* be updated during the first training process
for layer in baseModel.layers:
    layer.trainable = False

# compile our model
print("[INFO] compiling model...")
opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
model.compile(loss="binary_crossentropy", optimizer=opt,
    metrics=["accuracy"])

# train the head of the network
print("[INFO] Training model...")
step_size_train=train_generator.n//train_generator.batch_size
H = model.fit_generator(train_generator,
                        steps_per_epoch=step_size_train,
                        epochs=EPOCHS)
print("[INFO] End training model...")
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...