# 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(
Exemplo n.º 2
0
                                 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

# TF placeholder for graph input and output
x = tf.placeholder(tf.float32, [batch_size, 227, 227, 3], name="x")
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"):
Exemplo n.º 3
0
import torch.optim as optim
import torch.nn as nn
from torch.autograd import Variable
from data_process import MyDataset
from  torchvision import transforms,utils
from torch.utils.data import DataLoader
import  torch

#transform 把图片装换为tensor
train_data = MyDataset(txt='./data/train.txt',transform=transforms.ToTensor())
train_loader = DataLoader(train_data,batch_size=50,shuffle=True) #返回的是迭代器
test_data = MyDataset(txt='./data/val.txt',transform=transforms.ToTensor())
test_loader = DataLoader(test_data,batch_size=50)


model = AlexNet()#.cuda()#使用gpu,将模型加载到显存
print(model)
#print(list(model.parameters()))
optimizer = optim.Adam(model.parameters(), lr = 0.001)
loss_func = nn.CrossEntropyLoss()

#开始训练
for epoch in range(30):
	print('epoch {}'.format(epoch+1))

	#training------------------------
	train_loss = 0
	train_accu = 0

	for batch_x,batch_y in train_loader:
		#batch_x,batch_y = batch_x.cuda(),batch_y.cuda()#数据加载到显存
batch_size = 128

# Load traffic signs data.
with open('./train.p', 'rb') as f:
    data = pickle.load(f)

# Split data into training and validation sets.
X_train, X_valida, y_train, y_valida = train_test_split(data['features'], data['labels'], test_size=0.33, random_state=0)

# Define placeholders and resize operation.
features = tf.placeholder(tf.float32, (None, 32, 32, 3))
labels = tf.placeholder(tf.int64, None)
resized = tf.image.resize_images(features, (227, 227))

# TODO: pass placeholder as first argument to `AlexNet`.
fc7 = AlexNet(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)

# Add the final layer for traffic sign classification.
shape = (fc7.get_shape().as_list()[-1], nb_classes)  # shape for the weight matrix(4096, 43)
fc8W = tf.Variable(tf.random_normal(shape, stddev=1e-2), dtype=tf.float32)
fc8b = tf.Variable(tf.zeros(nb_classes, dtype=tf.float32))
logits = tf.matmul(fc7, fc8W) + fc8b

# Define loss, training, accuracy operations.
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels)
loss_op = tf.reduce_mean(cross_entropy)
opt = tf.train.AdamOptimizer()
# for i, img in enumerate(imgs):
#     fig.add_subplot(1,len(imgs),i+1)
#     plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
#     plt.axis('off')

# plt.show()

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, 1000, [])

#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)
Exemplo n.º 6
0
INPUT_HEIGHT = 70
INPUT_CHANNELS = 3

NUM_CLASSES = 10

LEARNING_RATE = 0.001   # Original value: 0.01
MOMENTUM = 0.9
KEEP_PROB = 0.5

EPOCHS = 10
BATCH_SIZE = 128

print('Reading CIFAR-10...')
X_train, Y_train, X_test, Y_test = read_cifar_10(image_width=INPUT_WIDTH, image_height=INPUT_HEIGHT)

alexnet = AlexNet(input_width=INPUT_WIDTH, input_height=INPUT_HEIGHT, input_channels=INPUT_CHANNELS,
                  num_classes=NUM_CLASSES, learning_rate=LEARNING_RATE, momentum=MOMENTUM, keep_prob=KEEP_PROB)

with tf.Session() as sess:
    print('Evaluating dataset...')
    print()

    sess.run(tf.global_variables_initializer())

    print('Loading model...')
    print()
    alexnet.restore(sess, './model')

    print('Evaluating...')

    train_accuracy = alexnet.evaluate(sess, X_train, Y_train, BATCH_SIZE)
    test_accuracy = alexnet.evaluate(sess, X_test, Y_test, BATCH_SIZE)
def main():

	set_ids = loadmat('setid.mat')
	test_ids = set_ids['trnid'].tolist()[0]
	train_ids = set_ids['tstid'].tolist()[0]
	raw_train_ids = indexes_processing(train_ids)
	raw_test_ids = indexes_processing(test_ids)

	image_labels = (loadmat('imagelabels.mat')['labels'] - 1).tolist()[0]

	image_processor = ImageProcessor()
	image_processor.set_up_images()

	x = tf.placeholder(tf.float32, [None, 227, 227, 3])
	y_true = tf.placeholder(tf.float32, [None, 102])
	keep_prob = tf.placeholder(tf.float32)

	global_step = tf.Variable(0, trainable=False)
	base_lr = 0.001
	base_lr = tf.train.exponential_decay(base_lr, global_step, 20000, 0.5, staircase=True)
	num_epochs = 50000
	drop_rate = 0.5
	train_layers = ['fc8']

	model = AlexNet(x, keep_prob, 102, train_layers)

	with tf.name_scope('network_output'):
    	y_pred = model.y_pred

	all_vars = tf.trainable_variables()
	conv_vars = [all_vars[0], all_vars[2], all_vars[4], all_vars[6], all_vars[8], all_vars[10], all_vars[12]]
	bias_vars = [all_vars[1], all_vars[3], all_vars[5], all_vars[7], all_vars[9], all_vars[11], all_vars[13]]
	last_weights = [all_vars[14]]
	last_bias = [all_vars[15]]

	with tf.name_scope('cross_entropy'):
    		cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true,logits=y_pred))
    	tf.summary.scalar('cross_entropy', cross_entropy)

    	with tf.name_scope('train'):
    		gradients = tf.gradients(cross_entropy, conv_vars + bias_vars + last_weights + last_bias)
    		conv_vars_gradients = gradients[:len(conv_vars)]
    		bias_vars_gradients = gradients[len(conv_vars):len(conv_vars) + len(bias_vars)]
    		last_weights_gradients = gradients[len(conv_vars) + len(bias_vars):len(conv_vars) + len(bias_vars) + len(last_weights)]
    		last_bias_gradients = gradients[len(conv_vars) + len(bias_vars) + len(last_weights):len(conv_vars) + len(bias_vars) + len(last_weights) + len(last_bias)]
    
	trained_weights_optimizer = tf.train.GradientDescentOptimizer(base_lr)
	trained_biases_optimizer = tf.train.GradientDescentOptimizer(2*base_lr)
	weights_optimizer = tf.train.GradientDescentOptimizer(10*base_lr)
	biases_optimizer = tf.train.GradientDescentOptimizer(20*base_lr)

	train_op1 = trained_weights_optimizer.apply_gradients(zip(conv_vars_gradients, conv_vars))
	train_op2 = trained_biases_optimizer.apply_gradients(zip(bias_vars_gradients, bias_vars))
	train_op3 = weights_optimizer.apply_gradients(zip(last_weights_gradients, last_weights))
	train_op4 = biases_optimizer.apply_gradients(zip(last_bias_gradients, last_bias))

	train = tf.group(train_op1, train_op2, train_op3, train_op4)

	with tf.name_scope('accuracy'):
	    matches = tf.equal(tf.argmax(y_pred, 1), tf.argmax(y_true, 1))
	    acc = tf.reduce_mean(tf.cast(matches, tf.float32))
    
	tf.summary.scalar('accuracy', acc)

	merged_summary = tf.summary.merge_all()
	writer = tf.summary.FileWriter('./summary')
	init = tf.global_variables_initializer()
	saver = tf.train.Saver(max_to_keep=3)

	with tf.Session() as sess:
    	sess.run(init)
    	writer.add_graph(sess.graph)
    	model.load_weights(sess)
    
    	print('Training process started at {}'.format(datetime.now()))

    	for i in range(num_epochs):
        	batches = image_processor.next_batch(128)
        	sess.run(train, feed_dict={x:batches[0], y_true:batches[1], keep_prob:0.5})
        	global_step += 1
        	if (i%500==0):
            	print('On Step {}'.format(i))
            	print('Current base learning rate: {0:.5f}'.format(sess.run(base_lr)))
            	print('At: {}'.format(datetime.now()))
            
            	accuracy = sess.run(acc, feed_dict={x:image_processor.testing_images, y_true:image_processor.testing_labels, keep_prob:1.0})
            	print('Accuracy: {0:.2f}%'.format(accuracy * 100))
            
            	print('Saving model...')
            	saver.save(sess, './models/model_iter.ckpt', global_step=i)
            	print('Model saved at step: {}'.format(i))
            	print('\n')
            
    	print('Saving final model...')
    	saver.save(sess, './models/model_final.ckpt')
    	print('Saved')
    	print('Training finished at {}'.format(datetime.now()))

def indexes_processing(int_list):
    returned_list = []
    for index, element in enumerate(int_list):
        returned_list.append(str(element))
    for index, element in enumerate(returned_list):
        if int(element) < 10:
            returned_list[index] = '0000' + element
        elif int(element) < 100:
            returned_list[index] = '000' + element
        elif int(element) < 1000:
            returned_list[index] = '00' + element
        else:
            returned_list[index] = '0' + element
    return returned_list

if __name__ == '__main__':
	main()
Exemplo n.º 8
0
X, y = train['features'], train['labels']

# TODO: Split data into training and validation sets.
X_train, X_valid, y_train, y_valid = train_test_split(X,
                                                      y,
                                                      test_size=0.2,
                                                      random_state=42)

# TODO: Define placeholders and resize operation.
x = tf.placeholder(tf.float32, (None, 32, 32, 3))
y = tf.placeholder(tf.int32, (None))
x_resized = tf.image.resize_images(x, (227, 227))

# TODO: pass placeholder as first argument to `AlexNet`.
fc7 = AlexNet(x_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)

num_classes = 43
reg = 0.001
fc8_W_shape = (fc7.get_shape().as_list()[-1], num_classes)

# TODO: Add the final layer for traffic sign classification.
fc8_W = tf.get_variable(
    'fc8_W',
    shape=fc8_W_shape,
    initializer=tf.contrib.layers.xavier_initializer(uniform=False),
Exemplo n.º 9
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, scratch_layers)

# Link variable to model output
score = model.fc8

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

# Op for calculating the loss
with tf.name_scope("cross_ent"):
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=score,
                                                                  labels=y))
global_step1 = tf.Variable(0, trainable=False)
global_step2 = tf.Variable(0, trainable=False)
Exemplo n.º 10
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):
    data = pickle.load(f)

X = data['features']
y = data['labels']

# TODO: Split data into training and validation sets.
X_train, X_valid, y_train, y_valid = train_test_split(X,y)

# TODO: Define placeholders and resize operation.
X = tf.placeholder(tf.float32, [None, 32,32,3])
y = tf.placeholder(tf.int32, [None])
one_hot_y = tf.one_hot(y,NB_CLASSES)
resized = tf.image.resize_images(X, [227,227])

# TODO: pass placeholder as first argument to `AlexNet`.
fc7 = AlexNet(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)
fc8_w = tf.Variable(tf.truncated_normal(shape, stddev=1e-2))
fc8_b = tf.Variable(tf.zeros(NB_CLASSES))
logits = tf.matmul(fc7, fc8_w) + fc8_b

# TODO: Define loss, training, accuracy operations.
# HINT: Look back at your traffic signs project solution, you may
# be able to reuse some the code.
Exemplo n.º 12
0
from tensorboardX import SummaryWriter

from alexnet import AlexNet
from utils import cifar10_loader, device

trainloader = cifar10_loader(train=True)
testloader = cifar10_loader(train=False)
writer = SummaryWriter("./logs")

epochs = 2
batch_size = 128
log_batch = 200
train_metrics = []
test_metrics = []

net = AlexNet()
net.to(device)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(net.parameters(), lr=0.001)


def train():
    for epoch in range(epochs):
        running_loss = 0.0
        correct_classified = 0
        total = 0
        start_time = time.time()
        for i, data in enumerate(trainloader):
            inputs, labels = data
            inputs, labels = inputs.to(device), labels.to(device)
            optimizer.zero_grad()
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

#tf.python.control_flow_ops = tf

# placeholders
x = tf.placeholder(tf.float32, (None, 227, 227, 3))

# By keeping `feature_extract` set to `False`
# we indicate to keep the 1000 class final layer
# originally used to train on ImageNet.
probs = AlexNet(x, feature_extract=False)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)

# Read Images
im1 = (imread("poodle.png")[:, :, :3]).astype(np.float32)
im1 = im1 - np.mean(im1)

im2 = (imread("weasel.png")[:, :, :3]).astype(np.float32)
im2 = im2 - np.mean(im2)

# Run Inference
t = time.time()
output = sess.run(probs, feed_dict={x: [im1, im2]})
Exemplo n.º 14
0
def main(_):
    if not FLAGS.train_paths:
        raise ValueError("Must set --train_paths")
    if not FLAGS.val_paths:
        raise ValueError("Must set --val_paths")
    if not FLAGS.test_paths:
        raise ValueError("Must set --test_paths")

    train_paths = FLAGS.train_paths
    val_paths = FLAGS.val_paths
    test_paths = FLAGS.test_paths
    batch_size = FLAGS.batch_size
    num_classes = FLAGS.num_classes
    num_epochs = FLAGS.num_epochs
    emb_dim = FLAGS.embedding_dim
    keep_prob = FLAGS.keep_prob
    exp = FLAGS.exp
    loss_func = FLAGS.loss_func
    learning_rate = FLAGS.learning_rate
    checkpoint_path = FLAGS.checkpoint_path
    filewriter_path = FLAGS.filewriter_path
    display_step = FLAGS.display_step
    max_threads = FLAGS.max_threads

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

    # TF placeholder for graph input and output
    x = tf.placeholder(tf.float32, [batch_size, 6*6*256])
    y = tf.placeholder(tf.float32, [batch_size, num_classes])
    kp = tf.placeholder(tf.float32)

    # Initialize model
    layer_names = ['fc6', 'fc7', 'fc8']
    train_layers = layer_names[-FLAGS.num_train_layers:]
    model = AlexNet(x, kp, num_classes, emb_dim, train_layers)

    # Link variable to model output
    score = model.fc8
    if exp != 1.0:
        sign = tf.sign(score)
        score = tf.multiply(sign, tf.pow(tf.abs(score), exp))

    # 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("loss_func"):
        if loss_func == 'softmax':
            loss = tf.nn.softmax_cross_entropy_with_logits(logits=score,
                                                           labels=y,
                                                           name='softmax_loss')
        elif loss_func == 'logistic':
            loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=y,
                                                           logits=score,
                                                           name='logistic_loss')
        elif loss_func == 'mse':
            loss = tf.losses.mean_squared_error(labels=y,
                                                predictions=score)
        elif loss_func == 'l2hinge':
            loss = tf.losses.hinge_loss(labels=y,
                                        logits=score,
                                        reduction=tf.losses.Reduction.NONE)
            loss = tf.square(loss)
        loss = tf.reduce_mean(loss)

    # 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.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('train_loss', loss)

    # Initialize the datasets
    start_time = time.time()
    tr_data = Dataset(train_paths, batch_size, num_classes, True)
    val_data = Dataset(val_paths, batch_size, num_classes, False)
    te_data = Dataset(test_paths, batch_size, num_classes, False)
    load_time = time.time() - start_time
    log_buff = 'Data loading time: %.2f' % load_time + '\n'

    # Ops for evaluation
    acc_split_weights_all = tr_data.get_acc_split_weights(caffe_class_ids, 0)
    acc_split_weights_all = tf.convert_to_tensor(acc_split_weights_all, tf.float32)

    acc_split_weights_1000 = tr_data.get_acc_split_weights(caffe_class_ids, 1000)
    acc_split_weights_1000 = tf.convert_to_tensor(acc_split_weights_1000, tf.float32)
    with tf.name_scope('accuracy'):
        # ops for top 1 accuracies and their splitting
        top1_score, _ = tf.nn.top_k(score, 1)
        top1_thresolds = tf.reduce_min(top1_score, axis = 1, keepdims = True)
        top1_thresolds_bc = tf.broadcast_to(top1_thresolds, score.shape)
        top1_y = tf.to_float(tf.math.greater_equal(score, top1_thresolds_bc))
        top1_correct_pred = tf.multiply(y, top1_y)
        top1_precision_all = tf.squeeze(tf.reduce_mean(tf.matmul(acc_split_weights_all, \
                                                       tf.transpose(top1_correct_pred)), axis = 1))
        top1_precision_1000 = tf.squeeze(tf.reduce_mean(tf.matmul(acc_split_weights_1000, \
                                                        tf.transpose(top1_correct_pred)), axis = 1))

        # ops for top 5 accuracies and their splitting
        top5_score, _ = tf.nn.top_k(score, 5)
        top5_thresolds = tf.reduce_min(top5_score, axis = 1, keepdims = True)
        top5_thresolds_bc = tf.broadcast_to(top5_thresolds, score.shape)
        top5_y = tf.to_float(tf.math.greater_equal(score, top5_thresolds_bc))
        top5_correct_pred = tf.multiply(y, top5_y)
        top5_precision_all = tf.squeeze(tf.reduce_mean(tf.matmul(acc_split_weights_all, \
                                                       tf.transpose(top5_correct_pred)), axis = 1))/5.0
        top5_precision_1000 = tf.squeeze(tf.reduce_mean(tf.matmul(acc_split_weights_1000, \
                                                        tf.transpose(top5_correct_pred)), axis = 1))/5.0

    # 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()


    # Start Tensorflow session
    sess_conf = tf.ConfigProto(intra_op_parallelism_threads=max_threads, 
                               inter_op_parallelism_threads=max_threads)
    with tf.Session(config=sess_conf) as sess:

        # 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)

        log_buff += "{} Start training...".format(datetime.now())+'\n'
        #print("{} Open Tensorboard at --logdir {}".format(datetime.now(),
        #                                              filewriter_path))


        print(log_buff)
        log_buff = ''

        # Loop over number of epochs
        prev_top5_acc = 0
        counter = 0
        for epoch in range(num_epochs):

            log_buff += "{} Epoch: {}".format(datetime.now(), epoch)+'\n'

            tr_batches_per_epoch = tr_data.data_size // batch_size
            tr_data.reset()
            start_time = time.time()
            tr_data.shuffle()
            shuffle_time = time.time() - start_time
            log_buff += 'Train data shuffling time: %.2f' % shuffle_time + '\n'
            cost = 0.0
            load_time = 0
            train_time = 0
            for step in range(tr_batches_per_epoch):

                # get next batch of data
                start_time = time.time()
                img_batch, label_batch = tr_data.next_batch()
                load_time += time.time() - start_time

                # And run the training op
                start_time = time.time()
                _, lss = sess.run((train_op, loss), feed_dict={x: img_batch,
                                                               y: label_batch,
                                                               kp: keep_prob})
                cost += lss
                train_time += time.time() - start_time

                # 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,
                                                            kp: 1.0})

                    writer.add_summary(s, epoch*tr_batches_per_epoch + step)
                '''

            elapsed_time = load_time + train_time
            cost /= tr_batches_per_epoch
            log_buff += 'Epoch: %d\tCost: %.6f\tElapsed Time: %.2f (%.2f / %.2f)' % \
                    (epoch+1, cost, elapsed_time, load_time, train_time) + '\n'

            # Test the model on the sampled train set
            tr_top1_all = ResultStruct()
            tr_top1_1000 = ResultStruct()
            tr_top5_all = ResultStruct()
            tr_top5_1000 = ResultStruct()
            # Evaluate on a for a smaller number of batches of trainset
            tr_data.reset()
            start_time = time.time()
            num_batches = int(tr_batches_per_epoch/4);
            for _ in range(num_batches):

                img_batch, label_batch = tr_data.next_batch()
                prec1_all, prec1_1000, prec5_all, prec5_1000 = \
                        sess.run((top1_precision_all, top1_precision_1000, \
                                  top5_precision_all, top5_precision_1000), \
                                                 feed_dict={x: img_batch, \
                                                            y: label_batch, \
                                                            kp: 1.0});
                tr_top1_all.add(prec1_all)
                tr_top1_1000.add(prec1_1000)
                tr_top5_all.add(prec5_all)
                tr_top5_1000.add(prec5_1000)
            tr_top1_all.scaler_div(num_batches)
            tr_top1_1000.scaler_div(num_batches)
            tr_top5_all.scaler_div(num_batches)
            tr_top5_1000.scaler_div(num_batches)
            log_buff += 'Epoch: ' + str(epoch+1) + '\tTrain Top 1 All  Acc: ' + str(tr_top1_all) + '\n'
            log_buff += 'Epoch: ' + str(epoch+1) + '\tTrain Top 1 1000 Acc: ' + str(tr_top1_1000) + '\n'
            log_buff += 'Epoch: ' + str(epoch+1) + '\tTrain Top 5 All  Acc: ' + str(tr_top5_all) + '\n'
            log_buff += 'Epoch: ' + str(epoch+1) + '\tTrain Top 5 1000 Acc: ' + str(tr_top5_1000) + '\n'
            tr_pred_time = time.time() - start_time

            # Test the model on the entire validation set
            val_top1_all = ResultStruct()
            val_top1_1000 = ResultStruct()
            val_top5_all = ResultStruct()
            val_top5_1000 = ResultStruct()
            val_data.reset()
            val_batches_per_epoch = val_data.data_size // batch_size
            start_time = time.time()
            for _ in range(val_batches_per_epoch):

                img_batch, label_batch = val_data.next_batch()
                prec1_all, prec1_1000, prec5_all, prec5_1000 = \
                        sess.run((top1_precision_all, top1_precision_1000, \
                                  top5_precision_all, top5_precision_1000), \
                                                 feed_dict={x: img_batch, \
                                                            y: label_batch, \
                                                            kp: 1.0})
                val_top1_all.add(prec1_all)
                val_top1_1000.add(prec1_1000)
                val_top5_all.add(prec5_all)
                val_top5_1000.add(prec5_1000)
            val_top1_all.scaler_div(val_batches_per_epoch)
            val_top1_1000.scaler_div(val_batches_per_epoch)
            val_top5_all.scaler_div(val_batches_per_epoch)
            val_top5_1000.scaler_div(val_batches_per_epoch)
            log_buff += 'Epoch: ' + str(epoch+1) + '\tVal   Top 1 All  Acc: ' + str(val_top1_all) + '\n'
            log_buff += 'Epoch: ' + str(epoch+1) + '\tVal   Top 1 1000 ACC: ' + str(val_top1_1000) + '\n'
            log_buff += 'Epoch: ' + str(epoch+1) + '\tVal   Top 5 All  Acc: ' + str(val_top5_all) + '\n'
            log_buff += 'Epoch: ' + str(epoch+1) + '\tVal   Top 5 1000 Acc: ' + str(val_top5_1000) + '\n'

            val_pred_time = time.time() - start_time

            # Test the model on the entire test set
            te_top1_all = ResultStruct()
            te_top1_1000 = ResultStruct()
            te_top5_all = ResultStruct()
            te_top5_1000 = ResultStruct()
            te_data.reset()
            te_batches_per_epoch = te_data.data_size // batch_size
            start_time = time.time()
            for _ in range(te_batches_per_epoch):

                img_batch, label_batch = te_data.next_batch()
                prec1_all, prec1_1000, prec5_all, prec5_1000 = \
                        sess.run((top1_precision_all, top1_precision_1000, \
                                  top5_precision_all, top5_precision_1000), \
                                                 feed_dict={x: img_batch, \
                                                            y: label_batch, \
                                                            kp: 1.0});
                te_top1_all.add(prec1_all)
                te_top1_1000.add(prec1_1000)
                te_top5_all.add(prec5_all)
                te_top5_1000.add(prec5_1000)
            te_top1_all.scaler_div(te_batches_per_epoch)
            te_top1_1000.scaler_div(te_batches_per_epoch)
            te_top5_all.scaler_div(te_batches_per_epoch)
            te_top5_1000.scaler_div(te_batches_per_epoch)
            log_buff += 'Epoch: ' + str(epoch+1) + '\tTest  Top 1 All  Acc: ' + str(te_top1_all) + '\n'
            log_buff += 'Epoch: ' + str(epoch+1) + '\tTest  Top 1 1000 Acc: ' + str(te_top1_1000) + '\n'
            log_buff += 'Epoch: ' + str(epoch+1) + '\tTest  Top 5 All  Acc: ' + str(te_top5_all) + '\n'
            log_buff += 'Epoch: ' + str(epoch+1) + '\tTest  Top 5 1000 Acc: ' + str(te_top5_1000) + '\n'
            te_pred_time = time.time() - start_time

            elapsed_time = tr_pred_time + val_pred_time + te_pred_time
            log_buff += 'Epoch %d Prediction: \tElapsed Time: %.2f (%.2f / %.2f / %.2f)' \
                    % (epoch+1, elapsed_time, tr_pred_time, val_pred_time, te_pred_time) + '\n'

            if math.isnan(cost):
                break
            cur_top5_acc = val_top5_all.acc
            if cur_top5_acc - prev_top5_acc > 0.003:
                counter = 0
                prev_top5_acc = cur_top5_acc
            elif (cur_top5_acc - prev_top5_acc < -0.05) or (counter == 15):
                break
            else:
                counter += 1

            # save checkpoint of the model
            '''
            print("{} Saving checkpoint of model...".format(datetime.now()))
            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))
            '''
           
            print(log_buff)
            log_buff = ''

        print(log_buff)
Exemplo n.º 15
0
# Initialize the training and validation dataset generators
trainGen = HDF5DatasetGenerator(dbPath=data_config.TRAIN_HDF5,
                                batchSize=128,
                                preprocessor=[pp, mp, iap],
                                aug=None,
                                classes=2)
valGen = HDF5DatasetGenerator(dbPath=data_config.VAL_HDF5,
                              batchSize=128,
                              preprocessor=[sp, mp, iap],
                              classes=2)

# Initialize the optimizer
print("[INFO] compiling model..,")
opt = Adam(lr=1e-3)
model = AlexNet.build(width=227, height=227, depth=3, classes=2, reg=0.0002)
model.compile(loss="binary_crossentropy", optimizer=opt, metrics=["accuracy"])

# Construct the set of callbacks
path = os.path.sep.join(
    [data_config.OUTPUT_PATH, "{}.png".format(os.getpid())])
callbacks = [TrainingMonitor(path)]

# train the network
model.fit_generator(trainGen.generator(),
                    steps_per_epoch=trainGen.numImages // 128,
                    validation_data=valGen.generator(),
                    validation_steps=valGen.numImages // 128,
                    epochs=75,
                    max_queue_size=128 * 2,
                    callbacks=None,
Exemplo n.º 16
0
        # Scaling factor to make the two losses operating in comparable ranges.
        magnitude_loss = 0.0001 * F.mse_loss(target=real_eig_vals,
                                             input=fake_eig_vals)
        structure_loss = -torch.sum(torch.mul(fake_eig_vecs, real_eig_vecs), 0)
        normalized_real_eig_vals = normalize_min_max(real_eig_vals)
        weighted_structure_loss = torch.sum(
            torch.mul(normalized_real_eig_vals, structure_loss))
        return magnitude_loss + weighted_structure_loss

    netG = Generator(ngpu).to(device)
    netG.apply(weights_init)
    if opt.netG != '':
        netG.load_state_dict(torch.load(opt.netG))
    print(netG)

    netC = AlexNet(ngpu).to(device)
    netC.load_state_dict(torch.load('./best_model.pth'))
    print(netC)
    netC.eval()

    netD = Discriminator(ngpu).to(device)
    netD.apply(weights_init)
    if opt.netD != '':
        netD.load_state_dict(torch.load(opt.netD))
    print(netD)

    criterion = nn.BCELoss()
    criterion_sum = nn.BCELoss(reduction='sum')

    fixed_noise = torch.randn(opt.batchSize, 100, 1, 1, device=device)
ready = 0
images = []

diseases = [
    "Tomato___Bacterial_spot"
    "Tomato___Early_blight", "Tomato___Late_blight", "Tomato___Leaf_Mold",
    "Tomato___Septoria_leaf_spot",
    "Tomato___Spider_mites Two-spotted_spider_mite", "Tomato___Target_Spot",
    "Tomato___Tomato_mosaic_virus", "Tomato___Tomato_Yellow_Leaf_Curl_Virus",
    "Tomato___healthy"
]

X = tf.placeholder(tf.float32, [None, 227, 227, 3])
Y = tf.placeholder(tf.float32, [None, 10])
keep_prob = tf.placeholder(tf.float32)
model = AlexNet(X, keep_prob, 10, [])
score = model.fc8
compare = tf.argmax(score, 1)
saver = tf.train.Saver()
f = open("plant.txt", "w+")
coordinate_for_camera = [[20, 15, 25], [20, 15, 20], [20, 15, 15]]
ready = 0
number_of_plants = 4
co = 1

a = -39
b = 42
a = str(a) + 'p'
b = str(b) + 'q'
d = 50
Exemplo n.º 18
0
        tf.reset_default_graph()
        print("Taking off")
        mdrone.takeoff()
        mdrone.get_battery()
        print(mdrone.get_battery())
        frame_read = mdrone.get_frame_read()
        mFrame = frame_read.frame
        image = cv2.imread(mFrame)
        image_ = image - imagenet_mean
        image_view = cv2.resize(mFrame, (width, height))
        #Transformar as dimensões para alimentar a rede
        image_transform = cv2.resize(image_, [227, 227])
        image_rgb = cv2.cvtColor(image_transform, cv2.COLOR_BGR2RGB)
        #ALEXNET
        saver = tf.train.import_meta_graph(metagrap_path)
        model = AlexNet(image_rgb, keep_prob, num_classes=6, skip_layer='', weights_path=saver)
        #Cálculo Softmax
        score = tf.nn.softmax(model.fc8)
        max = tf.arg_max(score, 1)

        with tf.Session() as sess:
            saver.restore(sess, checkpoint_path)
            sess.run(tf.global_variables_initializer())
            sess.run(tf.local_variables_initializer())
            pred = sess.run(score, feed_dict={keep_prob: 1.})
            print("prediction: ", pred)
            prob = sess.run(max, feed_dict={keep_prob: 1.})[0]
            print(prob)
            class_name = class_names[np.argmax(pred)]
            print("Categoria: ", class_name)
Exemplo n.º 19
0
import torchvision.datasets
from torch.utils.data import DataLoader
import torchvision.models as models

from alexnet import AlexNet

random.seed(0)
np.random.seed(0)
torch.manual_seed(0)
torch.cuda.manual_seed(0)
torch.backends.cudnn.deterministic = True

device = 'cuda' if torch.cuda.is_available(
) else 'cpu'  # device = 'cpu' # CPU ONLY

net = AlexNet(num_classes=10)
if torch.cuda.is_available():
    net = net.cuda()

if device == 'cuda':
    # make it concurent
    # net = torch.nn.DataParallel(net)
    cudnn.benchmark = True


def train_dataset(path, shuffle=True):

    transformation = torchvision.transforms.Compose([
        torchvision.transforms.Resize((224, 244)),
        torchvision.transforms.RandomHorizontalFlip(),
        torchvision.transforms.ToTensor()
Exemplo n.º 20
0
"""
Script to have AlexNet "watch" Sherlock episode and get the model's TR-by-TR Representational Similarity Matrix. Individual frames are fed
to the model and model activity of frames within each TR is averaged. 
"""
import numpy as np
import matplotlib.pyplot as plt
import cv2
import tensorflow as tf
from alexnet import AlexNet
import keras.preprocessing.image as kpimage

x = tf.placeholder(tf.float32, [1, 227, 227, 3])
keep_prob = tf.placeholder(tf.float32)

model = AlexNet(x, keep_prob, 1000, [], path_to_weights='bvlc_alexnet.npy')

#Choose layer you want to get the activations for
score = model.fc6

activations_averaged = []

trs = 0
i = 0
images = []
print("Getting frames")
vidcap = cv2.VideoCapture(
    movie_video_path)  #Edit in cfg for the movie video path.
success, image = vidcap.read()
count = 0
success = True
while success:
Exemplo n.º 21
0
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, tf.constant([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]})
    tf.config.experimental.set_memory_growth(gpu, True)


# Give the global constants.Please notify BATCH_SIZE for model.fit() and Batch_Size for 
# model.evaluate() and model.predict(). 
EPOCHS = 50
BATCH_SIZE = 32
Batch_Size = 1
image_width = 227
image_height = 227
channels = 3
num_classes = 6


# Call the alexnet model in alexnet.py. 
model = AlexNet((image_width,image_height,channels), 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()


# The dataset inlcude the three directories 
train_dir = '/home/mike/Documents/Six_Classify_AlexNet/seg_train/seg_train'
test_dir = '/home/mike/Documents/Six_Classify_AlexNet/seg_test/seg_test'
predict_dir = '/home/mike/Documents/Six_Classify_AlexNet/seg_pred/'
Exemplo n.º 23
0
class NearestNeighbor:

    cluster_dict = {}

    def __init__(self):

        # The number of output classes of Alexnet
        num_classes = 1000

        # No train on the layers
        train_layers = []

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

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

        # Start the Tensorflow Session and initialize all variables
        self.sess = tf.Session()
        self.sess.run(tf.global_variables_initializer())

        # Load the pre-trained weights into the non-trainable layer
        self.model.load_initial_weights(self.sess)

        # Declare the feature-vector-dictionary
        self.load_cluster_dict()

        # Init Audio
        self.audio = Audio()

    # Classifies the given picture and returns the label and if it was a new label
    def classify(self, name, speech=False):

        # Get the image
        image_path = "classification/pictures/" + name + ".jpeg"

        im1 = cv2.imread(image_path).astype(np.float32)
        im1 = im1 - np.mean(im1)

        # RGB to BGR
        im1[:, :, 0], im1[:, :, 2] = im1[:, :, 2], im1[:, :, 0]

        features = self.get_features(np.array([im1]))

        best_match = ("", np.inf)

        # Compute all distances to the clusters and search the nearest
        for label in self.cluster_dict.keys():

            cluster = self.cluster_dict[label]

            distance = spatial.distance.euclidean(np.array(features[0]), cluster[0])

            if distance < best_match[1]:
                best_match = (label, distance)

        self.audio.output("\nThe object is a " + best_match[0])

        # check if the estimation was correct or not
        while True:
            correct_estimation = self.audio.input("Is the shown object a '" + best_match[0] + "' ? (yes or no)",
                                                  speech=speech)

            # add the features to the neighbor dict if it was the correct estimation
            if correct_estimation == "y" or correct_estimation == "Y" or correct_estimation == "yes":
                self.calculate_cluster(np.array(features), best_match[0])
                return best_match[0], False

            # ask what the correct label is
            elif correct_estimation == "n" or correct_estimation == "N" or correct_estimation == "no":
                correct_label = self.audio.input("What is the correct label then? (correct spelling required!)",
                                                 speech=speech)

                if correct_label in self.cluster_dict.keys():
                    self.calculate_cluster(np.array(features), correct_label)
                    return correct_label, False

                # ask if it is a new label
                else:
                    new_label = self.audio.input("Is this a complete new label? (yes or no)", speech=speech)

                    if new_label == "y" or new_label == "Y" or new_label == "yes":
                        self.calculate_cluster(np.array(features), correct_label)
                        return correct_label, True

                    else:
                        self.audio.output("Then may check the correct spelling.")

            else:
                self.audio.output("Wrong input, please check again.")

    # Extracts the feature vector of the given picture and saves it in the dict
    def learn(self, label, image_name="tmp_picture"):

        image_path = "classification/pictures/" + image_name + ".jpeg"

        # Get the Image
        image = (cv2.imread(image_path)[:, :, :3]).astype(np.float32)
        image = image - np.mean(image)

        # RGB to BGR
        image[:, :, 0], image[:, :, 2] = image[:, :, 2], image[:, :, 0]

        features = self.get_features(np.array([image]))

        self.calculate_cluster(np.array(features), label)

        self.audio.output("\nCluster of the label is updated. \n")

    # Saves all feature vectors of the pictures of the given folder with the given label
    def batch_learn(self, label, folder_path="classification/pictures/"):

        folder_path = folder_path + label

        # Creates an array with all images as arrays
        images = []
        for image in os.listdir(folder_path):
            if image.endswith(".jpg") or image.endswith(".jpeg"):
                # Get the image
                image_path = os.path.join(folder_path, image)
                image = (cv2.imread(image_path)[:, :, :3]).astype(np.float32)
                image = image - np.mean(image)

                # RGB to BGR
                image[:, :, 0], image[:, :, 2] = image[:, :, 2], image[:, :, 0]
                images.append(image)

        features = self.get_features(np.array(images))

        self.calculate_cluster(np.array(features), label)

        self.audio.output("Finished " + label)

    # Get the feature vectors of all the images in the images array
    def get_features(self, images):

        features = []

        # Link variable to model output
        score = self.model.fc7

        for im in images:

            # Skip image if the shape is not the right one
            if im.shape != (227, 227, 3):
                print("Image has wrong resolution!")
                continue

            # Feed alexnet with the image
            output = self.sess.run(score, feed_dict={self.x: [im], self.keep_prob: 1.})
            features.append(output[0])

        return features

    # Calculates the new mean of the label regarding the feature array
    def calculate_cluster(self, features, label):

        mean_features = np.mean(features, axis=0)

        if label not in self.cluster_dict.keys():
            self.cluster_dict[label] = (mean_features, features.shape[0])
        else:
            cluster = self.cluster_dict[label]
            new_cluster = (cluster[1] * cluster[0] + features.shape[0] * mean_features) / (
                        cluster[1] + features.shape[0])
            self.cluster_dict[label] = (new_cluster, cluster[1]+features.shape[0])

    # Load the neighbor list out of the file
    def load_cluster_dict(self):
        try:
            print "open file"
            self.cluster_dict = dict(pickle.load(open("classification/cluster_list.p", "rb")))
            print "done"
        except IOError:
            print "no file found. start with empty dictionary"

    # Shows all labels of the neighbor dictionary
    def show_labels(self):
        for label in self.cluster_dict.keys():
            print(label + ": " + str(self.cluster_dict[label][1]) + " pictures included")

    # Deletes a label from the neighbor dict and the feature vectors of it
    def delete_label(self, label):
        if label in self.cluster_dict.keys():
            del self.cluster_dict[label]
        else:
            self.audio.output("The label is not included.")

    def clear_cluster_dict(self):
        self.cluster_dict.clear()

    def save_cluster_dict(self):
        pickle.dump(self.cluster_dict, open("classification/cluster_list.p", "wb"))
        print("file saved")

    # Saves the neighbor dictionary and closes the tensorflow session
    def close(self):
        self.save_cluster_dict()
        self.sess.close()
Exemplo n.º 24
0
    batch_size = 30
    # Network params
    dropout_rate = 0.5
    train_layers = ['fc6', 'fc7', 'fc8']

    # 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/finetune_alexnet/tensorboard"
    checkpoint_path = "/tmp/finetune_alexnet/checkpoints"

    x = tf.placeholder(tf.float32, [None, 227, 227, 3], name='input')
    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,
                    weights_path=weights_path)

    for i in range(0, len(DataDir.val_speaker)):
        #for i in range(1):
        train_file = DataDir.train_segments_path[i]
        val_file = DataDir.val_segments_path[i]
        alexnet_file = DataDir.alexnet[i]

        train_session(train_file, val_file, alexnet_file)
Exemplo n.º 25
0
                      if_dropout=args.dropout)
 elif args.model == 'vgg':
     model = VGG16(enable_lat=args.enable_lat,
                   epsilon=args.lat_epsilon,
                   pro_num=args.lat_pronum,
                   batch_size=args.model_batchsize,
                   num_classes=10,
                   if_dropout=args.dropout)
 elif args.model == 'densenet':
     model = DenseNet()
 elif args.model == 'inception':
     model = Inception_v2()
 elif args.model == 'alexnet':
     model = AlexNet(enable_lat=args.enable_lat,
                     epsilon=args.lat_epsilon,
                     pro_num=args.lat_pronum,
                     batch_size=args.model_batchsize,
                     num_classes=200,
                     if_dropout=args.dropout)
 model.cuda()
 model.load_state_dict(torch.load((args.modelpath)))
 print("model load successfully")
 # if cifar then normalize epsilon from [0,255] to [0,1]
 '''
 if args.dataset == 'cifar10':
     eps = args.attack_epsilon / 255.0
 else:
     eps = args.attack_epsilon
 '''
 eps = args.attack_epsilon
 # the last layer of densenet is F.log_softmax, while CrossEntropyLoss have contained Softmax()
 attack = Attack(
Exemplo n.º 26
0
def main_worker(gpu, ngpus_per_node, args):
    global best_acc1
    args.gpu = gpu

    if args.gpu is not None:
        print(f"Use GPU: {args.gpu} for training!")

    if args.distributed:
        if args.dist_url == "env://" and args.rank == -1:
            args.rank = int(os.environ["RANK"])
        if args.multiprocessing_distributed:
            # For multiprocessing distributed training, rank needs to be the
            # global rank among all the processes
            args.rank = args.rank * ngpus_per_node + gpu
        dist.init_process_group(backend=args.dist_backend,
                                init_method=args.dist_url,
                                world_size=args.world_size,
                                rank=args.rank)
    # create model
    if 'alexnet' in args.arch:  # NEW
        if args.pretrained:
            model = AlexNet.from_pretrained(args.arch, args.num_classes)
            print(f"=> using pre-trained model '{args.arch}'")
        else:
            print(f"=> creating model '{args.arch}'")
            model = AlexNet.from_name(args.arch)
    else:
        warnings.warn("Plesase --arch alexnet.")

    if args.distributed:
        # For multiprocessing distributed, DistributedDataParallel constructor
        # should always set the single device scope, otherwise,
        # DistributedDataParallel will use all available devices.
        if args.gpu is not None:
            torch.cuda.set_device(args.gpu)
            model.cuda(args.gpu)
            # When using a single GPU per process and per
            # DistributedDataParallel, we need to divide the batch size
            # ourselves based on the total number of GPUs we have
            args.batch_size = int(args.batch_size / ngpus_per_node)
            args.workers = int(args.workers / ngpus_per_node)
            model = torch.nn.parallel.DistributedDataParallel(
                model, device_ids=[args.gpu])
        else:
            model.cuda()
            # DistributedDataParallel will divide and allocate batch_size to all
            # available GPUs if device_ids are not set
            model = torch.nn.parallel.DistributedDataParallel(model)
    elif args.gpu is not None:
        torch.cuda.set_device(args.gpu)
        model = model.cuda(args.gpu)
    else:
        # DataParallel will divide and allocate batch_size to all available
        # GPUs
        if args.arch.startswith('alexnet'):
            model.features = torch.nn.DataParallel(model.features)
            model.cuda()
        else:
            model = torch.nn.DataParallel(model).cuda()

    # define loss function (criterion) and optimizer
    criterion = nn.CrossEntropyLoss().cuda(args.gpu)

    optimizer = torch.optim.SGD(model.parameters(),
                                args.lr,
                                momentum=args.momentum,
                                weight_decay=args.weight_decay)

    model, optimizer = amp.initialize(model,
                                      optimizer,
                                      opt_level=args.opt_level)

    # optionally resume from a checkpoint
    if args.resume:
        if os.path.isfile(args.resume):
            print(f"=> loading checkpoint '{args.resume}'")
            checkpoint = torch.load(args.resume)
            compress_model(checkpoint, filename=args.resume)
            args.start_epoch = checkpoint['epoch']
            best_acc1 = checkpoint['best_acc1']
            if args.gpu is not None:
                # best_acc1 may be from a checkpoint from a different GPU
                best_acc1 = best_acc1.to(args.gpu)
            model.load_state_dict(checkpoint['state_dict'])
            optimizer.load_state_dict(checkpoint['optimizer'])
            amp.load_state_dict(checkpoint['amp'])
            print(
                f"=> loaded checkpoint '{args.resume}' (epoch {checkpoint['epoch']})"
            )
        else:
            print(f"=> no checkpoint found at '{args.resume}'")

    cudnn.benchmark = True

    # Data loading code
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])

    train_dataset = datasets.CIFAR10(root=args.data,
                                     download=True,
                                     train=True,
                                     transform=transforms.Compose([
                                         transforms.RandomCrop(32, padding=4),
                                         transforms.RandomHorizontalFlip(),
                                         transforms.ToTensor(),
                                         normalize,
                                     ]))

    if args.distributed:
        train_sampler = torch.utils.data.distributed.DistributedSampler(
            train_dataset)
    else:
        train_sampler = None

    train_loader = torch.utils.data.DataLoader(train_dataset,
                                               batch_size=args.batch_size,
                                               shuffle=(train_sampler is None),
                                               num_workers=args.workers,
                                               pin_memory=True,
                                               sampler=train_sampler)

    val_dataset = datasets.CIFAR10(root=args.data,
                                   download=True,
                                   train=False,
                                   transform=transforms.Compose([
                                       transforms.RandomHorizontalFlip(),
                                       transforms.ToTensor(),
                                       normalize,
                                   ]))

    val_loader = torch.utils.data.DataLoader(val_dataset,
                                             batch_size=args.batch_size,
                                             shuffle=False,
                                             num_workers=args.workers,
                                             pin_memory=True)

    if args.evaluate:
        top1 = validate(val_loader, model, criterion, args)
        with open('res.txt', 'w') as f:
            print(f"Acc@1: {top1}", file=f)
        return

    for epoch in range(args.start_epoch, args.epochs):
        if args.distributed:
            train_sampler.set_epoch(epoch)
        adjust_learning_rate(optimizer, epoch, args)

        # train for one epoch
        train(train_loader, model, criterion, optimizer, epoch, args)

        # evaluate on validation set
        acc1 = validate(val_loader, model, criterion, args)

        # remember best acc@1 and save checkpoint
        is_best = acc1 > best_acc1
        best_acc1 = max(acc1, best_acc1)

        if not args.multiprocessing_distributed or (
                args.multiprocessing_distributed
                and args.rank % ngpus_per_node == 0):
            save_checkpoint(
                {
                    'epoch': epoch + 1,
                    'arch': args.arch,
                    'state_dict': model.state_dict(),
                    'best_acc1': best_acc1,
                    'optimizer': optimizer.state_dict(),
                    'amp': amp.state_dict(),
                }, is_best)
    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)
test_init_op = iterator.make_initializer(te_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, 4096, 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
            print("Saving checkpoint at", self.save_path)
            self.model.save_model_vars(self.save_path, self.session)
            print("Checkpoint Saved")
        else:
            print("Not Saving checkpoint due to configuration")

    def _retain_current_best(self):
        print(self.best_type, self.name, "remained {}".format(self.best))


if __name__ == "__main__":
    x = tf.placeholder(tf.float32, [None, 227, 227, 3], name="x")
    keep_prob = tf.placeholder(tf.float32, [], name="keep_prob")
    save_path = "/Users/liushuheng/Desktop/vars"
    name = "xent"
    net = AlexNet(x, keep_prob, 3, ['fc8'])
    checkpointer = Checkpointer(name, net, save_path, higher_is_better=False)
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for epoch in range(20):
            checkpointer.update_session(sess)
            new_value = np.random.rand(1)
            print("\nnew value = {}".format(new_value))
            checkpointer.update_best(new_value,
                                     epoch=epoch,
                                     checkpoint=False,
                                     mem_cache=True)

    print(checkpointer.best)
    mem_caches = checkpointer.list_memory_caches()
    # print(checkpointer.list_memory_caches())
Exemplo n.º 29
0
with open(training_file, mode='rb') as f:
    data = pickle.load(f)

X_0, y_0 = data['features'], data['labels']
X_0 = X_0[:int(4096 // 0.8)]
y_0 = y_0[:int(4096 // 0.8)]
# TODO: Split data into training and validation sets.
X_train, X_valid, y_train, y_valid = train_test_split(X_0, y_0, test_size=0.2)

# TODO: Define placeholders and resize operation.
x = tf.placeholder(tf.float32, (None, 32, 32, 3))
y = tf.placeholder(tf.int32, (None))
resized = tf.image.resize_images(x, (227, 227))

# TODO: pass placeholder as first argument to `AlexNet`.
fc7 = AlexNet(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.add(tf.matmul(fc7, fc8W), fc8b)

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

TESTSET_DIR = "/home/qamaruddin/bosch-360-lenet5-clf/ARIEL/WEB_data"

if model_name == "convnetc":
    model = convnets.ConvNetC(num_classes=10)
elif model_name == "convnetd":
    model = convnets.ConvNetD(num_classes=10)
elif model_name == "convnete":
    model = convnets.ConvNetE(num_classes=2)
elif model_name == "convnetf":
    model = convnets.ConvNetF(num_classes=2)
elif model_name == "capsnet":
    model = CapsuleNet(num_classes=10)
elif model_name == "alexnet":
    model = AlexNet(num_classes=10)

if torch.cuda.is_available():
    model = model.cuda()

model.train(False)
model.load_state_dict(torch.load(model_path, map_location='cpu'))
model.eval()

transform = transforms.Compose([
    transforms.Resize((224, 224)),
    # transforms.RandomCrop((224, 224)),
    transforms.Grayscale(num_output_channels=1),
    # transforms.Lambda(lambda img: apply_gaussian_blur(img)),
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))