将代码从 TFLearn 转换为在 Keras 中工作
Converting code from TFLearn to work in Keras
我正在使用一些用 TFLearn 编写的代码作为参考,并尝试使用 Keras 重写它。我对这两个包都很陌生,我不确定我是否写得正确。
我已经尝试了我的代码 - 它有效 - 但我没有得到预期的结果(准确度没有提高超过 20+ 个纪元)我想知道我是否在某处犯了错误。
就我的数据而言,我有一个 'data' 目录,其中包含 'train' 和 'validation' 目录。在每一个里面,我的 3 张图片都有 3 个目录 类.
原始 TFLearn 代码:
import numpy as np
import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
def createModel(nbClasses,imageSize):
convnet = input_data(shape=[None, imageSize, imageSize, 1], name='input')
convnet = conv_2d(convnet, 64, 2, activation='elu', weights_init="Xavier")
convnet = max_pool_2d(convnet, 2)
convnet = conv_2d(convnet, 128, 2, activation='elu', weights_init="Xavier")
convnet = max_pool_2d(convnet, 2)
convnet = conv_2d(convnet, 256, 2, activation='elu', weights_init="Xavier")
convnet = max_pool_2d(convnet, 2)
convnet = conv_2d(convnet, 512, 2, activation='elu', weights_init="Xavier")
convnet = max_pool_2d(convnet, 2)
convnet = fully_connected(convnet, 1024, activation='elu')
convnet = dropout(convnet, 0.5)
convnet = fully_connected(convnet, nbClasses, activation='softmax')
convnet = regression(convnet, optimizer='rmsprop', loss='categorical_crossentropy')
model = tflearn.DNN(convnet)
return model
我使用 Keras 的代码:
from keras import backend as K
from keras.layers.core import Flatten, Dense, Dropout, Activation
from keras.optimizers import rmsprop
from keras.models import Sequential
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
from keras.layers import Conv2D, MaxPooling2D, ZeroPadding2D
import numpy as np
num_classes = 3
image_size = 256
nb_epoch = 80
batch_size = 32
nb_train_samples = 7994
nb_validation_samples = 2000
if K.image_data_format() == 'channels_first':
input_shape = (3, image_size, image_size)
else:
input_shape = (image_size, image_size, 3)
model = Sequential()
model.add(ZeroPadding2D((1,1), input_shape=input_shape))
model.add(Conv2D(64, 2, activation='elu', kernel_initializer='glorot_normal'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(128, 2, activation='elu', kernel_initializer='glorot_normal'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(256, 2, activation='elu', kernel_initializer='glorot_normal'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(512, 2, activation='elu', kernel_initializer='glorot_normal'))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('elu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
opt = rmsprop()
model.compile(loss='categorical_crossentropy',
optimizer = opt,
metrics = ['accuracy'])
train_data_dir = 'data/train'
validation_data_dir = 'data/validation'
train_datagen = ImageDataGenerator(rescale= 1./255)
validation_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(image_size, image_size),
batch_size=batch_size,
class_mode='categorical'
)
validation_generator = validation_datagen.flow_from_directory(
validation_data_dir,
target_size=(image_size, image_size),
batch_size=batch_size,
class_mode='categorical'
)
model.fit_generator(train_generator,
steps_per_epoch=(nb_train_samples // batch_size),
epochs=nb_epoch,
validation_data=validation_generator,
validation_steps=(nb_validation_samples // batch_size)
)
model.save_weights('first_try.h5')
我用 MNIST 数据集的三个 类 尝试了你的代码,并且可以很好地训练。正如预期的那样,准确度在第一个时期内增加。
至少对于 MNIST,我可以通过仅使用前两个 Conv 层和一个 64 层的密集层来训练得更快。根据您的数据,我建议您尝试使用更简单的模型(即 2 个 Conv 层),检查是否该模型正在学习,然后从那里改进。
我正在使用一些用 TFLearn 编写的代码作为参考,并尝试使用 Keras 重写它。我对这两个包都很陌生,我不确定我是否写得正确。
我已经尝试了我的代码 - 它有效 - 但我没有得到预期的结果(准确度没有提高超过 20+ 个纪元)我想知道我是否在某处犯了错误。
就我的数据而言,我有一个 'data' 目录,其中包含 'train' 和 'validation' 目录。在每一个里面,我的 3 张图片都有 3 个目录 类.
原始 TFLearn 代码:
import numpy as np
import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
def createModel(nbClasses,imageSize):
convnet = input_data(shape=[None, imageSize, imageSize, 1], name='input')
convnet = conv_2d(convnet, 64, 2, activation='elu', weights_init="Xavier")
convnet = max_pool_2d(convnet, 2)
convnet = conv_2d(convnet, 128, 2, activation='elu', weights_init="Xavier")
convnet = max_pool_2d(convnet, 2)
convnet = conv_2d(convnet, 256, 2, activation='elu', weights_init="Xavier")
convnet = max_pool_2d(convnet, 2)
convnet = conv_2d(convnet, 512, 2, activation='elu', weights_init="Xavier")
convnet = max_pool_2d(convnet, 2)
convnet = fully_connected(convnet, 1024, activation='elu')
convnet = dropout(convnet, 0.5)
convnet = fully_connected(convnet, nbClasses, activation='softmax')
convnet = regression(convnet, optimizer='rmsprop', loss='categorical_crossentropy')
model = tflearn.DNN(convnet)
return model
我使用 Keras 的代码:
from keras import backend as K
from keras.layers.core import Flatten, Dense, Dropout, Activation
from keras.optimizers import rmsprop
from keras.models import Sequential
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
from keras.layers import Conv2D, MaxPooling2D, ZeroPadding2D
import numpy as np
num_classes = 3
image_size = 256
nb_epoch = 80
batch_size = 32
nb_train_samples = 7994
nb_validation_samples = 2000
if K.image_data_format() == 'channels_first':
input_shape = (3, image_size, image_size)
else:
input_shape = (image_size, image_size, 3)
model = Sequential()
model.add(ZeroPadding2D((1,1), input_shape=input_shape))
model.add(Conv2D(64, 2, activation='elu', kernel_initializer='glorot_normal'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(128, 2, activation='elu', kernel_initializer='glorot_normal'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(256, 2, activation='elu', kernel_initializer='glorot_normal'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(512, 2, activation='elu', kernel_initializer='glorot_normal'))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('elu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
opt = rmsprop()
model.compile(loss='categorical_crossentropy',
optimizer = opt,
metrics = ['accuracy'])
train_data_dir = 'data/train'
validation_data_dir = 'data/validation'
train_datagen = ImageDataGenerator(rescale= 1./255)
validation_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(image_size, image_size),
batch_size=batch_size,
class_mode='categorical'
)
validation_generator = validation_datagen.flow_from_directory(
validation_data_dir,
target_size=(image_size, image_size),
batch_size=batch_size,
class_mode='categorical'
)
model.fit_generator(train_generator,
steps_per_epoch=(nb_train_samples // batch_size),
epochs=nb_epoch,
validation_data=validation_generator,
validation_steps=(nb_validation_samples // batch_size)
)
model.save_weights('first_try.h5')
我用 MNIST 数据集的三个 类 尝试了你的代码,并且可以很好地训练。正如预期的那样,准确度在第一个时期内增加。
至少对于 MNIST,我可以通过仅使用前两个 Conv 层和一个 64 层的密集层来训练得更快。根据您的数据,我建议您尝试使用更简单的模型(即 2 个 Conv 层),检查是否该模型正在学习,然后从那里改进。