def load_model(self, create_raw_model = False): if not self.config["LOAD_MODEL"]: raise ValueError('LOAD_MODEL config must be set to true for inference') if create_raw_model: self.config["LOAD_MODEL"] = False model_factory = ModelFactory(self.config) model = model_factory.create_model(self.model_name) return model
def test_toy_model_factory_prediction(self): tf.random.set_seed(1) model = ModelFactory.create_model(model_name='toy_model', weights=None, input_shape=(32, 32, 3), classes=10) image_1 = np.ones((1, 32, 32, 3)) * 10 assert (np.allclose( model.predict(image_1)[0], [ 0.08292384, 0.05735856, 0.27028584, 0.2666999, 0.02177826, 0.01853362, 0.06498592, 0.04272136, 0.15957771, 0.015135 ])) tf.random.set_seed(None)
def test_invalid_id_of_device_mapping(self): train_strategy = ipu.ipu_strategy.IPUStrategy() with train_strategy.scope(): model = ModelFactory.create_model(model_name='toy_model', weights=None, input_shape=(28, 28, 1), classes=10) with self.assertRaises(DimensionError): model = ModelFactory.configure_model( model=model, gradient_accumulation_count=1, pipeline_splits=['conv2d_1', 'flatten'], device_mapping=[1, 2, 3], pipeline_schedule='Grouped', available_memory_proportion=[])
def get_predictions_for_model(self, model_name: str): tf.random.set_seed(1) np.random.seed(0) image0 = np.zeros((1, 32, 32, 3)) image1 = np.ones((1, 32, 32, 3)) * 10 model = ModelFactory.create_model(model_name=model_name, input_shape=(32, 32, 3), classes=2) image0_preds = model.predict(image0)[0] image1_preds = model.predict(image1)[0] tf.random.set_seed(None) np.random.seed(None) return (image0_preds, image1_preds)
def test_unsupported_model(self): with self.assertRaises(NameError): ModelFactory.create_model(model_name='foo', input_shape=(32, 32, 3), classes=2)
f'steps_per_execution {steps_per_execution} should divide micro_batches_per_epoch = {micro_batches_per_epoch}' ) time_to_train_timer = time_to_train.TimeToTrain() # Create an IPU distribution strategy train_strategy = PopDistStrategy( ) if distributed_training else ipu.ipu_strategy.IPUStrategy() with train_strategy.scope(): # Create an instance of the model model = ModelFactory.create_model( model_name=model_name, input_shape=img_shape, classes=num_classes, accelerator_side_preprocessing_fn= accelerator_side_preprocess_train_fn, eight_bit_transfer=eight_bit_transfer) model = ModelFactory.configure_model( model=model, gradient_accumulation_count=batch_config. gradient_accumulation_count, pipeline_splits=pipeline_splits, device_mapping=device_mapping, pipeline_schedule=pipeline_schedule, available_memory_proportion=available_memory_proportion, optimizer_state_offloading=optimizer_state_offloading) if training:
if args.dev: models.clear() models.append(TrainingParameters('dev', 128)) models.append(TrainingParameters('dev1', 128)) mf = ModelFactory() scores = [] for m in models: # Create directory for model model_dir = os.path.join('models', str(m) + ed_sufix + all_sufix + synt_sufix) if os.path.exists(model_dir): shutil.rmtree(model_dir) os.makedirs(model_dir) # JPG only keras_model = mf.create_model(m.model_name, in_shape=(m.in_size, m.in_size, 3), parameters=m.parameters) print ("Model created successfuly. Compiliing") keras_model.model.compile(loss=m.loss, optimizer=m.optimizer, metrics=['accuracy']) print ("Compilation done") print (keras_model.model.summary()) if args.multi_gpu: keras_model.model = to_multi_gpu(keras_model.model) keras_model.model.compile(loss=m.loss, optimizer=m.optimizer, metrics=['accuracy']) score = train_model(keras_model, args.batch_size, model_dir, args.epoch, str(m), args.early_stop_patience, args.comb, is_dev=args.dev, data_gen=datagen) scores.append(score) best_model_idx = np.argmax(scores) print("Best model %s with max score %f" % (models[best_model_idx], np.max(scores))) print("Training session finished.")
def generate_model(self): model_factory = ModelFactory(self.config) model = model_factory.create_model(self.model_name) compile_para = self.model_compile_para() model.compile_model(**compile_para) return model