class TrainInceptionV3GRU(Train): def __init__(self, config=CONFS, existing_model_path='', train_generator=None, validation_generator=None, language="english", score_model=True): super().__init__(config=config) self.language = language self._generator = Flickr8kGenerator(language=language) self._train_generator = train_generator or self._generator.train_generator( ) self._validation_generator = validation_generator or self._generator.validation_generator( ) self._train_data_amount = len(self._generator.train_captions) self._validation_data_amount = len(self._generator.validation_captions) self._tokenizer = self._generator.cp.tokenizer self.model = ImageCaptionModeler().get_model( self._generator.cp.vocab_size, tokenizer=self._generator.cp.tokenizer) self._score_model = score_model # load previous model if exist if os.path.exists(existing_model_path): self.model.load_weights(existing_model_path) def run(self): callback = Callback('InceptionV3GRU_' + self.model.layers[-2].name, language=self.language) self.model.fit_generator(generator=self._train_generator, steps_per_epoch=self._train_data_amount // self._batch_size, epochs=5, validation_data=self._validation_generator, validation_steps=64, callbacks=callback.callbacks) cur_time = datetime.datetime.now() model_path = 'models/InceptionV3GRU_model_' + str( cur_time ) + '_' + self.language + "_" + self.model.layers[-2].name + '.h5' model_weight_path = 'models/InceptionV3GRU_weight_' + str(cur_time) + '_' + self.language + "_" + \ self.model.layers[-2].name + '.h5' self.model.save(model_path) self.model.save_weights(model_weight_path) self.save_tokenizer('models/tokenizer_' + self.language + '.pickle') # Save model for serving self._serving.save_model(self.model) if self._score_model: bs = BleuScore() bs.get_model_score(weight_path=model_weight_path, language=self.language)
class TrainInceptionV3GRU(Train): def __init__(self, config=CONFS, existing_model_path='', train_generator=None, validation_generator=None): super().__init__(config=config) self._generator = Flickr8kGenerator() self._train_generator = train_generator or self._generator.train_generator( ) self._validation_generator = validation_generator or self._generator.validation_generator( ) self._train_data_amount = len(self._generator.train_captions) self._validation_data_amount = len(self._generator.validation_captions) self._tokenizer = self._generator.cp.tokenizer self.model = ImageCaptionModeler().get_model( self._generator.cp.vocab_size) # load previous model if exist if os.path.exists(existing_model_path): self.model.load_weights(existing_model_path) def run(self): callback = Callback('InceptionV3GRU') self.model.fit_generator(generator=self._train_generator, steps_per_epoch=self._train_data_amount // self._batch_size, epochs=10, validation_data=self._validation_generator, validation_steps=16, callbacks=callback.callbacks) cur_time = datetime.datetime.now() self.model.save('models/InceptionV3GRU_model.h5' + str(cur_time)) self.model.save_weights('models/InceptionV3GRU_weight.h5' + str(cur_time)) self.save_tokenizer('models/tokenizer.pickle' + str(cur_time)) # Save model for serving self._serving.save_model(self.model)