def handle(self, video_path: str) -> bool: super(KerasHandler, self).handle(video_path) video = VideoObject(video_path) if self.preload: video.load_frames() # --- cutter --- cutter = VideoCutter() res = cutter.cut(video) stable, unstable = res.get_range(threshold=0.98, offset=3) # --- classify --- cl = KerasClassifier() if self.model_path: logger.info("load existed pre-train model") cl.load_model(self.model_path) else: data_home = res.pick_and_save(stable, self.frame_count, to_dir=self.result_path) cl.train(data_home) self.classifier_result = cl.classify(video, stable) # --- draw --- r = Reporter() r.draw(self.classifier_result, report_path=self.result_report_path) return True
def keras_train( train_data_path: str, model_path: str, # options epochs: int = 10, target_size: str = "600x800", overwrite: bool = False, **kwargs, ): from stagesepx.classifier.keras import KerasClassifier # handle args target_size: typing.Sequence[int] = [ int(each) for each in target_size.split("x") ] cl = KerasClassifier( # 轮数 epochs=epochs, # 保证数据集的分辨率统一性 target_size=target_size, **kwargs, ) cl.train(train_data_path) # file existed while os.path.isfile(model_path): logger.warning(f"file {model_path} already existed") model_path = f"{uuid.uuid4()}.h5" logger.debug(f"trying to save it to {model_path}") cl.save_model(model_path, overwrite=overwrite)
def keras_train( train_data_path: str, model_path: str, # options epochs: int = 10, target_size: str = "600x800", overwrite: bool = False, **kwargs, ): from stagesepx.classifier.keras import KerasClassifier assert not os.path.isfile(model_path), f"file {model_path} already existed" # handle args target_size: typing.Sequence[int] = [ int(each) for each in target_size.split("x") ] cl = KerasClassifier( # 轮数 epochs=epochs, # 保证数据集的分辨率统一性 target_size=target_size, **kwargs, ) cl.train(train_data_path) cl.save_model(model_path, overwrite=overwrite)
def test_keras(): # set epochs to 1 for quickly training (test only) cl = KerasClassifier(epochs=1) cl.train(CUTTER_RESULT_DIR) cl.save_model("haha.h5") # recreate cl = KerasClassifier() cl.load_model("haha.h5") stable, _ = cutter_res.get_range() classify_result = cl.classify(VIDEO_PATH, stable, keep_data=True) assert classify_result.to_dict()
def train_model_Keras(_train_picture_path, _model_file_name): cl = KerasClassifier( # 轮数 epochs=10, compress_rate=0.2, # 保证数据集的分辨率统一性 # target_size=(600, 800), ) cl.train(_train_picture_path) cl.save_model(_model_file_name, overwrite=True) return cl
# overwrite this method to design your own model structure! # model = Sequential() # ... pass def train(self, data_path: str, *_, **__): # ... pass # or use the default one # and then init it # epochs=1 is just a example cl = KerasClassifier(epochs=1) # train model and save weights cl.train(data_home) cl.save_model("keras_model.h5") # you would better reuse the trained model for less time cost # keras model takes much more time than SVM # cl.load_model("keras_model.h5") classify_result = cl.classify(video, stable, keep_data=True) result_dict = classify_result.to_dict() # --- draw --- r = Reporter() r.draw(classify_result)
''' * @Description : 开始根据图像数据训练模型及预测 * @Autor : Tommy * @Date : 2021-02-21 18:51:11 * @LastEditors : Tommy * @LastEditTime : 2021-02-26 14:46:49 ''' from stagesepx.classifier.keras import KerasClassifier # 训练模型文件 cl = KerasClassifier( # 设置这个数,需要保证小于总样本数,否则报错。 # nb_train_samples=30, # 训练轮数 epochs=30) cl.train('./stable_frame') cl.save_model('./model.h5', overwrite=True)
cutter = VideoCutter() # 计算每一帧视频的每一个block的ssim和psnr。 res = cutter.cut(video, block=6) # 计算出哪些区间是稳定的,哪些是不稳定的。判断A帧到B帧之间是稳定还是不稳定 stable, unstable = res.get_range(threshold=0.97, offset=2) # 把分好类的稳定阶段的图片存本地 res.pick_and_save(stable, 100, to_dir='./picture/train_stable_frame', meaningful_name=True) # 训练模型文件 cl = KerasClassifier( # 训练轮数 epochs=10) cl.train('./train_stable_frame') cl.save_model('./model.h5', overwrite=True) # 使用Keras方法进行预测 cl = KerasClassifier() cl.load_model('./model.h5') # 将视频切分成帧 file_name = './video_for_forecast.mp4' # 预加载,大幅度提升分析速度 video = VideoObject(file_name, pre_load=True) # 新建帧,计算视频总共有多少帧,每帧多少ms video.load_frames() # 压缩视频 cutter = VideoCutter() # 这个hook是干什么的,后续做解释