def eval_lenet(net_name, ckpt_path, trainable=False, err_mean=None, err_stddev=None, train_vars=None, cost_factor=200., n_epoch=1): netparams = load.load_netparams_tf(ckpt_path, trainable=False) data_spec = networks.get_data_spec(net_name) input_node = tf.placeholder(tf.float32, shape=(None, data_spec.crop_size * data_spec.crop_size * data_spec.channels)) input_node_2d = tf.reshape(input_node, shape=(-1, data_spec.crop_size, data_spec.crop_size, data_spec.channels)) label_node = tf.placeholder(tf.float32, [None, 10]) logits_, err_w, err_b, err_lyr = lenet_noisy(input_node_2d, netparams, err_mean, err_stddev, train_vars) square = [tf.nn.l2_loss(err_w[layer]) for layer in err_w] square_sum = tf.reduce_sum(square) loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits_, labels=label_node)) + cost_factor / (1. + square_sum) optimizer = tf.train.AdamOptimizer(learning_rate=0.001) if trainable: train_op = optimizer.minimize(loss_op) probs = softmax(logits_) correct_pred = tf.equal(tf.argmax(probs, 1), tf.argmax(label_node, 1)) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) mnist = mnist_input.read_data_sets("/tmp/data/", one_hot=True) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) saver = tf.train.Saver() cur_accuracy = 0 for i in range(0, n_epoch): #if cur_accuracy >= NET_ACC[net_name]: #break if trainable: for step in range(0, mnist.train.num_examples/data_spec.batch_size): batch_x, batch_y = mnist.train.next_batch(data_spec.batch_size) sess.run(train_op, feed_dict={input_node: batch_x, label_node: batch_y}) #loss, acc = sess.run([loss_op_1, accuracy], feed_dict={input_node: batch_x, label_node: batch_y}) print('Training finished\n') cur_accuracy = 100 * (sess.run(accuracy, feed_dict={input_node: mnist.test.images[:], label_node: mnist.test.labels[:]})) print('test accuracy:\t' + (str)(cur_accuracy)) return sess.run(err_w), cur_accuracy
def eval_imagenet_q(net_name, param_pickle_path): netparams = load.load_netparams_tf_q(param_pickle_path) data_spec = networks.get_data_spec(net_name) input_node = tf.placeholder(tf.float32, shape=(None, data_spec.crop_size, data_spec.crop_size, data_spec.channels)) label_node = tf.placeholder(tf.int32) if net_name == 'alexnet': logits_ = networks.alexnet(input_node, netparams) elif net_name == 'googlenet': logits_ = networks.googlenet(input_node, netparams) elif net_name == 'nin': logits_ = networks.nin(input_node, netparams) elif net_name == 'resnet18': logits_ = networks.resnet18(input_node, netparams) elif net_name == 'resnet50': logits_ = networks.resnet50(input_node, netparams) elif net_name == 'squeezenet': logits_ = networks.squeezenet(input_node, netparams) elif net_name == 'vgg16net': logits_ = networks.vgg16net_noisy(input_node, netparams) loss_op = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=logits_, labels=label_node)) # probs = softmax(logits_) top_k_op = tf.nn.in_top_k(probs, label_node, 5) optimizer = tf.train.AdamOptimizer(learning_rate=0.001, epsilon=0.1) correct_pred = tf.equal(tf.argmax(probs, 1), tf.argmax(label_node, 1)) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) count = 0 correct = 0 cur_accuracy = 0 saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) image_producer = dataset.ImageNetProducer(val_path=IMAGE_LABLE, data_path=IMAGE_PATH, data_spec=data_spec) total = len(image_producer) coordinator = tf.train.Coordinator() threads = image_producer.start(session=sess, coordinator=coordinator) for (labels, images) in image_producer.batches(sess): correct += np.sum( sess.run(top_k_op, feed_dict={ input_node: images, label_node: labels })) count += len(labels) cur_accuracy = float(correct) * 100 / count #print('{:>6}/{:<6} {:>6.2f}%'.format(count, total, cur_accuracy)) print(cur_accuracy) coordinator.request_stop() coordinator.join(threads, stop_grace_period_secs=2) return cur_accuracy
def eval_imagenet(net_name, param_path, shift_back, trainable=False, err_mean=None, err_stddev=None, train_vars=None, cost_factor=200., n_epoch=1): if '.ckpt' in param_path: netparams = load.load_netparams_tf(param_path, trainable=False) else: netparams = load.load_netparams_tf_q(param_path, trainable=False) #print((len(netparams['biases']))) data_spec = networks.get_data_spec(net_name) input_node = tf.placeholder(tf.float32, shape=(None, data_spec.crop_size, data_spec.crop_size, data_spec.channels)) label_node = tf.placeholder(tf.int32) if net_name == 'alexnet_noisy': logits_, err_w, err_b, err_lyr = networks.alexnet_noisy( input_node, netparams, err_mean, err_stddev, train_vars) elif net_name == 'alexnet': logits_ = networks.alexnet(input_node, netparams) elif net_name == 'alexnet_shift': logits_ = networks.alexnet_shift(input_node, netparams) elif net_name == 'googlenet': logits_, err_w, err_b, err_lyr = networks.googlenet_noisy( input_node, netparams, err_mean, err_stddev, train_vars) elif net_name == 'nin': logits_, err_w, err_b, err_lyr = networks.nin_noisy( input_node, netparams, err_mean, err_stddev, train_vars) elif net_name == 'resnet18': logits_ = networks.resnet18(input_node, netparams) #logits_, err_w, err_b, err_lyr = networks.resnet18_noisy(input_node, netparams, err_mean, err_stddev, train_vars) elif net_name == 'resnet18_shift': logits_ = networks.resnet18_shift(input_node, netparams, shift_back) elif net_name == 'resnet50': logits_, err_w, err_b, err_lyr = networks.resnet50_noisy( input_node, netparams, err_mean, err_stddev, train_vars) elif net_name == 'squeezenet': logits_, err_w, err_b, err_lyr = networks.squeezenet_noisy( input_node, netparams, err_mean, err_stddev, train_vars) elif net_name == 'vgg16net': logits_, err_w, err_b, err_lyr = networks.vgg16net_noisy( input_node, netparams, err_mean, err_stddev, train_vars) #square = [tf.nn.l2_loss(err_w[layer]) for layer in err_w] #square_sum = tf.reduce_sum(square) #loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits_, labels=label_node)) + cost_factor / (1. + square_sum) # ======== calculating the quantization error of a certain layer # here needs PARAM w_q_pickle = '/home/behnam/results/quantized/resnet18/May12_resnet18_10_res2a_branch1_5_bits.pickle' with open(w_q_pickle, 'r') as f: params_quantized = pickle.load(f) # here needs PARAM layer = 'res2a_branch1' params_quantized_layer = tf.get_variable(name='params_quantized_layer', initializer=tf.constant( params_quantized[0][layer]), trainable=False) q_diff = tf.subtract(params_quantized_layer, netparams['weights'][layer]) q_diff_cost = tf.nn.l2_loss(q_diff) loss_op = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits( logits=logits_, labels=label_node)) + cost_factor * q_diff_cost #loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits_, labels=label_node)) probs = softmax(logits_) top_k_op = tf.nn.in_top_k(probs, label_node, 5) optimizer = tf.train.AdamOptimizer(learning_rate=0.001, epsilon=0.1) if trainable: train_op = optimizer.minimize(loss_op) correct_pred = tf.equal(tf.argmax(probs, 1), tf.argmax(label_node, 1)) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) if trainable: count = 0 correct = 0 cur_accuracy = 0 for i in range(0, n_epoch): #if cur_accuracy >= NET_ACC[net_name]: #break #image_producer = dataset.ImageNetProducer(val_path='/home/behnam/ILSVRC2012_img_val_40K/val_40.txt', data_path='/home/behnam/ILSVRC2012_img_val_40K', data_spec=data_spec) path_train = '/home/behnam/ILSVRC2012_img_train' image_producer = dataset.ImageNetProducer( val_path=path_train + '/train_shuf_200k.txt', data_path=path_train, data_spec=data_spec) total = len(image_producer) * n_epoch coordinator = tf.train.Coordinator() threads = image_producer.start(session=sess, coordinator=coordinator) for (labels, images) in image_producer.batches(sess): one_hot_labels = np.zeros((len(labels), 1000)) for k in range(len(labels)): one_hot_labels[k][labels[k]] = 1 sess.run(train_op, feed_dict={ input_node: images, label_node: one_hot_labels }) correct += np.sum( sess.run(top_k_op, feed_dict={ input_node: images, label_node: labels })) count += len(labels) cur_accuracy = float(correct) * 100 / count print('{:>6}/{:<6} {:>6.2f}%'.format( count, total, cur_accuracy)) coordinator.request_stop() coordinator.join(threads, stop_grace_period_secs=2) #return sess.run(err_w), cur_accuracy # "sess.run" returns the netparams as normal value (converts it from tf to normal python variable) return cur_accuracy, sess.run(netparams) else: count = 0 correct = 0 cur_accuracy = 0 path_val = '/home/behnam/ILSVRC2012_img_val_40K' image_producer = dataset.ImageNetProducer(val_path=path_val + '/val_40.txt', data_path=path_val, data_spec=data_spec) #image_producer = dataset.ImageNetProducer(val_path='/home/behnam/ILSVRC2012_img_val_40K/val_40.txt', data_path='/home/behnam/ILSVRC2012_img_val_40K', data_spec=data_spec) total = len(image_producer) coordinator = tf.train.Coordinator() threads = image_producer.start(session=sess, coordinator=coordinator) for (labels, images) in image_producer.batches(sess): one_hot_labels = np.zeros((len(labels), 1000)) for k in range(len(labels)): one_hot_labels[k][labels[k]] = 1 correct += np.sum( sess.run(top_k_op, feed_dict={ input_node: images, label_node: labels })) count += len(labels) cur_accuracy = float(correct) * 100 / count print('{:>6}/{:<6} {:>6.2f}%'.format(count, total, cur_accuracy)) coordinator.request_stop() coordinator.join(threads, stop_grace_period_secs=2) return cur_accuracy, 0