epochs = val_data.__len__() / hps['batch_size'] # epochs = 1 for i in range(epochs): print("========== Running Epoch {} / {} =========".format(i, epochs)) test_data = val_data[i * hps['batch_size']:(i + 1) * hps['batch_size']] image_ids = [sample['image_id'] for sample in test_data] images_data = load_images(image_ids) print("\t {} Images loaded".format(images_data.__len__())) images_modified = [preprocess_image(im) for im in images_data] features = [ CNN.extract_features( input=tf.reshape(im, [1, 266, 400, 3]))[hps['feature_layer']] for im in images_modified ] features = [ tf.nn.max_pool(feat, [1, 4, 4, 1], [1, 4, 4, 1], 'SAME') for feat in features ] features = tf.stack(features, axis=0) print("\t cnn features shape :::{}".format(features.shape)) features = tf.reshape(features, (features.shape[0], -1)) questions_vec = [ np.array( question_to_vector(test_data[j]['question'], words_to_idx, hps['enc_timesteps'])) for j in range(hps['batch_size'])
def build_graph(squeezenet_path, cond='train', param='initialize', batch_size=1): with tf.device('/cpu:0'): with tf.Graph().as_default(): sess = get_session() sess1 = tf.Session() if cond == 'train': SAVE_PATH = squeezenet_path #'C:/Users/Junaid/Desktop/style proj/datasets/squeezenet.ckpt' model = SqueezeNet(save_path=SAVE_PATH, sess=sess1) # print("kndflkn") x = tf.placeholder(tf.float32, [batch_size, 256, 256, 3], name="x") phase = tf.placeholder(tf.bool, name='phase') style_image = tf.placeholder(tf.float32, shape=[256, 256, 3], name='style_image') keep_prob = tf.placeholder(tf.float32, name="keep_prob") padding = [[0, 0], [40, 40], [40, 40], [0, 0]] x_new = tf.pad(x, padding, 'REFLECT') ########## MODEL ############### content_image = model_arch( x_new, phase, param, keep_prob, batch_size ) # put 1 for intilization and 2 for restoring saved weights in 3rd parametr if cond == 'train': # parameter initialzation content_layer = 3 content_weight = tf.constant([5e-2]) style_layers = [1, 4, 6, 7] style_weights = [2000.0, 500.0, 12.0, 1.0] tv_weight = tf.constant([5e-2]) # CONTENT_WEIGHT = 7.5e0 # STYLE_WEIGHT = 1e2 # TV_WEIGHT = 2e2 content_original = x # calculating_features # fake_extract = sess.run(fake_feat[content_layer]) original_feat = model.extract_features(content_original) original_extract = original_feat[content_layer] fake_feat = model.extract_features(content_image) fake_extract = fake_feat[content_layer] style_target = [ gram_matrix(original_feat[idx]) for idx in style_layers ] # fake_style=[fake_feat[idx] for idx in style_layers] s_loss = style_loss(fake_feat, style_layers, style_target, style_weights) # con_loss==tf.py_func(content_loss,[fake_extract, original_extract],tf.float32) con_loss = content_weight * tf.reduce_sum( (fake_extract - original_extract)**2) # tvariation_loss=tv_loss(content_image, tv_weight)#tf.py_func(tv_loss, [content_image, tv_weight], tf.float32) w_variance = tf.reduce_sum( tf.pow( content_image[:, :, :-1, :] - content_image[:, :, 1:, :], 2)) h_variance = tf.reduce_sum( tf.pow( content_image[:, :-1, :, :] - content_image[:, 1:, :, :], 2)) tvariation_loss = tv_weight * (h_variance + w_variance) total_loss = tf.add(s_loss, con_loss, name="total_loss") final_loss = tf.add(total_loss, tvariation_loss, name="final_loss") ###trainig_initialization update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): train = tf.train.AdamOptimizer(1e-2, name="adam1").minimize( total_loss, name="training") if cond == 'test': return dict(x=x, style_image=style_image, keep_prob=keep_prob, phase=phase, content_image=content_image, saver=tf.train.Saver()) else: return dict(x=x, style_image=style_image, keep_prob=keep_prob, phase=phase, content_image=content_image, final_loss=final_loss, train=train, saver=tf.train.Saver())