Пример #1
0
    # fully conv
    n.fc6, n.relu6 = conv_relu(n.conv5_3, 4096, ks=7, dilation=4)        
    n.fc7, n.relu7 = conv_relu(n.relu6, 4096, ks=1, pad=0)
    n.fc8 = L.Convolution(n.relu7, kernel_size=1, num_output=21)
    
    return n


if __name__ == '__main__':
    dilated_path = 'dilated_net_tmp.prototxt'
    dilated_weights = 'dilation8_pascal_voc.caffemodel'
    new_weights = 'simple_net.caffemodel'
    new_path = 'new_net_tmp.prototxt'
    
    dilated_netspec = create_dilated_net()
    new_netspec = simple_net('deploy', initialize_fc8=True)  
    save_net(dilated_path, str(dilated_netspec.to_proto()))
    save_net(new_path, str(new_netspec.to_proto()))
    dilated_net = caffe.Net(dilated_path, dilated_weights, caffe.TRAIN)
    new_net = caffe.Net(new_path, caffe.TRAIN)
    #transplant vgg-net conv weights
    transplant(new_net, dilated_net, 'c')
    #transplant fc6 weights
    new_net.params['fc6'][0].data[:, -512:][...] = dilated_net.params['fc6'][0].data
    new_net.params['fc6'][1].data[...] = dilated_net.params['fc6'][1].data
    #transplant fc7 weights
    new_net.params['fc7'][0].data[...] = dilated_net.params['fc7'][0].data
    new_net.params['fc7'][1].data[...] = dilated_net.params['fc7'][1].data
    new_net.save(new_weights)
    remove(dilated_path)
    remove(new_path)
def main():

    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.add_argument(
        "--device_id",
        help="Comma seperatted device IDs to run benchmark on.",
        type=int,
        default=0)
    parser.add_argument("--batch_size",
                        help="Batch size on each GPU",
                        type=int,
                        default=32)
    parser.add_argument("--num_classes",
                        help="Number of classes",
                        type=int,
                        default=100)
    parser.add_argument("--num_warmup",
                        help="Number of warm up iterations.",
                        type=int,
                        default=50)
    parser.add_argument("--num_iterations",
                        help="Number of benchmark iterations.",
                        type=int,
                        default=200)
    config = parser.parse_args()

    x = np.random.rand(config.batch_size, 224, 224, 3)
    y = np.random.randint(config.num_classes, size=(config.batch_size))

    with tf.device("/gpu:{}".format(config.device_id)):
        image = tf.placeholder(tf.float32,
                               shape=(config.batch_size, 224, 224, 3))
        label = tf.placeholder(tf.int32, shape=(config.batch_size))

        outputs = net.simple_net(image, config.batch_size, config.num_classes)

        loss = tf.losses.sparse_softmax_cross_entropy(labels=label,
                                                      logits=outputs)

        optimizer = tf.train.MomentumOptimizer(learning_rate=0.001,
                                               momentum=0.9)

        grads_and_vars = optimizer.compute_gradients(loss)
        minimize_op = optimizer.apply_gradients(grads_and_vars)

    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.90,
                                allow_growth=True)

    session_config = tf.ConfigProto(allow_soft_placement=False,
                                    log_device_placement=False,
                                    gpu_options=gpu_options)

    with tf.Session(config=session_config) as sess:
        sess.run(tf.global_variables_initializer())

        print("Warm up started.")
        for i_iter in range(config.num_warmup):
            sess.run(minimize_op, feed_dict={image: x, label: y})
            # sess.run([image, label], feed_dict={image: x, label: y})
        print("Warm up finished.")

        start_time = time.time()
        for i_iter in range(config.num_iterations):
            print("\rIteration: " + str(i_iter), end="")
            sess.run(minimize_op, feed_dict={image: x, label: y})
            # sess.run([image, label], feed_dict={image: x, label: y})
            sys.stdout.flush()
        end_time = time.time()

        total_time = end_time - start_time
        total_num_images = config.batch_size * config.num_iterations

        print("\nTotal time spend: " + str(total_time) + " secs.")
        print("Average Speed: " + str(total_num_images / total_time) +
              " images/sec.")
Пример #3
0
def main():

    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.add_argument(
        "--device_list",
        help="Comma seperatted device IDs to run benchmark on.",
        type=str,
        default="0")
    parser.add_argument("--batch_size_per_gpu",
                        help="Batch size on each GPU",
                        type=int,
                        default=32)
    parser.add_argument("--num_classes",
                        help="Number of classes",
                        type=int,
                        default=100)
    parser.add_argument("--num_warmup",
                        help="Number of warm up iterations.",
                        type=int,
                        default=50)
    parser.add_argument("--num_iterations",
                        help="Number of benchmark iterations.",
                        type=int,
                        default=200)
    config = parser.parse_args()

    config.device_list = list(map(int, config.device_list.split(",")))
    config.gpu_count = len(config.device_list)

    with tf.device("/cpu:0"):

        list_grads_and_vars = []

        # Map
        for split_id, device_id in enumerate(config.device_list):
            with tf.device(
                    assign_to_device("/gpu:{}".format(device_id),
                                     ps_device="/cpu:0")):

                images_batch = tf.constant(
                    1.0,
                    shape=[config.batch_size_per_gpu, 224, 224, 3],
                    dtype=tf.float32)
                labels_batch = tf.constant(1,
                                           shape=[config.batch_size_per_gpu],
                                           dtype=tf.int32)

                outputs = net.simple_net(images_batch,
                                         config.batch_size_per_gpu,
                                         config.num_classes)

                loss = tf.losses.sparse_softmax_cross_entropy(
                    labels=labels_batch, logits=outputs)

                optimizer = tf.train.MomentumOptimizer(learning_rate=0.001,
                                                       momentum=0.9)

                list_grads_and_vars.append(optimizer.compute_gradients(loss))

        ave_grads_and_vars = average_gradients(list_grads_and_vars)

        minimize_op = optimizer.apply_gradients(ave_grads_and_vars)

        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.90,
                                    allow_growth=True)

    session_config = tf.ConfigProto(allow_soft_placement=False,
                                    log_device_placement=False,
                                    gpu_options=gpu_options)

    with tf.Session(config=session_config) as sess:
        sess.run(tf.global_variables_initializer())

        print("Warm up started.")
        for i_iter in range(config.num_warmup):
            sess.run(minimize_op)
        print("Warm up finished.")

        start_time = time.time()
        for i_iter in range(config.num_iterations):
            print("\rIteration: " + str(i_iter), end="")
            sys.stdout.flush()
            sess.run(minimize_op)
        end_time = time.time()

        total_time = end_time - start_time
        total_num_images = (config.gpu_count * config.batch_size_per_gpu *
                            config.num_iterations)

        print("\nTotal time spend: " + str(total_time) + " secs.")
        print("Average Speed: " + str(total_num_images / total_time) +
              " images/sec.")
def train(char_set, batch_size, image_height, image_width, depth,
          captcha_size):
    '''
    训练函数
    :param char_set
    :param batch_size
    :param image_height:
    :param image_width:
    :param depth
    :param captcha_size: 验证码含有字符的个数
    :return:
    '''
    shape = [batch_size, image_height, image_width, depth]
    char_set_len = len(char_set)  #字符种类个数

    X = tf.placeholder(tf.float32, [None, image_height, image_width, depth])
    Y = tf.placeholder(tf.float32, [None, captcha_size * char_set_len])
    keep_prob = tf.placeholder(tf.float32)  # dropout

    # inputs = tf.reshape(X, shape=[-1, image_height, image_width, 1])
    inputs = X
    output = net.simple_net(inputs, captcha_size, char_set_len, keep_prob)
    # loss
    # loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(output, Y))
    loss = tf.reduce_mean(
        tf.nn.sigmoid_cross_entropy_with_logits(logits=output, labels=Y))
    # 最后一层用来分类的softmax和sigmoid有什么不同?
    # optimizer 为了加快训练 learning_rate应该开始大,然后慢慢衰
    optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)

    predict = tf.reshape(output, [-1, captcha_size, char_set_len])
    max_idx_p = tf.argmax(predict, 2)
    max_idx_l = tf.argmax(tf.reshape(Y, [-1, captcha_size, char_set_len]), 2)
    correct_pred = tf.equal(max_idx_p, max_idx_l)
    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())
        step = 0
        while True:
            batch_x, batch_y = create_dataset.get_next_batch(
                char_set, shape=shape, captcha_size=captcha_size)
            _, loss_ = sess.run([optimizer, loss],
                                feed_dict={
                                    X: batch_x,
                                    Y: batch_y,
                                    keep_prob: 0.75
                                })
            print("step:{},loss:{}".format(step, loss_))

            # 每100 step计算一次准确率
            if step % 10 == 0:
                batch_x_test, batch_y_test = create_dataset.get_next_batch(
                    char_set, shape=shape, captcha_size=captcha_size)
                acc = sess.run(accuracy,
                               feed_dict={
                                   X: batch_x_test,
                                   Y: batch_y_test,
                                   keep_prob: 1.
                               })
                print("------------------step:{},acc:{}".format(step, acc))
                # 如果准确率大于50%,保存模型,完成训练
                if acc > 0:
                    saver.save(sess,
                               "./model/crack_capcha.model",
                               global_step=step)
            if step == 50000:
                break
            step += 1