【发布时间】:2019-01-29 22:42:02
【问题描述】:
我尝试将我的数据集转换为生成器函数。
我使用大数据进行飞行。所以创建生成器函数来从数据集中读取一些数据。
现在我创建模型并使用小数据集训练模型。 它的准确率在 90-95% 左右。
但是,我尝试将相同的数据集与生成器函数一起使用,但它提供了不同的结果。
batch_size = 10
num_classes = 25
epochs_step = 10
img_rows, img_cols = 128, 128
ds_gen = FA.File_Helper()
# loading the data
(x_train, y_train,x_test,y_test) = ds_gen.unpackData(size=128)
# #
print(x_train.shape) #(11250,128,128,3)
print(y_train.shape) #(11250,)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /=255
y_train -=1 #reindex to 0
y_test -=1 #reindex to 0
input_shape = (img_rows, img_cols, 3)
print(input_shape) #(11250,128,128,3)
# # # the model
model = pretrained_model(input_shape,num_classes) #loss='sparse_categorical_crossentropy',
weigth_file="./weights/PreTrain_weights-improvement-{epoch:02d}-{val_acc:.4f}.hdf5"
checkpoint = ModelCheckpoint(weigth_file, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
tbCallBack = TensorBoard(log_dir='./Graph/Pretrain_128Px', histogram_freq=0, write_graph=True, write_images=True)
callbacks_list = [checkpoint,tbCallBack]
model.fit(x_train,y_train,
batch_size=10,
initial_epoch=0,
epochs=100,
verbose=1,
validation_data=(x_test,y_test),
callbacks = callbacks_list)
这段代码,我得到了 90-95% 的 acc,我创建了一个生成器函数。
batch_size = 10
num_classes = 25
epochs_step = 10
img_rows, img_cols = 128, 128
def data_genArray(batch_size=10):
ds_gen = FA.File_Helper()
# # # # loading the data
(x_train, y_train) = ds_gen.unpackDataTrain(size=128)
x_train = x_train.astype('float32')
x_train /= 255
y_train -=1
input_batch = np.zeros((batch_size, img_rows,img_cols,3),dtype=np.uint8)
target_batch = np.zeros((batch_size, ),dtype=np.uint8)
while True:
for key in range(y_train.shape[0]):
image = x_train[key]
label = y_train[key]
wrapped = key % batch_size
input_batch[wrapped] = image # rescale to 0-1
target_batch[wrapped] = label
if wrapped == batch_size - 1:
yield input_batch.astype("float32"), target_batch
def data_genArrayTest(batch_size = 10):
ds_gen = FA.File_Helper()
# # # # loading the data
(x_test, y_test) = ds_gen.unpackDataTest(size=128)
x_test = x_test.astype('float32')
x_test /= 255
y_test -= 1
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 3)
input_batch = np.zeros((batch_size, img_rows, img_cols, 3), dtype=np.uint8)
target_batch = np.zeros((batch_size,), dtype=np.uint8)
while True:
for key in range(y_test.shape[0]):
image = x_test[key]
label = y_test[key]
wrapped = key % batch_size
input_batch[wrapped] = image # rescale to 0-1
target_batch[wrapped] = label
if wrapped == batch_size - 1:
yield input_batch.astype("float32"), target_batch
model = pretrained_model(input_shape,num_classes)
weigth_file="./weights/PreTrain_weights-improvement-{epoch:02d}-{val_acc:.4f}.hdf5"
checkpoint = ModelCheckpoint(weigth_file, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
tbCallBack = TensorBoard(log_dir='./Graph/Pretrain_128Px', histogram_freq=0, write_graph=True, write_images=True)
callbacks_list = [checkpoint,tbCallBack]
model.fit_generator(data_genArray(batch_size),
steps_per_epoch=11250/batch_size,
epochs=100,
validation_data=data_genArrayTest(batch_size),
validation_steps=3750/batch_size,
verbose=1,
callbacks=callbacks_list)
此代码提供 acc -> 50-60%
拜托,有人建议我。
【问题讨论】:
-
什么是 FA 和 ds_gen?损失值是否每个时期都在减少?你比较过损失图和纪元图吗?
-
FA 是一个类 ds_gen.unpackDataTrain(size=128) 是一个对象和方法。它返回一个数据集。我复制了 minit.load() 的概念。