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 load_and_process_img(path_to_img): img = load_img(path_to_img) #print("img shape",img.shape,"path ",path_to_img) img = tf.keras.applications.vgg19.preprocess_input(img) return img
def load_and_process_img(path_to_img): img = load_img(path_to_img,max_size) print(img.shape) img = tf.keras.applications.vgg19.preprocess_input(img) return img