def make_model(sess, model_to_run, model_path, labels_path, randomize=False,): """Make an instance of a model. Args: sess: tf session instance. model_to_run: a string that describes which model to make. model_path: Path to models saved graph. randomize: Start with random weights labels_path: Path to models line separated class names text file. Returns: a model instance. Raises: ValueError: If model name is not valid. """ if model_to_run == 'InceptionV3': mymodel = model.InceptionV3Wrapper_public( sess, model_saved_path=model_path, labels_path=labels_path) elif model_to_run == 'GoogleNet': # common_typos_disable mymodel = model.GoolgeNetWrapper_public( sess, model_saved_path=model_path, labels_path=labels_path) else: raise ValueError('Invalid model name') if randomize: # randomize the network! sess.run(tf.global_variables_initializer()) return mymodel
def run(self): self.sess = utils.create_session() if self.model_to_run == 'InceptionV3': self.mymodel = model.InceptionV3Wrapper_public( self.sess, self.graph_path, self.labels_path) if self.model_to_run == 'GoogleNet': self.mymodel = model.GoolgeNetWrapper_public( self.sess, self.graph_path, self.labels_path) if self.model_to_run == 'XceptionHPV': self.mymodel = model.XceptionHPVWrapper_public( self.sess, self.graph_path, self.labels_path) act_generator = act_gen.ImageActivationGenerator(self.mymodel, self.source_dir, self.activation_dir, max_examples=100) tf.logging.set_verbosity(0) mytcav = tcav.TCAV(self.sess, self.target, self.concepts, self.bottlenecks, act_generator, self.alphas, cav_dir=self.cav_dir, num_random_exp=10) print('This may take a while... Go get coffee!') results = mytcav.run(run_parallel=False) print('done!') # returns dictionary of plot data plot_data = utils_plot.plot_results( results, os.path.join(self.project_dir, 'results/inceptionv3_tcav.png'), num_random_exp=10)
def make_model(sess, model_to_run, model_path, labels_path): if model_to_run == 'InceptionV3': if '.h5' in model_path: mymodel = load_model(model_path) endpoints_v3 = dict(input=mymodel.inputs[0].name, input_tensor=mymodel.inputs[0], logit=mymodel.outputs[0].name, prediction=mymodel.outputs[0].name, prediction_tensor=mymodel.outputs[0]) mymodel = KerasModelWrapper(sess, labels_path, [299, 299, 3], endpoints_v3, 'InceptionV3_public', (-1, 1)) else: mymodel = model.InceptionV3Wrapper_public( sess, model_saved_path=model_path, labels_path=labels_path) elif model_to_run == 'GoogleNet': mymodel = model.GoogleNetWrapper_public(sess, model_saved_path=model_path, labels_path=labels_path) else: raise ValueError('Invalid model name') return mymodel
def make_model(sess, model_to_run, model_path, labels_path, randomize=False,): """Make an instance of a model. Args: sess: tf session instance. model_to_run: a string that describes which model to make. model_path: Path to models saved graph. randomize: Start with random weights labels_path: Path to models line separated class names text file. Returns: a model instance. Raises: ValueError: If model name is not valid. """ if model_to_run == 'InceptionV3': if '.h5' in model_path: mymodel = load_model(model_path) endpoints = dict( input=mymodel.inputs[0].name, input_tensor=mymodel.inputs[0], logit=mymodel.outputs[0].name, prediction=mymodel.outputs[0].name, prediction_tensor=mymodel.outputs[0]) mymodel = wrapper.KerasModelWrapper(sess, labels_path, [299, 299, 3], endpoints, 'InceptionV3_public', (-1, 1)) else: mymodel = model.InceptionV3Wrapper_public( sess, model_saved_path=model_path, labels_path=labels_path) elif model_to_run == 'GoogleNet': # common_typos_disable mymodel = model.GoolgeNetWrapper_public( sess, model_saved_path=model_path, labels_path=labels_path) else: raise ValueError('Invalid model name') if randomize: # randomize the network! sess.run(tf.global_variables_initializer()) return mymodel