def testEndPoints(self): batch_size = 5 height, width = 224, 224 num_classes = 1000 with self.test_session(): inputs = tf.random_uniform((batch_size, height, width, 3)) _, end_points = vgg.vgg_16(inputs, num_classes) expected_names = ['vgg_16/conv1/conv1_1', 'vgg_16/conv1/conv1_2', 'vgg_16/pool1', 'vgg_16/conv2/conv2_1', 'vgg_16/conv2/conv2_2', 'vgg_16/pool2', 'vgg_16/conv3/conv3_1', 'vgg_16/conv3/conv3_2', 'vgg_16/conv3/conv3_3', 'vgg_16/pool3', 'vgg_16/conv4/conv4_1', 'vgg_16/conv4/conv4_2', 'vgg_16/conv4/conv4_3', 'vgg_16/pool4', 'vgg_16/conv5/conv5_1', 'vgg_16/conv5/conv5_2', 'vgg_16/conv5/conv5_3', 'vgg_16/pool5', 'vgg_16/fc6', 'vgg_16/fc7', 'vgg_16/fc8' ] self.assertSetEqual(set(end_points.keys()), set(expected_names))
def Setup(self): image_pre = vgg_preprocessing.preprocess_image(self.image, self.image_size, self.image_size, is_training=False) self.image_4d = tf.expand_dims(image_pre, 0) # net forward with slim.arg_scope(vgg.vgg_arg_scope()): # 1000 classes instead of 1001. _, _ = vgg.vgg_16(self.image_4d, num_classes=1000, is_training=False) self.log("Model loading...") self.init_fn = slim.assign_from_checkpoint_fn( VGG16_CKPT, slim.get_model_variables('vgg_16')) # net output self.fc7 = tf.get_default_graph().get_tensor_by_name( "vgg_16/fc7/Relu:0") self.sess = tf.Session() # variables need to be initialized before any sess.run() calls self.init_fn(self.sess) self.log("Restored model")
def eval(x, num_classes=110): with slim.arg_scope(inception.inception_v1_arg_scope()): logits_inc_v1, end_points_inc_v1 = inception.inception_v1( x, num_classes=num_classes, is_training=False, scope='InceptionV1') pred1 = tf.argmax(end_points_inc_v1['Predictions'], 1) # rescale pixle range from [-1, 1] to [0, 255] for resnet_v1 and vgg's input image = (((x + 1.0) * 0.5) * 255.0) processed_imgs_res_v1_50 = preprocess_for_model(image, 'resnet_v1_50') with slim.arg_scope(resnet_v1.resnet_arg_scope()): logits_res_v1_50, end_points_res_v1_50 = resnet_v1.resnet_v1_50( processed_imgs_res_v1_50, num_classes=num_classes, is_training=False, scope='resnet_v1_50') end_points_res_v1_50['logits'] = tf.squeeze( end_points_res_v1_50['resnet_v1_50/logits'], [1, 2]) end_points_res_v1_50['probs'] = tf.nn.softmax( end_points_res_v1_50['logits']) pred2 = tf.argmax(end_points_res_v1_50['probs'], 1) # image = (((x + 1.0) * 0.5) * 255.0)#.astype(np.uint8) processed_imgs_vgg_16 = preprocess_for_model(image, 'vgg_16') with slim.arg_scope(vgg.vgg_arg_scope()): logits_vgg_16, end_points_vgg_16 = vgg.vgg_16(processed_imgs_vgg_16, num_classes=num_classes, is_training=False, scope='vgg_16') end_points_vgg_16['logits'] = end_points_vgg_16['vgg_16/fc8'] end_points_vgg_16['probs'] = tf.nn.softmax(end_points_vgg_16['logits']) pred3 = tf.argmax(end_points_vgg_16['probs'], 1) return [pred1, pred2, pred3]
def testEndPoints(self): batch_size = 5 height, width = 224, 224 num_classes = 1000 for is_training in [True, False]: with tf.Graph().as_default(): inputs = tf.random_uniform((batch_size, height, width, 3)) _, end_points = vgg.vgg_16(inputs, num_classes, is_training=is_training) expected_names = ['vgg_16/conv1/conv1_1', 'vgg_16/conv1/conv1_2', 'vgg_16/pool1', 'vgg_16/conv2/conv2_1', 'vgg_16/conv2/conv2_2', 'vgg_16/pool2', 'vgg_16/conv3/conv3_1', 'vgg_16/conv3/conv3_2', 'vgg_16/conv3/conv3_3', 'vgg_16/pool3', 'vgg_16/conv4/conv4_1', 'vgg_16/conv4/conv4_2', 'vgg_16/conv4/conv4_3', 'vgg_16/pool4', 'vgg_16/conv5/conv5_1', 'vgg_16/conv5/conv5_2', 'vgg_16/conv5/conv5_3', 'vgg_16/pool5', 'vgg_16/fc6', 'vgg_16/fc7', 'vgg_16/fc8' ] self.assertSetEqual(set(end_points.keys()), set(expected_names))
def __init__(self): self.num_classes = 200 IMAGE_SIZE = 224 train_log_dir = 'cnn_model/vgg_16_2016_08_28/slim_fine_tune' feature_vecs = np.load('cnn_model/vgg_feature_vecs.npz') # 将训练集作为 检索库 self.feature_vecs_database = self.normalize(feature_vecs['train']) assert (tf.gfile.Exists(train_log_dir) == True) self.image_holder = tf.placeholder(tf.float32, [None, IMAGE_SIZE, IMAGE_SIZE, 3]) self.is_training = tf.placeholder(dtype=tf.bool) # 创建vgg16网络 如果想冻结所有层,可以指定slim.conv2d中的 trainable=False _, self.end_points = vgg.vgg_16(self.image_holder, is_training=self.is_training, num_classes=self.num_classes) # 用于保存检查点文件 save = tf.train.Saver() # 恢复模型 self.sess = tf.Session() self.sess.run(tf.global_variables_initializer()) # 检查最近的检查点文件 ckpt = tf.train.latest_checkpoint(train_log_dir) if ckpt != None: save.restore(self.sess, ckpt) print('加载上次训练保存后的模型!') else: assert (False)
def graph(x, y, i, x_max, x_min, grad, vgg_y): eps = FLAGS.max_epsilon num_iter = FLAGS.num_iter alpha = eps / num_iter momentum = FLAGS.momentum one_hot_vgg = tf.one_hot(vgg_y, 1000) x_div = input_diversity(x) with slim.arg_scope(vgg.vgg_arg_scope()): logits, end_points = vgg.vgg_16(x_div, num_classes=1001, is_training=False) cross_entropy = tf.losses.softmax_cross_entropy(one_hot_vgg, logits) noise = tf.gradients(cross_entropy, x)[0] noise = noise / tf.reduce_mean(tf.abs(noise), [1, 2, 3], keep_dims=True) noise = momentum * grad + noise x = x + alpha * tf.sign(noise) x = tf.clip_by_value(x, x_min, x_max) i = tf.add(i, 1) return x, y, i, x_max, x_min, noise, vgg_y
def build_model(inputs, style, learning_rate, content_loss_weight, style_loss_weight): with slim.arg_scope(transfer_arg_scope()): trans, var = transfer_net(inputs - MEAN_VALUES, reuse=False) inputs = tf.concat([trans, inputs, style], axis=0) _, end_points = vgg.vgg_16(inputs - MEAN_VALUES, spatial_squeeze=False) f1 = end_points["vgg_16/conv1/conv1_2"] f2 = end_points["vgg_16/conv2/conv2_2"] f3 = end_points["vgg_16/conv3/conv3_3"] f4 = end_points["vgg_16/conv4/conv4_3"] trans_f3, inputs_f3, _ = tf.split(f3, 3, 0) content_loss = tf.nn.l2_loss(trans_f3 - inputs_f3) / tf.to_float( tf.size(trans_f3)) content_loss = content_loss_weight * content_loss style_loss = style_loss_weight * styleloss(f1, f2, f3, f4) total_loss = content_loss + style_loss optimizer = tf.train.AdamOptimizer(learning_rate).minimize(total_loss, var_list=var) return optimizer, trans, total_loss, content_loss, style_loss
def non_target_graph(x, y, i, x_max, x_min, grad): eps = 2.0 * max_epsilon / 255.0 alpha = eps / num_iter num_classes = 110 with slim.arg_scope(inception.inception_v1_arg_scope()): logits_inc_v1, end_points_inc_v1 = inception.inception_v1( x, num_classes=num_classes, is_training=False, scope='InceptionV1') # rescale pixle range from [-1, 1] to [0, 255] for resnet_v1 and vgg's input image = (((x + 1.0) * 0.5) * 255.0) processed_imgs_res_v1_50 = preprocess_for_model(image, 'resnet_v1_50') with slim.arg_scope(resnet_v1.resnet_arg_scope()): logits_res_v1_50, end_points_res_v1_50 = resnet_v1.resnet_v1_50( processed_imgs_res_v1_50, num_classes=num_classes, is_training=False, scope='resnet_v1_50') end_points_res_v1_50['logits'] = tf.squeeze( end_points_res_v1_50['resnet_v1_50/logits'], [1, 2]) end_points_res_v1_50['probs'] = tf.nn.softmax( end_points_res_v1_50['logits']) # image = (((x + 1.0) * 0.5) * 255.0)#.astype(np.uint8) processed_imgs_vgg_16 = preprocess_for_model(image, 'vgg_16') with slim.arg_scope(vgg.vgg_arg_scope()): logits_vgg_16, end_points_vgg_16 = vgg.vgg_16(processed_imgs_vgg_16, num_classes=num_classes, is_training=False, scope='vgg_16') end_points_vgg_16['logits'] = end_points_vgg_16['vgg_16/fc8'] end_points_vgg_16['probs'] = tf.nn.softmax(end_points_vgg_16['logits']) ######################## # Using model predictions as ground truth to avoid label leaking pred = tf.argmax( end_points_inc_v1['Predictions'] + end_points_res_v1_50['probs'] + end_points_vgg_16['probs'], 1) first_round = tf.cast(tf.equal(i, 0), tf.int64) y = first_round * pred + (1 - first_round) * y one_hot = tf.one_hot(y, num_classes) ######################## logits = (end_points_inc_v1['Logits'] + end_points_res_v1_50['logits'] + end_points_vgg_16['logits']) / 3.0 cross_entropy = tf.losses.softmax_cross_entropy(one_hot, logits, label_smoothing=0.0, weights=1.0) noise = tf.gradients(cross_entropy, x)[0] noise = noise / tf.reduce_mean(tf.abs(noise), [1, 2, 3], keep_dims=True) noise = momentum * grad + noise x = x + alpha * tf.sign(noise) x = tf.clip_by_value(x, x_min, x_max) i = tf.add(i, 1) return x, y, i, x_max, x_min, noise
def target_graph(x, y, i, x_max, x_min, grad): eps = 2.0 * max_epsilon / 255.0 alpha = eps / num_iter num_classes = 110 #input image size[224,224,3] images3 = tf.image.resize_bilinear(input_diversity(x), [224, 224], align_corners=False) with slim.arg_scope(inception.inception_v1_arg_scope()): logits_inc_v1, end_points_inc_v1 = inception.inception_v1( images3, num_classes=num_classes, is_training=False, scope='InceptionV1') # rescale pixle range from [-1, 1] to [0, 255] for resnet_v1 and vgg's input image1 = (((input_diversity(x) + 1.0) * 0.5) * 255.0) processed_imgs_res_v1_50 = preprocess_for_model(image1, 'resnet_v1_50') with slim.arg_scope(resnet_v1.resnet_arg_scope()): logits_res_v1_50, end_points_res_v1_50 = resnet_v1.resnet_v1_50( processed_imgs_res_v1_50, num_classes=num_classes, is_training=False, scope='resnet_v1_50') end_points_res_v1_50['logits'] = tf.squeeze(end_points_res_v1_50['resnet_v1_50/logits'], [1, 2]) end_points_res_v1_50['probs'] = tf.nn.softmax(end_points_res_v1_50['logits']) # image = (((x + 1.0) * 0.5) * 255.0)#.astype(np.uint8) image2 = (((input_diversity(x) + 1.0) * 0.5) * 255.0) processed_imgs_vgg_16 = preprocess_for_model(image2, 'vgg_16') with slim.arg_scope(vgg.vgg_arg_scope()): logits_vgg_16, end_points_vgg_16 = vgg.vgg_16( processed_imgs_vgg_16, num_classes=num_classes, is_training=False, scope='vgg_16') end_points_vgg_16['logits'] = end_points_vgg_16['vgg_16/fc8'] end_points_vgg_16['probs'] = tf.nn.softmax(end_points_vgg_16['logits']) one_hot = tf.one_hot(y, num_classes) logits = (end_points_inc_v1['Logits'] + end_points_res_v1_50['logits'] + end_points_vgg_16['logits']) / 3.0 cross_entropy = tf.losses.softmax_cross_entropy(one_hot, logits, label_smoothing=0.0, weights=1.0) noise = tf.gradients(cross_entropy, x)[0] noise = tf.nn.depthwise_conv2d(noise, stack_kernel, strides=[1, 1, 1, 1], padding='SAME') noise = noise / tf.reshape(tf.contrib.keras.backend.std(tf.reshape(noise, [batch_size, -1]), axis=1), [batch_size, 1, 1, 1]) noise = momentum * grad + noise noise = noise / tf.reshape(tf.contrib.keras.backend.std(tf.reshape(noise, [batch_size, -1]), axis=1), [batch_size, 1, 1, 1]) noise1 = tf.image.resize_images(noise, [140, 140], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR) print("noise shape:", noise.shape) noise1 = alpha * tf.clip_by_value(tf.round(noise1), -2, 2) noise_paded = tf.pad(noise1,[[0, 0], [42, 42], [42, 42], [0, 0]], constant_values=0.) x = x - noise_paded x = tf.clip_by_value(x, x_min, x_max) print("x.shape:", x.shape) i = tf.add(i, 1) return x, y, i, x_max, x_min, noise
def testForward(self): batch_size = 1 height, width = 224, 224 with self.test_session() as sess: inputs = tf.random_uniform((batch_size, height, width, 3)) logits, _ = vgg.vgg_16(inputs) sess.run(tf.initialize_all_variables()) output = sess.run(logits) self.assertTrue(output.any())
def target_graph(x, y, i, x_max, x_min, grad): eps = 2.0 * max_epsilon / 255.0 alpha = eps / num_iter num_classes = 110 with slim.arg_scope(inception.inception_v1_arg_scope()): logits_inc_v1, end_points_inc_v1 = inception.inception_v1( x, num_classes=num_classes, is_training=False, scope='InceptionV1') # rescale pixle range from [-1, 1] to [0, 255] for resnet_v1 and vgg's input image = (((x + 1.0) * 0.5) * 255.0) processed_imgs_res_v1_50 = preprocess_for_model(image, 'resnet_v1_50') with slim.arg_scope(resnet_v1.resnet_arg_scope()): logits_res_v1_50, end_points_res_v1_50 = resnet_v1.resnet_v1_50( processed_imgs_res_v1_50, num_classes=num_classes, is_training=False, scope='resnet_v1_50') end_points_res_v1_50['logits'] = tf.squeeze( end_points_res_v1_50['resnet_v1_50/logits'], [1, 2]) end_points_res_v1_50['probs'] = tf.nn.softmax( end_points_res_v1_50['logits']) # image = (((x + 1.0) * 0.5) * 255.0)#.astype(np.uint8) processed_imgs_vgg_16 = preprocess_for_model(image, 'vgg_16') with slim.arg_scope(vgg.vgg_arg_scope()): logits_vgg_16, end_points_vgg_16 = vgg.vgg_16(processed_imgs_vgg_16, num_classes=num_classes, is_training=False, scope='vgg_16') end_points_vgg_16['logits'] = end_points_vgg_16['vgg_16/fc8'] end_points_vgg_16['probs'] = tf.nn.softmax(end_points_vgg_16['logits']) ######################## one_hot = tf.one_hot(y, num_classes) ######################## logits = (end_points_inc_v1['Logits'] + end_points_res_v1_50['logits'] + end_points_vgg_16['logits']) / 3.0 cross_entropy = tf.losses.softmax_cross_entropy(one_hot, logits, label_smoothing=0.0, weights=1.0) noise = tf.gradients(cross_entropy, x)[0] noise = noise / tf.reshape( tf.contrib.keras.backend.std(tf.reshape(noise, [batch_size, -1]), axis=1), [batch_size, 1, 1, 1]) noise = momentum * grad + noise noise = noise / tf.reshape( tf.contrib.keras.backend.std(tf.reshape(noise, [batch_size, -1]), axis=1), [batch_size, 1, 1, 1]) x = x - alpha * tf.clip_by_value(tf.round(noise), -2, 2) x = tf.clip_by_value(x, x_min, x_max) i = tf.add(i, 1) return x, y, i, x_max, x_min, noise
def test_imagenet_vgg16(self): tf.get_logger().setLevel('ERROR') session = tf.compat.v1.InteractiveSession(graph=tf.Graph()) input = tf.compat.v1.placeholder(tf.float32, shape=(None, 224, 224, 3)) with warnings.catch_warnings(): warnings.simplefilter('ignore', category=DeprecationWarning) logits, _ = vgg.vgg_16(input, is_training=False) restorer = tf.compat.v1.train.Saver() restorer.restore( session, utils.python_file_dir(__file__) + '/models/tensorflow_vgg_16/vgg_16.ckpt') mean = (123.68, 116.78, 103.94) std = (1, 1, 1) data_preprocess = self.ImageNetValData(224, 224, 'vgg16', transform=lambda x: (x - mean) / std, label_offset=0) data_original = self.ImageNetValData(224, 224, 'vgg16', transform=None, label_offset=0) bounds = (0, 255) measure_model = TensorFlowModel(session, logits, input) accuracy = Accuracy() measure_model.predict(data_preprocess.x, data_preprocess.y, [accuracy.update, accuracy.report]) neuron_coverage = NeuronCoverage() measure_model.intermediate_layer_outputs( data_preprocess.x, [neuron_coverage.update, neuron_coverage.report]) robustness = Robustness(bounds) measure_model.adversarial_samples( data_original.x, data_original.y, 3, bounds, [ robustness.update, robustness.report, utils.draw_adversarial_samples ], batch_size=1, preprocessing=(mean, std)) session.close() self.assertAlmostEqual(accuracy.get(1), 0.600000) self.assertAlmostEqual(accuracy.get(5), 0.925000) self.assertAlmostEqual(neuron_coverage.get(0.3), 0.630143, places=2) self.assertAlmostEqual(robustness.success_rate, 1.000000)
def testBuild(self): batch_size = 5 height, width = 224, 224 num_classes = 1000 with self.test_session(): inputs = tf.random_uniform((batch_size, height, width, 3)) logits, _ = vgg.vgg_16(inputs, num_classes) self.assertEquals(logits.op.name, 'vgg_16/fc8/squeezed') self.assertListEqual(logits.get_shape().as_list(), [batch_size, num_classes])
def build_single_vggnet(train_tfdata, is_train, dropout_keep_prob): if not FLAGS.is_train or FLAGS.debug_test: is_train = False with slim.arg_scope(vgg.vgg_arg_scope()): identity, end_points = vgg.vgg_16(train_tfdata, num_classes=FLAGS.num_class, is_training=is_train, dropout_keep_prob = dropout_keep_prob) # identity, end_points = vgg.vgg_19(train_tfdata, num_classes=FLAGS.num_class, is_training=is_train, dropout_keep_prob = dropout_keep_prob) for key in end_points.keys(): if 'fc7' in key: feature = tf.squeeze(end_points[key], [1, 2]) return identity, feature
def testModelVariables(self): batch_size = 5 height, width = 224, 224 num_classes = 1000 with self.test_session(): inputs = tf.random_uniform((batch_size, height, width, 3)) vgg.vgg_16(inputs, num_classes) expected_names = [ 'vgg_16/conv1/conv1_1/weights', 'vgg_16/conv1/conv1_1/biases', 'vgg_16/conv1/conv1_2/weights', 'vgg_16/conv1/conv1_2/biases', 'vgg_16/conv2/conv2_1/weights', 'vgg_16/conv2/conv2_1/biases', 'vgg_16/conv2/conv2_2/weights', 'vgg_16/conv2/conv2_2/biases', 'vgg_16/conv3/conv3_1/weights', 'vgg_16/conv3/conv3_1/biases', 'vgg_16/conv3/conv3_2/weights', 'vgg_16/conv3/conv3_2/biases', 'vgg_16/conv3/conv3_3/weights', 'vgg_16/conv3/conv3_3/biases', 'vgg_16/conv4/conv4_1/weights', 'vgg_16/conv4/conv4_1/biases', 'vgg_16/conv4/conv4_2/weights', 'vgg_16/conv4/conv4_2/biases', 'vgg_16/conv4/conv4_3/weights', 'vgg_16/conv4/conv4_3/biases', 'vgg_16/conv5/conv5_1/weights', 'vgg_16/conv5/conv5_1/biases', 'vgg_16/conv5/conv5_2/weights', 'vgg_16/conv5/conv5_2/biases', 'vgg_16/conv5/conv5_3/weights', 'vgg_16/conv5/conv5_3/biases', 'vgg_16/fc6/weights', 'vgg_16/fc6/biases', 'vgg_16/fc7/weights', 'vgg_16/fc7/biases', 'vgg_16/fc8/weights', 'vgg_16/fc8/biases', ] model_variables = [v.op.name for v in slim.get_model_variables()] self.assertSetEqual(set(model_variables), set(expected_names))
def testFullyConvolutional(self): batch_size = 1 height, width = 256, 256 num_classes = 1000 with self.test_session(): inputs = tf.random_uniform((batch_size, height, width, 3)) logits, _ = vgg.vgg_16(inputs, num_classes, spatial_squeeze=False) self.assertEquals(logits.op.name, 'vgg_16/fc8/BiasAdd') self.assertListEqual(logits.get_shape().as_list(), [batch_size, 2, 2, num_classes])
def testModelVariables(self): batch_size = 5 height, width = 224, 224 num_classes = 1000 with self.test_session(): inputs = tf.random_uniform((batch_size, height, width, 3)) vgg.vgg_16(inputs, num_classes) expected_names = ['vgg_16/conv1/conv1_1/weights', 'vgg_16/conv1/conv1_1/biases', 'vgg_16/conv1/conv1_2/weights', 'vgg_16/conv1/conv1_2/biases', 'vgg_16/conv2/conv2_1/weights', 'vgg_16/conv2/conv2_1/biases', 'vgg_16/conv2/conv2_2/weights', 'vgg_16/conv2/conv2_2/biases', 'vgg_16/conv3/conv3_1/weights', 'vgg_16/conv3/conv3_1/biases', 'vgg_16/conv3/conv3_2/weights', 'vgg_16/conv3/conv3_2/biases', 'vgg_16/conv3/conv3_3/weights', 'vgg_16/conv3/conv3_3/biases', 'vgg_16/conv4/conv4_1/weights', 'vgg_16/conv4/conv4_1/biases', 'vgg_16/conv4/conv4_2/weights', 'vgg_16/conv4/conv4_2/biases', 'vgg_16/conv4/conv4_3/weights', 'vgg_16/conv4/conv4_3/biases', 'vgg_16/conv5/conv5_1/weights', 'vgg_16/conv5/conv5_1/biases', 'vgg_16/conv5/conv5_2/weights', 'vgg_16/conv5/conv5_2/biases', 'vgg_16/conv5/conv5_3/weights', 'vgg_16/conv5/conv5_3/biases', 'vgg_16/fc6/weights', 'vgg_16/fc6/biases', 'vgg_16/fc7/weights', 'vgg_16/fc7/biases', 'vgg_16/fc8/weights', 'vgg_16/fc8/biases', ] model_variables = [v.op.name for v in slim.get_model_variables()] self.assertSetEqual(set(model_variables), set(expected_names))
def testEvaluation(self): batch_size = 2 height, width = 224, 224 num_classes = 1000 with self.test_session(): eval_inputs = tf.random_uniform((batch_size, height, width, 3)) logits, _ = vgg.vgg_16(eval_inputs, is_training=False) self.assertListEqual(logits.get_shape().as_list(), [batch_size, num_classes]) predictions = tf.argmax(logits, 1) self.assertListEqual(predictions.get_shape().as_list(), [batch_size])
def test_batch(): with tf.Graph().as_default(): tra_image_batch, tra_label_batch = get_batch(img_batch_list, lab_batch_list) logits, _ = vgg.vgg_16(tra_image_batch, num_classes=5, is_training=False) # logits, _ = vgg.vgg_16(tra_image_batch, num_classes=5, is_training=False) loss = slim.losses.softmax_cross_entropy(logits, tra_label_batch) accuracy = tools.accuracy(logits, tra_label_batch) saver = tf.train.Saver() with tf.Session() as sess: print("Reading checkpoints...") ckpt = tf.train.get_checkpoint_state(model_dir) if ckpt and ckpt.model_checkpoint_path: global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] saver.restore(sess, ckpt.model_checkpoint_path) print('Loading success, global_step is %s' % global_step) else: print('No checkpoint file found') return coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: print('\nEvaluating......') num_step = int(math.floor(408 / 32)) num_sample = num_step * 32 step = 0 total_correct = 0 total_loss = 0 while step < num_step and not coord.should_stop(): batch_accuracy = sess.run(accuracy) batch_loss = sess.run(loss) total_correct += np.sum(batch_accuracy) total_loss += np.sum(batch_loss) step += 1 print(batch_accuracy) print(batch_loss) print('Total testing samples: %d' % num_sample) print('Average accuracy: %.2f%%' % ( total_correct / step)) print('Average loss: %2.f' % (total_loss / step)) except Exception as e: coord.request_stop(e) finally: coord.request_stop() coord.join(threads)
def __init__(self, inputs, true_labels, is_train=False, num_classes=None): self.true_labels = true_labels self.NUM_CLASSES = num_classes with slim.arg_scope(vgg.vgg_arg_scope()): self.output, self.features = vgg.vgg_16(inputs=inputs, num_classes=1000, is_training=False) self.classifier, _ = cnn.fc(input=self.extract_features('fc7'), num_outputs=self.NUM_CLASSES, use_relu=False, name='classifier')
def forward(self, input_tensor, is_training): dropout_value = 0.5 # input_tensor = tf.image.resize_images(input_tensor, [224, 224]) batch_size = tf.shape(input_tensor)[0] print("Is training:", is_training) with slim.arg_scope(vgg.vgg_arg_scope()): h, end_points = vgg.vgg_16(input_tensor, is_training=is_training) print(end_points) print(list(end_points.keys())) h = end_points['vgg_16/pool4'] h = L.convolution2d_transpose(h, 256, [5, 5], [2, 2], activation_fn=None) h = tf.nn.relu(h) h = tf.concat([h, end_points['vgg_16/pool3']], axis=3) h = L.dropout(h, keep_prob=dropout_value, is_training=is_training) np_seed_mask = np.zeros((1, 56, 56, 1)) np_seed_mask[:, 28:29, 28:29, :] = 1.0 seed_mask = tf.constant(np_seed_mask, dtype=tf.float32) seed_mask = tf.tile(seed_mask, [batch_size, 1, 1, 1]) h = L.convolution2d_transpose(h, 128, [5, 5], [2, 2], activation_fn=None) h = tf.nn.relu(h) h = tf.concat([h, end_points['vgg_16/pool2'], seed_mask], axis=3) h = L.dropout(h, keep_prob=dropout_value, is_training=is_training) h = L.convolution2d_transpose(h, 64, [5, 5], [2, 2], activation_fn=None) h = tf.nn.relu(h) h = tf.concat([h, end_points['vgg_16/pool1']], axis=3) h = L.dropout(h, keep_prob=dropout_value, is_training=is_training) h = L.convolution2d_transpose(h, 64, [5, 5], [2, 2], activation_fn=None) h = tf.concat([h, input_tensor], axis=3) h = tf.nn.relu(h) h = L.dropout(h, keep_prob=dropout_value, is_training=is_training) # h = L.convolution2d_transpose(h, 64, [5, 5], [2, 2], activation_fn=None) # h = tf.nn.relu(h) # h = L.dropout(h, keep_prob=dropout_value, is_training=is_training) # h = L.convolution2d_transpose(h, 64, [5, 5], [2, 2], activation_fn=None) # h = tf.nn.relu(h) # h = L.dropout(h, keep_prob=dropout_value, is_training=is_training) h = L.convolution2d(h, len(self.classes) + 1, [1, 1], [1, 1], activation_fn=None) return h
def build_single_vggnet(train_tfdata, is_train, dropout_keep_prob): if not FLAGS.is_train or FLAGS.debug_test: is_train = False with slim.arg_scope(vgg.vgg_arg_scope()): identity, end_points = vgg.vgg_16(train_tfdata, num_classes=FLAGS.num_class, is_training=is_train, dropout_keep_prob=dropout_keep_prob) # identity, end_points = vgg.vgg_19(train_tfdata, num_classes=FLAGS.num_class, is_training=is_train, dropout_keep_prob = dropout_keep_prob) for key in end_points.keys(): if 'fc7' in key: feature = tf.squeeze(end_points[key], [1, 2]) return identity, feature
def testTrainEvalWithReuse(self): train_batch_size = 2 eval_batch_size = 1 train_height, train_width = 224, 224 eval_height, eval_width = 256, 256 num_classes = 1000 with self.test_session(): train_inputs = tf.random_uniform( (train_batch_size, train_height, train_width, 3)) logits, _ = vgg.vgg_16(train_inputs) self.assertListEqual(logits.get_shape().as_list(), [train_batch_size, num_classes]) tf.get_variable_scope().reuse_variables() eval_inputs = tf.random_uniform( (eval_batch_size, eval_height, eval_width, 3)) logits, _ = vgg.vgg_16(eval_inputs, is_training=False, spatial_squeeze=False) self.assertListEqual(logits.get_shape().as_list(), [eval_batch_size, 2, 2, num_classes]) logits = tf.reduce_mean(logits, [1, 2]) predictions = tf.argmax(logits, 1) self.assertEquals(predictions.get_shape().as_list(), [eval_batch_size])
def __call__(self, x_input, return_logits=False): """Constructs model and return probabilities for given input.""" reuse = True if self.built else None with slim.arg_scope(vgg.vgg_arg_scope()): _, end_points = vgg.vgg_16( x_input, num_classes=self.nb_classes, is_training=False) self.built = True self.logits = tf.squeeze(end_points['vgg_16/fc8']) # Strip off the extra reshape op at the output self.probs = tf.nn.softmax(end_points['vgg_16/fc8']) if return_logits: return self.logits else: return self.probs
def __call__(self, ens_x_input, vgg_x_input, inc_x_input, tcd_x_input): """Constructs model and return probabilities for given input.""" reuse = True if self.built else None logits = None aux_logits = None weights = [[0.7, 0.1], [0.2, 0.1]] all_inputs = [[ens_x_input, tcd_x_input], [inc_x_input, tcd_x_input]] scopes = [ inception_resnet_v2.inception_resnet_v2_arg_scope(), inception.inception_v3_arg_scope() ] reuse_flags = [reuse, True] for model_idx, model in enumerate( [inception_resnet_v2.inception_resnet_v2, inception.inception_v3]): with slim.arg_scope(scopes[model_idx]): for idx, inputs in enumerate(all_inputs[model_idx]): result = model(inputs, num_classes=self.num_classes, is_training=False, reuse=reuse_flags[idx]) weight = weights[model_idx][idx] # :1 is for slicing out the background class if logits == None: logits = result[0][:, 1:] * weight aux_logits = result[1]['AuxLogits'][:, 1:] * weight else: logits += result[0][:, 1:] * weight aux_logits += result[1]['AuxLogits'][:, 1:] * weight with slim.arg_scope(vgg.vgg_arg_scope()): weight = 0.1 result = vgg.vgg_16(vgg_x_input, num_classes=1000, is_training=False) logits += result[0] * weight with slim.arg_scope(resnet_utils.resnet_arg_scope()): weight = 0.05 result = resnet_v2.resnet_v2_152(vgg_x_input, num_classes=self.num_classes, reuse=reuse) logits += tf.squeeze(result[0])[:, 1:] * weight self.built = True aux_weight = 0.8 logits += aux_logits * aux_weight predictions = layers_lib.softmax(logits) return predictions
def main(): with tf.gfile.FastGFile('output_graph.pb', 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) tf.import_graph_def(graph_def, name='') with tf.Graph().as_default() as graph: inputs = tf.placeholder(dtype=tf.float32, shape=[1, 224, 224, 3]) with slim.arg_scope(vgg.vgg_arg_scope()): _, end_points = vgg.vgg_16(inputs, num_classes=1000, is_training=True, dropout_keep_prob=0.5, spatial_squeeze=False, scope='vgg_16') stats_graph(graph)
def Deeplab_v1(inputs, num_classes): '''A TensorFlow implementation of Deeplab_v1 model based on http://liangchiehchen.com/projects/DeepLab-LargeFOV.html Args: inputs: A 4-D tensor with dimensions [batch_size, height, width, channels] num_classes: Integer, the total number of categories in the dataset Returns: A score map with dimensions [batch_size, 1/8*height, 1/8*width, num_classes] ''' with slim.arg_scope(vgg.vgg_arg_scope()): _, end_points = vgg.vgg_16(inputs, num_classes=1000, is_training=True, dropout_keep_prob=0.5, spatial_squeeze=False, scope='vgg_16') conv4 = end_points["vgg_16/conv4/conv4_3"] # 1/8H * 1/8 * 256 with tf.variable_scope('Deeplab_v1'): pool4 = slim.max_pool2d(conv4, [3, 3], stride=1, padding='SAME', scope='pool4') conv5_1 = slim.conv2d(pool4, 512, [3, 3], rate=2, scope='conv5_1') conv5_2 = slim.conv2d(conv5_1, 512, [3, 3], rate=2, scope='conv5_2') conv5_3 = slim.conv2d(conv5_2, 512, [3, 3], rate=2, scope='conv5_3') pool5_1 = slim.max_pool2d(conv5_3, [3, 3], stride=1, padding='SAME', scope='pool5_1') pool5_2 = slim.avg_pool2d(pool5_1, [3, 3], stride=1, padding='SAME', scope='pool5_2') conv6 = slim.conv2d(pool5_2, 1024, [3, 3], rate=12, scope='conv6') dropout1 = slim.dropout(conv6, keep_prob=0.5) conv7 = slim.conv2d(dropout1, 1024, [1, 1], scope='conv7') dropout2 = slim.dropout(conv7, keep_prob=0.5) logits = slim.conv2d(dropout2, num_classes, [1, 1], activation_fn=None, scope='logits') return logits
def get_predicted_y(x, num_classes, args): """Calculate predicted label""" slim = tf.contrib.slim with slim.arg_scope(inception.inception_v1_arg_scope()): logits_inc_v1, end_points_inc_v1 = inception.inception_v1( dimension_224(x), num_classes=num_classes, is_training=False, scope='InceptionV1') image = (((x + 1.0) * 0.5) * 255.0) processed_imgs_res_v1_50 = preprocess_for_model(image, 'resnet_v1_50') with slim.arg_scope(resnet_v1.resnet_arg_scope()): logits_res_v1_50, end_points_res_v1_50 = resnet_v1.resnet_v1_50( dimension_224(processed_imgs_res_v1_50), num_classes=num_classes, is_training=False, scope='resnet_v1_50') end_points_res_v1_50['logits'] = tf.squeeze( end_points_res_v1_50['resnet_v1_50/logits'], [1, 2]) end_points_res_v1_50['probs'] = tf.nn.softmax( end_points_res_v1_50['logits']) processed_imgs_vgg_16 = preprocess_for_model(image, 'vgg_16') with slim.arg_scope(vgg.vgg_arg_scope()): logits_vgg_16, end_points_vgg_16 = vgg.vgg_16( dimension_224(processed_imgs_vgg_16), num_classes=num_classes, is_training=False, scope='vgg_16') end_points_vgg_16['logits'] = end_points_vgg_16['vgg_16/fc8'] end_points_vgg_16['probs'] = tf.nn.softmax(end_points_vgg_16['logits']) logits = [None] * args.num_model pred_y = [None] * args.num_model logits[0] = end_points_inc_v1['Logits'] logits[1] = end_points_res_v1_50['logits'] logits[2] = end_points_vgg_16['logits'] for i in range(args.num_model): pred_y[i] = tf.argmax(tf.nn.softmax(logits[i]), 1) return pred_y, logits
def ens_model(x): #input remains in[0,1] image = (x * 255.0) num_classes = 110 processed_incv1 = preprocess_for_model(image, 'inception_v1') with slim.arg_scope(inception.inception_v1_arg_scope()): logits_inc_v1, end_points_inc_v1 = inception.inception_v1( processed_incv1, num_classes=num_classes, is_training=False, scope='InceptionV1') processed_imgs_res_v1_50 = preprocess_for_model(image, 'resnet_v1_50') with slim.arg_scope(resnet_v1.resnet_arg_scope()): logits_res_v1_50, end_points_res_v1_50 = resnet_v1.resnet_v1_50( processed_imgs_res_v1_50, num_classes=num_classes, is_training=False, scope='resnet_v1_50') end_points_res_v1_50['logits'] = tf.squeeze( end_points_res_v1_50['resnet_v1_50/logits'], [1, 2]) end_points_res_v1_50['probs'] = tf.nn.softmax( end_points_res_v1_50['logits']) # image = (((x + 1.0) * 0.5) * 255.0)#.astype(np.uint8) processed_imgs_vgg_16 = preprocess_for_model(image, 'vgg_16') with slim.arg_scope(vgg.vgg_arg_scope()): logits_vgg_16, end_points_vgg_16 = vgg.vgg_16(processed_imgs_vgg_16, num_classes=num_classes, is_training=False, scope='vgg_16') end_points_vgg_16['logits'] = end_points_vgg_16['vgg_16/fc8'] end_points_vgg_16['probs'] = tf.nn.softmax(end_points_vgg_16['logits']) ######################## #one_hot = tf.one_hot(y, num_classes) ######################## logits = (end_points_inc_v1['Logits'] + end_points_res_v1_50['logits'] + end_points_vgg_16['logits']) / 3.0 print('logits.shape:', logits.shape) return logits
def build_encoder(self): _inputs = tf.reshape(self.x, [-1] + self.image_shape) _net = vgg.vgg_16(_inputs) _layers = _net[1] del _layers['vgg_16/fc8'] # the last layer (ie softmax) _encoding = _layers['vgg_16/fc7'] # the layer before the softmax _encoding = _encoding[:, 0, 0, :] _shape = _encoding.get_shape()[-1].value _encoding = tf.reshape(_encoding, [self.batch_size, -1, _shape]) _encoding = tf.transpose( _encoding, [1, 0, 2]) # shape is now [T_MAX, BATCH_SIZE, _shape] self.encodings = _encoding
def test_vgg(self): with slim.arg_scope(vgg.vgg_arg_scope()): net, end_points = vgg.vgg_16(self.inputs, self.nbclasses, is_training=False) net = slim.softmax(net) saver = tf.train.Saver(tf.global_variables()) check_point = 'test/data/vgg_16.ckpt' sess = tf.InteractiveSession() saver.restore(sess, check_point) self.sess = sess self.graph_origin = tf.get_default_graph() self.target_op_name = darkon.Gradcam.candidate_featuremap_op_names( sess, self.graph_origin)[-2] self.model_name = 'vgg'
def imageBreak(): imagepath = "../../data/beauty/001/2.jpg" with tf.Graph().as_default(): image = tf.image.decode_jpeg(tf.read_file(imagepath), channels=3) image = tf.image.resize_images(image, [224, 224]) #减去均值之前,将像素转为32位浮点数 image_float = tf.to_float(image, name="ToFloat") #每个像素减去像素的均值 processed_image = _mean_image_subtraction(image_float, [_R_MEAN, _G_MEAN, _B_MEAN]) input_image = tf.expand_dims(processed_image, 0) with slim.arg_scope(vgg.vgg_arg_scope()): logits, endpoints = vgg.vgg_16(inputs=input_image, num_classes=1000, is_training=False, spatial_squeeze=False) pred = tf.argmax(logits, dimension=3) #对输出层进行逐个比较,取得不同层同一位置中最大的概率所对应的值 init_fn = slim.assign_from_checkpoint_fn( os.path.join(configure.Vgg16ModelPath, configure.Vgg16ModelName), slim.get_model_variables("vgg_16")) with tf.Session() as sess: init_fn(sess) fcn8s, fcn16s, fcn32s = sess.run([ endpoints["vgg_16/pool3"], endpoints["vgg_16/pool4"], endpoints["vgg_16/pool5"] ]) upsampled_logits = upsample_tf(factor=16, input_img=fcn8s.squeeze()) upsampled_predictions32 = upsampled_logits.squeeze().argmax(2) unique_classes, relabeled_image = np.unique( upsampled_predictions32, return_inverse=True) relabeled_image = relabeled_image.reshape( upsampled_predictions32.shape) labels_names = [] names = imagenet_classes.class_names for index, current_class_number in enumerate(unique_classes): labels_names.append( str(index) + " " + names[current_class_number + 1]) discrete_matshow(data=relabeled_image, labels_names=labels_names, title="Segmentation")
def test_reducing_vgg16_slim(self): """ Test reducing vgg16 slim model """ tf.compat.v1.reset_default_graph() sess = tf.compat.v1.Session() module_zero_channels_list = [] inp = tf.compat.v1.placeholder(tf.float32, [1, 224, 224, 3]) _ = vgg.vgg_16(inp) init = tf.compat.v1.global_variables_initializer() sess.run(init) input_op_names = ["Placeholder"] output_op_names = ['vgg_16/fc8/squeezed'] tf_op = tf.compat.v1.get_default_graph().get_operation_by_name( "vgg_16/fc7/Conv2D") input_channels_to_winnow = [2, 3, 4] module_mask_pair = (tf_op, input_channels_to_winnow) module_zero_channels_list.append(module_mask_pair) new_sess, ordered_modules_list = winnow.winnow_tf_model( sess, input_op_names, output_op_names, module_zero_channels_list, reshape=True, in_place=True, verbose=True) # Save and reload modified graph to allow changes to take effect new_sess = save_and_load_graph('./saver', new_sess) # _ = tf.compat.v1.summary.FileWriter('./reduced_graph', new_sess.graph) with new_sess.graph.as_default(): inp = tf.random.uniform(shape=(1, 224, 224, 3)) inp_array = inp.eval(session=new_sess) model_input = new_sess.graph.get_tensor_by_name("Placeholder:0") model_output = new_sess.graph.get_tensor_by_name( "vgg_16/fc8/squeezed:0") # run through entire model to check no error is produced _ = new_sess.run(model_output, feed_dict={model_input: inp_array}) self.assertEqual(4, len(ordered_modules_list)) new_sess.close() sess.close()