# A loss function using mean-squared error def loss(weights, biases): error = prediction(training_inputs, weights, biases) - training_outputs return tf.reduce_mean(tf.square(error)) # Return the derivative of loss with respect tot weight and bias def grad(weights, biases): with tf.GradientTape() as tape: loss_value = loss(weights, biases) return tape.gradient(loss_value, [weights, biases]) train_steps = 200 learning_rate = 0.01 # Start with arbitrary values for M and B on the same batch of data W = tfe.Variable(5.) B = tfe.Variable(10.) print("Initial loss: {:.3f}".format(loss(W, B))) for i in range(train_steps): dW, dB = grad(W, B) W.assign_sub(dW * learning_rate) B.assign_sub(dB * learning_rate) if i % 20 == 0: print("Loss at step {:03d}: {:.3f}".format(i, loss(W, B))) print("Final loss: {:.3f}".format(loss(W, B))) print("W = {:.3f}, B = {:.3f}".format(W.numpy(), B.numpy()))
def attack(self, imgs): """ Return a tensor that constructs adversarial examples for the given input. Generate uses tf.py_func in order to operate over tensors. :param x: A tensor with the inputs. :param kwargs: See `parse_params` """ imgs = tf.cast(imgs, tf.float32) preds = self.fn_logits(imgs) preds_max = tf.reduce_max(preds, 1, keepdims=True) original_predictions = tf.to_float(tf.equal(preds, preds_max)) labs = tf.stop_gradient(original_predictions) repeat = self.binary_search_steps >= 10 shape = tf.shape(imgs) # # the variable we're going to optimize over # modifier = tfe.Variable(tf.zeros(shape, dtype=tf_dtype)) def compute_newimage(imgs, modifier): # the resulting instance, tanh'd to keep bounded from clip_min # to clip_max newimg = (tf.tanh(modifier + imgs) + 1) / 2 newimg = newimg * (self.clip_max - self.clip_min) + self.clip_min return newimg def get_l2dist(imgs, newimg): # distance to the input data other = (tf.tanh(imgs) + 1) / 2 * (self.clip_max - self.clip_min) + self.clip_min sum_axis = list(range(1, len(shape.numpy()))) l2dist = tf.reduce_sum(tf.square(newimg - other), sum_axis) return l2dist def loss(timg, tlab, const, modifier): newimg = compute_newimage(timg, modifier) # prediction BEFORE-SOFTMAX of the model if self.sample <= 1: output = self.fn_logits(newimg) else: logging.info( "Monte Carlo (MC) on attacks, sample: {}".format(self.sample)) for i in range(self.sample): logits = self.fn_logits(newimg) if i == 0: assert logits.op.type != 'Softmax' output.append(logits) output = tf.reduct_mean(output, 0) # distantce to the input data l2dist = get_l2dist(timg, newimg) # compute the probability of the label class versus the maximum other real_target = tf.reduce_sum((tlab) * output, 1) other_target = tf.reduce_max((1 - tlab) * output - tlab * 10000, 1) zero = tf.constant(0., dtype=tf_dtype) if self.y_target: # if targeted, optimize for making the other class most likely loss1 = tf.maximum(zero, other_target - real_target + self.confidence) else: # if untargeted, optimize for making this class least likely. loss1 = tf.maximum(zero, real_target - other_target + self.confidence) # sum up the losses loss2 = tf.reduce_sum(l2dist) loss1 = tf.reduce_sum(const * loss1) loss = loss1 + loss2 return loss, output def grad(imgs, labs, const, modifier): with tf.GradientTape(watch_accessed_variables=False) as tape: tape.watch(modifier) loss_value, logits = loss(imgs, labs, const, modifier) with tape.stop_recording(): gradients = tape.gradient(loss_value, [modifier]) return gradients, loss_value, logits def compare_multi(x, y): x_array = tf.unstack(x) if self.y_target: x_array[y] = x_array[y] - self.confidence else: x_array[y] = x_array[y] + self.confidence x = tf.argmax(tf.stack(x_array)) if self.y_target: return x == y else: return x != y def compare_single(x, y): if self.y_target: return x == y else: return x != y # batch_size = tf.shape(imgs)[0] batch_size = imgs.get_shape().as_list()[0] # re-scale instances to be within range [0, 1] imgs = (imgs - self.clip_min) / (self.clip_max - self.clip_min) imgs = tf.clip_by_value(imgs, 0, 1) # now convert to [-1, 1] imgs = (imgs * 2) - 1 # convert to tanh-space imgs = tf.atanh(imgs * .999999) # set the lower and upper bounds accordingly lower_bound = tfe.Variable(tf.zeros(batch_size), trainable=False) const = tfe.Variable(tf.ones(batch_size) * self.initial_const, trainable=False) upper_bound = tfe.Variable(tf.ones(batch_size) * 1e10, trainable=False) # placeholders for the best l2, score, and instance attack found so far o_bestl2 = tfe.Variable(tf.constant(1e10, shape=(batch_size, )), trainable=False) o_bestscore = tfe.Variable(tf.constant(-1, shape=(batch_size, )), trainable=False) o_bestattack = tfe.Variable(tf.identity(imgs), trainable=False) for outer_step in range(self.binary_search_steps): # completely reset adam's internal state. modifier = tfe.Variable(tf.zeros(shape, dtype=tf_dtype)) optimizer = tf.train.AdamOptimizer(self.learning_rate) bestl2 = tfe.Variable(tf.constant(1e10, shape=(batch_size, )), trainable=False) bestscore = tfe.Variable(tf.constant(-1, shape=(batch_size, )), trainable=False) logging.info(" Binary search step %s of %s", outer_step, self.binary_search_steps) # The last iteration (if we run many steps) repeat the search once. if repeat and outer_step == self.binary_search_steps - 1: const = upper_bound prev = 1e6 for iteration in range(self.max_iterations): import resource, gc mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss logging.info('resource {}'.format(mem)) gc.collect() tf.set_random_seed(np.random.randint(0, 100)) # perform the attack gradients, loss_value, scores = grad(imgs, labs, const, modifier) optimizer.apply_gradients(zip(gradients, [modifier])) nimg = compute_newimage(imgs, modifier) l2s = get_l2dist(imgs, nimg) if iteration % ((self.max_iterations // 10) or 1) == 0 and \ logging.get_verbosity() == logging.DEBUG: l2_mean = tf.reduce_mean(l2s).numpy() logging.debug( " Iteration {} of {}: loss={:.3g} l2={:.3g}".format( iteration, self.max_iterations, loss_value, l2_mean)) # check if we should abort search if we're getting nowhere. if self.abort_early and \ iteration % ((self.max_iterations // 10) or 1) == 0: if loss_value > prev * .9999: logging.debug(" Failed to make progress; stop early" ) break prev = loss_value # adjust the best result found so far for e, (l2, sc, ii) in enumerate(zip(l2s, scores, nimg)): lab = tf.argmax(labs[e]) comp = compare_multi(sc, lab) if l2 < bestl2[e] and comp: bestl2[e].assign(l2) bestscore[e].assign(tf.argmax(sc, output_type=tf.int32)) if l2 < o_bestl2[e] and comp: o_bestl2[e].assign(l2) o_bestscore[e].assign(tf.argmax(sc, output_type=tf.int32)) o_bestattack[e].assign(ii) # adjust the constant as needed for e in range(batch_size): if compare_single(bestscore[e], tf.argmax(labs[e])) and bestscore[e] != -1: # success, divide const by two upper_bound[e].assign(tf.minimum(upper_bound[e], const[e])) if upper_bound[e] < 1e9: const[e].assign((lower_bound[e] + upper_bound[e]) / 2) else: # failure, either multiply by 10 if no solution found yet # or do binary search with the known upper bound lower_bound[e].assign(tf.maximum(lower_bound[e], const[e])) if upper_bound[e] < 1e9: const[e].assign((lower_bound[e] + upper_bound[e]) / 2) else: const[e].assign(const[e]*10) if logging.get_verbosity() == logging.DEBUG: success = tf.cast(tf.less(upper_bound, 1e9), tf.int32) logging.debug(" Successfully generated adversarial examples " + "on {} of {} instances.".format( tf.reduce_sum(success), batch_size)) mask = tf.less(o_bestl2, 1e9) mean = tf.reduce_mean(tf.sqrt(tf.boolean_mask(o_bestl2, mask))) logging.debug(" Mean successful distortion: {:.4g}".format(mean.numpy())) # return the best solution found success = tf.cast(tf.less(upper_bound, 1e9), tf.int32) logging.info(" Successfully generated adversarial examples " + "on {} of {} instances.".format( tf.reduce_sum(success), batch_size)) mask = tf.less(o_bestl2, 1e9) mean = tf.reduce_mean(tf.sqrt(tf.boolean_mask(o_bestl2, mask))) logging.info(" Mean successful distortion: {:.4g}".format(mean.numpy())) return o_bestattack.read_value()
# axarr[2].hist(w3, 40) # axarr[2].grid(True) plt.title('temp = ' + str(temp)) plt.savefig(str(inst) + '_weights_temp_' + str(temp) + '.png') plt.show() ################################################################################### startTime = datetime.now() learn_rate = 3e-4 model2 = MyModel() learning_rate = tfe.Variable(learn_rate, name='learning_rate') optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate) optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) model_objects = { 'model': MyModel(), 'optimizer': optimizer, 'learning_rate': learning_rate, 'step_counter': tf.train.get_or_create_global_step(), } logdir = './test2' writer = tf.contrib.summary.create_file_writer(logdir) ## Tensorboard global_step = tf.train.get_or_create_global_step() writer.set_as_default() checkpoint_dir = './ckpt'
def run_style_transfer(content_path, style_path, num_iterations=1000, content_weight=1e3, style_weight=1e-2): # We don't need to (or want to) train any layers of our model, so we set their # trainable to false. model = get_model() for layer in model.layers: layer.trainable = False # Get the style and content feature representations (from our specified intermediate layers) style_features, content_features = get_feature_representations( model, content_path, style_path) gram_style_features = [ gram_matrix(style_feature) for style_feature in style_features ] # Set initial image init_image = load_and_process_img(content_path) init_image = tfe.Variable(init_image, dtype=tf.float32) # Create our optimizer opt = tf.train.AdamOptimizer(learning_rate=5, beta1=0.99, epsilon=1e-1) # For displaying intermediate images iter_count = 1 # Store our best result best_loss, best_img = float('inf'), None # Create a nice config loss_weights = (style_weight, content_weight) cfg = { 'model': model, 'loss_weights': loss_weights, 'init_image': init_image, 'gram_style_features': gram_style_features, 'content_features': content_features } # For displaying num_rows = 2 num_cols = 5 display_interval = num_iterations / (num_rows * num_cols) start_time = time.time() global_start = time.time() norm_means = np.array([103.939, 116.779, 123.68]) min_vals = -norm_means max_vals = 255 - norm_means imgs = [] for i in range(num_iterations): grads, all_loss = compute_grads(cfg) loss, style_score, content_score = all_loss opt.apply_gradients([(grads, init_image)]) clipped = tf.clip_by_value(init_image, min_vals, max_vals) init_image.assign(clipped) end_time = time.time() if loss < best_loss: # Update best loss and best image from total loss. best_loss = loss best_img = deprocess_img(init_image.numpy()) if i % display_interval == 0: start_time = time.time() # Use the .numpy() method to get the concrete numpy array plot_img = init_image.numpy() plot_img = deprocess_img(plot_img) imgs.append(plot_img) IPython.display.clear_output(wait=True) IPython.display.display_png(Image.fromarray(plot_img)) print('Iteration: {}'.format(i)) print('Total loss: {:.4e}, ' 'style loss: {:.4e}, ' 'content loss: {:.4e}, ' 'time: {:.4f}s'.format(loss, style_score, content_score, time.time() - start_time)) print('Total time: {:.4f}s'.format(time.time() - global_start)) IPython.display.clear_output(wait=True) plt.figure(figsize=(14, 4)) for i, img in enumerate(imgs): plt.subplot(num_rows, num_cols, i + 1) plt.imshow(img) plt.xticks([]) plt.yticks([]) return best_img, best_loss
def __init__(self, initial_weight): super(ODEModel, self).__init__() self.Weights = tfe.Variable(initial_weight, dtype=tf.float32)
# In order to use eager execution, `tfe.enable_eager_execution()` must be # called at the very beginning of a TensorFlow program. ############################# ########## TO DO ############ ############################# tfe.enable_eager_execution() # Read the data into a dataset. data, n_samples = utils.read_birth_life_data(DATA_FILE) dataset = tf.data.Dataset.from_tensor_slices((data[:, 0], data[:, 1])) # Create weight and bias variables, initialized to 0.0. ############################# ########## TO DO ############ ############################# w = tfe.Variable(0.0) # use tfe.Variable b = tfe.Variable(0.0) # Define the linear predictor. def prediction(x): ############################# ########## TO DO ############ ############################# return w * x + b # Define loss functions of the form: L(y, y_predicted) def squared_loss(y, y_predicted): ############################# ########## TO DO ############
import tensorflow as tf import tensorflow.contrib.eager as tfe from tensorflow.examples.tutorials.mnist import input_data tfe.enable_eager_execution() with tf.device("/cpu:0"): mnist = input_data.read_data_sets("./datasets/MNIST/MNIST-data", one_hot=True) x, y = mnist.train.next_batch(100) with tf.GradientTape() as tape: w = tfe.Variable(tf.zeros([784, 10])) b = tfe.Variable(tf.zeros([10])) logits = tf.nn.softmax(tf.matmul(x, w) + b) loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=y)) grads = tape.gradient(loss, [w, b]) optimizer = tf.train.AdamOptimizer(0.0001) for i in range(1000): optimizer.apply_gradients( zip(grads, [w, b]), global_step=tf.train.get_or_create_global_step()) print(loss)
def main(argv): CONTENT_PATH = "my_content.jpg" STYLE_PATH = "my_style.jpg" VGG_MODEL_PATH = "imagenet-vgg-verydeep-19" CONTENT_LAYER = 'block5_conv2' STYLE_LAYERS = [ 'block1_conv1', 'block2_conv1', 'block3_conv1', 'block4_conv1', 'block5_conv1' ] num_iterations = 1000 norm_means = np.array([103.939, 116.779, 123.68]) min_vals = -norm_means max_vals = 255 - norm_means #model = load_vgg_model(VGG_MODEL_PATH) model = get_vgg_model(STYLE_LAYERS, CONTENT_LAYER) content_activations, style_activations = initialize_activations( CONTENT_PATH, STYLE_PATH, model, STYLE_LAYERS, CONTENT_LAYER) # TODO - The output size must sync with content image. Also why not initialize noise to average of content input_image = generate_noise_image( reshape_and_normalize_image(scipy.misc.imread(CONTENT_PATH)), 0.4) input_image = tfe.Variable(input_image, dtype=tf.float32) optimizer = tf.train.AdamOptimizer(2.0) for i in range(num_iterations): with tf.GradientTape(persistent=True) as tape: tape.watch(input_image) input_image_vgg = model(tf.cast(input_image, tf.float32)) J, J_content, J_style = compute_total_cost(content_activations, style_activations, input_image_vgg, STYLE_LAYERS, CONTENT_LAYER, 0.3, 0.7) grads = tape.gradient(J, input_image) optimizer.apply_gradients([(grads, input_image)]) clipped = tf.clip_by_value(input_image, min_vals, max_vals) input_image.assign(clipped) #print("###Loss J ######" + str(J)) #print("###Loss J ######" + str(J_style)) #print("###Loss J ######" + str(J_content)) # Print every 20 iteration. if i % 20 == 0: #J, Jc, Js = compute_total_cost(content_activations,style_activations,model,CONTENT_LAYER) print("Iteration " + str(i) + " :") print("total cost = " + str(J)) print("content cost = " + str(J_content)) print("style cost = " + str(J_style)) # save current generated image in the "/output" directory save_image("output/" + str(i) + ".png", input_image) # save last generated image #generated_image = deprocess_img(input_image.numpy()) save_image('output/generated_image.jpg', input_image) #print(out) #print(model) return
def run_style_transfer(content_path, style_path, content_layers, style_layers, output_img=None, num_iterations=1000, content_weight=1e3, style_weight=1e-2, show_plots=True): display_num = 100 # We don't need to (or want to) train any layers of our model, so we set their # trainable to false. model = get_model(content_layers, style_layers) for layer in model.layers: layer.trainable = False # Get the style and content feature representations (from our specified intermediate layers) style_features, content_features = get_feature_representations( model, content_path, style_path, len(style_layers)) gram_style_features = [ gram_matrix(style_feature) for style_feature in style_features ] # Set initial image init_image = load_and_process_img(content_path) init_image = tfe.Variable(init_image, dtype=tf.float32) # Create our optimizer opt = tf.train.AdamOptimizer(learning_rate=5, beta1=0.99, epsilon=1e-1) # For displaying intermediate images iter_count = 1 # Store our best result best_loss, best_img = float('inf'), None # Create a nice config loss_weights = (style_weight, content_weight) cfg = { 'model': model, 'loss_weights': loss_weights, 'init_image': init_image, 'gram_style_features': gram_style_features, 'content_features': content_features, 'num_content_layers': len(content_layers), 'num_style_layers': len(style_layers), } # For displaying plt.figure(figsize=(14, 7)) num_rows = (num_iterations / display_num) // 5 start_time = time.time() global_start = time.time() norm_means = np.array([103.939, 116.779, 123.68]) min_vals = -norm_means max_vals = 255 - norm_means print('Running style transfer for %d iterations...' % num_iterations) for i in range(num_iterations): grads, all_loss = compute_grads(cfg) loss, style_score, content_score = all_loss # grads, _ = tf.clip_by_global_norm(grads, 5.0) opt.apply_gradients([(grads, init_image)]) clipped = tf.clip_by_value(init_image, min_vals, max_vals) init_image.assign(clipped) end_time = time.time() if loss < best_loss: # Update best loss and best image from total loss. best_loss = loss best_img = init_image.numpy() if i % display_num == 0: print('Iteration: {:03d}: ' 'Total loss: {:.4e}, ' 'style loss: {:.4e}, ' 'content loss: {:.4e}, ' 'time: {:.4f}s'.format(i, loss, style_score, content_score, time.time() - start_time)) start_time = time.time() # Display intermediate images if iter_count > num_rows * 5: continue elif show_plots: plt.subplot(num_rows, 5, iter_count) # Use the .numpy() method to get the concrete numpy array plot_img = init_image.numpy() plot_img = deprocess_img(plot_img) plt.imshow(plot_img) plt.title('Iteration {}'.format(i + 1)) iter_count += 1 print('Total time: {:.4f}s'.format(time.time() - global_start)) print('Best Loss: {:.1f}'.format(best_loss)) if show_plots: plt.show() show_results(best_img, content_path, style_path, show_large_final=True) if output_img: x = deprocess_img(best_img) save_image(x, output_img)
# x轴的点坐标 train_x = np.asarray([ 3.3, 4.4, 5.5, 6.71, 6.93, 4.168, 9.779, 6.182, 7.59, 2.167, 7.042, 10.791, 5.313, 7.997, 5.654, 9.27, 3.1 ]) # y轴的点坐标 train_y = np.asarray([ 1.7, 2.76, 2.09, 3.19, 1.694, 1.573, 3.366, 2.596, 2.53, 1.221, 2.827, 3.465, 1.65, 2.904, 2.42, 2.94, 1.3 ]) n_samples = train_x.shape[0] print("train_x", train_x.shape) print("train_y", train_x.shape) # 模型的权重 w = tfe.Variable(np.random.randn(), name="weight") # 模型的偏置 b = tfe.Variable(np.random.randn(), name="bias") # 线性模型 def line_model(inputs): return inputs * w + b # 平方差 def mean_square(model, x, y): return tf.reduce_sum(tf.pow(model(x) - y, 2)) / (2 * n_samples) # 随机梯度下降
# Get points to estimate points = generate_2d_point_cloud('circle', n_dots, rs=radiuses, show=0) if video_output: fig = plt.figure(1) plt.plot(points[:, 0], points[:, 1], 'bo') h_last = None curr_est_points = np.zeros((0, 2)) while curr_est_points.shape[0] < n_dots_to_train: # Variable to train tmp = np.random.uniform(-4, 4, (n_dots_to_add, 2)) tmp = np.vstack((curr_est_points, tmp)) vr_points = tfe.Variable(tmp, dtype=tf.float32) print vr_points.shape for iter in range(n_iters): dPoints, = grad(points, vr_points) vr_points.assign_sub(dPoints * learning_rate) curr_est_points = np.array(vr_points.value()) if video_output: plt.title((curr_est_points.shape[0], iter)) if h_last != None: h_last.remove() h_last, = plt.plot(curr_est_points[:, 0], curr_est_points[:, 1], 'r.') plt.axis((-4, 4, -4, 4)) img = figure_2_np_array(fig) video.append_data(img)
channels_image = 1 channels_events = channels - channels_image folder_best_model = args.model_path name_best_model = os.path.join(folder_best_model, 'best') dataset_path = args.dataset loader = Loader.Loader(dataFolderPath=dataset_path, n_classes=n_classes, problemType='segmentation', width=width, height=height, channels=channels_image, channels_events=channels_events) if not os.path.exists(folder_best_model): os.makedirs(folder_best_model) # build model and optimizer model = Segception.Segception_small(num_classes=n_classes, weights=None, input_shape=(None, None, channels)) # optimizer learning_rate = tfe.Variable(lr) #optimizer = tf.train.AdamOptimizer(learning_rate) optimizer = RAdamOptimizer(learning_rate) # Init models (optional, just for get_params function) init_model(model, input_shape=(batch_size, width, height, channels)) variables_to_restore = model.variables # [x for x in model.variables if 'block1_conv1' not in x.name] variables_to_save = model.variables variables_to_optimize = model.variables # Init saver. can use also ckpt = tfe.Checkpoint((model=model, optimizer=optimizer,learning_rate=learning_rate, global_step=global_step) saver_model = tfe.Saver(var_list=variables_to_save) restore_model = tfe.Saver(var_list=variables_to_restore) # restore if model saved and show number of params
def run_style_transfer(content_path, style_path, num_iterations=1000, content_weight=1e3, style_weight=1e-2, content_layers=DEFAULT_CONTENT_LAYERS, style_layers=DEFAULT_STYLE_LAYERS, save_iters=DEFAULT_SAVE_ITERATIONS): # We don't need to (or want to) train any layers of our model, so we set their # trainable to false. model = get_model(content_layers, style_layers) for layer in model.layers: layer.trainable = False # Get the style and content feature representations (from our specified intermediate layers) style_features, content_features = get_feature_representations( model, content_path, style_path, len(content_layers), len(style_layers)) gram_style_features = [ gram_matrix(style_feature) for style_feature in style_features ] # Set initial image init_image = load_and_process_image(content_path) init_image = tfe.Variable(init_image, dtype=tf.float32) # Create our optimizer opt = tf.train.AdamOptimizer(learning_rate=5, beta1=0.99, epsilon=1e-1) # For displaying intermediate images iter_count = 1 # Store our best result best_loss, best_img = float('inf'), None # Create a nice config loss_weights = (style_weight, content_weight) cfg = { 'model': model, 'loss_weights': loss_weights, 'init_image': init_image, 'gram_style_features': gram_style_features, 'content_features': content_features, 'num_content_layers': len(content_layers), 'num_style_layers': len(style_layers) } # For displaying num_rows = 2 num_cols = 5 display_interval = num_iterations / (num_rows * num_cols) start_time = time.time() global_start = time.time() norm_means = np.array([103.939, 116.779, 123.68]) min_vals = -norm_means max_vals = 255 - norm_means imgs = {} for i in range(1, num_iterations + 1): grads, all_loss = compute_grads(cfg) loss, style_score, content_score = all_loss opt.apply_gradients([(grads, init_image)]) clipped = tf.clip_by_value(init_image, min_vals, max_vals) init_image.assign(clipped) end_time = time.time() if loss < best_loss: # Update best loss and best image from total loss. best_loss = loss best_img = deprocess_image(init_image.numpy()) if i in save_iters: imgs[i] = deprocess_image(init_image.numpy()) if i % display_interval == 0: start_time = time.time() # Use the .numpy() method to get the concrete numpy array print('Iteration: {}'.format(i)) print('Total loss: {:.4e}, ' 'style loss: {:.4e}, ' 'content loss: {:.4e}, ' 'time: {:.4f}s'.format(loss, style_score, content_score, time.time() - start_time)) print('Total time: {:.4f}s'.format(time.time() - global_start)) return best_img, best_loss, imgs
def __init__(self): #参数初始化 self.W = tfe.Variable(1.0) self.b = tfe.Variable(1.0)
def driver(content_path, style_path, num_iterations=10, content_weight=1e3, style_weight=1e-2, SAVE_ITERATION=False): # we dont want to train or mess with any layers except the ones we're interested in, so set their trinable to false model = get_model() # disable training in the model for layer in model.layers: layer.trainable = False # get the style and feature representations, for our interested layers (intermidieate) # put the pictures through the model, get the output of the layers we are interested in style_features, content_features = get_feature_representations( model, content_path, style_path) #turn the output in gram style feature gram_style_features = [ gram_matrix(style_feature) for style_feature in style_features ] # load and process inital image, convert it init_image = load_and_process_img(content_path) # make the image into a tensor init_image = tfe.Variable(init_image, dtype=tf.float32) # create our optimizer opt = tf.train.AdamOptimizer(learning_rate=5, beta1=0.99, epsilon=1e-1) # tracker for displaying intermediate images iter_count = 1 # store out best result, initilize variables here best_loss, best_img = float('inf'), None loss_weights = (style_weight, content_weight) # To pass this information around easier cfg = { 'model': model, 'loss_weights': loss_weights, 'init_image': init_image, 'gram_style_features': gram_style_features, 'content_features': content_features } # for displaying and saving num_rows = 2 num_cols = 5 display_interval = num_iterations / (num_rows * num_cols) start_time = time.time() global_start = time.time() # what we want to normilize the mean around, this is what the VGG networks are trained on norm_means = np.array([103.939, 116.779, 123.68]) min_vals = -norm_means max_vals = 255 - norm_means imgs = [] #computing the next seetp in gradient decent. grads, all_loss = compute_grads(cfg) loss, style_score, content_score = all_loss #apply adam optimization opt.apply_gradients([(grads, init_image)]) #clipped is the init_image tensors clipped by min/max clipped = tf.clip_by_value(init_image, min_vals, max_vals) init_image.assign(clipped) # Image in tensor form -> init_image -> whats its loss -> all_loss -> # gradient decent -> change values -> pass it through the model -> whats its loss? counter = 0 for i in tqdm(range(num_iterations - 1)): counter += 1 # computing the next step in gradient decent. loss, style_score, content_score = all_loss grads, all_loss = compute_grads(cfg) # apply adam optimization - version of gradient decent opt.apply_gradients([(grads, init_image)]) # clipped is the init_image tensors clipped by min/max # to allow to deprocessing clipped = tf.clip_by_value(init_image, min_vals, max_vals) init_image.assign(clipped) if loss < best_loss: # updates best loss best_loss = loss best_img = deprocess_img(init_image.numpy()) best_img = Image.fromarray(best_img) if i % 15 == 0 and SAVE_ITERATION == True: # Use the .numpy() method to get the concrete numpy array plot_img = init_image.numpy() plot_img = deprocess_img(plot_img) imgs.append(plot_img) final_image = Image.fromarray(plot_img) # Save the image final_image.save('outputs/' + str(style_name) + '/' + str(style_name) + '-' + str(counter) + '.bmp') if i == num_iterations - 1: # Use the .numpy() method to get the concrete numpy array plot_img = init_image.numpy() plot_img = deprocess_img(plot_img) imgs.append(plot_img) final_image = Image.fromarray(plot_img) # Save the image final_image.save('test.bmp') return best_img
def run_style_transfer(self, content_path, style_path, num_iterations=100, content_weight=1e3, style_weight=1e-2): # We don't need to (or want to) train any layers of our model, so we set their # trainable to false. model = self.get_model() for layer in model.layers: layer.trainable = False # Get the style and content feature representations (from our specified intermediate layers) style_features, content_features = self.get_feature_representations( model, content_path, style_path) gram_style_features = [ self.gram_matrix(style_feature) for style_feature in style_features ] # Set initial image init_image = self.load_and_process_img(content_path) init_image = tfe.Variable(init_image, dtype=tf.float32) # Create our optimizer opt = tf.train.AdamOptimizer(learning_rate=5, beta1=0.99, epsilon=1e-1) # For displaying intermediate images iter_count = 1 # Store our best result best_loss, best_img = float('inf'), None # Create a nice config loss_weights = (style_weight, content_weight) cfg = { 'model': model, 'loss_weights': loss_weights, 'init_image': init_image, 'gram_style_features': gram_style_features, 'content_features': content_features } # For displaying num_rows = 2 num_cols = 5 num_iterations = int(num_iterations) norm_means = np.array([103.939, 116.779, 123.68]) min_vals = -norm_means max_vals = 255 - norm_means imgs = [] for i in range(num_iterations): grads, all_loss = self.compute_grads(cfg) loss, style_score, content_score = all_loss opt.apply_gradients([(grads, init_image)]) clipped = tf.clip_by_value(init_image, min_vals, max_vals) init_image.assign(clipped) if loss < best_loss: # Update best loss and best image from total loss. best_loss = loss best_img = self.deprocess_img(init_image.numpy()) return best_img, best_loss
def run_nst(c_path, s_path, c_layers, s_layers, num_s_layers, num_c_layers, i=1000, c_weight=1e3, s_weight=1e-2): model = vgg_model(c_layers, s_layers) for layer in model.layers: layer.trainable = False c_feats, s_feats = feature_map(model, c_path, s_path, c_layers, s_layers, num_c_layers, num_s_layers) g_s_feats = [gram_matrix(s_feat) for s_feat in s_feats] base_img = process_img(c_path) base_img = tfe.Variable(base_img, dtype=tf.float32) optimizer = tf.train.AdamOptimizer(learning_rate=7, beta1=0.99, epsilon=1e-1) i_count = 1 best_loss, best_img = float('inf'), None loss_weights = (s_weight, c_weight) c = { 'model': model, 'loss_weights': loss_weights, 'base_img': base_img, 'g_s_feats': g_s_feats, 'c_feats': c_feats, 'num_s_layers': num_s_layers, 'num_c_layers': num_c_layers } rows = 2 cols = 5 interval = i / (rows*cols) global_start = time.time() norm_means = np.array([103.939, 116.779, 123.68]) min_vals = -norm_means max_vals = 255 - norm_means imgs = [] for j in range(i): start_time = time.time() grads, loss = compute_gradient(c) loss, s_score, c_score = loss optimizer.apply_gradients([(grads, base_img)]) clipped = tf.clip_by_value(base_img, min_vals, max_vals) base_img.assign(clipped) if loss < best_loss: best_loss = loss best_img = deprocess_img(base_img.numpy()) if j % interval == 0: plot_img = base_img.numpy() plot_img = deprocess_img(plot_img) imgs.append(plot_img) IPython.display.clear_output(wait=True) IPython.display.display_png(Image.fromarray(plot_img)) print('Iteration: {}'.format(j)) print('Total Loss: {:.4e}, ' 'Style Loss: {:.4e},' 'Content Loss: {:.4e}' 'Time: {:.4f}s'.format(loss, s_score, c_score, time.time() - start_time)) print('Total Time: {:.4f}s'.format(time.time() - global_start)) IPython.display.clear_output(wait=True) plt.figure(figsize=(14, 20)) #Image.fromarray(best_img) for k, img in enumerate(imgs): plt.subplot(rows, cols, k+1) plt.imshow(img) plt.xticks([]) plt.yticks([]) return best_img, best_loss
import tensorflow as tf import tensorflow.contrib.eager as tfe tfe.enable_eager_execution() import numpy as np X_raw = np.array([2013, 2014, 2015, 2016, 2017], dtype=np.float32) y_raw = np.array([12000, 14000, 15000, 16500, 17500], dtype=np.float32) X = (X_raw - X_raw.min()) / (X_raw.max() - X_raw.min()) y = (y_raw - y_raw.min()) / (y_raw.max() - y_raw.min()) X = tf.constant(X) y = tf.constant(y) a = tfe.Variable(0., name='a') b = tfe.Variable(0., name='b') num_epoch = 10000 learning_rate = 1e-3 for e in range(num_epoch): # 前向传播 y_pred = a * X + b loss = 0.5 * tf.reduce_sum(tf.square(y_pred - y)) # loss = 0.5 * np.sum(np.square(a * X + b - y)) # 反向传播,手动计算变量(模型参数)的梯度 grad_a = tf.reduce_sum((y_pred - y) * X) grad_b = tf.reduce_sum(y_pred - y) # 更新参数 a, b = a - learning_rate * grad_a, b - learning_rate * grad_b
def run_style_transfer(self, content_path, style_path, content_map_path="", style_map_path="", num_iterations=100, content_weight=1e4, style_weight=1e-2, trans_weight=0): # We don't need to (or want to) train any layers of our model, so we set their # trainable to false. model = self.get_model() for layer in model.layers: layer.trainable = False # Get the style and content feature representations (from our specified intermediate layers) style_features, content_features = self.get_feature_representations( model, content_path, style_path) style_map_features, _ = self.get_feature_representations( model, content_map_path, style_map_path) content_map_features, _ = self.get_feature_representations( model, style_map_path, content_map_path) size = 5 stride = 4 base_style_patches = [] i = 0 style_img = load_img(style_path) for style_feat_img, style_map_img in zip(style_features, style_map_features): print(style_feat_img.shape) print(style_map_img.shape) style_feat_img = tf.concat([style_feat_img, style_map_img], -1) print(style_feat_img.shape) li = tf.squeeze( tf.extract_image_patches(tf.expand_dims(style_feat_img, axis=0), ksizes=[1, size, size, 1], strides=[1, stride, stride, 1], rates=[1, 1, 1, 1], padding='VALID'), 0) li = tf.reshape( li, [((style_feat_img.shape[0] - size) // stride + 1) * ((style_feat_img.shape[1] - size) // stride + 1), -1]) # li = tf.reshape(li, [(style_feat_img.shape[0] - 2) * (style_feat_img.shape[1] - 2), -1]) base_style_patches.append(li) # print( i,len( base_style_patches[i] ), base_style_patches[i][0] ) i += 1 # print(len(base_style_patches)) # Set initial image init_image = load_noise_img(load_and_process_img(content_path)) init_image = load_and_process_img(content_path) init_image = tfe.Variable(init_image, dtype=tf.float32) # Create our optimizer opt = tf.train.AdamOptimizer(learning_rate=50, beta1=0.99, epsilon=1e-1) # For displaying intermediate images iter_count = 1 # Store our best result best_loss, best_img = float('inf'), None # Create a nice config loss_weights = (style_weight, content_weight, trans_weight) cfg = { 'model': model, 'loss_weights': loss_weights, 'init_image': init_image, 'base_style_patches': base_style_patches, 'content_features': content_features, 'content_map_features': content_map_features } # For displaying num_rows = 2 num_cols = 10 display_interval = num_iterations / (num_rows * num_cols) start_time = time.time() global_start = time.time() norm_means = np.array([103.939, 116.779, 123.68]) min_vals = -norm_means max_vals = 255 - norm_means imgs = [] for i in range(num_iterations): print("himmat rakho") grads, all_loss = self.compute_grads(cfg) print("gradient aega") loss, style_score, content_score, trans_score = all_loss opt.apply_gradients([(grads, init_image)]) print("gradient agya") clipped = tf.clip_by_value(init_image, min_vals, max_vals) # print("II 1",init_image) init_image.assign(clipped) # print("II 2",cfg['init_image']) end_time = time.time() if loss < best_loss: # Update best loss and best image from total loss. best_loss = loss best_img = deprocess_img(init_image.numpy()) if i % 1 == 0: start_time = time.time() # Use the .numpy() method to get the concrete numpy array plot_img = init_image.numpy() plot_img = deprocess_img(plot_img) if i % display_interval == 0: imgs.append(plot_img) print('Iteration: {}'.format(i)) print('Total loss: {:.4e}, ' 'style loss: {:.4e}, ' 'content loss: {:.4e}, ' 'trans loss: {:.4e}, ' 'time: {:.4f}s'.format(loss, style_score, content_score, trans_score, time.time() - start_time)) print('Total time: {:.4f}s'.format(time.time() - global_start)) plt.figure(figsize=(14, 4)) for i, img in enumerate(imgs): plt.subplot(num_rows, num_cols, i + 1) plt.imshow(img) plt.xticks([]) plt.yticks([]) plt.savefig(self.results + content_path + '_inter.jpg') return best_img, best_loss
def run_style_transfer(content_path, style_path, num_iterations=1000, content_weight=1e3, style_weight=1e-2): model = get_model() for layer in model.layers: layer.trainable = False style_features, content_features = get_feature_representations( model, content_path, style_path) gram_style_features = [ gram_matrix(style_feature) for style_feature in style_features ] init_image = load_and_process_img(content_path) init_image = tfe.Variable(init_image, dtype=tf.float32) opt = tf.train.AdamOptimizer(learning_rate=5, beta1=0.99, epsilon=1e-1) iter_count = 1 best_loss, best_img = float('inf'), None loss_weights = (style_weight, content_weight) cfg = { 'model': model, 'loss_weights': loss_weights, 'init_image': init_image, 'gram_style_features': gram_style_features, 'content_features': content_features } num_rows = 2 num_cols = 5 display_interval = num_iterations / (num_rows * num_cols) start_time = time.time() global_start = time.time() norm_means = np.array([103.939, 116.779, 123.68]) min_vals = -norm_means max_vals = 255 - norm_means for i in range(num_iterations): grads, all_loss = compute_grads(cfg) loss, style_score, content_score = all_loss opt.apply_gradients([(grads, init_image)]) clipped = tf.clip_by_value(init_image, min_vals, max_vals) init_image.assign(clipped) end_time = time.time() if loss < best_loss: best_loss = loss best_img = deprocess_img(init_image.numpy()) if i % display_interval == 0: start_time = time.time() print('Iteration: {}'.format(i)) print('Total loss: {:.4e}, ' 'style loss: {:.4e}, ' 'content loss: {:.4e}, ' 'time: {:.4f}s'.format(loss, style_score, content_score, time.time() - start_time)) print('Total time: {:.4f}s'.format(time.time() - global_start)) return best_img, best_loss
parser.add_argument('--state-type', choices=['learned-distribution', 'zero'], required=True) parser.add_argument('--seed', default=42, type=int) args = parser.parse_args() tf.enable_eager_execution() tf.set_random_seed(args.seed) np.random.seed(args.seed) random.seed(args.seed) job_dir = os.path.join(args.job_dir, 'lcd', args.state_type, 'seed_{}'.format(args.seed)) print(job_dir) global_step = tfe.Variable(0, dtype=tf.int64) k = 25 num_epochs = 65 num_train_samples = 1000 num_test_samples = 500 num_generation_samples = 10 train_fn_periods = [.5, 1.5, 2.5, 3.5, 4.5] test_fn_periods = [1., 2., 3., 4.] batch_size = 128 train_dataset, test_dataset, min_target, max_target = create_datasets( [(create_dynamics_fn(p), p) for p in train_fn_periods], [(create_dynamics_fn(p), p) for p in test_fn_periods], num_train_samples, num_test_samples, k, [2.], [4.]) train_dataset = train_dataset.batch(batch_size) train_dataset = train_dataset.shuffle(num_train_samples // batch_size) test_states, test_periods, test_targets = test_dataset
## Gradients - Automatic differentiation is built into eager execution # Under the hood ... # - Operations are recorded on a tap # - The tape is Played back to compute gradients # - This is reverse-mode differentiation (backpropagation) # Eg: 01 def square(x): return x**2 grad = tfe.gradients_function(square) # differentiation w.r.t input of square print(square(3.)) print(grad(3.)) # Eg: 02 x = tfe.Variable(2.0) # use when eager execution is enabled def loss(y): return (y - x**2)**2 grad = tfe.implicit_gradients( loss) # Differentiation w.r.t variables used to compute loss print(loss(7.)) print(grad(7.)) # [gradient w.r.t x, x]
num = tf.constant(num) if int(num % 3) == 0 and int(num % 5) == 0: print('FizzBuzz') elif int(num % 3) == 0: print('Fizz') elif int(num % 5) == 0: print('Buzz') else: print(num) counter += 1 return counter print(fizzbuzz(15)) w = tfe.Variable([[1.0]]) with tf.GradientTape() as tape: loss = w * w grads = tape.gradient(loss, w) print(grads) # Training model process as follows: # first, create dataset # second, create keras model # third, foreach single data # with tf.gradientType() as tape: # logits = model(input), # loss = cal(logits, labels) # grads = tape.gradient(loss, variables) # optimizer.apply_gradient(zip(grads, variables), global_step=...)
train_X = [ 3.3, 4.4, 5.5, 6.71, 6.93, 4.168, 9.779, 6.182, 7.59, 2.167, 7.042, 10.791, 5.313, 7.997, 5.654, 9.27, 3.1 ] train_Y = [ 1.7, 2.76, 2.09, 3.19, 1.694, 1.573, 3.366, 2.596, 2.53, 1.221, 2.827, 3.465, 1.65, 2.904, 2.42, 2.94, 1.3 ] n_samples = len(train_X) # Parameters learning_rate = 0.01 display_step = 100 num_steps = 1000 W = tfe.Variable(np.random.randn()) b = tfe.Variable(np.random.randn()) def linear_regression(inputs): return inputs * W + b def mean_square_fn(model_fn, inputs, labels): return tf.reduce_sum(tf.pow(model_fn(inputs) - labels, 2)) optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
def __init__(self): super(ODEModel_pre, self).__init__() self.Weights = tfe.Variable( tf.random_normal([num_param // 2, 2], dtype=tf.float32) * 0.01, dtype=tf.float32)
def __init__(self, loc, scale): super(Normalizer, self).__init__() self.loc = tfe.Variable(loc, trainable=False) self.scale = tfe.Variable(scale, trainable=False)
def __init__( self, ds, num_units=64, layers=None, dropout_prob=None, apply_batchnorm=True, activation_fn="relu", apply_dropout=True, l2_regularizer=0.0, ): super(NeuralFM, self).__init__() self._num_weights = ds.num_features_one_hot self._num_units = num_units self._num_features = ds.num_features if layers and dropout_prob and apply_dropout: assert len(layers) + 1 == len(dropout_prob) if layers is None: layers = [64] if dropout_prob is None: dropout_prob = [0.8, 0.5] self.dropout_prob = dropout_prob self.apply_batchnorm = apply_batchnorm self.apply_dropout = apply_dropout self.activation = activation_fn self.dense_layers = [ tf.keras.layers.Dense(units, activation=self.activation) for units in layers ] self.final_dense_layer = tf.keras.layers.Dense(1) if self.apply_batchnorm: self.batch_norm_layer = tf.keras.layers.BatchNormalization() self.dense_batch_norm = [ tf.keras.layers.BatchNormalization() for _ in layers ] if self.apply_dropout: self.fm_dropout = tf.keras.layers.Dropout(self.dropout_prob[-1]) self.dense_dropout = [ tf.keras.layers.Dropout(self.dropout_prob[i]) for i in range(len(dropout_prob) - 1) ] self.w = tf.keras.layers.Embedding( self._num_weights, num_units, input_length=self._num_features, embeddings_initializer=tf.keras.initializers.RandomNormal( mean=0.0, stddev=0.01), embeddings_regularizer=tf.keras.regularizers.l2(l2_regularizer), ) self.w0 = tf.keras.layers.Embedding( self._num_weights, 1, input_length=self._num_features, embeddings_initializer="zeros", ) self.bias = tfe.Variable(tf.constant(0.0))
# In order to use eager execution, `tfe.enable_eager_execution()` must be # called at the very beginning of a TensorFlow program. tf.enable_eager_execution() ############################# ########## TO DO ############ ############################# # Read the data into a dataset. data, n_samples = utils.read_birth_life_data(DATA_FILE) dataset = tf.data.Dataset.from_tensor_slices((data[:, 0], data[:, 1])) # Create weight and bias variables, initialized to 0.0. ############################# ########## TO DO ############ ############################# w = tfe.Variable(0.0) b = tfe.Variable(0.0) # Define the linear predictor. def prediction(x): return x * w + b ############################# ########## TO DO ############ ############################# # Define loss functions of the form: L(y, y_predicted) def squared_loss(y, y_predicted): return (y - y_predicted)**2 #############################
def transfer_style(content_image, style_image, learning_rate=5, content_weight=1e3, style_weight=1e-2, num_iterations=100): model = get_intermediate_layers_model({ "style_layers": STYLE_LAYERS, "content_layers": CONTENT_LAYERS }) # Get intermediate representations for the content image and the style image content_image = utils.preprocess_image(content_image) style_image = utils.preprocess_image(style_image) content_representations = model(content_image) style_representations = model(style_image) content_representations = [ layer[0] for layer in content_representations[len(STYLE_LAYERS):] ] style_representations_gram_matrix = [ utils.compute_gram_matrix(layer[0]) for layer in style_representations[:len(STYLE_LAYERS)] ] # Create the generated image, as a trick use as base the content image generated_image = np.copy(content_image) generated_image = tfe.Variable(generated_image, dtype=tf.float32) optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=0.99, epsilon=1e-1) loss_weights = (style_weight, content_weight) best_loss, best_img = float('inf'), None # For displaying grid num_rows = 2 num_cols = 5 display_interval = num_iterations / (num_rows * num_cols) start_time = time.time() global_start = time.time() norm_means = np.array([103.939, 116.779, 123.68]) min_vals = -norm_means max_vals = 255 - norm_means imgs = [] for i in range(num_iterations): grads, all_loss = compute_grads(model, style_representations_gram_matrix, content_representations, generated_image, loss_weights) loss, style_score, content_score = all_loss optimizer.apply_gradients([(grads, generated_image)]) clipped = tf.clip_by_value(generated_image, min_vals, max_vals) generated_image.assign(clipped) # Save the image with the best score(smallest total loss) if loss < best_loss: best_loss = loss best_img = utils.prepare_image_visualization( generated_image.numpy()) if i % display_interval == 0: start_time = time.time() plot_img = generated_image.numpy() plot_img = utils.prepare_image_visualization(plot_img) imgs.append(plot_img) IPython.display.clear_output(wait=True) IPython.display.display_png(Image.fromarray(plot_img)) print(f'Iteration: {i}') print( f'Total loss: {loss}, style loss: {style_score}, content loss: {content_score}, time: {time.time() - start_time}s' ) print(f'Total time: {time.time() - global_start}s') IPython.display.clear_output(wait=True) plt.figure(figsize=(14, 4)) for i, img in enumerate(imgs): plt.subplot(num_rows, num_cols, i + 1) plt.imshow(img) plt.xticks([]) plt.yticks([]) return best_img, best_loss
import tensorflow as tf import tensorflow.contrib.eager as tfe import matplotlib.pyplot as plt import utils DATA_FILE = 'data/birth_life_2010.txt' # In order to use eager execution, `tfe.enable_eager_execution()` must be # called at the very beginning of a TensorFlow program. tfe.enable_eager_execution() data, n_samples = utils.read_birth_life_data(DATA_FILE) dataset = tf.data.Dataset.from_tensor_slices((data[:, 0], data[:, 1])) w = tfe.Variable(0.0, name="weight", dtype=tf.float32) b = tfe.Variable(0.0, name="bias", dtype=tf.float32) def prediction(x): Y_predicted = x * w + b return Y_predicted def squared_loss(y, Y_predicted): loss = tf.square(y - Y_predicted, name="loss") return loss def hubber_loss(labels, predictions, delta=1.0): # residual = tf.abs(predictions-labels)