예제 #1
0
def main():

    img = utils.load_image('./test_data/34.JPEG')
    model_path = '../ShuffleNetV1-1x-8g.npz'

    img = img.reshape((1, 224, 224, 3))
    img = np.float32(img) * 255.0

    arch = shufflenet.Shufflenet(model_path)

    feed_img = tf.placeholder('float', [1, 224, 224, 3])
    feed_dict = {feed_img: img}

    with tf.device('/cpu:0'):
        with tf.Session() as sess:
            conv_res = arch.build(feed_img)
            run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
            run_metadata = tf.RunMetadata()
            begin = time.time()
            prob = sess.run(conv_res,
                            feed_dict=feed_dict,
                            options=run_options,
                            run_metadata=run_metadata)
            end = time.time()
            utils.print_prob(prob[0], './synset.txt')
            print("Time: " + str(end - begin))
            export_graph = tf.summary.FileWriter('./logs/shufflenet_graph/')
            export_graph.add_graph(sess.graph)
            export_graph.add_run_metadata(run_metadata, 'zucc')
예제 #2
0
def test(batch):
    with tf.Session(
            config=tf.ConfigProto(gpu_options=(
                tf.GPUOptions(per_process_gpu_memory_fraction=0.7)))) as sess:
        images = tf.placeholder("float", [None, 224, 224, 3])
        feed_dict = {images: batch}

        vgg = vgg16.Vgg16()
        with tf.name_scope("content_vgg"):
            vgg.build(images)

        prob = sess.run(vgg.prob, feed_dict=feed_dict)
        print(prob)
        utils.print_prob(prob[0], './synset.txt')
예제 #3
0
def load_caffe(img_p, layers=50):
    caffe.set_mode_cpu()

    prototxt = "ResNet-%d-deploy.prototxt" % layers
    caffemodel = "ResNet-%d-model.caffemodel" % layers
    net = caffe.Net(prototxt, caffemodel, caffe.TEST)

    net.blobs['data'].data[0] = img_p.transpose((2,0,1))
    assert net.blobs['data'].data[0].shape == (3, 224, 224)
    net.forward()

    caffe_prob = net.blobs['prob'].data[0]
    utils.print_prob(caffe_prob)

    return net
예제 #4
0
def main():
    img = utils.load_image('./../tf_shufflenet/test_data/32.JPEG')
    img = img.reshape((1, 224, 224, 3))
    img = np.float32(img) * 255.0

    act = np.float32(np.arange(28 * 28 * 384).reshape(1, 28, 28, 384))
    act2 = np.float32(np.arange(28 * 28 * 192).reshape(1, 28, 28, 192))
    arch = Shufflenet('./../ShuffleNetV1-1x-8g.npz')
    res = arch.pw_gconv(act, 'stage3', 'block0', 'conv1', 8)
    res2 = arch.batch_normalization(act2, 'stage3', 'block0', 'conv1')
    res3 = arch.dw_conv(act2, 'stage3', 'block0', padding='SAME', stride=1)
    #	print(res3[0, 10:14, 10:14, 128:132])

    prob = arch.forward_pass(img)
    utils.print_prob(prob[0], '../tf_shufflenet/synset.txt')
예제 #5
0
def load_caffe(img_p, layers=50):
    caffe.set_mode_cpu()

    prototxt = "ResNet-%d-deploy.prototxt" % layers
    caffemodel = "ResNet-%d-model.caffemodel" % layers
    net = caffe.Net(prototxt, caffemodel, caffe.TEST)

    net.blobs['data'].data[0] = img_p.transpose((2, 0, 1))
    assert net.blobs['data'].data[0].shape == (3, 224, 224)
    net.forward()

    caffe_prob = net.blobs['prob'].data[0]
    utils.print_prob(caffe_prob)

    return net
예제 #6
0
def test_image_vgg19(imgdata, npypath, xmode=0):
    num = len(imgdata)
    images = tf.placeholder("float", [num, 224, 224, 3])
    model = Vgg19(npypath)
    with tf.name_scope("content_vgg"):  #content_vgg
        model.build(images, xmode)

    # score = model.fc8
    # prediction = tf.argmax(score, 1)
    batchs = []
    for x in imgdata:
        img = utils.load_image(x)
        b = img.reshape((1, 224, 224, 3))
        batchs.append(b)
    batch = np.concatenate(batchs, 0)
    #saver = tf.train.import_meta_graph('checkpoints/vgg_16_train.ckpt.meta')
    with tf.device('/cpu:0'):
        with tf.Session() as sess:
            #sess.run(tf.global_variables_initializer())
            #saver.restore(sess, tf.train.latest_checkpoint('./checkpoints/'))
            feed_dict = {images: batch}
            prob = sess.run(model.prob, feed_dict=feed_dict)
            for x in range(0, num):
                print("-" * 25,
                      tcolor.UseStyle(imgdata[x], mode='bold', fore='white'),
                      "-" * 25)
                res = utils.print_prob(prob[x], './synset.txt')
예제 #7
0
def tensor_imgdata(imgdata, images, vgg, bakpath):
    num = len(imgdata)
    batchs = []
    for x in imgdata:
        img = utils.load_image(x)
        b = img.reshape((1, 224, 224, 3))
        batchs.append(b)
    batch = np.concatenate(batchs, 0)
    with tf.device('/cpu:0'):
        with tf.Session() as sess:

            feed_dict = {images: batch}
            prob = sess.run(vgg.prob, feed_dict=feed_dict)
            #print(prob)
            for x in range(0, num):
                print("-" * 25, imgdata[x], "-" * 25)
                res = utils.print_prob(prob[x], './synset.txt')

                #print(res[10:])
                font = cv2.FONT_HERSHEY_SIMPLEX
                img = cv2.imread(imgdata[x])
                cv2.putText(img, res[10:],
                            (int(img.shape[0] / 3 - len(res[10:])),
                             int(img.shape[1] / 2)), font, 1, (0, 255, 0), 2)
                cv2.imwrite(os.path.join(bakpath, imgdata[x]), img)
예제 #8
0
def convert(graph, img, img_p, layers):
    net = load_caffe(img_p, layers)
    param_provider = CaffeParamProvider(net)
    with tf.device('/cpu:0'):
        images = tf.placeholder("float32", [None, 224, 224, 3], name="images")
        m = resnet.Model(param_provider)
        m.build(images, layers)
    sess = tf.Session()
    sess.run(tf.initialize_all_variables())


    i = [
        graph.get_tensor_by_name("conv1/relu:0"),
        graph.get_tensor_by_name("pool1:0"),
        graph.get_tensor_by_name("res2a/relu:0"),
        graph.get_tensor_by_name("res2b/relu:0"),
        graph.get_tensor_by_name("res2c/relu:0"),
        graph.get_tensor_by_name("res3a/relu:0"),
        graph.get_tensor_by_name("res5c/relu:0"),
        graph.get_tensor_by_name("pool5:0"),
        graph.get_tensor_by_name("prob:0"),
    ]

    o = sess.run(i, {
        images: img[np.newaxis,:]
    })

    assert_almost_equal(net.blobs['conv1'].data, o[0])
    assert_almost_equal(net.blobs['pool1'].data, o[1])
    assert_almost_equal(net.blobs['res2a'].data, o[2])
    assert_almost_equal(net.blobs['res2b'].data, o[3])
    assert_almost_equal(net.blobs['res2c'].data, o[4])
    assert_almost_equal(net.blobs['res3a'].data, o[5])
    assert_almost_equal(net.blobs['res5c'].data, o[6])
    assert_almost_equal(net.blobs['pool5'].data, o[7])

    utils.print_prob(o[8][0])

    prob_dist = np.linalg.norm(net.blobs['prob'].data - o[8])
    print 'prob_dist ', prob_dist
    assert prob_dist < 0.2 # XXX can this be tightened?

    save_graph("resnet-%d.tfmodel" % layers)
예제 #9
0
def convert(graph, img, img_p, layers):
    net = load_caffe(img_p, layers)
    param_provider = CaffeParamProvider(net)
    with tf.device('/cpu:0'):
        images = tf.placeholder("float32", [None, 224, 224, 3], name="images")
        m = resnet.Model(param_provider)
        m.build(images, layers)
    sess = tf.Session()
    sess.run(tf.initialize_all_variables())

    i = [
        graph.get_tensor_by_name("conv1/relu:0"),
        graph.get_tensor_by_name("pool1:0"),
        graph.get_tensor_by_name("res2a/relu:0"),
        graph.get_tensor_by_name("res2b/relu:0"),
        graph.get_tensor_by_name("res2c/relu:0"),
        graph.get_tensor_by_name("res3a/relu:0"),
        graph.get_tensor_by_name("res5c/relu:0"),
        graph.get_tensor_by_name("pool5:0"),
        graph.get_tensor_by_name("prob:0"),
    ]

    o = sess.run(i, {images: img[np.newaxis, :]})

    assert_almost_equal(net.blobs['conv1'].data, o[0])
    assert_almost_equal(net.blobs['pool1'].data, o[1])
    assert_almost_equal(net.blobs['res2a'].data, o[2])
    assert_almost_equal(net.blobs['res2b'].data, o[3])
    assert_almost_equal(net.blobs['res2c'].data, o[4])
    assert_almost_equal(net.blobs['res3a'].data, o[5])
    assert_almost_equal(net.blobs['res5c'].data, o[6])
    assert_almost_equal(net.blobs['pool5'].data, o[7])

    utils.print_prob(o[8][0])

    prob_dist = np.linalg.norm(net.blobs['prob'].data - o[8])
    print 'prob_dist ', prob_dist
    assert prob_dist < 0.2  # XXX can this be tightened?

    save_graph("resnet-%d.tfmodel" % layers)
예제 #10
0
def tensor_imgdata(sess, imgdata, images, train_mode, results, vgg, bakpath):
    num = len(imgdata)
    batchs = []
    for x in imgdata:
        img = utils.load_image(x)
        b = img.reshape((1, 224, 224, 3))
        batchs.append(b)
    batch = np.concatenate(batchs, 0)
    # with tf.device('/cpu:0') as device:
    #     with tf.Session() as sess:

    feed_dict = {images: batch, train_mode: False}
    prob = sess.run(vgg.prob, feed_dict=feed_dict)
    for x in range(0, num):
        print("-" * 25, tcolor.UseStyle(imgdata[x], mode='bold', fore='white'),
              "-" * 25)
        res = utils.print_prob(prob[x], './synset.txt')
        # font = cv2.FONT_HERSHEY_SIMPLEX
        # img = cv2.imread(imgdata[x])
        # cv2.putText(img, res[10:], (int(img.shape[0]/3 - len(res[10:])), int(img.shape[1]/2)), font, 1, (0, 255, 0), 2)
        # cv2.imwrite(os.path.join(bakpath,imgdata[x]),img)

    #print(tcolor.UseStyle("-"*125,mode = 'bold',fore = 'red'))
    true_out = tf.placeholder(tf.float32, [num, 1000])
    cost = tf.reduce_sum((vgg.prob - true_out)**2)
    train = tf.train.GradientDescentOptimizer(0.0001).minimize(cost)
    sess.run(train,
             feed_dict={
                 images: batch,
                 true_out: results,
                 train_mode: True
             })
    # test classification again, should have a higher probability about tiger
    prob = sess.run(vgg.prob, feed_dict={images: batch, train_mode: False})
    for x in range(0, num):
        print("-" * 25, tcolor.UseStyle(imgdata[x], mode='bold', fore='white'),
              "-" * 25)
        utils.print_prob(prob[x], './synset.txt', fore='blue')
예제 #11
0
def tensor_imgdata(imgdata, images, vgg, bakpath):
    num = len(imgdata)
    batchs = []
    for x in imgdata:
        img = utils.load_image(x)
        b = img.reshape((1, 224, 224, 3))
        batchs.append(b)
    batch = np.concatenate(batchs, 0)
    with tf.device('/cpu:0'):  #'/cpu:0'
        with tf.Session() as sess:

            feed_dict = {images: batch}
            prob = sess.run(vgg.prob, feed_dict=feed_dict)
            #print(prob)
            for x in range(0, num):

                print("-" * 25,
                      tcolor.UseStyle(imgdata[x], mode='bold', fore='white'),
                      "-" * 25)
                res = utils.print_prob(prob[x], './synset.txt')
예제 #12
0
def test_image_vgg16(imgdata, npypath, xmode=0):
    num = len(imgdata)
    images = tf.placeholder("float", [num, 224, 224, 3])
    model = Vgg16(npypath)
    with tf.name_scope("content_vgg"):  #content_vgg
        model.build(images, xmode)
    batchs = []
    for x in imgdata:
        img = utils.load_image(x)
        b = img.reshape((1, 224, 224, 3))
        batchs.append(b)
    batch = np.concatenate(batchs, 0)

    with tf.device('/device:GPU:0'):
        with tf.Session() as sess:
            feed_dict = {images: batch}
            prob = sess.run(model.prob, feed_dict=feed_dict)
            for x in range(0, num):
                print("-" * 25,
                      tcolor.UseStyle(imgdata[x], mode='bold', fore='white'),
                      "-" * 25)
                res = utils.print_prob(prob[x], './synset.txt')
예제 #13
0
def main(argv=None):
    if len(argv) >= 2:
        if argv[1] != "train" and argv[1] != "infer":
            print "the string of your input is not right," \
                  " you only input 'train' for training model, and 'infer' for inference"
            exit(0)
    else:
        print "please input the mode of implement, " \
              "note that 'train' for training model, and 'infer' for inference"
        exit(0)
    FLAGS.mode = argv[1]
    images = tf.placeholder(tf.float32,
                            shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3],
                            name="input_image")
    labels = tf.placeholder(tf.float32,
                            shape=[None, FLAGS.num_of_classes],
                            name="labels")
    train_mode = tf.placeholder(tf.bool)

    sess = tf.Session()

    vgg = Vgg16('vgg16.npy')
    vgg.build(images, train_mode)

    # print number of variables used: 143667240 variables, i.e. ideal size = 548MB
    print(vgg.get_var_count())

    opts, loss = vgg.train_opts(labels, FLAGS.learning_rate)

    sess.run(tf.global_variables_initializer())

    if FLAGS.mode == "train":
        # read training dataset

        for epoch in xrange(FLAGS.max_epochs):
            # train_images, train_annotations = train_dataset_reader.next_batch(FLAGS.batch_size)
            # valid_images, valid_annotations = validation_dataset_reader.next_batch(FLAGS.batch_size)
            feed_dict = {
                images: train_images,
                labels: train_labels,
                train_mode: True
            }
            sess.run(opts, feed_dict=feed_dict)

            if epoch % 10 == 0:
                train_loss = sess.run(loss, feed_dict=feed_dict)
                print("Epoch: %d, ------> loss:%g" % (epoch, train_loss))

            if epoch % (MAX_EPOCHS - 1) == 0:
                vgg.save_npy(sess, './vgg16_weights.npy')

    if FLAGS.mode == "infer":
        start_time = time.time()
        # read images for inference
        img1 = utils.load_image("./test_data/image10.jpg")
        # img1_label = [1 if i == 292 else 0 for i in range(1000)]  # 1-hot result for tiger
        batch = img1.reshape((1, 224, 224, 3))

        # test classification again, should have a higher probability about tiger
        prob = sess.run(vgg.prob, feed_dict={images: batch, train_mode: False})
        utils.print_prob(prob[0], './synset.txt')
        print "inference time:", time.time() - start_time
예제 #14
0
def train(net, model_name, hyper, cfg, writer):

    dataset = KittiDataset(cfg=cfg,
                           root='/data/cxg1/VoxelNet_pro/Data',
                           set='val')
    data_loader = data.DataLoader(dataset, batch_size=cfg.N, num_workers=4, collate_fn=detection_collate, shuffle=True, \
                              pin_memory=False)

    # define loss function
    criterion = VoxelLoss(alpha=hyper['alpha'],
                          beta=hyper['beta'],
                          gamma=hyper['gamma'])

    running_loss = 0.0
    running_reg_loss = 0.0
    running_conf_loss = 0.0

    # training process
    # batch_iterator = None
    epoch_size = len(dataset) // cfg.N
    print('Epoch size', epoch_size)
    iteration = 0
    for voxel_features, voxel_coords, gt_box3d_corner, gt_box3d, images, calibs, ids in data_loader:

        # wrapper to variable
        voxel_features = torch.tensor(voxel_features).to(cfg.device)

        pos_equal_one = []
        neg_equal_one = []
        targets = []

        for i in range(len(gt_box3d)):
            pos_equal_one_, neg_equal_one_, targets_ = dataset.cal_target(
                gt_box3d_corner[i], gt_box3d[i], cfg)
            pos_equal_one.append(pos_equal_one_)
            neg_equal_one.append(neg_equal_one_)
            targets.append(targets_)

        pos_equal_one = torch.stack(pos_equal_one, dim=0)
        neg_equal_one = torch.stack(neg_equal_one, dim=0)
        targets = torch.stack(targets, dim=0)

        # zero the parameter gradients
        # forward
        score, reg = net(voxel_features, voxel_coords)

        if iteration == 0:  # visualize the first image
            print_prob(score, "pred.png")
            print_prob(pos_equal_one, "gt.png")

        # calculate loss
        conf_loss, reg_loss, xyz_loss, whl_loss, r_loss = criterion(
            reg, score, pos_equal_one, neg_equal_one, targets)
        loss = hyper['lambda'] * conf_loss + reg_loss

        running_conf_loss += conf_loss.item()
        running_reg_loss += reg_loss.item()
        running_loss += (reg_loss.item() + conf_loss.item())

        pre_image = draw_boxes(reg, score, images, calibs, ids, 'pred')
        gt_image = draw_boxes(targets.float(), pos_equal_one.float(), images,
                              calibs, ids, 'true')
        try:
            writer.add_image("gt_image_box {}".format(iteration),
                             gt_image,
                             global_step=iteration,
                             dataformats='NHWC')
            writer.add_image("predict_image_box {}".format(iteration),
                             pre_image,
                             global_step=iteration,
                             dataformats='NHWC')
        except:
            pass
        iteration += 1
예제 #15
0
    for i in range(0, tf.app.flags.FLAGS.nb_images):
        input_loop = inputs[i:i + 1]

        feed_dict = {images: input_loop}

        prob_tensor = graph.get_tensor_by_name("import/prob:0")

        prob = sess.run(prob_tensor, feed_dict=feed_dict)

        print((35 / tf.app.flags.FLAGS.nb_images), end='', file=sys.stderr)
        sys.stderr.flush()

        prob_save.append(prob[0])

    top1 = utils.print_prob(prob, tf.app.flags.FLAGS.print_prob)

    for i in range(0, len(prob_save)):
        sys.stdout.flush()
        img_fin = cv2.imread(SAVED_IMAGES + "own_" + str(i) + ".png")
        cv2.putText(img_fin, str(top1[i]),
                    (math.floor(img_fin.shape[0] *
                                (0.05)), math.floor(img_fin.shape[1] * (0.7))),
                    cv2.FONT_HERSHEY_DUPLEX, 2, (1, 224, 177), 3)
        sys.stdout.flush()
        cv2.imwrite(SAVED_IMAGES + "image_finish_" + str(i) + ".png", img_fin)

        print((10 / tf.app.flags.FLAGS.nb_images), end='', file=sys.stderr)
        sys.stderr.flush()

        print("Done!")
            image,
            processed_image,
            probabilities,
        ])

        probabilities = probabilities[0, 0:]

        coord.request_stop()
        coord.join(threads)

    #
    # plt.figure()
    #
    # # 显示下载的图片及其预处理之后的对比图像
    # plt.subplot(1, 2, 1)
    # plt.imshow(np_image.astype(np.uint8))
    # plt.title("image")
    # plt.axis('off')
    #
    # # 显示最终传入网络模型的图片
    # # 图像的像素值做了[-1, 1]的归一化
    # plt.subplot(1, 2, 2)
    # plt.imshow(network_input / (network_input.max() - network_input.min()) )
    # plt.title("Resized, Cropped and Mean-Centered input to network")
    # plt.axis('off')
    #
    # plt.show()

    #show results
    utils.print_prob(probabilities, './synset.txt', 5)
예제 #17
0
batch_size = 10
images = tf.placeholder("float", [batch_size, 224, 224, 3])
batch = get_batch(batch_size)


with tf.Session() as sess:
  feed_dict = {images: batch}

  vgg = vgg16.Vgg16()
  with tf.name_scope("content_vgg"):
    vgg.build(images)
    pred = vgg.prob

    np_pred = sess.run(pred, feed_dict=feed_dict)
    print(np_pred)
    utils_vgg.print_prob(np_pred[0], './synset.txt')


    # Yitao-TLS-Begin
    export_path_base = "vgg_model"
    export_path = os.path.join(
        compat.as_bytes(export_path_base),
        compat.as_bytes(str(FLAGS.model_version)))
    print 'Exporting trained model to', export_path
    builder = saved_model_builder.SavedModelBuilder(export_path)

    tensor_info_x = tf.saved_model.utils.build_tensor_info(images)
    tensor_info_y = tf.saved_model.utils.build_tensor_info(pred)

    prediction_signature = tf.saved_model.signature_def_utils.build_signature_def(
        inputs={'images': tensor_info_x},
예제 #18
0
import numpy as np
import tensorflow as tf

import vgg19
import utils

img1 = utils.load_image("./test_data/tiger.jpeg")
img2 = utils.load_image("./test_data/puzzle.jpeg")

batch1 = img1.reshape((1, 224, 224, 3))
batch2 = img2.reshape((1, 224, 224, 3))

batch = np.concatenate((batch1, batch2), 0)

with tf.Session() as sess:
    images = tf.placeholder("float", [2, 224, 224, 3])
    feed_dict = {images: batch}

    vgg = vgg19.Vgg19("./vgg19.npy")
    with tf.name_scope("content_vgg"):
        vgg.build(images)

    prob = sess.run(vgg.prob, feed_dict=feed_dict)
    print(prob)
    utils.print_prob(prob[0], './synset.txt')
    utils.print_prob(prob[1], './synset.txt')
예제 #19
0
        x = self.fc8(x)

        return x

    def num_flat_features(self, x):
        size = x.size()[1:]  # all dimensions except the batch dimension
        num_features = 1
        for s in size:
            num_features *= s
        return num_features


model = VGGnet()
model.cuda()
model.eval()

image = utils.load_image(args.test)
image = image[:, :, ::-1]
VGG_MEAN = np.array([103.939, 116.779, 123.68])
image = (image * 255.0) - VGG_MEAN
image = image.transpose(2, 0, 1)
image = image.astype(np.float32)
input = torch.from_numpy(image)
input = input.cuda()
input_var = torch.autograd.Variable(input, volatile=True)

output = model(input_var.unsqueeze(0))
output = output.data.cpu().numpy()
out = torch.autograd.Variable(torch.from_numpy(output))
utils.print_prob(F.softmax(out).data.numpy()[0], './synset.txt')
예제 #20
0
with open("vgg16.tfmodel", mode='rb') as f:
    fileContent = f.read()

graph_def = tf.GraphDef()
graph_def.ParseFromString(fileContent)

images = tf.placeholder("float", [None, 224, 224, 3])

tf.import_graph_def(graph_def, input_map={"images": images})
print "graph loaded from disk"

graph = tf.get_default_graph()

cat = utils.load_image("cat.jpg")

with tf.Session() as sess:
    init = tf.initialize_all_variables()
    sess.run(init)
    print "variables initialized"

    batch = cat.reshape((1, 224, 224, 3))
    assert batch.shape == (1, 224, 224, 3)

    feed_dict = {images: batch}

    prob_tensor = graph.get_tensor_by_name("import/prob:0")
    prob = sess.run(prob_tensor, feed_dict=feed_dict)

utils.print_prob(prob[0])
예제 #21
0
import utils
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import vgg16

img_tiger = utils.load_image('data/tiger.jpeg')
img_puzzle = utils.load_image('data/puzzle.jpeg')
img_tiger = utils.resize_image(img_tiger).reshape([1, 224, 224, 3])
img_puzzle = utils.resize_image(img_puzzle).reshape([1, 224, 224, 3])

images = np.concatenate([img_tiger, img_puzzle], axis=0)

with tf.Session() as sess:
    images_ = tf.placeholder(dtype=tf.float32, shape=[None, 224, 224, 3])
    vgg = vgg16.Vgg16()
    vgg.build_nn(images_)
    results = sess.run(vgg.prob, feed_dict={images_: images})
    print(results)
    print('tiger-------------')
    utils.print_prob(results[0], 'data/synset.txt')
    print('puzzle------------')
    utils.print_prob(results[1], 'data/synset.txt')
예제 #22
0
with open("resnet-152.tfmodel", mode='rb') as f:
  fileContent = f.read()

graph_def = tf.GraphDef()
graph_def.ParseFromString(fileContent)

images = tf.placeholder("float", [None, size, size, 3])

tf.import_graph_def(graph_def, input_map={ "images": images })
print "graph loaded from disk"

graph = tf.get_default_graph()

cat = utils.load_image("cat.jpg", size)

with tf.Session() as sess:
  init = tf.initialize_all_variables()
  sess.run(init)
  print "variables initialized"

  batch = cat.reshape((1, size, size, 3))

  feed_dict = { images: batch }

  prob_tensor = graph.get_tensor_by_name("import/prob:0")
  prob = sess.run(prob_tensor, feed_dict=feed_dict)

utils.print_prob(prob[0])


예제 #23
0
파일: VGG16.py 프로젝트: Annis-Zeng/MyML
    # 识别所有图片,返回类别名称及概率
    NAME, PROB = [], []
    with tf.Session() as sess:
        vgg = vgg16.Vgg16()
        with tf.name_scope('Input'):
            image = tf.placeholder(tf.float32,
                                   shape=[1, 224, 224, 3],
                                   name='input_image')
            vgg.build(image)
        start_time = time.time()
        for im_l in im_list:
            img = utils.load_image(im_l)
            prob = sess.run(vgg.prob,
                            feed_dict={image: img.reshape((1, 224, 224, 3))})
            #         print(prob)
            name, p = utils.print_prob(prob.flatten(), './synset.txt')
            NAME.append(name)
            PROB.append(p)
        end_time = time.time()

    # 提取所有类别名称,概率
    num = len(im_list)
    print('There are {} images in total, recognition time: {:.4f}s.'.format(
        num, end_time - start_time))
    TITLE = [', '.join([name[10:], str(p)]) for name, p in zip(NAME, PROB)]
    print(TITLE)

    # 打印所有图片及其所属类别、概率
    cols = 3  # 每行显示的图片数
    nrows = num // cols + 1
    height = nrows * 5
예제 #24
0
import vgg16
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import utils

image1 = utils.load_image('./test_pic/tiger.jpeg')
image2 = utils.load_image('./test_pic/puzzle.jpeg')
image = np.concatenate((image1[None, :, :, :], image2[None, :, :, :]), axis=0)

plt.figure()
with tf.device('/cpu:0'):
    with tf.Session() as sess:
        #with  tf.Session(config=tf.ConfigProto(gpu_options=(tf.GPUOptions(allow_growth=True))))  as sess:
        vgg = vgg16.Vgg16("./../vgg16.npy")

        vgg.build_graph()

        sess.run(tf.global_variables_initializer())
        logits = sess.run(vgg.logits, {vgg.data_X: image})
        logits = np.array(logits)
        print(logits.shape)
        utils.print_prob(logits[0], './synset.txt')
        utils.print_prob(logits[1], './synset.txt')
예제 #25
0
def main():

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

    # TODO: extend this part to a list

    image_name = 'tabby'

    n_labels = 5

    n_input = 64

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

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

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

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

    batch_size = batch_img.shape[0]

    # tf session
    sess = tf.Session()

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

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

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

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

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

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

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

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

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

    for idx in range(batch_size):
        print_prob(probs[idx])
        visualize(batch_img[idx], target_conv_layer_val[idx],
                  conv_grad_norm_val[idx], sal_map_val[idx], sal_map_type,
                  save_dir, fns[idx], probs[idx])
예제 #26
0
import numpy as np
import tensorflow as tf

import vgg16
import utils

img1 = utils.load_image("./test_data/tiger.jpeg")
img2 = utils.load_image("./test_data/puzzle.jpeg")

batch1 = img1.reshape((1, 224, 224, 3))
batch2 = img2.reshape((1, 224, 224, 3))

batch = np.concatenate((batch1, batch2), 0)

# with tf.Session(config=tf.ConfigProto(gpu_options=(tf.GPUOptions(per_process_gpu_memory_fraction=0.7)))) as sess:
with tf.device('/cpu:0'):
    with tf.Session() as sess:
        images = tf.placeholder("float", [2, 224, 224, 3])
        feed_dict = {images: batch}

        vgg = vgg16.Vgg16()
        with tf.name_scope("content_vgg"):
            vgg.build(images)

        prob = sess.run(vgg.prob, feed_dict=feed_dict)
        print(prob)
        utils.print_prob(prob[0], './synset.txt')
        utils.print_prob(prob[1], './synset.txt')
예제 #27
0
def visualize_vgg():
    sal_map_type = "GuidedBackprop_maxlogit"  # change it to get different visualizations
    load_weights = 'random'  # how to load the weights of vgg16
    image_name = 'tabby'  # or using a list to deal with multiple images

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

    sal_type = sal_map_type.split('_')[0]
    logit_type = sal_map_type.split('_')[1]

    n_labels = 1000
    n_input = 224

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

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

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

    batch_size = batch_img.shape[0]

    # tf session
    sess = tf.Session()
    # tf.reset_default_graph()
    vgg = prepare_vgg(sal_type, sess)

    # saliency gradient to input layer
    sal_map = get_saliency(vgg, logit_type)

    if load_weights in ['part', 'reverse', 'only']:
        for layer_idx, layer in enumerate(layers):
            if load_weights == 'part':
                # fill the first "idx" layers with the trained weights
                # randomly initialize the rest
                vgg.load_weights_part(layer_idx * 2 + 1, 'vgg16_weights.npz',
                                      sess)

            elif load_weights == 'reverse':
                # do not fill the first "idx" layers with the trained weights
                # randomly initialize them
                vgg.load_weights_reverse(layer_idx * 2 + 1,
                                         'vgg16_weights.npz', sess)

            elif load_weights == 'only':
                # do not load a specific layer ("idx") with the trained weights
                # randomly initialize it
                vgg.load_weights_only(layer_idx * 2 + 1, 'vgg16_weights.npz',
                                      sess)

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

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

            for idx in range(batch_size):
                print_prob(probs[idx])
                visualize(sal_map_val[idx], sal_map_type, load_weights,
                          save_dir, fns[idx], layer_idx)

    elif load_weights in ['random', 'trained']:
        # different options for loading weights
        if load_weights == 'trained':
            vgg.load_weights('vgg16_weights.npz', sess)

        elif load_weights == 'random':
            vgg.init(sess)

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

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

        for idx in range(batch_size):
            print_prob(probs[idx])
            visualize(sal_map_val[idx], sal_map_type, load_weights, save_dir,
                      fns[idx])

    else:
        raise Exception("Unknown load_weights type")
예제 #28
0
import utils
import time

start = time.time()
def tick():
    print(time.time() - start)

filenames = ["./test_data/tiger.jpeg", "./test_data/puzzle.jpeg", "./test_data/tiger.jpeg", "./test_data/puzzle.jpeg"]
images = [utils.load_image(f) for f in filenames]
batches = [im.reshape((1, 224, 224, 3)) for im in images]

batch = np.concatenate(batches, 0)

tick()

with tf.Session(
        config=tf.ConfigProto(gpu_options=(tf.GPUOptions(per_process_gpu_memory_fraction=0.7)))) as sess:
    images = tf.placeholder("float", [len(filenames), 224, 224, 3])
    feed_dict = {images: batch}

    tick()
    vgg = vgg16.Vgg16()
    with tf.name_scope("content_vgg"):
        vgg.build(images)
    tick()
    probs = sess.run(vgg.prob, feed_dict=feed_dict)
    tick()
    for pr in probs:
        utils.print_prob(pr, './synset.txt')

예제 #29
0
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 15 16:35:00 2018

@author: wzhu16
"""

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import ResNet as resnet
import utils
import os
from imageio import imread
import pickle


im2 = utils.load_image224('Strawberry.jpg')
batch1 = im2
batch = [batch1]

resnet = resnet.resnet()

resnet.build_model()

pb2 = resnet.predict(batch)   
utils.print_prob(pb2[0], 'imagenet-classes.txt')
예제 #30
0
def visualize_convnet():
    sal_map_type = "Deconv_maxlogit"  # change it to get different visualizations
    image_name = 'tabby'  # or using a list to deal with multiple images
    max_pool = True  #indicate whether to add a max-pooling layer

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

    n_labels = 1000
    n_input = 224

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

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

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

    batch_size = batch_img.shape[0]

    # tf session
    sess = tf.Session()

    # construct the graph based on the gradient type we want
    # plain relu vs guidedrelu
    if sal_map_type.split('_')[0] == 'GuidedBackprop':
        eval_graph = tf.get_default_graph()
        with eval_graph.gradient_override_map({'Relu': 'GuidedRelu'}):
            conv_model = Convnet(sess,max_pool)
    # ADD DECONV
    elif sal_map_type.split('_')[0] == 'Deconv':
        eval_graph = tf.get_default_graph()
        with eval_graph.gradient_override_map({'Relu': 'DeconvRelu'}):
            conv_model = Convnet(sess,max_pool)
    elif sal_map_type.split('_')[0] == 'PlainSaliency':
        conv_model = Convnet(sess,max_pool)
    else:
        raise Exception("Unknown saliency type")

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

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

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

    for idx in range(batch_size):
        print_prob(probs[idx])
        visualize(sal_map_val[idx], sal_map_type, save_dir, fns[idx])
예제 #31
0
        #gb_grad = tf.gradients(cost, images)[0]
        gb_grad = tf.gradients(target_conv_layer, images)[0]

        # Normalizing the gradients
        target_conv_layer_grad_norm = tf.div(
            target_conv_layer_grad,
            tf.sqrt(tf.reduce_mean(tf.square(target_conv_layer_grad))) +
            tf.constant(1e-5))

        init = tf.global_variables_initializer()

# Run tensorflow

with tf.Session(graph=eval_graph) as sess:
    sess.run(init)

    prob = sess.run(vgg.prob, feed_dict={images: batch_img, train_mode: False})

    gb_grad_value, target_conv_layer_value, target_conv_layer_grad_value = sess.run(
        [gb_grad, target_conv_layer, target_conv_layer_grad_norm],
        feed_dict={
            images: batch_img,
            labels: batch_label,
            train_mode: True
        })

    for i in range(batch_size):
        utils.print_prob(prob[i], './synset.txt')
        utils.visualize(batch_img[i], target_conv_layer_value[i],
                        target_conv_layer_grad_value[i], gb_grad_value[i])
예제 #32
0
def main(_):
    worker_hosts = FLAGS.worker_hosts.split(",")

    # create the cluster configured by `ps_hosts' and 'worker_hosts'
    cluster = tf.train.ClusterSpec({"worker": worker_hosts})

    # create a server for local task
    server = tf.train.Server(cluster,
                             job_name=FLAGS.job_name,
                             task_index=FLAGS.task_index)

    if FLAGS.job_name == "ps":
        server.join()
    elif FLAGS.job_name == "worker":
        with tf.device(
                tf.train.replica_device_setter(
                    worker_device="/job:worker/task:%d" % (FLAGS.task_index),
                    cluster=cluster)):
            img1 = utils.load_image("./test_data/tiger.jpeg")
            img1_true_result = [1 if i == 292 else 0
                                for i in range(1000)]  # 1-hot result for tiger
            batch1 = img1.reshape((1, 224, 224, 3))

            images = tf.placeholder(tf.float32, [1, 224, 224, 3])
            true_out = tf.placeholder(tf.float32, [1, 1000])
            train_mode = tf.placeholder(tf.bool)

            vgg = vgg19.Vgg19('./vgg19.npy')
            vgg.build(images, train_mode)

            print(vgg.get_var_count())

            # The StopAtStepHook handles stopping after running given steps.
            hooks = [tf.train.StopAtStepHook(last_step=10000)]

            global_step = tf.train.get_or_create_global_step()
            optimizer = tf.train.AdamOptimizer(learning_rate=1e-04)

            if FLAGS.is_sync:
                # asynchronous training
                # use tf.train.SyncReplicasOptimizer wrap optimizer
                # ref: https://www.tensorflow.org/api_docs/python/tf/train/SyncReplicasOptimizer
                optimizer = tf.train.SyncReplicasOptimizer(
                    optimizer,
                    replicas_to_aggregate=FLAGS.num_workers,
                    total_num_replicas=FLAGS.num_workers)
                # create the hook which handles initialization and queues
                hooks.append(
                    optimizer.make_session_run_hook((FLAGS.task_index == 0)))

            loss = tf.reduce_sum((vgg.prob - true_out)**2)
            train_op = optimizer.minimize(
                loss,
                global_step=global_step,
                aggregation_method=tf.AggregationMethod.ADD_N)

            # The MonitoredTrainingSession takes care of session initialization,
            # restoring from a checkpoint, saving to a checkpoint, and closing when done
            # or an error occurs.
            with tf.train.MonitoredTrainingSession(
                    master=server.target,
                    is_chief=(FLAGS.task_index == 0),
                    checkpoint_dir="./checkpoint_dir",
                    hooks=hooks) as mon_sess:
                # mon_sess.run(tf.global_variables_initializer())
                while not mon_sess.should_stop():
                    _, prob, step = mon_sess.run(
                        [train_op, vgg.prob, global_step],
                        feed_dict={
                            images: batch1,
                            true_out: [img1_true_result],
                            train_mode: True
                        })
                    if step % 100 == 0:
                        print("Train step %d" % step)
                        utils.print_prob(prob[0], './synset.txt')