Example #1
0
def test_task2(root_path):
    '''
    :param root_path: root path of test data, e.g. ./dataset/task2/test/0/
    :return results: a dict of classification results
    results = {'audio_0000.pkl': 23, ‘audio_0001’: 11, ...}
    This means audio 'audio_0000.pkl' is matched to video 'video_0023' and ‘audio_0001’ is matched to 'video_0011'.
    '''
    results = dict()

    os.chdir(os.path.split(os.path.realpath(__file__))[0])
    audio_transforms = transforms.Compose([transforms.ToTensor()])
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    label_list = [9, 2, 3, 8, 7, 6, 5, 0, 4, 1]
    audio_model = ResNet.resnet18(num_classes=10)
    audio_model = audio_model.to(device)
    audio_model = torch.nn.DataParallel(audio_model)
    audio_model.load_state_dict(torch.load('./model/resnet18.pth'))
    audio_model.eval()

    audio_class_features = []
    video_class_features = []
    audio_motion_features = []
    video_motion_features = []
    Files = list(filter(lambda x: x.endswith(".pkl"), os.listdir(root_path)))
    Files.sort()
    for sample in Files:
        audio_path = root_path + '/' + sample
        data = np.load(audio_path, allow_pickle=True)['audio']
        for i in range(4):
            S = librosa.resample(data[:, i], orig_sr=44100, target_sr=11000)
            S = np.abs(librosa.stft(S[5650:-5650], n_fft=510, hop_length=128))
            S = np.log10(S + 0.0000001)
            S = np.clip(S, -5, 5)
            S -= np.min(S)
            S = 255 * (S / np.max(S))
            if S.shape[-1] < 256:
                S = np.pad(S, ((0, 0), (int(np.ceil((256 - S.shape[-1]) / 2)),
                                        int(np.floor(
                                            (256 - S.shape[-1]) / 2)))))
            if S.shape[-1] > 256:
                S = S[:,
                      int(np.ceil((S.shape[-1] - 256) /
                                  2)):-int(np.floor((S.shape[-1] - 256) / 2))]
            if i == 0:
                feature = np.uint8(S)[:, :, np.newaxis]
            else:
                feature = np.concatenate(
                    (np.uint8(S)[:, :, np.newaxis], feature), axis=-1)
        X = audio_transforms(feature)
        X = X.to(device)
        class_feature_t = torch.softmax(audio_model(X.unsqueeze(0)),
                                        dim=-1).squeeze(0)
        class_feature = np.zeros(10)
        for i in range(10):
            class_feature[label_list[i]] = class_feature_t[i]
        threshold = 0.35
        label_list2 = [1, 2, 0, 3]
        label = [0] * 4
        for i in range(4):
            if np.max(data[:, i]) > threshold:
                label[label_list2[i]] = 1
        audio_class_features.append(class_feature)
        audio_motion_features.append(label)

    net = AlexNet()
    net.load_state_dict(torch.load('./model/alexnet.pt'))

    k = 0
    while os.path.exists(root_path + '/video_' + str('%04d' % k)):
        video_class_feature, video_move_feature = get_video_feature(
            net, root_path + '/video_' + str('%04d' % k))
        video_class_features.append(video_class_feature)
        video_motion_features.append(video_move_feature)
        k = k + 1

    indices = pairing(audio_class_features, audio_motion_features,
                      video_class_features, video_motion_features, -100)
    j = 0
    for sample in Files:
        results[sample] = indices[j][1]
        j = j + 1
    return results
def stupid_characterness_score(image, character):
    # send the image to the alexnet to compute the score for each class.
    # once you have it, use the character to find the score and return
    # use the evaluate alexnet thing here
    """
	Configuration settings
	"""
    # get the _id of the character in the list
    #print character
    try:
        _id = string.ascii_lowercase.index(character)
        #print _id
    except:
        _id = range(10).index(int(character)) + 26
        #print _id

    label = _id

    # evaluating params
    batch_size = 1

    # Network params
    num_classes = 36

    image = np.float32(image) - np.float32(VGG_MEAN)
    image = np.expand_dims(image, axis=0)

    # generate one hot variable
    #one_hot = tf.one_hot(label, num_classes)
    one_hot = np.zeros((num_classes))
    one_hot[label] = 1
    one_hot = np.expand_dims(one_hot, axis=0)
    one_hot = np.float32(one_hot)

    # placeholder for graph inputs
    x = tf.placeholder(tf.float32, [batch_size, 227, 227, 3])
    y = tf.placeholder(tf.float32, [None, num_classes])
    keep_prob = tf.placeholder(tf.float32)

    # Initialize model
    model = AlexNet(x, keep_prob, num_classes, [])

    # Link variable to model output
    score = tf.nn.softmax(model.fc8)

    #initialize store model
    saver = tf.train.Saver

    #'''
    with tf.Session() as sess:
        saver = tf.train.import_meta_graph(
            '/home/ray/finetune_alexnet_with_tensorflow/tmp/finetune_alexnet/chkpt/model_epoch20.ckpt.meta'
        )
        saver.restore(
            sess,
            '/home/ray/finetune_alexnet_with_tensorflow/tmp/finetune_alexnet/chkpt/model_epoch20.ckpt'
        )
        sess.run(tf.global_variables_initializer())
        #graph = tf.get_default_graph()
        print("Model restored.")
        print('Initialized')
        #print image.shape, type(image), image.dtype
        #print one_hot.shape, type(one_hot), one_hot.dtype
        #print one_hot

        score_out = sess.run(score,
                             feed_dict={
                                 x: image,
                                 y: one_hot,
                                 keep_prob: 1.
                             })
    #'''
    tf.reset_default_graph()
    return score_out[0, label]
import time
import tensorflow as tf
import numpy as np
import pandas as pd
from scipy.misc import imread
from alexnet import AlexNet

sign_names = pd.read_csv('signnames.csv')
nb_classes = 43

x = tf.placeholder(tf.float32, (None, 32, 32, 3))
resized = tf.image.resize_images(x, (227, 227))

# NOTE: By setting `feature_extract` to `True` we return
# the second to last layer.
fc7 = AlexNet(resized, feature_extract=True)
# TODO: Define a new fully connected layer followed by a softmax activation to classify
# the traffic signs. Assign the result of the softmax activation to `probs` below.
# HINT: Look at the final layer definition in alexnet.py to get an idea of what this
# should look like.
shape = (fc7.get_shape().as_list()[-1], nb_classes
         )  # use this shape for the weight matrix
# fc8, 43
fc8W = tf.Variable(tf.truncated_normal(shape, stddev=1e-2))
fc8b = tf.Variable(tf.zeros(nb_classes))
logits = tf.matmul(fc7, fc8W) + fc8b
probs = tf.nn.softmax(logits)

init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
passing them to AlexNet.
"""
import time
import tensorflow as tf
import numpy as np
from scipy.misc import imread
from caffe_classes import class_names
from alexnet import AlexNet

x = tf.placeholder(tf.float32, (None, 32, 32, 3))
# TODO: Resize the images so they can be fed into AlexNet.
# HINT: Use `tf.image.resize_images` to resize the images
resized = tf.image.resize_images (x, [227, 227])

assert resized is not Ellipsis, "resized needs to modify the placeholder image size to (227,227)"
probs = AlexNet(resized)

init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

# Read Images
im1 = imread("construction.jpg").astype(np.float32)
im1 = im1 - np.mean(im1)

im2 = imread("stop.jpg").astype(np.float32)
im2 = im2 - np.mean(im2)

# Run Inference
t = time.time()
output = sess.run(probs, feed_dict={x: [im1, im2]})
Example #5
0
                                 shape=(None, args.img_size, args.img_size, 3))
    input_labels = tf.placeholder(dtype=tf.float32,
                                  shape=(None, None),
                                  name='train_labels')
    input_vectors = tf.placeholder(dtype=tf.float32,
                                   shape=(None, dim_vector),
                                   name='seen_word_vector')
    batch_input_labels = tf.placeholder(dtype=tf.float32,
                                        shape=(None, sementic_batch_size),
                                        name='batch_train_labels')

    semantic_embedding = tf.placeholder(dtype=tf.float32,
                                        shape=(nb_labels_seen, args.delen),
                                        name='semantic_embedding')

    model = AlexNet(input_image, dropout_rate, args.hash_bits,
                    args.nb_labels_seen, skip_layer, args.weights_path)
    hash_layer = model.fc8
    visual_fc1 = fc(hash_layer,
                    args.hash_bits,
                    args.delen,
                    'visual_fc1',
                    relu=False)

    c_matrix1 = tf.matmul(visual_fc1,
                          semantic_embedding,
                          transpose_a=False,
                          transpose_b=True)

    regularizer_loss = tf.reduce_mean(
        tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES))
    visual_rank_loss = ranking_loss(c_matrix1, input_labels) + regularizer_loss
Example #6
0
    wandb.init(project='homework1-cc7221', entity='p137')
    wandb.run.name = wandb.run.name + args.tag

    # model selection
    if args.model == 'resnet':
        model = ResNet50(img_channel=3, num_classes=19)
        # model = torch.hub.load('pytorch/vision:v0.9.0', 'resnet50', pretrained=False)
    elif args.model == 'resnet_torch':
        model = torch.hub.load('pytorch/vision:v0.9.0',
                               'resnet50',
                               pretrained=False)
    elif args.model == 'resnext':
        model = resnext50(img_channel=3, num_classes=19)
    elif args.model == 'alexnet':
        model = AlexNet(num_classes=19)
    else:
        raise ValueError("This utility can't train that kind of model.")

    logging.info("Setting dataset")
    torch.cuda.empty_cache()
    train_dataset = TrainImageDataset(
        args.data,
        224,
        224,
    )
    optimizer = torch.optim.Adam(model.parameters(), args.lr)
    # scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.1)
    criterion = nn.CrossEntropyLoss()
    config = wandb.config
Example #7
0
def get_activations(layer_num, base_model, mode='summation',
                    folder='/home/z840/ALISURE/Data/VOC2012/ILSVRC2012_img_val'):
    """
    返回矩阵 activations[i][j] 是 summed/maxed 过得 image[i] at filter[j]
    矩阵保存到 .csv 文件, 之后'find_strongest_images'会使用
    :param layer_num: Specifies layer, whose filters' activations are returned
    :param base_model: Pass alexnet_model 
    :param mode: either 'summation' or 'maximum'
    :param folder: Specify folder that contains validation images
    """

    assert mode in ('summation', 'maximum'), "Mode has to be either 'summation' or 'maximum'"

    print('Working on layer {}\n'.format(layer_num))
    filters = AlexNet.channels[layer_num]
    activation_matrix_filename = 'data/Strongest_Activation_Layer{}.csv'.format(layer_num)

    # 初始化激活矩阵, Filter and image numbers start with 1!!
    activations = np.full((AlexNet.val_set_size + 1, filters + 1), fill_value=0.0, dtype=np.float32)

    # Create Model up to layer_num
    model = AlexNet(layer_num, base_model)

    # For timing
    timesteps = [time.time()]
    save_interval = 10000
    time_interval = 1000
    for img_id in range(1, AlexNet.val_set_size + 1):

        # Convert image_id to file location
        id_str = str(img_id)
        while len(id_str) < 5:
            id_str = '0' + id_str
        img_name = folder + '/ILSVRC2012_val_000' + id_str + '.JPEG'

        # 获得对应层的特征图
        activation_img = model.predict(img_name)

        # Make sure that dimensions 2 and 3 are spacial (Image is square)
        assert activation_img.shape[2] == activation_img.shape[3], "Index ordering incorrect"

        # 降维
        if mode == 'summation':
            assert activation_img.shape[2] == activation_img.shape[3], "Index ordering incorrect"
            activation_img = activation_img.sum(3)
            activation_img = activation_img.sum(2)

        if mode == 'maximum':
            activation_img = np.nanmax(activation_img, axis=3)
            activation_img = np.nanmax(activation_img, axis=2)

        # Remove batch size dimension
        assert activation_img.shape[0] == 1
        activation_img = activation_img.sum(0)

        # 使得激活图从1开始
        activation_img = np.insert(activation_img, 0, 0.0)

        #  activation_image现在存储filter(下标从1开始)
        #  每个filter通过maximum/summed 处理

        # 拷贝激活值到图片
        activations[img_id] = activation_img[:]

        # Print progress
        if img_id % time_interval == 0:
            timesteps.append(time.time())

            last_interval = timesteps[-1] - timesteps[-2]
            total_execution = timesteps[-1] - timesteps[0]
            expected_remaining_time = total_execution / img_id * (AlexNet.val_set_size - img_id)

            print("Current image ID: {}".format(img_id))
            print("The last 100 images took {0:.2f} s.".format(100 * last_interval / time_interval))
            print("The average time per 100 images so far is {0:.2f} s".format(100 * total_execution / img_id))
            print("Total execution time so far: {0:.2f} min".format(total_execution / 60))
            print("The extrapolated remaining time is {0:.2f} min".format(expected_remaining_time / 60))
            print('\n')

        # save
        if img_id % save_interval == 0:
            print("Results for first {} images saved\n".format(img_id))
            try:
                os.remove(activation_matrix_filename)
            except OSError:
                pass
            np.savetxt(activation_matrix_filename, activations, delimiter=',')

    np.savetxt(activation_matrix_filename, activations, delimiter=',')
Example #8
0
def main():
    """
    Configuration Part.
    """

    # Path to the textfiles for the trainings and validation set
    train_file = './train.txt'
    val_file = './val.txt'

    # Learning params
    learning_rate = 0.01
    num_epochs = 100
    batch_size = 10

    # Network params
    dropout_rate = 0.5
    num_classes = 2
    train_layers = ['fc8', 'fc7', 'fc6']

    # How often we want to write the tf.summary data to disk
    display_step = 20

    # Path for tf.summary.FileWriter and to store model checkpoints
    filewriter_path = "./tmp/tensorboard"
    checkpoint_path = "./tmp/checkpoints"
    """
    Main Part of the finetuning Script.
    """

    # Create parent path if it doesn't exist
    if not os.path.isdir(checkpoint_path):
        os.mkdir(checkpoint_path)

    # Place data loading and preprocessing on the cpu
    with tf.device('/cpu:0'):
        tr_data = ImageDataGenerator(train_file,
                                     mode='training',
                                     batch_size=batch_size,
                                     num_classes=num_classes,
                                     shuffle=True)
        val_data = ImageDataGenerator(val_file,
                                      mode='inference',
                                      batch_size=batch_size,
                                      num_classes=num_classes,
                                      shuffle=False)

        # create an reinitializable iterator given the dataset structure
        iterator = Iterator.from_structure(tr_data.data.output_types,
                                           tr_data.data.output_shapes)
        next_batch = iterator.get_next()

    # Ops for initializing the two different iterators
    training_init_op = iterator.make_initializer(tr_data.data)
    validation_init_op = iterator.make_initializer(val_data.data)

    # TF placeholder for graph input and output
    x = tf.placeholder(tf.float32, [None, 227, 227, 3])
    y = tf.placeholder(tf.float32, [None, num_classes])
    keep_prob = tf.placeholder(tf.float32)

    # Initialize model
    model = AlexNet(x, keep_prob, num_classes, train_layers)

    # Link variable to model output
    score = model.fc8
    pred = tf.nn.softmax(score)

    # List of trainable variables of the layers we want to train
    var_list = [
        v for v in tf.trainable_variables()
        if v.name.split('/')[0] in train_layers
    ]

    # Op for calculating the loss
    with tf.name_scope("cross_ent"):
        loss = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(logits=score, labels=y))

    # Train op
    with tf.name_scope("train"):
        # Get gradients of all trainable variables
        gradients = tf.gradients(loss, var_list)
        gradients = list(zip(gradients, var_list))

        # Create optimizer and apply gradient descent to the trainable variables
        # optimizer = tf.train.GradientDescentOptimizer(learning_rate)
        optimizer = tf.train.AdamOptimizer(learning_rate)
        train_op = optimizer.apply_gradients(grads_and_vars=gradients)

    # Add gradients to summary
    for gradient, var in gradients:
        tf.summary.histogram(var.name + '/gradient', gradient)

    # Add the variables we train to the summary
    for var in var_list:
        tf.summary.histogram(var.name, var)

    # Add the loss to summary
    tf.summary.scalar('cross_entropy', loss)

    # Evaluation op: Accuracy of the model
    with tf.name_scope("accuracy"):
        correct_pred = tf.equal(tf.argmax(score, 1), tf.argmax(y, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

    # Add the accuracy to the summary
    tf.summary.scalar('accuracy', accuracy)

    # Merge all summaries together
    merged_summary = tf.summary.merge_all()

    # Initialize the FileWriter
    writer = tf.summary.FileWriter(filewriter_path)

    # Initialize an saver for store model checkpoints
    saver = tf.train.Saver()

    # Get the number of training/validation steps per epoch
    train_batches_per_epoch = int(np.floor(tr_data.data_size / batch_size))
    val_batches_per_epoch = int(np.floor(val_data.data_size / batch_size))

    # Start Tensorflow session
    with tf.Session() as sess:
        print("# isTrain : ", args.istrain)
        if not args.istrain:
            ckpt = tf.train.get_checkpoint_state(checkpoint_path)
            if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path):
                print("Reading model parameters from %s" %
                      ckpt.model_checkpoint_path)
                saver.restore(sess, ckpt.model_checkpoint_path)
            else:
                print("checkpoint path isn't correct")
                return

            file = tf.read_file(args.input_path)
            decoded_img = tf.image.decode_jpeg(file, channels=3)
            resized_img = tf.image.resize_images(decoded_img, [227, 227])
            resized_img = tf.reshape(resized_img, [1, 227, 227, 3])
            print("# decoded img : ", decoded_img.eval().shape)

            pred_ = sess.run(pred,
                             feed_dict={
                                 x: resized_img.eval(),
                                 y: [[1, 0]],
                                 keep_prob: np.array(1.0)
                             })
            print("P(man|data)  : ", pred_[0][0])
            print("P(woman|data)  : ", pred_[0][1])

            img = decoded_img.eval()
            plt.imshow(img)
            plt.show()

            if args.visualize:
                w1, w2, w3, w4, w5 = sess.run(model.weight,
                                              feed_dict={
                                                  x: resized_img.eval(),
                                                  y: [[1, 0]],
                                                  keep_prob: np.array(1.0)
                                              })
                print("W1 : ", w1.shape)
                visualize(w1[:, :, 0, :25])
                print("W2 : ", w2.shape)
                visualize(w2[:, :, 0, :25])
                print("W3 : ", w3.shape)
                visualize(w3[:, :, 0, :25])
                print("W4 : ", w4.shape)
                visualize(w4[:, :, 0, :25])
                print("W5 : ", w5.shape)
                visualize(w5[:, :, 0, :25])
                f1, f2, f3, f4, f5 = sess.run(model.fm, {
                    x: resized_img.eval(),
                    y: [[1, 0]],
                    keep_prob: np.array(1.0)
                })
                print("F1 : ", f1.shape)
                visualize(f1[0][:, :, :25])
                print("F2 : ", f2.shape)
                visualize(f2[0][:, :, :25])
                print("F3 : ", f3.shape)
                visualize(f3[0][:, :, :25])
                print("F4 : ", f4.shape)
                visualize(f4[0][:, :, :25])
                print("F5 : ", f5.shape)
                visualize(f5[0][:, :, :25])

            return
        else:
            # Initialize all variables
            sess.run(tf.global_variables_initializer())

        # Add the model graph to TensorBoard
        writer.add_graph(sess.graph)

        # Load the pretrained weights into the non-trainable layer
        model.load_initial_weights(sess)

        print("{} Start training...".format(datetime.now()))
        print("{} Open Tensorboard at --logdir {}".format(
            datetime.now(), filewriter_path))

        # Loop over number of epochs
        for epoch in range(num_epochs):

            print("{} Epoch number: {}".format(datetime.now(), epoch + 1))

            # Initialize iterator with the training dataset
            sess.run(training_init_op)
            train_acc = 0.
            train_count = 0.
            for step in range(train_batches_per_epoch):
                # get next batch of data
                img_batch, label_batch = sess.run(next_batch)

                # And run the training op
                acc, _ = sess.run([accuracy, train_op],
                                  feed_dict={
                                      x: img_batch,
                                      y: label_batch,
                                      keep_prob: dropout_rate
                                  })
                train_acc += acc
                train_count += 1

                # Generate summary with the current batch of data and write to file
                # if step % display_step == 0:
                #     s = sess.run(merged_summary, feed_dict={x: img_batch,
                #                                             y: label_batch,
                #                                             keep_prob: 1.})
                #
                #     writer.add_summary(s, epoch*train_batches_per_epoch + step)
            train_acc /= train_count
            print("{} Train Accuracy = {:.4f}".format(datetime.now(),
                                                      train_acc))

            # Validate the model on the entire validation set
            print("{} Start validation".format(datetime.now()))
            sess.run(validation_init_op)
            test_acc = 0.
            test_count = 0

            for ind in range(val_batches_per_epoch):

                img_batch, label_batch = sess.run(next_batch)
                acc = sess.run(accuracy,
                               feed_dict={
                                   x: img_batch,
                                   y: label_batch,
                                   keep_prob: 1.
                               })
                test_acc += acc
                test_count += 1
                if epoch is 2 and ind is 0:
                    fm = sess.run(model.fm,
                                  feed_dict={
                                      x: img_batch,
                                      y: label_batch,
                                      keep_prob: 1.
                                  })
                    weight = sess.run(model.weight,
                                      feed_dict={
                                          x: img_batch,
                                          y: label_batch,
                                          keep_prob: 1.
                                      })
                    # print("fm0 : ", np.array(fm[0]).shape)
                    # print("fm1 : ", np.array(fm[1]).shape)
                    # print("fm2 : ", np.array(fm[2]).shape)
                    # print("fm3 : ", np.array(fm[3]).shape)
                    # print("fm4 : ", np.array(fm[4]).shape)
                    #
                    # print("weight0 : ", np.array(weight[0]).shape)
                    # print("weight1 : ", np.array(weight[1]).shape)
                    # print("weight2 : ", np.array(weight[2]).shape)
                    # print("weight3 : ", np.array(weight[3]).shape)
                    # print("weight4 : ", np.array(weight[4]).shape)

            test_acc /= test_count
            print("{} Validation Accuracy = {:.4f}".format(
                datetime.now(), test_acc))
            print("{} Saving checkpoint of model...".format(datetime.now()))

            # save checkpoint of the model
            checkpoint_name = os.path.join(
                checkpoint_path, 'model_epoch' + str(epoch + 1) + '.ckpt')
            save_path = saver.save(sess, checkpoint_name)

            print("{} Model checkpoint saved at {}".format(
                datetime.now(), checkpoint_name))
def get_activations(layer_num, base_model, mode='summation', folder='ILSVRC2012_img_val'):
    """
    Returns a matrix s.t. activations[i][j] is the summed/maxed activation of image[i] at filter[j] for a given layer
    This matrix of activations is saved as a .csv file and will later be used by 'find_strongest_images'
    :param layer_num: Specifies layer, whose filters' activations are returned
    :param base_model: Pass alexnet_model 
    :param mode: either 'summation' or 'maximum'
    :param folder: Specify folder that contains validation images
    """

    assert mode in ('summation', 'maximum'), "Mode has to be either 'summation' or 'maximum'"

    print('Working on layer {}\n'.format(layer_num))
    filters = AlexNet.channels[layer_num]
    activation_matrix_filename = 'Data/Strongest_Activation_Layer{}.csv'.format(layer_num)

    # Init array to save activations, Filter and image numbers start with 1!!
    activations = np.full((AlexNet.val_set_size + 1, filters + 1), fill_value=0.0, dtype=np.float32)

    # Create Model up to layer_num
    model = AlexNet(layer_num, base_model)

    # For timing
    timesteps = [time.time()]
    save_interval = 10000
    time_interval = 1000
    for img_id in range(1, AlexNet.val_set_size + 1):

        # Convert image_id to file location
        id_str = str(img_id)
        while len(id_str) < 5:
            id_str = '0' + id_str
        img_name = folder + '/ILSVRC2012_val_000' + id_str + '.JPEG'

        # Get activations for shortened model
        activation_img = model.predict(img_name)

        # Make sure that dimensions 2 and 3 are spacial (Image is square)
        assert activation_img.shape[2] == activation_img.shape[3], "Index ordering incorrect"

        if mode == 'summing':
            # Sum over spacial dimension to get activation for given filter and given image
            assert activation_img.shape[2] == activation_img.shape[3], "Index ordering incorrect"
            activation_img = activation_img.sum(3)
            activation_img = activation_img.sum(2)

        if mode == 'maximum':
            # Find maximum activation for each filter for a given image
            activation_img = np.nanmax(activation_img, axis=3)
            activation_img = np.nanmax(activation_img, axis=2)

        # Remove batch size dimension
        assert activation_img.shape[0] == 1
        activation_img = activation_img.sum(0)

        # Make activations 1-based indexing
        activation_img = np.insert(activation_img, 0, 0.0)

        #  activation_image is now a vector of length equal to number of filters (plus one for one-based indexing)
        #  each entry corresponds to the maximum/summed activation of each filter for a given image form validation set

        # Copy activation results for image to matrix
        activations[img_id] = activation_img[:]

        # Print progress
        if img_id % time_interval == 0:
            timesteps.append(time.time())

            last_interval = timesteps[-1] - timesteps[-2]
            total_execution = timesteps[-1] - timesteps[0]
            expected_remaining_time = total_execution / img_id * (AlexNet.val_set_size - img_id)

            print("Current image ID: {}".format(img_id))
            print("The last 100 images took {0:.2f} s.".format(100 * last_interval / time_interval))
            print("The average time per 100 images so far is {0:.2f} s".format(100 * total_execution / img_id))
            print("Total execution time so far: {0:.2f} min".format(total_execution / 60))
            print("The extrapolated remaining time is {0:.2f} min".format(expected_remaining_time / 60))
            print('\n')

        # Update output file
        if img_id % save_interval == 0:
            print("Results for first {} images saved\n".format(img_id))
            try:
                os.remove(activation_matrix_filename)
            except OSError:
                pass
            np.savetxt(activation_matrix_filename, activations, delimiter=',')

    np.savetxt(activation_matrix_filename, activations, delimiter=',')
Example #10
0
    if args.enable_lat:
        real_model_path = args.model_path + "lat_param.pkl"
        print('loading the LAT model')
    else:
        real_model_path = args.model_path + "naive_param.pkl"
        print('loading the naive model')
    '''
    if args.test_flag:
        args.enable_lat = False
    '''
    # switch models
    if args.model == 'alexnet':
        cnn = AlexNet(enable_lat=args.enable_lat,
                      epsilon=args.epsilon,
                      pro_num=args.pro_num,
                      batch_size=args.batchsize,
                      num_classes=200,
                      if_dropout=args.dropout)
    elif args.model == 'resnet':
        cnn = ResNet18(enable_lat=args.enable_lat,
                       epsilon=args.epsilon,
                       pro_num=args.pro_num,
                       batch_size=args.batchsize,
                       num_classes=200,
                       if_dropout=args.dropout)
        #cnn.apply(conv_init)
    elif args.model == 'alexnetBN':
        cnn = AlexNetBN(enable_lat=args.enable_lat,
                        epsilon=args.epsilon,
                        pro_num=args.pro_num,
                        batch_size=args.batchsize,
Example #11
0
        # y_diff[layer].append(box_borders[-1][3] - box_borders[-1][2])

        projections.append(projection)
    superposed_projections = np.maximum.reduce(projections)
    # superposed_projections = sum(projections)
    assert superposed_projections.shape == projections[0].shape
    DeconvOutput(superposed_projections).save_as(
        save_to_folder, '{}_activations.JPEG'.format(img_id))

    original_image = preprocess_image_batch(path)
    original_image = draw_bounding_box(original_image, box_borders)
    DeconvOutput(original_image).save_as(save_to_folder,
                                         '{}.JPEG'.format(img_id))


if __name__ == '__main__':
    deconv_base_model = Deconvolution(AlexNet().model)
    # for _ in range(15):
    #     #project_top_layer_filters(deconv_base_model=deconv_base_model)
    #     #project_multiple_layer_filters(deconv_base_model=deconv_base_model)
    # project_multiple_layer_filters(deconv_base_model=deconv_base_model)
    for img_id in (14913, 31634, 48518, 37498, 2254):
        project_multiple_layer_filters(img_id=img_id,
                                       deconv_base_model=deconv_base_model)
    for img_id in (31977, ):
        project_top_layer_filters(img_id=img_id,
                                  deconv_base_model=deconv_base_model)

    pass
    # visualize_top_images(layer=5, f=4, constrast=13)
Example #12
0
import torch
from torch.nn.functional import softmax

from alexnet import AlexNet
from utils import cifar10_loader, device, cifar10_classes

torch.random.manual_seed(128)
batch_size = 1
testloader = cifar10_loader(train=False, batch_size=batch_size)

net = AlexNet()
net.load_state_dict(torch.load("model/model.h5"))
net.eval()

correct = 0
total = 0


def run():
    global correct, total
    with torch.no_grad():
        for data in testloader:
            images, labels = data
            inputs, labels = images.to(device), labels.to(device)
            outputs = net(inputs)
            _, predicted = torch.topk(outputs.data, 5)
            #print(predicted)
            indexes = predicted.numpy()[0].tolist()
            #print(indexes)
            #print(softmax(outputs).numpy()[0][indexes])
            #print([cifar10_classes[i] for i in indexes])
Example #13
0
    # Pick the class with the highest confidence for each image
    class_indices = np.argmax(probs, axis=1)
    # Display the results
    print('\n{:20} {:30} {}'.format('Image', 'Classified As', 'Confidence'))
    print('-' * 70)
    for img_idx, image_path in enumerate(image_paths):
        img_name = osp.basename(image_path)
        class_name = class_labels[class_indices[img_idx]]
        confidence = round(probs[img_idx, class_indices[img_idx]] * 100, 2)
        print('{:20} {:30} {} %'.format(img_name, class_name, confidence))


spec = DataSpec()
input_node = tf.placeholder(tf.float32, [None, 227, 227, 3])

net = AlexNet({"data": input_node})

# image = (imread(r"tank_1.jpg")[:, :, :3]).astype(np.float32)
# image -= np.mean(image)
# image = imresize(image, (227, 227, 3))
image_paths = [r"tank_1.jpg"]
image_producer = dataset.ImageProducer(image_paths=image_paths, data_spec=spec)

with tf.Session() as sess:
    # Start the image processing workers
    coordinator = tf.train.Coordinator()
    threads = image_producer.start(session=sess, coordinator=coordinator)

    # Load the converted parameters
    print('Loading the model')
    net.load(r"../AlexNet.npy", sess, encoding="latin1")
Example #14
0
    torch.backends.cudnn.benchmark = True

    SAVE_PATH = './cp_large.bin'
    logger = Logger('./largecnnlogs')

    lossfunction = nn.MSELoss()

    dataset = Rand_num()
    sampler = RandomSampler(dataset)
    loader = DataLoader(dataset,
                        batch_size=20,
                        sampler=sampler,
                        shuffle=False,
                        num_workers=1,
                        drop_last=True)
    net = AlexNet(3)
    #net.load_state_dict(torch.load(SAVE_PATH))
    net.cuda()
    optimizer = optim.Adam(net.parameters(), lr=0.001)
    for epoch in range(10000):
        for i, data in enumerate(loader, 0):
            net.zero_grad()
            video, labels = data
            video = video.view(-1, 3, 227, 227)
            labels = labels.view(-1, 3)
            labels = torch.squeeze(Variable(labels.float().cuda()))
            video = torch.squeeze(Variable((video.float() / 256).cuda()))
            net.train()
            outputs = net.forward(video)
            loss = lossfunction(outputs, labels)
            loss.backward()
Example #15
0
imgs2 = []
for i in img1:
    imgs1.append(cv2.imread(i))
for i in img2:
    imgs2.append(cv2.imread(i))
#print(imgs1[0])

from alexnet import AlexNet
from caffe_classes import class_names

#placeholder for input and dropout rate
x = tf.placeholder(tf.float32, [1, 227, 227, 3])
keep_prob = tf.placeholder(tf.float32)

#create model with default config ( == no skip_layer and 1000 units in the last layer)
model = AlexNet(x, keep_prob, 5749, ['fc8'])

#define activation of last layer as score
score = model.fc7
#f7=model.fc7;

saver=tf.train.Saver();
#create op to calculate softmax 
softmax = score
# tf.nn.softmax(score)
res=[]

with tf.Session() as sess:
    
    # Initialize all variables
    #sess.run(tf.global_variables_initializer())
Example #16
0
def main(_):
    #convert jpg image(s) into iamge representations using alexnet:
    filenames = [
        os.path.join(image_dir, f) for f in [
            'overly-attached-girlfriend.jpg',
            'high-expectations-asian-father.jpg', 'foul-bachelor-frog.jpg',
            'stoner-stanley.jpg', 'y-u-no.jpg', 'willy-wonka.jpg',
            'futurama-fry.jpg', 'success-kid.jpg', 'one-does-not-simply.jpg',
            'bad-luck-brian.jpg', 'first-world-problems.jpg',
            'philosoraptor.jpg', 'what-if-i-told-you.jpg', 'TutorPP.jpg'
        ]
    ]
    print(filenames)
    tf.logging.info("Running caption generation on %d files matching %s",
                    len(filenames), FLAGS.input_files)
    #mean of imagenet dataset in BGR
    imagenet_mean = np.array([104., 117., 124.], dtype=np.float32)

    #placeholder for input and dropout rate
    x_Alex = tf.placeholder(tf.float32, [1, 227, 227, 3])
    keep_prob_Alex = tf.placeholder(tf.float32)

    #create model with default config ( == no skip_layer and 1000 units in the last layer)
    modelAlex = AlexNet(x_Alex, keep_prob_Alex, 1000, [], ['fc7', 'fc8'],
                        512)  #maybe need to put fc8 in skip_layers

    #define activation of last layer as score
    score = modelAlex.fc6

    meme_embeddings = []
    with tf.Session() as sess:

        # Initialize all variables
        sess.run(tf.global_variables_initializer())

        # Load the pretrained weights into the model
        modelAlex.load_initial_weights(sess)

        for i, meme in enumerate(filenames):
            img = Image.open(meme)
            try:
                img.thumbnail((227, 227), Image.ANTIALIAS)
                #img = img.resize((227,227))
                #use img.thumbnail for square images, img.resize for non square
                assert np.shape(img) == (227, 227, 3)
            except AssertionError:
                img = img.resize((227, 227))
                print('sizing error')

            # Subtract the ImageNet mean
            img = img - imagenet_mean  #should probably change this

            # Reshape as needed to feed into model
            img = img.reshape((1, 227, 227, 3))

            meme_vector = sess.run(score,
                                   feed_dict={
                                       x_Alex: img,
                                       keep_prob_Alex: 1
                                   })  #[1,4096]
            meme_vector = np.reshape(meme_vector, [4096])
            assert np.shape(meme_vector) == (4096, )

            #now have np embeddings to feed for inference
            meme_embeddings.append(meme_vector)

    with open('Captions.txt', 'r') as f:
        data_captions = f.readlines()
    data_captions = [s.lower() for s in data_captions]

    # Build the inference graph.
    g = tf.Graph()
    with g.as_default():
        model = inference_wrapper.InferenceWrapper()
        restore_fn = model.build_graph_from_config(configuration.ModelConfig(),
                                                   FLAGS.checkpoint_path)
    g.finalize()

    # Create the vocabulary.
    vocab = vocabulary.Vocabulary(FLAGS.vocab_file)

    #filenames = []
    #for file_pattern in FLAGS.input_files.split(","):
    #filenames.extend(tf.gfile.Glob(file_pattern))
    #tf.logging.info("Running caption generation on %d files matching %s",
    #len(filenames), FLAGS.input_files)
    with tf.Session(graph=g) as sess:
        # Load the model from checkpoint.
        restore_fn(sess)

        # Prepare the caption generator. Here we are implicitly using the default
        # beam search parameters. See caption_generator.py for a description of the
        # available beam search parameters.
        generator = caption_generator.CaptionGenerator(model, vocab)
        num_in_data_total = 0
        num_captions = 0
        for i, meme in enumerate(meme_embeddings):
            #with tf.gfile.GFile(filename, "rb") as f:
            #image = f.read()
            captions = generator.beam_search(sess, meme)
            print("Captions for image %s:" % os.path.basename(filenames[i]))
            num_in_data = 0
            for i, caption in enumerate(captions):
                # Ignore begin and end words.
                sentence = [
                    vocab.id_to_word(w) for w in caption.sentence[1:-1]
                ]
                sentence = " ".join(sentence)
                in_data = 0
                if b_any(sentence in capt for capt in data_captions):
                    in_data = 1
                    num_in_data += 1
                    num_in_data_total += 1
                    num_captions += 1
                else:
                    num_captions += 1
                print("  %d) %s (p=%f) [in data = %d]" %
                      (i, sentence, math.exp(caption.logprob), in_data))
            print("number of captions in data = %d" % (num_in_data))
        print("(total number of captions in data = %d) percent in data = %f" %
              (num_in_data_total, (num_in_data_total / num_captions)))
Example #17
0
def main():
    """build, train, and test an implementation of the alexnet CNN model in keras.
    This model is trained and tested on the CIFAR-100 dataset
    """
    # parse arguments
    args = parse_arguments()
    save_dir = os.path.join(os.getcwd(), args['d'])
    if not os.path.isdir(save_dir):
        os.makedirs(save_dir)
    model_path = os.path.join(save_dir, args['n'])
    epochs = int(args['e'])
    predict_flag = args['p']
    show_flag = args['f']
    cifar10_label = [
        'airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog',
        'horse', 'ship', 'truck'
    ]
    # build and train the model
    if predict_flag == True:
        if show_flag == True:
            print(filter)
            alexnet = AlexNet(10)
            model = alexnet.build_model(class_count=10)
            model.load_weights(model_path)
            print("load complete")

            (x_train, y_train), (x_test, y_test) = load_dataset()
            batch_generator = generator_ramdom_one(10, 112, 112, x_test,
                                                   y_test)
            test_x, test_y = next(batch_generator)

            layer = "conv2d_2"
            show_filter(model, test_x, layer)

        else:
            alexnet = AlexNet(10)
            model = alexnet.build_model(class_count=10)
            model.load_weights(model_path)
            print("load complete")

            (x_train, y_train), (x_test, y_test) = load_dataset()
            batch_generator = generator_ramdom_one(10, 112, 112, x_test,
                                                   y_test)

            test_x, test_y = next(batch_generator)
            print(cifar10_label[np.argmax(test_y)])
            result = model.predict(test_x)
            print(cifar10_label[np.argmax(result)])
            test_img_x = test_x[0, ::-1].copy()
            plt.imshow(test_img_x / 255, interpolation='nearest')
            plt.title(cifar10_label[np.argmax(test_y)] + "/" +
                      cifar10_label[np.argmax(result)])
            plt.show()

    else:
        alexnet = AlexNet(10)
        model = alexnet.build_model(class_count=10)
        print(model.summary())
        train_model(model, class_count=10, epochs=epochs)

        # test the model
        evaluate(model, class_count=10)

        # save the trained model
        model.save(model_path)
        print("Alexnet model saved to: %s" % model_path)
Example #18
0
    os.path.join(image_dir, f) for f in os.listdir(image_dir)
    if f.endswith('.JPEG')
]

# load all images
imgs = []
for f in img_files:
    imgs.append(cv2.imread(f))

# placeholder for input and dropout rate
x = tf.placeholder(tf.float32, [None, 227, 227, 3])
keep_prob = tf.placeholder(tf.float32)

# create model with default config ( == no skip_layer and 1000 units in the last layer)
model = AlexNet(x,
                keep_prob,
                1000, [],
                weights_path="./weights/bvlc_alexnet.npy")

# define activation of last layer as score
score = model.fc8

# create op to calculate softmax
softmax = tf.nn.softmax(score)

with tf.Session() as sess:
    # Initialize all variables
    sess.run(tf.global_variables_initializer())

    # Load the pretrained weights into the model
    model.load_initial_weights(sess)
Example #19
0
    # Find maximum activation for each filter for a given image
    activation_img = np.nanmax(activation_img, axis=3)
    activation_img = np.nanmax(activation_img, axis=2)

    # Remove batch size dimension
    assert activation_img.shape[0] == 1
    activation_img = activation_img.sum(0)

    # Make activations 1-based indexing
    activation_img = np.insert(activation_img, 0, 0.0)

    #  activation_image is now a vector of length equal to number of filters (plus one for one-based indexing)
    #  each entry corresponds to the maximum/summed activation of each filter for a given image

    top_filters = activation_img.argsort()[-top:]
    return list(top_filters)


if __name__ == '__main__':
    # 获得网络
    base_model = AlexNet().model
    # Get activations and copy maximally activating images to folder
    # This takes a while! (~4h on my Surface i5)
    for i in (5,):  # 4, 3, 2, 1
        # 为数据集创建一个filter张特征图表示, 也就是创建(number, filter)数据,然后存入文件,number表示数据集
        # get_activations(i, base_model, mode='summation')
        start = time.time()
        # 从数据集中选择对每个filter响应最大图片拷贝到目录下
        find_strongest_image(i)
        print("Copied images in {} s\n".format(time.time() - start))
with open(training_file, mode='rb') as f:
    train = pickle.load(f)
with open(validation_file, mode='rb') as f:
    valid = pickle.load(f)
with open(testing_file, mode='rb') as f:
    test = pickle.load(f)

X_train, y_train = train['features'], train['labels']
X_valid, y_valid = valid['features'], valid['labels']
X_test, y_test = test['features'], test['labels']

# TODO: Split data into training and validation sets.

# TODO: Define placeholders and resize operation.

# TODO: pass placeholder as first argument to `AlexNet`.
fc7 = AlexNet(..., feature_extract=True)
# NOTE: `tf.stop_gradient` prevents the gradient from flowing backwards
# past this point, keeping the weights before and up to `fc7` frozen.
# This also makes training faster, less work to do!
fc7 = tf.stop_gradient(fc7)

# TODO: Add the final layer for traffic sign classification.

# TODO: Define loss, training, accuracy operations.
# HINT: Look back at your traffic signs project solution, you may
# be able to reuse some the code.

# TODO: Train and evaluate the feature extraction model.
    # create an reinitializable iterator given the dataset structure
    iterator = Iterator.from_structure(test_data.data.output_types,
                                       test_data.data.output_shapes)
    next_batch = iterator.get_next()

# Ops for initializing the two different iterators
testing_init_op = iterator.make_initializer(test_data.data)

# TF placeholder for graph input and output
x = tf.placeholder(tf.float32, [batch_size, 227, 227, 3])
y = tf.placeholder(tf.float32, [batch_size, num_classes])
keep_prob = tf.placeholder(tf.float32)

# Initialize model
model = AlexNet(x, keep_prob, num_classes, [])

# Link variable to model output
score = model.fc8

softmax = tf.nn.softmax(score)

# Initialize an saver for store model checkpoints
saver = tf.train.Saver()

labels = indexToLabel()
f = open('word_predictions.txt', 'wb')

# Start Tensorflow session
with tf.Session() as sess:
    saver = tf.train.import_meta_graph(
    #cv2.waitKey(0)

#plot images
#fig = plt.figure(figsize=(15,6))
#for i, img in enumerate(imgs):
#    fig.add_subplot(1,41,i+1)
#    plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
#    plt.show()
#    plt.axis('off')

#placeholder for input and dropout rate
x = tf.placeholder(tf.float32, [1, 227, 227, 3])
keep_prob = tf.placeholder(tf.float32)

#create model with default config ( == no skip_layer and 1000 units in the last layer)
model = AlexNet(x, keep_prob, 3, [])

#define activation of last layer as score
score = model.fc8

#create op to calculate softmax
softmax = tf.nn.softmax(score)

with tf.Session() as sess:

    # Initialize all variables
    sess.run(tf.global_variables_initializer())

    # Load the pretrained weights into the model
    model.load_initial_weights(sess)
# Modify the original ipython code into the lines of code in Python
array1 = os.listdir(
    '/home/mike/Documents/Alexnet_Callback/content/train/ships')
array2 = os.listdir(
    '/home/mike/Documents/Alexnet_Callback/content/train/bikes')
array3 = os.listdir(
    '/home/mike/Documents/Alexnet_Callback/content/validation/ships')
array4 = os.listdir(
    '/home/mike/Documents/Alexnet_Callback/content/validation/bikes')

# Add the print function to show the results of images ended with jpg
print(array1, array2, array3, array4)

# Call the alexnet model in alexnet.py
model = AlexNet((227, 227, 3), num_classes)

# Compile the model
model.compile(optimizer=tf.keras.optimizers.Adam(0.001),
              loss='categorical_crossentropy',
              metrics=['accuracy'])

# It will output the AlexNet model after executing the command
model.summary()

# Designate the directories for training, validation and model saved.
train_dir = '/home/mike/Documents/Alexnet_Callback/content/train'
valid_dir = '/home/mike/Documents/Alexnet_Callback/content/validation'
model_dir = '/home/mike/Documents/Alexnet_Callback/content/my_model.h5'

# Assign both the image and the diretory generators
with open(validation_file, mode='rb') as f:
    valid = pickle.load(f)
with open(testing_file, mode='rb') as f:
    test = pickle.load(f)

X_train, y_train = train['features'], train['labels']
X_validation, y_validation = valid['features'], valid['labels']
X_test, y_test = test['features'], test['labels']

# TODO: Define placeholders and resize operation.
X_train_resized = tf.image.resize_images(X_train, (227, 227))
X_validation_resized = tf.image.resize_images(X_validation, (227, 227))
X_test_resized = tf.image.resize_images(X_test, (227, 227))

# TODO: pass placeholder as first argument to `AlexNet`.
fc7 = AlexNet(X_train_resized, feature_extract=True)
# NOTE: `tf.stop_gradient` prevents the gradient from flowing backwards
# past this point, keeping the weights before and up to `fc7` frozen.
# This also makes training faster, less work to do!
fc7 = tf.stop_gradient(fc7)

# TODO: Add the final layer for traffic sign classification.
shape = (fc7.get_shape().as_list()[-1], nb_classes
         )  # use this shape for the weight matrix
fc8W = tf.Variable(tf.truncated_normal(shape, stddev=0.01))
fc8b = tf.Variable(tf.zeros(nb_classes))

logits = tf.matmul(fc7, fc8W) + fc8b
probs = tf.nn.softmax(logits)
# TODO: Define loss, training, accuracy operations.
# HINT: Look back at your traffic signs project solution, you may
Example #25
0
from alexnet import AlexNet

height, width, channel, num_class = 227, 227, 3, 2

dataset_path = 'G:\\dataset\\kaggle\dog-vs-cat\\dogs-vs-cats-redux-kernels-edition\\train'

alexNet = AlexNet(height, width, channel, num_class, dataset_path)
alexNet.train()
Example #26
0
from alexnet import AlexNet
from image_loader import load_images

files = sys.argv[1:]
imgs = load_images(files)

m, h, w, _ = imgs.shape

# load the saved tensorflow model and evaluate a list of paths to PNG files (must be 150x150)
num_classes = 1

X = tf.placeholder(tf.float32, shape=(m, h, w, 1))
Y = tf.placeholder(tf.float32, shape=(m, num_classes))
dropout = tf.placeholder(tf.float32)

model = AlexNet(X, dropout, num_classes)

predictions = model.logits > 0

saver = tf.train.Saver()

with tf.Session() as sess:
    saver.restore(sess, "./tensorflow-ckpt/model.ckpt")
    logits = sess.run(model.logits, feed_dict={X: imgs, dropout: 0})
    _, y_h, y_w, __ = logits.shape
    width_between = (w - 150) / y_w
    height_between = (h - 150) / y_h
    for m, res in enumerate(logits):
        fig, ax = plt.subplots(1, figsize=(8.5, 11))
        ax.imshow(np.squeeze(imgs[m]), cmap="gray")
        for i, row in enumerate(res):
Example #27
0
    # create an reinitializable iterator given the dataset structure
    iterator = Iterator.from_structure(tr_data.data.output_types,
                                       tr_data.data.output_shapes)
    next_batch = iterator.get_next()

# Ops for initializing the two different iterators
training_init_op = iterator.make_initializer(tr_data.data)
validation_init_op = iterator.make_initializer(val_data.data)

# TF placeholder for graph input and output
x = tf.placeholder(tf.float32, [batch_size, 227, 227, 3])
y = tf.placeholder(tf.float32, [batch_size, num_classes])
keep_prob = tf.placeholder(tf.float32)

# Initialize model
model = AlexNet(x, keep_prob, num_classes, train_layers)

# Link variable to model output
score = model.fc8

# List of trainable variables of the layers we want to train
var_list = [v for v in tf.trainable_variables() if v.name.split('/')[0] in train_layers]

# Op for calculating the loss
with tf.name_scope("cross_ent"):
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=score,
                                                                  labels=y))

# Train op
with tf.name_scope("train"):
    # Get gradients of all trainable variables
Example #28
0
image_dir=os.path.join(current,'images')
img_files=[os.path.join(image_dir,f)for f in os.listdir(image_dir) if f.endswith('.jpeg')]
imgs=[]
for f in img_files:
    imgs.append(cv2.imread(f))
fig=plt.figure(figsize=(15,6))
# for i,img in enumerate(imgs):
#     fig.add_subplot(1,3,i+1)
#     # plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2GRB))
#     plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
#     plt.axis('off')
# plt.show()

x=tf.placeholder(tf.float32,[1,227,227,3])
keep_prob=tf.placeholder(tf.float32)
model=AlexNet(x,keep_prob,1000,[])
score=model.fc8
softmax=tf.nn.softmax(score)
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.5
config.gpu_options.allow_growth = True
config.gpu_options.allocator_type = 'BFC'
with tf.Session(config=config) as sess:
    sess.run(tf.global_variables_initializer())
    model.load_initial_weights(sess)
    fig2=plt.figure(figsize=(15,6))
    for i,image in enumerate(imgs):
        # cv2.imshow(str(i),image)
        img=cv2.resize(image.astype(np.float32),(227,227))
        img-=imagenet_mean
        img=img.reshape((1,227,227,3))
Example #29
0
    with tf.variable_scope(name) as scope:
        weights = tf.get_variable('weights',
                                  shape=[num_in, num_out],
                                  trainable=True)
        biases = tf.get_variable('biases', [num_out], trainable=True)
        act = tf.nn.xw_plus_b(x, weights, biases, name=scope.name)
    return act


with tf.Session() as sess:
    if FLAGS.use_alexnet:
        if FLAGS.use_logistic == True:
            clf = linear_model.LogisticRegression()

            input_x = tf.placeholder(tf.float32, [None, 227, 227, 3])
            model = AlexNet(input_x, 1, 1000, [])
            tf.global_variables_initializer().run()
            model.load_initial_weights(sess)

            train_dict = readTrainSet()
            train_set = loadTrainSet(train_dict)
            test_set = loadTestNp()

            train_epoch = np.zeros((50, 227, 227, 3))
            train_epoch_label = np.zeros((50, ))
            for i in range(10):
                for c in range(50):
                    train_epoch[c] = train_set[c + 1][i + 1]
                    train_epoch_label[c] = c + 1
                _fc = sess.run([model.fc7], feed_dict={input_x:
                                                       train_epoch})[0]
def main():
    # tensors
    features = tf.placeholder(tf.float32, (None, 32, 32, 3))
    resized = tf.image.resize_images(features, (227, 227))
    labels = tf.placeholder(tf.int32, (None))
    labels_one_hot = tf.one_hot(labels, nb_classes)
    # TODO: make this function work above
    # train_op, acc_op, loss_op = AlexNetFeatureExtractor(resized, labels)

    # feature extraction from AlexNet
    fc7 = AlexNet(resized, feature_extract=True)
    fc7 = tf.stop_gradient(fc7)

    shape = (fc7.get_shape().as_list()[-1], nb_classes)
    fc8W = tf.Variable(tf.truncated_normal(shape, mean, stddev))
    fc8b = tf.Variable(tf.zeros(nb_classes))
    logits = tf.nn.xw_plus_b(fc7, fc8W, fc8b)
    # probs = tf.nn.softmax(logits)
    preds = tf.argmax(logits, axis=1)
    trues = tf.argmax(labels_one_hot, axis=1)
    acc_op = tf.reduce_mean(tf.cast(tf.equal(preds, trues), tf.float32))
    cross_entropy = tf.nn.softmax_cross_entropy_with_logits(
        logits, labels_one_hot)
    loss_op = tf.reduce_mean(cross_entropy)
    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
    train_op = optimizer.minimize(loss_op, var_list=[fc8W, fc8b])

    init_op = tf.global_variables_initializer()

    # load the data
    X, y = load_data()
    # split the data into train and val
    X_train, X_val, y_train, y_val = \
    train_test_split(X, y, test_size=0.25, random_state=0)

    def evaluate(sess, X_eval, y_eval):
        nb_samples = X_eval.shape[0]
        total_loss = 0
        total_acc = 0

        for start_i in range(0, nb_samples, batch_size):
            end_i = start_i + batch_size
            X_eval_batch = X_eval[start_i:end_i]
            y_eval_batch = y_eval[start_i:end_i]

            loss, accuracy = \
            sess.run([loss_op, acc_op], feed_dict={features: X_eval_batch, labels: y_eval_batch})
            total_loss += (loss * X_eval_batch.shape[0])
            total_loss += (accuracy * X_eval_batch.shape[0])

        return total_loss / nb_samples, total_acc / nb_samples

    with tf.Session() as sess:
        sess.run(init_op)
        nb_samples = len(X_train)

        # TODO: write more concisely if possible
        for epoch_i in range(epochs):
            print('Epoch {}'.format(epoch_i + 1))
            X_train, y_train = shuffle(X_train, y_train)
            t0 = time.time()

            for start_i in range(0, nb_samples, batch_size):
                end_i = start_i + batch_size
                X_batch = X_train[start_i:end_i]
                y_batch = y_train[start_i:end_i]
                sess.run(train_op,
                         feed_dict={
                             features: X_batch,
                             labels: y_batch
                         })

            val_loss, val_acc = evaluate(sess, X_val, y_val)
            # show loss and accuracy for each epoch
            print('Time {:.3f} seconds'.format(epoch_i, time.time() - t0))
            print('Validation Loss = ', val_loss)
            print('Validation Accuracy = ', val_acc)