def get_loss(self): nl = tf.nn.leaky_relu ac = tf.one_hot(self.ac, self.ac_space.n, axis=2) sh = tf.shape(ac) ac = flatten_two_dims(ac) ac_four_dim = tf.expand_dims(tf.expand_dims(ac, 1), 1) def add_ac(x): if x.get_shape().ndims == 2: return tf.concat([x, ac], axis=-1) elif x.get_shape().ndims == 4: sh = tf.shape(x) return tf.concat([ x, ac_four_dim + tf.zeros([ sh[0], sh[1], sh[2], ac_four_dim.get_shape()[3].value ], tf.float32) ], axis=-1) with tf.variable_scope(self.scope): x = flatten_two_dims(self.features) x = unet(x, nl=nl, feat_dim=self.feat_dim, cond=add_ac) x = unflatten_first_dim(x, sh) self.prediction_pixels = x * self.ob_std + self.ob_mean return tf.reduce_mean((x - tf.stop_gradient(self.out_features))**2, [2, 3, 4])
def predict_next(self, reuse): nl = tf.nn.leaky_relu if isinstance(self.ac_space, gym.spaces.Discrete): ac = tf.one_hot(self.ac, get_action_n(self.ac_space), axis=2) else: ac = self.ac sh = tf.shape(ac) ac = flatten_two_dims(ac) ac_four_dim = tf.expand_dims(tf.expand_dims(ac, 1), 1) def add_ac(x): if x.get_shape().ndims == 2: return tf.concat([x, ac], axis=-1) elif x.get_shape().ndims == 4: sh = tf.shape(x) return tf.concat([ x, ac_four_dim + tf.zeros([ sh[0], sh[1], sh[2], ac_four_dim.get_shape()[3].value ], tf.float32) ], axis=-1) with tf.variable_scope(self.scope, reuse=reuse): x = flatten_two_dims(self.features) x = unet(x, nl=nl, feat_dim=self.feat_dim, cond=add_ac) x = unflatten_first_dim(x, sh) self.prediction_pixels = x * self.ob_std + self.ob_mean # return tf.reduce_mean((x - tf.stop_gradient(self.out_features)) ** 2, [2, 3, 4]) return x
def get_loss(self): nl = tf.nn.leaky_relu ac = tf.one_hot(self.ac, self.ac_space.n, axis=2) sh = tf.shape(ac) ac = flatten_two_dims(ac) ac_four_dim = tf.expand_dims(tf.expand_dims(ac, 1), 1) def add_ac(x): if x.get_shape().ndims == 2: return tf.concat([x, ac], axis=-1) elif x.get_shape().ndims == 4: sh = tf.shape(x) return tf.concat( [ x, ac_four_dim + tf.zeros( [ sh[0], sh[1], sh[2], ac_four_dim.get_shape()[3].value ], tf.float32, ), ], axis=-1, ) with tf.variable_scope(self.scope): x = flatten_two_dims(self.features) mu, log_sigma_squared = unet(x, nl=nl, feat_dim=self.feat_dim, cond=add_ac) mu = unflatten_first_dim(mu, sh) log_sigma_squared = unflatten_first_dim(log_sigma_squared, sh) prediction_pixels = mu * self.ob_std + self.ob_mean if self.ama == "true": mse = tf.square(mu - 2 * tf.stop_gradient(self.out_features)) dynamics_reward = tf.reduce_mean((mse - tf.exp(log_sigma_squared)), axis=[2, 3, 4]) if self.clip_ama == "true": dynamics_reward = tf.clip_by_value(dynamics_reward, 0, 1e6) loss = tf.reduce_mean( (tf.exp(-log_sigma_squared) * (mse) + self.uncertainty_penalty * log_sigma_squared), axis=[2, 3, 4], ) elif self.ama == "false": mse = tf.square(mu - tf.stop_gradient(self.out_features)) dynamics_reward = tf.reduce_mean(mse, axis=[2, 3, 4]) loss = dynamics_reward else: raise ValueError("Please specify whether to use AMA or not") return ( loss, dynamics_reward, prediction_pixels, log_sigma_squared, )
patches_val, patches_val_ref) patches_val_ref_aug_h = tf.keras.utils.to_categorical(patches_val_ref_aug, number_class) #%% start_time = time.time() exp = 1 rows = patch_size cols = patch_size adam = Adam(lr=0.0001, beta_1=0.9) batch_size = 8 weights = [0.5, 0.5, 0] loss = weighted_categorical_crossentropy(weights) model = unet((rows, cols, channels)) model.compile(optimizer=adam, loss=loss, metrics=['accuracy']) # print model information model.summary() filepath = 'models/' # define early stopping callback earlystop = EarlyStopping(monitor='val_loss', min_delta=0.0001, patience=10, verbose=1, mode='min') checkpoint = ModelCheckpoint(filepath + 'unet_exp_' + str(exp) + '.h5', monitor='val_loss', verbose=1, save_best_only=True, mode='min')
args.num_classes, args) model = resuneta.model model.summary() model.compile(optimizer=optm, loss=loss, metrics=[ 'accuracy', tf.keras.metrics.TruePositives(), tf.keras.metrics.FalsePositives(), tf.keras.metrics.TrueNegatives(), tf.keras.metrics.FalseNegatives() ]) print('ResUnet-a compiled!') else: model = unet((rows, cols, channels), args.num_classes) model.summary() model.compile(optimizer=optm, loss=loss, metrics=[ 'accuracy', tf.keras.metrics.TruePositives(), tf.keras.metrics.FalsePositives(), tf.keras.metrics.TrueNegatives(), tf.keras.metrics.FalseNegatives() ]) else: # load checkpoint compiled print(f"[INFO] loading {args.checkpoint_path}...") model = load_model(args.checkpoint_path) model.summary()