Keras使用训练有素的InceptionV3模型+ CIFAR10遇到了有关批处理大小的错误 [英] Keras use trained InceptionV3 model + CIFAR10 got error about Batch Size

查看:674
本文介绍了Keras使用训练有素的InceptionV3模型+ CIFAR10遇到了有关批处理大小的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是机器学习和Keras等的新手.

I am new to Machine Learning and Keras etc.

尝试使用经过训练的模型来提高准确性,在我的情况下,我跟随Pluralsight上的Jerry Kurata使用InceptionV3,仅修改了最后一层以进行识别鸟类的训练.

Trying to use trained model to increase accuracy, in my case I followed Jerry Kurata on Pluralsight to use InceptionV3 and only modify the last layer to train for recognizing birds.

我拥有的数据集来自Keras内置的CIFAR10,这是官方教程

The dataset I have is from Keras built-in CIFAR10 and here is the official tutorial

这是错误消息:

F tensorflow/stream_executor/cuda/cuda_dnn.cc:516]检查失败: cudnnSetTensorNdDescriptor(handle_.get(),elem_type,nd,dims.data(), strides.data())== CUDNN_STATUS_SUCCESS(3 vs.0)batch_descriptor: {count:32个feature_map_count:288个空间:%d 0%d 0 value_min: 0.000000 value_max:0.000000布局:BatchDepthYX}已中止(核心已转储)

F tensorflow/stream_executor/cuda/cuda_dnn.cc:516] Check failed: cudnnSetTensorNdDescriptor(handle_.get(), elem_type, nd, dims.data(), strides.data()) == CUDNN_STATUS_SUCCESS (3 vs. 0)batch_descriptor: {count: 32 feature_map_count: 288 spatial: %d 0%d 0 value_min: 0.000000 value_max: 0.000000 layout: BatchDepthYX} Aborted (core dumped)

我从此处

CIFAR10(32 * 32)中的图像样本太小,这导致 问题

The image samples in CIFAR10 (32*32) is too small which cause this issue

但是我不知道如何解决它.

But I cannot figure out how to fix it.

这是我的代码:

import matplotlib.pyplot as plt
import keras
from keras import backend as K
with K.tf.device("/device:GPU:0"):
    config = K.tf.ConfigProto(intra_op_parallelism_threads=4,
           inter_op_parallelism_threads=4, allow_soft_placement=True,
           device_count = {'CPU' : 1, 'GPU' : 1})
    session = K.tf.Session(config=config)
    K.set_session(session)

from keras.callbacks import EarlyStopping
from keras.applications.inception_v3 import InceptionV3, preprocess_input
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import SGD
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
from keras.datasets import cifar10
# "/device:GPU:0"
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'


def create_generator():
    return ImageDataGenerator(
            featurewise_center=False,  # set input mean to 0 over the dataset
            samplewise_center=False,  # set each sample mean to 0
            featurewise_std_normalization=False,  # divide inputs by std of the dataset
            samplewise_std_normalization=False,  # divide each input by its std
            zca_whitening=False,  # apply ZCA whitening
            zca_epsilon=1e-06,  # epsilon for ZCA whitening
            rotation_range=0,  # randomly rotate images in the range (degrees, 0 to 180)
            # randomly shift images horizontally (fraction of total width)
            width_shift_range=0.1,
            # randomly shift images vertically (fraction of total height)
            height_shift_range=0.1,
            shear_range=0.,  # set range for random shear
            zoom_range=0.,  # set range for random zoom
            channel_shift_range=0.,  # set range for random channel shifts
            # set mode for filling points outside the input boundaries
            fill_mode='nearest',
            cval=0.,  # value used for fill_mode = "constant"
            horizontal_flip=True,  # randomly flip images
            vertical_flip=False,  # randomly flip images
            # set rescaling factor (applied before any other transformation)
            rescale=None,
            # set function that will be applied on each input
            preprocessing_function=None,
            # image data format, either "channels_first" or "channels_last"
            data_format=None,
            # fraction of images reserved for validation (strictly between 0 and 1)
            validation_split=0.0)

Training_Epochs = 1
Batch_Size = 32
Number_FC_Neurons = 1024
Num_Classes = 10

(x_train, y_train), (x_test, y_test) = cifar10.load_data()

# Convert class vectors to binary class matrices.
y_train = keras.utils.to_categorical(y_train, Num_Classes)
y_test = keras.utils.to_categorical(y_test, Num_Classes)


# load cifar10 data here https://keras.io/datasets/

datagen = create_generator()
datagen.fit(x_train)

Inceptionv3_model = InceptionV3(weights='imagenet', include_top=False)
print('Inception v3 model without last FC loaded')

x = Inceptionv3_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(Number_FC_Neurons, activation='relu')(x)
predictions = Dense(Num_Classes, activation='softmax')(x)

model = Model(inputs=Inceptionv3_model.input, outputs=predictions)
# print(model.summary())

print('\nFine tuning existing model')

Layers_To_Freeze = 172
for layer in model.layers[:Layers_To_Freeze]:
    layer.trainable = False
for layer in model.layers[Layers_To_Freeze:]:
    layer.trainable = True

model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy', metrics=['accuracy'])

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255

cbk_early_stopping = EarlyStopping(monitor='val_acc', mode='max')

print(len(x_train))

history_transfer_learning = model.fit_generator(
    datagen.flow(x_train, y_train, batch_size=Batch_Size),
    epochs=Training_Epochs,
    validation_data=(x_test, y_test),
    workers=4,
    steps_per_epoch=len(x_train)//Batch_Size,
    callbacks=[cbk_early_stopping]
)

model.save('incepv3_transfer_cifar10.h5', overwrite=True, include_optimizer=True)

# Score trained model.
scores = model.evaluate(x_test, 12, y_test, verbose=1)
print('Test loss:', scores[0])
print('Test accuracy:', scores[1])

推荐答案

您所说的错误是输入大小的差异.预先训练的Imagenet模型比Cifar-10(32,32)拍摄更大的图像.

Your error as you said is the input size difference. The pre trained Imagenet model takes a bigger size of image than the Cifar-10 (32, 32).

您需要像这样手动指定模型的输入形状.

You need to specify the input_shape of the model before hand like this.

Inceptionv3_model = InceptionV3(weights='imagenet', include_top=False, input_shape=(32, 32, 3))

更多说明,您可以查看

For more explanation you can check this tutorial.

这篇关于Keras使用训练有素的InceptionV3模型+ CIFAR10遇到了有关批处理大小的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆