Beispiel #1
0
def main():
    """
    main func
    """

    text, x, y, char2idx, idx2char = getty()
    T = 100

    config = {
        'dim_hidden': 300,
        'l': T,
        'clip': 5,
        'mu': 0.9,
        'step_size': 0.001
    }

    #np.random.seed(42)
    r = RNN(config)
    r.accept([27])

    ttb = r.sample('f', char2idx, idx2char)
    r.fit(x[:T], y[:T], 100, char2idx, idx2char)
    tta = r.sample('f', char2idx, idx2char)
    print(ttb)
    print(tta)
    print(text[:T])
    return

    (data, label) = cifar()
    N = 10000
    data = np.array(data, dtype=float)[:N, ]
    label = np.array(label)[:N, ]

    data = normalize(data)

    config = {
        'input_shape': [3, 32, 32],
        'mu': 0.9,
        'step_size': 0.000001,
        'step_decay': 0.95
    }

    nn = Net(config)
    conv1 = Conv([3, 3], 6)
    relu1 = Relu()
    conv2 = Conv([3, 3], 32)
    relu2 = Relu()
    pool = MaxPool()
    fc = FC([10])

    nn.add(conv1)
    nn.add(relu1)
    nn.add(pool)
    nn.add(fc)

    print(nn)
    nn.fit(data, label, 200)
Beispiel #2
0
 def test_add_layer(self):
     config = {
         'input_shape': [3, 5, 5],
         'step_size': 1,
         'mu': 0.9,
         'step_decay': 0.9
     }
     n = Net(config)
     l = FC(10)
     n.add(l)
def main():

    parser = ArgumentParser()
    parser.add_argument("--numTrain", dest="num_images_train", type=int)
    parser.add_argument("--numValid", dest="num_images_validation", type=int)
    parser.add_argument("--numTest", dest="num_images_test", type=int)
    parser.add_argument("--numEpochs", dest="num_epochs", type=int)
    parser.add_argument("--batchSize", dest="batch_size", type=int)
    parser.add_argument("--model", dest="model", default="", type=str)
    parser.add_argument("--lr", dest="learning_rate", type=float)
    parser.add_argument("--name", dest="name", type=str)

    args = parser.parse_args()

    dataset = ScatterImageSet(
        x_path='/bigdata/hplsim/production/aipr/fsize_sigma_set/images/',
        y_path='/bigdata/hplsim/production/aipr/fsize_sigma_set/labels.npy',
        batch_size=args.batch_size,
        training_size=args.num_images_train,
        test_size=args.num_images_test,
        validation_size=args.num_images_validation,
        normalize=False,
        shift=False)

    # 5. Create Hyperparamter String
    # 6. Create CNN

    tensorboard = "training_sessions/" + args.name
    savedir = './models/' + args.name + '/model.ckpt'

    fc_model = FC(savedir, tensorboard, args.learning_rate)

    # 7. Fit Neuronal Network
    start_time = time.time()
    best_val, best_training = fc_model.fit(args.num_epochs, args.batch_size,
                                           dataset, args.model)
    end_time = time.time()
    time_elapsed = (end_time - start_time) / 60.
    print("Best Validation", best_val)
    print("Best Training", best_training)
    print("In time:", time_elapsed)
Beispiel #4
0
 def test_fit(self):
     config = {
         'input_shape': [3, 5, 5],
         'step_size': 1,
         'mu': 0.9,
         'step_decay': 0.9
     }
     n = Net(config)
     l = FC(10)
     n.add(l)
     x = np.random.random([1, 3, 5, 5]).reshape(1, -1)
     y = np.array([0])
     n.fit(x, y, 10)
Beispiel #5
0
 def test_train_iteration(self):
     config = {
         'input_shape': [3, 5, 5],
         'step_size': 1,
         'mu': 0.9,
         'step_decay': 0.9
     }
     n = Net(config)
     l = FC(10)
     n.add(l)
     x = np.random.random([1, 3, 5, 5]).reshape(1, -1)
     y = np.array([0])
     loss = n.train_one_iteration(x, y)
Beispiel #6
0
def main():

    sal_map_type = "GuidedBackprop_maxlogit"
    # sal_map_type = "PlainSaliency_maxlogit"
    data_dir = "../VGGImagenet/data_imagenet"
    save_dir = "results/10222017/fc"

    # TODO: extend this part to a list

    image_name = 'tabby'

    n_labels = 5

    n_input = 64

    layers = [
        'conv1_1', 'conv1_2', 'pool1', 'conv2_1', 'conv2_2', 'pool2',
        'conv3_1', 'conv3_2', 'conv3_3', 'pool3', 'conv4_1', 'conv4_2',
        'conv4_3', 'pool4', 'conv5_1', 'conv5_2', 'conv5_3', 'pool5', 'fc1',
        'fc2', 'fc3'
    ]

    fns = []
    image_list = []
    label_list = []

    # load in the original image and its adversarial examples
    for image_path in glob.glob(
            os.path.join(data_dir, '{}.png'.format(image_name))):
        fns.append(os.path.basename(image_path).split('.')[0])
        image = imread(image_path, mode='RGB')
        image = imresize(image, (n_input, n_input)).astype(np.float32)
        image_list.append(image)
        onehot_label = np.array(
            [1 if i == image_dict[image_name] else 0 for i in range(n_labels)])
        label_list.append(onehot_label)

    batch_img = np.array(image_list)
    batch_label = np.array(label_list)

    batch_size = batch_img.shape[0]

    # tf session
    sess = tf.Session()

    # construct the graph based on the gradient type we want
    # plain relu vs guidedrelu
    if sal_map_type.split('_')[0] == 'GuidedBackprop':
        eval_graph = tf.get_default_graph()
        with eval_graph.gradient_override_map({'Relu': 'GuidedRelu'}):
            conv_model = FC(sess)

    elif sal_map_type.split('_')[0] == 'NGuidedBackprop':
        eval_graph = tf.get_default_graph()
        with eval_graph.gradient_override_map({'Relu': 'NGuidedRelu'}):
            # load the vgg graph
            # plain_init = true -> load the graph with random weights
            # plain_init = false -> load the graph with pre-trained weights
            conv_model = FC(sess)

    elif sal_map_type.split('_')[0] == 'PlainSaliency':
        # load the vgg graph
        # plain_init = true -> load the graph with random weights
        # plain_init = false -> load the graph with pre-trained weights
        conv_model = FC(sess)

    else:
        raise Exception("Unknown saliency_map type - 1")

    # --------------------------------------------------------------------------
    # Visualize grad-camp and its adversarial examples
    # --------------------------------------------------------------------------
    # Get last convolutional layer gradient for generating gradCAM visualization
    target_conv_layer = conv_model.convnet_out
    if sal_map_type.split('_')[1] == "cost":
        conv_grad = tf.gradients(conv_model.cost, target_conv_layer)[0]
    elif sal_map_type.split('_')[1] == 'maxlogit':
        conv_grad = tf.gradients(conv_model.maxlogit, target_conv_layer)[0]
    elif sal_map_type.split('_')[1] == 'randlogit':
        conv_grad = tf.gradients(conv_model.logits[0], target_conv_layer)[0]
        # conv_grad = tf.gradients(conv_model.logits[random.randint(0, 999)], target_conv_layer)[0]
    else:
        raise Exception("Unknown saliency_map type - 2")

    # normalization
    conv_grad_norm = tf.div(conv_grad, tf.norm(conv_grad) + tf.constant(1e-5))

    # saliency gradient to input layer
    if sal_map_type.split('_')[1] == "cost":
        sal_map = tf.gradients(conv_model.cost, conv_model.imgs)[0]
    elif sal_map_type.split('_')[1] == 'maxlogit':
        sal_map = tf.gradients(conv_model.maxlogit, conv_model.imgs)[0]
    elif sal_map_type.split('_')[1] == 'randlogit':
        sal_map = tf.gradients(conv_model.logits[0], conv_model.imgs)[0]
        # sal_map = tf.gradients(conv_model.logits[random.randint(0, 999)], conv_model.imgs)[0]
    else:
        raise Exception("Unknown saliency_map type - 2")

    # predict
    probs = sess.run(conv_model.probs,
                     feed_dict={conv_model.images: batch_img})

    # sal_map and conv_grad
    sal_map_val, target_conv_layer_val, conv_grad_norm_val =\
        sess.run([sal_map, target_conv_layer, conv_grad_norm],
                 feed_dict={conv_model.images: batch_img, conv_model.labels: batch_label})

    for idx in range(batch_size):
        print_prob(probs[idx])
        visualize(batch_img[idx], target_conv_layer_val[idx],
                  conv_grad_norm_val[idx], sal_map_val[idx], sal_map_type,
                  save_dir, fns[idx], probs[idx])
Beispiel #7
0
def train(V_train,
          Y_train,
          T_train,
          S_train,
          V_seen,
          Y_seen,
          T_seen,
          S_seen,
          V_unseen,
          Y_unseen,
          T_unseen,
          S_unseen,
          dataset,
          resource,
          obj='BCE',
          hid_dim=0,
          batch_size=200,
          max_epoch=100,
          store=False):

    if not obj == 'BCE':  # Change to {-1, 1} coding
        Y_train = 2. * Y_train - 1.
        Y_seen = 2. * Y_seen - 1.
        Y_unseen = 2. * Y_unseen - 1.

    # mlp_t_layers, mlp_v_layers = [T_train.shape[1], 300, 50], [V_train.shape[1], 200, 50]
    if 'w' in resource:
        mlp_t_layers, mlp_v_layers = [T_train.shape[1] + hid_dim, 300,
                                      50], [V_train.shape[1], 200, 50]
    else:
        mlp_t_layers, mlp_v_layers = [T_train.shape[1] + hid_dim,
                                      50], [V_train.shape[1], 200, 50]
    model = FC(mlp_t_layers,
               mlp_v_layers,
               word_dim=S_train.shape[2],
               hid_dim=hid_dim)
    symbols = model.define_functions(obj=obj)

    model_fn = model.name + '_' + obj\
               + '_tmlp_' + '-'.join([str(x) for x in mlp_t_layers])\
               + '_vmlp_' + '-'.join([str(x) for x in mlp_v_layers])\
               + '_' + dataset + '_' + resource  # + time.strftime("%m%d-%H-%M-%S", time.localtime())
    log_file = os.path.join(log_root, model_fn + '.log')
    writer = open(log_file, 'w')

    V_batch, T_batch, Y_batch, updates = symbols['V_batch'], symbols[
        'T_batch'], symbols['Y_batch'], symbols['updates']
    is_train, cost, loss, acc, pred, sim = symbols['is_train'], symbols['cost'], symbols['loss'], symbols['acc'],\
                                           symbols['pred'], symbols['sim']
    S_batch = symbols['S_batch']

    start_symbol, end_symbol = T.lscalar(), T.lscalar()

    V_train_shared = theano.shared(V_train, borrow=True)
    Y_train_shared = theano.shared(Y_train, borrow=True)
    T_train_shared = theano.shared(T_train, borrow=True)
    S_train_shared = theano.shared(S_train, borrow=True)

    V_seen_shared = theano.shared(V_seen, borrow=True)
    Y_seen_shared = theano.shared(Y_seen, borrow=True)
    T_seen_shared = theano.shared(T_seen, borrow=True)
    S_seen_shared = theano.shared(S_seen, borrow=True)

    V_unseen_shared = theano.shared(V_unseen, borrow=True)
    Y_unseen_shared = theano.shared(Y_unseen, borrow=True)
    T_unseen_shared = theano.shared(T_unseen, borrow=True)
    S_unseen_shared = theano.shared(S_unseen, borrow=True)

    print 'Compiling functions ... '
    train_model = theano.function(inputs=[start_symbol, end_symbol, is_train],
                                  outputs=[pred, cost, loss, acc],
                                  updates=updates,
                                  givens={
                                      V_batch:
                                      V_train_shared[start_symbol:end_symbol],
                                      Y_batch:
                                      Y_train_shared[start_symbol:end_symbol],
                                      T_batch:
                                      T_train_shared,
                                      S_batch:
                                      S_train_shared
                                  },
                                  on_unused_input='ignore')

    test_seen = theano.function(inputs=[start_symbol, end_symbol, is_train],
                                outputs=[pred, cost, loss, acc, sim],
                                givens={
                                    V_batch:
                                    V_seen_shared[start_symbol:end_symbol],
                                    Y_batch:
                                    Y_seen_shared[start_symbol:end_symbol],
                                    T_batch: T_seen_shared,
                                    S_batch: S_seen_shared,
                                },
                                on_unused_input='ignore')

    test_unseen = theano.function(inputs=[start_symbol, end_symbol, is_train],
                                  outputs=[pred, cost, loss, acc, sim],
                                  givens={
                                      V_batch:
                                      V_unseen_shared[start_symbol:end_symbol],
                                      Y_batch:
                                      Y_unseen_shared[start_symbol:end_symbol],
                                      T_batch:
                                      T_unseen_shared,
                                      S_batch:
                                      S_unseen_shared
                                  },
                                  on_unused_input='ignore')

    print 'Compilation done'

    num_samples = V_train.shape[0]

    best_acc = 0.25

    for epoch_index in xrange(max_epoch):
        print 'Epoch = %d' % (epoch_index + 1)
        writer.write('Epoch = %d\n' % (epoch_index + 1))

        start_time = timeit.default_timer()

        batch_index = 0
        acc_epoch, cost_epoch, loss_epoch = 0., 0., 0.
        while True:
            start, end = batch_index * batch_size, min(
                (batch_index + 1) * batch_size, num_samples)
            batch_index += 1

            pred, cost, loss, acc = train_model(start, end, 1)
            cost_epoch += cost * (end - start)
            acc_epoch += acc * (end - start)
            loss_epoch += loss * (end - start)

            if end >= num_samples - 1:
                break

        cost_epoch /= num_samples
        acc_epoch /= num_samples
        loss_epoch /= num_samples

        writer.write('\tTraining\tAccuracy = %.4f\tCost = %f\tLoss = %f\n' %
                     (acc_epoch, cost_epoch, loss_epoch))

        print '\tTraining\tAccuracy = %.4f\tCost = %f\tLoss = %f'\
              % (acc_epoch, cost_epoch, loss_epoch)

        end_time = timeit.default_timer()
        print 'Train %.3f seconds for this epoch' % (end_time - start_time)
        roc_auc_seen, ap_seen, top1_accu_seen, top5_accu_seen\
            = validate(test_seen, writer, Y_seen)
        roc_auc_unseen, ap_unseen, top1_accu_unseen, top5_accu_unseen\
            = validate(test_unseen, writer, Y_unseen, seen='unseen')

        n_seen = Y_seen.shape[0]
        n_unseen = Y_unseen.shape[0]

        roc_auc = (n_seen * roc_auc_seen +
                   n_unseen * roc_auc_unseen) / (n_seen + n_unseen)
        pr_auc = (n_seen * ap_seen + n_unseen * ap_unseen) / (n_seen +
                                                              n_unseen)

        top1_accu = (n_seen * top1_accu_seen +
                     n_unseen * top1_accu_unseen) / (n_seen + n_unseen)
        top5_accu = (n_seen * top5_accu_seen +
                     n_unseen * top5_accu_unseen) / (n_seen + n_unseen)

        writer.write(
            '\tMean\tROC-AUC = %.4f\tPR-AUC = %.4f\tTop-1 Acc = %.4f\tTop-5 Acc = %.4f\n'
            % (roc_auc, pr_auc, top1_accu, top5_accu))
        print '\tMean\tROC-AUC = %.4f\tPR-AUC = %.4f\tTop-1 Acc = %.4f\tTop-5 Acc = %.4f'\
              % (roc_auc, pr_auc, top1_accu, top5_accu)

        if store and top1_accu > best_acc:
            best_acc = top1_accu
            with open(
                    os.path.join(
                        model_root,
                        model_fn + '_epoch_' + str(epoch_index + 1) + '_acc_' +
                        str(top1_accu) + '.pkl'), 'wb') as pickle_file:
                pickle.dump(model, pickle_file)
        print

    writer.close()
Beispiel #8
0
def train(V_train,
          T_matrix,
          Y_train,
          V_test,
          Y_test,
          obj='BCE',
          batch_size=200,
          max_epoch=100,
          unseen_file=None,
          store=False):

    if not obj == 'BCE':  # 0-1 coding
        Y_train = 2. * Y_train - 1.
        Y_test = 2. * Y_test - 1.

    if unseen_file:
        Y_train, T_train = remove_unseen_in_train(Y_train, T_matrix,
                                                  unseen_file)
    else:
        T_train = T_matrix
    mlp_t_layers, mlp_v_layers = [T_matrix.shape[1], 300,
                                  50], [V_train.shape[1], 200, 50]
    # mlp_t_layers, mlp_v_layers = [T_matrix.shape[1], 50], [V_train.shape[1], 200, 50]
    model = FC(mlp_t_layers, mlp_v_layers)
    symbols = model.define_functions(obj=obj)

    model_fn = model.name + '_' + obj\
               + '_tmlp_' + '-'.join([str(x) for x in mlp_t_layers])\
               + '_vmlp_' + '-'.join([str(x) for x in mlp_v_layers])\
               + '_bs_' + str(batch_size) + '_' + time.strftime("%m%d-%H-%M-%S", time.localtime())
    log_file = os.path.join(log_root, model_fn + '.log')
    writer = open(log_file, 'w')

    V_batch, T_batch, Y_batch, updates = symbols['V_batch'], symbols[
        'T_batch'], symbols['Y_batch'], symbols['updates']
    is_train, cost, loss, acc, pred, sim = symbols['is_train'], symbols['cost'], symbols['loss'], symbols['acc'],\
                                           symbols['pred'], symbols['sim']

    start_symbol, end_symbol = T.lscalar(), T.lscalar()

    V_train_shared = theano.shared(V_train, borrow=True)
    Y_train_shared = theano.shared(Y_train, borrow=True)
    T_train_shared = theano.shared(T_train, borrow=True)

    V_test_shared = theano.shared(V_test, borrow=True)
    Y_test_shared = theano.shared(Y_test, borrow=True)
    T_test_shared = theano.shared(T_matrix, borrow=True)

    print 'Compiling functions ... '
    train_model = theano.function(inputs=[start_symbol, end_symbol, is_train],
                                  outputs=[pred, cost, loss, acc],
                                  updates=updates,
                                  givens={
                                      V_batch:
                                      V_train_shared[start_symbol:end_symbol],
                                      Y_batch:
                                      Y_train_shared[start_symbol:end_symbol],
                                      T_batch:
                                      T_train_shared
                                  },
                                  on_unused_input='ignore')
    test_model = theano.function(inputs=[start_symbol, end_symbol, is_train],
                                 outputs=[pred, cost, loss, acc, sim],
                                 givens={
                                     V_batch:
                                     V_test_shared[start_symbol:end_symbol],
                                     Y_batch:
                                     Y_test_shared[start_symbol:end_symbol],
                                     T_batch:
                                     T_test_shared
                                 },
                                 on_unused_input='ignore')
    print 'Compilation done'

    num_samples = V_train.shape[0]

    best_acc = 0.25

    for epoch_index in xrange(max_epoch):
        print 'Epoch = %d' % (epoch_index + 1)
        writer.write('Epoch = %d\n' % (epoch_index + 1))

        start_time = timeit.default_timer()

        batch_index = 0
        acc_epoch, cost_epoch, loss_epoch = 0., 0., 0.
        while True:
            start, end = batch_index * batch_size, min(
                (batch_index + 1) * batch_size, num_samples)
            batch_index += 1

            pred, cost, loss, acc = train_model(start, end, 1)
            cost_epoch += cost * (end - start)
            acc_epoch += acc * (end - start)
            loss_epoch += loss * (end - start)

            if end >= num_samples - 1:
                break

        cost_epoch /= num_samples
        acc_epoch /= num_samples
        loss_epoch /= num_samples

        writer.write('\tTraining\tAccuracy = %.4f\tCost = %f\tLoss = %f\n' %
                     (acc_epoch, cost_epoch, loss_epoch))

        print '\tTraining\tAccuracy = %.4f\tCost = %f\tLoss = %f'\
              % (acc_epoch, cost_epoch, loss_epoch)

        end_time = timeit.default_timer()
        print 'Train %.3f seconds for this epoch' % (end_time - start_time)

        acc_val = validate(test_model, V_test.shape[0], writer, Y_test)
        if store and acc_val > best_acc:
            best_acc = acc_val
            with open(
                    os.path.join(
                        model_root,
                        model_fn + '_epoch_' + str(epoch_index + 1) + '_acc_' +
                        str(acc_val) + '.pkl'), 'wb') as pickle_file:
                pickle.dump(model, pickle_file)
        print

    writer.close()
def build_PE(in_dim,
             out_dim,
             name='BNN',
             hidden_dims=(200, 200, 200),
             num_networks=7,
             num_elites=5,
             loss='MSPE',
             activation='swish',
             output_activation=None,
             decay=1e-4,
             lr=1e-3,
             lr_decay=None,
             decay_steps=None,
             use_scaler_in=False,
             use_scaler_out=False,
             clip_loss=False,
             kl_cliprange=0.1,
             max_logvar=.5,
             min_logvar=-6,
             session=None):
    """
	Constructs a tf probabilistic ensemble model.
	Args:
		loss: Choose from 'MSPE', 'NLL', 'MSE', 'Huber', or 'CE'. 
				choosing MSPE or NLL will construct a model with variance output
	"""
    print('[PE] dim in / out: {} / {} | Hidden dim: {}'.format(
        in_dim, out_dim, hidden_dims))
    #print('[ BNN ] Input Layer dim: {} | Output Layer dim: {} '.format(obs_dim_in+act_dim+prior_dim, obs_dim_out+rew_dim))
    params = {
        'name': name,
        'loss': loss,
        'num_networks': num_networks,
        'num_elites': num_elites,
        'sess': session,
        'use_scaler_in': use_scaler_in,
        'use_scaler_out': use_scaler_out,
        'clip_loss': clip_loss,
        'kl_cliprange': kl_cliprange,
        'max_logvar': max_logvar,
        'min_logvar': min_logvar,
    }
    model = PE(params)
    model.add(
        FC(hidden_dims[0],
           input_dim=in_dim,
           activation=activation,
           weight_decay=decay / 4))  # def dec: 0.000025))

    for hidden_dim in hidden_dims[1:]:
        model.add(FC(hidden_dim, activation=activation,
                     weight_decay=decay / 2))  # def dec: 0.00005))

    model.add(FC(out_dim, activation=output_activation,
                 weight_decay=decay))  # def dec: 0.0001

    opt_params = {
        "learning_rate": lr
    } if lr_decay is None else {
        "learning_rate": lr,
        "learning_rate_decay": lr_decay,
        "decay_steps": decay_steps
    }
    model.finalize(tf.train.AdamOptimizer, opt_params, lr_decay=lr_decay)

    total_parameters = 0
    for variable in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                                      scope=name):
        # shape is an array of tf.Dimension
        shape = variable.get_shape()
        variable_parameters = 1
        for dim in shape:
            variable_parameters *= dim.value
        total_parameters += variable_parameters
    print('[ Probabilistic Ensemble ] Total trainable Parameteres: {} '.format(
        total_parameters))

    return model
Beispiel #10
0
def visualize_fc():
    sal_map_type = "GuidedBackprop_maxlogit"  # change it to get different visualizations
    image_name = 'tabby'  # or using a list to deal with multiple images

    data_dir = "../data"
    save_dir = "results"
    if not os.path.exists(save_dir):
        os.mkdir(save_dir)

    n_labels = 1000
    dim_input = 64

    fns = []
    image_list = []
    label_list = []

    # load in the original image
    for image_path in glob.glob(
            os.path.join(data_dir, '{}.png'.format(image_name))):
        fns.append(os.path.basename(image_path).split('.')[0])
        image = imread(image_path, mode='RGB')
        image = imresize(image, (dim_input, dim_input)).astype(np.float32)
        image_list.append(image)
        onehot_label = np.array(
            [1 if i == image_dict[image_name] else 0 for i in range(n_labels)])
        label_list.append(onehot_label)

    batch_img = np.array(image_list)
    batch_label = np.array(label_list)

    batch_size = batch_img.shape[0]

    # tf session
    sess = tf.Session()

    # construct the graph based on the gradient type we want
    # plain relu vs guidedrelu
    if sal_map_type.split('_')[0] == 'GuidedBackprop':
        eval_graph = tf.get_default_graph()
        with eval_graph.gradient_override_map({'Relu': 'GuidedRelu'}):
            fc_models = FC(sess)

    elif sal_map_type.split('_')[0] == 'Deconv':
        eval_graph = tf.get_default_graph()
        with eval_graph.gradient_override_map({'Relu': 'DeconvRelu'}):
            fc_models = FC(sess)

    elif sal_map_type.split('_')[0] == 'PlainSaliency':
        fc_models = FC(sess)

    else:
        raise Exception("Unknown saliency type")

    # saliency gradient to input layer
    if sal_map_type.split('_')[1] == "cost":
        sal_map = tf.gradients(fc_models.cost, fc_models.imgs)[0]
    elif sal_map_type.split('_')[1] == 'maxlogit':
        sal_map = tf.gradients(fc_models.maxlogit, fc_models.imgs)[0]
    elif sal_map_type.split('_')[1] == 'randlogit':
        sal_map = tf.gradients(fc_models.logits[:, random.randint(0, 999)],
                               fc_models.imgs)[0]
    else:
        raise Exception("Unknown logit gradient type")

    # predict
    probs = sess.run(fc_models.probs, feed_dict={fc_models.images: batch_img})

    # sal_map and conv_grad
    sal_map_val = sess.run(sal_map,
                           feed_dict={
                               fc_models.images: batch_img,
                               fc_models.labels: batch_label
                           })

    for idx in range(batch_size):
        print_prob(probs[idx])
        visualize(sal_map_val[idx], sal_map_type, save_dir, fns[idx])
Beispiel #11
0
def train():
    """
    Performs training and evaluation of your model.

    First define your graph using vgg.py with your fully connected layer.
    Then define necessary operations such as trainer (train_step in this case),
    savers and summarizers. Finally, initialize your model within a
    tf.Session and do the training.

    ---------------------------------
    How often to evaluate your model:
    ---------------------------------
    - on training set every PRINT_FREQ iterations
    - on test set every EVAL_FREQ iterations

    ---------------------------
    How to evaluate your model:
    ---------------------------
    Evaluation on test set should be conducted over full batch, i.e. 10k images,
    while it is alright to do it over minibatch for train set.
    """
    # Set the random seeds for reproducibility. DO NOT CHANGE.
    tf.set_random_seed(42)
    np.random.seed(42)

    ########################
    # PUT YOUR CODE HERE  #
    ########################
    cifar10 = cifar10_utils.get_cifar10(FLAGS.data_dir)
    x_test, y_test = cifar10.test.images, cifar10.test.labels

    #### PARAMETERS
    classes = [
        'plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship',
        'truck'
    ]
    n_classes = len(classes)
    input_data_dim = cifar10.test.images.shape[1]
    #####

    fc = FC()

    refine_after_k = tf.placeholder(tf.bool)
    x = tf.placeholder(tf.float32, shape=(None, None, None, 3), name="x")
    y = tf.placeholder(tf.float32, shape=(None, n_classes), name="y")

    with tf.name_scope('refine_cnn'):
        pool5, assign_ops = load_pretrained_VGG16_pool5(x)
        pool5 = tf.cond(refine_after_k, lambda: tf.stop_gradient(pool5),
                        lambda: pool5)

        infs = fc.inference(pool5)
        with tf.name_scope('cross-entropy-loss'):
            loss = fc.loss(infs, y)
        with tf.name_scope('accuracy'):
            accuracy = fc.accuracy(infs, y)

        merged = tf.merge_all_summaries()
        opt_operation = train_step(loss)

    with tf.Session() as sess:
        saver = tf.train.Saver()

        sess.run(tf.initialize_all_variables())
        for op in assign_ops:
            sess.run(op)

        test_acc = sess.run(accuracy,
                            feed_dict={
                                x: x_test,
                                y: y_test,
                                refine_after_k: False
                            })
        print("Initial Test Accuracy = {0:.3f}".format(test_acc))

        train_writer = tf.train.SummaryWriter(FLAGS.log_dir + "/train/",
                                              sess.graph)
        test_writer = tf.train.SummaryWriter(FLAGS.log_dir + "/test/",
                                             sess.graph)

        for iteration in range(FLAGS.max_steps + 1):
            x_batch, y_batch = cifar10.train.next_batch(FLAGS.batch_size)
            _ = sess.run(
                [opt_operation],
                feed_dict={
                    x: x_batch,
                    y: y_batch,
                    refine_after_k: FLAGS.refine_after_k > iteration
                })

            if iteration % FLAGS.print_freq == 0:
                [train_acc, train_loss,
                 summary_train] = sess.run([accuracy, loss, merged],
                                           feed_dict={
                                               x: x_batch,
                                               y: y_batch,
                                               refine_after_k: False
                                           })
                train_writer.add_summary(summary_train, iteration)
                print(
                    "Iteration {0:d}/{1:d}. Train Loss = {2:.3f}, Train Accuracy = {3:.3f}"
                    .format(iteration, FLAGS.max_steps, train_loss, train_acc))

            if iteration % FLAGS.eval_freq == 0:
                [test_acc, test_loss,
                 summary_test] = sess.run([accuracy, loss, merged],
                                          feed_dict={
                                              x: x_test,
                                              y: y_test,
                                              refine_after_k: False
                                          })
                test_writer.add_summary(summary_test, iteration)
                print(
                    "Iteration {0:d}/{1:d}. Test Loss = {2:.3f}, Test Accuracy = {3:.3f}"
                    .format(iteration, FLAGS.max_steps, test_loss, test_acc))

            if iteration > 0 and iteration % FLAGS.checkpoint_freq == 0:
                saver.save(sess, FLAGS.checkpoint_dir + '/vgg_model.ckpt')

        train_writer.flush()
        test_writer.flush()
        train_writer.close()
        test_writer.close()

        test_acc = sess.run(accuracy,
                            feed_dict={
                                x: x_test,
                                y: y_test,
                                refine_after_k: False
                            })
        print("Final Test Accuracy = {0:.3f}".format(test_acc))

        sess.close()