我是深度学习的新手,我遇到了一些错误。
这是我的代码:
import os
import caer
import canaro
import numpy as np
import cv2 as cv
import gc
import matplotlib.pyplot as plt
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.callbacks import LearningRateScheduler
IMG_SIZE = (80,80)
channels = 1
char_path = r"simpsons_dataset"
char_dict = {}
for char in os.listdir(char_path):
char_dict[char] = len(os.listdir(os.path.join(char_path,char)))
# sorth in descending order
char_dict = caer.sort_dict(char_dict, descending=True)
# print(char_dict)
characters = []
count = 0
for i in char_dict:
characters.append(i[0])
count += 1
if count >= 10:
break
print(characters)
# create the training data
train = caer.preprocess_from_dir(char_path, characters, channels=channels, IMG_SIZE=IMG_SIZE, isShuffle=True)
len(train)
plt.figure(figsize=(30,30))
plt.imshow(train[0][0], cmap='gray')
plt.show()
featureSet, labels = caer.sep_train(train, IMG_SIZE=IMG_SIZE)
# Normalize the featureSet ==> (0,1)
featureSet = caer.normalize(featureSet)
labels = to_categorical(labels, len(characters))
x_train, x_val, y_train, y_val = caer.train_val_split(featureSet, labels, val_ratio=.2)
del train
del featureSet
del labels
gc.collect()
BATCH_SIZE = 32
EPOCHS = 10
# Image data generator
datagen = canaro.generators.imageDataGenerator()
train_gen = datagen.flow(x_train, y_train, batch_size=BATCH_SIZE)
# Creating the model. returns the compiled model
model = canaro.models.createSimpsonsModel(IMG_SIZE=IMG_SIZE, channels=channels, output_dim=len(characters),loss='binary_crossentropy', decay=1e-6, learning_rate=0.001, momentum=0.9, nesterov=None)
model.summary()
callbacks_list = [LearningRateScheduler(canaro.lr_schedule())]
training = model.fit(train_gen, steps_per_epoch = len(x_train)//BATCH_SIZE, epochs=EPOCHS, validation_data = (x_val, y_val), validation_steps=len(y_val)//BATCH_SIZE, callbacks = callbacks_list)
我得到的错误:
WARNING:absl:`lr` is deprecated in Keras optimizer, please use `learning_rate` or use the legacy optimizer, e.g.,tf.keras.optimizers.legacy.SGD. <br>
Traceback (most recent call last): <br>
model = canaro.models.createSimpsonsModel(IMG_SIZE=IMG_SIZE, channels=channels, output_dim=len(characters),
optimizer = SGD(lr=learning_rate, decay=decay, momentum=momentum, nesterov=nesterov)
ValueError: decay is deprecated in the new Keras optimizer, pleasecheck the docstring for valid arguments, or use the legacy optimizer
我已经搜索了解决方案,但仍然没有正确的答案。 我该如何解决?
看起来您正在使用引用旧库的代码。 自 Keras 2.3 以来,优化器已弃用 decay 参数。
你基本上有两个选择
Another answer was given here which also points out (here), that if you are not married to canaro you can use following options to get your code to wokr:
TF<2.3 - style
import tensorflow as tf
epochs = 50
learning_rate = 0.01
decay_rate = learning_rate / epochs
optimizer = tf.keras.optimizers.Adam(lr=learning_rate, decay=decay_rate)
TF>=2.3 - 风格
import tensorflow as tf
lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
initial_learning_rate=0.01,
decay_steps=10000,
decay_rate=0.9)
optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)