简体   繁体   中英

Keras - How to properly use fit() to train a model?

I've been familiarizing myself with Keras and going from the documentation i've put together a basic model and loaded my own folder of images to train rather than using the mnist dataset. Ive gotten to the point of setting up the model, but i'm not sure how to proceed from there in regards to calling my dataset using the fit() method and then training the model to make a prediction. Here is the code so far:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers.experimental.preprocessing import CenterCrop
from tensorflow.keras.layers.experimental.preprocessing import Rescaling
from tensorflow.keras import layers

#Importing the dataset and setting the path
dataset = keras.preprocessing.image_dataset_from_directory(
    'PetImages',
    batch_size = 64,
    image_size = (200, 200)
)

dataset = keras.Input(shape = (None, None, 3))

# PreProcessing layers to better format the datset 
x = CenterCrop(height=150, width=150)(dataset)
x = Rescaling(scale=1.0 / 255)(x)

# Convolution and Pooling Layers
x = layers.Conv2D(filters=32, kernel_size=(3, 3), activation="relu")(x)
x = layers.MaxPooling2D(pool_size=(3, 3))(x)
x = layers.Conv2D(filters=32, kernel_size=(3, 3), activation="relu")(x)
x = layers.MaxPooling2D(pool_size=(3, 3))(x)
x = layers.Conv2D(filters=32, kernel_size=(3, 3), activation="relu")(x)

# Global average pooling to get flat feature vectors
x = layers.GlobalAveragePooling2D()(x)

# Adding a dense classifier 
num_classes = 10
outputs = layers.Dense(num_classes, activation="softmax")(x)

# Instantiates the model once layers have been set
model = keras.Model(inputs = dataset, outputs = outputs)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')

# Problem: Unsure how to further call on dataset to train the model and make a prediction
model.fit()

The .fit() method is the method that actually will train your network so that it behaves in the manner that you want it to train. Models require data in order to be trained. I would take a look at their documentation or some of their examples to see how to go forward with your model as a good starting place.

Depending on the version of tensorflow.keras that you are using, .fit can either take two positional arguments x , and y or it can take a generator object, which is something that acts like a continuously active function. You might also want to set a batch_size , which is essentially how many samples to evaluate at once. Again, the documentation will have a lot more information on what sort of parameters it can take.

In your case, it seems like you've gotten some good input images in the variable dataset (which you promptly overwrite), but you have no labels . Labels define what the expected output is for your training images you input. The first step you'll need is a set of labels, and then, below are some adjustments you can make to your code to make it run:

# Add line below
labels = # ... load labels from someplace, like how you loaded the images

# Change this
dataset = keras.Input(shape = (None, None, 3))
# to this
input_layer = layers.Input(shape=(None, None, 3))

# Change this
model = keras.Model(inputs = dataset, outputs = outputs)
# to this
model = keras.models.Model(inputs = input_layer, outputs = outputs)

# and finally you can fit your model using
model.fit(dataset, labels)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM