Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

python - Keras Image Identification Training Model

I've been tasked with creating a training model which will produce and .h5 file at the end. I've been having a lot of trouble with this however and was hoping I could get some guidance. At the moment I'm trying to get it to basically just start training and then create the .h5 file, I will work on normalising the images and such later.

A few things to note, I have more than two categories and my images aren't all the same size. I also don't have a GPU. I was hoping to get some guidance or answers. Thanks for any help you guys can give

######################################################
# Imports
######################################################
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras import layers

######################################################
# Settings and Parameters
######################################################
train_pics = ImageDataGenerator(rescale=1/255)  # setting the value of which we multiply before any other processing
test_pics = ImageDataGenerator(rescale=1/255)  # setting the value of which we multiply before any other processing
trainSet = train_pics.flow_from_directory('Pictures/train/', target_size=(100, 100), batch_size=11,
                                          class_mode='categorical')  # path and data
testSet = train_pics.flow_from_directory('Pictures/test/', target_size=(100, 100), batch_size=20,
                                        class_mode='categorical')  # path and data

######################################################
# Model Creation
######################################################
model = keras.Sequential(
    [
        tf.keras.layers.Conv2D(16, (3, 3), activation='relu', input_shape=(250, 250, 3)),
        tf.keras.layers.MaxPooling2D(2, 2),
        tf.keras.layers.Conv2D(32, (3, 3), activation="relu"),
        tf.keras.layers.MaxPooling2D(2, 2),
        tf.keras.layers.Conv2D(64, (3, 3), activation="relu"),
        tf.keras.layers.MaxPooling2D(2, 2),
        tf.keras.layers.Conv2D(64, (3, 3), activation="relu"),
        tf.keras.layers.MaxPooling2D(2, 2),
        tf.keras.layers.Conv2D(64, (3, 3), activation="relu"),
        tf.keras.layers.MaxPooling2D(2, 2),
        layers.Flatten(),
        layers.Dropout(0.5),
        layers.Dense(8, activation="softmax"),
    ]
)

model.compile(loss='categorical_crossentropy',  # compile the model
              optimizer='adam',
              metrics=['accuracy'])

train = model.fit(  # train the model
    train_pics,
    steps_per_epoch=200,
    epochs=100,
    validation_data=testSet
)

model.save('charactersPics.h5')

This is the output: enter image description here

question from:https://stackoverflow.com/questions/65833214/keras-image-identification-training-model

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're trying to pass in your ImageDataGenerator as an argument to the model. You need to pass in the training set. Change

train = model.fit(train_pics,...)

to

train = model.fit(trainSet,...)

Edit: The warnings are fairly normal in pycharm don't worry about them too much.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...