def generate(self): model_path = os.path.expanduser(self.model_path) assert model_path.endswith( '.h5'), 'Keras model or weights must be a .h5 file.' # 计算总的种类 self.num_classes = len(self.class_names) + 1 # 载入模型 self.rfb_model = rfb.rfb300(self.model_image_size, self.num_classes) self.rfb_model.load_weights(self.model_path, by_name=True) self.rfb_model.summary() print('{} model, anchors, and classes loaded.'.format(model_path)) # 画框设置不同的颜色 hsv_tuples = [(x / len(self.class_names), 1., 1.) for x in range(len(self.class_names))] self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples)) self.colors = list( map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), self.colors))
NUM_CLASSES = 21 input_shape = (300, 300, 3) priors = get_anchors() bbox_util = BBoxUtility(NUM_CLASSES, priors) # 0.1用于验证,0.9用于训练 val_split = 0.1 with open(annotation_path) as f: lines = f.readlines() np.random.seed(10101) np.random.shuffle(lines) np.random.seed(None) num_val = int(len(lines)*val_split) num_train = len(lines) - num_val model = rfb300(input_shape, num_classes=NUM_CLASSES) model.load_weights("model_data/rfb_weights.h5", by_name=True, skip_mismatch=True) # 训练参数设置 logging = TensorBoard(log_dir=log_dir) checkpoint = ModelCheckpoint(log_dir + 'ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5', monitor='val_loss', save_weights_only=True, save_best_only=False, period=1) reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=2, verbose=1) early_stopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=6, verbose=1) BATCH_SIZE = 4 gen = Generator(bbox_util, BATCH_SIZE, lines[:num_train], lines[num_train:], (input_shape[0], input_shape[1]),NUM_CLASSES) for i in range(21): model.layers[i].trainable = False if True: