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