def __init__(self, project): self.project = project self._dumps = None self.deploy_path = join(project.path, 'deploy') self.lock_path = join(self.deploy_path, 'locked.txt') self.dumps_path = join(self.deploy_path, 'dumps') utils.maybe_makedirs(self.dumps_path) self.remote_dump_local_path = join('deploy', 'dump.sql.gz')
def __init__(self, config_dir=os.path.expanduser('~/.synthese_kiosk'), verbose=False): self._verbose = verbose self._config = None self._config_dir = config_dir self._config_dir = os.path.normpath(os.path.abspath(self._config_dir)) utils.maybe_makedirs(self._config_dir) self._online = False self._force_offline = False self._force_offline_url_to_check = None self._sched = sched.scheduler(time.time, time.sleep) self._config_path = os.path.join(self._config_dir, 'config.json') self._init_logging() log.debug('Config path: %s', self._config_path) if self.config.has_key('webapp_port'): self.WEBAPP_PORT = self.config['webapp_port'] if not self.config['offline_cache_dir']: print "ERROR: Missing parameter 'offline_cache_dir' in your json config" sys.exit(1) self._cache_manager = CacheManager(self.config['offline_cache_dir'], self.config['synthese_url'], self.config['kiosk_config']) self._cache_manager.refresh_kiosk_config() self._kiosk_config = KioskConfig(self.config['offline_cache_dir'], self.config['kiosk_config']) # If we don't have the kiosk_config.json file localy, we # download it now. while not self._kiosk_config.load(): log.info("We have no config yet, will retry later in 10s") time.sleep(10) self._cache_manager.refresh_kiosk_config() # Init refresh timers self._next_config_refresh_date = datetime.now() + \ timedelta(seconds=self._kiosk_config.getConfigRefreshTimeout()) self._next_fallback_refresh_date = datetime.now() + \ timedelta(seconds=self._kiosk_config.getFallBackRefreshTimeout()) if self.config['auto_refresh_timeout']: self._next_browser_refresh_date = datetime.now() + \ timedelta(seconds=self.config['auto_refresh_timeout']) else: self._next_browser_refresh_date = None self._init_displays()
def __init__(self, config_dir=os.path.expanduser('~/.synthese_kiosk'), verbose=False): self._verbose = verbose self._config = None self._config_dir = config_dir self._config_dir = os.path.normpath(os.path.abspath(self._config_dir)) utils.maybe_makedirs(self._config_dir) self._online = False self._force_offline = False self._force_offline_url_to_check = None self._sched = sched.scheduler(time.time, time.sleep) self._config_path = os.path.join(self._config_dir, 'config.json') self._init_logging() log.debug('Config path: %s', self._config_path) if not self.config['offline_cache_dir']: print "ERROR: Missing parameter 'offline_cache_dir' in your json config" sys.exit(1) self._cache_manager = CacheManager(self.config['offline_cache_dir'], self.config['synthese_url']) self._cache_manager.refresh_kiosk_config() self._kiosk_config = KioskConfig(self.config['offline_cache_dir']) # If we don't have the kiosk_config.json file localy, we # download it now. while not self._kiosk_config.load(): log.info("We have no config yet, will retry later in 10s") time.sleep(10) self._cache_manager.refresh_kiosk_config() # Init refresh timers self._next_config_refresh_date = datetime.now() + \ timedelta(seconds=self._kiosk_config.getConfigRefreshTimeout()) self._next_fallback_refresh_date = datetime.now() + \ timedelta(seconds=self._kiosk_config.getFallBackRefreshTimeout()) if self.config['auto_refresh_timeout']: self._next_browser_refresh_date = datetime.now() + \ timedelta(seconds=self.config['auto_refresh_timeout']) else: self._next_browser_refresh_date = None self._init_displays()
def _wget(self, directory, url): log.debug('running wget in ' + directory + " for url " + url) utils.maybe_makedirs(directory) cmd_line = ["wget"] cmd_line.append('--recursive') cmd_line.append('--quiet') cmd_line.append('--no-host-directories') cmd_line.append('--no-parent') cmd_line.append('--page-requisites') cmd_line.append('--convert-links') cmd_line.append('--no-verbose') cmd_line.append('--directory-prefix') cmd_line.append(directory) cmd_line.append(url) log.debug('Wget command line: %s', cmd_line) try: retval = subprocess.call(cmd_line) # Return True if the wget succeeded return retval == 0 except Exception, e: log.error("Failed to launch wget: %s", e)
def deploy_remote_prepare(self): utils.maybe_makedirs(self.deploy_path)
'position': { 'x': {'mean': 0, 'std': 5}, 'y': {'mean': 0, 'std': 5} }, 'velocity': { 'x': {'mean': 0, 'std': 2}, 'y': {'mean': 0, 'std': 2} }, 'acceleration': { 'x': {'mean': 0, 'std': 1}, 'y': {'mean': 0, 'std': 1} } } } maybe_makedirs('../processed') data_columns = pd.MultiIndex.from_product([['position', 'velocity', 'acceleration'], ['x', 'y']]) for desired_source in ['particles']: dataset = dict() for data_class in ['train', 'val', 'test']: with open(data_class + '_data_2_robot.pkl', 'rb') as f: dataset[data_class] = dill.load(f, encoding='latin1') env = Environment(node_type_list=['PARTICLE'], standardization=standardization) attention_radius = dict() attention_radius[(env.NodeType.PARTICLE, env.NodeType.PARTICLE)] = 10.0 env.attention_radius = attention_radius env.robot_type = env.NodeType.PARTICLE scenes = [] data_dict_path = os.path.join('../processed', '_'.join([desired_source, data_class]) + '_2_robot.pkl') # open dataset
def retrain(image_dir): # Create the base model from the pre-trained model InceptionV3 base_model = tf.keras.applications.InceptionV3(input_shape=IMG_SHAPE, include_top=False, pooling='avg', weights='imagenet') # base_model.summary() base_model.compile(loss='mse') input_shape = base_model.output_shape[1:] print("Generating Bottleneck Dataset... this may take some minutes.") bottleneck_train_ds, num_train = get_bottleneck_dataset(base_model, img_dir=image_dir, img_size=IMG_SIZE) train_batches = bottleneck_train_ds.shuffle(10000).batch( BATCH_SIZE).repeat() print("Done generating Bottleneck Dataset") ######### Your code starts here ######### # We want to create a linear classifier which takes the bottleneck data as input # 1. Get the size of the bottleneck tensor. Hint: You can get the shape of a tensor via tensor.get_shape().as_list() # 2. Define a new tf.keras Model which is a linear classifier # 2.1 Define a keras Input (retrain_input) # 2.2 Define the trainable layer (retrain_layer) # 2.3 Define the activation function (retrain activation) # 2.4 Create a new model # 3. Define a loss and a evaluation metric # Fill in the parts indicated by #FILL#. No additional lines are required. input_shape = base_model.output_shape[1:] retrain_model = tf.keras.models.Sequential( [tf.keras.layers.Dense(len(LABELS), input_shape=input_shape)]) loss = tf.keras.losses.CategoricalCrossentropy(from_logits=True) metric = 'accuracy' ######### Your code ends here ######### retrain_model.compile(optimizer=tf.keras.optimizers.SGD(lr=lr), loss=loss, metrics=[metric]) retrain_model.summary() EPOCHS = 1 steps_per_epoch = 5000 tensorboard_callback = tf.keras.callbacks.TensorBoard( log_dir='./retrain_logs', update_freq='batch') retrain_model.fit(train_batches, epochs=EPOCHS, steps_per_epoch=steps_per_epoch) # callbacks=[tensorboard_callback]) ######### Your code starts here ######### # We now want to create the full model using the newly trained classifier # Use tensorflow keras Sequential to stack the base_model and the new layers # Fill in the parts indicated by #FILL#. No additional lines are required. model = tf.keras.models.Sequential([base_model, retrain_model]) ######### Your code ends here ######### model.compile(loss=loss, metrics=[metric]) maybe_makedirs('./trained_models') model.save('./trained_models/trained.h5')
def compute_and_plot_saliency(model, image_path): """ This function computes and plots the saliency plot. You need to compute the matrix M detailed in section 3.1 in K. Simonyan, A. Vedaldi, and A. Zisserman, "Deep inside convolutional networks: Visualising imageclassification models and saliency maps," 2013, Available at https://arxiv.org/abs/1312.6034. :param model: Model which is used :param image_path: Path to the image to be analysed :return: None """ raw_image = tf.dtypes.cast(decode_jpeg(image_path), tf.float32) logits_tensor = model.get_layer("classifier") logits_model = tf.keras.Model(model.input, logits_tensor.output) with tf.GradientTape() as t: ######### Your code starts here ######### # Fill in the parts indicated by #FILL#. No additional lines are # required. ######### Your code ends here ######### plt.subplot(2, 1, 1) plt.imshow(M) plt.title("Saliency with respect to predicted class %s" % LABELS[top_class]) plt.subplot(2, 1, 2) plt.imshow(decode_jpeg(image_path).numpy()) plt.savefig("../plots/saliency.png") plt.show() def plot_classification(image_path, classification_array): nH, nW, _ = classification_array.shape image_data = decode_jpeg(image_path).numpy() aspect_ratio = float(image_data.shape[0]) / image_data.shape[1] plt.figure(figsize=(8, 8 * aspect_ratio)) p1 = plt.subplot(2, 2, 1) plt.imshow(classification_array[:, :, 0], interpolation="none", cmap="jet") plt.title("%s probability" % LABELS[0]) p1.set_aspect(aspect_ratio * nW / nH) plt.colorbar() p2 = plt.subplot(2, 2, 2) plt.imshow(classification_array[:, :, 1], interpolation="none", cmap="jet") plt.title("%s probability" % LABELS[1]) p2.set_aspect(aspect_ratio * nW / nH) plt.colorbar() p2 = plt.subplot(2, 2, 3) plt.imshow(classification_array[:, :, 2], interpolation="none", cmap="jet") plt.title("%s probability" % LABELS[2]) p2.set_aspect(aspect_ratio * nW / nH) plt.colorbar() plt.subplot(2, 2, 4) plt.imshow(image_data) plt.savefig("../plots/detect.png") plt.show() if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--image", type=str) parser.add_argument("--scheme", type=str) FLAGS, _ = parser.parse_known_args() maybe_makedirs("../plots") model = tf.keras.models.load_model("./trained_models/trained.h5") model.__call__ = tf.function(model.__call__) writer = tf.summary.create_file_writer("retrain_logs") tf.summary.trace_on() if FLAGS.scheme == "brute": plot_classification( FLAGS.image, compute_brute_force_classification(model, FLAGS.image, 8, 8), ) elif FLAGS.scheme == "conv": plot_classification( FLAGS.image, compute_convolutional_KxK_classification(model, FLAGS.image), ) elif FLAGS.scheme == "saliency": compute_and_plot_saliency(model, FLAGS.image) else: print("Unrecognized scheme:", FLAGS.scheme) with writer.as_default(): tf.summary.trace_export("detect_%s" % FLAGS.scheme, step=0)
NUM_TEST = 1000 Z_HIDDENS = [20, 20] T_HIDDENS = [] R_HIDDENS = [] A_HIDDENS = None LEARNING_RATE = 0.01 Z_SIZE = 4 BATCH_SIZE = 32 TRAIN_STEPS = 250 NORMS = [0.5, 0.6, 0.7, 0.8, 0.9, 1.0] LAMBDAS = [0.5, 0.1, 0.05, 0.01, 0.005, 0.001] RUNS = 20 SAVE_DIR = "results/env_2d" utils.maybe_makedirs(SAVE_DIR) SAVE_FILE = "1a_medium_norm_lambda.pickle" SAVE_PATH = os.path.join(SAVE_DIR, SAVE_FILE) results = utils.maybe_read_pickle(SAVE_PATH) for norm in NORMS: for lambda_1 in LAMBDAS: for run in range(RUNS): key = (norm, lambda_1, run) if key not in results:
def compute_and_plot_saliency(model, image_path): """ This function computes and plots the saliency plot. You need to compute the matrix M detailed in section 3.1 in K. Simonyan, A. Vedaldi, and A. Zisserman, "Deep inside convolutional networks: Visualising imageclassification models and saliency maps," 2013, Available at https://arxiv.org/abs/1312.6034. :param model: Model which is used :param image_path: Path to the image to be analysed :return: None """ raw_image = tf.dtypes.cast(decode_jpeg(image_path), tf.float32) logits_tensor = model.get_layer('classifier') logits_model = tf.keras.Model(model.input, logits_tensor.output) with tf.GradientTape() as t: ######### Your code starts here ######### ######### Your code ends here ######### plt.subplot(2, 1, 1) plt.imshow(M) plt.title('Saliency with respect to predicted class %s' % LABELS[top_class]) plt.subplot(2, 1, 2) plt.imshow(decode_jpeg(image_path).numpy()) plt.savefig("../plots/saliency.png") plt.show() def plot_classification(image_path, classification_array): nH, nW, _ = classification_array.shape image_data = decode_jpeg(image_path).numpy() aspect_ratio = float(image_data.shape[0]) / image_data.shape[1] plt.figure(figsize=(8, 8*aspect_ratio)) p1 = plt.subplot(2,2,1) plt.imshow(classification_array[:,:,0], interpolation='none', cmap='jet') plt.title('%s probability' % LABELS[0]) p1.set_aspect(aspect_ratio*nW/nH) plt.colorbar() p2 = plt.subplot(2,2,2) plt.imshow(classification_array[:,:,1], interpolation='none', cmap='jet') plt.title('%s probability' % LABELS[1]) p2.set_aspect(aspect_ratio*nW/nH) plt.colorbar() p2 = plt.subplot(2,2,3) plt.imshow(classification_array[:,:,2], interpolation='none', cmap='jet') plt.title('%s probability' % LABELS[2]) p2.set_aspect(aspect_ratio*nW/nH) plt.colorbar() plt.subplot(2,2,4) plt.imshow(image_data) plt.savefig("../plots/detect.png") plt.show() if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--image', type=str) parser.add_argument('--scheme', type=str) FLAGS, _ = parser.parse_known_args() maybe_makedirs("../plots") model = tf.keras.models.load_model('./trained_models/trained.h5') if FLAGS.scheme == 'brute': plot_classification(FLAGS.image, compute_brute_force_classification(model, FLAGS.image, 8, 8)) elif FLAGS.scheme == 'conv': plot_classification(FLAGS.image, compute_convolutional_KxK_classification(model, FLAGS.image)) elif FLAGS.scheme == 'saliency': compute_and_plot_saliency(model, FLAGS.image) else: print('Unrecognized scheme:', FLAGS.scheme)
def retrain(image_dir): # Create the base model from the pre-trained model InceptionV3 base_model = tf.keras.applications.InceptionV3( input_shape=IMG_SHAPE, include_top=False, pooling="avg", weights="imagenet", ) base_model.summary() base_model.compile(loss="mse") print("Generating Bottleneck Dataset... this may take some minutes.") bottleneck_train_ds, num_train = get_bottleneck_dataset( base_model, img_dir=image_dir, img_size=IMG_SIZE ) train_batches = ( bottleneck_train_ds.shuffle(10000).batch(BATCH_SIZE).repeat() ) print("Done generating Bottleneck Dataset") ######### Your code starts here ######### # We want to create a linear classifier which takes the bottleneck data as input # 1. Get the size of the bottleneck tensor. Hint: You can get the shape of a tensor via tensor.get_shape().as_list() # 2. Define a new tf.keras Model which is a linear classifier # 2.1 Define a keras Input (retrain_input) # 2.2 Define the trainable layer (retrain_layer) # 2.3 Define the activation function (retrain activation) # 2.4 Create a new model # 3. Define a loss and a evaluation metric # Fill in the parts indicated by #FILL#. No additional lines are required. ######### Your code ends here ######### retrain_model.compile( optimizer=tf.keras.optimizers.SGD(lr=lr), loss=loss, metrics=[metric] ) retrain_model.summary() EPOCHS = 1 steps_per_epoch = 5000 tensorboard_callback = tf.keras.callbacks.TensorBoard( log_dir="retrain_logs", update_freq="batch" ) retrain_model.fit( train_batches, epochs=EPOCHS, steps_per_epoch=steps_per_epoch, callbacks=[tensorboard_callback], ) ######### Your code starts here ######### # We now want to create the full model using the newly trained classifier # Use tensorflow keras Sequential to stack the base_model and the new layers # Fill in the parts indicated by #FILL#. No additional lines are required. ######### Your code ends here ######### model.compile(loss=loss, metrics=[metric]) maybe_makedirs("trained_models") model.save("trained_models/trained.h5")