使用更多类别重新训练现有的Keras CNN顺序模型(预测16个类别)

以下是创建模型并将其保存到本地目录的代码。这里所有图像都放置在本地标记的文件夹中。现在我想在不同的标记文件夹中添加更多图像,并将它们包含在这个模型中。因此,总的来说,我希望增量添加新标签,而不是从头开始重新训练模型

from keras.layers import Conv2D, Activation, MaxPooling2D, Flatten, Densefrom keras.models import Sequentialfrom keras.optimizers import Adamdef readTestData(testDir):data = []filenames = []# loop over the input imagesimages = os.listdir(testDir)for imageFileName in images:    # load the image, pre-process it, and store it in the data list    imageFullPath = os.path.join(testDir, imageFileName)    #print(imageFullPath)    img = load_img(imageFullPath)    arr = img_to_array(img)  # Numpy array with shape (...,..,3)    arr = cv2.resize(arr, (HEIGHT,WIDTH))     data.append(arr)    filenames.append(imageFileName)    return data, filenames def createModel():    #model = Sequential()    #model.add(Conv2D(20, (5, 5), padding="same", input_shape=inputShape))    #model.add(Activation("relu"))    #model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))    #model.add(Conv2D(50, (5, 5), padding="same"))    #model.add(Activation("relu"))    #model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))    #model.add(Flatten())    #model.add(Dense(500))    #model.add(Activation("relu"))    #model.add(Dense(output_dim=22))    #model.add(Activation("softmax"))    model = load_model('test')    model.pop()    model.pop()    for layer in model.layers:     layer.trainable = False    model.add(Dense(output_dim=24,name='new_Dense',activation='softmax'))    opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)    model.compile(loss="categorical_crossentropy", optimizer=opt, metrics= ["accuracy"])    return model    random.seed(10)X, Y = readTrainData("labelled images directory path")# scale the raw pixel intensities to the range [0, 1]X = np.array(X, dtype="float") / 255.0Y = np.array(Y)# convert the labels from integers to vectorsY =  to_categorical(Y, num_classes=22)(trainX, valX, trainY, valY) = train_test_split(X,Y,test_size=0.10,  random_state=10)aug = ImageDataGenerator(rotation_range=30, width_shift_range=0.1, \height_shift_range=0.1, shear_range=0.2, zoom_range=0.2,\horizontal_flip=True, fill_mode="nearest")# initialize the modelmodel = createModel()# train the networkH = model.fit_generator(aug.flow(trainX, trainY, batch_size=BS), \validation_data=(valX, valY), \steps_per_epoch=len(trainX) // BS, samples_per_epoch=len(trainX) * 5,epochs=EPOCHS, verbose=1)# save the model to diskmodel.save("test_new")

回答:

您可能想要做的是移除最后两层,这些层对应于22的输出维度,然后添加两个新层,对应于新的输出维度(相同但Dense层的维度不同)。

然后,您可以用新数据重新拟合您的模型,如果您只是想要一个好的初始化。然而,如果您想“冻结”模型的权重并且只微调最后几层,您需要将模型的所有层设置为不可训练,然后重新编译模型:

# these lines will remove the last 2 layersmodel.pop()model.pop() # do the following 2 lines only if you want to keep the weights from the first trainingfor layer in model.layers:  layer.trainable = Falsemodel.add(Dense(output_dim=new_output_dim))model.add(Activation("softmax"))# do the following 2 lines only if you want to keep the weights from the first trainingopt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)model.compile(loss="categorical_crossentropy", optimizer=opt, metrics= ["accuracy"])

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注